Prompt specification
Build or customize the Three.js 3D scene "Marble track junction tile" 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: Marble track junction tile
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/marble-track-junction-tile-x0353
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Model a Y-shaped channel with raised edges and a movable gate, plus a marble sized to fit; show both alternative paths from above.
Working Three.js Starter (marble-track-junction-tile-x0353):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Y-Channel — A Study in Choice</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf7f7;
--muted: #91a9b4;
--panel: rgba(10, 24, 36, 0.78);
--line: rgba(188, 224, 228, 0.16);
--cyan: #58e0e4;
--orange: #ffb45d;
--brass: #f0c66f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07131f;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 28px;
left: 30px;
max-width: 340px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 1px;
background: currentColor;
content: "";
}
h1 {
margin: 0;
font-size: clamp(28px, 4vw, 52px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 0.98;
}
.brand p {
max-width: 310px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.hint {
position: absolute;
top: 30px;
right: 30px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 19, 31, 0.58);
color: #b9cdd3;
font-size: 11px;
letter-spacing: 0.03em;
backdrop-filter: blur(10px);
}
.panel {
position: absolute;
right: 28px;
bottom: 26px;
left: 28px;
display: flex;
align-items: center;
gap: 22px;
width: fit-content;
max-width: calc(100% - 56px);
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.route-buttons {
display: flex;
gap: 8px;
}
.route-button {
display: flex;
align-items: center;
gap: 8px;
height: 40px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
color: #a9bcc4;
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
transition: 160ms ease;
}
.route-button:hover {
border-color: rgba(255, 255, 255, 0.28);
color: white;
transform: translateY(-1px);
}
.route-button.active[data-route="-1"] {
border-color: rgba(88, 224, 228, 0.6);
background: rgba(88, 224, 228, 0.12);
color: var(--cyan);
box-shadow: inset 0 0 20px rgba(88, 224, 228, 0.05);
}
.route-button.active[data-route="1"] {
border-color: rgba(255, 180, 93, 0.6);
background: rgba(255, 180, 93, 0.12);
color: var(--orange);
box-shadow: inset 0 0 20px rgba(255, 180, 93, 0.05);
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.gate-control {
display: grid;
grid-template-columns: auto minmax(110px, 170px) 48px;
align-items: center;
gap: 12px;
}
.control-label {
color: #8fa6af;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#gate {
width: 100%;
accent-color: var(--brass);
cursor: ew-resize;
}
#gate-value {
color: var(--brass);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-align: right;
}
.actions {
display: flex;
gap: 8px;
margin-left: auto;
}
.action {
height: 40px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 11px;
background: transparent;
color: #b9cbd1;
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
transition: 160ms ease;
}
.action:hover {
border-color: rgba(255, 255, 255, 0.26);
color: white;
}
.action.primary {
border-color: #f0c66f;
background: #f0c66f;
color: #14202a;
box-shadow: 0 7px 24px rgba(240, 198, 111, 0.18);
}
.action.primary:hover {
background: #ffda8e;
transform: translateY(-1px);
}
.action:disabled {
opacity: 0.45;
cursor: default;
transform: none;
}
.status {
position: absolute;
left: 30px;
bottom: 31px;
color: #8099a4;
font-size: 10px;
letter-spacing: 0.06em;
line-height: 1.65;
text-transform: uppercase;
}
.status strong {
color: #bcd0d6;
font-weight: 650;
}
.pin {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(6, 18, 29, 0.72);
color: #c9dadd;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
transition: opacity 150ms ease;
}
.pin i {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 9px currentColor;
}
#pin-a {
color: var(--cyan);
}
#pin-b {
color: var(--orange);
}
@media (max-width: 760px) {
.brand {
top: 20px;
left: 20px;
}
.brand p,
.hint,
.status {
display: none;
}
.panel {
right: 12px;
bottom: 12px;
left: 12px;
flex-wrap: wrap;
gap: 10px;
max-width: none;
padding: 12px;
}
.route-buttons {
flex: 1;
}
.route-button {
flex: 1;
justify-content: center;
}
.gate-control {
order: 3;
width: 100%;
grid-template-columns: auto 1fr 45px;
}
.actions {
width: 100%;
margin-left: 0;
}
.action {
flex: 1;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive overhead view of a Y-shaped marble channel"></div>
<div class="overlay">
<header class="brand">
<div class="eyebrow">Kinetic study 01</div>
<h1>Y-Channel</h1>
<p>
A movable gate, one marble, and two possible futures.
Choose a branch, move the brass gate, and release the marble.
</p>
</header>
<div class="hint">Drag scene to pan · wheel to zoom · drag the brass gate</div>
<div class="pin" id="pin-a"><i></i><span>PATH A</span></div>
<div class="pin" id="pin-b"><i></i><span>PATH B</span></div>
<div class="status">
<strong id="route-status">Path B selected</strong><br />
Marble Ø 0.72 · Clear channel 1.48
</div>
<section class="panel" aria-label="Scene controls">
<div class="route-buttons">
<button class="route-button" data-route="-1" type="button">
<span class="dot"></span> Path A
</button>
<button class="route-button active" data-route="1" type="button">
<span class="dot"></span> Path B
</button>
</div>
<label class="gate-control">
<span class="control-label">Gate</span>
<input id="gate" type="range" min="-1" max="1" step="1" value="1" />
<span id="gate-value">RIGHT</span>
</label>
<div class="actions">
<button class="action" id="reset" type="button">Reset</button>
<button class="action primary" id="release" type="button">Release marble</button>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const gateSlider = document.querySelector("#gate");
const gateValue = document.querySelector("#gate-value");
const routeStatus = document.querySelector("#route-status");
const releaseButton = document.querySelector("#release");
const resetButton = document.querySelector("#reset");
const routeButtons = [...document.querySelectorAll(".route-button")];
const pinA = document.querySelector("#pin-a");
const pinB = document.querySelector("#pin-b");
// Renderer
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Top-down interactive 3D Y-channel with a movable gate and marble"
);
sceneHost.appendChild(renderer.domElement);
// Scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131f);
scene.fog = new THREE.Fog(0x07131f, 18, 36);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 80);
camera.position.set(0, 18, 7);
camera.up.set(0, 0, -1);
camera.lookAt(0, 0, 0);
const cameraTarget = new THREE.Vector3(0, 0, 0.25);
const initialCameraPosition = camera.position.clone();
const initialCameraTarget = cameraTarget.clone();
function updateCamera() {
camera.position.set(
cameraTarget.x,
18,
cameraTarget.z + 7
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfHeight = Math.max(4.8, 5.8 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
// Lighting
scene.add(new THREE.HemisphereLight(0xaeddeb, 0x07101a, 1.65));
const keyLight = new THREE.DirectionalLight(0xe8f7ff, 3.4);
keyLight.position.set(-5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4da8d8, 1.25);
rimLight.position.set(7, 6, -6);
scene.add(rimLight);
// Ground and technical grid
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x0a1b29,
roughness: 0.93,
metalness: 0.03
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.055;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(32, 32, 0x315466, 0x183544);
grid.position.y = -0.035;
grid.material.transparent = true;
grid.material.opacity = 0.18;
grid.material.depthWrite = false;
scene.add(grid);
// Shared materials
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x183747,
roughness: 0.72,
metalness: 0.12
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0xc9dde0,
roughness: 0.32,
metalness: 0.34,
emissive: 0x0b2028,
emissiveIntensity: 0.35
});
const leftFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x163e4a,
roughness: 0.62,
metalness: 0.16,
emissive: 0x58e0e4,
emissiveIntensity: 0.025
});
const rightFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x41362d,
roughness: 0.62,
metalness: 0.16,
emissive: 0xffb45d,
emissiveIntensity: 0.025
});
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xf0c66f,
roughness: 0.25,
metalness: 0.72,
emissive: 0x4b310d,
emissiveIntensity: 0.22
});
const routeMeshes = [];
// Helpers
const up = new THREE.Vector3(0, 1, 0);
function floorSegment(a, b, width, material) {
const dx = b.x - a.x;
const dz = b.y - a.y;
const length = Math.hypot(dx, dz);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.22, length),
material
);
mesh.position.set((a.x + b.x) / 2, 0.11, (a.y + b.y) / 2);
mesh.rotation.y = Math.atan2(dx, dz);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function railBetween(a, b, radius = 0.12, material = railMaterial) {
const start = new THREE.Vector3(a.x, 0.43, a.y);
const end = new THREE.Vector3(b.x, 0.43, b.y);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 18),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function branchRails(a, b, width) {
const direction = new THREE.Vector2(
b.x - a.x,
b.y - a.y
).normalize();
const normal = new THREE.Vector2(-direction.y, direction.x);
const offset = width / 2 + 0.08;
const p1 = new THREE.Vector2(
a.x + normal.x * offset,
a.y + normal.y * offset
);
const p2 = new THREE.Vector2(
b.x + normal.x * offset,
b.y + normal.y * offset
);
const p3 = new THREE.Vector2(
a.x - normal.x * offset,
a.y - normal.y * offset
);
const p4 = new THREE.Vector2(
b.x - normal.x * offset,
b.y - normal.y * offset
);
railBetween(p1, p2);
railBetween(p3, p4);
}
function makeDots(start, end, color, count) {
const materials = [];
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.26,
depthWrite: false
});
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 12, 8),
material
);
dot.position.set(
THREE.MathUtils.lerp(start.x, end.x, t),
0.29,
THREE.MathUtils.lerp(start.y, end.y, t)
);
scene.add(dot);
materials.push(material);
}
return materials;
}
function makeChevron(position, angle, color) {
const shape = new THREE.Shape();
shape.moveTo(-0.16, -0.15);
shape.lineTo(0, 0.17);
shape.lineTo(0.16, -0.15);
shape.lineTo(0, -0.035);
shape.closePath();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.26,
side: THREE.DoubleSide,
depthWrite: false
});
const arrow = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
material
);
arrow.rotation.x = -Math.PI / 2;
arrow.rotation.z = -angle;
arrow.position.set(position.x, 0.3, position.y);
scene.add(arrow);
return material;
}
// Y-channel geometry
const channelWidth = 1.6;
const trunkStart = new THREE.Vector2(0, 4.02);
const trunkEnd = new THREE.Vector2(0, 0.58);
const leftStart = new THREE.Vector2(-0.38, -0.3);
const leftEnd = new THREE.Vector2(-4.05, -3.25);
const rightStart = new THREE.Vector2(0.38, -0.3);
const rightEnd = new THREE.Vector2(4.05, -3.25);
floorSegment(trunkStart, trunkEnd, channelWidth, floorMaterial);
const leftFloor = floorSegment(
leftStart,
leftEnd,
channelWidth,
leftFloorMaterial
);
leftFloor.userData.route = -1;
routeMeshes.push(leftFloor);
const rightFloor = floorSegment(
rightStart,
rightEnd,
channelWidth,
rightFloorMaterial
);
rightFloor.userData.route = 1;
routeMeshes.push(rightFloor);
const junction = new THREE.Mesh(
new THREE.CylinderGeometry(1.12, 1.12, 0.24, 64),
floorMaterial
);
junction.position.y = 0.12;
junction.receiveShadow = true;
scene.add(junction);
// Raised tubular edges
railBetween(
new THREE.Vector2(-0.88, 4.02),
new THREE.Vector2(-0.88, 0.62)
);
railBetween(
new THREE.Vector2(0.88, 4.02),
new THREE.Vector2(0.88, 0.62)
);
railBetween(
new THREE.Vector2(-0.88, 4.02),
new THREE.Vector2(0.88, 4.02)
);
branchRails(leftStart, leftEnd, channelWidth);
branchRails(rightStart, rightEnd, channelWidth);
// Route graphics
const leftDots = makeDots(
new THREE.Vector2(-0.08, -0.08),
leftEnd,
0x58e0e4,
10
);
const rightDots = makeDots(
new THREE.Vector2(0.08, -0.08),
rightEnd,
0xffb45d,
10
);
const sharedDots = [];
for (let i = 0; i < 5; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xa9c5cc,
transparent: true,
opacity: 0.32
});
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 12, 8),
material
);
dot.position.set(0, 0.29, 2.9 - i * 0.43);
scene.add(dot);
sharedDots.push(material);
}
const leftArrows = [
makeChevron(new THREE.Vector2(-1.45, -1.15), -0.83, 0x58e0e4),
makeChevron(new THREE.Vector2(-2.85, -2.28), -0.83, 0x58e0e4)
];
const rightArrows = [
makeChevron(new THREE.Vector2(1.45, -1.15), 0.83, 0xffb45d),
makeChevron(new THREE.Vector2(2.85, -2.28), 0.83, 0xffb45d)
];
// Terminal catch rings
function catchRing(position, color) {
const material = new THREE.MeshStandardMaterial({
color: 0x213847,
emissive: color,
emissiveIntensity: 0.25,
roughness: 0.38,
metalness: 0.38
});
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.63, 0.63, 0.19, 48),
material
);
disc.position.set(position.x, 0.22, position.y);
disc.receiveShadow = true;
scene.add(disc);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.57, 0.085, 14, 56),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.set(position.x, 0.39, position.y);
ring.castShadow = true;
scene.add(ring);
const light = new THREE.PointLight(color, 5.5, 4.5, 2);
light.position.set(position.x, 1.65, position.y);
scene.add(light);
return material;
}
const leftRingMaterial = catchRing(leftEnd, 0x58e0e4);
const rightRingMaterial = catchRing(rightEnd, 0xffb45d);
// Movable gate
const gatePivot = new THREE.Group();
gatePivot.position.set(0, 0, 0.02);
scene.add(gatePivot);
const gateArm = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.13, 1.42, 20),
brassMaterial
);
gateArm.rotation.x = Math.PI / 2;
gateArm.position.set(0, 0.55, -0.71);
gateArm.castShadow = true;
gatePivot.add(gateArm);
const gateTip = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 20, 14),
new THREE.MeshStandardMaterial({
color: 0xffd88c,
roughness: 0.22,
metalness: 0.65,
emissive: 0x6c430d,
emissiveIntensity: 0.35
})
);
gateTip.position.set(0, 0.55, -1.43);
gateTip.castShadow = true;
gatePivot.add(gateTip);
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.29, 0.82, 24),
brassMaterial
);
hinge.position.set(0, 0.43, 0.02);
hinge.castShadow = true;
scene.add(hinge);
const hingeCap = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 24, 16),
brassMaterial
);
hingeCap.position.set(0, 0.88, 0.02);
hingeCap.castShadow = true;
scene.add(hingeCap);
// Invisible hit target for direct gate manipulation
const gateHit = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 0.9, 1.8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
gateHit.position.set(0, 0.45, -0.55);
gatePivot.add(gateHit);
// Marble, sized to fit comfortably inside the channel
const marble = new THREE.Group();
const marbleRadius = 0.36;
const marbleBody = new THREE.Mesh(
new THREE.SphereGeometry(marbleRadius, 48, 32),
new THREE.MeshPhysicalMaterial({
color: 0xf3f7f4,
roughness: 0.12,
metalness: 0.18,
clearcoat: 1,
clearcoatRoughness: 0.08
})
);
marbleBody.castShadow = true;
marble.add(marbleBody);
const marbleBand = new THREE.Mesh(
new THREE.TorusGeometry(0.295, 0.035, 12, 56),
new THREE.MeshStandardMaterial({
color: 0xf0c66f,
roughness: 0.2,
metalness: 0.65
})
);
marbleBand.rotation.x = 0.25;
marbleBand.castShadow = true;
marble.add(marbleBand);
const marbleStart = new THREE.Vector3(0, 0.59, 3.45);
marble.position.copy(marbleStart);
scene.add(marble);
// State
let activeRoute = 1;
let gateAngle = 0.58;
let targetGateAngle = 0.58;
let marbleState = "ready";
let marbleTime = 0;
let marbleCurve = null;
function buildMarbleCurve(route) {
const points = [
new THREE.Vector3(0, 0.59, 3.45),
new THREE.Vector3(0, 0.59, 1.45),
new THREE.Vector3(0, 0.59, 0.02)
];
if (route < 0) {
points.push(
new THREE.Vector3(-1.15, 0.59, -0.92),
new THREE.Vector3(-2.55, 0.59, -2.02),
new THREE.Vector3(-4.05, 0.59, -3.25)
);
} else {
points.push(
new THREE.Vector3(1.15, 0.59, -0.92),
new THREE.Vector3(2.55, 0.59, -2.02),
new THREE.Vector3(4.05, 0.59, -3.25)
);
}
const curve = new THREE.CurvePath();
for (let i = 0; i < points.length - 1; i++) {
curve.add(new THREE.LineCurve3(points[i], points[i + 1]));
}
curve.arcLengthDivisions = 250;
curve.updateArcLengths();
return curve;
}
marbleCurve = buildMarbleCurve(activeRoute);
function updateRouteAppearance() {
routeButtons.forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.route) === activeRoute
);
});
gateValue.textContent = activeRoute < 0 ? "LEFT" : "RIGHT";
routeStatus.textContent =
activeRoute < 0
? "Path A selected"
: "Path B selected";
gateSlider.value = String(activeRoute);
targetGateAngle = activeRoute * 0.58;
}
function resetMarble() {
marbleState = "ready";
marbleTime = 0;
marble.position.copy(marbleStart);
marble.quaternion.identity();
marbleCurve = buildMarbleCurve(activeRoute);
releaseButton.disabled = false;
releaseButton.textContent = "Release marble";
}
function setRoute(route) {
activeRoute = route;
updateRouteAppearance();
resetMarble();
}
routeButtons.forEach((button) => {
button.addEventListener("click", () => {
setRoute(Number(button.dataset.route));
});
});
gateSlider.addEventListener("input", () => {
setRoute(Number(gateSlider.value));
});
releaseButton.addEventListener("click", () => {
marbleState = "running";
marbleTime = 0;
marbleCurve = buildMarbleCurve(activeRoute);
releaseButton.disabled = true;
releaseButton.textContent = "Rolling…";
routeStatus.textContent =
activeRoute < 0 ? "Marble taking Path A" : "Marble taking Path B";
});
resetButton.addEventListener("click", resetMarble);
updateRouteAppearance();
// Direct camera controls: drag to pan, wheel to zoom, double-click to reset.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.55);
const dragPoint = new THREE.Vector3();
let pointerMode = null;
let lastX = 0;
let lastY = 0;
let dragDistance = 0;
function setPointer(event) {
const rect = renderer.domElement.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);
}
function moveGateFromPointer() {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
setRoute(dragPoint.x < 0 ? -1 : 1);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
lastX = event.clientX;
lastY = event.clientY;
dragDistance = 0;
setPointer(event);
const gateHits = raycaster.intersectObject(gateHit, false);
if (gateHits.length) {
pointerMode = "gate";
moveGateFromPointer();
} else {
pointerMode = "pan";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerMode) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
dragDistance += Math.abs(dx) + Math.abs(dy);
lastX = event.clientX;
lastY = event.clientY;
if (pointerMode === "gate") {
moveGateFromPointer();
} else {
const worldPerPixel =
(camera.right - camera.left) /
(window.innerWidth * camera.zoom);
cameraTarget.x = THREE.MathUtils.clamp(
cameraTarget.x - dx * worldPerPixel,
-4,
4
);
cameraTarget.z = THREE.MathUtils.clamp(
cameraTarget.z + dy * worldPerPixel,
-3,
4
);
updateCamera();
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerMode === "pan" && dragDistance < 6) {
setPointer(event);
const routeHits = raycaster.intersectObjects(routeMeshes, false);
if (routeHits.length) {
setRoute(routeHits[0].object.userData.route);
}
}
pointerMode = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerMode = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.2
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTarget.copy(initialCameraTarget);
camera.position.copy(initialCameraPosition);
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
});
// Project DOM labels into the 3D view.
const projected = new THREE.Vector3();
function positionPin(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// Animation
const clock = new THREE.Clock();
const marbleDuration = 4.8;
const marbleRotationAxis = new THREE.Vector3();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Smoothly move the gate.
gateAngle = THREE.MathUtils.damp(
gateAngle,
targetGateAngle,
7.5,
delta
);
gatePivot.rotation.y = gateAngle;
// Route highlighting.
const leftActive = activeRoute < 0 ? 1 : 0;
const rightActive = activeRoute > 0 ? 1 : 0;
leftFloorMaterial.emissiveIntensity = THREE.MathUtils.damp(
leftFloorMaterial.emissiveIntensity,
0.025 + leftActive * 0.18,
7,
delta
);
rightFloorMaterial.emissiveIntensity = THREE.MathUtils.damp(
rightFloorMaterial.emissiveIntensity,
0.025 + rightActive * 0.18,
7,
delta
);
leftDots.forEach((material) => {
material.opacity = THREE.MathUtils.damp(
material.opacity,
leftActive ? 0.95 : 0.2,
7,
delta
);
});
rightDots.forEach((material) => {
material.opacity = THREE.MathUtils.damp(
material.opacity,
rightActive ? 0.95 : 0.2,
7,
delta
);
});
leftArrows.forEach((material) => {
material.opacity = THREE.MathUtils.damp(
material.opacity,
leftActive ? 0.9 : 0.16,
7,
delta
);
});
rightArrows.forEach((material) => {
material.opacity = THREE.MathUtils.damp(
material.opacity,
rightActive ? 0.9 : 0.16,
7,
delta
);
});
leftRingMaterial.emissiveIntensity = THREE.MathUtils.damp(
leftRingMaterial.emissiveIntensity,
0.25 + leftActive * 0.75,
7,
delta
);
rightRingMaterial.emissiveIntensity = THREE.MathUtils.damp(
rightRingMaterial.emissiveIntensity,
0.25 + rightActive * 0.75,
7,
delta
);
// Animate the marble along the selected route.
if (marbleState === "running") {
marbleTime += delta;
const progress = Math.min(marbleTime / marbleDuration, 1);
const eased = progress < 0.5
? 2 * progress * progress
: 1 - Math.pow(-2 * progress + 2, 2) / 2;
const previous = marble.position.clone();
marble.position.copy(marbleCurve.getPointAt(eased));
const tangent = marbleCurve.getTangentAt(eased).normalize();
marbleRotationAxis.crossVectors(up, tangent).normalize();
const travel = previous.distanceTo(marble.position);
marble.rotateOnWorldAxis(
marbleRotationAxis,
travel / marbleRadius
);
if (progress >= 1) {
marbleState = "arrived";
releaseButton.disabled = false;
releaseButton.textContent = "Release again";
routeStatus.textContent =
activeRoute < 0
? "Arrived at Path A"
: "Arrived at Path B";
}
}
// A subtle pulse on the active terminal ring.
const pulse = 1 + Math.sin(elapsed * 2.4) * 0.012;
leftRingMaterial.emissiveIntensity *=
activeRoute < 0 ? pulse : 1;
rightRingMaterial.emissiveIntensity *=
activeRoute > 0 ? pulse : 1;
positionPin(
pinA,
new THREE.Vector3(leftEnd.x, 0.9, leftEnd.y)
);
positionPin(
pinB,
new THREE.Vector3(rightEnd.x, 0.9, rightEnd.y)
);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```