Prompt specification
Build or customize the Three.js 3D scene "Ruined Library Access Puzzle" 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: Ruined Library Access Puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ruined-library-access-puzzle-x3507
Scene Description Prompt:
An original voxel ruined-library puzzle with permanent unlocks, temporary switches, and a reusable tool. Three.js from cdnjs, one HTML file.
Working Three.js Starter (ruined-library-access-puzzle-x3507):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Last Index — A Voxel Library Puzzle</title>
<style>
:root {
--ink: #081115;
--panel: rgba(10, 22, 27, 0.88);
--panel-soft: rgba(16, 31, 36, 0.78);
--line: rgba(218, 195, 133, 0.25);
--gold: #e6c77b;
--paper: #eee7d3;
--muted: #9ca9a7;
--cyan: #72e0dc;
--red: #e18c79;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--ink);
color: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#game, #game canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#game canvas { cursor: grab; }
#game canvas:active { cursor: grabbing; }
#hud {
position: fixed;
inset: 0;
pointer-events: none;
opacity: 0;
transition: opacity 0.5s ease;
}
#hud.visible { opacity: 1; }
.panel {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
#title-panel {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px 16px;
border-radius: 4px 18px 18px 18px;
}
.eyebrow {
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
#title-panel h1 {
margin: 7px 0 13px;
font: 600 27px/1.05 Georgia, "Times New Roman", serif;
letter-spacing: 0.015em;
}
#objective {
min-height: 42px;
color: #d8ded8;
font-size: 13px;
line-height: 1.5;
}
#progress {
display: flex;
gap: 7px;
margin-top: 14px;
}
.progress-step {
flex: 1;
padding: 8px 6px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 8px;
color: #7f8d8d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
transition: 0.3s ease;
}
.progress-step.active {
color: var(--gold);
border-color: rgba(230, 199, 123, 0.35);
background: rgba(230, 199, 123, 0.07);
}
.progress-step.done {
color: var(--cyan);
border-color: rgba(114, 224, 220, 0.34);
background: rgba(114, 224, 220, 0.08);
}
#inventory {
position: absolute;
top: 22px;
right: 22px;
width: 225px;
padding: 16px;
border-radius: 18px 4px 18px 18px;
}
.item-name {
display: flex;
align-items: center;
gap: 10px;
margin-top: 8px;
font: 600 17px Georgia, serif;
}
.key-icon {
width: 19px;
height: 19px;
border: 2px solid var(--gold);
border-radius: 50%;
position: relative;
}
.key-icon::after {
content: "";
position: absolute;
width: 10px;
height: 2px;
background: var(--gold);
right: -8px;
top: 7px;
box-shadow: 3px 3px 0 -0.5px var(--gold);
}
#item-description {
margin-top: 9px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#echo-status {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: var(--cyan);
font: 700 10px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#prompt {
position: absolute;
left: 50%;
bottom: 105px;
transform: translateX(-50%);
min-width: 280px;
padding: 12px 17px;
border-radius: 12px;
text-align: center;
opacity: 0;
transition: opacity 0.15s ease, transform 0.15s ease;
}
#prompt.visible {
opacity: 1;
transform: translateX(-50%) translateY(-4px);
}
.prompt-row {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
color: #e4e7df;
font-size: 12px;
line-height: 1.4;
}
.prompt-row + .prompt-row { margin-top: 6px; }
kbd {
display: inline-grid;
min-width: 27px;
height: 25px;
place-items: center;
border: 1px solid rgba(230, 199, 123, 0.55);
border-bottom-width: 3px;
border-radius: 5px;
background: rgba(230, 199, 123, 0.08);
color: var(--gold);
font: 800 11px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#log {
position: absolute;
left: 22px;
bottom: 22px;
width: min(440px, calc(100vw - 44px));
padding: 14px 17px;
border-radius: 14px 14px 4px 14px;
}
.log-line {
color: #aebbb7;
font-size: 11px;
line-height: 1.55;
}
.log-line:first-child { color: var(--paper); }
.log-line + .log-line { margin-top: 4px; }
#controls {
position: absolute;
right: 22px;
bottom: 22px;
padding: 13px 15px;
border-radius: 4px 14px 14px 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.8;
text-align: right;
}
#controls strong { color: var(--paper); }
#overlay, #win-overlay {
position: fixed;
inset: 0;
display: grid;
place-items: center;
padding: 24px;
background:
radial-gradient(circle at 50% 35%, rgba(42, 78, 81, 0.32), transparent 45%),
rgba(4, 10, 13, 0.93);
z-index: 10;
transition: opacity 0.6s ease, visibility 0.6s ease;
}
#overlay.hidden {
opacity: 0;
visibility: hidden;
pointer-events: none;
}
.intro {
width: min(690px, 100%);
text-align: center;
}
.intro .eyebrow { margin-bottom: 15px; }
.intro h1 {
margin: 0;
font: 500 clamp(44px, 8vw, 78px)/0.95 Georgia, serif;
color: #f0ead8;
text-shadow: 0 5px 40px rgba(0, 0, 0, 0.5);
}
.intro h1 em {
display: block;
margin-top: 10px;
color: var(--gold);
font-size: 0.48em;
font-style: normal;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.intro p {
max-width: 570px;
margin: 25px auto;
color: #aeb9b5;
font: 15px/1.75 Georgia, serif;
}
.feature-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
margin: 25px 0;
}
.feature {
padding: 15px 10px;
border: 1px solid rgba(230, 199, 123, 0.16);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.feature b {
display: block;
margin-bottom: 5px;
color: var(--gold);
font-size: 11px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.feature span {
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#start-button, #restart-button {
padding: 14px 30px;
border: 1px solid var(--gold);
border-radius: 8px;
background: var(--gold);
color: #172024;
cursor: pointer;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 0.2s ease;
}
#start-button:hover, #restart-button:hover {
background: #f2dda8;
transform: translateY(-2px);
}
.control-note {
margin-top: 18px;
color: #758481;
font-size: 11px;
line-height: 1.6;
}
#win-overlay {
opacity: 0;
visibility: hidden;
pointer-events: none;
background: rgba(4, 10, 13, 0.9);
transition: opacity 0.7s ease, visibility 0.7s ease;
}
#win-overlay.visible {
opacity: 1;
visibility: visible;
pointer-events: auto;
}
#win-overlay h2 {
margin: 12px 0;
font: 500 50px Georgia, serif;
}
#win-stats {
margin: 22px 0 28px;
color: var(--muted);
line-height: 1.8;
}
@media (max-width: 720px) {
#inventory { display: none; }
#controls { display: none; }
#title-panel { width: calc(100vw - 44px); }
#prompt { bottom: 22px; width: calc(100vw - 44px); }
#log { display: none; }
.feature-row { grid-template-columns: 1fr; }
.feature { padding: 10px; }
}
</style>
</head>
<body>
<div id="game"></div>
<div id="hud">
<section id="title-panel" class="panel">
<div class="eyebrow">Ruin 07 · Memorial Archive</div>
<h1>The Last Index</h1>
<div id="objective">Restore the archive.</div>
<div id="progress">
<div class="progress-step active" id="step-oath">Oath Door</div>
<div class="progress-step" id="step-echo">Echo Gates</div>
<div class="progress-step" id="step-vault">Index Vault</div>
</div>
</section>
<section id="inventory" class="panel">
<div class="eyebrow">Carried relic</div>
<div class="item-name">
<span class="key-icon"></span>
Mnemonic Key
</div>
<div id="item-description">
Turns oath dials and carries one memory stone.
Reusable indefinitely.
</div>
<div id="echo-status">Echo gates: quiet</div>
</section>
<section id="prompt" class="panel">
<div class="prompt-row"><kbd>E</kbd><span id="prompt-e"></span></div>
<div class="prompt-row"><kbd>F</kbd><span id="prompt-f"></span></div>
</section>
<section id="log" class="panel">
<div class="log-line">The archive waits in silence.</div>
</section>
<section id="controls" class="panel">
<strong>WASD / arrows</strong> move <strong>drag</strong> orbit<br>
<strong>wheel</strong> zoom <strong>shift</strong> hurry<br>
<strong>E</strong> inspect / lever <strong>F</strong> use key
</section>
</div>
<div id="overlay">
<main class="intro">
<div class="eyebrow">An original voxel puzzle</div>
<h1>The Last Index<em>A ruined-library mystery</em></h1>
<p>
The library forgot its final book, but its mechanisms still remember.
Align the three oath dials, cross the vanishing echo gates, and bind
the scattered memory stones to reopen the Index Vault.
</p>
<div class="feature-row">
<div class="feature">
<b>Permanent locks</b>
<span>Some mechanisms remember forever. Solve the archive in order.</span>
</div>
<div class="feature">
<b>Temporary echoes</b>
<span>Levers open gates briefly. Their light does not wait.</span>
</div>
<div class="feature">
<b>One reusable key</b>
<span>The same Mnemonic Key turns dials and carries every stone.</span>
</div>
</div>
<button id="start-button">Enter the Archive</button>
<div class="control-note">
WASD or arrows to move · Drag to orbit · E to inspect and use levers<br>
F to use the Mnemonic Key · Mouse wheel to zoom
</div>
</main>
</div>
<div id="win-overlay">
<main class="intro">
<div class="eyebrow">The Index remembers</div>
<h2>The Book Returns</h2>
<p>
The last catalogue opens without sound. Every forgotten title finds
a shelf again, and the ruined library becomes whole for one quiet moment.
</p>
<div id="win-stats"></div>
<button id="restart-button">Begin Again</button>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* --------------------------------------------------------------------------
DOM and renderer
--------------------------------------------------------------------------- */
const gameElement = document.querySelector("#game");
const hud = document.querySelector("#hud");
const overlay = document.querySelector("#overlay");
const winOverlay = document.querySelector("#win-overlay");
const startButton = document.querySelector("#start-button");
const restartButton = document.querySelector("#restart-button");
const objectiveElement = document.querySelector("#objective");
const promptElement = document.querySelector("#prompt");
const promptE = document.querySelector("#prompt-e");
const promptF = document.querySelector("#prompt-f");
const logElement = document.querySelector("#log");
const echoStatus = document.querySelector("#echo-status");
const winStats = document.querySelector("#win-stats");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091317);
scene.fog = new THREE.FogExp2(0x091317, 0.025);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
gameElement.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 100);
const viewSize = 11.5;
const cameraTarget = new THREE.Vector3(0, 0.8, 6.3);
let cameraYaw = 0;
let cameraPitch = 0.96;
let cameraDistance = 16;
const clock = new THREE.Clock();
const colliders = [];
const logs = [];
let started = false;
let won = false;
let dialTurns = 0;
let switchUses = 0;
let randomSeed = 71239;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function addCollider(x, z, width, depth) {
const collider = {
minX: x - width / 2,
maxX: x + width / 2,
minZ: z - depth / 2,
maxZ: z + depth / 2,
active: true
};
colliders.push(collider);
return collider;
}
/* --------------------------------------------------------------------------
Materials and geometry helpers
--------------------------------------------------------------------------- */
const materials = {
stone: [
new THREE.MeshStandardMaterial({ color: 0x27363a, roughness: 0.96 }),
new THREE.MeshStandardMaterial({ color: 0x304044, roughness: 0.95 }),
new THREE.MeshStandardMaterial({ color: 0x202e32, roughness: 0.98 })
],
darkStone: new THREE.MeshStandardMaterial({ color: 0x172326, roughness: 1 }),
floor: [
new THREE.MeshStandardMaterial({ color: 0x344044, roughness: 0.98 }),
new THREE.MeshStandardMaterial({ color: 0x3c4748, roughness: 0.98 }),
new THREE.MeshStandardMaterial({ color: 0x2c393b, roughness: 1 })
],
wood: new THREE.MeshStandardMaterial({ color: 0x533b2f, roughness: 0.9 }),
woodLight: new THREE.MeshStandardMaterial({ color: 0x78543a, roughness: 0.88 }),
brass: new THREE.MeshStandardMaterial({
color: 0xd2ad5f,
roughness: 0.42,
metalness: 0.7,
emissive: 0x281707,
emissiveIntensity: 0.25
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x70d9d6,
roughness: 0.32,
metalness: 0.25,
emissive: 0x1b7777,
emissiveIntensity: 1.35,
transparent: true,
opacity: 0.76
}),
cyanSolid: new THREE.MeshStandardMaterial({
color: 0x6ad6d2,
emissive: 0x247c7b,
emissiveIntensity: 0.9,
roughness: 0.4
}),
red: new THREE.MeshStandardMaterial({
color: 0xbe6658,
emissive: 0x4b1510,
emissiveIntensity: 0.5,
roughness: 0.6
}),
parchment: new THREE.MeshStandardMaterial({ color: 0xc8b990, roughness: 0.95 }),
black: new THREE.MeshStandardMaterial({ color: 0x101719, roughness: 0.9 }),
skin: new THREE.MeshStandardMaterial({ color: 0xb99172, roughness: 0.9 }),
coat: new THREE.MeshStandardMaterial({ color: 0x345a61, roughness: 0.86 }),
coatDark: new THREE.MeshStandardMaterial({ color: 0x203d43, roughness: 0.9 }),
pants: new THREE.MeshStandardMaterial({ color: 0x232d31, roughness: 0.95 })
};
function addBox(width, height, depth, x, y, z, material, parent = scene, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addSolid(width, height, depth, x, y, z, material) {
const mesh = addBox(width, height, depth, x, y, z, material);
addCollider(x, z, width, depth);
return mesh;
}
function localBox(parent, width, height, depth, x, y, z, material) {
return addBox(width, height, depth, x, y, z, material, parent, true);
}
/* --------------------------------------------------------------------------
Canvas labels
--------------------------------------------------------------------------- */
function labelTexture(text, foreground = "#ead28c", background = "#142126") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
function draw(value, fg = foreground, bg = background) {
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = bg;
context.beginPath();
context.roundRect(5, 5, 502, 150, 24);
context.fill();
context.strokeStyle = fg;
context.globalAlpha = 0.45;
context.lineWidth = 3;
context.stroke();
context.globalAlpha = 1;
let size = 58;
context.font = `700 ${size}px Georgia`;
while (context.measureText(value).width > 450 && size > 22) {
size -= 2;
context.font = `700 ${size}px Georgia`;
}
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = fg;
context.fillText(value, 256, 81);
texture.needsUpdate = true;
}
draw(text);
texture.userData = { canvas, draw };
return texture;
}
function createLabel(text, width = 2, height = 0.62, foreground, background) {
const texture = labelTexture(text, foreground, background);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
return sprite;
}
function updateLabel(sprite, text, foreground, background) {
const data = sprite.material.map.userData;
data.draw(text, foreground, background);
}
function makeRuneLabel(text, color) {
const foreground = {
SUN: "#f0c85f",
MOON: "#8dc9df",
TIDE: "#69dfd0",
KEY: "#e18c79"
}[color] || "#ead28c";
return createLabel(text, 1.45, 0.46, foreground, "#111c20");
}
/* --------------------------------------------------------------------------
Lighting and atmosphere
--------------------------------------------------------------------------- */
scene.add(new THREE.HemisphereLight(0x9dc7c6, 0x151b1d, 1.2));
const sun = new THREE.DirectionalLight(0xf0d7a6, 2.15);
sun.position.set(9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x5b929b, 0.8);
fill.position.set(-10, 9, -13);
scene.add(fill);
const warmLight = new THREE.PointLight(0xe7b86b, 8, 14, 2);
warmLight.position.set(0, 5, 4);
scene.add(warmLight);
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(260 * 3);
for (let i = 0; i < 260; i++) {
dustPositions[i * 3] = (random() - 0.5) * 22;
dustPositions[i * 3 + 1] = random() * 6 + 0.3;
dustPositions[i * 3 + 2] = (random() - 0.5) * 34;
}
dustGeometry.setAttribute("position", new THREE.BufferAttribute(dustPositions, 3));
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xc7b783,
size: 0.035,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(dust);
const water = addBox(
60, 0.08, 70, 0, -0.43, -6,
new THREE.MeshStandardMaterial({
color: 0x153137,
roughness: 0.26,
metalness: 0.45,
transparent: true,
opacity: 0.72
}),
scene,
false
);
water.receiveShadow = false;
/* --------------------------------------------------------------------------
Floors and ruined walls
--------------------------------------------------------------------------- */
function tiledFloor(x0, x1, z0, z1, step) {
let index = 0;
for (let x = x0 + step / 2; x < x1; x += step) {
for (let z = z0 + step / 2; z < z1; z += step) {
const material = materials.floor[Math.floor(random() * materials.floor.length)];
addBox(step - 0.055, 0.28, step - 0.055, x, -0.14, z, material, scene, false);
index++;
}
}
}
tiledFloor(-7, 7, -4, 9, 1.65);
tiledFloor(-4, 4, -12, -4, 1.35);
tiledFloor(-6, 6, -22, -12, 1.65);
function wallX(x0, x1, z, levels = 3) {
const length = x1 - x0;
const count = Math.max(1, Math.ceil(length / 1.35));
const blockWidth = length / count;
for (let level = 0; level < levels; level++) {
for (let i = 0; i < count; i++) {
const material = materials.stone[Math.floor(random() * materials.stone.length)];
addBox(
Math.max(0.1, blockWidth - 0.045),
1.01,
0.58,
x0 + blockWidth * (i + 0.5),
0.505 + level,
z,
material
);
}
}
addCollider((x0 + x1) / 2, z, length, 0.58);
}
function wallZ(z0, z1, x, levels = 3) {
const length = z1 - z0;
const count = Math.max(1, Math.ceil(length / 1.35));
const blockDepth = length / count;
for (let level = 0; level < levels; level++) {
for (let i = 0; i < count; i++) {
const material = materials.stone[Math.floor(random() * materials.stone.length)];
addBox(
0.58,
1.01,
Math.max(0.1, blockDepth - 0.045),
x,
0.505 + level,
z0 + blockDepth * (i + 0.5),
material
);
}
}
addCollider(x, (z0 + z1) / 2, 0.58, length);
}
wallZ(-4, 9, -7.25);
wallZ(-4, 9, 7.25);
wallX(-7.25, 7.25, 9.25);
wallX(-7.25, -2.5, -4);
wallX(2.5, 7.25, -4);
wallZ(-12, -4, -4.25);
wallZ(-12, -4, 4.25);
wallX(-6.25, -4, -12);
wallX(4, 6.25, -12);
wallZ(-22, -12, -6.25);
wallZ(-22, -12, 6.25);
wallX(-6.25, 6.25, -22.25);
wallX(-6.25, -1.65, -20.3);
wallX(1.65, 6.25, -20.3);
/* --------------------------------------------------------------------------
Shelves, rubble, and decorative ruins
--------------------------------------------------------------------------- */
function makeShelf(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
localBox(group, 2.65, 0.16, 0.78, 0, 0.16, 0, materials.wood);
localBox(group, 2.65, 0.16, 0.78, 0, 2.55, 0, materials.wood);
localBox(group, 0.17, 2.55, 0.78, -1.24, 1.35, 0, materials.wood);
localBox(group, 0.17, 2.55, 0.78, 1.24, 1.35, 0, materials.wood);
localBox(group, 2.65, 2.55, 0.12, 0, 1.35, 0.33, materials.darkStone);
for (let shelfIndex = 0; shelfIndex < 3; shelfIndex++) {
localBox(group, 2.38, 0.1, 0.73, 0, 0.78 + shelfIndex * 0.74, 0, materials.woodLight);
let cursor = -1.05;
while (cursor < 1.0) {
const width = 0.09 + random() * 0.12;
const height = 0.34 + random() * 0.28;
const bookMaterial = random() > 0.5 ? materials.red : materials.coat;
localBox(
group,
width,
height,
0.39 + random() * 0.12,
cursor + width / 2,
0.83 + shelfIndex * 0.74 + height / 2,
-0.04,
bookMaterial
);
cursor += width + 0.025 + random() * 0.035;
}
}
const isRotated = Math.abs(Math.sin(rotation)) > 0.5;
if (isRotated) addCollider(x, z, 0.78, 2.65);
else addCollider(x, z, 2.65, 0.78);
}
makeShelf(-6.3, 2.4, Math.PI / 2);
makeShelf(-6.3, 5.7, Math.PI / 2);
makeShelf(6.3, 1.8, Math.PI / 2);
makeShelf(6.3, 5.2, Math.PI / 2);
makeShelf(-4.9, 7.45, 0);
makeShelf(4.7, 7.5, 0);
for (const [x, z] of [[-5.8, -2.5], [5.8, -2.7], [-5.9, 0.3], [5.8, 6.9]]) {
addSolid(0.85, 3.15, 0.85, x, 1.575, z, materials.stone[0]);
addBox(1.12, 0.18, 1.12, x, 3.2, z, materials.stone[1]);
}
for (let i = 0; i < 32; i++) {
const side = random() > 0.5 ? 1 : -1;
const x = side * (6.2 + random() * 0.7);
const z = -3 + random() * 12;
addBox(
0.18 + random() * 0.45,
0.12 + random() * 0.25,
0.18 + random() * 0.45,
x,
0.08,
z,
materials.stone[Math.floor(random() * materials.stone.length)]
);
}
/* --------------------------------------------------------------------------
Permanent doors
--------------------------------------------------------------------------- */
function makeDoor(z, width, name) {
const group = new THREE.Group();
group.position.set(0, 0, z);
scene.add(group);
localBox(group, width, 3.35, 0.48, 0, 1.675, 0, materials.darkStone);
localBox(group, width - 0.25, 0.12, 0.54, 0, 2.65, 0, materials.brass);
localBox(group, width - 0.25, 0.12, 0.54, 0, 0.7, 0, materials.brass);
localBox(group, 0.11, 2.05, 0.55, 0, 1.67, 0, materials.brass);
const emblem = createLabel(name, Math.min(2.4, width * 0.55), 0.54);
emblem.position.set(0, 1.7, 0.31);
group.add(emblem);
const collider = addCollider(0, z, width, 0.55);
return { group, collider, unlocked: false, amount: 0, target: 0, type: name };
}
const oathDoor = makeDoor(-4, 4.65, "OATH");
const vaultDoor = makeDoor(-20.3, 3.25, "INDEX");
/* --------------------------------------------------------------------------
Oath dials and their clues
--------------------------------------------------------------------------- */
const dialDefinitions = [
{
x: -3, z: -0.8,
symbols: ["MOON", "SUN", "TIDE", "KEY"],
target: "SUN",
state: 0,
clue: "I — The first oath is sworn beneath the crowned dawn. Align SUN."
},
{
x: 0, z: -0.8,
symbols: ["KEY", "MOON", "SUN", "TIDE"],
target: "TIDE",
state: 1,
clue: "II — The second oath rises where the sea answers the moon. Align TIDE."
},
{
x: 3, z: -0.8,
symbols: ["SUN", "TIDE", "MOON", "KEY"],
target: "KEY",
state: 0,
clue: "III — The final oath belongs to what opens what remains. Align KEY."
}
];
const dials = dialDefinitions.map((definition, index) => {
addSolid(0.95, 0.62, 0.95, definition.x, 0.31, definition.z, materials.stone[1]);
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
scene.add(group);
localBox(group, 0.78, 0.24, 0.78, 0, 0.75, 0, materials.brass);
localBox(group, 0.57, 0.57, 0.57, 0, 1.12, 0, materials.darkStone);
localBox(group, 0.08, 0.42, 0.61, 0, 1.12, 0, materials.brass);
localBox(group, 0.42, 0.08, 0.61, 0, 1.12, 0, materials.brass);
const numeral = createLabel(["I", "II", "III"][index], 0.75, 0.36);
numeral.position.set(0, 0.42, 0.55);
group.add(numeral);
const symbol = makeRuneLabel(definition.symbols[definition.state], definition.symbols[definition.state]);
symbol.position.set(0, 1.93, 0);
group.add(symbol);
const light = new THREE.PointLight(0xe0b969, 2.2, 4, 2);
light.position.set(0, 1.6, 0);
group.add(light);
return {
...definition,
group,
symbol,
light,
get current() { return this.symbols[this.state]; },
get solved() { return this.current === this.target; }
};
});
/* --------------------------------------------------------------------------
Temporary echo gates
--------------------------------------------------------------------------- */
function makeEchoGate(z, name) {
const group = new THREE.Group();
group.position.set(0, 0, z);
scene.add(group);
localBox(group, 0.3, 3.6, 0.52, -4.02, 1.8, 0, materials.stone[1]);
localBox(group, 0.3, 3.6, 0.52, 4.02, 1.8, 0, materials.stone[1]);
for (let i = 0; i < 9; i++) {
localBox(group, 0.12, 3.25, 0.18, -3.6 + i * 0.9, 1.62, 0, materials.cyan);
}
localBox(group, 8, 0.1, 0.2, 0, 0.24, 0, materials.cyanSolid);
localBox(group, 8, 0.1, 0.2, 0, 3.12, 0, materials.cyanSolid);
const collider = addCollider(0, z, 8, 0.3);
const light = new THREE.PointLight(0x53dbd6, 3.5, 8, 2);
light.position.set(0, 2, 0);
group.add(light);
return { group, collider, light, z, name, activeUntil: 0, lift: 0 };
}
const echoGates = [
makeEchoGate(-7.65, "FIRST"),
makeEchoGate(-10.95, "SECOND")
];
function makeSwitch(x, z, gate, name) {
addSolid(0.78, 0.58, 0.78, x, 0.29, z, materials.stone[1]);
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
localBox(group, 0.67, 0.12, 0.67, 0, 0.66, 0, materials.brass);
const pivot = new THREE.Group();
pivot.position.set(0, 0.7, 0);
group.add(pivot);
localBox(pivot, 0.13, 0.65, 0.13, 0, 0.31, 0, materials.brass);
localBox(pivot, 0.35, 0.17, 0.23, 0, 0.61, 0, materials.red);
const label = createLabel(name, 1.3, 0.4, "#80e4df", "#111c20");
label.position.set(0, 1.55, 0);
group.add(label);
const light = new THREE.PointLight(0x5edbd6, 0.5, 4, 2);
light.position.set(0, 1, 0);
group.add(light);
return { x, z, gate, group, pivot, light, name };
}
const switches = [
makeSwitch(-2.25, -5.65, echoGates[0], "ECHO I"),
makeSwitch(2.25, -9.45, echoGates[1], "ECHO II")
];
/* --------------------------------------------------------------------------
Memory stones and permanent sockets
--------------------------------------------------------------------------- */
const stoneDefinitions = [
{
symbol: "SUN",
color: 0xe0b957,
home: { x: -4.2, z: -14.1 },
socket: { x: 3.25, z: -18.2 }
},
{
symbol: "MOON",
color: 0x86bfd7,
home: { x: 0, z: -14.65 },
socket: { x: -3.25, z: -18.2 }
},
{
symbol: "TIDE",
color: 0x59c6b7,
home: { x: 4.2, z: -14.1 },
socket: { x: 0, z: -18.2 }
}
];
function makeStone(definition) {
const stoneMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
emissive: new THREE.Color(definition.color).multiplyScalar(0.22),
emissiveIntensity: 0.75,
roughness: 0.38,
metalness: 0.18
});
const group = new THREE.Group();
const core = localBox(group, 0.7, 0.7, 0.7, 0, 0, 0, stoneMaterial);
core.rotation.y = Math.PI / 4;
localBox(group, 0.38, 0.38, 0.38, 0, 0, 0, stoneMaterial);
const label = makeRuneLabel(definition.symbol, definition.symbol);
label.position.set(0, 0.82, 0);
group.add(label);
group.position.set(definition.home.x, 0.94, definition.home.z);
scene.add(group);
addSolid(0.92, 0.6, 0.92, definition.home.x, 0.3, definition.home.z, materials.stone[1]);
return {
...definition,
group,
label,
state: "home"
};
}
const stones = stoneDefinitions.map(makeStone);
const sockets = stones.map((stone) => {
const group = new THREE.Group();
group.position.set(stone.socket.x, 0, stone.socket.z);
scene.add(group);
localBox(group, 1.12, 0.12, 0.16, 0, 0.08, 0.48, materials.brass);
localBox(group, 1.12, 0.12, 0.16, 0, 0.08, -0.48, materials.brass);
localBox(group, 0.16, 0.12, 0.8, 0.48, 0.08, 0, materials.brass);
localBox(group, 0.16, 0.12, 0.8, -0.48, 0.08, 0, materials.brass);
const label = makeRuneLabel(stone.symbol, stone.symbol);
label.position.set(0, 0.45, 0);
group.add(label);
const light = new THREE.PointLight(stone.color, 0.25, 3.5, 2);
light.position.set(0, 0.5, 0);
group.add(light);
return {
symbol: stone.symbol,
x: stone.socket.x,
z: stone.socket.z,
group,
light,
bound: false
};
});
/* --------------------------------------------------------------------------
Player
--------------------------------------------------------------------------- */
const player = new THREE.Group();
player.position.set(0, 0, 6.3);
player.rotation.y = Math.PI;
scene.add(player);
localBox(player, 0.68, 0.82, 0.42, 0, 1.05, 0, materials.coat);
localBox(player, 0.75, 0.16, 0.5, 0, 1.38, 0, materials.coatDark);
localBox(player, 0.49, 0.48, 0.47, 0, 1.67, 0, materials.skin);
localBox(player, 0.56, 0.15, 0.53, 0, 1.92, 0, materials.coatDark);
localBox(player, 0.12, 0.36, 0.12, 0, 2.05, 0, materials.brass);
const leftArm = new THREE.Group();
leftArm.position.set(-0.44, 1.35, 0);
player.add(leftArm);
localBox(leftArm, 0.2, 0.67, 0.22, 0, -0.27, 0, materials.coatDark);
localBox(leftArm, 0.2, 0.18, 0.22, 0, -0.63, 0, materials.skin);
const rightArm = new THREE.Group();
rightArm.position.set(0.44, 1.35, 0);
player.add(rightArm);
localBox(rightArm, 0.2, 0.67, 0.22, 0, -0.27, 0, materials.coatDark);
localBox(rightArm, 0.2, 0.18, 0.22, 0, -0.63, 0, materials.skin);
const legLeft = new THREE.Group();
legLeft.position.set(-0.2, 0.67, 0);
player.add(legLeft);
localBox(legLeft, 0.25, 0.61, 0.28, 0, -0.29, 0, materials.pants);
localBox(legLeft, 0.28, 0.16, 0.42, 0, -0.56, 0.08, materials.black);
const legRight = new THREE.Group();
legRight.position.set(0.2, 0.67, 0);
player.add(legRight);
localBox(legRight, 0.25, 0.61, 0.28, 0, -0.29, 0, materials.pants);
localBox(legRight, 0.28, 0.16, 0.42, 0, -0.56, 0.08, materials.black);
const tool = new THREE.Group();
tool.position.set(0.48, 0.78, 0.14);
tool.rotation.z = -0.5;
player.add(tool);
localBox(tool, 0.1, 0.55, 0.1, 0, 0, 0, materials.brass);
localBox(tool, 0.28, 0.1, 0.1, 0.08, 0.25, 0, materials.brass);
localBox(tool, 0.1, 0.22, 0.1, 0.15, 0.13, 0, materials.brass);
localBox(tool, 0.16, 0.09, 0.16, 0, -0.31, 0, materials.cyanSolid);
const carriedStone = new THREE.Group();
carriedStone.visible = false;
player.add(carriedStone);
const focusMarker = new THREE.Group();
scene.add(focusMarker);
for (const [x, z] of [[-0.55, -0.55], [0.55, -0.55], [-0.55, 0.55], [0.55, 0.55]]) {
localBox(focusMarker, 0.22, 0.035, 0.07, x, 0.04, z, materials.cyanSolid);
}
focusMarker.visible = false;
/* --------------------------------------------------------------------------
Audio: generated locally, no assets
--------------------------------------------------------------------------- */
let audioContext = null;
function initAudio() {
if (!audioContext) {
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
if (AudioContextClass) audioContext = new AudioContextClass();
}
if (audioContext?.state === "suspended") audioContext.resume();
}
function tone(frequency, duration = 0.08, type = "sine", volume = 0.025, delay = 0) {
if (!audioContext) return;
const start = audioContext.currentTime + delay;
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = type;
oscillator.frequency.setValueAtTime(frequency, start);
gain.gain.setValueAtTime(0.0001, start);
gain.gain.exponentialRampToValueAtTime(volume, start + 0.012);
gain.gain.exponentialRampToValueAtTime(0.0001, start + duration);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start(start);
oscillator.stop(start + duration + 0.02);
}
/* --------------------------------------------------------------------------
Messages and objective state
--------------------------------------------------------------------------- */
function addMessage(message) {
logs.unshift(message);
if (logs.length > 4) logs.pop();
logElement.replaceChildren();
for (const text of logs) {
const line = document.createElement("div");
line.className = "log-line";
line.textContent = text;
logElement.appendChild(line);
}
}
function countSolvedDials() {
return dials.filter((dial) => dial.solved).length;
}
function countBoundStones() {
return sockets.filter((socket) => socket.bound).length;
}
function updateObjective() {
const solved = countSolvedDials();
const bound = countBoundStones();
if (!oathDoor.unlocked) {
objectiveElement.textContent =
`Restore the Oath Door — ${solved}/3 dials aligned. E reads each inscription; F turns the dial with the Mnemonic Key.`;
} else if (player.position.z > -7.25) {
objectiveElement.textContent =
"The Oath Door is permanently open. Wake the first Echo Gate, then cross before its light fades.";
} else if (player.position.z > -10.7) {
objectiveElement.textContent =
"The first echo has vanished behind you. Wake the second Echo Gate and pass through.";
} else if (bound < 3) {
objectiveElement.textContent =
`Bind the memory stones to their matching sockets — ${bound}/3 permanently sealed. F lifts and places them.`;
} else if (!vaultDoor.unlocked) {
objectiveElement.textContent =
"All three memories are bound. The Index Vault is unlocking.";
} else {
objectiveElement.textContent =
"The Index Vault is open. Step through the central doorway and recover the last book.";
}
document.querySelector("#step-oath").className =
"progress-step " + (oathDoor.unlocked ? "done" : "active");
document.querySelector("#step-echo").className =
"progress-step " + (player.position.z < -10.8 ? "done" : oathDoor.unlocked ? "active" : "");
document.querySelector("#step-vault").className =
"progress-step " + (vaultDoor.unlocked ? "done" : bound > 0 ? "active" : "");
}
function updateEchoStatus() {
const now = performance.now();
const active = switches.filter((item) => item.gate.activeUntil > now);
if (!active.length) {
echoStatus.textContent = "Echo gates: quiet";
return;
}
echoStatus.textContent = active.map((item) => {
const remaining = Math.max(0, (item.gate.activeUntil - now) / 1000);
return `${item.name} ${remaining.toFixed(1)}s`;
}).join(" · ");
}
/* --------------------------------------------------------------------------
Interactions
--------------------------------------------------------------------------- */
function distance2D(a, b) {
return Math.hypot(a.x - b.x, a.z - b.z);
}
function currentCarriedStone() {
return stones.find((stone) => stone.state === "carried") || null;
}
function getActions() {
const eActions = [];
const fActions = [];
for (const item of switches) {
const distance = distance2D(player.position, item);
if (distance < 1.9) {
const active = item.gate.activeUntil > performance.now();
eActions.push({
kind: "switch",
data: item,
distance,
label: active
? `Cancel ${item.name} before it fades`
: `Wake ${item.name} — the gate lasts 9 seconds`
});
}
}
for (const dial of dials) {
const distance = distance2D(player.position, dial);
if (distance < 2.05) {
eActions.push({
kind: "clue",
data: dial,
distance,
label: `Read oath inscription ${dial.solved ? "again" : ""}`.trim()
});
if (!currentCarriedStone()) {
fActions.push({
kind: "turn-dial",
data: dial,
distance,
label: `Turn dial — currently ${dial.current}`
});
}
}
}
const carried = currentCarriedStone();
if (carried) {
for (const socket of sockets) {
const distance = distance2D(player.position, socket);
if (distance < 1.9) {
fActions.push({
kind: "place-stone",
data: { stone: carried, socket },
distance,
label: socket.symbol === carried.symbol
? `Bind ${carried.symbol} to the ${socket.symbol} socket`
: `Try ${carried.symbol} in the ${socket.symbol} socket`
});
}
}
const distanceHome = distance2D(player.position, carried.home);
if (distanceHome < 1.9) {
fActions.push({
kind: "drop-stone",
data: carried,
distance: distanceHome,
label: `Return ${carried.symbol} to its pedestal`
});
}
} else {
for (const stone of stones) {
if (stone.state !== "home") continue;
const distance = distance2D(player.position, stone);
if (distance < 1.9) {
fActions.push({
kind: "pick-stone",
data: stone,
distance,
label: `Lift the ${stone.symbol} memory stone`
});
}
}
}
eActions.sort((a, b) => a.distance - b.distance);
fActions.sort((a, b) => a.distance - b.distance);
return { e: eActions[0] || null, f: fActions[0] || null };
}
function performE() {
const action = getActions().e;
if (!action) return;
if (action.kind === "switch") {
const item = action.data;
const now = performance.now();
if (item.gate.activeUntil > now) {
item.gate.activeUntil = 0;
addMessage(`${item.name} is silenced. Its gate will close.`);
tone(185, 0.13, "triangle", 0.018);
} else {
item.gate.activeUntil = now + 9000;
switchUses++;
addMessage(`${item.name} answers for nine seconds. Move.`);
tone(390, 0.11, "triangle", 0.022);
tone(585, 0.16, "sine", 0.014, 0.08);
}
}
if (action.kind === "clue") {
addMessage(action.data.clue);
tone(260, 0.08, "sine", 0.014);
}
}
function performF() {
const action = getActions().f;
if (!action) return;
if (action.kind === "turn-dial") {
const dial = action.data;
dial.state = (dial.state + 1) % dial.symbols.length;
dialTurns++;
updateLabel(dial.symbol, dial.current, dial.current);
if (dial.solved) {
dial.light.color.setHex(0x62d9cf);
dial.light.intensity = 3.4;
addMessage(`The ${dial.current} oath settles into place.`);
tone(440, 0.1, "sine", 0.022);
tone(660, 0.18, "sine", 0.017, 0.07);
} else {
tone(210 + dial.state * 35, 0.07, "triangle", 0.014);
}
if (dials.every((item) => item.solved) && !oathDoor.unlocked) {
oathDoor.unlocked = true;
oathDoor.target = 1;
oathDoor.collider.active = false;
addMessage("Three oaths agree. The Oath Door sinks into the archive floor forever.");
tone(196, 0.28, "triangle", 0.02);
tone(294, 0.3, "sine", 0.018, 0.12);
tone(392, 0.42, "sine", 0.017, 0.24);
}
}
if (action.kind === "pick-stone") {
const stone = action.data;
stone.state = "carried";
player.add(stone.group);
stone.group.position.set(0, 1.35, 0.78);
stone.group.scale.setScalar(0.82);
carriedStone.visible = false;
addMessage(`The ${stone.symbol} memory stone hangs from the Mnemonic Key.`);
tone(330, 0.09, "sine", 0.018);
}
if (action.kind === "drop-stone") {
const stone = action.data;
stone.state = "home";
scene.add(stone.group);
stone.group.position.set(stone.home.x, 0.94, stone.home.z);
stone.group.scale.setScalar(1);
addMessage(`The ${stone.symbol} stone returns to its pedestal.`);
tone(220, 0.08, "triangle", 0.013);
}
if (action.kind === "place-stone") {
const { stone, socket } = action.data;
if (stone.symbol !== socket.symbol) {
addMessage(`The ${stone.symbol} stone recoils from the ${socket.symbol} socket. Find its matching seal.`);
tone(115, 0.18, "sawtooth", 0.014);
return;
}
stone.state = "placed";
scene.add(stone.group);
stone.group.position.set(socket.x, 0.58, socket.z);
stone.group.scale.setScalar(1);
socket.bound = true;
socket.light.intensity = 4.5;
addMessage(`${stone.symbol} is permanently bound. The archive remembers another title.`);
tone(330, 0.13, "sine", 0.02);
tone(495, 0.18, "sine", 0.018, 0.09);
tone(660, 0.25, "sine", 0.016, 0.18);
if (sockets.every((item) => item.bound) && !vaultDoor.unlocked) {
vaultDoor.unlocked = true;
vaultDoor.target = 1;
vaultDoor.collider.active = false;
addMessage("Three memories make one key. The Index Vault opens permanently.");
tone(147, 0.38, "triangle", 0.02);
tone(220, 0.42, "triangle", 0.018, 0.14);
tone(294, 0.48, "sine", 0.017, 0.28);
}
}
}
function updatePrompt() {
const actions = getActions();
promptE.textContent = actions.e?.label || "";
promptF.textContent = actions.f?.label || "";
promptElement.classList.toggle("visible", Boolean(actions.e || actions.f));
const focus = actions.f || actions.e;
focusMarker.visible = Boolean(focus);
if (focus) {
const data = focus.data;
const x = data.x ?? data.home?.x ?? data.socket?.x;
const z = data.z ?? data.home?.z ?? data.socket?.z;
focusMarker.position.set(x, 0.03, z);
focusMarker.rotation.y += 0.015;
}
}
/* --------------------------------------------------------------------------
Input and collision
--------------------------------------------------------------------------- */
const keys = new Set();
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (["arrowup", "arrowdown", "arrowleft", "arrowright", " "].includes(key)) {
event.preventDefault();
}
if (!started || won) return;
keys.add(key);
if (!event.repeat && key === "e") performE();
if (!event.repeat && key === "f") performF();
});
window.addEventListener("keyup", (event) => {
keys.delete(event.key.toLowerCase());
});
window.addEventListener("blur", () => {
keys.clear();
dragging = false;
});
renderer.domElement.addEventListener("pointerdown", (event) => {
if (!started) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
cameraYaw -= deltaX * 0.007;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + deltaY * 0.005, 0.65, 1.35);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => { dragging = false; });
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
viewSize = THREE.MathUtils.clamp(viewSize + event.deltaY * 0.008, 8, 17);
}, { passive: false });
function isBlocked(x, z, radius = 0.32) {
for (const collider of colliders) {
if (!collider.active) continue;
const closestX = THREE.MathUtils.clamp(x, collider.minX, collider.maxX);
const closestZ = THREE.MathUtils.clamp(z, collider.minZ, collider.maxZ);
const dx = x - closestX;
const dz = z - closestZ;
if (dx * dx + dz * dz < radius * radius) return true;
}
return false;
}
function updatePlayer(delta) {
if (!started || won) return;
const horizontal =
(keys.has("d") || keys.has("arrowright") ? 1 : 0) -
(keys.has("a") || keys.has("arrowleft") ? 1 : 0);
const vertical =
(keys.has("w") || keys.has("arrowup") ? 1 : 0) -
(keys.has("s") || keys.has("arrowdown") ? 1 : 0);
const length = Math.hypot(horizontal, vertical);
let moving = false;
if (length > 0) {
const sprinting = keys.has("shift");
const speed = sprinting ? 6.2 : 4.15;
const forwardX = -Math.sin(cameraYaw);
const forwardZ = -Math.cos(cameraYaw);
const rightX = Math.cos(cameraYaw);
const rightZ = -Math.sin(cameraYaw);
let moveX = (rightX * horizontal + forwardX * vertical) / length;
let moveZ = (rightZ * horizontal + forwardZ * vertical) / length;
const proposedX = player.position.x + moveX * speed * delta;
if (!isBlocked(proposedX, player.position.z)) {
player.position.x = proposedX;
}
const proposedZ = player.position.z + moveZ * speed * delta;
if (!isBlocked(player.position.x, proposedZ)) {
player.position.z = proposedZ;
}
const targetRotation = Math.atan2(moveX, moveZ);
let difference = targetRotation - player.rotation.y;
difference = Math.atan2(Math.sin(difference), Math.cos(difference));
player.rotation.y += difference * Math.min(1, delta * 12);
moving = true;
}
const swing = moving ? Math.sin(clock.elapsedTime * 11) * 0.55 : 0;
legLeft.rotation.x = THREE.MathUtils.lerp(legLeft.rotation.x, swing, 0.18);
legRight.rotation.x = THREE.MathUtils.lerp(legRight.rotation.x, -swing, 0.18);
leftArm.rotation.x = THREE.MathUtils.lerp(leftArm.rotation.x, -swing * 0.55, 0.14);
rightArm.rotation.x = THREE.MathUtils.lerp(rightArm.rotation.x, swing * 0.55, 0.14);
const carried = currentCarriedStone();
carriedStone.visible = Boolean(carried);
if (carried) {
carriedStone.position.set(0, 1.35 + Math.sin(clock.elapsedTime * 3) * 0.035, 0.78);
}
if (!won && vaultDoor.unlocked && player.position.z < -21.15) {
triggerVictory();
}
}
function updateCamera(delta) {
const desiredTarget = new THREE.Vector3(player.position.x, 0.85, player.position.z);
cameraTarget.lerp(desiredTarget, 1 - Math.pow(0.001, delta));
const horizontalDistance = Math.cos(cameraPitch) * cameraDistance;
const offset = new THREE.Vector3(
Math.sin(cameraYaw) * horizontalDistance,
Math.sin(cameraPitch) * cameraDistance,
Math.cos(cameraYaw) * horizontalDistance
);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
camera.left = (-viewSize * aspect) / 2;
camera.right = (viewSize * aspect) / 2;
camera.top = viewSize / 2;
camera.bottom = -viewSize / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
}
window.addEventListener("resize", resize);
resize();
/* --------------------------------------------------------------------------
Mechanism animation
--------------------------------------------------------------------------- */
function updateMechanisms(delta) {
const now = performance.now();
for (const item of switches) {
const active = item.gate.activeUntil > now;
item.pivot.rotation.z = THREE.MathUtils.lerp(
item.pivot.rotation.z,
active ? -0.82 : 0.08,
Math.min(1, delta * 9)
);
item.light.intensity = THREE.MathUtils.lerp(
item.light.intensity,
active ? 4.2 : 0.45,
Math.min(1, delta * 8)
);
}
for (const gate of echoGates) {
const active = gate.activeUntil > now;
gate.lift = THREE.MathUtils.lerp(gate.lift, active ? 1 : 0, Math.min(1, delta * 5.5));
gate.group.position.y = gate.lift * 4.2;
gate.light.intensity = 0.7 + gate.lift * 3.2;
const playerInside =
Math.abs(player.position.x - gate.z * 0) < 4.1 &&
Math.abs(player.position.z - gate.z) < 0.55;
gate.collider.active = gate.lift < 0.68 && !(active === false && playerInside);
}
oathDoor.amount = THREE.MathUtils.lerp(oathDoor.amount, oathDoor.target, Math.min(1, delta * 2.5));
oathDoor.group.position.y = -oathDoor.amount * 3.55;
vaultDoor.amount = THREE.MathUtils.lerp(vaultDoor.amount, vaultDoor.target, Math.min(1, delta * 1.7));
vaultDoor.group.position.y = vaultDoor.amount * 5.4;
for (const dial of dials) {
dial.symbol.position.y = 1.93 + Math.sin(clock.elapsedTime * 2 + dial.x) * 0.035;
if (dial.solved) {
dial.symbol.material.opacity = 0.95;
}
}
for (const socket of sockets) {
socket.group.rotation.y = Math.sin(clock.elapsedTime * 0.4) * 0.04;
}
const positions = dust.geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
let y = positions.getY(i) + delta * (0.018 + (i % 5) * 0.003);
if (y > 6.2) y = 0.2;
positions.setY(i, y);
}
positions.needsUpdate = true;
}
/* --------------------------------------------------------------------------
Start and victory
--------------------------------------------------------------------------- */
function triggerVictory() {
if (won) return;
won = true;
keys.clear();
winStats.innerHTML =
`Oath dials aligned: <strong>${countSolvedDials()}/3</strong><br>` +
`Dial turns: <strong>${dialTurns}</strong><br>` +
`Echo levers used: <strong>${switchUses}</strong><br>` +
`Memories permanently bound: <strong>${countBoundStones()}/3</strong>`;
setTimeout(() => winOverlay.classList.add("visible"), 450);
tone(196, 0.65, "triangle", 0.02);
tone(294, 0.7, "sine", 0.018, 0.12);
tone(392, 0.85, "sine", 0.018, 0.26);
tone(587, 1.1, "sine", 0.014, 0.42);
}
startButton.addEventListener("click", () => {
initAudio();
started = true;
overlay.classList.add("hidden");
hud.classList.add("visible");
addMessage("Three oath dials guard the northern door. Inspect each with E.");
addMessage("The Mnemonic Key is infinite: it turns dials and carries memory stones.");
tone(220, 0.2, "triangle", 0.016);
});
restartButton.addEventListener("click", () => window.location.reload());
/* --------------------------------------------------------------------------
Main loop
--------------------------------------------------------------------------- */
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
updatePlayer(delta);
updateCamera(delta);
updateMechanisms(delta);
if (started) {
updatePrompt();
updateObjective();
updateEchoStatus();
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```