Prompt specification
Build or customize the Three.js 3D scene "Multi-stage geometry maze toy" 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: Multi-stage geometry maze toy
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/multi-stage-geometry-maze-toy-x0376
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a shape sorter. Create a transparent box with several profile gates; users rotate and translate one block through compatible openings while a path trace records the successful sequence. Voxel style.
Working Three.js Starter (multi-stage-geometry-maze-toy-x0376):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Pass — Profile Gate Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #06111d;
--panel: rgba(8, 22, 36, 0.82);
--line: rgba(132, 218, 255, 0.2);
--text: #eaf8ff;
--muted: #7fa4b7;
--cyan: #45e0d1;
--violet: #8d7bff;
--amber: #ffb84d;
--pink: #ff6f91;
--danger: #ff5d73;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 18%, #12314a 0%, transparent 35%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
user-select: none;
}
button,
input {
font: inherit;
}
button {
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border: 1px solid rgba(69, 224, 209, 0.55);
background: rgba(69, 224, 209, 0.08);
box-shadow: 0 0 24px rgba(69, 224, 209, 0.12);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--cyan);
box-shadow: 0 0 10px rgba(69, 224, 209, 0.6);
}
.brand-mark::before {
left: 8px;
right: 8px;
top: 8px;
height: 7px;
}
.brand-mark::after {
top: 14px;
bottom: 7px;
left: 16px;
width: 7px;
}
.brand h1 {
margin: 0;
font-size: 17px;
font-weight: 800;
letter-spacing: 0.16em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.19em;
}
.status-panel {
position: absolute;
top: 92px;
left: 26px;
width: 282px;
padding: 17px;
border: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(69, 224, 209, 0.06), transparent 42%),
var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
}
.status-panel::before {
content: "";
position: absolute;
top: -1px;
left: -1px;
width: 42px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(69, 224, 209, 0.7);
}
.status-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font: 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.15em;
}
#gateCounter {
color: var(--text);
}
#sequence {
display: grid;
gap: 6px;
margin: 0;
padding: 0;
list-style: none;
}
#sequence li {
display: grid;
grid-template-columns: 26px 22px 1fr auto;
align-items: center;
gap: 8px;
min-height: 44px;
padding: 7px 8px;
border: 1px solid transparent;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
transition: 160ms ease;
}
#sequence li.current {
border-color: rgba(69, 224, 209, 0.25);
background: rgba(69, 224, 209, 0.055);
color: var(--text);
}
#sequence li.passed {
color: #a6e8df;
background: rgba(69, 224, 209, 0.08);
}
.step-number {
font: 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
opacity: 0.7;
}
.mini-t {
position: relative;
display: inline-block;
width: 17px;
height: 17px;
transform: rotate(var(--rotation, 0deg));
transition: transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.mini-t::before,
.mini-t::after {
content: "";
position: absolute;
background: currentColor;
}
.mini-t::before {
left: 0;
top: 1px;
width: 17px;
height: 5px;
}
.mini-t::after {
left: 6px;
top: 5px;
width: 5px;
height: 12px;
}
.gate-name {
display: grid;
gap: 2px;
}
.gate-name b {
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
}
.gate-name small {
color: var(--muted);
font: 9px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.step-state {
font: 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
opacity: 0.65;
}
#sequence li.passed .step-state {
color: var(--cyan);
opacity: 1;
}
#sequence li.current .step-state {
color: var(--text);
opacity: 1;
}
.alignment {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.06);
color: var(--muted);
font: 9px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.04em;
}
#alignmentDot {
width: 7px;
height: 7px;
flex: 0 0 auto;
background: var(--muted);
box-shadow: 0 0 10px currentColor;
}
#alignmentDot.good {
background: var(--cyan);
}
#alignmentDot.warn {
background: var(--amber);
}
#message {
min-height: 47px;
margin-top: 10px;
color: #a9c2cf;
font-size: 11px;
line-height: 1.45;
}
#message[data-tone="good"] {
color: #9de9df;
}
#message[data-tone="bad"] {
color: #ff9aaa;
}
.camera-controls {
position: absolute;
top: 24px;
right: 26px;
display: flex;
gap: 7px;
pointer-events: auto;
}
.icon-button,
.control-button {
border: 1px solid var(--line);
background: rgba(8, 22, 36, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
cursor: pointer;
transition:
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
.icon-button:hover,
.control-button:hover {
border-color: rgba(69, 224, 209, 0.7);
background: rgba(21, 52, 69, 0.9);
}
.icon-button:active,
.control-button:active {
transform: translateY(2px);
}
.icon-button {
width: 38px;
height: 38px;
color: var(--muted);
font: 17px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.control-deck {
position: absolute;
left: 50%;
bottom: 24px;
width: min(680px, calc(100vw - 40px));
padding: 13px;
transform: translateX(-50%);
border: 1px solid var(--line);
background:
linear-gradient(110deg, rgba(141, 123, 255, 0.055), transparent 40%),
rgba(8, 22, 36, 0.9);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.deck-row {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 15px;
}
.control-button {
min-width: 42px;
height: 38px;
padding: 0 12px;
color: var(--text);
font: 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
}
.control-button.primary {
border-color: rgba(69, 224, 209, 0.45);
color: #baf8ef;
background: rgba(69, 224, 209, 0.08);
}
.depth-control {
display: grid;
grid-template-columns: 1fr auto;
gap: 4px 10px;
align-items: center;
min-width: 0;
}
.depth-control label,
#depthValue {
color: var(--muted);
font: 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.12em;
}
#depthValue {
color: var(--text);
}
#depthRange {
grid-column: 1 / -1;
width: 100%;
height: 4px;
margin: 7px 0 3px;
appearance: none;
border-radius: 0;
background: linear-gradient(
90deg,
rgba(69, 224, 209, 0.75),
rgba(141, 123, 255, 0.65)
);
cursor: ew-resize;
}
#depthRange::-webkit-slider-thumb {
width: 12px;
height: 18px;
appearance: none;
border: 1px solid #baf8ef;
border-radius: 0;
background: #102d3b;
box-shadow: 0 0 12px rgba(69, 224, 209, 0.45);
}
#depthRange::-moz-range-thumb {
width: 12px;
height: 18px;
border: 1px solid #baf8ef;
border-radius: 0;
background: #102d3b;
}
.button-pair {
display: flex;
gap: 6px;
}
.hint {
position: absolute;
left: 26px;
bottom: 29px;
max-width: 235px;
color: rgba(173, 207, 220, 0.62);
font: 9px/1.7 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.055em;
}
.hint b {
color: rgba(218, 246, 255, 0.85);
font-weight: 600;
}
#completeBanner {
position: absolute;
top: 50%;
left: 50%;
width: min(430px, calc(100vw - 40px));
padding: 25px;
transform: translate(-50%, -46%) scale(0.96);
border: 1px solid rgba(69, 224, 209, 0.45);
background:
linear-gradient(135deg, rgba(69, 224, 209, 0.13), rgba(141, 123, 255, 0.08)),
rgba(5, 18, 29, 0.94);
box-shadow:
0 0 80px rgba(69, 224, 209, 0.15),
0 28px 90px rgba(0, 0, 0, 0.5);
text-align: center;
opacity: 0;
visibility: hidden;
transition: 320ms ease;
}
#completeBanner.show {
opacity: 1;
visibility: visible;
transform: translate(-50%, -50%) scale(1);
}
#completeBanner small {
color: var(--cyan);
font: 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.22em;
}
#completeBanner h2 {
margin: 10px 0 8px;
font-size: 24px;
letter-spacing: 0.09em;
}
#completeBanner p {
margin: 0;
color: var(--muted);
font-size: 11px;
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
}
.brand h1 {
font-size: 14px;
}
.camera-controls {
top: 16px;
right: 16px;
}
.status-panel {
top: 72px;
left: 16px;
width: 218px;
padding: 12px;
}
.gate-name small,
.hint {
display: none;
}
#sequence li {
grid-template-columns: 19px 19px 1fr auto;
gap: 5px;
min-height: 35px;
padding: 5px;
}
.alignment {
margin-top: 8px;
padding: 8px;
}
#message {
min-height: 0;
font-size: 10px;
}
.control-deck {
bottom: 14px;
width: calc(100vw - 24px);
padding: 10px;
}
.deck-row {
gap: 8px;
}
.control-button {
min-width: 36px;
height: 35px;
padding: 0 8px;
font-size: 9px;
}
.depth-control {
gap: 3px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel shape sorter"></canvas>
<div class="ui">
<header class="brand">
<div class="brand-mark"></div>
<div>
<h1>VOXEL PASS</h1>
<p>PROFILE GATE LAB / 04</p>
</div>
</header>
<section class="status-panel" aria-label="Puzzle status">
<div class="status-heading">
<span>GATE SEQUENCE</span>
<span id="gateCounter">0 / 4</span>
</div>
<ol id="sequence"></ol>
<div class="alignment">
<span id="alignmentDot"></span>
<span id="alignmentText">KEY 0° / GATE 0°</span>
</div>
<div id="message" aria-live="polite" data-tone="info">
Align the orange T-block with the first illuminated aperture.
</div>
</section>
<nav class="camera-controls" aria-label="Camera controls">
<button class="icon-button" id="zoomIn" title="Zoom in" aria-label="Zoom in">+</button>
<button class="icon-button" id="zoomOut" title="Zoom out" aria-label="Zoom out">−</button>
<button class="icon-button" id="cameraReset" title="Reset camera" aria-label="Reset camera">⌂</button>
</nav>
<section class="control-deck" aria-label="Block controls">
<div class="deck-row">
<div class="button-pair">
<button class="control-button" id="rotateLeft" title="Rotate counter-clockwise (Q)">↺ 90°</button>
<button class="control-button" id="rotateRight" title="Rotate clockwise (E)">↻ 90°</button>
</div>
<div class="depth-control">
<label for="depthRange">DEPTH / FORWARD →</label>
<output id="depthValue">+6.4</output>
<input
id="depthRange"
type="range"
min="-6.8"
max="6.8"
step="0.05"
value="6.4"
aria-label="Block depth"
/>
</div>
<div class="button-pair">
<button class="control-button" id="depthBack" title="Pull backward (S)">− Z</button>
<button class="control-button primary" id="depthForward" title="Push forward (W)">+ Z</button>
<button class="control-button" id="reset" title="Reset puzzle (R)">RESET</button>
</div>
</div>
</section>
<div class="hint">
<b>DRAG KEY</b> translate in the gate plane<br />
<b>Q / E</b> rotate · <b>WHEEL</b> push / pull<br />
<b>DRAG VOID</b> orbit camera · <b>+ / −</b> zoom
</div>
<div id="completeBanner">
<small>ROUTE VERIFIED</small>
<h2>SEQUENCE COMPLETE</h2>
<p>Four compatible profiles verified. The path trace is complete.</p>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const sequenceEl = document.querySelector("#sequence");
const gateCounterEl = document.querySelector("#gateCounter");
const alignmentTextEl = document.querySelector("#alignmentText");
const alignmentDotEl = document.querySelector("#alignmentDot");
const messageEl = document.querySelector("#message");
const depthRangeEl = document.querySelector("#depthRange");
const depthValueEl = document.querySelector("#depthValue");
const completeBannerEl = document.querySelector("#completeBanner");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06111d);
scene.fog = new THREE.FogExp2(0x06111d, 0.018);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const cameraState = {
radius: 25,
theta: 0.58,
phi: 1.12
};
function updateCamera() {
const { radius, theta, phi } = cameraState;
const horizontal = radius * Math.sin(phi);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + horizontal * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa5ecff, 0x0b1324, 1.45);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(8, 14, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x7667ff, 32, 25, 2);
rimLight.position.set(-8, 4, -7);
scene.add(rimLight);
const entranceLight = new THREE.PointLight(0x45e0d1, 24, 18, 2);
entranceLight.position.set(0, 1, 8);
scene.add(entranceLight);
const world = new THREE.Group();
scene.add(world);
const BOX = {
width: 10.6,
height: 8.4,
depth: 16.4
};
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(34, 34),
new THREE.MeshStandardMaterial({
color: 0x071722,
roughness: 0.93,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -BOX.height / 2 - 0.02;
floor.receiveShadow = true;
world.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x28536a, 0x123044);
grid.position.y = -BOX.height / 2 + 0.01;
grid.material.transparent = true;
grid.material.opacity = 0.3;
world.add(grid);
const glass = new THREE.Mesh(
new THREE.BoxGeometry(BOX.width, BOX.height, BOX.depth),
new THREE.MeshStandardMaterial({
color: 0x80dfff,
transparent: true,
opacity: 0.045,
roughness: 0.12,
metalness: 0.05,
side: THREE.DoubleSide,
depthWrite: false
})
);
glass.renderOrder = -1;
world.add(glass);
const boxEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(BOX.width, BOX.height, BOX.depth)
),
new THREE.LineBasicMaterial({
color: 0x72c9e8,
transparent: true,
opacity: 0.46
})
);
world.add(boxEdges);
const UNIT = 0.52;
const CUBE = UNIT * 0.92;
const PROFILE = [
[-1, 2], [0, 2], [1, 2],
[0, 1], [0, 0], [0, -1], [0, -2]
];
const gateDefinitions = [
{ z: 4.2, rotation: 0, color: 0x45e0d1, css: "#45e0d1" },
{ z: 1.4, rotation: 1, color: 0x8d7bff, css: "#8d7bff" },
{ z: -1.4, rotation: 2, color: 0xffb84d, css: "#ffb84d" },
{ z: -4.0, rotation: 3, color: 0xff6f91, css: "#ff6f91" }
];
function rotateCell(x, y, rotation) {
switch (((rotation % 4) + 4) % 4) {
case 0: return [x, y];
case 1: return [-y, x];
case 2: return [-x, -y];
default: return [y, -x];
}
}
function cellKey(x, y) {
return `${x},${y}`;
}
function makeGateLabel(gate) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(0, 4.18, 0);
sprite.scale.set(3.35, 0.84, 1);
sprite.renderOrder = 20;
gate.labelCanvas = labelCanvas;
gate.labelTexture = texture;
gate.labelSprite = sprite;
drawGateLabel(gate);
return sprite;
}
function drawGateLabel(gate) {
const ctx = gate.labelCanvas.getContext("2d");
const accent = gate.css;
ctx.clearRect(0, 0, 512, 128);
ctx.fillStyle = "rgba(5, 18, 29, 0.88)";
ctx.fillRect(0, 0, 512, 128);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 8, 128);
ctx.fillRect(20, 22, 472, 2);
ctx.font = "700 34px monospace";
ctx.fillStyle = "#eaf8ff";
ctx.fillText(
`GATE ${String(gate.index + 1).padStart(2, "0")}`,
24,
66
);
ctx.font = "600 23px monospace";
ctx.fillStyle = accent;
ctx.fillText(
gate.passed ? "PROFILE CLEARED" : `PROFILE ${gate.requiredRotation * 90}°`,
24,
101
);
gate.labelTexture.needsUpdate = true;
}
function makeReticle(gate) {
const transformed = PROFILE.map(([x, y]) =>
rotateCell(x, y, gate.requiredRotation)
);
const xs = transformed.map(([x]) => x);
const ys = transformed.map(([, y]) => y);
const minX = Math.min(...xs) * UNIT - 0.12;
const maxX = Math.max(...xs) * UNIT + 0.12;
const minY = Math.min(...ys) * UNIT - 0.12;
const maxY = Math.max(...ys) * UNIT + 0.12;
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(minX, minY, 0),
new THREE.Vector3(maxX, minY, 0),
new THREE.Vector3(maxX, maxY, 0),
new THREE.Vector3(minX, maxY, 0)
]);
const material = new THREE.LineDashedMaterial({
color: gate.color,
transparent: true,
opacity: 0.8,
dashSize: 0.15,
gapSize: 0.09,
depthTest: false,
depthWrite: false
});
const line = new THREE.LineLoop(geometry, material);
line.computeLineDistances();
line.position.z = 0.38;
line.renderOrder = 15;
return line;
}
const gates = [];
gateDefinitions.forEach((definition, index) => {
const gate = {
...definition,
index,
requiredRotation: definition.rotation,
passed: false,
flashUntil: 0,
errorUntil: 0
};
const group = new THREE.Group();
group.position.z = definition.z;
const holeSet = new Set(
PROFILE.map(([x, y]) => {
const [rx, ry] = rotateCell(x, y, gate.requiredRotation);
return cellKey(rx, ry);
})
);
const panelCells = [];
for (let x = -6; x <= 6; x++) {
for (let y = -6; y <= 6; y++) {
if (!holeSet.has(cellKey(x, y))) {
panelCells.push([x, y]);
}
}
}
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.1,
emissive: definition.color,
emissiveIntensity: 0.035
});
const panel = new THREE.InstancedMesh(
new THREE.BoxGeometry(CUBE, CUBE, 0.42),
material,
panelCells.length
);
const dummy = new THREE.Object3D();
const panelColor = new THREE.Color(0x18364a);
const accentColor = new THREE.Color(definition.color);
panelCells.forEach(([x, y], cellIndex) => {
dummy.position.set(x * UNIT, y * UNIT, 0);
dummy.updateMatrix();
panel.setMatrixAt(cellIndex, dummy.matrix);
const isBoundary = [
[x + 1, y],
[x - 1, y],
[x, y + 1],
[x, y - 1]
].some(([nx, ny]) => holeSet.has(cellKey(nx, ny)));
panel.setColorAt(cellIndex, isBoundary ? accentColor : panelColor);
});
panel.instanceMatrix.needsUpdate = true;
if (panel.instanceColor) panel.instanceColor.needsUpdate = true;
panel.castShadow = true;
panel.receiveShadow = true;
gate.material = material;
group.add(panel);
group.add(makeGateLabel(gate));
gate.reticle = makeReticle(gate);
group.add(gate.reticle);
world.add(group);
gates.push(gate);
});
const blockGroup = new THREE.Group();
world.add(blockGroup);
const blockMaterial = new THREE.MeshStandardMaterial({
color: 0xff8248,
emissive: 0x8d2608,
emissiveIntensity: 0.42,
roughness: 0.32,
metalness: 0.08
});
const blockGeometry = new THREE.BoxGeometry(CUBE, CUBE, 0.62);
const blockMesh = new THREE.InstancedMesh(
blockGeometry,
blockMaterial,
PROFILE.length
);
const blockDummy = new THREE.Object3D();
PROFILE.forEach(([x, y], index) => {
blockDummy.position.set(x * UNIT, y * UNIT, 0);
blockDummy.updateMatrix();
blockMesh.setMatrixAt(index, blockDummy.matrix);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(blockGeometry),
new THREE.LineBasicMaterial({
color: 0x6b210e,
transparent: true,
opacity: 0.72
})
);
edges.position.copy(blockDummy.position);
blockGroup.add(edges);
});
blockMesh.instanceMatrix.needsUpdate = true;
blockMesh.castShadow = true;
blockMesh.receiveShadow = true;
blockGroup.add(blockMesh);
const blockLight = new THREE.PointLight(0xff6b2d, 4.2, 4.5, 2);
blockLight.position.set(0, 0, 0.4);
blockGroup.add(blockLight);
const state = {
x: 0,
y: 0,
z: 6.4,
rotation: 0,
nextGate: 0,
complete: false
};
const START_Z = state.z;
const pathPoints = [new THREE.Vector3(0, 0, START_Z)];
let pathMesh = null;
const pathMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.18, 0.18),
new THREE.MeshBasicMaterial({ color: 0xaaffef })
);
pathMarker.visible = false;
world.add(pathMarker);
const dustGeometry = new THREE.BoxGeometry(1, 1, 1);
const dustMaterial = new THREE.MeshStandardMaterial({
color: 0x477b92,
transparent: true,
opacity: 0.23,
roughness: 0.8
});
const dust = new THREE.InstancedMesh(dustGeometry, dustMaterial, 54);
const dustDummy = new THREE.Object3D();
for (let i = 0; i < 54; i++) {
const size = 0.08 + Math.random() * 0.22;
dustDummy.position.set(
(Math.random() - 0.5) * 33,
-2 + Math.random() * 15,
(Math.random() - 0.5) * 31
);
dustDummy.scale.set(size, size, size);
dustDummy.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
dustDummy.updateMatrix();
dust.setMatrixAt(i, dustDummy.matrix);
}
dust.instanceMatrix.needsUpdate = true;
world.add(dust);
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x45e0d1,
transparent: true,
opacity: 0.45
});
const tickGeometry = new THREE.BoxGeometry(0.12, 0.05, 0.12);
const ticks = new THREE.InstancedMesh(tickGeometry, tickMaterial, 10);
const tickDummy = new THREE.Object3D();
for (let i = 0; i < 10; i++) {
tickDummy.position.set(-4.85, -3.96, 6.7 - i * 1.48);
tickDummy.updateMatrix();
ticks.setMatrixAt(i, tickDummy.matrix);
}
ticks.instanceMatrix.needsUpdate = true;
world.add(ticks);
const orientationNames = ["0°", "90°", "180°", "270°"];
sequenceEl.innerHTML = gates
.map(
(gate, index) => `
<li data-gate="${index}">
<span class="step-number">${String(index + 1).padStart(2, "0")}</span>
<span class="mini-t" style="--rotation:${gate.requiredRotation * 90}deg"></span>
<span class="gate-name">
<b>${orientationNames[gate.requiredRotation]} PROFILE</b>
<small>ROTATE ${gate.requiredRotation * 90}°</small>
</span>
<span class="step-state">LOCK</span>
</li>
`
)
.join("");
const sequenceRows = [...sequenceEl.querySelectorAll("li")];
function showMessage(text, tone = "info") {
messageEl.textContent = text;
messageEl.dataset.tone = tone;
}
function updateBlockTransform() {
blockGroup.position.set(state.x, state.y, state.z);
blockGroup.rotation.z = state.rotation * Math.PI / 2;
blockGroup.updateMatrixWorld(true);
}
function updateHUD() {
gateCounterEl.textContent = `${state.nextGate} / ${gates.length}`;
sequenceRows.forEach((row, index) => {
row.classList.toggle("passed", index < state.nextGate);
row.classList.toggle("current", index === state.nextGate);
const label = row.querySelector(".step-state");
label.textContent =
index < state.nextGate
? "PASS"
: index === state.nextGate
? "NEXT"
: "LOCK";
});
depthRangeEl.value = state.z.toFixed(2);
depthValueEl.textContent = `${state.z >= 0 ? "+" : ""}${state.z.toFixed(1)}`;
if (state.nextGate >= gates.length) {
alignmentTextEl.textContent = "ALL PROFILES CLEAR / EXIT OPEN";
alignmentDotEl.className = "good";
return;
}
const gate = gates[state.nextGate];
const offset = Math.hypot(state.x, state.y);
const rotationMatches = state.rotation === gate.requiredRotation;
const near = offset < 0.65;
alignmentTextEl.textContent =
`KEY ${state.rotation * 90}° / GATE ${gate.requiredRotation * 90}°` +
` • ${rotationMatches && offset < 0.15 ? "FIT" : near ? "NEAR" : "OFFSET"}`;
alignmentDotEl.className =
rotationMatches && offset < 0.15 ? "good" : near ? "warn" : "";
}
function rebuildPath() {
if (pathMesh) {
world.remove(pathMesh);
pathMesh.geometry.dispose();
pathMesh.material.dispose();
pathMesh = null;
}
if (pathPoints.length < 2) {
pathMarker.visible = false;
return;
}
const curve = new THREE.CurvePath();
for (let i = 0; i < pathPoints.length - 1; i++) {
curve.add(
new THREE.LineCurve3(
pathPoints[i],
pathPoints[i + 1]
)
);
}
pathMesh = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(64, pathPoints.length * 24),
0.042,
6,
false
),
new THREE.MeshStandardMaterial({
color: 0x69f4df,
emissive: 0x27cdb9,
emissiveIntensity: 1.1,
roughness: 0.25,
transparent: true,
opacity: 0.95
})
);
world.add(pathMesh);
pathMarker.visible = true;
}
function flashGate(gate, profileError) {
gate.flashUntil = performance.now() + 700;
gate.errorUntil = performance.now() + 700;
if (profileError) {
showMessage(
`Profile blocked — Gate ${String(gate.index + 1).padStart(2, "0")} ` +
`requires ${gate.requiredRotation * 90}°; the key is at ${state.rotation * 90}°.`,
"bad"
);
} else {
showMessage(
`Not aligned — drag the key into Gate ${String(gate.index + 1).padStart(2, "0")}'s ` +
"illuminated dashed reticle.",
"bad"
);
}
}
function recordSuccess(gate) {
gate.passed = true;
state.nextGate += 1;
drawGateLabel(gate);
pathPoints.push(new THREE.Vector3(0, 0, gate.z));
if (state.nextGate === gates.length) {
pathPoints.push(new THREE.Vector3(0, 0, -6.8));
showMessage(
"All four apertures are clear. Continue forward through the exit.",
"good"
);
} else {
const nextGate = gates[state.nextGate];
showMessage(
`Gate ${String(gate.index + 1).padStart(2, "0")} cleared. ` +
`Rotate the key to ${nextGate.requiredRotation * 90}° for the next aperture.`,
"good"
);
}
rebuildPath();
updateHUD();
}
function attemptMoveTo(requestedZ) {
const targetZ = THREE.MathUtils.clamp(requestedZ, -6.8, 6.8);
const startZ = state.z;
const direction = Math.sign(targetZ - startZ);
if (direction === 0) {
updateHUD();
return;
}
let currentZ = startZ;
const stepSize = 0.1;
while (Math.abs(targetZ - currentZ) > 0.0001) {
const remaining = Math.abs(targetZ - currentZ);
const nextZ = currentZ + direction * Math.min(stepSize, remaining);
if (
direction < 0 &&
state.nextGate < gates.length
) {
const gate = gates[state.nextGate];
if (currentZ > gate.z && nextZ <= gate.z) {
const rotationMatches =
state.rotation === gate.requiredRotation;
if (
rotationMatches &&
Math.hypot(state.x, state.y) < 0.36
) {
state.x = 0;
state.y = 0;
}
const aligned =
rotationMatches &&
Math.hypot(state.x, state.y) <= 0.14;
if (aligned) {
recordSuccess(gate);
} else {
state.z = gate.z + 0.58;
updateBlockTransform();
updateHUD();
flashGate(gate, !rotationMatches);
return;
}
}
}
state.z = nextZ;
currentZ = nextZ;
}
state.z = targetZ;
updateBlockTransform();
updateHUD();
if (
!state.complete &&
state.nextGate === gates.length &&
state.z <= -6.55
) {
completePuzzle();
}
}
function rotateProfile(delta) {
state.rotation = (state.rotation + delta + 4) % 4;
updateBlockTransform();
updateHUD();
if (state.nextGate < gates.length) {
const gate = gates[state.nextGate];
if (state.rotation === gate.requiredRotation) {
showMessage(
`Profile orientation matches Gate ${String(gate.index + 1).padStart(2, "0")}. ` +
"Align the key with the reticle and push forward.",
"good"
);
} else {
showMessage(
`Key rotated to ${state.rotation * 90}°. ` +
`The next aperture needs ${gate.requiredRotation * 90}°.`
);
}
}
}
let confettiGroup = null;
function completePuzzle() {
state.complete = true;
completeBannerEl.classList.add("show");
showMessage("Route verified. The full successful path is recorded.", "good");
celebrate();
}
function celebrate() {
if (confettiGroup) {
scene.remove(confettiGroup);
confettiGroup = null;
}
confettiGroup = new THREE.Group();
confettiGroup.position.copy(blockGroup.position);
const colors = [0x45e0d1, 0x8d7bff, 0xffb84d, 0xff6f91, 0xff8248];
const geometry = new THREE.BoxGeometry(0.12, 0.12, 0.12);
for (let i = 0; i < 36; i++) {
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color: colors[i % colors.length]
})
);
mesh.position.set(
(Math.random() - 0.5) * 0.7,
(Math.random() - 0.5) * 0.7,
(Math.random() - 0.5) * 0.7
);
mesh.userData.velocity = new THREE.Vector3(
(Math.random() - 0.5) * 3.4,
1.4 + Math.random() * 2.8,
(Math.random() - 0.5) * 3.4
);
confettiGroup.add(mesh);
}
confettiGroup.userData.age = 0;
scene.add(confettiGroup);
}
function updateConfetti(dt) {
if (!confettiGroup) return;
confettiGroup.userData.age += dt;
confettiGroup.children.forEach((mesh) => {
mesh.position.addScaledVector(mesh.userData.velocity, dt);
mesh.userData.velocity.y -= 4.2 * dt;
mesh.rotation.x += dt * 5;
mesh.rotation.y += dt * 7;
});
if (confettiGroup.userData.age > 2.8) {
scene.remove(confettiGroup);
confettiGroup.children.forEach((mesh) => mesh.material.dispose());
confettiGroup = null;
}
}
function resetPuzzle() {
state.x = 0;
state.y = 0;
state.z = START_Z;
state.rotation = 0;
state.nextGate = 0;
state.complete = false;
gates.forEach((gate) => {
gate.passed = false;
gate.flashUntil = 0;
gate.errorUntil = 0;
drawGateLabel(gate);
});
pathPoints.length = 0;
pathPoints.push(new THREE.Vector3(0, 0, START_Z));
rebuildPath();
completeBannerEl.classList.remove("show");
if (confettiGroup) {
scene.remove(confettiGroup);
confettiGroup = null;
}
updateBlockTransform();
updateHUD();
showMessage(
"Route reset. Gate 01 needs the upright 0° T-profile."
);
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -state.z);
const dragPoint = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
let pointerMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
event.preventDefault();
canvas.setPointerCapture(event.pointerId);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
setPointerFromEvent(event);
if (event.button === 0) {
const hits = raycaster.intersectObject(blockMesh, false);
if (hits.length) {
pointerMode = "block";
dragPlane.constant = -state.z;
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset.set(
state.x - dragPoint.x,
state.y - dragPoint.y,
0
);
}
canvas.style.cursor = "grabbing";
return;
}
}
pointerMode = "orbit";
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerMode === "block") {
setPointerFromEvent(event);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
state.x = THREE.MathUtils.clamp(
dragPoint.x + dragOffset.x,
-4.15,
4.15
);
state.y = THREE.MathUtils.clamp(
dragPoint.y + dragOffset.y,
-3.25,
3.25
);
updateBlockTransform();
updateHUD();
}
return;
}
if (pointerMode === "orbit") {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.38,
1.48
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
return;
}
setPointerFromEvent(event);
const hoveringBlock = raycaster.intersectObject(blockMesh, false).length > 0;
canvas.style.cursor = hoveringBlock ? "grab" : "default";
});
function endPointer(event) {
pointerMode = null;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const amount = THREE.MathUtils.clamp(
Math.abs(event.deltaY) * 0.002,
0.18,
0.55
);
attemptMoveTo(
state.z + (event.deltaY > 0 ? amount : -amount)
);
},
{ passive: false }
);
document.querySelector("#rotateLeft").addEventListener("click", () => {
rotateProfile(-1);
});
document.querySelector("#rotateRight").addEventListener("click", () => {
rotateProfile(1);
});
document.querySelector("#depthForward").addEventListener("click", () => {
attemptMoveTo(state.z - 0.55);
});
document.querySelector("#depthBack").addEventListener("click", () => {
attemptMoveTo(state.z + 0.55);
});
document.querySelector("#reset").addEventListener("click", resetPuzzle);
depthRangeEl.addEventListener("input", (event) => {
attemptMoveTo(Number(event.target.value));
});
function zoomCamera(amount) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + amount,
15,
35
);
updateCamera();
}
document.querySelector("#zoomIn").addEventListener("click", () => {
zoomCamera(-1.5);
});
document.querySelector("#zoomOut").addEventListener("click", () => {
zoomCamera(1.5);
});
document.querySelector("#cameraReset").addEventListener("click", () => {
cameraState.radius = 25;
cameraState.theta = 0.58;
cameraState.phi = 1.12;
cameraTarget.set(0, 0, 0);
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
const key = event.key.toLowerCase();
const step = event.shiftKey ? 0.3 : 0.13;
if (key === "q") {
rotateProfile(-1);
} else if (key === "e") {
rotateProfile(1);
} else if (key === "w") {
attemptMoveTo(state.z - 0.38);
} else if (key === "s") {
attemptMoveTo(state.z + 0.38);
} else if (key === "r") {
resetPuzzle();
} else if (key === "arrowleft") {
state.x = Math.max(-4.15, state.x - step);
updateBlockTransform();
updateHUD();
} else if (key === "arrowright") {
state.x = Math.min(4.15, state.x + step);
updateBlockTransform();
updateHUD();
} else if (key === "arrowup") {
state.y = Math.min(3.25, state.y + step);
updateBlockTransform();
updateHUD();
} else if (key === "arrowdown") {
state.y = Math.max(-3.25, state.y - step);
updateBlockTransform();
updateHUD();
} else if (key === "+" || key === "=") {
zoomCamera(-1);
} else if (key === "-") {
zoomCamera(1);
} else {
return;
}
event.preventDefault();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function updatePathMarker(elapsed) {
if (!pathMarker.visible || pathPoints.length < 2) return;
const progress = (elapsed * 0.13) % 1;
let totalLength = 0;
const lengths = [];
for (let i = 0; i < pathPoints.length - 1; i++) {
const length = pathPoints[i].distanceTo(pathPoints[i + 1]);
lengths.push(length);
totalLength += length;
}
let remaining = progress * totalLength;
for (let i = 0; i < lengths.length; i++) {
if (remaining <= lengths[i]) {
const t = lengths[i] === 0 ? 0 : remaining / lengths[i];
pathMarker.position.lerpVectors(
pathPoints[i],
pathPoints[i + 1],
t
);
break;
}
remaining -= lengths[i];
}
pathMarker.rotation.x = elapsed * 2.3;
pathMarker.rotation.y = elapsed * 3.1;
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const now = performance.now();
const pulse = 1 + Math.sin(elapsed * 3.1) * 0.016;
blockGroup.scale.setScalar(pulse);
blockMaterial.emissiveIntensity = 0.4 + Math.sin(elapsed * 3.1) * 0.08;
gates.forEach((gate) => {
if (now < gate.flashUntil) {
gate.material.emissiveIntensity = 0.85;
gate.reticle.material.color.set(0xff5d73);
gate.reticle.material.opacity = 1;
} else {
gate.material.emissiveIntensity = gate.passed ? 0.24 : 0.035;
gate.reticle.material.color.set(gate.color);
gate.reticle.material.opacity = gate.passed ? 0.24 : 0.8;
}
});
dust.rotation.y = elapsed * 0.008;
dust.rotation.x = Math.sin(elapsed * 0.08) * 0.015;
updatePathMarker(elapsed);
updateConfetti(dt);
renderer.render(scene, camera);
}
updateBlockTransform();
updateHUD();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```