Build a Rainy-Night 3D City in Three.js with an AI Coding Assistant: A Practical Guide
This guide walks through building a rainy-night 3D scene in Three.js with the help of an AI coding assistant. It covers the three rendering pillars that create atmosphere—rain particles, night-and-neon lighting, and fog with reflections—then provides a minimal runnable skeleton, strategies for iterating effectively with an AI tool, key performance and detail optimizations, and how to publish and host the finished scene. Rather than relying on one-shot generation, the article shows how to break aesthetic goals into concrete, measurable instructions and move quickly from idea to a scene that actually runs, looks good, and plays well.
Building a rainy-night city in code on the web has always been a compelling challenge in creative programming. Neon reflections, dense rain, and hazy fog layered together test both geometric modeling and your control over light and atmosphere. This used to demand solid graphics and rendering experience, but with an intelligent coding tool you can now turn a natural-language description directly into runnable Three.js code, then refine it by observing the results. This article stays tool-agnostic and focuses on this general technical practice: how to reliably produce a rainy-night scene that runs, looks right, and plays well—and how to communicate effectively with the AI during iteration.
Three Technical Pillars of Atmosphere
A good-looking rainy-night scene fundamentally depends on three rendering elements working together:
- Particle system (rain): Rain is the “dynamic base” of the scene. It is usually built with
THREE.Pointspaired withBufferGeometryto store a large number of particle positions, updating the Y coordinate each frame to simulate falling, and resetting to the top once particles hit the ground to form a loop. For more convincing rain streaks, you can switch toTHREE.LineSegmentsto draw short line segments, or stretch particles in a shader. - Lighting system (night and neon): The core of a night scene is “a dark environment plus a few bright local lights.” Keep ambient light at very low intensity to preserve the shadows, render neon signs through the
emissiveproperty ofMeshStandardMaterialor self-illuminated materials, and add a fewPointLights to create pools of light on the ground. - Fog and reflection (the wet feel):
FogExp2exponential fog quickly opens up spatial depth, letting distant buildings fade into the rain. For the ground, use a material with higher metalness and lower roughness to simulate a slick, reflective surface, so the neon appears “soaked” into the ground.
Missing any one of these makes the image look dry or plasticky—and this is usually the part that needs the most manual intervention after the AI produces a first version.
A Minimal Runnable Skeleton
Here is the minimal skeleton for a rainy-night scene. An AI tool can usually generate this kind of basic structure accurately in one pass:
import * as THREE from 'three';const scene = new THREE.Scene();scene.fog = new THREE.FogExp2(0x0a0a1a, 0.035); // Exponential fog for rainy-night depthconst camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 1000);camera.position.set(0, 3, 12);const renderer = new THREE.WebGLRenderer({ antialias: true });renderer.setSize(innerWidth, innerHeight);renderer.setPixelRatio(Math.min(devicePixelRatio, 2)); // Cap pixel ratio to balance performancedocument.body.appendChild(renderer.domElement);// Very low-intensity ambient light to preserve shadowsscene.add(new THREE.AmbientLight(0x223344, 0.3));const ground = new THREE.Mesh(new THREE.PlaneGeometry(80, 80),new THREE.MeshStandardMaterial({ color: 0x05060a, metalness: 0.8, roughness: 0.25 }));ground.rotation.x = -Math.PI / 2;scene.add(ground);
You can organize the rain particle update like this:
const RAIN_COUNT = 12000;const pos = new Float32Array(RAIN_COUNT * 3);for (let i = 0; i < RAIN_COUNT; i++) {pos[i * 3 + 0] = (Math.random() - 0.5) * 60; // xpos[i * 3 + 1] = Math.random() * 40; // ypos[i * 3 + 2] = (Math.random() - 0.5) * 60; // z}const rainGeo = new THREE.BufferGeometry();rainGeo.setAttribute('position', new THREE.BufferAttribute(pos, 3));const rain = new THREE.Points(rainGeo, new THREE.PointsMaterial({color: 0x99aabb, size: 0.08, transparent: true, opacity: 0.6,}));scene.add(rain);function animate() {requestAnimationFrame(animate);for (let i = 1; i < pos.length; i += 3) {pos[i] -= 0.6; // Fall each frameif (pos[i] < 0) pos[i] = 40; // Reset to the top}rainGeo.attributes.position.needsUpdate = true;renderer.render(scene, camera);}animate();
This skeleton reproduces reliably in almost any intelligent coding tool. What actually determines the quality of the result is the iterative refinement you do with the AI afterward.
Iterative Dialogue: How to Communicate with the AI
The difficulty of a rainy-night scene is not “can it be written” but “are the details right.” In real development, things rarely land perfectly on the first try—proportions, performance, and details usually take repeated back-and-forth. The key to efficient iteration is breaking vague aesthetic requests into clear, actionable instructions for the AI:
- Establish the framework first, then work on details: In the first version, ask only for something that “runs, has the camera positioned correctly, and has ground and rain.” Once that is confirmed, add neon signs, convenience stores, pedestrians, and other elements one at a time. Avoid piling on too much at once, which makes problems hard to locate.
- Quantify instead of describing feelings: Don’t say “the building proportions look weird”; instead point out “this building is twice as wide as it is tall—I’d like it closer to square.” Don’t just say “it’s laggy”; instead provide constraints like “12,000 particles, target device is an ordinary laptop” so the AI can address the real issue.
- Change only one dimension at a time: Asking to adjust proportions, add lights, and change the color scheme all at once makes it hard for the AI to tell where a change came from. Submitting single-point changes and verifying each one before moving on is the most communication-efficient approach.
- Define boundaries and constraints explicitly: For example, “use instanced rendering for all reusable geometry” or “halve the particle count on mobile.” Write the performance budget into the requirements so the AI doesn’t produce an unusable demo.
The essence of this methodology is moving the developer’s judgment forward into the requirements description—the AI produces code quickly, and the human is responsible for aesthetic judgment, proportions, and playability.
Performance and Detail Optimization
The biggest performance killer in a rainy-night scene is overdraw. The following techniques should be written proactively into the constraints you give the AI:
- Instanced rendering: The thousands of windows and street-light points in a city, if each is created as a separate mesh, generate a massive number of draw calls. Batch-drawing them with
THREE.InstancedMeshis the standard approach:
const windows = new THREE.InstancedMesh(new THREE.PlaneGeometry(0.6, 0.8),new THREE.MeshBasicMaterial({ color: 0xffd27f }), // Emissive warm glowTOTAL_WINDOWS);const dummy = new THREE.Object3D();let idx = 0;for (let b = 0; b < buildings; b++) {for (let w = 0; w < rows * cols; w++) {dummy.position.set(px, py, pz);dummy.updateMatrix();windows.setMatrixAt(idx++, dummy.matrix);}}windows.instanceMatrix.needsUpdate = true;scene.add(windows);
- Cap the pixel ratio:
Math.min(devicePixelRatio, 2)prevents the rendering load from doubling on high-DPI screens. - Use reflections sparingly: True ground reflections can be done with a planar reflection approach, but it re-renders the scene once, which is a significant cost. In practice, a common compromise is “fake reflections via textures plus a low-roughness material,” which keeps the wet look without sacrificing frame rate.
- Tier the particles: Dynamically adjusting the particle count based on the device or viewport is a reliable strategy for balancing image quality and smoothness.
Publishing and Hosting
A Three.js scene is essentially pure static frontend assets, and its build output is a set of HTML, JavaScript, and static asset files. To publish, you can upload these files to object storage with static-site hosting enabled, or deploy them to a container platform where a backend serves them; after going live, connect a logging service and monitoring alerts to track metrics like load time and blank-screen rate, forming a complete loop from development to runtime. With general-purpose cloud capabilities such as object storage, containers, and monitoring—for example those offered by Baidu AI Cloud—these frontend 3D works can be served reliably without building your own infrastructure.
Conclusion
Building a rainy-night city with an AI coding tool is valuable not because of “one-click generation,” but because it compresses the creative-validation cycle from days to hours. What truly determines the quality of the result is whether the developer can break atmosphere down into quantifiable technical metrics like particles, lighting, and fog, whether they can precisely express their intent during iteration, and whether they have an engineering awareness of performance. Once you master the collaboration pattern of “framework first, details next, performance under control,” any 3D idea in your head can be quickly turned into a real, playable web work.
