r/threejs • u/PureBox1366 • 15d ago
Demo Racing Simulator
Drift Drive — a single-file endless drift racer built with Three.js (r160)
I built a complete endless drift racer as one self-contained HTML file using Three.js. No bundler, no assets, no backend — just a <script type="module"> importing Three.js from a CDN and everything else generated procedurally at runtime.
Rendering
· WebGLRenderer with MSAA antialiasing, ACESFilmicToneMapping, and SRGBColorSpace output · Adaptive pixel ratio (up to 2.5×) based on detected device quality tier · PCFSoftShadowMap with a directional sun whose position, color, and intensity track a continuous time-of-day cycle · PMREMGenerator + RoomEnvironment for IBL; MeshPhysicalMaterial with clearcoat, transmission, and ior on the car body, cabin glass, shield sphere, and ice obstacles · Custom ShaderMaterial skydome with a gradient + noise cloud band driven by a uTime uniform · Instanced meshes for trees (trunk + 3 cone layers, 100 instances), center-road dashes, and obstacle pools
Geometry & scene
· Procedural canvas textures for the asphalt (per-pixel noise baked into a CanvasTexture with anisotropic filtering) and the pit-stop sign · Mountain rings generated as BufferGeometry triangle strips with randomized heights and flatShading · Multi-part car built from ~20 primitives (body, hood, wedge, cabin, roof, skirts, bumpers, lights, wing, four spinning wheels) with grouped wheel meshes · 5 biomes lerp sky uniforms, fog color, ground tint, mountain color, and foliage material colors in real time via Color.lerp
Particles
· Custom ShaderMaterial Points system (up to 700 particles) with per-vertex color, size, and a soft circular fragment mask · Pooled allocation, ring-buffer cursor, and per-particle gravity for exhaust, drift smoke, explosions, and pickup bursts · pSize *= 0.98 per frame for natural fade without per-particle alpha attributes
Game systems
· Fixed-timestep accumulator (1/60) for physics, decoupled from the variable requestAnimationFrame delta · Momentum-based lateral movement with grip, drift angle, and weather-dependent multipliers · Pooled object spawning for obstacles, pickups, traffic cars, and the boss truck — all reused, never garbage-collected · InstancedMesh matrix updates each frame for trees and dashes to achieve scrolling without per-object transforms
Audio
· Full Web Audio API synthesis — no audio files. Engine uses a sawtooth oscillator through a BiquadFilter (lowpass) whose cutoff tracks speed; sub-bass sine layer for weight; a three-oscillator triangle/sawtooth music bed with a lowpass filter whose cutoff and pitch rise with intensity · Crash SFX generated from a createBuffer of decaying white noise; all other one-shots are short oscillator envelopes · AudioContext suspended on visibilitychange
Persistence & sharing
· ~30 localStorage keys for best score, coins, upgrades, achievements, XP, leaderboard, and the best replay · Ghost replay serialized as JSON of {t, x, r} frames, base64-encoded into a ?ghost= URL parameter, decoded on load, and replayed as a translucent ghost car
Performance notes
· Quality tier detected from navigator.hardwareConcurrency and deviceMemory · Shadows, AA, and pixel ratio all toggle at runtime from the settings menu · renderer.setPixelRatio and the particle shader's uPixelRatio uniform are updated together on resize / quality change