Prompt specification
Build or customize the Three.js 3D scene "Plate Button and Linked Gate" 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: Plate Button and Linked Gate
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/plate-button-and-linked-gate-x3485
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows pressure plates and Boolean gate conditions. Model a floor plate connected visibly to a nearby gate indicator. A weight depresses the plate, and removing it restores the original gate state. Voxel style.
Working Three.js Starter (plate-button-and-linked-gate-x3485):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Boolean Gate</title>
<style>
:root {
color-scheme: dark;
--bg: #0a1019;
--panel: rgba(13, 23, 34, 0.9);
--line: rgba(143, 207, 231, 0.2);
--text: #e8f7ff;
--muted: #8ca6b6;
--cyan: #58dff5;
--green: #66f5a2;
--red: #ff6d72;
--orange: #ff9b63;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
body {
user-select: none;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
outline: none;
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.5);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
background: var(--cyan);
box-shadow:
13px 0 #28798c,
26px 0 #173947;
}
h1 {
margin: 10px 0 5px;
font-size: clamp(24px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 800;
}
.brand p {
margin: 0;
max-width: 390px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--line);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
}
.logic-panel {
top: 24px;
right: 24px;
width: 286px;
padding: 17px;
border-radius: 16px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
color: var(--muted);
text-transform: uppercase;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--orange);
box-shadow: 0 0 16px currentColor;
color: var(--orange);
}
.input-row {
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 9px;
min-height: 39px;
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.055);
background: rgba(255, 255, 255, 0.025);
}
.input-row + .input-row {
margin-top: 7px;
}
.input-key {
display: grid;
place-items: center;
width: 25px;
height: 25px;
color: var(--cyan);
background: rgba(88, 223, 245, 0.09);
border: 1px solid rgba(88, 223, 245, 0.2);
font: 800 12px/1 ui-monospace, monospace;
}
.input-name {
font-size: 12px;
font-weight: 700;
}
.input-name small {
display: block;
margin-top: 2px;
color: var(--muted);
font: 500 10px/1.2 ui-monospace, monospace;
}
.bit {
min-width: 34px;
padding: 5px 7px;
border-radius: 6px;
text-align: center;
color: #6f8290;
background: #111c27;
border: 1px solid rgba(255, 255, 255, 0.06);
font: 800 12px/1 ui-monospace, monospace;
}
.bit.on {
color: #07140d;
background: var(--green);
border-color: var(--green);
box-shadow: 0 0 18px rgba(102, 245, 162, 0.22);
}
.logic-label {
margin: 15px 0 7px;
color: var(--muted);
font: 700 10px/1 ui-monospace, monospace;
letter-spacing: 0.12em;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
border: 1px solid var(--line);
color: var(--text);
background: #152330;
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease,
color 120ms ease;
}
button:hover {
border-color: rgba(88, 223, 245, 0.55);
background: #1b3040;
}
button:active {
transform: translateY(1px);
}
.segmented button {
padding: 10px;
border-radius: 8px;
}
.segmented button.active {
color: #061116;
background: var(--cyan);
border-color: var(--cyan);
box-shadow: 0 0 18px rgba(88, 223, 245, 0.16);
}
.output-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 14px;
padding: 11px 12px;
border: 1px solid rgba(255, 109, 114, 0.2);
background: rgba(255, 109, 114, 0.06);
}
.output-row span {
color: var(--muted);
font: 700 10px/1 ui-monospace, monospace;
letter-spacing: 0.12em;
}
.output-row strong {
color: var(--red);
font: 900 14px/1 ui-monospace, monospace;
}
.output-row.true {
border-color: rgba(102, 245, 162, 0.28);
background: rgba(102, 245, 162, 0.07);
}
.output-row.true strong {
color: var(--green);
}
.actions {
right: 24px;
bottom: 24px;
display: flex;
gap: 8px;
padding: 10px;
border-radius: 14px;
}
.actions button {
min-height: 38px;
padding: 0 13px;
border-radius: 9px;
}
.actions .primary {
color: #081116;
background: var(--orange);
border-color: var(--orange);
}
.actions .primary:hover {
background: #ffb17e;
border-color: #ffb17e;
}
.help {
position: fixed;
left: 26px;
bottom: 24px;
z-index: 5;
max-width: 440px;
padding: 10px 13px;
border-left: 2px solid var(--cyan);
color: var(--muted);
background: rgba(9, 16, 25, 0.66);
font: 600 11px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
.help strong {
color: var(--text);
}
.toast {
position: fixed;
left: 50%;
bottom: 28px;
z-index: 20;
max-width: min(560px, calc(100vw - 40px));
padding: 11px 16px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--text);
background: rgba(9, 18, 27, 0.93);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
font: 700 11px/1.4 ui-monospace, monospace;
text-align: center;
pointer-events: none;
opacity: 0;
transform: translate(-50%, 12px);
transition: 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
}
.brand p {
display: none;
}
.logic-panel {
top: auto;
right: 12px;
bottom: 78px;
width: 250px;
padding: 12px;
}
.actions {
right: 12px;
bottom: 12px;
}
.actions button {
padding: 0 9px;
font-size: 9px;
}
.help {
left: 12px;
bottom: 12px;
max-width: calc(100vw - 275px);
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="brand">
<div class="eyebrow">Interactive voxel logic</div>
<h1>Pressure Logic</h1>
<p>
Load the weight onto plate A, toggle switch B, and change the Boolean
operation to control the nearby gate indicator.
</p>
</header>
<aside class="panel logic-panel">
<div class="panel-title">
Boolean gate
<span class="live-dot" id="liveDot"></span>
</div>
<div class="input-row">
<span class="input-key">A</span>
<span class="input-name">Pressure plate<small>automatic input</small></span>
<span class="bit" id="plateBit">0</span>
</div>
<div class="input-row">
<span class="input-key">B</span>
<span class="input-name">Auxiliary switch<small>manual input</small></span>
<span class="bit on" id="switchBit">1</span>
</div>
<div class="logic-label">LOGIC OPERATION</div>
<div class="segmented">
<button class="active" data-mode="AND" aria-pressed="true">AND</button>
<button data-mode="OR" aria-pressed="false">OR</button>
</div>
<div class="output-row" id="outputRow" aria-live="polite">
<span>GATE OUTPUT</span>
<strong id="outputValue">FALSE</strong>
</div>
</aside>
<div class="panel actions">
<button class="primary" id="weightButton">LOAD WEIGHT</button>
<button id="switchButton">SWITCH B: ON</button>
<button id="viewButton">RESET VIEW</button>
</div>
<div class="help">
<strong>Click the red weight</strong> to load or unload it ·
<strong>Click the green lever</strong> to toggle B ·
Drag to orbit · Scroll to zoom · W / S / R shortcuts
</div>
<div class="toast" id="toast"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
// ---------------------------------------------------------------------
// Renderer, scene, and orthographic camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a1019);
scene.fog = new THREE.Fog(0x0a1019, 22, 42);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraControls = {
yaw: 0.69,
pitch: 0.72,
distance: 18,
viewSize: 11.2,
target: new THREE.Vector3(0, 0.65, 0)
};
function updateCamera() {
const c = cameraControls;
const horizontal = Math.cos(c.pitch) * c.distance;
camera.position.set(
c.target.x + Math.sin(c.yaw) * horizontal,
c.target.y + Math.sin(c.pitch) * c.distance,
c.target.z + Math.cos(c.yaw) * horizontal
);
camera.lookAt(c.target);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const half = cameraControls.viewSize / 2;
camera.left = -half * aspect;
camera.right = half * aspect;
camera.top = half;
camera.bottom = -half;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
updateCamera();
resize();
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc5e9ff, 0x172333, 2.1));
const sun = new THREE.DirectionalLight(0xfff1dc, 3.2);
sun.position.set(7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rimLight = new THREE.PointLight(0x36c9ff, 14, 18, 2);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Shared materials and voxel helpers
// ---------------------------------------------------------------------
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x152331,
roughness: 0.82,
metalness: 0.12
}),
dark: new THREE.MeshStandardMaterial({
color: 0x101923,
roughness: 0.78,
metalness: 0.18
}),
metal: new THREE.MeshStandardMaterial({
color: 0x33495a,
roughness: 0.52,
metalness: 0.38
}),
trim: new THREE.MeshStandardMaterial({
color: 0x2d7184,
roughness: 0.45,
metalness: 0.28
}),
orange: new THREE.MeshStandardMaterial({
color: 0xffad59,
roughness: 0.48,
metalness: 0.12
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x26323b,
roughness: 0.92,
metalness: 0.02
})
};
function addBox(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(text, accent = "#58dff5") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#08121b";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = accent;
ctx.lineWidth = 7;
ctx.strokeRect(5, 5, canvas.width - 10, canvas.height - 10);
ctx.fillStyle = accent;
ctx.font = "900 47px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
return texture;
}
function makeLabel(text, width, height, accent = "#58dff5") {
const texture = makeLabelTexture(text, accent);
const material = new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
});
return new THREE.Mesh(new THREE.PlaneGeometry(width, height), material);
}
// ---------------------------------------------------------------------
// Voxel floor
// ---------------------------------------------------------------------
addBox(scene, 13.5, 0.5, 13.5, materials.base, 0, -0.1, 0);
const tileGeometry = new THREE.BoxGeometry(1.08, 0.18, 1.08);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0.04
});
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, 121);
tiles.receiveShadow = true;
const matrix = new THREE.Matrix4();
const tileColors = [0x263a48, 0x2a4050, 0x233745, 0x304858];
let tileIndex = 0;
for (let row = 0; row < 11; row++) {
for (let column = 0; column < 11; column++) {
const x = (column - 5) * 1.15;
const z = (row - 5) * 1.15;
matrix.makeTranslation(x, 0.09, z);
tiles.setMatrixAt(tileIndex, matrix);
const colorIndex =
(column * 7 + row * 3 + ((column + row) % 2)) % tileColors.length;
tiles.setColorAt(tileIndex, new THREE.Color(tileColors[colorIndex]));
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: 0x0a1019,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.36;
ground.receiveShadow = true;
scene.add(ground);
// Small illuminated corner markers.
for (const x of [-6.15, 6.15]) {
for (const z of [-6.15, 6.15]) {
addBox(scene, 0.18, 0.65, 0.18, materials.metal, x, 0.31, z);
addBox(
scene,
0.24,
0.12,
0.24,
new THREE.MeshStandardMaterial({
color: 0x58dff5,
emissive: 0x58dff5,
emissiveIntensity: 1.1
}),
x,
0.69,
z
);
}
}
// ---------------------------------------------------------------------
// Pressure plate
// ---------------------------------------------------------------------
const plateRoot = new THREE.Group();
plateRoot.position.set(-1.8, 0, 0.2);
plateRoot.userData.action = "plate";
scene.add(plateRoot);
addBox(plateRoot, 2.5, 0.18, 2.5, materials.dark, 0, 0.24, 0);
const platePad = addBox(
plateRoot,
1.72,
0.14,
1.72,
materials.metal,
0,
0.42,
0
);
addBox(plateRoot, 2.5, 0.22, 0.28, materials.trim, 0, 0.36, 1.11);
addBox(plateRoot, 2.5, 0.22, 0.28, materials.trim, 0, 0.36, -1.11);
addBox(plateRoot, 0.28, 0.22, 1.94, materials.trim, 1.11, 0.36, 0);
addBox(plateRoot, 0.28, 0.22, 1.94, materials.trim, -1.11, 0.36, 0);
const plateSignalMaterial = new THREE.MeshStandardMaterial({
color: 0x183c4d,
emissive: 0x58dff5,
emissiveIntensity: 0.12,
roughness: 0.35
});
for (const x of [-0.94, 0.94]) {
for (const z of [-0.94, 0.94]) {
addBox(
plateRoot,
0.17,
0.11,
0.17,
plateSignalMaterial,
x,
0.5,
z
);
}
}
const plateLabel = makeLabel("PLATE A", 1.65, 0.41);
plateLabel.position.set(0, 0.76, -1.3);
plateRoot.add(plateLabel);
addBox(plateRoot, 0.1, 0.45, 0.1, materials.metal, -0.58, 0.43, -1.29);
addBox(plateRoot, 0.1, 0.45, 0.1, materials.metal, 0.58, 0.43, -1.29);
// Parking outline for the weight.
const parkingX = -1.8;
const parkingZ = 2.55;
for (const dx of [-0.7, 0.7]) {
for (const dz of [-0.7, 0.7]) {
addBox(
scene,
0.34,
0.045,
0.12,
materials.orange,
parkingX + dx,
0.205,
parkingZ + dz
);
addBox(
scene,
0.12,
0.045,
0.34,
materials.orange,
parkingX + dx,
0.205,
parkingZ + dz
);
}
}
const weightLabel = makeLabel("WEIGHT", 1.45, 0.34, "#ffad59");
weightLabel.position.set(parkingX, 0.44, 3.48);
scene.add(weightLabel);
// ---------------------------------------------------------------------
// Weight
// ---------------------------------------------------------------------
const weightRoot = new THREE.Group();
weightRoot.userData.action = "weight";
scene.add(weightRoot);
const weightBodyMaterial = new THREE.MeshStandardMaterial({
color: 0xe96d4f,
emissive: 0x7a1f10,
emissiveIntensity: 0.08,
roughness: 0.56,
metalness: 0.08
});
addBox(weightRoot, 1.25, 1.25, 1.25, weightBodyMaterial);
addBox(
weightRoot,
1.3,
0.18,
0.065,
materials.rubber,
0,
0,
0.632
);
addBox(
weightRoot,
1.3,
0.18,
0.065,
materials.rubber,
0,
0,
-0.632
);
addBox(
weightRoot,
0.065,
0.18,
1.3,
materials.rubber,
0.632,
0,
0
);
addBox(
weightRoot,
0.065,
0.18,
1.3,
materials.rubber,
-0.632,
0,
0
);
addBox(weightRoot, 0.18, 0.2, 0.18, materials.dark, -0.28, 0.72, -0.28);
addBox(weightRoot, 0.18, 0.2, 0.18, materials.dark, 0.28, 0.72, -0.28);
addBox(weightRoot, 0.64, 0.16, 0.18, materials.dark, 0, 0.83, -0.28);
const weightNumber = makeLabel("10", 0.48, 0.25, "#ffd0b7");
weightNumber.position.set(0, 0.08, 0.638);
weightRoot.add(weightNumber);
weightRoot.position.set(parkingX, 0.805, parkingZ);
// ---------------------------------------------------------------------
// Boolean gate and indicator
// ---------------------------------------------------------------------
const gateRoot = new THREE.Group();
gateRoot.position.set(3.45, 0, -1.3);
scene.add(gateRoot);
addBox(gateRoot, 2, 0.54, 1.4, materials.dark, 0, 0.28, 0);
addBox(gateRoot, 1.7, 0.12, 1.15, materials.metal, 0, 0.59, 0);
addBox(gateRoot, 0.62, 2.45, 0.62, materials.metal, 0, 1.76, 0);
addBox(gateRoot, 2.5, 1.45, 0.65, materials.dark, 0, 2.9, 0);
const gateCanvas = document.createElement("canvas");
gateCanvas.width = 512;
gateCanvas.height = 256;
const gateContext = gateCanvas.getContext("2d");
const gateTexture = new THREE.CanvasTexture(gateCanvas);
gateTexture.colorSpace = THREE.SRGBColorSpace;
gateTexture.minFilter = THREE.NearestFilter;
gateTexture.magFilter = THREE.NearestFilter;
gateTexture.generateMipmaps = false;
const gateDisplay = new THREE.Mesh(
new THREE.PlaneGeometry(2.22, 1.12),
new THREE.MeshBasicMaterial({
map: gateTexture,
toneMapped: false
})
);
gateDisplay.position.set(0, 2.9, 0.331);
gateRoot.add(gateDisplay);
const greenLampMaterial = new THREE.MeshStandardMaterial({
color: 0x19382a,
emissive: 0x66f5a2,
emissiveIntensity: 0.08,
roughness: 0.3
});
const redLampMaterial = new THREE.MeshStandardMaterial({
color: 0x4a1c25,
emissive: 0xff5b64,
emissiveIntensity: 0.08,
roughness: 0.3
});
addBox(
gateRoot,
0.3,
0.3,
0.3,
greenLampMaterial,
0.78,
3.78,
0.03
);
addBox(
gateRoot,
0.3,
0.3,
0.3,
redLampMaterial,
-0.78,
3.78,
0.03
);
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x352029,
emissive: 0xff5b64,
emissiveIntensity: 0.12,
roughness: 0.35
});
addBox(gateRoot, 0.5, 0.5, 0.5, beaconMaterial, 0, 3.78, 0);
addBox(gateRoot, 0.64, 0.12, 0.64, materials.metal, 0, 3.46, 0);
// ---------------------------------------------------------------------
// Physical auxiliary switch
// ---------------------------------------------------------------------
const switchRoot = new THREE.Group();
switchRoot.position.set(3.35, 0, 1.05);
switchRoot.userData.action = "switch";
scene.add(switchRoot);
addBox(switchRoot, 0.95, 0.24, 0.95, materials.dark, 0, 0.3, 0);
addBox(switchRoot, 0.7, 0.08, 0.7, materials.metal, 0, 0.46, 0);
const switchPivot = new THREE.Group();
switchPivot.position.set(0, 0.47, 0);
switchRoot.add(switchPivot);
addBox(
switchPivot,
0.16,
0.82,
0.16,
new THREE.MeshStandardMaterial({
color: 0x66f5a2,
emissive: 0x227344,
emissiveIntensity: 0.35,
roughness: 0.42
}),
0,
0.36,
0
);
addBox(switchPivot, 0.31, 0.18, 0.31, materials.rubber, 0, 0.75, 0);
const switchGreenMaterial = new THREE.MeshStandardMaterial({
color: 0x244b37,
emissive: 0x66f5a2,
emissiveIntensity: 1.2
});
const switchRedMaterial = new THREE.MeshStandardMaterial({
color: 0x4b2025,
emissive: 0xff5b64,
emissiveIntensity: 0.08
});
addBox(
switchRoot,
0.18,
0.12,
0.18,
switchGreenMaterial,
-0.27,
0.51,
0.36
);
addBox(
switchRoot,
0.18,
0.12,
0.18,
switchRedMaterial,
0.27,
0.51,
0.36
);
const switchLabel = makeLabel("SWITCH B", 1.05, 0.28, "#66f5a2");
switchLabel.position.set(0, 0.37, 0.51);
switchRoot.add(switchLabel);
// ---------------------------------------------------------------------
// Visible cable from plate to gate
// ---------------------------------------------------------------------
const cablePoints = [
new THREE.Vector3(-0.47, 0.28, 0.2),
new THREE.Vector3(2.0, 0.28, 0.2),
new THREE.Vector3(2.0, 0.28, -1.3),
new THREE.Vector3(2.45, 0.28, -1.3)
];
const cableMaterial = new THREE.MeshStandardMaterial({
color: 0x17232d,
roughness: 0.68,
metalness: 0.35
});
function addCableSegment(a, b) {
const middle = a.clone().add(b).multiplyScalar(0.5);
const length = a.distanceTo(b);
if (Math.abs(a.x - b.x) > 0.001) {
addBox(
scene,
length,
0.14,
0.18,
cableMaterial,
middle.x,
middle.y,
middle.z
);
} else {
addBox(
scene,
0.18,
0.14,
length,
cableMaterial,
middle.x,
middle.y,
middle.z
);
}
}
for (let i = 0; i < cablePoints.length - 1; i++) {
addCableSegment(cablePoints[i], cablePoints[i + 1]);
}
const signalMaterial = new THREE.MeshStandardMaterial({
color: 0x76552b,
emissive: 0xffad59,
emissiveIntensity: 0.2,
roughness: 0.34
});
const signalBeads = [];
for (let i = 0; i < 14; i++) {
const bead = addBox(
scene,
0.15,
0.12,
0.15,
signalMaterial,
0,
0.4,
0
);
signalBeads.push(bead);
}
const cableLengths = [];
let totalCableLength = 0;
for (let i = 0; i < cablePoints.length - 1; i++) {
const length = cablePoints[i].distanceTo(cablePoints[i + 1]);
cableLengths.push(length);
totalCableLength += length;
}
function sampleCable(t, target) {
let remaining = THREE.MathUtils.clamp(t, 0, 0.999999) * totalCableLength;
for (let i = 0; i < cableLengths.length; i++) {
if (remaining <= cableLengths[i]) {
const localT = remaining / cableLengths[i];
return target.lerpVectors(cablePoints[i], cablePoints[i + 1], localT);
}
remaining -= cableLengths[i];
}
return target.copy(cablePoints[cablePoints.length - 1]);
}
// ---------------------------------------------------------------------
// Logic state and UI
// ---------------------------------------------------------------------
const state = {
weightOnPlate: false,
switchOn: true,
mode: "AND",
output: false
};
const ui = {
plateBit: document.getElementById("plateBit"),
switchBit: document.getElementById("switchBit"),
outputRow: document.getElementById("outputRow"),
outputValue: document.getElementById("outputValue"),
weightButton: document.getElementById("weightButton"),
switchButton: document.getElementById("switchButton"),
viewButton: document.getElementById("viewButton"),
liveDot: document.getElementById("liveDot"),
toast: document.getElementById("toast"),
modeButtons: [...document.querySelectorAll("[data-mode]")]
};
let toastTimer;
function announce(message) {
ui.toast.textContent = message;
ui.toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => ui.toast.classList.remove("show"), 2200);
}
function drawGateDisplay() {
const ctx = gateContext;
const outputColor = state.output ? "#66f5a2" : "#ff6d72";
ctx.fillStyle = "#07121a";
ctx.fillRect(0, 0, gateCanvas.width, gateCanvas.height);
ctx.fillStyle = "#58dff5";
ctx.font = "900 30px monospace";
ctx.fillText("BOOLEAN GATE", 20, 40);
ctx.fillStyle = "#385263";
ctx.fillRect(20, 55, 472, 3);
ctx.font = "700 24px monospace";
ctx.fillStyle = "#d9f4ff";
ctx.fillText(`MODE ${state.mode}`, 20, 92);
ctx.fillStyle = state.weightOnPlate ? "#66f5a2" : "#718391";
ctx.fillText(`A PLATE ${state.weightOnPlate ? "1" : "0"}`, 20, 135);
ctx.fillStyle = state.switchOn ? "#66f5a2" : "#718391";
ctx.fillText(`B SWITCH ${state.switchOn ? "1" : "0"}`, 20, 178);
ctx.fillStyle = outputColor;
ctx.font = "900 35px monospace";
ctx.fillText(
`OUTPUT ${state.output ? "TRUE" : "FALSE"}`,
20,
232
);
gateTexture.needsUpdate = true;
}
function updateLogic() {
state.output =
state.mode === "AND"
? state.weightOnPlate && state.switchOn
: state.weightOnPlate || state.switchOn;
ui.plateBit.textContent = state.weightOnPlate ? "1" : "0";
ui.plateBit.classList.toggle("on", state.weightOnPlate);
ui.switchBit.textContent = state.switchOn ? "1" : "0";
ui.switchBit.classList.toggle("on", state.switchOn);
ui.outputValue.textContent = state.output ? "TRUE" : "FALSE";
ui.outputRow.classList.toggle("true", state.output);
ui.weightButton.textContent = state.weightOnPlate
? "REMOVE WEIGHT"
: "LOAD WEIGHT";
ui.switchButton.textContent = `SWITCH B: ${state.switchOn ? "ON" : "OFF"}`;
for (const button of ui.modeButtons) {
const active = button.dataset.mode === state.mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
}
ui.liveDot.style.background = state.output ? "var(--green)" : "var(--red)";
ui.liveDot.style.color = state.output ? "var(--green)" : "var(--red)";
if (state.output) {
greenLampMaterial.color.setHex(0x66f5a2);
greenLampMaterial.emissiveIntensity = 2.2;
redLampMaterial.color.setHex(0x35171d);
redLampMaterial.emissiveIntensity = 0.05;
beaconMaterial.color.setHex(0x66f5a2);
beaconMaterial.emissive.setHex(0x66f5a2);
beaconMaterial.emissiveIntensity = 2;
} else {
greenLampMaterial.color.setHex(0x19382a);
greenLampMaterial.emissiveIntensity = 0.08;
redLampMaterial.color.setHex(0xff6d72);
redLampMaterial.emissiveIntensity = 1.8;
beaconMaterial.color.setHex(0x8d3440);
beaconMaterial.emissive.setHex(0xff5b64);
beaconMaterial.emissiveIntensity = 0.7;
}
switchGreenMaterial.emissiveIntensity = state.switchOn ? 1.5 : 0.08;
switchRedMaterial.emissiveIntensity = state.switchOn ? 0.08 : 1.2;
drawGateDisplay();
}
// ---------------------------------------------------------------------
// Interaction: clicking the weight and switch
// ---------------------------------------------------------------------
let weightAnimation = null;
function toggleWeight() {
state.weightOnPlate = !state.weightOnPlate;
weightAnimation = {
from: weightRoot.position.clone(),
to: new THREE.Vector3(
state.weightOnPlate ? plateRoot.position.x : parkingX,
weightRoot.position.y,
state.weightOnPlate ? plateRoot.position.z : parkingZ
),
t: 0
};
updateLogic();
announce(
state.weightOnPlate
? "Plate A is pressed. The weight is now feeding signal 1 into the gate."
: "Weight removed. Plate A has returned to 0 and the gate has been recalculated."
);
}
function toggleSwitch() {
state.switchOn = !state.switchOn;
updateLogic();
announce(`Switch B is now ${state.switchOn ? "ON (1)" : "OFF (0)"}.`);
}
function setMode(mode) {
state.mode = mode;
updateLogic();
announce(`Gate operation changed to ${mode}.`);
}
ui.weightButton.addEventListener("click", toggleWeight);
ui.switchButton.addEventListener("click", toggleSwitch);
ui.modeButtons.forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
ui.viewButton.addEventListener("click", () => {
cameraControls.yaw = 0.69;
cameraControls.pitch = 0.72;
cameraControls.viewSize = 11.2;
updateCamera();
resize();
});
// ---------------------------------------------------------------------
// Raycasting and manually implemented orbit controls
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const interactiveRoots = [weightRoot, switchRoot, plateRoot];
function findAction(object) {
let current = object;
while (current) {
if (current.userData.action) return current.userData.action;
current = current.parent;
}
return null;
}
function pickAction(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(interactiveRoots, true);
return intersections.length ? findAction(intersections[0].object) : null;
}
let pointerDown = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
let hoveredAction = null;
function updateHover(clientX, clientY) {
if (pointerDown && pointerMoved) return;
hoveredAction = pickAction(clientX, clientY);
renderer.domElement.style.cursor =
hoveredAction === "weight" || hoveredAction === "switch"
? "pointer"
: "grab";
weightBodyMaterial.emissiveIntensity =
hoveredAction === "weight" ? 0.34 : 0.08;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) {
updateHover(event.clientX, event.clientY);
return;
}
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (Math.abs(dx) + Math.abs(dy) > 4) pointerMoved = true;
if (pointerMoved) {
cameraControls.yaw -= dx * 0.006;
cameraControls.pitch = THREE.MathUtils.clamp(
cameraControls.pitch + dy * 0.005,
0.38,
1.28
);
updateCamera();
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerDown) return;
if (!pointerMoved) {
const action = pickAction(event.clientX, event.clientY);
if (action === "weight" || action === "plate") toggleWeight();
if (action === "switch") toggleSwitch();
}
pointerDown = false;
pointerMoved = false;
renderer.domElement.style.cursor = "grab";
updateHover(event.clientX, event.clientY);
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControls.viewSize = THREE.MathUtils.clamp(
cameraControls.viewSize + event.deltaY * 0.008,
7.5,
15
);
resize();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.repeat) return;
const key = event.key.toLowerCase();
if (key === "w") toggleWeight();
if (key === "s") toggleSwitch();
if (key === "1") setMode("AND");
if (key === "2") setMode("OR");
if (key === "r") {
cameraControls.yaw = 0.69;
cameraControls.pitch = 0.72;
cameraControls.viewSize = 11.2;
updateCamera();
resize();
}
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const signalSample = new THREE.Vector3();
let plateProgress = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
plateProgress = THREE.MathUtils.damp(
plateProgress,
state.weightOnPlate ? 1 : 0,
10,
delta
);
platePad.position.y = 0.42 - plateProgress * 0.15;
const plateTargetY = state.weightOnPlate
? platePad.position.y + 0.07 + 0.625
: 0.805;
if (weightAnimation) {
weightAnimation.t = Math.min(weightAnimation.t + delta / 0.72, 1);
const t = weightAnimation.t;
const eased = t * t * (3 - 2 * t);
weightRoot.position.x = THREE.MathUtils.lerp(
weightAnimation.from.x,
weightAnimation.to.x,
eased
);
weightRoot.position.z = THREE.MathUtils.lerp(
weightAnimation.from.z,
weightAnimation.to.z,
eased
);
weightRoot.position.y =
THREE.MathUtils.lerp(weightAnimation.from.y, plateTargetY, eased) +
Math.sin(t * Math.PI) * 1.02;
weightRoot.rotation.y = Math.sin(t * Math.PI) * 0.32;
if (t >= 1) {
weightAnimation = null;
weightRoot.position.y = plateTargetY;
weightRoot.rotation.y = 0;
}
} else {
weightRoot.position.y = THREE.MathUtils.damp(
weightRoot.position.y,
plateTargetY,
12,
delta
);
}
switchPivot.rotation.z = THREE.MathUtils.damp(
switchPivot.rotation.z,
state.switchOn ? -0.52 : 0.52,
11,
delta
);
if (state.weightOnPlate) {
plateSignalMaterial.color.setHex(0x58dff5);
plateSignalMaterial.emissive.setHex(0x58dff5);
plateSignalMaterial.emissiveIntensity = 1.7;
signalMaterial.color.setHex(0x58dff5);
signalMaterial.emissive.setHex(0x58dff5);
signalMaterial.emissiveIntensity = 1.8;
} else {
plateSignalMaterial.color.setHex(0x183c4d);
plateSignalMaterial.emissive.setHex(0x173b4a);
plateSignalMaterial.emissiveIntensity = 0.12;
signalMaterial.color.setHex(0x76552b);
signalMaterial.emissive.setHex(0xffad59);
signalMaterial.emissiveIntensity = 0.2;
}
signalBeads.forEach((bead, index) => {
const t = (elapsed * 0.28 + index / signalBeads.length) % 1;
sampleCable(t, signalSample);
bead.position.copy(signalSample);
bead.position.y = 0.4;
const pulse = 0.82 + Math.sin(elapsed * 7 - index * 0.65) * 0.18;
bead.scale.setScalar(pulse);
});
const outputPulse =
state.output ? 1.85 + Math.sin(elapsed * 5) * 0.25 : 0.7;
beaconMaterial.emissiveIntensity = THREE.MathUtils.damp(
beaconMaterial.emissiveIntensity,
outputPulse,
9,
delta
);
renderer.render(scene, camera);
}
updateLogic();
animate();
</script>
</body>
</html>
```