Prompt specification
Build or customize the Three.js 3D scene "Repeat a Three-Step Switch Pattern" 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: Repeat a Three-Step Switch Pattern
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/repeat-a-three-step-switch-pattern-x3466
Scene Description Prompt:
Create a tabletop memory lock that plays and accepts a three-step sequence. Compare ordered inputs exactly and provide replay without changing the target pattern. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (repeat-a-three-step-switch-pattern-x3466):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mnemo Lock — Three-Step Memory Lock</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(10, 20, 34, 0.86);
--panel-border: rgba(151, 201, 255, 0.18);
--text: #edf7ff;
--muted: #91a6bb;
--cyan: #57d9ff;
--green: #67f29a;
--red: #ff647d;
--amber: #ffc857;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
border: 1px solid var(--panel-border);
background:
linear-gradient(145deg, rgba(25, 43, 65, 0.62), transparent),
var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.36),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.info-panel {
position: absolute;
top: 20px;
left: 20px;
width: min(330px, calc(100vw - 40px));
padding: 20px;
border-radius: 18px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.55rem, 4vw, 2.15rem);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 9px 0 16px;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.45;
}
.status {
padding: 12px 13px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
background: rgba(2, 8, 16, 0.38);
}
.status[data-tone="watch"] {
border-color: rgba(87, 217, 255, 0.36);
}
.status[data-tone="input"] {
border-color: rgba(255, 200, 87, 0.36);
}
.status[data-tone="success"] {
border-color: rgba(103, 242, 154, 0.44);
}
.status[data-tone="fail"] {
border-color: rgba(255, 100, 125, 0.44);
}
#statusTitle {
display: block;
font-size: 0.94rem;
font-weight: 750;
}
#statusDetail {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 0.76rem;
line-height: 1.4;
}
.sequence-block {
margin-top: 15px;
}
.sequence-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.sequence-label span:last-child {
letter-spacing: 0;
font-weight: 550;
text-transform: none;
}
.sequence {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.slot {
display: grid;
min-height: 43px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 10px;
background: rgba(0, 0, 0, 0.2);
color: #718499;
font-size: 1.15rem;
font-weight: 850;
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.slot.filled {
color: var(--slot-color, var(--text));
border-color: color-mix(in srgb, var(--slot-color, var(--cyan)) 48%, transparent);
background: color-mix(in srgb, var(--slot-color, var(--cyan)) 10%, transparent);
}
.slot.broadcast {
color: var(--cyan);
border-color: rgba(87, 217, 255, 0.7);
background: rgba(87, 217, 255, 0.12);
transform: translateY(-2px);
box-shadow: 0 0 20px rgba(87, 217, 255, 0.16);
}
.console {
position: absolute;
bottom: 20px;
left: 50%;
width: min(720px, calc(100vw - 32px));
padding: 15px;
border-radius: 18px;
transform: translateX(-50%);
}
.console-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.console-copy {
min-width: 0;
}
.console-title {
display: block;
font-size: 0.83rem;
font-weight: 750;
}
.console-hint {
display: block;
margin-top: 2px;
overflow: hidden;
color: var(--muted);
font-size: 0.69rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.actions {
display: flex;
flex-shrink: 0;
gap: 7px;
}
.action-button {
cursor: pointer;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
padding: 8px 11px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font-size: 0.7rem;
font-weight: 750;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
.action-button:hover {
border-color: rgba(87, 217, 255, 0.42);
background: rgba(87, 217, 255, 0.09);
}
.action-button:active {
transform: translateY(1px);
}
.action-button.primary {
border-color: rgba(87, 217, 255, 0.35);
color: #a9edff;
}
.pad-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 9px;
}
.pad-button {
--pad: var(--cyan);
position: relative;
display: flex;
min-height: 67px;
cursor: pointer;
align-items: center;
justify-content: center;
gap: 9px;
border: 1px solid color-mix(in srgb, var(--pad) 30%, transparent);
border-radius: 13px;
background:
linear-gradient(
145deg,
color-mix(in srgb, var(--pad) 9%, transparent),
rgba(0, 0, 0, 0.16)
);
color: var(--text);
transition:
transform 130ms ease,
border-color 130ms ease,
background 130ms ease,
opacity 130ms ease;
}
.pad-button:hover:not(:disabled) {
border-color: color-mix(in srgb, var(--pad) 75%, transparent);
background:
linear-gradient(
145deg,
color-mix(in srgb, var(--pad) 17%, transparent),
rgba(0, 0, 0, 0.13)
);
transform: translateY(-2px);
}
.pad-button:active:not(:disabled) {
transform: translateY(1px);
}
.pad-button:disabled {
cursor: default;
opacity: 0.43;
}
.glyph {
color: var(--pad);
font-size: 1.5rem;
line-height: 1;
text-shadow: 0 0 15px color-mix(in srgb, var(--pad) 40%, transparent);
}
.pad-name {
font-size: 0.73rem;
font-weight: 750;
}
kbd {
position: absolute;
top: 6px;
right: 7px;
color: rgba(255, 255, 255, 0.35);
font-family: inherit;
font-size: 0.55rem;
font-weight: 700;
}
.camera-hint {
position: absolute;
top: 20px;
right: 20px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 999px;
background: rgba(7, 17, 29, 0.58);
color: var(--muted);
font-size: 0.66rem;
backdrop-filter: blur(10px);
}
button:focus-visible {
outline: 2px solid #a9edff;
outline-offset: 3px;
}
@media (max-width: 700px) {
.info-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.subtitle {
display: none;
}
.sequence-block {
display: none;
}
.status {
margin-top: 12px;
padding: 9px 11px;
}
.camera-hint {
display: none;
}
.console {
bottom: 12px;
width: calc(100vw - 24px);
padding: 11px;
}
.console-top {
align-items: flex-start;
margin-bottom: 9px;
}
.console-hint {
max-width: 180px;
}
.action-button {
padding: 7px 9px;
font-size: 0.64rem;
}
.pad-grid {
gap: 6px;
}
.pad-button {
min-height: 58px;
flex-direction: column;
gap: 4px;
}
.pad-name {
font-size: 0.61rem;
}
.glyph {
font-size: 1.25rem;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly tabletop memory lock"></div>
<div class="hud">
<section class="panel info-panel" aria-label="Game status">
<p class="eyebrow">Three-step memory lock</p>
<h1>Mnemo Lock</h1>
<p class="subtitle">
Watch the three illuminated pads, then enter them in the exact same order.
</p>
<div id="statusBox" class="status" data-tone="watch" aria-live="polite">
<span id="statusTitle">Preparing sequence</span>
<span id="statusDetail">The lock is generating a three-step target.</span>
</div>
<div class="sequence-block">
<div class="sequence-label">
<span>Target</span>
<span id="targetState">Concealed</span>
</div>
<div id="targetSlots" class="sequence" aria-label="Target sequence"></div>
<div class="sequence-label" style="margin-top: 12px">
<span>Your entry</span>
<span id="entryCount">0 / 3</span>
</div>
<div id="attemptSlots" class="sequence" aria-label="Your input"></div>
</div>
</section>
<section class="panel console" aria-label="Sequence controls">
<div class="console-top">
<div class="console-copy">
<span class="console-title">Enter the lock sequence</span>
<span class="console-hint">
Tap the 3D pads or use keys 1–4. Replay keeps the same target.
</span>
</div>
<div class="actions">
<button id="clearButton" class="action-button" type="button">
Clear
</button>
<button id="replayButton" class="action-button primary" type="button">
Replay Target
</button>
<button id="newButton" class="action-button" type="button">
New Target
</button>
</div>
</div>
<div class="pad-grid">
<button
class="pad-button"
type="button"
data-pad="0"
style="--pad: #ffb454"
aria-label="Enter triangle"
disabled
>
<span class="glyph">▲</span>
<span class="pad-name">Triangle</span>
<kbd>1</kbd>
</button>
<button
class="pad-button"
type="button"
data-pad="1"
style="--pad: #57d9ff"
aria-label="Enter circle"
disabled
>
<span class="glyph">●</span>
<span class="pad-name">Circle</span>
<kbd>2</kbd>
</button>
<button
class="pad-button"
type="button"
data-pad="2"
style="--pad: #ff6ca8"
aria-label="Enter square"
disabled
>
<span class="glyph">■</span>
<span class="pad-name">Square</span>
<kbd>3</kbd>
</button>
<button
class="pad-button"
type="button"
data-pad="3"
style="--pad: #67f29a"
aria-label="Enter star"
disabled
>
<span class="glyph">★</span>
<span class="pad-name">Star</span>
<kbd>4</kbd>
</button>
</div>
</section>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Click a pad</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Game state
// -------------------------------------------------------------------------
const PAD_META = [
{ name: "Triangle", symbol: "▲", color: 0xffb454, css: "#ffb454" },
{ name: "Circle", symbol: "●", color: 0x57d9ff, css: "#57d9ff" },
{ name: "Square", symbol: "■", color: 0xff6ca8, css: "#ff6ca8" },
{ name: "Star", symbol: "★", color: 0x67f29a, css: "#67f29a" }
];
const targetSequence = [];
const attempt = [];
const attemptLitPads = new Set();
let mode = "idle";
let playbackToken = 0;
let entryToken = 0;
let playbackIndex = -1;
let playbackStepCount = 0;
let playbackTimers = new Set();
const statusBox = document.getElementById("statusBox");
const statusTitle = document.getElementById("statusTitle");
const statusDetail = document.getElementById("statusDetail");
const targetSlots = document.getElementById("targetSlots");
const attemptSlots = document.getElementById("attemptSlots");
const targetState = document.getElementById("targetState");
const entryCount = document.getElementById("entryCount");
const clearButton = document.getElementById("clearButton");
const replayButton = document.getElementById("replayButton");
const newButton = document.getElementById("newButton");
const padButtons = [...document.querySelectorAll(".pad-button")];
function generateTarget() {
const pool = [0, 1, 2, 3];
for (let i = pool.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[pool[i], pool[j]] = [pool[j], pool[i]];
}
return pool.slice(0, 3);
}
function setStatus(title, detail, tone = "neutral") {
statusTitle.textContent = title;
statusDetail.textContent = detail;
statusBox.dataset.tone = tone;
}
function renderSlots() {
const revealTarget = mode === "success" || mode === "fail";
targetSlots.replaceChildren();
attemptSlots.replaceChildren();
for (let i = 0; i < 3; i++) {
const targetSlot = document.createElement("div");
targetSlot.className = "slot";
targetSlot.style.setProperty(
"--slot-color",
PAD_META[targetSequence[i]].css
);
targetSlot.textContent = revealTarget
? PAD_META[targetSequence[i]].symbol
: "?";
targetSlot.classList.toggle("filled", revealTarget);
targetSlot.classList.toggle(
"broadcast",
mode === "playing" && playbackIndex === i
);
targetSlots.append(targetSlot);
const attemptSlot = document.createElement("div");
attemptSlot.className = "slot";
if (attempt[i] !== undefined) {
attemptSlot.textContent = PAD_META[attempt[i]].symbol;
attemptSlot.style.setProperty(
"--slot-color",
PAD_META[attempt[i]].css
);
attemptSlot.classList.add("filled");
} else {
attemptSlot.textContent = "·";
}
attemptSlots.append(attemptSlot);
}
targetState.textContent = revealTarget ? "Revealed" : "Concealed";
entryCount.textContent = `${attempt.length} / 3`;
const accepting = mode === "input";
padButtons.forEach((button) => {
button.disabled = !accepting;
});
clearButton.disabled = !accepting || attempt.length === 0;
}
function delay(milliseconds) {
return new Promise((resolve) => {
const timer = window.setTimeout(() => {
playbackTimers.delete(timer);
resolve();
}, milliseconds);
playbackTimers.add(timer);
});
}
function cancelPlayback() {
playbackToken++;
playbackTimers.forEach((timer) => window.clearTimeout(timer));
playbackTimers.clear();
}
// -------------------------------------------------------------------------
// Optional synthesized audio
// -------------------------------------------------------------------------
let audioContext = null;
function ensureAudio() {
const AudioContextClass =
window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) return;
if (!audioContext) {
audioContext = new AudioContextClass();
}
if (audioContext.state === "suspended") {
audioContext.resume().catch(() => {});
}
}
function playTone(padIndex, type = "pad") {
if (!audioContext || audioContext.state !== "running") return;
const frequencies = [329.63, 392, 493.88, 587.33];
const now = audioContext.currentTime;
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = type === "fail" ? "sawtooth" : "triangle";
oscillator.frequency.setValueAtTime(
type === "fail" ? 155 : frequencies[padIndex],
now
);
gain.gain.setValueAtTime(0.0001, now);
gain.gain.exponentialRampToValueAtTime(0.075, now + 0.012);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.23);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start(now);
oscillator.stop(now + 0.25);
}
function playSuccessChord() {
[329.63, 493.88, 659.25].forEach((_, index) => {
window.setTimeout(() => playTone(index, "success"), index * 85);
});
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 9, 19);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional tabletop memory lock"
);
renderer.domElement.tabIndex = 0;
document.getElementById("scene").append(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbfe9ff, 0x17202d, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x347dff, 1.8);
rimLight.position.set(-5, 3.5, -4);
scene.add(rimLight);
const tableMaterial = new THREE.MeshStandardMaterial({
color: 0x5a3828,
roughness: 0.88,
metalness: 0.04,
flatShading: true
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x111923,
roughness: 0.3,
metalness: 0.78,
flatShading: true
});
const edgeMetal = new THREE.MeshStandardMaterial({
color: 0x32475b,
roughness: 0.28,
metalness: 0.82,
flatShading: true
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x090f18,
roughness: 1,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 32),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.05;
floor.receiveShadow = true;
scene.add(floor);
const tableTop = new THREE.Mesh(
new THREE.CylinderGeometry(3.42, 3.62, 0.5, 12),
tableMaterial
);
tableTop.position.y = -0.25;
tableTop.receiveShadow = true;
tableTop.castShadow = true;
scene.add(tableTop);
const tableRim = new THREE.Mesh(
new THREE.TorusGeometry(3.43, 0.055, 6, 48),
edgeMetal
);
tableRim.rotation.x = Math.PI / 2;
tableRim.position.y = -0.05;
scene.add(tableRim);
const legGeometry = new THREE.BoxGeometry(0.36, 1.65, 0.36);
const legPositions = [
[-2.35, -1.32, -2.35],
[2.35, -1.32, -2.35],
[-2.35, -1.32, 2.35],
[2.35, -1.32, 2.35]
];
legPositions.forEach(([x, y, z]) => {
const leg = new THREE.Mesh(legGeometry, darkMetal);
leg.position.set(x, y, z);
leg.rotation.y = Math.PI * 0.25;
leg.castShadow = true;
scene.add(leg);
});
const lock = new THREE.Group();
scene.add(lock);
const lockBase = new THREE.Mesh(
new THREE.CylinderGeometry(1.65, 1.8, 0.28, 12),
darkMetal
);
lockBase.position.y = 0.14;
lockBase.castShadow = true;
lockBase.receiveShadow = true;
lock.add(lockBase);
const baseTrim = new THREE.Mesh(
new THREE.TorusGeometry(1.56, 0.036, 6, 48),
edgeMetal
);
baseTrim.rotation.x = Math.PI / 2;
baseTrim.position.y = 0.29;
lock.add(baseTrim);
const housing = new THREE.Mesh(
new THREE.CylinderGeometry(0.76, 0.9, 0.5, 10),
darkMetal
);
housing.position.y = 0.43;
housing.castShadow = true;
housing.receiveShadow = true;
lock.add(housing);
const housingTrim = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.032, 6, 32),
edgeMetal
);
housingTrim.rotation.x = Math.PI / 2;
housingTrim.position.y = 0.68;
lock.add(housingTrim);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x57d9ff,
emissive: 0x163d50,
emissiveIntensity: 0.9,
roughness: 0.18,
metalness: 0.45,
flatShading: true
});
const coreCrystal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.25, 0),
coreMaterial
);
coreCrystal.position.y = 0.93;
coreCrystal.castShadow = true;
lock.add(coreCrystal);
const coreLight = new THREE.PointLight(0x57d9ff, 5.5, 4.5, 2);
coreLight.position.set(0, 1.05, 0);
lock.add(coreLight);
const boltGeometry = new THREE.CylinderGeometry(0.045, 0.045, 0.035, 6);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0x8393a4,
roughness: 0.25,
metalness: 0.9,
flatShading: true
});
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.position.set(
Math.cos(angle) * 1.48,
0.3,
Math.sin(angle) * 1.48
);
lock.add(bolt);
}
function createTriangleGeometry() {
const shape = new THREE.Shape();
shape.moveTo(0, 0.33);
shape.lineTo(-0.31, -0.25);
shape.lineTo(0.31, -0.25);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.08,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.025,
bevelThickness: 0.025
});
geometry.rotateX(-Math.PI / 2);
geometry.center();
return geometry;
}
function createStarGeometry() {
const shape = new THREE.Shape();
const points = 10;
for (let i = 0; i < points; i++) {
const radius = i % 2 === 0 ? 0.33 : 0.145;
const angle = Math.PI / 2 + (i * Math.PI) / 5;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.08,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.02,
bevelThickness: 0.02
});
geometry.rotateX(-Math.PI / 2);
geometry.center();
return geometry;
}
function createPadGeometry(index) {
if (index === 0) return createTriangleGeometry();
if (index === 1) return new THREE.CylinderGeometry(0.27, 0.27, 0.11, 10);
if (index === 2) return new THREE.BoxGeometry(0.43, 0.11, 0.43);
return createStarGeometry();
}
const padPositions = [
new THREE.Vector3(-0.92, 0, -0.72),
new THREE.Vector3(0.92, 0, -0.72),
new THREE.Vector3(0.92, 0, 0.72),
new THREE.Vector3(-0.92, 0, 0.72)
];
const pads = [];
const pickables = [];
PAD_META.forEach((meta, index) => {
const group = new THREE.Group();
group.position.copy(padPositions[index]);
lock.add(group);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.36, 0.4, 0.1, 8),
edgeMetal
);
base.position.y = 0.33;
base.castShadow = true;
base.receiveShadow = true;
base.userData.padIndex = index;
group.add(base);
const material = new THREE.MeshStandardMaterial({
color: meta.color,
emissive: meta.color,
emissiveIntensity: 0.08,
roughness: 0.27,
metalness: 0.4,
flatShading: true
});
const symbol = new THREE.Mesh(createPadGeometry(index), material);
symbol.position.y = 0.445;
symbol.castShadow = true;
symbol.userData.padIndex = index;
group.add(symbol);
const ringMaterial = new THREE.MeshStandardMaterial({
color: meta.color,
emissive: meta.color,
emissiveIntensity: 0.08,
roughness: 0.3,
metalness: 0.7,
transparent: true,
opacity: 0.8
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.355, 0.024, 6, 20),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.39;
group.add(ring);
pads.push({
group,
material,
ringMaterial,
playbackActive: false,
attemptLit: false
});
pickables.push(base, symbol);
});
const stepLamps = [];
for (let i = 0; i < 3; i++) {
const lampMaterial = new THREE.MeshStandardMaterial({
color: 0x30475b,
emissive: 0x30475b,
emissiveIntensity: 0.08,
roughness: 0.25,
metalness: 0.5,
flatShading: true
});
const lamp = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
lampMaterial
);
lamp.position.set((i - 1) * 0.25, 0.53, 0.68);
lamp.castShadow = true;
lock.add(lamp);
stepLamps.push(lamp);
}
// -------------------------------------------------------------------------
// Custom camera controls — no external controls/addons
// -------------------------------------------------------------------------
const orbit = {
theta: 0.56,
phi: 0.79,
radius: window.innerWidth < 700 ? 8.4 : 7.05
};
const cameraTarget = new THREE.Vector3(0, 0.12, 0);
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerGesture = null;
function pickPad(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
return intersections.length
? intersections[0].object.userData.padIndex
: undefined;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
ensureAudio();
pointerGesture = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerGesture || pointerGesture.id !== event.pointerId) return;
const totalDistance = Math.hypot(
event.clientX - pointerGesture.startX,
event.clientY - pointerGesture.startY
);
if (totalDistance > 5) pointerGesture.moved = true;
if (pointerGesture.moved) {
const deltaX = event.clientX - pointerGesture.lastX;
const deltaY = event.clientY - pointerGesture.lastY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.005,
0.42,
1.28
);
updateCamera();
}
pointerGesture.lastX = event.clientX;
pointerGesture.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerGesture || pointerGesture.id !== event.pointerId) return;
const wasMoved = pointerGesture.moved;
pointerGesture = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasMoved) {
const padIndex = pickPad(event.clientX, event.clientY);
if (padIndex !== undefined) acceptPad(padIndex);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerGesture = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.005,
5.4,
10.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Lock states and exact sequence comparison
// -------------------------------------------------------------------------
const coreTargetColor = new THREE.Color(0x57d9ff);
function setCoreState(state) {
const colors = {
watch: 0x57d9ff,
input: 0xffc857,
success: 0x67f29a,
fail: 0xff647d,
idle: 0x57d9ff
};
coreTargetColor.setHex(colors[state] ?? colors.idle);
}
async function startPlayback(regenerate = false) {
cancelPlayback();
entryToken++;
// Only New Target changes the stored target.
// Replay always uses the existing targetSequence unchanged.
if (regenerate) {
targetSequence.splice(0, targetSequence.length, ...generateTarget());
}
attempt.splice(0, attempt.length);
attemptLitPads.clear();
pads.forEach((pad) => {
pad.playbackActive = false;
pad.attemptLit = false;
});
playbackIndex = -1;
playbackStepCount = 0;
mode = "playing";
setCoreState("watch");
setStatus(
"Watch the lock",
"Three pulses. Memorize their exact order.",
"watch"
);
renderSlots();
const token = playbackToken;
for (let i = 0; i < targetSequence.length; i++) {
if (token !== playbackToken) return;
const padIndex = targetSequence[i];
pads.forEach((pad) => {
pad.playbackActive = false;
});
pads[padIndex].playbackActive = true;
playbackIndex = i;
playbackStepCount = i + 1;
renderSlots();
playTone(padIndex);
await delay(520);
if (token !== playbackToken) return;
pads[padIndex].playbackActive = false;
await delay(190);
if (token !== playbackToken) return;
}
if (token !== playbackToken) return;
playbackIndex = -1;
mode = "input";
setCoreState("input");
setStatus(
"Your turn",
"Enter all three pads in the same order.",
"input"
);
renderSlots();
}
function acceptPad(padIndex) {
if (mode !== "input") return;
attempt.push(padIndex);
attemptLitPads.add(padIndex);
playTone(padIndex);
renderSlots();
if (attempt.length === 3) {
mode = "checking";
renderSlots();
const token = entryToken;
window.setTimeout(() => {
if (token !== entryToken) return;
// Exact ordered comparison: same length and identical item at each index.
const isExactMatch =
attempt.length === targetSequence.length &&
attempt.every((value, index) => value === targetSequence[index]);
if (isExactMatch) {
mode = "success";
setCoreState("success");
setStatus(
"Unlocked",
"Exact match — all three pads were entered in order.",
"success"
);
playSuccessChord();
} else {
mode = "fail";
setCoreState("fail");
setStatus(
"Sequence mismatch",
"Replay this target to try again, or create a new target.",
"fail"
);
playTone(0, "fail");
}
renderSlots();
}, 280);
}
}
function clearEntry() {
if (mode !== "input") return;
attempt.splice(0, attempt.length);
attemptLitPads.clear();
renderSlots();
}
padButtons.forEach((button) => {
button.addEventListener("click", () => {
ensureAudio();
acceptPad(Number(button.dataset.pad));
});
});
replayButton.addEventListener("click", () => {
ensureAudio();
startPlayback(false);
});
newButton.addEventListener("click", () => {
ensureAudio();
startPlayback(true);
});
clearButton.addEventListener("click", () => {
ensureAudio();
clearEntry();
});
document.addEventListener("keydown", (event) => {
if (!["1", "2", "3", "4"].includes(event.key)) return;
const index = Number(event.key) - 1;
ensureAudio();
acceptPad(index);
});
document.addEventListener(
"pointerdown",
ensureAudio,
{ once: true, capture: true }
);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
coreCrystal.rotation.y = elapsed * 0.85;
coreCrystal.rotation.x = Math.sin(elapsed * 0.65) * 0.13;
coreCrystal.position.y = 0.93 + Math.sin(elapsed * 2.1) * 0.035;
coreMaterial.color.lerp(coreTargetColor, 0.075);
coreMaterial.emissive.lerp(coreTargetColor, 0.075);
coreMaterial.emissiveIntensity = THREE.MathUtils.lerp(
coreMaterial.emissiveIntensity,
mode === "success" ? 1.55 : 0.9,
0.08
);
coreLight.color.lerp(coreTargetColor, 0.08);
coreLight.intensity =
4.5 + Math.sin(elapsed * 3.2) * 1.1 + (mode === "success" ? 2 : 0);
pads.forEach((pad, index) => {
const isLit = pad.playbackActive || attemptLitPads.has(index);
const desiredIntensity = isLit ? 1.55 : 0.08;
pad.material.emissiveIntensity = THREE.MathUtils.lerp(
pad.material.emissiveIntensity,
desiredIntensity,
0.16
);
pad.ringMaterial.emissiveIntensity = THREE.MathUtils.lerp(
pad.ringMaterial.emissiveIntensity,
isLit ? 1.1 : 0.07,
0.13
);
pad.ringMaterial.opacity = isLit ? 1 : 0.62;
});
let illuminatedStepCount = 0;
if (mode === "playing") {
illuminatedStepCount = playbackStepCount;
} else if (mode === "input" || mode === "checking") {
illuminatedStepCount = attempt.length;
} else if (mode === "success") {
illuminatedStepCount = 3;
}
stepLamps.forEach((lamp, index) => {
const desiredIntensity = index < illuminatedStepCount ? 1.45 : 0.07;
lamp.material.emissiveIntensity = THREE.MathUtils.lerp(
lamp.material.emissiveIntensity,
desiredIntensity,
0.15
);
});
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// Start with a generated target and visually play it.
targetSequence.push(...generateTarget());
renderSlots();
animate();
startPlayback(false);
</script>
</body>
</html>
```