Building a Three.js Rainy Night City Scene with AI Coding Assistants

    Rainy night scenes are visually striking but notoriously hard to render in real time, pushing WebGL against multiple performance bottlenecks. This article walks through building a Three.js rainy night city as a set of independently verifiable modules: base scene and volumetric fog, a batched rain particle system, wet-ground reflections, neon lighting with emissive materials, and bloom post-processing. It includes working code examples, concrete tuning advice for the parameters that never feel right on the first try, and a practical workflow for collaborating with an AI coding assistant across iterative rounds. It closes with a prioritized performance optimization checklist and a list of common pitfalls around transparency sorting, bloom thresholds, and reflection cost.

    1. Why Rainy Night Scenes Are Both Captivating and Hard to Build

    Rainy nights are a classic “atmosphere hotspot” in real-time rendering: wet pavement reflecting sign glow, raindrops lit up under street lamps, and distant buildings fading in and out of fog. This look almost naturally carries a cinematic feel — which is exactly why it hits several of WebGL’s biggest performance traps at once: large numbers of semi-transparent particles, expensive reflections, multiple dynamic lights, and the fill-rate pressure of stacking several post-processing passes.

    Push all of these effects in at once, and the scene can easily drop to a dozen frames per second on mobile or integrated-graphics devices. So the sensible approach isn’t to “hand-write everything from scratch” — it’s to split the scene into modules you can validate independently, then implement and stress-test them one at a time. This is also where an AI coding assistant delivers the most value: it can quickly generate an initial implementation of a given module, and you iterate from there instead of starting from a blank file.

    2. First, Break Down the Scene: Turning “a Small City” into a Module Checklist

    Before writing the first line of code, break the goal into clear modules — either mentally or while talking to your coding assistant:

    1. Base scene: camera, renderer, color management, and fog.
    2. Static buildings: the convenience store body, shelves, windows, and signs.
    3. Rain particle system: vertically falling raindrops.
    4. Wet-ground reflections: ground material and reflected imagery.
    5. Lighting and neon: emissive materials plus point/spot lights.
    6. Post-processing: bloom and tone mapping.
    7. Performance and interaction: frame-rate monitoring and a rotatable view.

    The benefit of this breakdown is that each module can be rendered, tuned, and stress-tested on its own. When something goes wrong, the scope of your investigation is confined to a small block of code — rather than staring at a full black screen with nowhere to start.

    3. Core Module Implementation Approaches

    3.1 Base Scene and Volumetric Fog

    The “can’t see far away” quality of a rainy night is itself a source of atmosphere, and fog serves double duty here: it deepens the scene while obscuring distant detail to save rendering cost. Exponential fog is the recommended choice:

    1. const scene = new THREE.Scene();
    2. scene.background = new THREE.Color(0x0a0d14);
    3. scene.fog = new THREE.FogExp2(0x0a0d14, 0.045);
    4. const camera = new THREE.PerspectiveCamera(
    5. 55, window.innerWidth / window.innerHeight, 0.1, 200
    6. );
    7. const renderer = new THREE.WebGLRenderer({ antialias: true });
    8. renderer.setSize(window.innerWidth, window.innerHeight);
    9. renderer.outputColorSpace = THREE.SRGBColorSpace;
    10. renderer.toneMapping = THREE.ACESFilmicToneMapping;
    11. renderer.toneMappingExposure = 1.1;
    12. document.body.appendChild(renderer.domElement);

    The fog density parameter needs repeated fine-tuning: too high, and even nearby signs turn gray; too low, and the distant buildings look harsh and flat. These “feel” parameters often can’t be nailed in a single generation — which is exactly the kind of thing you resolve through multiple rounds of dialogue.

    3.2 Rain Particle System

    Raindrops are essentially a large number of thin line segments. Creating a separate Mesh for every drop instantly produces tens of thousands of draw calls, so you must go the batching route. There are two mainstream approaches:

    • Use THREE.Points with a custom shader that stretches points into raindrops, easily scaling to tens of thousands.
    • Use THREE.LineSegments, where each drop is a short line segment and the geometry is built once.

    Below is a minimal LineSegments-based implementation that manages thousands of raindrops by filling the buffers once:

    1. const RAIN_COUNT = 8000;
    2. const positions = new Float32Array(RAIN_COUNT * 6); // two endpoints per raindrop
    3. const velocities = new Float32Array(RAIN_COUNT);
    4. for (let i = 0; i < RAIN_COUNT; i++) {
    5. const x = (Math.random() - 0.5) * 60;
    6. const y = Math.random() * 30;
    7. const z = (Math.random() - 0.5) * 60;
    8. const o = i * 6;
    9. positions[o] = x; positions[o + 1] = y; positions[o + 2] = z;
    10. positions[o + 3] = x; positions[o + 4] = y - 0.6; positions[o + 5] = z;
    11. velocities[i] = 0.2 + Math.random() * 0.3;
    12. }
    13. const rainGeo = new THREE.BufferGeometry();
    14. rainGeo.setAttribute('position', new THREE.BufferAttribute(positions, 3));
    15. const rain = new THREE.LineSegments(
    16. rainGeo,
    17. new THREE.LineBasicMaterial({ color: 0x8899aa, transparent: true, opacity: 0.5 })
    18. );
    19. scene.add(rain);
    20. // Fall each frame and reset to the top when hitting the ground
    21. function updateRain(dt) {
    22. for (let i = 0; i < RAIN_COUNT; i++) {
    23. const o = i * 6;
    24. let dy = velocities[i];
    25. positions[o + 1] -= dy;
    26. positions[o + 4] -= dy;
    27. if (positions[o + 1] < 0) {
    28. positions[o + 1] = 30;
    29. positions[o + 4] = 29.4;
    30. }
    31. }
    32. rainGeo.attributes.position.needsUpdate = true;
    33. }

    The key point is that all raindrops share a single geometry and a single material, so there is only one draw call. Particle count, fall speed, opacity, and segment length are all parameters you can tune through dialogue — you can ask the assistant for the right value ranges for “heavy, driving rain” versus “a fine, lingering drizzle.”

    3.3 Wet Ground and Reflections

    Wet-ground reflections are the soul of a rainy night scene. To cheaply achieve that “soaked” look, there are two layers:

    • Material layer: use the physical MeshStandardMaterial, lower the ground’s roughness and raise its metalness, so ambient light forms highlight bands on the ground for a wet, reflective feel.
    • Reflection layer: if you want signs and lights to genuinely reflect on the ground, you can use planar reflection, rendering the scene into a texture and projecting it back onto the ground. This kind of reflection is relatively costly and usually requires lowering reflection resolution and limiting the set of reflected objects to trade for frame rate.

    A pragmatic compromise is to have the ground catch light with a low-roughness material, then layer on textures or simple fake reflections, instead of doing real-time reflection for the whole scene. For a close-up-focused scene like a convenience store, the former is often already convincing enough to pass as real.

    3.4 Lighting and Neon Signs

    The strong contrast between warm lighting and the cool night is central to the atmosphere. The approach is:

    • Use emissive materials on the store interior and signs so they “glow on their own.”
    • Add a few point lights (under the sign, at the entrance) to cast light onto the wet ground.
    • Keep the number of lights restrained — too many dynamic lights noticeably slow down rendering. Think along the lines of baking: use emissive materials and post-process bloom to “fake” complex lighting.
    1. const sign = new THREE.Mesh(
    2. new THREE.BoxGeometry(6, 1.2, 0.2),
    3. new THREE.MeshStandardMaterial({
    4. color: 0xffffff,
    5. emissive: 0xff4488,
    6. emissiveIntensity: 2.5
    7. })
    8. );
    9. const signLight = new THREE.PointLight(0xff4488, 2, 12, 2);
    10. signLight.position.set(0, 4, 2);
    11. scene.add(sign, signLight);

    3.5 Post-Processing: Making the Neon Actually Glow

    Emissive materials only make things brighter by themselves; the halo that truly makes neon “spread” comes from post-processing. The standard approach is to set up a rendering pipeline, add a bloom pass, and take the regions above a certain brightness threshold, blur them, and add them back over the image:

    1. // Example: the Render -> Bloom -> Output pass combination
    2. composer.addPass(new RenderPass(scene, camera));
    3. const bloomPass = new UnrealBloomPass(
    4. new THREE.Vector2(window.innerWidth, window.innerHeight),
    5. 1.2, // strength: bloom intensity
    6. 0.6, // radius: spread radius
    7. 0.85 // threshold: brightness threshold
    8. );
    9. composer.addPass(bloomPass);

    Bloom strength, radius, and threshold all need to be tuned together with the overall exposure: too much strength washes the whole frame into a blur, while too low a threshold makes even ordinary highlights glow. This is yet another classic “tweak it by eye, over and over” parameter.

    4. How to Collaborate Effectively with AI Coding Assistants

    As the breakdown above shows, almost no part of a complete rainy night scene is “correct on the first generation” — fog density, rain intensity, and bloom strength all need repeated adjustment. What an AI coding assistant is genuinely good at is continuously speeding things up within this iterative loop. Here are a few proven collaboration tips:

    1. Advance one module at a time. Don’t start with “give me a rainy night city.” First ask for “a rain particle system that actually works,” and once it’s verified, add the ground, lighting, and post-processing step by step. The smaller the scope, the more controllable the result.

    2. Describe “feel” as quantifiable requirements. Instead of saying “make it a bit prettier,” say “denser, faster raindrops, tighter bloom, and an overall darker image.” The more specific the description, the more predictable the changes.

    3. Have the assistant explain rather than pile up code. Ask it to explain the key buffer management, shader approach, or performance trade-offs, so you can judge whether the code actually holds up in your own scene.

    4. Feed it both the errors and the symptoms. When rendering goes wrong (black screen, flickering, frame drops), describe the console errors, the relevant code snippets, and the expected result all together — far more efficient than just tossing it a “doesn’t work.”

    5. Accept that proportion, performance, and detail need back-and-forth polishing. Visual projects like this are iterative by nature; don’t expect to get it right in one shot. The assistant compresses the time of each round — it doesn’t eliminate iteration itself.

    5. Performance Optimization Checklist

    For optimizing a rainy night scene, work through the following in order of priority:

    • Batching and instancing: merge geometry or use instanced rendering for repeated objects like raindrops, shelves, and street lamps to drive draw calls as low as possible.
    • Control particle count: dynamically adjust the number of raindrops based on device capability, actively scaling down on low-end devices.
    • Restrain dynamic lights: use emissive + bloom to simulate complex lighting and reduce real-time point lights.
    • Enable post-processing on demand: the bloom pass carries a noticeable cost, so on mobile you can disable it or lower its resolution.
    • Occlude distant scenery with fog: use exponential fog to hide far-off objects and avoid rendering unnecessary detail.
    • Adaptive resolution: dynamically adjust render resolution based on frame rate, prioritizing smoothness.

    6. Common Pitfalls

    • Transparency sorting: heavy overlap of semi-transparent raindrops can cause flickering; you can disable depth writing or adjust render order.
    • Bloom “eating” the shadows: a poorly set threshold can flatten the depth of the night scene, so it needs to be tuned together with exposure.
    • Reflections tanking the frame rate: full-scene real-time reflection is often not worth it; for close-up scenes, prioritize the material-level wet feel first.
    • Parameters looking “plastic”: when ground roughness, light intensity, and similar settings aren’t tuned well, the image looks fake — and the essence of a rainy night lies precisely in the restraint of these details.

    7. Conclusion

    Building a Three.js rainy night city with an AI coding assistant is less about “generating everything in one sentence” and more about breaking a complex scene into independently verifiable modules, then using the assistant to get off the ground quickly and refine each part — particle rain, wet-ground reflections, neon bloom, and fog post-processing. When you translate subjective impressions like “the fog is too thick, the rain too harsh, the light too diffuse” into tunable parameters such as density, speed, intensity, and threshold, and let the assistant keep iterating around those parameters, the vision in your head genuinely becomes a rainy night city you can rotate, gaze at, and actually run. This methodology — “modular breakdown + iterative collaboration + performance first” — applies equally to any real-time 3D scene creation that demands high atmosphere and high performance.