Building a Three.js Rainy Night 3D Scene with AI-Powered Coding Assistants

    In Web 3D development, turning a creative idea into an interactive, runnable scene often means wrestling with complex APIs, geometry math, and shader code. This article walks through building a "rainy night convenience store" scene in Three.js with the help of LLM-driven coding assistants, covering scene planning, prompt engineering, iterative code generation, performance optimization, and cloud deployment.

    In Web 3D development, transforming a fleeting creative idea into an interactive, runnable 3D scene often means overcoming obstacles like complex API memorization, tedious geometric calculations, and time-consuming shader writing. In recent years, as AI coding assistants powered by large language models have matured, developers can now use natural-language, multi-turn conversations with AI to quickly generate well-structured front-end code. This article uses the construction of a “rainy night convenience store” 3D scene as an example to walk through the full workflow—from concept to performance tuning—using an intelligent coding assistant.

    1. Scene Planning: Breaking Down the Core Technologies

    Before issuing instructions to an AI coding assistant, a developer needs the ability to decompose a high-level scene into concrete technical modules. An immersive “rainy night convenience store” scene in Three.js relies on several key technical pillars:

    1. Geometry and Materials: The store’s main structure (walls, roof, doors, and windows) can be assembled from basic geometries such as BoxGeometry. To capture a rainy-night look, the glass doors and windows should use physically based rendering (PBR) materials with appropriate refractive index and roughness settings, so they reflect the surrounding neon lights.
    2. Lighting and Shadows: The rainy-night atmosphere depends heavily on lighting. The scene needs ambient light as a base, point lights to simulate the store’s interior fluorescent lighting, and spotlights or colored point lights for the exterior neon signage. Light sources must cast shadows, and the shadow camera’s near and far clipping planes should be configured properly to preserve detail in dark areas.
    3. Particles and Post-Processing: Rain is typically implemented with a Points particle system, animating the fall either with a custom shader or by updating Y coordinates in the vertex shader. The ground needs a material with reflections to simulate the sheen of pooled rainwater. Adding post-processing passes such as UnrealBloomPass makes neon lights glow softly, greatly enhancing the cyberpunk or late-night-loneliness mood.

    2. AI Collaboration in Practice: Having an LLM Write Three.js Code

    Once the technical architecture is clear, collaboration with the AI assistant can begin. Along the way, developers need to master “prompt engineering”—and avoid the illusion of getting everything done in one shot.

    1. Build the Basic Skeleton

    First, ask the assistant to generate a standard Three.js project template containing a scene, camera, renderer, and a basic animation loop. Specify the module approach (e.g., ES Modules with Vite) and ask it to include OrbitControls for later interactive exploration.

    2. Add Scene Elements Incrementally

    Don’t try to describe every detail in a single prompt. Instead, iterate step by step:

    • Step 1: “Create a box at the center of the scene as the convenience store’s main body, roughly 10x4x6. Make the front face transparent glass and the rest a dark, rough material.”
    • Step 2: “Add a glowing red neon sign above the glass using MeshBasicMaterial and a point light. Add ambient and directional light to the scene, and enable shadows.”
    • Step 3: “Create a particle system with 5,000 thin, elongated particles randomly distributed in a range near the top of the scene, falling steadily along the negative Y axis and looping, to simulate rain.”

    The LLM can quickly produce corresponding JavaScript snippets. For example, the base scene structure looks like this:

    1. import * as THREE from 'three';
    2. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
    3. // Initialize the scene
    4. const scene = new THREE.Scene();
    5. scene.fog = new THREE.FogExp2(0x0a0a1a, 0.05); // Add fog to enhance the rainy-night mood
    6. // Initialize the camera
    7. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    8. camera.position.set(12, 6, 12);
    9. // Initialize the renderer
    10. const renderer = new THREE.WebGLRenderer({ antialias: true });
    11. renderer.setSize(window.innerWidth, window.innerHeight);
    12. renderer.shadowMap.enabled = true;
    13. document.body.appendChild(renderer.domElement);
    14. // Controls
    15. const controls = new OrbitControls(camera, renderer.domElement);
    16. // Render loop
    17. function animate() {
    18. requestAnimationFrame(animate);
    19. controls.update();
    20. renderer.render(scene, camera);
    21. }
    22. animate();

    3. Human Review and Fine-Tuning

    Although AI tools can generate large amounts of code, the results are not always perfect. Misaligned proportions, overexposed lighting, and unreasonable material parameters are common issues. Developers should run the generated code locally, observe the actual result, and feed specific feedback back to the tool—“the sign is too bright,” “the rain falls too fast”—to guide parameter corrections. This “generate–run–feedback–refine” loop is currently the best model for human-AI collaboration.

    3. Performance Optimization and Detail Polish

    Once the scene takes shape, performance bottlenecks often appear. A 3D scene suffering a frame-rate drop on low-end devices must be addressed. You can ask the AI assistant to help with performance analysis and apply common best practices:

    1. Geometry Instancing: If the scene contains many repeated elements (such as products on shelves or floor tiles), use InstancedMesh instead of creating individual meshes. This dramatically reduces draw-call counts.
    2. Shadow Resolution and Range Control: High-resolution shadow maps are extremely memory-hungry. Adjust the light’s shadow camera properties dynamically based on the camera’s view range so only objects in view cast shadows, and keep the shadow map size at 1024 or 2048.
    3. Shader Optimization: For rain particles, updating coordinates on the CPU can cause stuttering when particle counts are large. Move the position-update logic into a GPU vertex shader and let the GPU’s parallelism take the load off the CPU.

    4. Cloud Deployment and Asset Hosting

    After local development and tuning, the next step is putting the work on the internet. A complete front-end 3D application typically consists of HTML, JavaScript, and static assets like texture maps. For fast access and high availability, the best practice is to deploy these static assets in the cloud.

    Developers can host these files with a general-purpose object storage service: upload the bundled files to a bucket and enable static website hosting. To serve users across regions, pair it with a CDN (content delivery network) to cache static assets at edge nodes. Enterprise users who choose offerings such as Baidu AI Cloud’s object storage and CDN products can get stable high-bandwidth support and low-latency access, ensuring large textures in the 3D scene load quickly instead of leaving users staring at a blank page.

    5. Conclusion

    This “rainy night convenience store” project shows that AI coding assistants now work well as a developer’s “copilot,” quickly turning ideas in your head into runnable, interactive technical prototypes. While they cannot fully replace developers in architecture design, performance tuning, and aesthetic judgment, they demonstrate remarkable efficiency in generating boilerplate API code, configuring complex parameters, and iterating through trial and error. Going forward, mastering collaboration with large language models will be a key competitive skill for every Web 3D developer.