Prompt specification
Build or customize the Three.js 3D scene "Wheel follows a smooth transition" 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: Wheel follows a smooth transition
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/wheel-follows-a-smooth-transition-x0470
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: wheel follows a smooth transition. Animate a simple stroller along pavement and a ramped curb connection; maintain wheel-ground contact and show where an abrupt step would interrupt the prescribed route.
Working Three.js Starter (wheel-follows-a-smooth-transition-x0470):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Stroller — Curb Transition Study</title>
<style>
:root {
--ink: #17313a;
--panel: rgba(250, 252, 248, 0.92);
--cyan: #00b8c4;
--orange: #ef6a38;
--muted: #61747a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: linear-gradient(#dcebf0 0%, #eef3e8 72%, #d8dfcf 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#app canvas {
display: block;
width: 100%;
height: 100%;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
background: var(--panel);
border: 1px solid rgba(23, 49, 58, 0.12);
border-radius: 16px;
box-shadow: 0 12px 35px rgba(23, 49, 58, 0.13);
backdrop-filter: blur(12px);
}
#header {
top: 18px;
left: 18px;
width: min(370px, calc(100vw - 36px));
padding: 18px 20px 16px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 28px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 14px;
}
.stat {
min-width: 0;
padding: 9px 10px;
border-radius: 10px;
background: rgba(23, 49, 58, 0.055);
}
.stat small {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.stat strong {
display: block;
margin-top: 3px;
font-size: 13px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
button {
appearance: none;
border: 0;
border-radius: 9px;
padding: 9px 12px;
background: var(--ink);
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #244852;
}
button.secondary {
background: rgba(23, 49, 58, 0.09);
color: var(--ink);
}
button.active {
background: var(--cyan);
color: white;
}
#speed {
display: flex;
flex: 1;
min-width: 140px;
align-items: center;
gap: 8px;
padding: 0 5px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
#speed input {
width: 100%;
accent-color: var(--cyan);
}
#profile {
left: 18px;
bottom: 18px;
width: min(370px, calc(100vw - 36px));
padding: 15px 17px 14px;
}
.profile-title {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
}
.profile-title strong {
font-size: 13px;
}
.profile-title span {
color: var(--muted);
font-size: 10px;
}
#profile svg {
display: block;
width: 100%;
height: 105px;
overflow: visible;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 16px;
margin-top: 4px;
font-size: 10px;
font-weight: 750;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 18px;
height: 4px;
border-radius: 10px;
background: var(--cyan);
}
.swatch.step {
background: var(--orange);
}
.swatch.contact {
width: 8px;
height: 8px;
border-radius: 50%;
background: #49e5ce;
}
#help {
right: 18px;
bottom: 18px;
max-width: 290px;
padding: 10px 13px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
#help strong {
color: var(--ink);
}
#world-labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 7px 10px;
border-radius: 8px;
background: rgba(250, 252, 248, 0.94);
box-shadow: 0 5px 18px rgba(23, 49, 58, 0.17);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.06em;
line-height: 1.25;
text-align: center;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-label::after {
position: absolute;
bottom: -7px;
left: 50%;
width: 10px;
height: 10px;
background: inherit;
content: "";
transform: translateX(-50%) rotate(45deg);
}
#label-ramp {
border-left: 4px solid var(--cyan);
}
#label-step {
border-left: 4px solid var(--orange);
}
.world-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.04em;
}
@media (max-width: 700px) {
#header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
}
#profile {
left: 10px;
bottom: 10px;
width: calc(100vw - 20px);
}
#profile svg {
height: 72px;
}
#help {
display: none;
}
.subtitle {
display: none;
}
.stats {
margin-bottom: 10px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="header" class="panel">
<div class="eyebrow">Voxel Mobility Study</div>
<h1>Curbside transition</h1>
<p class="subtitle">
A four-wheel stroller follows the cyan prescribed route over a smooth
curb ramp. The orange counterfactual crosses the same curb as an
abrupt vertical step.
</p>
<div class="stats">
<div class="stat">
<small>Wheel contact</small>
<strong id="contact-stat">4 / 4</strong>
</div>
<div class="stat">
<small>Elevation</small>
<strong id="elevation-stat">0.00 m</strong>
</div>
<div class="stat">
<small>Current zone</small>
<strong id="zone-stat">Pavement</strong>
</div>
</div>
<div class="controls">
<button id="pause">Pause</button>
<button id="replay" class="secondary">Replay</button>
<button id="follow" class="active">Follow</button>
<label id="speed">
Speed
<input
id="speed-slider"
type="range"
min="0.5"
max="1.5"
step="0.1"
value="1"
>
</label>
</div>
</section>
<section id="profile" class="panel">
<div class="profile-title">
<strong>Elevation profile</strong>
<span>0.65 m curb</span>
</div>
<svg viewBox="0 0 300 110" role="img" aria-label="Smooth ramp versus abrupt step">
<defs>
<pattern id="grid" width="20" height="20" patternUnits="userSpaceOnUse">
<path
d="M 20 0 L 0 0 0 20"
fill="none"
stroke="rgba(23,49,58,.08)"
stroke-width="1"
/>
</pattern>
</defs>
<rect x="20" y="8" width="260" height="68" fill="url(#grid)" rx="5" />
<path
d="M 40 68 L 120 68 C 143 68 157 20 192 20 L 280 20"
fill="none"
stroke="#00b8c4"
stroke-width="5"
stroke-linecap="round"
stroke-linejoin="round"
/>
<path
d="M 40 68 L 120 68 L 120 20 L 280 20"
fill="none"
stroke="#ef6a38"
stroke-width="3"
stroke-dasharray="7 5"
stroke-linecap="round"
stroke-linejoin="round"
/>
<path d="M 120 14 L 120 74" stroke="rgba(23,49,58,.18)" stroke-dasharray="3 4" />
<text x="120" y="94" text-anchor="middle" fill="#61747a" font-size="9">CURB</text>
<text x="48" y="91" fill="#61747a" font-size="9">PAVEMENT</text>
<text x="232" y="91" fill="#61747a" font-size="9">SIDEWALK</text>
</svg>
<div class="legend">
<span><i class="swatch"></i> Prescribed smooth route</span>
<span><i class="swatch step"></i> Abrupt step</span>
<span><i class="swatch contact"></i> Wheel contact point</span>
</div>
</section>
<div id="help" class="panel">
<strong>Camera:</strong> drag to orbit · wheel to zoom · right-drag to pan.
The four cyan contact markers are sampled directly from the ramp surface.
</div>
<div id="world-labels">
<div id="label-ramp" class="world-label">
Smooth curb ramp
<small>Prescribed route</small>
</div>
<div id="label-step" class="world-label">
Abrupt step
<small>Route would break here</small>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and route profile
// -------------------------------------------------------------------------
const CURB_HEIGHT = 0.65;
const RAMP_START = 0;
const RAMP_END = 1.8;
const RAMP_Z_MIN = -2.3;
const RAMP_Z_MAX = 0;
const ROUTE_Z = -1.15;
const WHEEL_RADIUS = 0.22;
const WHEELBASE = 0.96;
const TRACK = 0.62;
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function smoothstep(t) {
t = clamp(t, 0, 1);
return t * t * (3 - 2 * t);
}
function rampHeight(x) {
if (x <= RAMP_START) return 0;
if (x >= RAMP_END) return CURB_HEIGHT;
return CURB_HEIGHT * smoothstep(
(x - RAMP_START) / (RAMP_END - RAMP_START)
);
}
function rampSlope(x) {
if (x <= RAMP_START || x >= RAMP_END) return 0;
const t = (x - RAMP_START) / (RAMP_END - RAMP_START);
return (
CURB_HEIGHT *
6 * t * (1 - t) /
(RAMP_END - RAMP_START)
);
}
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdcebf0);
scene.fog = new THREE.Fog(0xdcebf0, 18, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.05,
100
);
const hemisphere = new THREE.HemisphereLight(0xf8fcff, 0x6d7964, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d8, 3.2);
sun.position.set(-7, 14, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbfe9ff, 0.8);
fill.position.set(10, 6, -8);
scene.add(fill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
const mats = {
road: material(0x777f7d),
roadDark: material(0x646d6b),
concrete: material(0xc7c8bb),
concreteLight: material(0xd9dacd),
seam: material(0x9ea39a),
route: material(0x00c8d0, {
emissive: 0x006c70,
emissiveIntensity: 0.55
}),
warning: material(0xef6a38, {
emissive: 0xb52e0b,
emissiveIntensity: 0.28,
transparent: true,
opacity: 0.72
}),
warningBright: material(0xff8a4c, {
emissive: 0xd63c0d,
emissiveIntensity: 0.85
}),
frame: material(0x24444d),
coral: material(0xe96850),
coralDark: material(0xb94539),
fabric: material(0xf1bd54),
tire: material(0x20272a),
hub: material(0xb9c5c5, { metalness: 0.25, roughness: 0.48 }),
tread: material(0x343d40),
leaf: material(0x5f9469),
leafLight: material(0x78ad78),
trunk: material(0x8a6750),
white: material(0xf2f0df),
yellow: material(0xe8b950)
};
function box(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
mat
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(parent, a, b, thickness, mat) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(thickness, length, thickness),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground: lower pavement, upper sidewalk, ramp, and abrupt curb
// -------------------------------------------------------------------------
// Lower pavement extends under the raised sidewalk.
box(
scene,
new THREE.Vector3(20, 0.28, 10),
new THREE.Vector3(0, -0.14, 0),
mats.road
);
// Upper sidewalk sections leave a corridor for the ramp.
box(
scene,
new THREE.Vector3(10, CURB_HEIGHT, 2.7),
new THREE.Vector3(5, CURB_HEIGHT / 2, -3.65),
mats.concrete
);
box(
scene,
new THREE.Vector3(10, CURB_HEIGHT, 5),
new THREE.Vector3(5, CURB_HEIGHT / 2, 2.5),
mats.concrete
);
// Smooth ramp surface.
{
const nx = 28;
const nz = 10;
const positions = [];
const indices = [];
for (let ix = 0; ix <= nx; ix++) {
const u = ix / nx;
const x = lerp(RAMP_START, RAMP_END, u);
for (let iz = 0; iz <= nz; iz++) {
const v = iz / nz;
const z = lerp(RAMP_Z_MIN, RAMP_Z_MAX, v);
positions.push(x, rampHeight(x), z);
}
}
for (let ix = 0; ix < nx; ix++) {
for (let iz = 0; iz < nz; iz++) {
const a = ix * (nz + 1) + iz;
const b = (ix + 1) * (nz + 1) + iz;
const c = a + 1;
const d = b + 1;
indices.push(a, c, b);
indices.push(b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const ramp = new THREE.Mesh(geometry, mats.concreteLight);
ramp.receiveShadow = true;
ramp.castShadow = true;
scene.add(ramp);
}
// Sidewalk seams.
for (let x = 1; x <= 9; x++) {
box(
scene,
new THREE.Vector3(0.025, 0.012, 2.7),
new THREE.Vector3(x, CURB_HEIGHT + 0.007, -3.65),
mats.seam,
false
);
box(
scene,
new THREE.Vector3(0.025, 0.012, 5),
new THREE.Vector3(x, CURB_HEIGHT + 0.007, 2.5),
mats.seam,
false
);
}
// Transverse ramp seams.
for (const x of [0.45, 0.9, 1.35]) {
const seam = box(
scene,
new THREE.Vector3(0.025, 0.012, RAMP_Z_MAX - RAMP_Z_MIN),
new THREE.Vector3(
x,
rampHeight(x) + 0.009,
(RAMP_Z_MIN + RAMP_Z_MAX) / 2
),
mats.seam,
false
);
seam.rotation.z = Math.atan(rampSlope(x));
}
// The abrupt curb face on the alternate alignment.
const abruptFace = box(
scene,
new THREE.Vector3(0.045, CURB_HEIGHT + 0.015, 2.15),
new THREE.Vector3(0.018, CURB_HEIGHT / 2, 1.3),
mats.warning
);
abruptFace.material.transparent = true;
// Thin bright outline around the step.
box(
scene,
new THREE.Vector3(0.065, 0.055, 2.15),
new THREE.Vector3(0.052, CURB_HEIGHT, 1.3),
mats.warningBright,
false
);
// Prescribed cyan route markers.
for (let x = -8.5; x < 8.6; x += 0.55) {
const dash = box(
scene,
new THREE.Vector3(0.38, 0.028, 0.085),
new THREE.Vector3(
x,
rampHeight(x) + 0.026,
ROUTE_Z
),
mats.route,
false
);
dash.rotation.z = Math.atan(rampSlope(x));
}
// Orange counterfactual route: lower side of the step.
for (let x = -3.1; x < -0.18; x += 0.48) {
box(
scene,
new THREE.Vector3(0.33, 0.028, 0.085),
new THREE.Vector3(x, 0.028, 1.3),
mats.warningBright,
false
);
}
// Vertical interruption.
for (let y = 0.08; y < CURB_HEIGHT; y += 0.17) {
box(
scene,
new THREE.Vector3(0.085, 0.11, 0.085),
new THREE.Vector3(0.06, y, 1.3),
mats.warningBright,
false
);
}
// Upper side of the counterfactual route.
for (let x = 0.2; x < 3.2; x += 0.48) {
box(
scene,
new THREE.Vector3(0.33, 0.028, 0.085),
new THREE.Vector3(x, CURB_HEIGHT + 0.028, 1.3),
mats.warningBright,
false
);
}
// Lower pavement lane marking.
for (let x = -9.1; x < -0.45; x += 1.05) {
box(
scene,
new THREE.Vector3(0.62, 0.018, 0.075),
new THREE.Vector3(x, 0.015, 3.85),
mats.white,
false
);
}
// -------------------------------------------------------------------------
// Simple voxel environment
// -------------------------------------------------------------------------
function addTree(x, z, groundY) {
box(
scene,
new THREE.Vector3(0.18, 1.15, 0.18),
new THREE.Vector3(x, groundY + 0.575, z),
mats.trunk
);
box(
scene,
new THREE.Vector3(0.9, 0.75, 0.9),
new THREE.Vector3(x, groundY + 1.42, z),
mats.leaf
);
box(
scene,
new THREE.Vector3(0.66, 0.58, 0.66),
new THREE.Vector3(x + 0.08, groundY + 1.95, z - 0.03),
mats.leafLight
);
}
addTree(-6.6, -4.35, 0);
addTree(-2.4, -4.35, 0);
addTree(4.7, -4.35, CURB_HEIGHT);
addTree(8.1, -4.35, CURB_HEIGHT);
addTree(5.2, 4.25, CURB_HEIGHT);
addTree(8.4, 4.25, CURB_HEIGHT);
function addLamp(x, z, groundY) {
box(
scene,
new THREE.Vector3(0.11, 2.65, 0.11),
new THREE.Vector3(x, groundY + 1.325, z),
mats.frame
);
box(
scene,
new THREE.Vector3(0.62, 0.1, 0.12),
new THREE.Vector3(x + 0.22, groundY + 2.55, z),
mats.frame
);
box(
scene,
new THREE.Vector3(0.38, 0.12, 0.28),
new THREE.Vector3(x + 0.43, groundY + 2.47, z),
mats.yellow
);
}
addLamp(-7.8, 4.3, 0);
addLamp(6.2, -4.3, CURB_HEIGHT);
// -------------------------------------------------------------------------
// Voxel stroller
// -------------------------------------------------------------------------
const stroller = new THREE.Group();
scene.add(stroller);
// Axles.
for (const x of [-WHEELBASE / 2, WHEELBASE / 2]) {
beam(
stroller,
[x, 0, -TRACK / 2 + 0.03],
[x, 0, TRACK / 2 - 0.03],
0.065,
mats.hub
);
}
// Lower frame.
for (const side of [-1, 1]) {
const z = side * 0.29;
beam(
stroller,
[-0.48, -0.01, z],
[0.48, -0.01, z],
0.055,
mats.frame
);
beam(
stroller,
[-0.48, -0.01, z],
[-0.25, 0.35, z],
0.055,
mats.frame
);
beam(
stroller,
[0.48, -0.01, z],
[0.25, 0.35, z],
0.055,
mats.frame
);
beam(
stroller,
[-0.29, 0.31, z],
[-0.58, 1.04, z],
0.055,
mats.frame
);
}
// Handle.
beam(
stroller,
[-0.58, 1.04, -0.3],
[-0.58, 1.04, 0.3],
0.065,
mats.coralDark
);
// Basket and seat.
box(
stroller,
new THREE.Vector3(0.72, 0.25, 0.58),
new THREE.Vector3(0.02, 0.43, 0),
mats.coral
);
box(
stroller,
new THREE.Vector3(0.57, 0.065, 0.5),
new THREE.Vector3(0.02, 0.57, 0),
mats.coralDark
);
box(
stroller,
new THREE.Vector3(0.12, 0.43, 0.57),
new THREE.Vector3(-0.31, 0.65, 0),
mats.coral
);
// Voxel canopy.
box(
stroller,
new THREE.Vector3(0.87, 0.12, 0.67),
new THREE.Vector3(0.01, 0.96, 0),
mats.fabric
);
box(
stroller,
new THREE.Vector3(0.22, 0.29, 0.63),
new THREE.Vector3(0.34, 0.81, 0),
mats.fabric
);
box(
stroller,
new THREE.Vector3(0.12, 0.24, 0.61),
new THREE.Vector3(-0.33, 0.82, 0),
mats.yellow
);
// Front basket detail.
box(
stroller,
new THREE.Vector3(0.055, 0.18, 0.47),
new THREE.Vector3(0.395, 0.44, 0),
mats.white
);
function createWheel() {
const mount = new THREE.Group();
const spin = new THREE.Group();
mount.add(spin);
const tireGeometry = new THREE.CylinderGeometry(
WHEEL_RADIUS,
WHEEL_RADIUS,
0.16,
12
);
tireGeometry.rotateX(Math.PI / 2);
const tire = new THREE.Mesh(tireGeometry, mats.tire);
tire.castShadow = true;
tire.receiveShadow = true;
spin.add(tire);
const hubGeometry = new THREE.CylinderGeometry(
0.105,
0.105,
0.175,
10
);
hubGeometry.rotateX(Math.PI / 2);
const hub = new THREE.Mesh(hubGeometry, mats.hub);
hub.castShadow = true;
spin.add(hub);
// Chunky voxel tread blocks.
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const tread = new THREE.Mesh(
new THREE.BoxGeometry(0.105, 0.045, 0.185),
mats.tread
);
tread.position.set(
Math.cos(angle) * WHEEL_RADIUS,
Math.sin(angle) * WHEEL_RADIUS,
0
);
tread.rotation.z = angle;
tread.castShadow = true;
spin.add(tread);
}
scene.add(mount);
return { mount, spin };
}
const wheels = [];
for (const axle of [-1, 1]) {
for (const side of [-1, 1]) {
wheels.push({
...createWheel(),
axle,
side,
contactMarker: null
});
}
}
// Contact points sampled from the terrain.
const contactMaterial = new THREE.MeshBasicMaterial({
color: 0x49e5ce,
transparent: true,
opacity: 0.95,
depthTest: false
});
for (const wheel of wheels) {
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.038, 10, 8),
contactMaterial
);
marker.renderOrder = 10;
scene.add(marker);
wheel.contactMarker = marker;
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let cycleTime = 0;
let localTravel = 0;
let routeX = -7;
let paused = false;
let speed = 1;
let follow = true;
const routeState = (time) => {
if (time < 9) {
return {
x: lerp(-7, 7, time / 9),
yaw: 0,
travelSign: 1
};
}
if (time < 10) {
return {
x: 7,
yaw: Math.PI * smoothstep(time - 9),
travelSign: 0
};
}
if (time < 19) {
return {
x: lerp(7, -7, (time - 10) / 9),
yaw: Math.PI,
travelSign: -1
};
}
return {
x: -7,
yaw: Math.PI + Math.PI * smoothstep(time - 19),
travelSign: 0
};
};
const axleY = { front: 0, rear: 0 };
function updateStroller(dt) {
const previousX = routeX;
const state = routeState(cycleTime);
routeX = state.x;
localTravel += (routeX - previousX) * state.travelSign;
const yaw = state.yaw;
const cosYaw = Math.cos(yaw);
const sinYaw = Math.sin(yaw);
const qYaw = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 1, 0),
yaw
);
let totalCenterY = 0;
let frontY = 0;
let rearY = 0;
for (const wheel of wheels) {
const localX = wheel.axle * WHEELBASE / 2;
const localZ = wheel.side * TRACK / 2;
const worldX =
routeX +
cosYaw * localX +
sinYaw * localZ;
const worldZ =
ROUTE_Z -
sinYaw * localX +
cosYaw * localZ;
const groundY = rampHeight(worldX);
const worldSlope = rampSlope(worldX);
const slopeLength = Math.sqrt(1 + worldSlope * worldSlope);
// Center the circle at exactly one radius from the inclined surface.
const centerY =
groundY +
WHEEL_RADIUS * slopeLength;
const localPitch = Math.atan(worldSlope * cosYaw);
const qPitch = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 0, 1),
localPitch
);
wheel.mount.quaternion.copy(qYaw).multiply(qPitch);
wheel.mount.position.set(worldX, centerY, worldZ);
wheel.spin.rotation.z = -localTravel / WHEEL_RADIUS;
// Tangent/contact point on the ramp.
const tangentX =
worldX +
worldSlope * WHEEL_RADIUS / slopeLength;
const tangentY =
centerY -
WHEEL_RADIUS / slopeLength +
0.012;
wheel.contactMarker.position.set(
tangentX,
tangentY,
worldZ
);
totalCenterY += centerY;
if (wheel.axle > 0) frontY += centerY / 2;
else rearY += centerY / 2;
}
axleY.front = frontY;
axleY.rear = rearY;
const framePitch = Math.atan2(
axleY.front - axleY.rear,
WHEELBASE
);
const qFramePitch = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 0, 1),
framePitch
);
stroller.position.set(
routeX,
totalCenterY / wheels.length,
ROUTE_Z
);
stroller.quaternion.copy(qYaw).multiply(qFramePitch);
// HUD values.
const height = rampHeight(routeX);
document.querySelector("#elevation-stat").textContent =
`${height.toFixed(2)} m`;
document.querySelector("#contact-stat").textContent =
`${wheels.length} / ${wheels.length}`;
const zone =
routeX < RAMP_START
? "Pavement"
: routeX <= RAMP_END
? "Smooth ramp"
: "Sidewalk";
document.querySelector("#zone-stat").textContent = zone;
}
// -------------------------------------------------------------------------
// Camera controls implemented directly, without addons
// -------------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(-7, 0.4, ROUTE_Z),
radius: 9.2,
theta: 0.62,
phi: 1.02
};
const desiredTarget = new THREE.Vector3();
function updateCamera(dt) {
if (follow) {
desiredTarget.set(routeX, 0.42, ROUTE_Z);
} else {
desiredTarget.set(0, 0.4, 0);
}
orbit.target.lerp(
desiredTarget,
1 - Math.exp(-dt * 4.5)
);
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x +
orbit.radius * sinPhi * Math.cos(orbit.theta),
orbit.target.y +
orbit.radius * Math.cos(orbit.phi),
orbit.target.z +
orbit.radius * sinPhi * Math.sin(orbit.theta)
);
camera.lookAt(orbit.target);
}
const pointer = {
active: false,
x: 0,
y: 0,
button: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.button = event.button;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointer.button === 2 || event.shiftKey) {
const right = new THREE.Vector3();
camera.getWorldDirection(right);
right.cross(camera.up).normalize();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.cross(camera.up).normalize();
const scale = orbit.radius * 0.0015;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, dy * scale);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = clamp(orbit.phi - dy * 0.006, 0.25, 1.42);
}
});
function releasePointer(event) {
pointer.active = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = clamp(
orbit.radius + event.deltaY * 0.012,
4.2,
28
);
}, { passive: false });
// -------------------------------------------------------------------------
// World-space HTML labels
// -------------------------------------------------------------------------
const rampLabel = document.querySelector("#label-ramp");
const stepLabel = document.querySelector("#label-step");
const rampLabelPosition = new THREE.Vector3(0.85, 1.15, ROUTE_Z);
const stepLabelPosition = new THREE.Vector3(0.08, 1.2, 1.3);
function updateLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const pauseButton = document.querySelector("#pause");
const followButton = document.querySelector("#follow");
const speedSlider = document.querySelector("#speed-slider");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
pauseButton.classList.toggle("secondary", paused);
});
document.querySelector("#replay").addEventListener("click", () => {
cycleTime = 0;
localTravel = 0;
routeX = -7;
paused = false;
pauseButton.textContent = "Pause";
pauseButton.classList.remove("secondary");
});
followButton.addEventListener("click", () => {
follow = !follow;
followButton.classList.toggle("active", follow);
followButton.textContent = follow ? "Follow" : "Overview";
orbit.radius = follow ? 9.2 : 16;
});
speedSlider.addEventListener("input", () => {
speed = Number(speedSlider.value);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
cycleTime = (cycleTime + dt * speed) % 20;
}
updateStroller(paused ? 0 : dt);
updateCamera(dt);
// Pulse the interrupted curb face.
const pulse = 0.54 + Math.sin(performance.now() * 0.004) * 0.15;
abruptFace.material.opacity = pulse;
updateLabel(rampLabel, rampLabelPosition);
updateLabel(stepLabel, stepLabelPosition);
renderer.render(scene, camera);
}
updateStroller(0);
updateCamera(0);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```