Prompt specification
Build or customize the Three.js 3D scene "Cooperative Ruin Switch Room" 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: Cooperative Ruin Switch Room
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cooperative-ruin-switch-room-x3487
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts an original voxel ruin containing a cooperative switch puzzle. Use a compact chamber with two floor plates, several movable stone crates, and a gate separating the entrance from a small exit courtyard. Expose the dependency between the plates and gate with carved channels or inset indicator stones; choose a limited number of weights and a layout that has at least one valid solution. Allow the player to push crates onto and off the plates, updating the gate from their actual occupied states. Keep the corridors wide enough for the crates and distinguish permanent walls from objects that can move. Put one plate in an alcove and the other beside the gate, with a clear crate-width route connecting the two positions.
Working Three.js Starter (cooperative-ruin-switch-room-x3487):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>The Quiet Relay — Voxel Ruin Puzzle</title>
<style>
:root {
color-scheme: dark;
--ink: #e9efe9;
--muted: #9caeaa;
--panel: rgba(15, 24, 27, 0.86);
--line: rgba(188, 219, 207, 0.17);
--cyan: #74ead2;
--amber: #ffc15d;
--red: #ff7668;
--cool: #a9c2cb;
--warm: #d79a58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #10191c;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 14px;
box-shadow:
0 14px 45px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(12px);
}
#title-card {
position: absolute;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 17px 18px 15px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 28px;
font-weight: 500;
letter-spacing: 0.015em;
}
.subtitle {
margin: 5px 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.rule {
height: 1px;
margin: 13px 0;
background: var(--line);
}
.readout {
display: grid;
gap: 8px;
}
.readout-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
font-size: 12px;
}
.readout-label {
display: flex;
align-items: center;
gap: 8px;
color: #c9d5d1;
}
.readout-value {
min-width: 58px;
text-align: right;
font-variant-numeric: tabular-nums;
font-weight: 800;
color: var(--amber);
}
.readout-value.good {
color: var(--cyan);
}
.readout-value.over {
color: var(--red);
}
.dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--amber);
box-shadow: 0 0 9px currentColor;
}
.dot.cyan {
background: var(--cyan);
}
.dot.red {
background: var(--red);
}
#gate-status {
margin-top: 13px;
padding: 9px 11px;
border: 1px solid rgba(255, 193, 93, 0.2);
border-radius: 8px;
background: rgba(255, 193, 93, 0.07);
color: var(--amber);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.1em;
text-align: center;
text-transform: uppercase;
}
#gate-status.open {
border-color: rgba(116, 234, 210, 0.35);
background: rgba(116, 234, 210, 0.1);
color: var(--cyan);
}
#cargo-card {
position: absolute;
left: 18px;
bottom: 18px;
width: min(330px, calc(100vw - 36px));
padding: 14px 16px;
}
.small-heading {
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.cargo {
display: grid;
gap: 6px;
margin-bottom: 10px;
font-size: 11px;
color: #c8d3cf;
}
.cargo-item {
display: flex;
align-items: center;
gap: 9px;
}
.cargo-swatch {
width: 14px;
height: 14px;
border: 2px solid #6f5437;
background: #c99b63;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.08);
}
.cargo-swatch.heavy {
background: #aa7444;
}
.legend {
display: flex;
gap: 12px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
display: inline-block;
width: 10px;
height: 10px;
background: var(--cool);
}
.legend i.movable {
background: var(--warm);
}
#controls-card {
position: absolute;
right: 18px;
bottom: 18px;
max-width: 360px;
padding: 12px 14px;
font-size: 11px;
line-height: 1.55;
color: var(--muted);
}
#controls-card strong {
color: var(--ink);
font-weight: 700;
}
.button-row {
display: flex;
gap: 8px;
margin-top: 9px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(190, 222, 211, 0.22);
border-radius: 8px;
padding: 8px 11px;
background: rgba(225, 241, 236, 0.07);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 150ms, border-color 150ms, transform 100ms;
}
button:hover {
border-color: rgba(116, 234, 210, 0.55);
background: rgba(116, 234, 210, 0.12);
}
button:active {
transform: translateY(1px);
}
#toast {
position: absolute;
top: 22px;
left: 50%;
max-width: min(520px, calc(100vw - 36px));
padding: 11px 16px;
border: 1px solid rgba(116, 234, 210, 0.25);
border-radius: 999px;
background: rgba(11, 20, 22, 0.88);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.3);
color: #dce9e4;
font-size: 12px;
text-align: center;
opacity: 0;
transform: translate(-50%, -12px);
transition: opacity 200ms, transform 200ms;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#touch-pad {
position: absolute;
right: 18px;
bottom: 112px;
display: none;
grid-template-columns: repeat(3, 42px);
grid-template-rows: repeat(3, 42px);
gap: 4px;
pointer-events: auto;
}
#touch-pad button {
padding: 0;
border-radius: 9px;
background: rgba(16, 28, 31, 0.82);
font-size: 17px;
touch-action: none;
}
#touch-pad .up {
grid-column: 2;
grid-row: 1;
}
#touch-pad .left {
grid-column: 1;
grid-row: 2;
}
#touch-pad .down {
grid-column: 2;
grid-row: 3;
}
#touch-pad .right {
grid-column: 3;
grid-row: 2;
}
@media (max-width: 760px) {
#title-card {
top: 10px;
left: 10px;
width: min(300px, calc(100vw - 20px));
padding: 13px 14px;
}
h1 {
font-size: 23px;
}
.subtitle {
margin-bottom: 10px;
}
#cargo-card {
display: none;
}
#controls-card {
right: 10px;
bottom: 10px;
max-width: calc(100vw - 20px);
}
#touch-pad {
display: grid;
right: 12px;
bottom: 116px;
}
#toast {
top: auto;
bottom: 202px;
}
}
@media (max-height: 650px) {
#cargo-card {
display: none;
}
#title-card .subtitle,
#title-card .rule,
#cargo-card {
display: none;
}
}
</style>
</head>
<body>
<div id="hud">
<section id="title-card" class="panel">
<div class="eyebrow">Voxel ruin mechanism</div>
<h1>The Quiet Relay</h1>
<p class="subtitle">
Two inset plates share one ancient gate. Only stones actually resting on
a plate count toward its load.
</p>
<div class="readout">
<div class="readout-row">
<span class="readout-label">
<i class="dot"></i> Alcove plate
</span>
<span id="alcove-value" class="readout-value">0 / 3</span>
</div>
<div class="readout-row">
<span class="readout-label">
<i class="dot cyan"></i> Gate-side plate
</span>
<span id="threshold-value" class="readout-value">0 / 2</span>
</div>
</div>
<div id="gate-status">Gate sealed</div>
<div class="rule"></div>
<div class="small-heading">Available cargo</div>
<div class="cargo">
<div class="cargo-item">
<i class="cargo-swatch"></i>
<span>One stone · weight 1</span>
</div>
<div class="cargo-item">
<i class="cargo-swatch heavy"></i>
<span>Two stones · weight 2 each</span>
</div>
</div>
<div class="legend">
<span><i></i> Fixed masonry</span>
<span><i class="movable"></i> Movable stone</span>
</div>
</section>
<section id="cargo-card" class="panel">
<div class="small-heading">Relay logic</div>
<div class="cargo">
<div class="cargo-item">
<span>
The alcove requires exactly <strong>3</strong> weight.
The gate-side plate requires exactly <strong>2</strong>.
</span>
</div>
<div class="cargo-item">
<span>
The luminous channels show each plate's condition. The gate listens
to both channels together.
</span>
</div>
</div>
</section>
<section id="controls-card" class="panel">
<div>
<strong>WASD / arrows</strong> walk and push ·
<strong>drag</strong> orbit ·
<strong>wheel</strong> zoom
</div>
<div>
Stand behind a stone and walk into it to push. Stones can be moved
onto or away from the inset plates.
</div>
<div class="button-row">
<button id="reset-button" type="button">Reset puzzle</button>
<button id="camera-button" type="button">Reset camera</button>
</div>
</section>
<div id="touch-pad" aria-label="Movement controls">
<button class="up" data-move="up" type="button" aria-label="Move up">↑</button>
<button class="left" data-move="left" type="button" aria-label="Move left">←</button>
<button class="right" data-move="right" type="button" aria-label="Move right">→</button>
<button class="down" data-move="down" type="button" aria-label="Move down">↓</button>
</div>
<div id="toast" role="status" aria-live="polite"></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Configuration and grid
// -------------------------------------------------------------------------
const CELL = 1.12;
const MAX_X = 13;
const MAX_Z = 10;
const GATE_X = 10;
const GATE_CELLS = [
{ x: 10, z: 3 },
{ x: 10, z: 4 }
];
const worldX = x => (x - 6.5) * CELL;
const worldZ = z => (z - 5) * CELL;
const keyOf = (x, z) => `${x},${z}`;
const sameCell = (a, b) => a.x === b.x && a.z === b.z;
const insideGrid = (x, z) => x >= 0 && x <= MAX_X && z >= 0 && z <= MAX_Z;
const plates = [
{
id: "alcove",
name: "Alcove plate",
target: 3,
cells: [
{ x: 1, z: 2 },
{ x: 1, z: 3 },
{ x: 2, z: 2 },
{ x: 2, z: 3 }
],
pipMaterials: [],
panelMaterial: null,
channelMaterial: null,
activeColor: 0xffc15d,
activeEmissive: 0xff8b28
},
{
id: "threshold",
name: "Gate-side plate",
target: 2,
cells: [
{ x: 8, z: 3 },
{ x: 8, z: 4 },
{ x: 9, z: 3 },
{ x: 9, z: 4 }
],
pipMaterials: [],
panelMaterial: null,
channelMaterial: null,
activeColor: 0x74ead2,
activeEmissive: 0x28bda5
}
];
const crateStarts = [
{ weight: 1, x: 5, z: 2 },
{ weight: 2, x: 6, z: 6 },
{ weight: 2, x: 8, z: 7 }
];
const crates = [];
const staticBlocked = new Set();
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111b20);
scene.fog = new THREE.Fog(0x111b20, 24, 43);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xb8d9e4, 0x30251f, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe2b5, 3.1);
sun.position.set(-10, 17, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x70c9d1, 1.25);
rim.position.set(12, 7, -13);
scene.add(rim);
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
const cameraState = {
yaw: 0.58,
pitch: 0.78,
radius: innerWidth < 700 ? 23 : 19.8
};
function updateCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraState.yaw) * horizontal,
cameraTarget.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraTarget.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.yaw = 0.58;
cameraState.pitch = 0.78;
cameraState.radius = innerWidth < 700 ? 23 : 19.8;
}
updateCamera();
// Direct camera controls; no Three.js addons are used.
let dragging = false;
let dragX = 0;
let dragY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.38,
1.25
);
});
function stopDragging() {
dragging = false;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
29
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const wallMaterials = [
new THREE.MeshStandardMaterial({
color: 0x64767a,
roughness: 0.94,
metalness: 0.02
}),
new THREE.MeshStandardMaterial({
color: 0x718184,
roughness: 0.96,
metalness: 0.02
}),
new THREE.MeshStandardMaterial({
color: 0x596a70,
roughness: 0.98,
metalness: 0.01
})
];
const floorMaterials = [
new THREE.MeshStandardMaterial({
color: 0x4f5551,
roughness: 0.98
}),
new THREE.MeshStandardMaterial({
color: 0x565b56,
roughness: 0.98
}),
new THREE.MeshStandardMaterial({
color: 0x474f4d,
roughness: 1
}),
new THREE.MeshStandardMaterial({
color: 0x59615a,
roughness: 0.98
})
];
const routeFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x536867,
roughness: 0.94
});
const courtyardFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x4c5b55,
roughness: 0.98
});
const stoneTrimMaterial = new THREE.MeshStandardMaterial({
color: 0x7f8c89,
roughness: 0.9
});
const mossMaterial = new THREE.MeshStandardMaterial({
color: 0x526d4b,
roughness: 1
});
const ironMaterial = new THREE.MeshStandardMaterial({
color: 0x333b3d,
roughness: 0.53,
metalness: 0.72
});
const crateBandsMaterial = new THREE.MeshStandardMaterial({
color: 0x5b4431,
roughness: 0.9
});
const crateMaterials = {
1: new THREE.MeshStandardMaterial({
color: 0xc59b64,
roughness: 0.88
}),
2: new THREE.MeshStandardMaterial({
color: 0xaa7445,
roughness: 0.9
})
};
function addBox(parent, size, position, material, castShadow = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function hash(x, z) {
return Math.abs((x * 73856093) ^ (z * 19349663)) % 10007;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(text, options = {}) {
const {
color = "#eaf2ed",
background = "rgba(12,22,25,0.84)",
border = "rgba(174,210,200,0.28)",
width = 512,
height = 128,
fontSize = 38
} = options;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height);
roundedRect(ctx, 5, 5, width - 10, height - 10, 20);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = border;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = `700 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width / 2, height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
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.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Floor and permanent masonry
// -------------------------------------------------------------------------
const floorBase = addBox(
scene,
[(MAX_X + 1) * CELL + 0.2, 0.42, (MAX_Z + 1) * CELL + 0.2],
[0, -0.29, 0],
new THREE.MeshStandardMaterial({
color: 0x292f2d,
roughness: 1
})
);
floorBase.castShadow = false;
const outerGround = addBox(
scene,
[90, 0.25, 90],
[0, -0.72, 0],
new THREE.MeshStandardMaterial({
color: 0x182326,
roughness: 1
}),
false
);
outerGround.receiveShadow = true;
for (let x = 0; x <= MAX_X; x++) {
for (let z = 0; z <= MAX_Z; z++) {
const isEntrance = z === MAX_Z && (x === 4 || x === 5);
const isRoute = x >= 3 && x <= 9 && (z === 3 || z === 4);
const isCourtyard = x >= 11;
let material = floorMaterials[hash(x, z) % floorMaterials.length];
if (isRoute) material = routeFloorMaterial;
if (isCourtyard) material = courtyardFloorMaterial;
addBox(
scene,
[CELL * 0.94, 0.14, CELL * 0.94],
[worldX(x), -0.07, worldZ(z)],
material,
false
);
if (!isEntrance && hash(x + 40, z) % 13 === 0) {
addBox(
scene,
[CELL * 0.15, 0.035, CELL * 0.15],
[worldX(x) + CELL * 0.25, 0.015, worldZ(z) - CELL * 0.24],
mossMaterial,
false
);
}
}
}
function markWall(x, z) {
staticBlocked.add(keyOf(x, z));
}
// Outer shell, leaving a two-cell entrance at the front.
for (let x = 0; x <= MAX_X; x++) {
markWall(x, 0);
if (!(x === 4 || x === 5)) markWall(x, MAX_Z);
}
for (let z = 0; z <= MAX_Z; z++) {
markWall(0, z);
markWall(MAX_X, z);
}
// The full divider separates the chamber from the exit courtyard.
for (let z = 0; z <= MAX_Z; z++) {
if (z !== 3 && z !== 4) markWall(GATE_X, z);
}
// Masonry around the recessed alcove.
markWall(3, 1);
markWall(3, 5);
function buildWallCell(x, z) {
const h =
x === GATE_X && (z === 2 || z === 5)
? 2.12
: 1.36 + (hash(x, z) % 4) * 0.13;
const material = wallMaterials[hash(x + 3, z) % wallMaterials.length];
addBox(
scene,
[CELL * 0.98, h, CELL * 0.98],
[worldX(x), h / 2, worldZ(z)],
material
);
if (hash(x + 12, z) % 8 === 0) {
addBox(
scene,
[CELL * 0.22, 0.09, CELL * 0.23],
[
worldX(x) + CELL * 0.16,
h + 0.035,
worldZ(z) - CELL * 0.13
],
mossMaterial
);
}
}
for (const blocked of staticBlocked) {
const [x, z] = blocked.split(",").map(Number);
buildWallCell(x, z);
}
// Overhead lintel makes the alcove visibly recessed.
for (let z = 2; z <= 4; z++) {
addBox(
scene,
[CELL * 0.98, 0.28, CELL * 0.98],
[worldX(3), 1.88, worldZ(z)],
stoneTrimMaterial
);
}
// Gate lintel and central keystone.
for (let z = 3; z <= 4; z++) {
addBox(
scene,
[CELL * 0.98, 0.28, CELL * 0.98],
[worldX(GATE_X), 1.88, worldZ(z)],
stoneTrimMaterial
);
}
addBox(
scene,
[CELL * 0.58, 0.48, CELL * 0.58],
[worldX(GATE_X), 2.12, worldZ(3.5)],
wallMaterials[0]
);
// Entrance steps.
addBox(
scene,
[CELL * 2.05, 0.22, CELL * 0.48],
[worldX(4.5), -0.12, worldZ(10.23)],
stoneTrimMaterial
);
addBox(
scene,
[CELL * 2.05, 0.18, CELL * 0.46],
[worldX(4.5), -0.29, worldZ(10.67)],
wallMaterials[2]
);
// -------------------------------------------------------------------------
// Plates, inset indicator stones, and carved channels
// -------------------------------------------------------------------------
function buildPlate(plate) {
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x3f4d4c,
emissive: 0x000000,
emissiveIntensity: 0,
roughness: 0.64,
metalness: 0.42
});
plate.panelMaterial = panelMaterial;
for (const cell of plate.cells) {
addBox(
scene,
[CELL * 0.92, 0.035, CELL * 0.92],
[worldX(cell.x), 0.012, worldZ(cell.z)],
panelMaterial,
false
);
// Four low corner rivets give each plate a built, inset appearance.
for (const dx of [-0.37, 0.37]) {
for (const dz of [-0.37, 0.37]) {
addBox(
scene,
[0.075, 0.045, 0.075],
[
worldX(cell.x) + dx * CELL,
0.043,
worldZ(cell.z) + dz * CELL
],
ironMaterial,
false
);
}
}
}
const pipGeometry = new THREE.BoxGeometry(
CELL * 0.16,
CELL * 0.12,
CELL * 0.16
);
for (let i = 0; i < plate.target; i++) {
const pipMaterial = new THREE.MeshStandardMaterial({
color: 0x5b4932,
emissive: 0x21170d,
emissiveIntensity: 0.2,
roughness: 0.55,
metalness: 0.25
});
let px;
let pz;
if (plate.id === "alcove") {
px = 0.72 + i * 0.34;
pz = 4.12;
} else {
px = 9.08 + i * 0.34;
pz = 2.65;
}
const pip = new THREE.Mesh(pipGeometry, pipMaterial);
pip.position.set(worldX(px), 0.075, worldZ(pz));
pip.castShadow = true;
scene.add(pip);
plate.pipMaterials.push(pipMaterial);
}
const label = makeLabel(
plate.id === "alcove" ? "ALCOVE • 3" : "GATE SIDE • 2",
{
color: plate.id === "alcove" ? "#ffd38a" : "#9af3df",
border:
plate.id === "alcove"
? "rgba(255,193,93,0.38)"
: "rgba(116,234,210,0.38)"
}
);
label.scale.set(2.4, 0.6, 1);
label.position.set(
worldX(plate.id === "alcove" ? 1.5 : 8.5),
1.18,
worldZ(plate.id === "alcove" ? 2.0 : 4.82)
);
scene.add(label);
}
plates.forEach(buildPlate);
const channelRunes = [];
function buildChannel(path, plate) {
const grooveMaterial = new THREE.MeshStandardMaterial({
color: 0x2f3532,
roughness: 0.95,
metalness: 0.08
});
const runeMaterial = new THREE.MeshStandardMaterial({
color: 0x624b2e,
emissive: 0x281b0d,
emissiveIntensity: 0.18,
roughness: 0.55,
metalness: 0.22
});
plate.channelMaterial = runeMaterial;
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const size =
Math.abs(dx) > Math.abs(dz)
? [length * CELL + 0.035, 0.026, CELL * 0.115]
: [CELL * 0.115, 0.026, length * CELL + 0.035];
addBox(
scene,
size,
[
worldX((a.x + b.x) / 2),
0.014,
worldZ((a.z + b.z) / 2)
],
grooveMaterial,
false
);
const steps = Math.max(1, Math.ceil(length / 0.43));
for (let s = 0; s <= steps; s++) {
const t = s / steps;
const x = a.x + dx * t;
const z = a.z + dz * t;
const rune = addBox(
scene,
[CELL * 0.13, 0.035, CELL * 0.13],
[worldX(x), 0.039, worldZ(z)],
runeMaterial,
false
);
channelRunes.push(rune);
}
}
}
buildChannel(
[
{ x: 1.5, z: 2.5 },
{ x: 2.75, z: 2.5 },
{ x: 2.75, z: 3.5 },
{ x: 7.8, z: 3.5 },
{ x: 8.5, z: 3.5 }
],
plates[0]
);
buildChannel(
[
{ x: 9.5, z: 3.5 },
{ x: 10.35, z: 3.5 }
],
plates[1]
);
// -------------------------------------------------------------------------
// Gate and exit courtyard
// -------------------------------------------------------------------------
const gateGroup = new THREE.Group();
gateGroup.position.set(worldX(GATE_X), 0, worldZ(3.5));
scene.add(gateGroup);
for (let i = -2; i <= 2; i++) {
addBox(
gateGroup,
[CELL * 0.12, 1.75 * CELL, CELL * 0.095],
[0, 0.88 * CELL, i * CELL * 0.43],
ironMaterial
);
}
addBox(
gateGroup,
[CELL * 0.17, CELL * 0.13, CELL * 2.03],
[0, 0.45 * CELL, 0],
ironMaterial
);
addBox(
gateGroup,
[CELL * 0.17, CELL * 0.13, CELL * 2.03],
[0, 1.34 * CELL, 0],
ironMaterial
);
const gateLampMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd58,
emissive: 0xff7c21,
emissiveIntensity: 0.85,
roughness: 0.38
});
const gateLamp = addBox(
scene,
[CELL * 0.25, CELL * 0.25, CELL * 0.25],
[worldX(9.86), 2.08, worldZ(3.5)],
gateLampMaterial
);
const exitMaterial = new THREE.MeshStandardMaterial({
color: 0x4c8f8a,
emissive: 0x267d78,
emissiveIntensity: 1.1,
roughness: 0.6
});
addBox(
scene,
[CELL * 0.68, 0.07, CELL * 0.68],
[worldX(12), 0.04, worldZ(4)],
exitMaterial,
false
);
const exitLabel = makeLabel("EXIT COURT", {
color: "#a9fff0",
background: "rgba(10,31,32,0.82)",
border: "rgba(116,234,210,0.35)",
fontSize: 35
});
exitLabel.scale.set(2.1, 0.53, 1);
exitLabel.position.set(worldX(12), 0.9, worldZ(4));
scene.add(exitLabel);
// Small torches add warmth without obscuring the puzzle.
function makeTorch(x, z) {
const group = new THREE.Group();
group.position.set(worldX(x), 0, worldZ(z));
scene.add(group);
addBox(
group,
[0.14, 0.36, 0.14],
[0, 0.84, 0],
ironMaterial
);
const flameMaterial = new THREE.MeshStandardMaterial({
color: 0xffc265,
emissive: 0xff7b22,
emissiveIntensity: 2.2,
roughness: 0.4
});
addBox(
group,
[0.16, 0.24, 0.16],
[0, 1.1, 0],
flameMaterial,
false
);
const light = new THREE.PointLight(0xff9d4d, 1.6, 5.5, 2);
light.position.set(0, 1.25, 0);
group.add(light);
}
makeTorch(0.58, 3.1);
makeTorch(10.55, 7.1);
// -------------------------------------------------------------------------
// Movable stone crates
// -------------------------------------------------------------------------
function buildCrate(weight, x, z, index) {
const group = new THREE.Group();
scene.add(group);
const size = CELL * 0.84;
addBox(
group,
[size, size, size],
[0, CELL * 0.46, 0],
crateMaterials[weight]
);
addBox(
group,
[CELL * 0.9, CELL * 0.12, CELL * 0.9],
[0, CELL * 0.84, 0],
crateBandsMaterial
);
addBox(
group,
[CELL * 0.9, CELL * 0.11, CELL * 0.9],
[0, CELL * 0.08, 0],
crateBandsMaterial
);
for (const dx of [-1, 1]) {
for (const dz of [-1, 1]) {
addBox(
group,
[CELL * 0.12, CELL * 0.75, CELL * 0.12],
[dx * CELL * 0.34, CELL * 0.45, dz * CELL * 0.34],
crateBandsMaterial
);
}
}
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(size, size, size)),
new THREE.LineBasicMaterial({
color: 0x493626,
transparent: true,
opacity: 0.75
})
);
edge.position.y = CELL * 0.46;
group.add(edge);
const label = makeLabel(String(weight), {
width: 128,
height: 128,
fontSize: 70,
color: "#fff0ca",
background: "rgba(50,37,25,0.9)",
border: "rgba(255,210,132,0.45)"
});
label.scale.set(0.48, 0.48, 1);
label.position.set(0, CELL * 1.22, 0);
group.add(label);
const target = new THREE.Vector3(worldX(x), 0, worldZ(z));
group.position.copy(target);
const crate = {
id: index,
weight,
cell: { x, z },
group,
targetPosition: target
};
crates.push(crate);
return crate;
}
crateStarts.forEach((start, index) => {
buildCrate(start.weight, start.x, start.z, index);
});
// -------------------------------------------------------------------------
// Player avatar
// -------------------------------------------------------------------------
const playerRoot = new THREE.Group();
scene.add(playerRoot);
const playerModel = new THREE.Group();
playerRoot.add(playerModel);
const robeMaterial = new THREE.MeshStandardMaterial({
color: 0x3e8f91,
roughness: 0.86
});
const clothMaterial = new THREE.MeshStandardMaterial({
color: 0x23585d,
roughness: 0.95
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xd4ae82,
roughness: 0.88
});
const visorMaterial = new THREE.MeshStandardMaterial({
color: 0x92fff0,
emissive: 0x32a99d,
emissiveIntensity: 0.8,
roughness: 0.35
});
addBox(
playerModel,
[CELL * 0.54, CELL * 0.66, CELL * 0.43],
[0, CELL * 0.49, 0],
robeMaterial
);
addBox(
playerModel,
[CELL * 0.34, CELL * 0.34, CELL * 0.34],
[0, CELL * 0.96, 0],
skinMaterial
);
addBox(
playerModel,
[CELL * 0.43, CELL * 0.23, CELL * 0.43],
[0, CELL * 1.07, 0],
clothMaterial
);
addBox(
playerModel,
[CELL * 0.24, CELL * 0.1, CELL * 0.06],
[0, CELL * 0.99, CELL * 0.19],
visorMaterial,
false
);
addBox(
playerModel,
[CELL * 0.18, CELL * 0.2, CELL * 0.25],
[-CELL * 0.16, CELL * 0.12, 0],
clothMaterial
);
addBox(
playerModel,
[CELL * 0.18, CELL * 0.2, CELL * 0.25],
[CELL * 0.16, CELL * 0.12, 0],
clothMaterial
);
const playerRing = new THREE.Mesh(
new THREE.RingGeometry(CELL * 0.31, CELL * 0.39, 24),
new THREE.MeshBasicMaterial({
color: 0x6fe6d1,
transparent: true,
opacity: 0.55,
side: THREE.DoubleSide
})
);
playerRing.rotation.x = -Math.PI / 2;
playerRing.position.y = 0.015;
playerRoot.add(playerRing);
const player = {
cell: { x: 4, z: 8 },
targetPosition: new THREE.Vector3(worldX(4), 0, worldZ(8)),
yaw: Math.PI,
targetYaw: Math.PI
};
playerRoot.position.copy(player.targetPosition);
// -------------------------------------------------------------------------
// Puzzle state and movement
// -------------------------------------------------------------------------
let gatePassable = false;
let gateTargetY = 0;
let lastMoveTime = -Infinity;
let toastTimer = null;
const alcoveValue = document.getElementById("alcove-value");
const thresholdValue = document.getElementById("threshold-value");
const gateStatus = document.getElementById("gate-status");
const toast = document.getElementById("toast");
function showToast(message, duration = 2600) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), duration);
}
function crateAt(x, z) {
return crates.find(crate => crate.cell.x === x && crate.cell.z === z);
}
function isGateCell(x, z) {
return GATE_CELLS.some(cell => cell.x === x && cell.z === z);
}
function isBlocked(x, z) {
if (!insideGrid(x, z)) return true;
if (staticBlocked.has(keyOf(x, z))) return true;
if (isGateCell(x, z) && !gatePassable) return true;
return false;
}
function calculateLoads() {
const loads = {};
for (const plate of plates) {
loads[plate.id] = crates.reduce((total, crate) => {
const onPlate = plate.cells.some(cell =>
sameCell(cell, crate.cell)
);
return total + (onPlate ? crate.weight : 0);
}, 0);
}
return loads;
}
function snapshot() {
return {
loads: calculateLoads(),
open: plates.every(plate => calculateLoads()[plate.id] === plate.target)
};
}
function updatePlateVisuals(plate, load) {
const exact = load === plate.target;
const over = load > plate.target;
plate.panelMaterial.color.setHex(exact ? 0x466b66 : 0x3f4d4c);
plate.panelMaterial.emissive.setHex(exact ? plate.activeEmissive : 0x000000);
plate.panelMaterial.emissiveIntensity = exact ? 0.28 : 0;
plate.pipMaterials.forEach((material, index) => {
let color = 0x5b4932;
let emissive = 0x21170d;
let intensity = 0.2;
if (over) {
color = 0xff665b;
emissive = 0x9f2018;
intensity = 1.5;
} else if (index < load) {
color = plate.activeColor;
emissive = plate.activeEmissive;
intensity = 1.55;
} else if (exact) {
color = plate.activeColor;
emissive = plate.activeEmissive;
intensity = 1.55;
}
material.color.setHex(color);
material.emissive.setHex(emissive);
material.emissiveIntensity = intensity;
});
plate.channelMaterial.color.setHex(exact ? plate.activeColor : 0x624b2e);
plate.channelMaterial.emissive.setHex(exact ? plate.activeEmissive : 0x281b0d);
plate.channelMaterial.emissiveIntensity = exact ? 1.55 : 0.18;
}
function updatePuzzleState(previous = null) {
const loads = calculateLoads();
for (const plate of plates) {
const load = loads[plate.id];
updatePlateVisuals(plate, load);
const element =
plate.id === "alcove" ? alcoveValue : thresholdValue;
element.textContent = `${load} / ${plate.target}`;
element.className = "readout-value";
if (load === plate.target) element.classList.add("good");
if (load > plate.target) element.classList.add("over");
}
const allSatisfied = plates.every(
plate => loads[plate.id] === plate.target
);
const anyOvershot = plates.some(
plate => loads[plate.id] > plate.target
);
gatePassable = allSatisfied;
gateTargetY = allSatisfied ? 2.05 : 0;
if (allSatisfied) {
gateLampMaterial.color.setHex(0x74ead2);
gateLampMaterial.emissive.setHex(0x2ce0c3);
gateLampMaterial.emissiveIntensity = 2.4;
gateStatus.textContent = "Gate open — both circuits agree";
gateStatus.classList.add("open");
} else {
gateLampMaterial.color.setHex(anyOvershot ? 0xff665b : 0xffbd58);
gateLampMaterial.emissive.setHex(anyOvershot ? 0xb42118 : 0xff7c21);
gateLampMaterial.emissiveIntensity = anyOvershot ? 1.7 : 0.85;
gateStatus.textContent = anyOvershot
? "Gate sealed — plate overloaded"
: "Gate sealed";
gateStatus.classList.remove("open");
}
if (previous) {
const oldOpen = previous.open;
if (oldOpen !== allSatisfied) {
showToast(
allSatisfied
? "Both plates agree. The ancient gate rises."
: "The relay is unbalanced. The gate seals again."
);
} else {
const changedPlate = plates.find(
plate => previous.loads[plate.id] !== loads[plate.id]
);
if (changedPlate) {
showToast(
`${changedPlate.name}: ${loads[changedPlate.id]} / ${changedPlate.target} weight.`
);
}
}
}
}
function tryMove(dx, dz) {
const now = performance.now();
if (now - lastMoveTime < 115) return;
const previous = snapshot();
const nextX = player.cell.x + dx;
const nextZ = player.cell.z + dz;
if (!insideGrid(nextX, nextZ)) {
showToast("The ruin ends at that wall.");
return;
}
if (isBlocked(nextX, nextZ)) {
if (isGateCell(nextX, nextZ) && !gatePassable) {
showToast("The gate is sealed. Balance both plates first.");
}
return;
}
const pushedCrate = crateAt(nextX, nextZ);
let crateMoved = false;
if (pushedCrate) {
const destinationX = nextX + dx;
const destinationZ = nextZ + dz;
if (
!insideGrid(destinationX, destinationZ) ||
isBlocked(destinationX, destinationZ) ||
crateAt(destinationX, destinationZ)
) {
showToast("There is no room to push that stone farther.");
return;
}
pushedCrate.cell = { x: destinationX, z: destinationZ };
pushedCrate.targetPosition.set(
worldX(destinationX),
0,
worldZ(destinationZ)
);
crateMoved = true;
}
player.cell = { x: nextX, z: nextZ };
player.targetPosition.set(worldX(nextX), 0, worldZ(nextZ));
player.targetYaw = Math.atan2(dx, dz);
lastMoveTime = now;
updatePuzzleState(crateMoved ? previous : null);
}
const movement = {
up: [0, -1],
down: [0, 1],
left: [-1, 0],
right: [1, 0]
};
window.addEventListener("keydown", event => {
const directions = {
ArrowUp: movement.up,
ArrowDown: movement.down,
ArrowLeft: movement.left,
ArrowRight: movement.right,
KeyW: movement.up,
KeyS: movement.down,
KeyA: movement.left,
KeyD: movement.right
};
if (directions[event.code]) {
event.preventDefault();
tryMove(...directions[event.code]);
} else if (event.code === "KeyR") {
resetPuzzle();
} else if (event.code === "KeyC") {
resetCamera();
}
});
document.querySelectorAll("#touch-pad button").forEach(button => {
button.addEventListener("pointerdown", event => {
event.preventDefault();
const direction = movement[button.dataset.move];
tryMove(...direction);
});
});
function resetPuzzle() {
crateStarts.forEach((start, index) => {
const crate = crates[index];
crate.cell = { x: start.x, z: start.z };
crate.targetPosition.set(worldX(start.x), 0, worldZ(start.z));
});
player.cell = { x: 4, z: 8 };
player.targetPosition.set(worldX(4), 0, worldZ(8));
player.targetYaw = Math.PI;
gateGroup.position.y = 0;
updatePuzzleState(null);
showToast("The stones and the gate have been reset.");
}
document.getElementById("reset-button").addEventListener("click", resetPuzzle);
document.getElementById("camera-button").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
const clock = new THREE.Clock();
function lerpAngle(current, target, amount) {
let difference = (target - current + Math.PI) % (Math.PI * 2) - Math.PI;
if (difference < -Math.PI) difference += Math.PI * 2;
return current + difference * amount;
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const positionEase = 1 - Math.exp(-14 * delta);
playerRoot.position.lerp(player.targetPosition, positionEase);
playerRoot.rotation.y = lerpAngle(
playerRoot.rotation.y,
player.targetYaw,
positionEase
);
for (const crate of crates) {
crate.group.position.lerp(crate.targetPosition, positionEase);
}
gateGroup.position.y = THREE.MathUtils.lerp(
gateGroup.position.y,
gateTargetY,
1 - Math.exp(-5.5 * delta)
);
const pulse = 1 + Math.sin(clock.elapsedTime * 3.2) * 0.08;
gateLamp.scale.setScalar(pulse);
updateCamera();
renderer.render(scene, camera);
}
updatePuzzleState(null);
animate();
setTimeout(() => {
showToast(
"The gate reads both plates. Move the three ochre stones with W A S D or the arrow keys."
);
}, 500);
</script>
</body>
</html>
```