Building a Three.js Rainy Night Scene with AI Coding Tools: Lighting, Rain, Reflections, and Mobile Performance
This article walks through building a rainy-night Three.js scene step by step, from breaking the concept down into computable elements to assembling a minimal runnable loop. It covers camera and renderer setup, warm and cool lighting with emissive neon signs, delta-time-based rain particles with click-to-switch intensity, wet-ground reflections, post-processing with UnrealBloomPass and OutputPass, and a mobile performance degradation checklist. It also provides a structured prompt template for AI coding tools along with an acceptance checklist for verifying that the result runs correctly.
First, the questions readers ask most: how to build a Three.js rainy-night effect, how to handle common errors, and whether it can be interactive
People search for things like “how to make a Three.js rainy-night effect,” “how to build a rainy-night convenience store,” and “how to build a rainy-night small town.” Along the way, they run into a black screen, rain that looks like square blocks, frame drops on mobile, blown-out bloom, and ground surfaces that don’t read as wet pavement. This article is not aimed at “one-click generation.” Instead, it offers a practical path that can be verified: first decompose the scene, then establish a minimal runnable loop, then gradually add lighting, rain effects, reflections, and post-processing, and finally use AI coding tools to speed up iteration.
Scenarios that fit this article include a rainy-night convenience store, a rainy-night small town, a rainy-night street corner, neon signs, wet pavement, and mobile WebGL showcases. Ideally, readers end up with a Three.js page that opens, runs, lets you drag the camera, lets you click to switch rain intensity, and runs smoothly in ordinary laptop and mobile browsers.
Decomposing the scene: turning “rainy night” into computable elements
Before handing the requirements to an AI coding tool, break the sensory description into technical elements. This meaningfully improves communication efficiency. When the elements are clearly enumerated, the first-pass generation success rate tends to go up, but you still need runtime verification, performance verification, and manual calibration of proportions.
A complete rainy-night scene typically includes:
- Spatial structure: streets, sidewalks, storefront facades, signs, street lamps, building blocks.
- Lighting system: warm interior light, cool street lights, neon emissive materials, faint ambient light.
- Weather effects: falling rain streaks, a sense of angled wind, rain haze, aerial perspective.
- Material appearance: wet pavement, low roughness with strong highlights, metallic feel, glass reflections.
- Atmospheric layers: fog, bloom halos, depth of field, warm–cool contrast.
- Interaction: OrbitControls camera control, click-to-switch rain intensity, resize handling.
Once these elements are listed clearly, break the task into modules such as “scene skeleton,” “rain effects,” “reflections,” “post-processing,” and “performance fallback.” The code the AI generates will be much closer to something that runs.
The minimal runnable loop: from initialization to rendering
Many examples show only scene initialization or an animation function without a render loop or window adaptation, so they don’t run when copied. The minimal runnable loop below includes the scene, camera, renderer, fog, lights, ground, a simple shop, rain effects, OrbitControls, click-to-switch rain intensity, resize handling, and post-processing.
Version note: a relatively recent Three.js is recommended, for example r152 or later. In newer versions, the recommended import path is three/addons instead of three/examples/jsm. If you use an older version, keep the import path as three/examples/jsm. For post-processing, OutputPass is recommended to handle the output color space, and renderer.outputColorSpace should be set explicitly.
import * as THREE from 'three';import { OrbitControls } from 'three/addons/controls/OrbitControls.js';import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';const scene = new THREE.Scene();scene.background = new THREE.Color(0x05060d);scene.fog = new THREE.FogExp2(0x0a0a12, 0.018);const camera = new THREE.PerspectiveCamera(60,window.innerWidth / window.innerHeight,0.1,1000);camera.position.set(8, 5, 14);const renderer = new THREE.WebGLRenderer({ antialias: true });renderer.setSize(window.innerWidth, window.innerHeight);renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));renderer.toneMapping = THREE.ACESFilmicToneMapping;renderer.toneMappingExposure = 1.1;renderer.outputColorSpace = THREE.SRGBColorSpace;document.body.appendChild(renderer.domElement);const controls = new OrbitControls(camera, renderer.domElement);controls.target.set(0, 2, 0);controls.enableDamping = true;scene.add(new THREE.AmbientLight(0x223344, 0.3));const warmLight = new THREE.PointLight(0xffaa55, 2, 20, 2);warmLight.position.set(3, 4, 0);scene.add(warmLight);const moonLight = new THREE.DirectionalLight(0x4466aa, 0.4);moonLight.position.set(-5, 10, -5);scene.add(moonLight);const groundMaterial = new THREE.MeshStandardMaterial({color: 0x0a0a0f,roughness: 0.15,metalness: 0.6});const ground = new THREE.Mesh(new THREE.PlaneGeometry(80, 80),groundMaterial);ground.rotation.x = -Math.PI / 2;scene.add(ground);const shopMaterial = new THREE.MeshStandardMaterial({color: 0x222233,roughness: 0.8});const shop = new THREE.Mesh(new THREE.BoxGeometry(6, 4, 4),shopMaterial);shop.position.set(0, 2, -4);scene.add(shop);const signMaterial = new THREE.MeshStandardMaterial({color: 0x111111,emissive: 0xff3366,emissiveIntensity: 2});const sign = new THREE.Mesh(new THREE.PlaneGeometry(4, 1.2),signMaterial);sign.position.set(0, 4.2, -1.95);scene.add(sign);const rainCount = 8000;const positions = new Float32Array(rainCount * 3);for (let i = 0; i < rainCount; i++) {positions[i * 3] = (Math.random() - 0.5) * 40;positions[i * 3 + 1] = Math.random() * 30;positions[i * 3 + 2] = (Math.random() - 0.5) * 40;}const rainGeo = new THREE.BufferGeometry();rainGeo.setAttribute('position',new THREE.BufferAttribute(positions, 3));const rainMaterial = new THREE.PointsMaterial({color: 0xaaccff,size: 0.08,transparent: true,opacity: 0.6,depthWrite: false});const rain = new THREE.Points(rainGeo, rainMaterial);scene.add(rain);let rainLevelIndex = 1;const rainLevels = [0.25, 0.6, 1.0];let currentRainCount = Math.floor(rainCount * rainLevels[rainLevelIndex]);rainGeo.setDrawRange(0, currentRainCount);let rainSpeed = 18;function updateRain(delta) {const pos = rainGeo.attributes.position.array;const wind = 2.2;for (let i = 0; i < currentRainCount; i++) {const xIndex = i * 3;const yIndex = i * 3 + 1;pos[yIndex] -= rainSpeed * delta;pos[xIndex] += wind * delta;if (pos[yIndex] < 0) {pos[yIndex] = 30;// On reset, change only y and keep the current x/z so the rain curtain doesn't jump}if (pos[xIndex] > 20) pos[xIndex] = -20;if (pos[xIndex] < -20) pos[xIndex] = 20;}rainGeo.attributes.position.needsUpdate = true;}renderer.domElement.addEventListener('click', () => {rainLevelIndex = (rainLevelIndex + 1) % rainLevels.length;currentRainCount = Math.floor(rainCount * rainLevels[rainLevelIndex]);rainGeo.setDrawRange(0, currentRainCount);rainSpeed = 10 + rainLevels[rainLevelIndex] * 18;});const composer = new EffectComposer(renderer);composer.addPass(new RenderPass(scene, camera));const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight),0.8,0.5,0.85);composer.addPass(bloomPass);composer.addPass(new OutputPass());window.addEventListener('resize', () => {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth, window.innerHeight);composer.setSize(window.innerWidth, window.innerHeight);});const clock = new THREE.Clock();function animate() {const delta = Math.min(clock.getDelta(), 0.05);updateRain(delta);controls.update();composer.render();requestAnimationFrame(animate);}requestAnimationFrame(animate);
The point of this code is not “how many features it has” but that it forms a complete loop: initialization, rendering, animation, interaction, resize handling, and post-processing all work. If you later drop post-processing, you can replace composer.render() with renderer.render(scene, camera), though the rainy-night atmosphere will be much weaker.
The lighting system: warm–cool contrast is the soul of a rainy night
What makes a rainy-night scene look good is not model detail but the light and dark relationships. A good combination is “ambient light as a base + warm local lights + cool sky light + emissive signs.”
- Ambient light shouldn’t be too strong, or the night scene turns gray and washed out.
- Warm point lights are used inside the convenience store, on street lamps, or in windows to create warmth.
- A cool directional light simulates the night sky or moonlight and adds depth.
- Neon signs don’t need to be real lights. An
emissivematerial combined with bloom is more stable performance-wise.
Emissive material example:
const signMaterial = new THREE.MeshStandardMaterial({color: 0x111111,emissive: 0xff3366,emissiveIntensity: 2});
If a sign is too bright and the halo smears into a blob, lower emissiveIntensity, raise the bloom threshold, or reduce the amount of bright area in the scene.
Rain particles: control speed with delta time to stay independent of frame rate
Rain is the most important moving element in the scene. If you update with a fixed value per frame, different frame rates produce different rain speeds. The correct approach is to update based on the time delta.
The core idea:
const delta = clock.getDelta();pos[yIndex] -= rainSpeed * delta;
This keeps the falling speed more consistent at 30fps, 60fps, and across different devices.
Raindrops also need to be recycled once they reach the ground. In practice, the common approach is to reset y to a high position while keeping the current x/z, which prevents drops from suddenly jumping sideways. If you want the rain to fall around a fixed curtain, wrap x or z back into range when they pass the boundary.
If the Points look like squares or round dots, three options are worth trying:
- Add a transparent circular texture to
PointsMaterial. - Switch to
LineSegmentsto simulate thin rain lines. - Use
InstancedMeshor a custom shader for more realistic rain streaks.
On mobile, prefer Points first because the draw cost is low. If you want realistic rain lines, evaluate device performance before committing.
Wet ground and reflections: low roughness is not the same as a full reflection
The rainy-night ground is where effects are easiest to achieve and easiest to misunderstand. Many people assume that setting the ground material to low roughness and high metalness will automatically reflect the streetscape and signs. That isn’t the case.
Low roughness and high metalness usually produce only local highlights or point-light reflections; they don’t automatically generate a full scene reflection. If you need stronger reflections, consider these options:
- Environment maps: low cost, good for conveying an overall sense of wetness.
- Planar reflection / Reflector: strong effect but relatively expensive; best limited to key ground areas.
- Screen-space reflections or post-processing approaches: suitable for high-quality scenes, but compatibility and performance need evaluation.
- Fake reflections: semi-transparent duplicated blocks or gradient textures to imitate reflections, at low cost.
Example ground material:
const groundMaterial = new THREE.MeshStandardMaterial({color: 0x0a0a0f,roughness: 0.15,metalness: 0.6});
If the scene has no environment map, a high-metalness material may look too dark. In that case, lower the metalness, or add an environment map, fill lights, and point-light highlights.
Post-processing: bloom, tone mapping, and output color space
The rainy-night atmosphere depends heavily on bloom. It gives bright emissive areas a soft halo, making neon, street lamps, and wet-surface highlights look more like night.
Post-processing import paths depend on the version. For recent Three.js, the recommended imports are:
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
With older versions, the import path is typically:
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
Common things to watch:
- With bloom enabled, add
OutputPass; otherwise the image may look too dark or have color space issues. - Set
renderer.outputColorSpace = THREE.SRGBColorSpaceexplicitly. - Don’t overdo bloom strength, or signs smear into bright blobs.
- On mobile, lower the bloom resolution or disable bloom based on frame rate.
- If the page is a pure black screen, first check the import paths, the render loop, and WebGL errors.
Performance optimization and a mobile degradation checklist
Rainy-night scenes naturally involve large numbers of particles, lights, and post-processing, so the performance pressure is significant. Control cost from five angles: draw calls, resolution, particle count, shadows, and post-processing.
A mobile degradation reference:
- Lower
rainCountfrom 8000 to 3000 or 2000. - Cap
pixelRatioat 1.5, and use 1 on low-end devices. - Turn off shadows, especially real-time shadows from multiple lights.
- Lower the bloom resolution, or disable bloom on small screens.
- Reduce the number of point lights and replace some fill lighting with directional or ambient light.
- Use
InstancedMeshfor repeated objects such as building windows, signs, and street lamps. - Turn off detail or reduce material complexity for distant objects.
- Monitor frame rate and dynamically adjust rain volume, bloom, and pixel ratio.
Performance goals need to be stated precisely. For example, “mobile must run at 60fps” is not rigorous, because the result depends on the device, browser, resolution, and how you measure. A more reliable phrasing is: “On mainstream mid-range mobile devices, use frame rate measured in Chrome DevTools or on the actual device as the reference, targeting a stable 30fps, targeting 60fps on desktop, and providing options to scale down.”
Working with AI coding tools: prompt templates and acceptance methods
AI coding tools are well suited to quickly generating skeletons, completing API calls, locating errors, and offering alternatives, but proportions, performance, and fine details still need human verification. Writing requirements as a structured prompt is recommended.
A usable template:
Please generate a runnable Three.js rainy-night convenience store scene.Requirements:1. Use three/addons to import OrbitControls, EffectComposer, RenderPass, UnrealBloomPass, and OutputPass.2. Camera fov of 60, initial position (8, 5, 14), target (0, 2, 0).3. The scene should include a 40x40 ground, a simple shop, one emissive sign, cool ambient light, and a warm point light.4. Rain uses Points with a count of 8000 and must update based on delta time.5. After a drop reaches the bottom, reset only y and keep x/z.6. Add click interaction that cycles through light, medium, and heavy rain.7. Add window resize handling that updates the camera, renderer, and composer together.8. Make it degradable on mobile: pixel ratio capped at 1.5, rain count reducible to 3000.9. If a black screen occurs, list common causes and troubleshooting steps.
For acceptance, check the following:
- Does the page open correctly, or is it a black screen?
- Does the rain speed change with frame rate?
- Can the camera be dragged?
- Does clicking switch the rain intensity?
- Does the image distort after resizing the window?
- Does the ground show wet highlights, without assuming it automatically reflects the streetscape?
- Is bloom too strong or too weak?
- Is mobile noticeably laggy?
Closing thoughts
Building a Three.js rainy-night scene with AI coding tools is essentially about breaking a creative idea into computable, verifiable, and iterable engineering modules. The tools lower the barrier to starting from scratch and let developers get a runnable skeleton faster, but what ultimately determines quality is still scene decomposition, the rendering loop, performance control, and repeated refinement.
Once you master this method — establish the minimal loop first, then layer in atmosphere; ensure it runs first, then pursue realism; state clear constraints first, then let AI fill in the details — that rainy, warm-lit street corner in your head can reliably become a real, runnable, interactive 3D web piece.
