Prompt specification
Build or customize the Three.js 3D scene "Instruction at the Correct Hierarchy Level" 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: Instruction at the Correct Hierarchy Level
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/instruction-at-the-correct-hierarchy-level-x3404
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: instruction at the correct hierarchy level. Build a repair puzzle involving a bicycle, wheel, tire, and valve. Execute instructions only on the requested level, keeping parent-child transformations consistent. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (instruction-at-the-correct-hierarchy-level-x3404):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Bicycle Repair</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(12, 24, 34, 0.92);
--panel-edge: rgba(126, 222, 218, 0.18);
--text: #edf8f7;
--muted: #8da6ad;
--cyan: #4ee6d0;
--cyan-dark: #173d43;
--orange: #ff785d;
--yellow: #f6c85f;
--danger: #ff6f78;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
bottom: 18px;
left: 18px;
width: 372px;
padding: 18px;
overflow-y: auto;
overscroll-behavior: contain;
background: var(--panel);
border: 1px solid var(--panel-edge);
border-radius: 20px;
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #31525b transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
content: "";
background: var(--cyan);
box-shadow: 0 0 16px var(--cyan);
}
h1 {
margin: 8px 0 7px;
font-size: 27px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.progress-wrap {
margin: 17px 0 16px;
padding: 12px;
background: rgba(0, 0, 0, 0.18);
border: 1px solid rgba(255, 255, 255, 0.06);
border-radius: 12px;
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #b9cdd1;
font-size: 11px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.progress-track {
height: 6px;
overflow: hidden;
background: #1c3039;
border-radius: 999px;
}
#progress-fill {
width: 0%;
height: 100%;
background: linear-gradient(90deg, var(--cyan), #8ff6bd);
border-radius: inherit;
transition: width 350ms ease;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 9px;
color: #b9cdd1;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--cyan);
letter-spacing: 0;
text-transform: none;
}
.tree {
padding: 8px;
background: rgba(0, 0, 0, 0.17);
border: 1px solid rgba(255, 255, 255, 0.06);
border-radius: 13px;
}
.tree-row {
position: relative;
display: flex;
width: 100%;
align-items: center;
gap: 10px;
padding: 8px 9px;
color: #b9c9ce;
background: transparent;
border: 1px solid transparent;
border-radius: 9px;
cursor: pointer;
text-align: left;
transition: 160ms ease;
}
.tree-row:hover {
color: white;
background: rgba(78, 230, 208, 0.06);
}
.tree-row.active {
color: white;
background: rgba(78, 230, 208, 0.11);
border-color: rgba(78, 230, 208, 0.24);
}
.tree-row[data-level="wheel"] {
margin-left: 15px;
}
.tree-row[data-level="tire"] {
margin-left: 30px;
}
.tree-row[data-level="valve"] {
margin-left: 45px;
}
.tree-row[data-level="wheel"]::before,
.tree-row[data-level="tire"]::before,
.tree-row[data-level="valve"]::before {
position: absolute;
top: 50%;
left: -9px;
width: 9px;
height: 1px;
content: "";
background: #35525b;
}
.tree-icon {
display: grid;
width: 25px;
height: 25px;
flex: 0 0 auto;
place-items: center;
color: var(--cyan);
background: #112b34;
border: 1px solid #244752;
border-radius: 7px;
font-size: 10px;
font-weight: 900;
}
.tree-copy {
min-width: 0;
flex: 1;
}
.tree-copy strong {
display: block;
font-size: 12px;
}
.tree-copy small {
display: block;
margin-top: 1px;
color: #708991;
font-size: 10px;
}
.scope-note {
margin: 8px 2px 0;
color: #789099;
font-size: 10px;
line-height: 1.45;
}
#steps {
display: grid;
gap: 9px;
}
.step {
display: grid;
grid-template-columns: 32px 1fr;
gap: 10px;
padding: 11px;
background: rgba(255, 255, 255, 0.035);
border: 1px solid rgba(255, 255, 255, 0.065);
border-radius: 12px;
transition: 180ms ease;
}
.step.active {
background: rgba(78, 230, 208, 0.075);
border-color: rgba(78, 230, 208, 0.38);
box-shadow: inset 3px 0 0 var(--cyan);
}
.step.done {
opacity: 0.62;
}
.step-number {
display: grid;
width: 30px;
height: 30px;
place-items: center;
color: #abc2c7;
background: #162832;
border: 1px solid #29424b;
border-radius: 9px;
font-size: 11px;
font-weight: 900;
}
.step.active .step-number {
color: #06201e;
background: var(--cyan);
border-color: var(--cyan);
}
.step.done .step-number {
color: #092019;
background: #7be0a6;
border-color: #7be0a6;
}
.step-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 3px;
}
.step-title strong {
font-size: 12px;
letter-spacing: 0.02em;
}
.target {
padding: 3px 6px;
color: var(--yellow);
background: rgba(246, 200, 95, 0.1);
border: 1px solid rgba(246, 200, 95, 0.22);
border-radius: 5px;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.step p {
margin: 0 0 9px;
color: #829aa1;
font-size: 10.5px;
line-height: 1.45;
}
.run {
padding: 6px 9px;
color: #d9f8f4;
background: #17343d;
border: 1px solid #2e5962;
border-radius: 7px;
cursor: pointer;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.09em;
text-transform: uppercase;
transition: 150ms ease;
}
.run:hover:not(:disabled) {
color: #06201e;
background: var(--cyan);
border-color: var(--cyan);
transform: translateY(-1px);
}
.run:disabled {
cursor: default;
opacity: 0.55;
}
.panel-actions {
display: flex;
gap: 8px;
margin-top: 14px;
}
.secondary {
flex: 1;
padding: 9px;
color: #abc1c7;
background: rgba(255, 255, 255, 0.04);
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 9px;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.secondary:hover {
color: white;
border-color: rgba(78, 230, 208, 0.35);
}
#hud {
position: fixed;
z-index: 5;
top: 18px;
right: 18px;
display: grid;
width: 238px;
gap: 7px;
pointer-events: none;
}
.hud-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 9px 11px;
color: #91a8ae;
background: rgba(9, 20, 28, 0.78);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 9px;
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.hud-row strong {
color: var(--cyan);
font-size: 10px;
}
.camera-hint {
margin-top: 4px;
color: rgba(207, 228, 230, 0.52);
font-size: 9px;
line-height: 1.45;
text-align: right;
letter-spacing: 0.05em;
text-transform: uppercase;
}
#toast {
position: fixed;
z-index: 30;
top: 22px;
left: 50%;
max-width: min(460px, calc(100vw - 32px));
padding: 11px 16px;
color: white;
background: rgba(20, 37, 47, 0.96);
border: 1px solid rgba(78, 230, 208, 0.35);
border-radius: 10px;
box-shadow: var(--shadow);
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition: 180ms ease;
font-size: 12px;
font-weight: 700;
text-align: center;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
border-color: rgba(255, 111, 120, 0.55);
}
#final {
position: fixed;
z-index: 40;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(3, 9, 13, 0.66);
opacity: 0;
pointer-events: none;
backdrop-filter: blur(7px);
transition: 250ms ease;
}
#final.show {
opacity: 1;
pointer-events: auto;
}
.final-card {
width: min(420px, 100%);
padding: 30px;
background: #10232c;
border: 1px solid rgba(78, 230, 208, 0.38);
border-radius: 20px;
box-shadow: var(--shadow);
text-align: center;
transform: translateY(12px) scale(0.98);
transition: 250ms ease;
}
#final.show .final-card {
transform: translateY(0) scale(1);
}
.final-icon {
display: grid;
width: 58px;
height: 58px;
margin: 0 auto 15px;
place-items: center;
color: #092019;
background: linear-gradient(135deg, #8ff6bd, var(--cyan));
border-radius: 18px;
box-shadow: 0 0 28px rgba(78, 230, 208, 0.24);
font-size: 27px;
font-weight: 900;
}
.final-card h2 {
margin: 0 0 8px;
font-size: 25px;
}
.final-card p {
margin: 0 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.final-card button {
width: 100%;
padding: 11px;
color: #06201e;
background: var(--cyan);
border: 0;
border-radius: 10px;
cursor: pointer;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.target-pulse {
animation: target-pulse 650ms ease;
}
@keyframes target-pulse {
0%, 100% { box-shadow: none; }
35% {
box-shadow: 0 0 0 3px rgba(246, 200, 95, 0.42);
background: rgba(246, 200, 95, 0.12);
}
}
@media (max-width: 760px) {
#panel {
top: auto;
right: 8px;
bottom: 8px;
left: 8px;
width: auto;
max-height: 49vh;
padding: 14px;
border-radius: 16px;
}
h1 {
font-size: 22px;
}
.intro,
.scope-note {
display: none;
}
.progress-wrap {
margin: 12px 0;
}
.tree {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 4px;
}
.tree-row,
.tree-row[data-level="wheel"],
.tree-row[data-level="tire"],
.tree-row[data-level="valve"] {
margin: 0;
padding: 7px 4px;
}
.tree-row::before,
.tree-copy small {
display: none;
}
.tree-icon {
width: 22px;
height: 22px;
}
.tree-copy strong {
font-size: 9px;
text-align: center;
}
#hud {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel bicycle repair scene"></main>
<aside id="panel">
<div class="eyebrow">Workshop Task 07</div>
<h1>Voxel Bicycle Repair</h1>
<p class="intro">
Select the requested hierarchy level, then execute each repair instruction.
A command only runs on its exact target; descendants inherit parent transforms.
</p>
<div class="progress-wrap">
<div class="progress-label">
<span>Repair progress</span>
<span id="progress-text">0 / 7</span>
</div>
<div class="progress-track">
<div id="progress-fill"></div>
</div>
</div>
<div class="section-title">
<span>Request level</span>
<span id="selected-level">Bicycle</span>
</div>
<div class="tree" role="group" aria-label="Scene hierarchy">
<button class="tree-row active" data-level="bicycle" aria-pressed="true">
<span class="tree-icon">BC</span>
<span class="tree-copy">
<strong>Bicycle</strong>
<small>Assembly root</small>
</span>
</button>
<button class="tree-row" data-level="wheel" aria-pressed="false">
<span class="tree-icon">WH</span>
<span class="tree-copy">
<strong>Wheel</strong>
<small>Front wheel assembly</small>
</span>
</button>
<button class="tree-row" data-level="tire" aria-pressed="false">
<span class="tree-icon">TR</span>
<span class="tree-copy">
<strong>Tire</strong>
<small>Wheel child</small>
</span>
</button>
<button class="tree-row" data-level="valve" aria-pressed="false">
<span class="tree-icon">VL</span>
<span class="tree-copy">
<strong>Valve</strong>
<small>Tire child</small>
</span>
</button>
</div>
<p class="scope-note">
Exact-level execution: selecting Wheel does not directly modify the Tire or Valve.
Those nodes move only when they inherit a parent transform.
</p>
<div class="section-title">
<span>Repair sequence</span>
<span id="active-step">Step 1</span>
</div>
<div id="steps"></div>
<div class="panel-actions">
<button class="secondary" id="reset-button">Reset repair</button>
<button class="secondary" id="camera-button">Reset camera</button>
</div>
</aside>
<div id="hud">
<div class="hud-row">
<span>Pressure</span>
<strong id="pressure-value">32 PSI</strong>
</div>
<div class="hud-row">
<span>Valve</span>
<strong id="valve-value">Mounted · Dirty</strong>
</div>
<div class="hud-row">
<span>Tire</span>
<strong id="tire-value">Loose</strong>
</div>
<div class="hud-row">
<span>Alignment</span>
<strong id="alignment-value">−4.3°</strong>
</div>
<div class="camera-hint">
Drag to orbit · Scroll to zoom<br />
Arrow keys rotate · + / − zoom
</div>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="final" aria-hidden="true">
<div class="final-card">
<div class="final-icon">✓</div>
<h2>Bicycle repaired</h2>
<p>
Every instruction ran at its requested hierarchy level. Parent-child
transforms remained consistent throughout the repair.
</p>
<button id="final-reset">Repair it again</button>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// UI and puzzle state
// -------------------------------------------------------------------------
const $ = (selector) => document.querySelector(selector);
const steps = [
{
target: "valve",
title: "Detach valve",
description:
"Remove the valve from the tire while preserving its current world transform during reparenting."
},
{
target: "valve",
title: "Clean valve",
description:
"Clear debris from the valve stem and restore the brass finish. This changes only the valve."
},
{
target: "valve",
title: "Install valve",
description:
"Return the valve to the tire. The valve becomes a child of the Tire again."
},
{
target: "tire",
title: "Seat tire",
description:
"Move the tire onto the wheel rim. The installed valve inherits the Tire transform."
},
{
target: "tire",
title: "Inflate tire",
description:
"Raise the tire from 32 to 100 PSI. Only the Tire's local scale is animated."
},
{
target: "wheel",
title: "True wheel",
description:
"Correct the front wheel angle. The Tire and Valve inherit the Wheel rotation."
},
{
target: "bicycle",
title: "Test bicycle",
description:
"Run a final balance check on the complete Bicycle assembly."
}
];
const levelNames = {
bicycle: "Bicycle",
wheel: "Wheel",
tire: "Tire",
valve: "Valve"
};
const state = {
selectedLevel: "bicycle",
completed: 0,
animating: false,
pressure: 32,
alignment: -4.3,
tween: null,
particles: []
};
const sceneHost = $("#scene");
const stepList = $("#steps");
const toastElement = $("#toast");
let toastTimer = 0;
function toast(message, error = false) {
clearTimeout(toastTimer);
toastElement.textContent = message;
toastElement.classList.toggle("error", error);
toastElement.classList.add("show");
toastTimer = setTimeout(() => {
toastElement.classList.remove("show");
}, 2600);
}
function renderSteps() {
stepList.innerHTML = steps
.map((step, index) => {
const done = index < state.completed;
const active = index === state.completed;
const status = done ? "Done" : active ? "Execute" : "Locked";
return `
<article class="step ${done ? "done" : ""} ${active ? "active" : ""}">
<div class="step-number">${done ? "✓" : index + 1}</div>
<div>
<div class="step-title">
<strong>${step.title}</strong>
<span class="target">${step.target}</span>
</div>
<p>${step.description}</p>
<button
class="run"
data-step="${index}"
${done || state.animating ? "disabled" : ""}
>
${status}
</button>
</div>
</article>
`;
})
.join("");
$("#progress-fill").style.width = `${(state.completed / steps.length) * 100}%`;
$("#progress-text").textContent = `${state.completed} / ${steps.length}`;
$("#active-step").textContent =
state.completed >= steps.length ? "Complete" : `Step ${state.completed + 1}`;
}
stepList.addEventListener("click", (event) => {
const button = event.target.closest("[data-step]");
if (!button) return;
executeStep(Number(button.dataset.step));
});
// -------------------------------------------------------------------------
// Three.js core scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.Fog(0x071018, 9, 19);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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 hemisphere = new THREE.HemisphereLight(0xbcecff, 0x172027, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d4, 3.4);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55d9d1, 2.3);
rimLight.position.set(-4, 4, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa15e, 20, 9, 2);
warmLight.position.set(-3.5, 3.2, -1.5);
scene.add(warmLight);
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0xff7055,
roughness: 0.56,
metalness: 0.14
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x273942,
roughness: 0.68,
metalness: 0.16
}),
cream: new THREE.MeshStandardMaterial({
color: 0xece5cf,
roughness: 0.78
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x202930,
roughness: 0.96
}),
rim: new THREE.MeshStandardMaterial({
color: 0x9eb2b5,
roughness: 0.34,
metalness: 0.75
}),
spoke: new THREE.MeshStandardMaterial({
color: 0x70868b,
roughness: 0.4,
metalness: 0.62
}),
brass: new THREE.MeshStandardMaterial({
color: 0xc89a47,
roughness: 0.34,
metalness: 0.72
}),
dirt: new THREE.MeshStandardMaterial({
color: 0x554a35,
roughness: 1
}),
bench: new THREE.MeshStandardMaterial({
color: 0x674c39,
roughness: 0.92
}),
benchDark: new THREE.MeshStandardMaterial({
color: 0x27383d,
roughness: 0.72,
metalness: 0.2
}),
teal: new THREE.MeshStandardMaterial({
color: 0x45b9b1,
roughness: 0.5,
metalness: 0.18
}),
green: new THREE.MeshStandardMaterial({
color: 0x79e6a4,
roughness: 0.42,
emissive: 0x173d25,
emissiveIntensity: 0.8
})
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBeam(parent, start, end, thickness, material) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(thickness, length, thickness),
material
);
mesh.position.copy(a).add(b).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;
}
function addVoxelRing(parent, radius, count, radialSize, tangentSize, depth, material) {
const group = new THREE.Group();
parent.add(group);
for (let i = 0; i < count; i++) {
const angle = (i / count) * Math.PI * 2;
const segment = addBox(
group,
[radialSize, tangentSize, depth],
[Math.cos(angle) * radius, Math.sin(angle) * radius, 0],
material
);
segment.rotation.z = angle;
}
return group;
}
// -------------------------------------------------------------------------
// Environment and workshop
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 32),
new THREE.MeshStandardMaterial({
color: 0x101b20,
roughness: 0.92
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.105;
floor.receiveShadow = true;
scene.add(floor);
const tileGeometry = new THREE.BoxGeometry(1.16, 0.08, 1.16);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
});
const tileCount = 11 * 8;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const tileMatrix = new THREE.Matrix4();
let tileIndex = 0;
for (let x = -5; x <= 5; x++) {
for (let z = -4; z <= 3; z++) {
tileMatrix.makeTranslation(x * 1.2, -0.065, z * 1.2);
tiles.setMatrixAt(tileIndex, tileMatrix);
const color = new THREE.Color(
(x + z) % 2 === 0 ? 0x1b2a30 : 0x16252b
);
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
const grid = new THREE.GridHelper(24, 24, 0x35515a, 0x20343b);
grid.position.y = -0.02;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// Workbench.
const bench = new THREE.Group();
bench.position.set(-2.8, 0, -2.25);
scene.add(bench);
addBox(bench, [3.2, 0.18, 0.82], [0, 1.25, 0], materials.bench);
addBox(bench, [0.18, 1.3, 0.18], [-1.35, 0.62, 0.23], materials.benchDark);
addBox(bench, [0.18, 1.3, 0.18], [1.35, 0.62, 0.23], materials.benchDark);
addBox(bench, [0.18, 1.3, 0.18], [-1.35, 0.62, -0.23], materials.benchDark);
addBox(bench, [0.18, 1.3, 0.18], [1.35, 0.62, -0.23], materials.benchDark);
// Pegboard.
addBox(bench, [3.15, 1.45, 0.12], [0, 2.06, -0.38], materials.benchDark);
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 10; column++) {
addBox(
bench,
[0.055, 0.055, 0.02],
[-1.25 + column * 0.275, 1.58 + row * 0.3, -0.3],
materials.spoke,
false
);
}
}
// Voxel tools on the pegboard.
addBox(bench, [0.12, 0.52, 0.09], [-0.75, 2.05, -0.24], materials.teal);
addBox(bench, [0.32, 0.12, 0.09], [-0.64, 2.26, -0.24], materials.teal);
addBox(bench, [0.1, 0.46, 0.08], [-0.2, 2.04, -0.24], materials.cream);
addBox(bench, [0.25, 0.1, 0.08], [-0.2, 2.27, -0.24], materials.cream);
// Toolboxes and loose blocks.
addBox(bench, [0.72, 0.38, 0.55], [0.85, 1.53, 0], materials.teal);
addBox(bench, [0.32, 0.08, 0.03], [0.85, 1.55, 0.29], materials.cream);
addBox(bench, [0.27, 0.18, 0.26], [-0.72, 1.43, 0.1], materials.brass);
addBox(bench, [0.18, 0.32, 0.18], [-0.25, 1.5, 0.13], materials.frame);
// Repair stand.
const stand = new THREE.Group();
stand.position.set(-0.2, 0, -0.82);
scene.add(stand);
addBox(stand, [0.18, 2.15, 0.18], [0, 1.02, 0], materials.benchDark);
addBox(stand, [1.05, 0.14, 0.16], [0, 1.83, 0], materials.teal);
addBox(stand, [0.18, 0.14, 1.0], [0, 0.06, 0], materials.benchDark);
addBox(stand, [0.18, 0.14, 0.65], [0, 0.06, 0.3], materials.benchDark);
addBox(stand, [0.32, 0.32, 0.24], [0, 1.85, 0.05], materials.frameDark);
// -------------------------------------------------------------------------
// Bicycle hierarchy:
//
// Bicycle
// ├── Rear wheel (decoration)
// └── Wheel (front repair assembly)
// └── Tire
// └── Valve
// -------------------------------------------------------------------------
const bicycle = new THREE.Group();
bicycle.name = "Bicycle";
bicycle.position.set(0, 0.62, 0);
bicycle.rotation.y = -0.16;
scene.add(bicycle);
function buildWheel(parent, withValve) {
const wheel = new THREE.Group();
parent.add(wheel);
// Hub and axle.
addBox(wheel, [0.22, 0.16, 0.28], [0, 0, 0], materials.rim);
addBox(wheel, [0.1, 0.1, 0.48], [0, 0, 0], materials.frameDark);
// Voxel rim.
addVoxelRing(wheel, 0.425, 20, 0.065, 0.15, 0.105, materials.rim);
// Radial spokes.
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
addBeam(
wheel,
[0, 0, 0.025],
[Math.cos(angle) * 0.4, Math.sin(angle) * 0.4, 0.025],
0.024,
materials.spoke
);
}
const tire = new THREE.Group();
wheel.add(tire);
const rubberMaterial = withValve
? materials.rubber.clone()
: materials.rubber;
addVoxelRing(tire, 0.5, 24, 0.14, 0.18, 0.19, rubberMaterial);
let valve = null;
if (withValve) {
const angle = 0.55;
valve = new THREE.Group();
valve.name = "Valve";
valve.position.set(
Math.cos(angle) * 0.39,
Math.sin(angle) * 0.39,
0.13
);
valve.rotation.z = angle - Math.PI / 2;
tire.add(valve);
const capMaterial = materials.brass.clone();
addBox(valve, [0.055, 0.17, 0.055], [0, 0.025, 0], capMaterial);
addBox(valve, [0.095, 0.06, 0.095], [0, -0.105, 0], capMaterial);
const dirt = addBox(
valve,
[0.112, 0.08, 0.112],
[0, -0.104, 0],
materials.dirt
);
valve.userData.capMaterial = capMaterial;
valve.userData.dirt = dirt;
}
return { wheel, tire, valve, rubberMaterial };
}
const rearWheelData = buildWheel(bicycle, false);
rearWheelData.wheel.name = "Rear Wheel";
rearWheelData.wheel.position.set(-1.25, 0, 0);
rearWheelData.wheel.rotation.z = 0.015;
const frontWheelData = buildWheel(bicycle, true);
const frontWheel = frontWheelData.wheel;
const frontTire = frontWheelData.tire;
const valve = frontWheelData.valve;
frontWheel.name = "Wheel";
frontWheel.position.set(1.25, 0, 0);
frontWheel.rotation.z = -0.075;
frontTire.name = "Tire";
frontTire.scale.set(0.92, 0.92, 1);
frontTire.position.z = 0.035;
// Frame geometry.
const rearAxle = [-1.25, 0, 0];
const frontAxle = [1.25, 0, 0];
const crank = [-0.2, -0.16, 0];
const seatJoint = [-0.58, 0.91, 0];
const headTop = [0.8, 0.85, 0];
const headBottom = [0.91, 0.5, 0];
addBeam(bicycle, rearAxle, crank, 0.085, materials.frame);
addBeam(bicycle, crank, seatJoint, 0.085, materials.frame);
addBeam(bicycle, seatJoint, rearAxle, 0.085, materials.frame);
addBeam(bicycle, seatJoint, headTop, 0.085, materials.frame);
addBeam(bicycle, crank, headBottom, 0.095, materials.frame);
addBeam(bicycle, headTop, headBottom, 0.085, materials.frame);
addBeam(bicycle, headBottom, frontAxle, 0.075, materials.frame);
// Seat.
addBeam(bicycle, seatJoint, [-0.68, 1.16, 0], 0.075, materials.rim);
addBox(bicycle, [0.42, 0.11, 0.24], [-0.73, 1.2, 0], materials.cream);
addBox(bicycle, [0.25, 0.055, 0.17], [-0.75, 1.27, 0], materials.frameDark);
// Handlebar.
addBeam(bicycle, headTop, [0.72, 1.14, 0], 0.075, materials.rim);
addBox(bicycle, [0.12, 0.09, 0.68], [0.72, 1.14, 0], materials.frameDark);
addBox(bicycle, [0.19, 0.1, 0.16], [0.72, 1.14, 0.35], materials.cream);
addBox(bicycle, [0.19, 0.1, 0.16], [0.72, 1.14, -0.35], materials.cream);
// Crank, chainring and pedals.
addVoxelRing(bicycle, 0.22, 8, 0.06, 0.1, 0.1, materials.rim);
addBox(bicycle, [0.08, 0.34, 0.09], [-0.2, -0.16, 0.13], materials.rim);
addBox(bicycle, [0.28, 0.08, 0.18], [-0.07, -0.31, 0.17], materials.frameDark);
addBox(bicycle, [0.08, 0.29, 0.09], [-0.2, -0.16, -0.13], materials.rim);
addBox(bicycle, [0.28, 0.08, 0.18], [-0.33, -0.01, -0.17], materials.frameDark);
// Chain approximated with voxel links.
for (let i = 0; i < 12; i++) {
const t = i / 11;
const x = THREE.MathUtils.lerp(-1.2, -0.2, t);
const y = THREE.MathUtils.lerp(-0.08, -0.25, t);
addBox(bicycle, [0.08, 0.045, 0.045], [x, y, 0.12], materials.spoke);
}
// Small beacon, revealed only by the Bicycle-level test.
const beacon = addBox(
bicycle,
[0.13, 0.13, 0.13],
[0.08, 0.76, 0.16],
materials.green,
false
);
beacon.visible = false;
const nodes = {
bicycle,
wheel: frontWheel,
tire: frontTire,
valve
};
// -------------------------------------------------------------------------
// Selection helper
// -------------------------------------------------------------------------
let selectionHelper = null;
function selectLevel(level) {
state.selectedLevel = level;
document.querySelectorAll("[data-level]").forEach((button) => {
const active = button.dataset.level === level;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
$("#selected-level").textContent = levelNames[level];
if (selectionHelper) {
scene.remove(selectionHelper);
selectionHelper.geometry.dispose();
selectionHelper.material.dispose();
}
selectionHelper = new THREE.BoxHelper(nodes[level], 0x4ee6d0);
selectionHelper.material.transparent = true;
selectionHelper.material.opacity = 0.78;
selectionHelper.material.depthTest = false;
selectionHelper.renderOrder = 10;
scene.add(selectionHelper);
}
document.querySelectorAll("[data-level]").forEach((button) => {
button.addEventListener("click", () => {
selectLevel(button.dataset.level);
});
});
// -------------------------------------------------------------------------
// Lightweight tween system
// -------------------------------------------------------------------------
const easeInOutCubic = (t) =>
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
function startTween(duration, update, complete) {
state.tween = {
start: performance.now(),
duration,
update,
complete
};
}
function updateTween(now) {
if (!state.tween) return;
const tween = state.tween;
const raw = THREE.MathUtils.clamp(
(now - tween.start) / tween.duration,
0,
1
);
tween.update(easeInOutCubic(raw), raw);
if (raw >= 1) {
state.tween = null;
if (tween.complete) tween.complete();
}
}
// -------------------------------------------------------------------------
// Strict, exact-level instruction execution
// -------------------------------------------------------------------------
function executeStep(index) {
if (state.animating) return;
const step = steps[index];
// The requested level must exactly match the instruction target.
if (state.selectedLevel !== step.target) {
toast(
`This instruction is scoped to ${levelNames[step.target]}. Select ${levelNames[step.target]} first.`,
true
);
const targetButton = document.querySelector(
`[data-level="${step.target}"]`
);
targetButton.classList.remove("target-pulse");
void targetButton.offsetWidth;
targetButton.classList.add("target-pulse");
return;
}
if (index !== state.completed) {
toast(
`Step ${index + 1} is locked. Complete step ${state.completed + 1} first.`,
true
);
return;
}
state.animating = true;
renderSteps();
switch (index) {
case 0:
detachValve();
break;
case 1:
cleanValve();
break;
case 2:
installValve();
break;
case 3:
seatTire();
break;
case 4:
inflateTire();
break;
case 5:
trueWheel();
break;
case 6:
testBicycle();
break;
}
}
function completeStep(index) {
state.completed = Math.max(state.completed, index + 1);
state.animating = false;
updateStatus();
renderSteps();
if (state.completed === steps.length) {
toast("Repair complete — every scope was executed correctly.");
createConfetti();
setTimeout(() => {
$("#final").classList.add("show");
$("#final").setAttribute("aria-hidden", "false");
}, 550);
} else {
toast(`${steps[index].title} completed at the ${levelNames[stepTarget(index)]} level.`);
}
}
function stepTarget(index) {
return steps[index].target;
}
function detachValve() {
bicycle.updateMatrixWorld(true);
/*
* Object3D.attach preserves the object's world transform while changing
* its parent. The valve is therefore detached without a visual jump.
*/
scene.attach(valve);
const startPosition = valve.position.clone();
const startQuaternion = valve.quaternion.clone();
const endPosition = new THREE.Vector3(0.42, 1.48, 0.72);
const endQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(-0.12, 0.28, -0.18)
);
startTween(
850,
(t) => {
valve.position.lerpVectors(startPosition, endPosition, t);
valve.quaternion.slerpQuaternions(
startQuaternion,
endQuaternion,
t
);
},
() => completeStep(0)
);
}
function cleanValve() {
const startScale = valve.scale.clone();
startTween(
650,
(t, raw) => {
const pulse = 1 + Math.sin(raw * Math.PI) * 0.12;
valve.scale.set(pulse, pulse, pulse);
if (raw > 0.42) {
valve.userData.dirt.visible = false;
}
},
() => {
valve.scale.copy(startScale);
valve.userData.capMaterial.color.setHex(0xf2c75c);
completeStep(1);
}
);
}
function installValve() {
/*
* Reattach to Tire while preserving the current world pose, then animate
* to the valve's canonical local mounting transform inside the Tire.
*/
frontTire.attach(valve);
const startPosition = valve.position.clone();
const startQuaternion = valve.quaternion.clone();
const angle = 0.55;
const endPosition = new THREE.Vector3(
Math.cos(angle) * 0.39,
Math.sin(angle) * 0.39,
0.13
);
const endQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, 0, angle - Math.PI / 2)
);
startTween(
900,
(t) => {
valve.position.lerpVectors(startPosition, endPosition, t);
valve.quaternion.slerpQuaternions(
startQuaternion,
endQuaternion,
t
);
},
() => completeStep(2)
);
}
function seatTire() {
const startZ = frontTire.position.z;
startTween(
750,
(t) => {
frontTire.position.z = THREE.MathUtils.lerp(startZ, 0, t);
},
() => {
frontTire.position.z = 0;
completeStep(3);
}
);
}
function inflateTire() {
const startScale = frontTire.scale.x;
startTween(
1100,
(t) => {
const scale = THREE.MathUtils.lerp(startScale, 1.01, t);
frontTire.scale.set(scale, scale, 1);
state.pressure = Math.round(THREE.MathUtils.lerp(32, 100, t));
updateStatus();
},
() => {
frontTire.scale.set(1.01, 1.01, 1);
state.pressure = 100;
completeStep(4);
}
);
}
function trueWheel() {
const startRotation = frontWheel.rotation.z;
startTween(
850,
(t) => {
frontWheel.rotation.z = THREE.MathUtils.lerp(
startRotation,
0,
t
);
state.alignment = THREE.MathUtils.radToDeg(frontWheel.rotation.z);
updateStatus();
},
() => {
frontWheel.rotation.z = 0;
state.alignment = 0;
completeStep(5);
}
);
}
function testBicycle() {
const baseY = bicycle.position.y;
startTween(
900,
(t) => {
bicycle.position.y = baseY + Math.sin(t * Math.PI) * 0.075;
},
() => {
bicycle.position.y = baseY;
beacon.visible = true;
completeStep(6);
}
);
}
// -------------------------------------------------------------------------
// Status panel
// -------------------------------------------------------------------------
function updateStatus() {
$("#pressure-value").textContent = `${state.pressure} PSI`;
$("#alignment-value").textContent =
`${state.alignment < 0 ? "−" : ""}${Math.abs(state.alignment).toFixed(1)}°`;
let valveStatus;
if (state.completed === 0) valveStatus = "Mounted · Dirty";
else if (state.completed === 1) valveStatus = "Detached · Dirty";
else if (state.completed === 2) valveStatus = "Detached · Clean";
else valveStatus = "Installed · Clean";
$("#valve-value").textContent = valveStatus;
let tireStatus = "Loose";
if (state.completed >= 4) tireStatus = "Seated";
if (state.completed >= 5) tireStatus = "Seated · Inflated";
$("#tire-value").textContent = tireStatus;
}
// -------------------------------------------------------------------------
// Completion particles
// -------------------------------------------------------------------------
const confettiGeometry = new THREE.BoxGeometry(0.09, 0.09, 0.09);
const confettiColors = [
0x4ee6d0,
0xff7055,
0xf6c85f,
0x8ff6bd,
0xffffff
];
function createConfetti() {
for (let i = 0; i < 42; i++) {
const material = new THREE.MeshStandardMaterial({
color: confettiColors[i % confettiColors.length],
roughness: 0.65
});
const piece = new THREE.Mesh(confettiGeometry, material);
piece.position.set(
THREE.MathUtils.randFloatSpread(2.8),
THREE.MathUtils.randFloat(1.4, 2.6),
THREE.MathUtils.randFloatSpread(1.4)
);
piece.castShadow = true;
scene.add(piece);
state.particles.push({
mesh: piece,
velocity: new THREE.Vector3(
THREE.MathUtils.randFloatSpread(2.4),
THREE.MathUtils.randFloat(1.5, 4.2),
THREE.MathUtils.randFloatSpread(1.7)
),
spin: new THREE.Vector3(
THREE.MathUtils.randFloatSpread(5),
THREE.MathUtils.randFloatSpread(5),
THREE.MathUtils.randFloatSpread(5)
),
life: 2.8
});
}
}
function updateParticles(dt) {
for (let i = state.particles.length - 1; i >= 0; i--) {
const particle = state.particles[i];
particle.life -= dt;
if (particle.life <= 0) {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
state.particles.splice(i, 1);
continue;
}
particle.velocity.y -= 4.5 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += particle.spin.x * dt;
particle.mesh.rotation.y += particle.spin.y * dt;
particle.mesh.rotation.z += particle.spin.z * dt;
}
}
// -------------------------------------------------------------------------
// Direct camera controls; no examples or addons are imported.
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.5,
pitch: 0.3,
radius: 7.2,
target: new THREE.Vector3(0, 0.92, 0)
};
function resetCamera() {
cameraState.yaw = 0.5;
cameraState.pitch = 0.3;
cameraState.radius = 7.2;
}
function updateCamera() {
const narrowFactor = camera.aspect < 0.8 ? 1.32 : 1;
const radius = cameraState.radius * narrowFactor;
const horizontal = Math.cos(cameraState.pitch) * radius;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * radius,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
const pointer = {
active: false,
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
-0.05,
1.15
);
pointer.x = event.clientX;
pointer.y = event.clientY;
});
function releasePointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.006,
4.1,
11
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") cameraState.yaw += 0.09;
else if (key === "arrowright") cameraState.yaw -= 0.09;
else if (key === "arrowup") {
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + 0.06,
-0.05,
1.15
);
} else if (key === "arrowdown") {
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - 0.06,
-0.05,
1.15
);
} else if (key === "+" || key === "=") {
cameraState.radius = Math.max(4.1, cameraState.radius - 0.35);
} else if (key === "-") {
cameraState.radius = Math.min(11, cameraState.radius + 0.35);
} else {
return;
}
event.preventDefault();
});
// -------------------------------------------------------------------------
// Reset
// -------------------------------------------------------------------------
function resetRepair() {
state.tween = null;
state.animating = false;
state.completed = 0;
state.pressure = 32;
state.alignment = -4.3;
// Restore the Valve as a child of the Tire with its canonical local pose.
frontTire.attach(valve);
const angle = 0.55;
valve.position.set(
Math.cos(angle) * 0.39,
Math.sin(angle) * 0.39,
0.13
);
valve.rotation.set(0, 0, angle - Math.PI / 2);
valve.scale.set(1, 1, 1);
valve.userData.dirt.visible = true;
valve.userData.capMaterial.color.setHex(0xc89a47);
frontTire.scale.set(0.92, 0.92, 1);
frontTire.position.z = 0.035;
frontWheel.rotation.z = -0.075;
bicycle.position.y = 0.62;
beacon.visible = false;
for (const particle of state.particles) {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
}
state.particles.length = 0;
$("#final").classList.remove("show");
$("#final").setAttribute("aria-hidden", "true");
selectLevel("bicycle");
updateStatus();
renderSteps();
toast("Repair reset. Begin with Step 1.");
}
$("#reset-button").addEventListener("click", resetRepair);
$("#final-reset").addEventListener("click", resetRepair);
$("#camera-button").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
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);
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
updateTween(now);
updateParticles(dt);
updateCamera();
if (selectionHelper) {
selectionHelper.update();
}
renderer.render(scene, camera);
}
selectLevel("bicycle");
updateStatus();
renderSteps();
requestAnimationFrame(animate);
setTimeout(() => {
toast("Select the Valve level, then execute Step 1.");
}, 500);
</script>
</body>
</html>
```