Building a Three.js Rainy Night 3D Scene with AI Coding Assistants
This article explores how to leverage AI coding assistants to rapidly prototype a complex Three.js 3D scene—a rainy night convenience store. It covers technical selection, translating natural language into runnable code, and overcoming common challenges such as scene scaling, performance optimization, and visual detail refinement. The article highlights the collaborative workflow between developers and AI to bridge the gap between a basic prototype and a polished 3D experience.
Introduction: AI-Empowered Rapid Prototyping for 3D Visualization
In modern web development, 3D visualization technology is becoming increasingly popular. Thanks to WebGL and its higher-level encapsulation libraries, developers can build complex interactive 3D scenes directly in the browser. However, building a 3D scene with a specific atmosphere from scratch often requires handling tedious geometric modeling, material tuning, lighting calculations, and particle system scripting, which presents a high barrier to entry and is time-consuming.
With the rapid development of Large Language Models (LLMs) and intelligent coding tools, developers can now use natural language to interact with AI through multiple rounds, quickly generating foundational code frameworks. This article takes the construction of a “rainy night convenience store” 3D scene as an example to explore how to use AI coding assistants for Three.js development. It also deeply analyzes the technical challenges encountered during this process—such as proportion control, performance optimization, and detail refinement—along with their corresponding solutions.
1. Scene Concept and Technical Selection
1.1 Visual Target Breakdown
A “rainy night convenience store” is a highly visual scene, with core visual elements including:
- Basic Environment: Dark-toned night environment with reflective, slippery ground.
- Main Architecture: A convenience store structure with glass display windows and a glowing signboard.
- Dynamic Elements: Continuously falling raindrops, possibly accompanied by faint streetlight halos.
- Lighting Atmosphere: A contrast between warm and cool light, where the warm light inside the store forms a visual focal point against the cold night rain outside.
1.2 Technical Approach
The mainstream web-based solution for achieving this scene is Three.js. It utilizes various geometries (such as BoxGeometry and PlaneGeometry) to build the outline of the convenience store; uses PBR (Physically Based Rendering) materials (MeshStandardMaterial or MeshPhysicalMaterial) to simulate glass and metal; constructs a custom particle system via BufferGeometry to achieve the raindrop effect; and combines post-processing passes like UnrealBloomPass to enhance the neon glow effect.
2. From Natural Language to Runnable Code
Developing with AI coding assistants is not a simple “one-click generation,” but an iterative process of “stating requirements - generating code - running validation - providing feedback for refinement.”
2.1 Basic Scene and Architecture Setup
In the initial phase, we first input basic prompts to the AI tool, for example: “Please use Three.js to create a night scene containing a box-shaped convenience store building. The building needs a glass-material front, with a warm-colored point light placed inside. The scene needs to initialize the camera and renderer.”
The tool can quickly return foundational code containing scene initialization, camera setup, and a render loop. However, upon direct execution, it often becomes apparent that parameters like the building’s geometric dimensions and the glass transparency do not reach the ideal state. This is the AI’s default behavior when lacking specific constraints.
2.2 Implementing the Rain Particle System
Next, we need to add dynamic raindrop effects. The prompt can be refined as: “Add a raindrop effect to the current scene. Please implement this using Three.js’s Points object and BufferGeometry. Set the number of raindrops to 5000, distributed within a space of width 20, height 30, and depth 20. The raindrops should move downward and reset to the top when they reach the ground.”
The AI will generate code snippets containing vertex position attributes and animation update functions. By integrating this snippet into the main render loop, a basic rainfall effect appears in the scene.
3. Bridging the Gap from Runnable to Usable
Although AI tools can quickly generate runnable code, there is still a gap between that and an “excellent interactive piece.” Developers need to face and resolve the following core issues:
3.1 Scene Scaling and Spatial Adaptation
When generating code, AI typically uses generic dimensional parameters, which can result in the convenience store being too large to fit entirely within the camera’s field of view, or the point light range being too small, causing the lighting to fail.
Solution Strategy: In subsequent conversations, explicit numerical constraints must be provided. For example, feedback like: “The proportions of the convenience store are incorrect. Please adjust the BoxGeometry dimensions to width=10, height=4, depth=8. Also, adjust the camera’s position to (0, 3, 15) so it can fully overlook the convenience store.” Through precise numerical fine-tuning, the scene proportions become much more coordinated.
3.2 Render Performance Optimization
When there are many particles in the scene, and complex PBR materials and post-processing bloom effects are enabled, frame drops are prone to occur on low-end devices.
Solution Strategy: This requires developers to have a certain understanding of 3D performance optimization and input it to the AI as a prompt. For example: “The current scene has a low frame rate in the browser. Please optimize the raindrop particle system. Reduce the size attribute of PointsMaterial and try using an object pool to reuse particles, avoiding frequent creation of new objects in each frame.” Additionally, you can ask the AI to add frustumCulled = true to cull objects outside the frustum, or lower the pixel ratio with renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) to balance performance.
3.3 Visual Detail Refinement and Multi-turn Communication
In realistic rainy night scenes, the ground usually has water puddle reflections. To enhance realism, you can have the AI supplement this detail through multi-turn communication. Prompt: “Please add a PlaneGeometry to the ground in front of the convenience store, use MeshStandardMaterial, set roughness to 0.1 and metalness to 0.9 to simulate the reflection effect of a wet ground.”
For glass materials, you can further request: “Adjust the glass material’s transmission property to 1.0 and thickness to 0.5 to give it a more realistic refraction effect.” Through this kind of prompt engineering based on professional 3D knowledge, the AI can output more accurate material parameters, significantly improving the visual expressiveness.
4. Conclusion: Creative Implementation through Human-AI Collaboration
Through the practice of building a Three.js rainy night convenience store scene, it is evident that AI coding tools are already highly suitable for quickly transforming ideas in a developer’s mind into genuinely runnable, interactive prototype code. It significantly lowers the threshold for initializing projects, freeing developers from tedious API lookups and boilerplate code writing.
However, AI is not an omnipotent “one-click rendering machine.” For proportion control, performance optimization, and handling subtle visual details, developers still need to intervene in the roles of “architect” and “art director.” Developers need a solid understanding of Three.js fundamentals to accurately diagnose performance bottlenecks and translate them into precise prompts fed back to the AI. Only through repeated communication and iteration can a 3D piece with both performance and aesthetics be ultimately polished. In the future, as human-AI collaboration models continue to mature, this rapid prototype validation process based on large models is bound to become the new norm for frontend 3D development.
