Building a Three.js Rainy Night City Scene with Large Language Models

    This article explores how developers can leverage Large Language Models (LLMs) and AI coding tools to efficiently build interactive 3D web scenes using Three.js. Using a "Rainy Night City" scene as a practical example, it details the core technical elements required, the human-AI collaboration workflow from initial prompts to code generation, and best practices for iterating on geometry scaling, performance optimization, and material refinement.

    In traditional Web3D development, turning a conceptual idea into an interactive, browser-run 3D scene often requires tedious code writing, parameter tuning, and resource integration. However, with the deep integration of Large Language Models (LLMs) and intelligent coding tools, developers can skip much of the foundational boilerplate code and jump directly into core logic construction and effect optimization. This article uses building a “Rainy Night Small City” Three.js scene as an example to explore how to efficiently transition from concept to a runnable prototype through human-AI collaboration.

    I. Core Technical Elements of a Rainy Night City Scene

    Before collaborating with intelligent coding tools, developers must first define the core visual and technical elements of the scene in their minds. The visual impact of a “rainy night small city” with a cyberpunk or tranquil vibe primarily relies on Three.js technical implementations across several dimensions:

    1. Complex Lighting System: Night scenes require extremely low ambient light combined with multiple local light sources. Neon signs of convenience stores (point lights or spotlights), cold-colored streetlights, and faint glows reflecting off wet ground all need to be achieved through reasonable light parameter configurations.
    2. Particle System for Rain Effects: The core of a rainy night lies in the continuous rain. In Three.js, this is typically simulated using THREE.Points combined with a custom ShaderMaterial, or using BufferGeometry to generate a large number of vertically falling line segments or points to mimic real raindrops.
    3. Materials and Reflections: Wet asphalt roads and glass storefronts require strong reflective properties. This requires developers to properly set the metalness and roughness parameters of MeshStandardMaterial, and potentially introduce environment maps (PMREMGenerator) to enhance physical realism.

    II. Human-AI Collaboration Workflow: From Prompt to Code Generation

    Collaborating with AI coding tools is not a simple “Q&A” process, but a closed-loop process involving requirement decomposition, progressive generation, and logic validation.

    1. Requirement Decomposition and Initial Architecture Generation

    In the initial stage, you cannot expect a single vague prompt to generate a perfect scene. Developers should first ask the tool to generate the basic Three.js skeleton code, including scene initialization, perspective camera configuration, WebGL renderer setup, and a basic animation render loop. For example, we can prompt the tool: “Please help me initialize a Three.js scene, set up a perspective camera suitable for overlooking a small city, add OrbitControls for mouse dragging, and configure anti-aliasing rendering.”

    2. Building Scene Geometry and Materials

    Once the skeleton code is runnable, gradually add entities to the scene. Through step-by-step instructions, ask the tool to generate geometry for convenience store buildings, streets, streetlights, etc.

    1. // Example code: guide the tool to generate the basic structure of a convenience store
    2. const storeGeometry = new THREE.BoxGeometry(10, 5, 8);
    3. const storeMaterial = new THREE.MeshStandardMaterial({
    4. color: 0x808080,
    5. roughness: 0.8
    6. });
    7. const store = new THREE.Mesh(storeGeometry, storeMaterial);
    8. scene.add(store);

    At this stage, the tool can quickly translate natural language into corresponding API calls, significantly reducing the time spent looking up documentation.

    3. Visual Rendering of the Rainy Night Atmosphere

    Next, developers need to use precise prompts to guide the tool to add light sources and particle effects. For example: “Please add a low-intensity ambient light to this scene, and add a blue-green point light at the convenience store sign. Meanwhile, use THREE.Points to generate a rain effect system containing 2000 particles, requiring the particles to move up and down on the Y-axis, returning to the top when hitting the bottom.”

    III. Pitfalls and Iteration: Scale, Performance, and Details

    Although AI coding tools can quickly generate usable code, in actual experience, developers will find that the generated scenes often have flaws in scale, performance, and details. This is the critical stage requiring “repeated communication” and human intervention.

    1. Correcting Geometry Scale

    LLMs do not always perceive physical world dimensions accurately. The generated convenience store might look like a flat box, or the streetlight might tower into the clouds. Developers need to issue adjustment instructions to the tool based on visual feedback, such as: “Increase the width of the convenience store by 1.5 times and reduce the height by 20% to make it look more like a real-life street shop.” Through this conversational fine-tuning, the model’s cognitive bias regarding spatial proportions is gradually corrected.

    2. Runtime Performance Optimization

    Once thousands of rain particles and multiple light sources with shadow calculations are introduced, the page frame rate often drops drastically. Performance bottlenecks are a common pain point in 3D development. At this point, developers should ask the intelligent tool to step in for performance optimization:

    • DrawCall Optimization: Merge static buildings into a single Mesh to reduce render state switches.
    • Shadow Calculation Downgrade: Ask the tool to reduce the shadow map resolution from 2048 to 1024, or limit the rendering distance of shadows.
    • Particle System Optimization: Optimize the displacement calculation of raindrops at the Shader level, avoiding updating the position matrices of a massive number of particles frame by frame in the JavaScript main thread.

    3. Detail Material Polishing

    Details determine the immersion of the scene. The initial materials generated by AI are usually quite stiff, and developers need to guide it to apply more advanced textures and material properties. For example: “Reduce the roughness of the street plane material to 0.3, increase the metalness to 0.6 to simulate the wet and slippery reflection effect after rain; and add transparency (opacity: 0.4) and refraction effects to the convenience store’s glass window.” Through these targeted prompts, the realism of the scene will experience a qualitative leap.

    IV. Summary and Outlook

    The process of using intelligent coding tools and LLMs to build a Three.js rainy night small city proves that such tools are already highly suitable for quickly turning ideas in developers’ minds into genuinely runnable, playable works. It takes on a large amount of tedious API memorization and template writing, allowing developers to focus their energy on creative conception, effect optimization, and logic control.

    However, this also raises new requirements for developers: we need to possess clearer architectural thinking and requirement decomposition skills. Only through structured, logically rigorous prompt inputs can we guide models to generate high-quality code. In the future, as general cloud service capabilities (such as object storage, container platforms, etc.) and edge AI computing power further integrate, this “human-AI co-creation” development model will inevitably become the norm in Web3D and even the entire frontend domain, further lowering the barrier to innovation and unlocking developer productivity.