Dream Car runs entirely inside a single HTML file. On load, it bootstraps a Three.js scene, attaches a full-window WebGL renderer toDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/JustADev1024/threejs-car/llms.txt
Use this file to discover all available pages before exploring further.
document.body, and starts three independent requestAnimationFrame loops — one for the render pass, one for road/scenery recycling, and one for wheel animation. Everything from fog colour to light positions is coded inline with exact numeric constants, making the file self-contained with no build step and no external assets beyond the Three.js CDN bundle.
CDN Import Map
Three.js and itsOrbitControls add-on are loaded from jsDelivr via a native importmap. No bundler or node_modules is required.
Three.js r128 is pinned. Upgrading to a newer release may require adjusting the
OrbitControls import path and verifying that FogExp2, MeshStandardMaterial, and PointsMaterial APIs remain compatible.Renderer
AWebGLRenderer with hardware anti-aliasing is created, sized to the full browser viewport, and appended directly to document.body. Shadow mapping is enabled globally so any mesh with castShadow = true participates in the shadow pass.
resize listener keeps the renderer and camera projection matrix in sync with the viewport:
Scene
The scene background is a deep night-sky blue (0x0a1030) and uses exponential fog of the same colour to fade distant objects smoothly.
updateTime() function mutates both scene.background and scene.fog.color every frame so they interpolate from night blue to sky blue as the time-of-day slider moves.
Camera
A perspective camera with a 45° vertical field of view is placed slightly above and behind the car to give a classic third-person driving view.OrbitControls allow the user to orbit, pan, and zoom freely. Damping is enabled for smooth deceleration.
Lighting Setup
Three lights provide the base illumination for the scene.Ambient Light
0x404060) that lifts shadows uniformly across all surfaces. Its intensity is dynamically adjusted by updateTime() — it rises to 0.6 at noon and drops to 0.3 at midnight.
Directional Light — Sun
0xfff5e6) directional light simulating sunlight at intensity 1.2. It is the primary shadow caster; castShadow = true activates the shadow map pass for this light. updateTime() scales its intensity with the sun angle.
Point Light — Blue Fill
0x88aaff) centered just above the road surface. It adds cool specular highlights to the car body at night and is never toggled — it is always on at intensity 0.3.
Sun and Moon Meshes
The sun and moon are rendered asSphereGeometry meshes with MeshStandardMaterial. Their positions and emissiveIntensity are updated every time updateTime() is called.
| Body | Geometry | Color | Emissive |
|---|---|---|---|
| Sun | SphereGeometry(1.2, 64, 64) | 0xffaa66 | 0xff4411 |
| Moon | SphereGeometry(0.9, 64, 64) | 0xdddddd | 0x88aaff |
updateTime() calculates the arc position from the hour value and toggles sunMesh.visible / moonMesh.visible depending on which body is above the horizon:
Render Loop
The main render loop callscontrols.update() every frame (required for enableDamping) and then issues the draw call.
Dream Car runs three separate
requestAnimationFrame loops: renderLoop() for drawing, updateScenery() for moving road lines and landscape objects, and animateCar() for wheel rotation. They are independent and started in sequence at the bottom of the script.