Prompt specification
Build or customize the Three.js 3D scene "Wind and release sequence" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Wind and release sequence
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/wind-and-release-sequence-x0398
Scene Description Prompt:
Wind and release a voxel toy car, showing stored energy decreasing as its wheels turn. Three.js from cdnjs, one HTML file.
Working Three.js Starter (wind-and-release-sequence-x0398):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Voxel Wind Car</title>
<style>
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9dcff;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: #f8fafc;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background: radial-gradient(circle at 50% 42%, transparent 45%, rgba(20, 45, 70, .16));
}
#hud {
position: fixed;
top: 20px;
left: 20px;
width: min(330px, calc(100vw - 40px));
padding: 18px;
border: 1px solid rgba(255, 255, 255, .28);
border-radius: 18px;
background: rgba(15, 29, 48, .78);
box-shadow: 0 16px 45px rgba(15, 35, 55, .22);
backdrop-filter: blur(14px);
pointer-events: none;
user-select: none;
}
.eyebrow {
color: #9cd8ff;
font-size: 11px;
font-weight: 800;
letter-spacing: .17em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 17px;
font-size: 24px;
line-height: 1;
letter-spacing: -.04em;
}
.label-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
font-size: 11px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
#energyValue {
color: #ffd166;
font-size: 15px;
letter-spacing: 0;
}
.meter {
height: 14px;
margin: 8px 0 13px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, .15);
border-radius: 999px;
background: rgba(0, 0, 0, .28);
box-shadow: inset 0 2px 6px rgba(0, 0, 0, .3);
}
#energyFill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #ff9f43, #ffd166);
box-shadow: 0 0 18px rgba(255, 190, 70, .7);
transition: width .08s linear;
}
.stats {
display: flex;
justify-content: space-between;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, .12);
color: #b8c7d9;
font-size: 11px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
#speedValue {
color: #fff;
}
#status {
min-height: 36px;
margin-top: 13px;
color: #d8e7f5;
font-size: 13px;
line-height: 1.4;
}
#windButton {
position: fixed;
left: 50%;
bottom: max(25px, env(safe-area-inset-bottom));
min-width: 240px;
padding: 17px 28px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, .5);
border-radius: 999px;
background: linear-gradient(180deg, #ffcf57, #f39a32);
box-shadow:
0 9px 0 #a85d25,
0 18px 36px rgba(72, 39, 16, .28);
color: #3b2511;
font: inherit;
font-size: 14px;
font-weight: 900;
letter-spacing: .08em;
text-transform: uppercase;
cursor: pointer;
touch-action: none;
user-select: none;
transition: transform .08s ease, filter .15s ease, opacity .15s ease;
}
#windButton:hover:not(:disabled) {
filter: brightness(1.06);
}
#windButton.active {
transform: translateX(-50%) translateY(6px);
box-shadow:
0 3px 0 #a85d25,
0 9px 20px rgba(72, 39, 16, .25);
}
#windButton:disabled {
cursor: default;
opacity: .55;
filter: saturate(.55);
}
#resetButton {
position: fixed;
top: 20px;
right: 20px;
padding: 11px 16px;
border: 1px solid rgba(255, 255, 255, .32);
border-radius: 12px;
background: rgba(15, 29, 48, .72);
box-shadow: 0 8px 24px rgba(15, 35, 55, .18);
color: #fff;
font: inherit;
font-size: 12px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(12px);
}
#resetButton:hover {
background: rgba(15, 29, 48, .9);
}
#hint {
position: fixed;
right: 20px;
bottom: 24px;
max-width: 300px;
padding: 10px 13px;
border-radius: 12px;
background: rgba(15, 29, 48, .55);
color: rgba(255, 255, 255, .84);
font-size: 11px;
line-height: 1.5;
text-align: right;
pointer-events: none;
backdrop-filter: blur(10px);
}
@media (max-width: 700px) {
#hud {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
h1 {
margin-bottom: 12px;
font-size: 20px;
}
#resetButton {
top: auto;
right: 12px;
bottom: 99px;
}
#windButton {
min-width: 210px;
padding: 16px 22px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<section id="hud" aria-label="Toy car energy display">
<div class="eyebrow">Wind-powered play lab</div>
<h1>Voxel Wind Car</h1>
<div class="label-row">
<span>Stored wind energy</span>
<strong id="energyValue">0%</strong>
</div>
<div class="meter">
<div id="energyFill"></div>
</div>
<div class="stats">
<span>Wheel speed</span>
<strong id="speedValue">0 km/h</strong>
</div>
<div id="status" aria-live="polite">Hold WIND to store energy, then release to drive.</div>
</section>
<button id="resetButton" type="button">Reset</button>
<button id="windButton" type="button">Hold to Wind</button>
<div id="hint">
Hold the yellow button to wind.<br>
Release to drive · Drag to orbit · Scroll/pinch to zoom
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const energyValue = document.getElementById("energyValue");
const energyFill = document.getElementById("energyFill");
const speedValue = document.getElementById("speedValue");
const statusElement = document.getElementById("status");
const windButton = document.getElementById("windButton");
const resetButton = document.getElementById("resetButton");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9dcff);
scene.fog = new THREE.Fog(0xb9dcff, 42, 115);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
220
);
const hemisphere = new THREE.HemisphereLight(0xe9f7ff, 0x78955e, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1ce, 3.2);
sun.position.set(-20, 32, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00035;
scene.add(sun);
scene.add(sun.target);
const fillLight = new THREE.DirectionalLight(0x8bdcff, 0.75);
fillLight.position.set(10, 12, -18);
scene.add(fillLight);
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
...options
});
const materials = {
grass: standard(0x83b96b),
road: standard(0x59636e),
roadDark: standard(0x4c5660),
shoulder: standard(0xc8d0d2),
white: standard(0xf7fafc),
black: standard(0x151b24),
bark: standard(0x76523b),
leafA: standard(0x4d945c),
leafB: standard(0x65ad67),
leafC: standard(0x3f8051),
car: standard(0xffbd3f, { roughness: 0.48, metalness: 0.08 }),
carDark: standard(0xdc7227, { roughness: 0.52 }),
glass: standard(0x183e57, { roughness: 0.24, metalness: 0.18 }),
glassLight: standard(0x4c9fc1, { roughness: 0.22, metalness: 0.12 }),
tire: standard(0x171c22, { roughness: 0.96 }),
rim: standard(0xe5edf2, { roughness: 0.32, metalness: 0.65 }),
headlight: standard(0xfff2a6, {
emissive: 0xffd85a,
emissiveIntensity: 1.2,
roughness: 0.3
}),
taillight: standard(0xef3d43, {
emissive: 0xa40e18,
emissiveIntensity: 0.8
}),
fan: standard(0x3d596c, { roughness: 0.55, metalness: 0.25 }),
fanBlade: standard(0xf28d35, { roughness: 0.5 })
};
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let seed = 73421;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
// Road and terrain
box(160, 0.5, 70, materials.grass, 5, -0.58, 0);
box(140, 0.3, 9.4, materials.road, 7, -0.15, 0);
box(140, 0.18, 1.15, materials.shoulder, 7, -0.04, -5.05);
box(140, 0.18, 1.15, materials.shoulder, 7, -0.04, 5.05);
box(140, 0.035, 0.12, materials.white, 7, 0.025, -4.47);
box(140, 0.035, 0.12, materials.white, 7, 0.025, 4.47);
for (let x = -60; x <= 76; x += 3.6) {
box(1.8, 0.035, 0.12, materials.white, x, 0.025, 0);
}
// Starting line
for (let i = 0; i < 12; i++) {
box(0.8, 0.04, 0.72, i % 2 ? materials.black : materials.white,
-42, 0.035, -3.9 + i * 0.71);
}
// Finish gantry
box(0.45, 4.5, 0.45, materials.fan, 61, 2.25, -4.05);
box(0.45, 4.5, 0.45, materials.fan, 61, 2.25, 4.05);
box(0.45, 0.35, 8.55, materials.fan, 61, 4.35, 0);
for (let i = 0; i < 14; i++) {
box(0.5, 0.52, 0.59, i % 2 ? materials.black : materials.white,
61, 4.35, -3.82 + i * 0.59);
}
// Voxel trees and roadside rocks
const leafMaterials = [materials.leafA, materials.leafB, materials.leafC];
for (let i = 0; i < 28; i++) {
const x = -62 + i * 5.2;
const side = i % 2 === 0 ? -1 : 1;
const z = side * (9 + random() * 8);
const height = 1.6 + random() * 1.4;
box(0.48, height, 0.48, materials.bark, x, height * 0.5, z);
const leafMat = leafMaterials[i % leafMaterials.length];
box(2.1, 1.35, 2.1, leafMat, x, height + 0.55, z);
box(1.55, 1.15, 1.55, leafMat, x, height + 1.55, z);
box(0.95, 0.9, 0.95, leafMat, x + 0.15, height + 2.35, z);
}
for (let i = 0; i < 30; i++) {
const x = -60 + random() * 140;
const side = random() > 0.5 ? 1 : -1;
const z = side * (5.8 + random() * 1.5);
const size = 0.25 + random() * 0.45;
box(size, size * 0.65, size, materials.shoulder, x, size * 0.32, z);
}
// Blocky clouds
const clouds = [];
for (let i = 0; i < 8; i++) {
const cloud = new THREE.Group();
cloud.position.set(-60 + random() * 120, 12 + random() * 5,
(i % 2 === 0 ? -1 : 1) * (18 + random() * 13));
box(3.8, 1.25, 1.8, materials.white, 0, 0, 0, cloud);
box(2.4, 1.7, 2.1, materials.white, 1.2, 0.55, 0, cloud);
box(2.6, 1.1, 1.7, materials.white, -1.4, 0.2, 0, cloud);
scene.add(cloud);
clouds.push(cloud);
}
// Toy car
const car = new THREE.Group();
car.position.set(-41, 0, 0);
scene.add(car);
box(3.2, 0.48, 1.42, materials.car, 0, 0.91, 0, car);
box(1.18, 0.3, 1.36, materials.car, 0.95, 1.24, 0, car);
box(0.82, 0.28, 1.34, materials.carDark, -1.15, 1.23, 0, car);
box(1.4, 0.68, 1.15, materials.glass, -0.25, 1.56, 0, car);
box(1.66, 0.17, 1.34, materials.car, -0.25, 1.96, 0, car);
// Cabin pillars and windows
box(0.09, 0.49, 1.08, materials.glassLight, 0.47, 1.57, 0, car);
box(0.88, 0.42, 0.035, materials.glassLight, -0.25, 1.58, 0.59, car);
box(0.88, 0.42, 0.035, materials.glassLight, -0.25, 1.58, -0.59, car);
box(0.12, 0.58, 1.28, materials.car, -0.88, 1.57, 0, car);
box(0.12, 0.58, 1.28, materials.car, 0.38, 1.57, 0, car);
box(0.22, 0.18, 1.52, materials.rim, 1.68, 0.85, 0, car);
box(0.22, 0.18, 1.52, materials.rim, -1.68, 0.85, 0, car);
box(0.1, 0.2, 0.29, materials.headlight, 1.63, 1.06, 0.45, car);
box(0.1, 0.2, 0.29, materials.headlight, 1.63, 1.06, -0.45, car);
box(0.1, 0.2, 0.27, materials.taillight, -1.63, 1.06, 0.45, car);
box(0.1, 0.2, 0.27, materials.taillight, -1.63, 1.06, -0.45, car);
box(0.28, 0.17, 0.35, materials.carDark, 0.4, 1.58, 0.82, car);
box(0.28, 0.17, 0.35, materials.carDark, 0.4, 1.58, -0.82, car);
box(0.2, 0.2, 0.08, materials.rim, 0.46, 1.18, 0.74, car);
box(0.2, 0.2, 0.08, materials.rim, 0.46, 1.18, -0.74, car);
// Pixelated voxel wheels
const wheelPivots = [];
function createWheel(x, z) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.57, z);
car.add(pivot);
for (let ix = -3; ix <= 3; ix++) {
for (let iy = -3; iy <= 3; iy++) {
if (ix * ix + iy * iy > 9.4) continue;
box(0.145, 0.145, 0.22, materials.tire,
ix * 0.15, iy * 0.15, 0, pivot);
if (ix * ix + iy * iy <= 2.35) {
const face = z > 0 ? 0.122 : -0.122;
box(0.15, 0.15, 0.045, materials.rim,
ix * 0.15, iy * 0.15, face, pivot);
}
}
}
const hubFace = z > 0 ? 0.155 : -0.155;
box(0.22, 0.22, 0.06, materials.carDark, 0, 0, hubFace, pivot);
wheelPivots.push(pivot);
return pivot;
}
createWheel(1.05, 0.88);
createWheel(1.05, -0.88);
createWheel(-1.05, 0.88);
createWheel(-1.05, -0.88);
// Wind-up key on the roof
const keyMaterial = standard(0xffd166, {
emissive: 0xff8a00,
emissiveIntensity: 0.15,
roughness: 0.35,
metalness: 0.35
});
const windKey = new THREE.Group();
windKey.position.set(-0.62, 2.03, 0);
car.add(windKey);
box(0.13, 0.42, 0.13, materials.rim, 0, 0.18, 0, windKey);
const keyHandle = new THREE.Group();
keyHandle.position.y = 0.42;
windKey.add(keyHandle);
box(0.78, 0.12, 0.13, keyMaterial, 0, 0, 0, keyHandle);
box(0.13, 0.12, 0.78, keyMaterial, 0, 0, 0, keyHandle);
// Blocky wind fan
const fan = new THREE.Group();
fan.position.set(-45, 0, 0);
scene.add(fan);
box(1.25, 0.25, 1.7, materials.fan, 0, 0.12, 0, fan);
box(0.38, 1.65, 0.38, materials.fan, 0, 0.95, 0, fan);
const rotor = new THREE.Group();
rotor.position.set(0.35, 1.8, 0);
fan.add(rotor);
for (let i = 0; i < 4; i++) {
const bladePivot = new THREE.Group();
bladePivot.rotation.x = i * Math.PI * 0.5;
rotor.add(bladePivot);
box(0.16, 0.94, 0.24, materials.fanBlade, 0, 0.52, 0, bladePivot);
}
box(0.28, 0.28, 0.28, materials.rim, 0.35, 1.8, 0, fan);
box(0.25, 2.65, 0.16, materials.fan, 0.35, 1.8, -1.25, fan);
box(0.25, 2.65, 0.16, materials.fan, 0.35, 1.8, 1.25, fan);
box(0.25, 0.16, 2.65, materials.fan, 0.35, 0.55, 0, fan);
box(0.25, 0.16, 2.65, materials.fan, 0.35, 3.05, 0, fan);
// World-space voxel energy reservoir
const energyCells = [];
box(0.22, 3.65, 0.22, materials.fan, -42.5, 1.82, -3.35);
box(0.65, 0.2, 0.65, materials.fan, -42.5, 0.1, -3.35);
for (let i = 0; i < 10; i++) {
const cellMaterial = standard(0x344457, {
emissive: 0x000000,
emissiveIntensity: 0,
roughness: 0.35
});
const cell = box(
0.48, 0.25, 0.48,
cellMaterial,
-42.23, 0.42 + i * 0.32, -3.35
);
energyCells.push(cell);
}
// Wind streaks
const windMaterial = new THREE.MeshBasicMaterial({
color: 0xdffbff,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const windStreaks = [];
const windGeometry = new THREE.BoxGeometry(0.95, 0.035, 0.035);
for (let i = 0; i < 28; i++) {
const streak = new THREE.Mesh(windGeometry, windMaterial);
streak.userData.phase = random();
streak.userData.speed = 0.55 + random() * 0.8;
streak.userData.y = 0.65 + random() * 1.7;
streak.userData.z = (random() - 0.5) * 3.1;
streak.userData.scale = 0.45 + random() * 1.4;
streak.scale.x = streak.userData.scale;
streak.rotation.z = (random() - 0.5) * 0.12;
scene.add(streak);
windStreaks.push(streak);
}
const state = {
phase: "idle",
winding: false,
energy: 0,
speed: 0,
wheelAngle: 0,
keyAngle: 0,
windOpacity: 0,
windEndX: -41,
fanSpeed: 0.35
};
let currentStatus = "";
function setStatus(text) {
if (currentStatus !== text) {
currentStatus = text;
statusElement.textContent = text;
}
}
function beginWind() {
if (state.phase !== "idle") return;
state.winding = true;
state.windEndX = car.position.x;
windButton.classList.add("active");
}
function releaseWind() {
if (!state.winding) return;
state.winding = false;
windButton.classList.remove("active");
if (state.phase === "idle") {
if (state.energy >= 5) {
state.phase = "driving";
state.speed = 0.25;
state.windEndX = car.position.x;
} else {
setStatus("Wind a little longer before releasing.");
}
}
}
function resetToy() {
state.phase = "idle";
state.winding = false;
state.energy = 0;
state.speed = 0;
state.wheelAngle = 0;
state.keyAngle = 0;
state.windOpacity = 0;
state.windEndX = -41;
state.fanSpeed = 0.35;
car.position.set(-41, 0, 0);
windButton.classList.remove("active");
setStatus("Hold WIND to store energy, then release to drive.");
}
windButton.addEventListener("pointerdown", event => {
event.preventDefault();
windButton.setPointerCapture(event.pointerId);
beginWind();
});
windButton.addEventListener("pointerup", releaseWind);
windButton.addEventListener("pointercancel", releaseWind);
windButton.addEventListener("lostpointercapture", releaseWind);
resetButton.addEventListener("click", resetToy);
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
if (!event.repeat) beginWind();
}
});
window.addEventListener("keyup", event => {
if (event.code === "Space") {
event.preventDefault();
releaseWind();
}
});
window.addEventListener("blur", releaseWind);
// Custom camera controls: orbit, wheel zoom, and touch pinch.
const cameraControl = {
yaw: 0.72,
pitch: 0.38,
radius: 12,
target: new THREE.Vector3(-41, 0.95, 0)
};
let followX = -41;
const pointers = new Map();
let orbitPointer = null;
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
orbitPointer = event.pointerId;
} else if (pointers.size === 2) {
orbitPointer = null;
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", event => {
const old = pointers.get(event.pointerId);
if (!old) return;
const dx = event.clientX - old.x;
const dy = event.clientY - old.y;
old.x = event.clientX;
old.y = event.clientY;
if (pointers.size >= 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && distance > 0) {
cameraControl.radius *= pinchDistance / distance;
cameraControl.radius = THREE.MathUtils.clamp(
cameraControl.radius, 7, 25
);
}
pinchDistance = distance;
} else if (orbitPointer === event.pointerId) {
cameraControl.yaw -= dx * 0.006;
cameraControl.pitch = THREE.MathUtils.clamp(
cameraControl.pitch + dy * 0.005,
0.12,
1.12
);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (orbitPointer === event.pointerId) {
orbitPointer = pointers.keys().next().value ?? null;
}
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraControl.radius *= Math.exp(event.deltaY * 0.001);
cameraControl.radius = THREE.MathUtils.clamp(
cameraControl.radius, 7, 25
);
}, { passive: false });
function updateCamera(delta) {
followX += (car.position.x - followX) * (1 - Math.exp(-delta * 5));
cameraControl.target.set(followX, 0.95, 0);
const horizontal = Math.cos(cameraControl.pitch) * cameraControl.radius;
camera.position.set(
cameraControl.target.x + Math.sin(cameraControl.yaw) * horizontal,
cameraControl.target.y + Math.sin(cameraControl.pitch) * cameraControl.radius,
cameraControl.target.z + Math.cos(cameraControl.yaw) * horizontal
);
camera.lookAt(cameraControl.target);
}
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (state.winding && state.phase === "idle") {
state.energy = Math.min(100, state.energy + delta * 29);
state.keyAngle += delta * 11;
}
if (state.phase === "driving") {
const driveForce = state.energy * 0.28;
const resistance = 1.15 + state.speed * state.speed * 0.028;
const acceleration = driveForce - resistance;
state.speed = Math.max(0, Math.min(16, state.speed + acceleration * delta));
const distance = state.speed * delta;
car.position.x += distance;
const wheelTurn = distance / 0.53;
state.wheelAngle -= wheelTurn;
state.keyAngle -= wheelTurn * 1.35;
state.energy = Math.max(
0,
state.energy - distance * 0.95 - state.speed * delta * 0.012
);
if (car.position.x >= 61) {
state.phase = "finished";
}
if (state.energy <= 0) {
state.energy = 0;
state.phase = "spent";
}
} else if (state.phase === "finished" || state.phase === "spent") {
state.speed *= Math.exp(-delta * 1.4);
const distance = state.speed * delta;
car.position.x = Math.min(74, car.position.x + distance);
const wheelTurn = distance / 0.53;
state.wheelAngle -= wheelTurn;
state.keyAngle -= wheelTurn * 1.2;
if (state.phase === "finished") {
state.energy = Math.max(0, state.energy - distance * 0.65);
if (state.energy <= 0) state.phase = "spent";
}
}
for (const pivot of wheelPivots) {
pivot.rotation.z = state.wheelAngle;
}
keyHandle.rotation.y = state.keyAngle;
keyMaterial.emissiveIntensity =
0.12 + (state.energy / 100) * 0.9 + (state.winding ? 0.35 : 0);
car.position.y = state.speed > 0.05
? Math.sin(state.wheelAngle * 1.6) * 0.012 * Math.min(1, state.speed / 4)
: 0;
// Fan and wind animation
state.fanSpeed = state.winding
? 18
: state.fanSpeed * Math.exp(-delta * 2.2) + 0.25 * (1 - Math.exp(-delta * 3));
rotor.rotation.x += state.fanSpeed * delta;
const windTarget = state.winding ? 1 : 0;
state.windOpacity +=
(windTarget - state.windOpacity) * (1 - Math.exp(-delta * 6));
windMaterial.opacity = state.windOpacity * 0.7;
for (const streak of windStreaks) {
streak.userData.phase =
(streak.userData.phase + delta * streak.userData.speed) % 1;
const progress = streak.userData.phase;
const startX = -44.4;
const endX = state.windEndX + 0.8;
streak.position.set(
startX + (endX - startX) * progress,
streak.userData.y,
streak.userData.z
);
}
// Energy reservoir
for (let i = 0; i < energyCells.length; i++) {
const cell = energyCells[i];
const lit = state.energy > i * 10;
const pulse = lit && i === Math.floor(state.energy / 10)
? 1 + Math.sin(elapsed * 5) * 0.06
: 1;
cell.material.color.set(lit ? 0xffc857 : 0x344457);
cell.material.emissive.set(lit ? 0xff8a00 : 0x000000);
cell.material.emissiveIntensity = lit ? 0.7 : 0;
cell.scale.set(lit ? pulse : 0.72, lit ? pulse : 0.62, lit ? pulse : 0.72);
}
// Clouds
for (const cloud of clouds) {
cloud.position.x += delta * 0.45;
if (cloud.position.x > 82) cloud.position.x = -82;
}
// Moving sunlight shadow
sun.position.x = car.position.x + 15;
sun.position.z = 24;
sun.target.position.set(car.position.x, 0, 0);
sun.target.updateMatrixWorld();
// Status and HUD
if (state.winding) {
setStatus(
state.energy >= 99
? "Fully wound! Release the button to let the car roll."
: "Winding the roof spring and filling the energy reservoir…"
);
} else if (state.phase === "idle") {
setStatus(
state.energy >= 5
? "Energy is stored. Release WIND to launch the toy car."
: "Hold WIND to store energy, then release to drive."
);
} else if (state.phase === "driving") {
setStatus("Rolling! Stored wind energy is decreasing as the wheels turn.");
} else if (state.phase === "finished") {
setStatus("Run complete — the spring is unwinding as the car coasts.");
} else {
setStatus("Energy depleted. Reset the toy to wind it again.");
}
const roundedEnergy = Math.round(state.energy);
energyValue.textContent = `${roundedEnergy}%`;
energyFill.style.width = `${state.energy}%`;
const meterColor = state.energy > 55
? "#ffd166"
: state.energy > 22
? "#ff9f43"
: "#ff6b6b";
energyFill.style.background =
`linear-gradient(90deg, #ff9f43, ${meterColor})`;
speedValue.textContent = `${Math.round(state.speed * 6)} km/h`;
windButton.disabled = state.phase !== "idle";
windButton.textContent = state.winding
? `Winding · ${roundedEnergy}%`
: state.phase === "driving"
? "Released!"
: state.phase === "finished" || state.phase === "spent"
? "Reset to Wind Again"
: "Hold to Wind";
updateCamera(delta);
renderer.render(scene, camera);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
updateCamera(1);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```