Prompt specification
Build or customize the Three.js 3D scene "Logic Circuit Escape Platform" 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: Logic Circuit Escape Platform
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/logic-circuit-escape-platform-x3488
Scene Description Prompt:
Create a standalone HTML game with Three.js from cdnjs: a voxel escape platform controlled by switches and AND, OR, and NOT gates.
Working Three.js Starter (logic-circuit-escape-platform-x3488):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#070b14">
<title>Logic Shift — Voxel Escape</title>
<style>
:root {
--bg: #070b14;
--panel: rgba(8, 15, 28, 0.82);
--border: rgba(120, 190, 255, 0.22);
--cyan: #42dcff;
--amber: #ffbd55;
--pink: #ff5cda;
--green: #62ff9c;
--red: #ff6278;
--text: #eaf5ff;
--muted: #8ca5bd;
}
* {
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;
user-select: none;
}
button {
font: inherit;
}
#game {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
#game.dragging {
cursor: grabbing;
}
.scanlines {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
opacity: 0.12;
background:
repeating-linear-gradient(
to bottom,
transparent 0,
transparent 3px,
rgba(255, 255, 255, 0.08) 4px
);
mix-blend-mode: overlay;
}
#hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
opacity: 0;
transition: opacity 300ms ease;
}
body.playing #hud {
opacity: 1;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 12px;
box-shadow:
0 14px 50px rgba(0, 0, 0, 0.35),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
}
#brand {
position: absolute;
top: 20px;
left: 20px;
display: flex;
align-items: center;
gap: 12px;
padding: 11px 15px;
}
.brand-mark {
width: 30px;
height: 30px;
position: relative;
transform: rotate(45deg);
border: 2px solid var(--cyan);
box-shadow: 0 0 18px rgba(66, 220, 255, 0.35);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--cyan);
}
.brand-mark::before {
width: 2px;
height: 38px;
left: 12px;
top: -6px;
}
.brand-mark::after {
width: 38px;
height: 2px;
left: -6px;
top: 12px;
}
.brand-title {
font-weight: 850;
letter-spacing: 0.17em;
font-size: 13px;
}
.brand-sub {
margin-top: 3px;
color: var(--muted);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
#objective {
position: absolute;
top: 88px;
left: 20px;
width: min(360px, calc(100vw - 40px));
padding: 16px 18px;
border-left: 3px solid var(--cyan);
}
.eyebrow {
color: var(--cyan);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.2em;
text-transform: uppercase;
}
#objectiveTitle {
margin-top: 8px;
font-size: 17px;
line-height: 1.25;
font-weight: 750;
}
#objectiveText {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#status {
position: absolute;
top: 20px;
right: 20px;
min-width: 212px;
padding: 14px 16px;
}
.status-row {
display: flex;
justify-content: space-between;
align-items: center;
gap: 20px;
font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.status-row + .status-row {
margin-top: 8px;
}
.status-label {
color: var(--muted);
}
#timer {
color: var(--green);
font-weight: 700;
}
#progress {
display: flex;
gap: 7px;
}
.progress-dot {
flex: 1;
height: 5px;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
transition: 250ms ease;
}
.progress-dot.active {
background: var(--green);
box-shadow: 0 0 12px rgba(98, 255, 156, 0.8);
}
#logicPanel {
position: absolute;
right: 20px;
bottom: 20px;
width: 310px;
padding: 16px;
}
.panel-heading {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.13em;
}
#latchCount {
color: var(--green);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.logic-row {
display: grid;
grid-template-columns: 54px 1fr 70px;
align-items: center;
gap: 10px;
padding: 10px 0;
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.gate-name {
font-weight: 850;
letter-spacing: 0.09em;
}
.gate-name.and {
color: var(--cyan);
}
.gate-name.or {
color: var(--amber);
}
.gate-name.not {
color: var(--pink);
}
.inputs {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font: 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.bit {
display: inline-flex;
min-width: 32px;
justify-content: center;
padding: 4px 5px;
border-radius: 4px;
border: 1px solid rgba(255, 255, 255, 0.12);
color: #617186;
background: rgba(255, 255, 255, 0.03);
}
.bit.on {
color: #07110c;
border-color: var(--green);
background: var(--green);
box-shadow: 0 0 10px rgba(98, 255, 156, 0.3);
}
.output {
text-align: right;
color: var(--red);
font: 800 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.output.on {
color: var(--green);
}
.logic-note {
margin-top: 9px;
color: #617186;
font: 9px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#controls {
position: absolute;
left: 20px;
bottom: 20px;
padding: 11px 14px;
color: var(--muted);
font: 10px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
}
kbd {
display: inline-block;
min-width: 20px;
padding: 2px 5px;
margin: 0 2px;
color: var(--text);
border: 1px solid rgba(255, 255, 255, 0.18);
border-bottom-width: 2px;
border-radius: 4px;
background: rgba(255, 255, 255, 0.06);
text-align: center;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#prompt {
position: absolute;
left: 50%;
bottom: 92px;
transform: translate(-50%, 14px);
display: flex;
align-items: center;
gap: 10px;
padding: 10px 15px;
opacity: 0;
transition: 160ms ease;
white-space: nowrap;
}
#prompt.visible {
opacity: 1;
transform: translate(-50%, 0);
}
.prompt-key {
display: grid;
width: 28px;
height: 28px;
place-items: center;
border-radius: 6px;
color: #061018;
background: var(--cyan);
font-weight: 900;
box-shadow: 0 0 18px rgba(66, 220, 255, 0.35);
}
#promptText {
font-size: 12px;
font-weight: 700;
}
#toast {
position: absolute;
top: 24px;
left: 50%;
transform: translate(-50%, -16px);
padding: 11px 18px;
border-color: rgba(66, 220, 255, 0.4);
opacity: 0;
transition: 180ms ease;
text-align: center;
font-size: 12px;
font-weight: 700;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
.overlay {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 24px;
background:
radial-gradient(circle at 50% 42%, rgba(25, 65, 100, 0.22), transparent 45%),
rgba(3, 7, 14, 0.73);
backdrop-filter: blur(8px);
transition: opacity 350ms ease, visibility 350ms ease;
}
.overlay.hidden {
opacity: 0;
visibility: hidden;
pointer-events: none;
}
.intro-card {
width: min(760px, 100%);
padding: clamp(24px, 5vw, 48px);
border: 1px solid rgba(92, 205, 255, 0.28);
border-radius: 20px;
background:
linear-gradient(145deg, rgba(15, 31, 51, 0.96), rgba(6, 11, 21, 0.96));
box-shadow:
0 35px 100px rgba(0, 0, 0, 0.55),
inset 0 1px rgba(255, 255, 255, 0.06);
}
.intro-kicker {
color: var(--cyan);
font: 800 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.26em;
}
.intro-card h1 {
margin: 13px 0 0;
font-size: clamp(40px, 8vw, 76px);
line-height: 0.95;
letter-spacing: -0.055em;
font-weight: 900;
}
.intro-card h1 span {
color: var(--cyan);
text-shadow: 0 0 30px rgba(66, 220, 255, 0.25);
}
.intro-lead {
max-width: 580px;
margin: 22px 0 0;
color: #a9bed1;
font-size: 15px;
line-height: 1.65;
}
.rule-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
margin: 27px 0;
}
.rule-card {
padding: 15px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.rule-card strong {
display: block;
margin-bottom: 8px;
letter-spacing: 0.12em;
}
.rule-card.and strong {
color: var(--cyan);
}
.rule-card.or strong {
color: var(--amber);
}
.rule-card.not strong {
color: var(--pink);
}
.rule-card span {
color: var(--muted);
font: 10px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.intro-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
}
.primary-button {
border: 0;
border-radius: 9px;
padding: 14px 24px;
color: #051018;
background: var(--cyan);
box-shadow: 0 0 30px rgba(66, 220, 255, 0.25);
font-weight: 900;
letter-spacing: 0.12em;
cursor: pointer;
transition: 160ms ease;
}
.primary-button:hover {
transform: translateY(-2px);
background: #8beaff;
box-shadow: 0 0 40px rgba(66, 220, 255, 0.42);
}
.intro-controls {
color: var(--muted);
font: 10px/1.7 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: right;
}
#winOverlay {
background: rgba(3, 9, 14, 0.74);
}
.win-card {
width: min(540px, 100%);
padding: 42px;
text-align: center;
}
.win-icon {
width: 74px;
height: 74px;
display: grid;
place-items: center;
margin: 0 auto 22px;
border: 2px solid var(--green);
border-radius: 18px;
color: var(--green);
font-size: 34px;
box-shadow:
0 0 35px rgba(98, 255, 156, 0.22),
inset 0 0 25px rgba(98, 255, 156, 0.08);
}
.win-card h2 {
margin: 0;
font-size: 42px;
letter-spacing: -0.04em;
}
.win-card p {
color: var(--muted);
line-height: 1.65;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin: 25px 0;
}
.stat {
padding: 15px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.stat span {
display: block;
color: var(--muted);
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.15em;
}
.stat strong {
display: block;
margin-top: 7px;
color: var(--green);
font: 800 22px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#touchControls {
display: none;
position: fixed;
z-index: 10;
inset: auto 16px 16px;
pointer-events: none;
}
.dpad {
position: absolute;
left: 0;
bottom: 0;
display: grid;
grid-template-columns: repeat(3, 46px);
grid-template-rows: repeat(2, 46px);
gap: 5px;
}
.touch-button {
pointer-events: auto;
border: 1px solid rgba(120, 190, 255, 0.3);
border-radius: 10px;
color: var(--text);
background: rgba(9, 18, 31, 0.78);
backdrop-filter: blur(8px);
font-weight: 800;
touch-action: none;
}
#touchInteract {
position: absolute;
right: 0;
bottom: 15px;
width: 66px;
height: 66px;
border-radius: 50%;
color: #061018;
background: var(--cyan);
}
@media (pointer: coarse) {
body.playing #touchControls {
display: block;
}
#controls {
display: none;
}
#logicPanel {
bottom: 128px;
}
}
@media (max-width: 720px) {
.rule-grid {
grid-template-columns: 1fr;
}
.intro-actions {
align-items: flex-start;
flex-direction: column;
}
.intro-controls {
text-align: left;
}
#logicPanel {
width: 235px;
right: 12px;
bottom: 12px;
padding: 12px;
}
.logic-row {
grid-template-columns: 45px 1fr 55px;
gap: 5px;
}
.logic-note,
#brand .brand-sub {
display: none;
}
#brand {
top: 12px;
left: 12px;
padding: 9px 11px;
}
#objective {
top: 72px;
left: 12px;
width: calc(100vw - 24px);
}
#status {
top: 12px;
right: 12px;
min-width: 145px;
padding: 10px 12px;
}
.status-row {
font-size: 9px;
}
#controls {
display: none;
}
#prompt {
bottom: 155px;
}
}
</style>
</head>
<body>
<canvas id="game" aria-label="Logic Shift 3D game"></canvas>
<div class="scanlines"></div>
<div id="hud">
<div id="brand" class="panel">
<div class="brand-mark"></div>
<div>
<div class="brand-title">LOGIC//SHIFT</div>
<div class="brand-sub">VOXEL ESCAPE PROTOCOL</div>
</div>
</div>
<section id="objective" class="panel">
<div id="zoneTag" class="eyebrow">ZONE 01 // AND GATE</div>
<div id="objectiveTitle">Find the two switches.</div>
<div id="objectiveText">An AND gate opens only when every input is ON.</div>
</section>
<section id="status" class="panel">
<div class="status-row">
<span class="status-label">RUN TIME</span>
<span id="timer">00:00</span>
</div>
<div class="status-row">
<span class="status-label">BARRIERS</span>
<div id="progress">
<div class="progress-dot" id="dot0"></div>
<div class="progress-dot" id="dot1"></div>
<div class="progress-dot" id="dot2"></div>
</div>
</div>
</section>
<section id="logicPanel" class="panel">
<div class="panel-heading">
<strong>LIVE LOGIC</strong>
<span id="latchCount">0 / 3 LATCHED</span>
</div>
<div class="logic-row">
<div class="gate-name and">AND</div>
<div class="inputs">
<span class="bit" id="bitA">A OFF</span>
<span>+</span>
<span class="bit" id="bitB">B OFF</span>
</div>
<div class="output" id="out0">OUTPUT 0</div>
</div>
<div class="logic-row">
<div class="gate-name or">OR</div>
<div class="inputs">
<span class="bit" id="bitC">C OFF</span>
<span>+</span>
<span class="bit" id="bitD">D OFF</span>
</div>
<div class="output" id="out1">OUTPUT 0</div>
</div>
<div class="logic-row">
<div class="gate-name not">NOT</div>
<div class="inputs">
<span>¬</span>
<span class="bit" id="bitE">E ON</span>
</div>
<div class="output" id="out2">OUTPUT 0</div>
</div>
<div class="logic-note">Gate outputs remain live. Once a barrier unlocks, its access signal is latched for safe progression.</div>
</section>
<div id="controls" class="panel">
<kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd> MOVE
<kbd>E</kbd> INTERACT<br>
DRAG VIEW <kbd>SHIFT</kbd> SPRINT <kbd>R</kbd> RESET
</div>
<div id="prompt" class="panel">
<div class="prompt-key">E</div>
<div id="promptText">Toggle switch</div>
</div>
<div id="toast" class="panel"></div>
</div>
<div id="introOverlay" class="overlay">
<div class="intro-card">
<div class="intro-kicker">TACTICAL LOGIC PUZZLE // 01</div>
<h1>LOGIC<span>//</span>SHIFT</h1>
<p class="intro-lead">
Escape a voxel test facility where every barrier obeys a Boolean rule.
Find the switches, read the gates, and route the correct signals to the exit.
</p>
<div class="rule-grid">
<div class="rule-card and">
<strong>AND</strong>
<span>Output is ON only when both inputs are ON.</span>
</div>
<div class="rule-card or">
<strong>OR</strong>
<span>Output is ON when at least one input is ON.</span>
</div>
<div class="rule-card not">
<strong>NOT</strong>
<span>Output is always the opposite of its input.</span>
</div>
</div>
<div class="intro-actions">
<button id="startButton" class="primary-button">ENTER PLATFORM</button>
<div class="intro-controls">
<kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd> MOVE
<kbd>E</kbd> INTERACT<br>
DRAG TO ORBIT SCROLL TO ZOOM <kbd>R</kbd> RESET
</div>
</div>
</div>
</div>
<div id="winOverlay" class="overlay hidden">
<div class="win-card panel">
<div class="win-icon">✓</div>
<div class="eyebrow">PROTOCOL COMPLETE</div>
<h2>ESCAPED.</h2>
<p>All three logic barriers were unlocked and the exit portal accepted your signal.</p>
<div class="stats">
<div class="stat">
<span>RUN TIME</span>
<strong id="finalTime">00:00</strong>
</div>
<div class="stat">
<span>SWITCH FLIPS</span>
<strong id="finalFlips">0</strong>
</div>
</div>
<button id="restartButton" class="primary-button">RUN AGAIN</button>
</div>
</div>
<div id="touchControls">
<div class="dpad">
<button class="touch-button" data-key="KeyW" style="grid-column:2">▲</button>
<button class="touch-button" data-key="KeyA" style="grid-column:1;grid-row:2">◀</button>
<button class="touch-button" data-key="KeyS" style="grid-column:2;grid-row:2">▼</button>
<button class="touch-button" data-key="KeyD" style="grid-column:3;grid-row:2">▶</button>
</div>
<button id="touchInteract" class="touch-button">E</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.getElementById("game");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b14);
scene.fog = new THREE.FogExp2(0x070b14, 0.018);
const camera = new THREE.PerspectiveCamera(
52,
window.innerWidth / window.innerHeight,
0.1,
120
);
const clock = new THREE.Clock();
scene.add(new THREE.HemisphereLight(0x9fcfff, 0x151126, 1.25));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.1);
keyLight.position.set(7, 18, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 35;
keyLight.shadow.camera.bottom = -35;
keyLight.shadow.camera.far = 70;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4d8dff, 1.0);
rimLight.position.set(-10, 8, -20);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.68,
metalness: 0.18,
...options
});
const basic = (color, options = {}) =>
new THREE.MeshBasicMaterial({
color,
...options
});
function box(parent, width, height, depth, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabelTexture(title, subtitle = "", color = "#42dcff", compact = false) {
const c = document.createElement("canvas");
c.width = compact ? 256 : 1024;
c.height = compact ? 128 : 256;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = "rgba(5, 12, 22, 0.92)";
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = color;
ctx.globalAlpha = 0.75;
ctx.lineWidth = compact ? 5 : 8;
ctx.strokeRect(7, 7, c.width - 14, c.height - 14);
ctx.globalAlpha = 1;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.shadowColor = color;
ctx.shadowBlur = compact ? 10 : 18;
if (compact) {
ctx.font = "900 68px ui-monospace, monospace";
ctx.fillText(title, c.width / 2, subtitle ? 48 : 66);
if (subtitle) {
ctx.shadowBlur = 0;
ctx.fillStyle = "#9bb1c8";
ctx.font = "700 18px ui-monospace, monospace";
ctx.fillText(subtitle, c.width / 2, 101);
}
} else {
ctx.font = "900 78px ui-monospace, monospace";
ctx.fillText(title, c.width / 2, subtitle ? 91 : 128);
if (subtitle) {
ctx.shadowBlur = 0;
ctx.fillStyle = "#a6bbce";
ctx.font = "700 29px ui-monospace, monospace";
ctx.fillText(subtitle, c.width / 2, 181);
}
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function labelPlane(
parent,
title,
subtitle,
color,
width,
height,
x,
y,
z
) {
const material = new THREE.MeshBasicMaterial({
map: makeLabelTexture(title, subtitle, color),
transparent: true,
toneMapped: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(width, height), material);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
function labelSprite(parent, title, subtitle, color, x, y, z, scale = 1.5) {
const material = new THREE.SpriteMaterial({
map: makeLabelTexture(title, subtitle, color, true),
transparent: true,
toneMapped: false,
depthTest: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, z);
sprite.scale.set(scale, scale * 0.5, 1);
sprite.renderOrder = 20;
parent.add(sprite);
return sprite;
}
function floorText(parent, text, color, x, z, width = 4.6) {
const mesh = labelPlane(parent, text, "", color, width, 1.15, x, 0.13, z);
mesh.rotation.x = -Math.PI / 2;
return mesh;
}
function wireSegment(parent, ax, az, bx, bz, color) {
const a = new THREE.Vector3(ax, 0.13, az);
const b = new THREE.Vector3(bx, 0.13, bz);
const midpoint = a.clone().add(b).multiplyScalar(0.5);
const direction = b.clone().sub(a);
const length = direction.length();
const material = standard(color, {
emissive: color,
emissiveIntensity: 0.8,
roughness: 0.35
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.035, length),
material
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
parent.add(mesh);
}
// -------------------------------------------------------------------------
// Platform
// -------------------------------------------------------------------------
const platform = new THREE.Group();
scene.add(platform);
box(
platform,
14.2,
0.5,
48.2,
standard(0x101827),
0,
-0.42,
-2.5
);
const tileGeometry = new THREE.BoxGeometry(0.96, 0.16, 0.96);
const tileMaterial = standard(0xffffff, {
vertexColors: true,
roughness: 0.82,
metalness: 0.1
});
const tileCount = 14 * 48;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
tiles.receiveShadow = true;
const matrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let zIndex = 0; zIndex < 48; zIndex++) {
for (let xIndex = 0; xIndex < 14; xIndex++) {
const x = -6.5 + xIndex;
const z = -26 + zIndex;
matrix.makeTranslation(x, -0.08, z);
tiles.setMatrixAt(tileIndex, matrix);
const stripe = (xIndex + zIndex * 3) % 9;
if (stripe === 0) tileColor.set(0x1b2c40);
else if ((xIndex + zIndex) % 2 === 0) tileColor.set(0x142132);
else tileColor.set(0x101c2c);
const variation = ((xIndex * 17 + zIndex * 31) % 7) * 0.004;
tileColor.offsetHSL(0, 0, variation);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
platform.add(tiles);
const railMaterial = standard(0x17273a, {
emissive: 0x0b2636,
emissiveIntensity: 0.5
});
const railGlowMaterial = standard(0x257d9f, {
emissive: 0x42dcff,
emissiveIntensity: 1.2
});
for (const side of [-1, 1]) {
box(
platform,
0.28,
0.7,
47,
railMaterial,
side * 6.82,
0.34,
-2.5
);
box(
platform,
0.055,
0.07,
46.4,
railGlowMaterial,
side * 6.65,
0.72,
-2.5
);
for (let z = -24; z <= 20; z += 4) {
box(
platform,
0.65,
1.25,
0.65,
railMaterial,
side * 6.65,
0.55,
z
);
box(
platform,
0.12,
0.16,
0.12,
railGlowMaterial,
side * 6.31,
1.05,
z
);
}
}
labelPlane(
platform,
"LOGIC//SHIFT",
"AUTHORIZATION REQUIRED",
"#42dcff",
4.8,
1.2,
0,
2.8,
20.8
);
box(platform, 0.5, 2.8, 0.5, railMaterial, -2.65, 1.4, 20.8);
box(platform, 0.5, 2.8, 0.5, railMaterial, 2.65, 1.4, 20.8);
box(platform, 5.8, 0.5, 0.5, railMaterial, 0, 2.8, 20.8);
// Ambient stars.
const starPositions = new Float32Array(420 * 3);
for (let i = 0; i < 420; i++) {
starPositions[i * 3] = (Math.random() - 0.5) * 60;
starPositions[i * 3 + 1] = 5 + Math.random() * 22;
starPositions[i * 3 + 2] = -42 + Math.random() * 70;
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x87caff,
size: 0.08,
transparent: true,
opacity: 0.75
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Logic gates
// -------------------------------------------------------------------------
const gateDefinitions = [
{
name: "AND",
z: 9,
color: 0x42dcff,
css: "#42dcff",
subtitle: "ALL INPUTS MUST BE ON"
},
{
name: "OR",
z: -1,
color: 0xffbd55,
css: "#ffbd55",
subtitle: "ANY INPUT CAN BE ON"
},
{
name: "NOT",
z: -11,
color: 0xff5cda,
css: "#ff5cda",
subtitle: "OUTPUT IS THE INVERSE"
}
];
const gates = [];
function buildGate(definition, index) {
const group = new THREE.Group();
group.position.z = definition.z;
scene.add(group);
const frameMaterial = standard(0x1a293b, {
emissive: definition.color,
emissiveIntensity: 0.09,
roughness: 0.55,
metalness: 0.42
});
const neonMaterial = standard(definition.color, {
emissive: definition.color,
emissiveIntensity: 1.35
});
box(group, 0.7, 4.8, 0.8, frameMaterial, -6.1, 2.4, 0);
box(group, 0.7, 4.8, 0.8, frameMaterial, 6.1, 2.4, 0);
box(group, 12.9, 1.05, 0.8, frameMaterial, 0, 4.25, 0);
box(group, 0.09, 3.8, 0.12, neonMaterial, -5.69, 2.05, 0.43);
box(group, 0.09, 3.8, 0.12, neonMaterial, 5.69, 2.05, 0.43);
labelPlane(
group,
definition.name,
definition.subtitle,
definition.css,
4.5,
1.13,
0,
4.25,
0.43
);
const barrier = new THREE.Group();
group.add(barrier);
const barrierMaterial = standard(definition.color, {
emissive: definition.color,
emissiveIntensity: 0.72,
transparent: true,
opacity: 0.72,
roughness: 0.3,
metalness: 0.3
});
for (let i = 0; i < 13; i++) {
const x = -5.4 + i * 0.9;
box(barrier, 0.69, 3.95, 0.36, barrierMaterial, x, 2, 0);
box(
barrier,
0.42,
0.08,
0.4,
neonMaterial,
x,
3.72,
0
);
}
const statusMaterial = standard(0x602331, {
emissive: 0xff334f,
emissiveIntensity: 0.8
});
const statusLight = box(
group,
0.28,
0.28,
0.28,
statusMaterial,
0,
5.05,
0.25
);
const light = new THREE.PointLight(definition.color, 2.2, 8);
light.position.set(0, 3.1, 1.4);
group.add(light);
floorText(
platform,
`LOGIC 0${index + 1} // ${definition.name}`,
definition.css,
0,
definition.z + 1.45,
4.6
);
for (let stripe = -5; stripe <= 5; stripe++) {
box(
platform,
0.5,
0.045,
0.18,
neonMaterial,
stripe * 1.05,
0.07,
definition.z + 0.65
);
}
const gate = {
...definition,
index,
group,
barrier,
barrierMaterial,
statusLight,
statusMaterial,
light,
output: false,
latched: false,
openAmount: 0
};
gates.push(gate);
return gate;
}
gateDefinitions.forEach(buildGate);
// -------------------------------------------------------------------------
// Switches
// -------------------------------------------------------------------------
const switches = [];
function buildSwitch(id, x, z, color, css, initial = false) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const darkMaterial = standard(0x1a2636, {
roughness: 0.55,
metalness: 0.5
});
const colorMaterial = standard(color, {
emissive: color,
emissiveIntensity: initial ? 1.35 : 0.15
});
box(group, 1.2, 0.16, 1.2, darkMaterial, 0, 0.08, 0);
box(group, 0.78, 0.72, 0.78, darkMaterial, 0, 0.52, 0);
box(group, 0.91, 0.08, 0.91, colorMaterial, 0, 0.91, 0);
const pivot = new THREE.Group();
pivot.position.set(0, 1.0, 0);
pivot.rotation.z = initial ? -0.62 : 0.62;
group.add(pivot);
box(pivot, 0.14, 0.72, 0.14, colorMaterial, 0, 0.32, 0);
box(pivot, 0.36, 0.18, 0.36, colorMaterial, 0, 0.71, 0);
const indicatorMaterial = standard(initial ? color : 0x263244, {
emissive: initial ? color : 0x000000,
emissiveIntensity: initial ? 1.5 : 0.08
});
box(group, 0.24, 0.24, 0.24, indicatorMaterial, 0, 1.02, 0.48);
const ringMaterial = basic(color, {
transparent: true,
opacity: 0.38,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.73, 0.035, 6, 40),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.17;
group.add(ring);
labelSprite(
group,
id,
"SWITCH",
css,
0,
2.05,
0,
1.35
);
const sw = {
id,
x,
z,
color,
css,
group,
pivot,
indicatorMaterial,
ring,
ringMaterial,
on: initial
};
switches.push(sw);
return sw;
}
buildSwitch("A", -3.8, 15, 0x42dcff, "#42dcff");
buildSwitch("B", 3.8, 15, 0x42dcff, "#42dcff");
buildSwitch("C", -3.8, 5, 0xffbd55, "#ffbd55");
buildSwitch("D", 3.8, 5, 0xffbd55, "#ffbd55");
buildSwitch("E", 3.8, -5, 0xff5cda, "#ff5cda", true);
// Floor circuit traces.
wireSegment(platform, -3.8, 14.1, -3.8, 10.2, 0x42dcff);
wireSegment(platform, -3.8, 10.2, -1.4, 10.2, 0x42dcff);
wireSegment(platform, -1.4, 10.2, -1.4, 9.5, 0x42dcff);
wireSegment(platform, 3.8, 14.1, 3.8, 10.2, 0x42dcff);
wireSegment(platform, 3.8, 10.2, 1.4, 10.2, 0x42dcff);
wireSegment(platform, 1.4, 10.2, 1.4, 9.5, 0x42dcff);
wireSegment(platform, -3.8, 4.1, -3.8, 0.2, 0xffbd55);
wireSegment(platform, -3.8, 0.2, -1.4, 0.2, 0xffbd55);
wireSegment(platform, -1.4, 0.2, -1.4, -0.5, 0xffbd55);
wireSegment(platform, 3.8, 4.1, 3.8, 0.2, 0xffbd55);
wireSegment(platform, 3.8, 0.2, 1.4, 0.2, 0xffbd55);
wireSegment(platform, 1.4, 0.2, 1.4, -0.5, 0xffbd55);
wireSegment(platform, 3.8, -5.9, 3.8, -10.2, 0xff5cda);
wireSegment(platform, 3.8, -10.2, 1.4, -10.2, 0xff5cda);
wireSegment(platform, 1.4, -10.2, 1.4, -10.5, 0xff5cda);
// -------------------------------------------------------------------------
// Exit portal
// -------------------------------------------------------------------------
const portal = new THREE.Group();
portal.position.z = -23.3;
scene.add(portal);
const portalFrameMaterial = standard(0x1a2b3d, {
emissive: 0x173a2d,
emissiveIntensity: 0.3,
metalness: 0.5
});
const portalGreenMaterial = standard(0x62ff9c, {
emissive: 0x62ff9c,
emissiveIntensity: 1.5
});
box(portal, 0.75, 4.9, 0.8, portalFrameMaterial, -2.35, 2.45, 0);
box(portal, 0.75, 4.9, 0.8, portalFrameMaterial, 2.35, 2.45, 0);
box(portal, 5.45, 0.85, 0.8, portalFrameMaterial, 0, 4.5, 0);
box(portal, 0.09, 4.1, 0.12, portalGreenMaterial, -1.94, 2.25, 0.43);
box(portal, 0.09, 4.1, 0.12, portalGreenMaterial, 1.94, 2.25, 0.43);
labelPlane(
portal,
"EXIT",
"ALL SIGNALS MUST BE TRUE",
"#62ff9c",
4.25,
1.06,
0,
4.5,
0.43
);
const portalPlaneMaterial = basic(0x173f2c, {
transparent: true,
opacity: 0.36,
side: THREE.DoubleSide,
depthWrite: false
});
const portalPlane = new THREE.Mesh(
new THREE.PlaneGeometry(3.7, 4.0),
portalPlaneMaterial
);
portalPlane.position.set(0, 2.2, 0.05);
portal.add(portalPlane);
const portalRings = [];
for (let i = 0; i < 3; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.25 - i * 0.23, 0.055, 8, 48),
basic(0x62ff9c, {
transparent: true,
opacity: 0.7 - i * 0.12
})
);
ring.position.set(0, 2.15, 0.3 + i * 0.06);
ring.scale.y = 1.42;
portal.add(ring);
portalRings.push(ring);
}
const portalLight = new THREE.PointLight(0x62ff9c, 1.4, 9);
portalLight.position.set(0, 2.5, 1.4);
portal.add(portalLight);
floorText(platform, "MASTER EXIT", "#62ff9c", 0, -21.8, 4.1);
// -------------------------------------------------------------------------
// Player avatar
// -------------------------------------------------------------------------
const player = new THREE.Group();
scene.add(player);
const playerBodyMaterial = standard(0xff8a45, {
emissive: 0x6b1f08,
emissiveIntensity: 0.25
});
const playerDarkMaterial = standard(0x172235, {
metalness: 0.45,
roughness: 0.5
});
const playerLightMaterial = standard(0xe9f6ff, {
emissive: 0x6acfff,
emissiveIntensity: 0.65
});
box(player, 0.72, 0.9, 0.46, playerBodyMaterial, 0, 1.12, 0);
box(player, 0.64, 0.62, 0.62, playerDarkMaterial, 0, 1.9, 0);
box(player, 0.49, 0.18, 0.08, playerLightMaterial, 0, 1.93, 0.34);
box(player, 0.48, 0.62, 0.23, playerDarkMaterial, 0, 1.13, -0.34);
const leftArm = box(player, 0.22, 0.74, 0.24, playerBodyMaterial, -0.49, 1.18, 0);
const rightArm = box(player, 0.22, 0.74, 0.24, playerBodyMaterial, 0.49, 1.18, 0);
const leftLeg = box(player, 0.27, 0.66, 0.3, playerDarkMaterial, -0.2, 0.36, 0);
const rightLeg = box(player, 0.27, 0.66, 0.3, playerDarkMaterial, 0.2, 0.36, 0);
const playerShadow = new THREE.Mesh(
new THREE.CircleGeometry(0.62, 24),
basic(0x000000, {
transparent: true,
opacity: 0.3,
depthWrite: false
})
);
playerShadow.rotation.x = -Math.PI / 2;
playerShadow.position.y = 0.015;
player.add(playerShadow);
player.position.set(0, 0, 18.2);
player.rotation.y = Math.PI;
// -------------------------------------------------------------------------
// State and UI
// -------------------------------------------------------------------------
let started = false;
let won = false;
let elapsed = 0;
let flips = 0;
let nearestSwitch = null;
let toastTimeout = 0;
const keys = new Set();
const ui = {
zoneTag: document.getElementById("zoneTag"),
objectiveTitle: document.getElementById("objectiveTitle"),
objectiveText: document.getElementById("objectiveText"),
timer: document.getElementById("timer"),
latchCount: document.getElementById("latchCount"),
prompt: document.getElementById("prompt"),
promptText: document.getElementById("promptText"),
toast: document.getElementById("toast"),
dots: [
document.getElementById("dot0"),
document.getElementById("dot1"),
document.getElementById("dot2")
],
bits: {
A: document.getElementById("bitA"),
B: document.getElementById("bitB"),
C: document.getElementById("bitC"),
D: document.getElementById("bitD"),
E: document.getElementById("bitE")
},
outputs: [
document.getElementById("out0"),
document.getElementById("out1"),
document.getElementById("out2")
]
};
function formatTime(seconds) {
const mins = Math.floor(seconds / 60).toString().padStart(2, "0");
const secs = Math.floor(seconds % 60).toString().padStart(2, "0");
return `${mins}:${secs}`;
}
function showToast(message, color = "#42dcff") {
ui.toast.textContent = message;
ui.toast.style.borderColor = color;
ui.toast.style.color = color;
ui.toast.classList.add("visible");
clearTimeout(toastTimeout);
toastTimeout = setTimeout(() => {
ui.toast.classList.remove("visible");
}, 2300);
}
function evaluateGate(gate) {
if (gate.name === "AND") return switches[0].on && switches[1].on;
if (gate.name === "OR") return switches[2].on || switches[3].on;
return !switches[4].on;
}
function updateLogicUI() {
for (const sw of switches) {
const bit = ui.bits[sw.id];
bit.textContent = `${sw.id} ${sw.on ? "ON" : "OFF"}`;
bit.classList.toggle("on", sw.on);
}
gates.forEach((gate, index) => {
ui.outputs[index].textContent = gate.output
? "OUTPUT 1"
: "OUTPUT 0";
ui.outputs[index].classList.toggle("on", gate.output);
ui.dots[index].classList.toggle("active", gate.latched);
});
const latchCount = gates.filter(gate => gate.latched).length;
ui.latchCount.textContent = `${latchCount} / 3 LATCHED`;
if (!gates[0].latched) {
ui.zoneTag.textContent = "ZONE 01 // AND GATE";
ui.objectiveTitle.textContent = "Find switches A and B.";
ui.objectiveText.textContent =
"An AND gate opens only when every input is ON. Turn on both cyan switches.";
} else if (!gates[1].latched) {
ui.zoneTag.textContent = "ZONE 02 // OR GATE";
ui.objectiveTitle.textContent = "Satisfy either input.";
ui.objectiveText.textContent =
"An OR gate accepts either signal. Turn on C or D to unlock the next barrier.";
} else if (!gates[2].latched) {
ui.zoneTag.textContent = "ZONE 03 // NOT GATE";
ui.objectiveTitle.textContent = "Invert the input.";
ui.objectiveText.textContent =
switches[4].on
? "Switch E is ON, so the NOT output is OFF. Toggle E to OFF."
: "Switch E is OFF, so the NOT output is ON. The next barrier should unlock.";
} else {
ui.zoneTag.textContent = "ZONE 04 // MASTER EXIT";
ui.objectiveTitle.textContent = "The exit is online.";
ui.objectiveText.textContent =
"All three barriers are latched. Reach the green portal at the far end of the platform.";
}
}
// -------------------------------------------------------------------------
// Camera controls — implemented directly, without addons
// -------------------------------------------------------------------------
let cameraYaw = 0;
let cameraPitch = 0.57;
let cameraRadius = 9.2;
let dragging = false;
let dragPointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", event => {
if (!started || won) return;
dragging = true;
dragPointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", event => {
if (!dragging || event.pointerId !== dragPointerId) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.004,
0.25,
1.08
);
});
function endDrag(event) {
if (dragPointerId !== null && event.pointerId !== dragPointerId) return;
dragging = false;
dragPointerId = null;
canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
4.6,
14
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Input
// -------------------------------------------------------------------------
window.addEventListener("keydown", event => {
if (
["KeyW", "KeyA", "KeyS", "KeyD", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]
.includes(event.code)
) {
event.preventDefault();
}
keys.add(event.code);
if (event.repeat) return;
if (event.code === "KeyE") interact();
if (event.code === "KeyR" && started) resetGame();
});
window.addEventListener("keyup", event => {
keys.delete(event.code);
});
window.addEventListener("blur", () => {
keys.clear();
dragging = false;
canvas.classList.remove("dragging");
});
document.querySelectorAll("[data-key]").forEach(button => {
const key = button.dataset.key;
button.addEventListener("pointerdown", event => {
event.preventDefault();
keys.add(key);
button.setPointerCapture(event.pointerId);
});
const release = () => keys.delete(key);
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("pointerleave", release);
});
document.getElementById("touchInteract").addEventListener("pointerdown", event => {
event.preventDefault();
interact();
});
function interact() {
if (!started || won || !nearestSwitch) return;
const sw = nearestSwitch;
sw.on = !sw.on;
flips++;
const state = sw.on ? "ON" : "OFF";
showToast(
`SWITCH ${sw.id} → ${state}`,
sw.css
);
updateLogicUI();
}
// -------------------------------------------------------------------------
// Collision and movement
// -------------------------------------------------------------------------
function positionBlocked(x, z) {
if (x < -6.25 || x > 6.25 || z < -25.1 || z > 20.4) return true;
for (const gate of gates) {
if (
gate.openAmount < 0.79 &&
Math.abs(z - gate.z) < 0.62 &&
Math.abs(x) < 6.25
) {
return true;
}
}
for (const sw of switches) {
if (Math.abs(x - sw.x) < 0.57 && Math.abs(z - sw.z) < 0.57) {
return true;
}
}
return false;
}
function updatePlayer(dt) {
let strafe = 0;
let forward = 0;
if (keys.has("KeyD") || keys.has("ArrowRight")) strafe += 1;
if (keys.has("KeyA") || keys.has("ArrowLeft")) strafe -= 1;
if (keys.has("KeyW") || keys.has("ArrowUp")) forward += 1;
if (keys.has("KeyS") || keys.has("ArrowDown")) forward -= 1;
const length = Math.hypot(strafe, forward);
if (length > 0) {
strafe /= length;
forward /= length;
}
const sprint = keys.has("ShiftLeft") || keys.has("ShiftRight");
const speed = sprint ? 7.4 : 4.8;
const rightX = Math.cos(cameraYaw);
const rightZ = -Math.sin(cameraYaw);
const forwardX = -Math.sin(cameraYaw);
const forwardZ = -Math.cos(cameraYaw);
const vx = rightX * strafe + forwardX * forward;
const vz = rightZ * strafe + forwardZ * forward;
const nextX = player.position.x + vx * speed * dt;
if (!positionBlocked(nextX, player.position.z)) {
player.position.x = nextX;
}
const nextZ = player.position.z + vz * speed * dt;
if (!positionBlocked(player.position.x, nextZ)) {
player.position.z = nextZ;
}
if (Math.hypot(vx, vz) > 0.01) {
const targetAngle = Math.atan2(vx, vz);
let difference = targetAngle - player.rotation.y;
difference = Math.atan2(Math.sin(difference), Math.cos(difference));
player.rotation.y += difference * Math.min(1, dt * 12);
const stride = Math.sin(elapsed * 12) * 0.48;
leftLeg.rotation.x = stride;
rightLeg.rotation.x = -stride;
leftArm.rotation.x = -stride * 0.75;
rightArm.rotation.x = stride * 0.75;
player.position.y = Math.abs(Math.sin(elapsed * 12)) * 0.035;
} else {
leftLeg.rotation.x *= 0.82;
rightLeg.rotation.x *= 0.82;
leftArm.rotation.x *= 0.82;
rightArm.rotation.x *= 0.82;
player.position.y *= 0.82;
}
nearestSwitch = null;
let nearestDistance = 2.25;
for (const sw of switches) {
const distance = Math.hypot(
player.position.x - sw.x,
player.position.z - sw.z
);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestSwitch = sw;
}
}
if (nearestSwitch) {
ui.prompt.classList.add("visible");
ui.promptText.textContent =
`Toggle switch ${nearestSwitch.id} · currently ${nearestSwitch.on ? "ON" : "OFF"}`;
} else {
ui.prompt.classList.remove("visible");
}
}
// -------------------------------------------------------------------------
// Switching, gates, portal
// -------------------------------------------------------------------------
function updateSwitches(dt, time) {
for (const sw of switches) {
const targetRotation = sw.on ? -0.62 : 0.62;
sw.pivot.rotation.z +=
(targetRotation - sw.pivot.rotation.z) * Math.min(1, dt * 12);
sw.indicatorMaterial.color.setHex(sw.on ? sw.color : 0x263244);
sw.indicatorMaterial.emissive.setHex(sw.on ? sw.color : 0x000000);
sw.indicatorMaterial.emissiveIntensity = sw.on ? 1.5 : 0.08;
const isNearest = nearestSwitch === sw;
const pulse = 1 + Math.sin(time * 4 + sw.x) * 0.07;
const targetScale = isNearest ? pulse * 1.15 : pulse;
sw.ring.scale.lerp(
new THREE.Vector3(targetScale, targetScale, targetScale),
Math.min(1, dt * 10)
);
sw.ringMaterial.opacity = isNearest ? 0.85 : 0.3;
}
}
function updateGates(dt) {
gates.forEach((gate, index) => {
gate.output = evaluateGate(gate);
if (gate.output && !gate.latched) {
gate.latched = true;
showToast(
`${gate.name} GATE SATISFIED — BARRIER LATCHED OPEN`,
gate.css
);
updateLogicUI();
}
const target = gate.latched ? 4.75 : 0;
gate.openAmount +=
(target - gate.openAmount) * Math.min(1, dt * 3.9);
gate.barrier.position.y = gate.openAmount;
const color = gate.latched ? 0x62ff9c : 0xff334f;
gate.statusMaterial.color.setHex(color);
gate.statusMaterial.emissive.setHex(color);
gate.statusLight.scale.setScalar(
gate.latched ? 1 + Math.sin(elapsed * 6 + index) * 0.15 : 1
);
});
}
function updatePortal(dt, time) {
const open = gates.every(gate => gate.latched);
const targetColor = open ? 0x62ff9c : 0x355344;
portalPlaneMaterial.color.setHex(targetColor);
portalPlaneMaterial.opacity +=
((open ? 0.58 : 0.18) - portalPlaneMaterial.opacity) *
Math.min(1, dt * 3);
portalLight.intensity +=
((open ? 4.2 : 0.8) - portalLight.intensity) * Math.min(1, dt * 3);
portalRings.forEach((ring, index) => {
ring.rotation.z += dt * (open ? 1.2 + index * 0.45 : 0.16);
ring.material.opacity = open ? 0.85 - index * 0.16 : 0.2;
});
if (open && player.position.z < -22.25 && !won) {
triggerWin();
}
}
// -------------------------------------------------------------------------
// Camera and effects
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3();
const desiredCameraPosition = new THREE.Vector3();
function updateCamera(dt) {
cameraTarget.set(
player.position.x,
player.position.y + 1.35,
player.position.z
);
const horizontalRadius = Math.cos(cameraPitch) * cameraRadius;
desiredCameraPosition.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.position.lerp(desiredCameraPosition, Math.min(1, dt * 8));
camera.lookAt(cameraTarget);
}
const ambientCubes = [];
const ambientCubeGeometry = new THREE.BoxGeometry(0.16, 0.16, 0.16);
for (let i = 0; i < 26; i++) {
const material = standard(
i % 3 === 0 ? 0x42dcff : i % 3 === 1 ? 0x345272 : 0xff5cda,
{
emissive: i % 3 === 0 ? 0x42dcff : 0x24113c,
emissiveIntensity: 0.7,
transparent: true,
opacity: 0.35
}
);
const cube = new THREE.Mesh(ambientCubeGeometry, material);
cube.position.set(
(Math.random() - 0.5) * 18,
2 + Math.random() * 9,
-30 + Math.random() * 58
);
cube.userData.speed = 0.12 + Math.random() * 0.28;
cube.userData.offset = Math.random() * Math.PI * 2;
scene.add(cube);
ambientCubes.push(cube);
}
function updateAmbient(dt, time) {
for (const cube of ambientCubes) {
cube.rotation.x += dt * 0.3;
cube.rotation.y += dt * 0.45;
cube.position.y += dt * cube.userData.speed;
cube.position.x += Math.sin(time * 0.4 + cube.userData.offset) * dt * 0.08;
if (cube.position.y > 12) cube.position.y = 1.5;
}
}
const confetti = new THREE.Group();
scene.add(confetti);
const confettiPieces = [];
function triggerWin() {
won = true;
keys.clear();
document.getElementById("finalTime").textContent = formatTime(elapsed);
document.getElementById("finalFlips").textContent = flips.toString();
document.getElementById("winOverlay").classList.remove("hidden");
document.body.classList.add("won");
const colors = [0x42dcff, 0xffbd55, 0xff5cda, 0x62ff9c];
for (let i = 0; i < 55; i++) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.12, 0.12),
standard(colors[i % colors.length], {
emissive: colors[i % colors.length],
emissiveIntensity: 0.8
})
);
mesh.position.set(
(Math.random() - 0.5) * 2.5,
2.3 + Math.random() * 2,
-23.3 + (Math.random() - 0.5) * 1.5
);
confetti.add(mesh);
confettiPieces.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 4,
2 + Math.random() * 4,
(Math.random() - 0.5) * 4
),
spin: new THREE.Vector3(
Math.random() * 5,
Math.random() * 5,
Math.random() * 5
)
});
}
}
function updateConfetti(dt) {
for (const piece of confettiPieces) {
piece.velocity.y -= 5.5 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.mesh.position.y < 0.2) {
piece.mesh.position.y = 0.2;
piece.velocity.y = Math.abs(piece.velocity.y) * 0.35;
piece.velocity.x *= 0.92;
piece.velocity.z *= 0.92;
}
}
}
// -------------------------------------------------------------------------
// Start and reset
// -------------------------------------------------------------------------
function resetGame() {
switches[0].on = false;
switches[1].on = false;
switches[2].on = false;
switches[3].on = false;
switches[4].on = true;
gates.forEach(gate => {
gate.output = false;
gate.latched = false;
gate.openAmount = 0;
gate.barrier.position.y = 0;
});
elapsed = 0;
flips = 0;
won = false;
nearestSwitch = null;
player.position.set(0, 0, 18.2);
player.rotation.y = Math.PI;
cameraYaw = 0;
cameraPitch = 0.57;
cameraRadius = 9.2;
confetti.clear();
confettiPieces.length = 0;
document.getElementById("winOverlay").classList.add("hidden");
document.body.classList.remove("won");
updateLogicUI();
showToast("PLATFORM RESET", "#8ca5bd");
}
function startGame() {
resetGame();
started = true;
document.body.classList.add("playing");
document.getElementById("introOverlay").classList.add("hidden");
showToast("FIND SWITCHES A AND B", "#42dcff");
canvas.focus();
}
document.getElementById("startButton").addEventListener("click", startGame);
document.getElementById("restartButton").addEventListener("click", resetGame);
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
if (started && !won) {
elapsed += dt;
updatePlayer(dt);
} else {
ui.prompt.classList.remove("visible");
}
updateSwitches(dt, time);
updateGates(dt);
updatePortal(dt, time);
updateCamera(dt);
updateAmbient(dt, time);
updateConfetti(dt);
ui.timer.textContent = formatTime(elapsed);
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));
});
updateLogicUI();
animate();
</script>
</body>
</html>
```