Prompt specification
Build or customize the Three.js 3D scene "Archive lookup classroom scene" 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: Archive lookup classroom scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/archive-lookup-classroom-scene-x1851
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts binary search within an ordered collection. Compose sorted document drawers and a target card with a visible candidate-range overlay; animate decisions based on comparisons rather than guessing locations from visual proximity. Voxel style.
Working Three.js Starter (archive-lookup-classroom-scene-x1851):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Binary Search — Voxel Archive</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 21, 32, 0.84);
--border: rgba(106, 231, 218, 0.25);
--cyan: #6ae7da;
--cyan-soft: #a5fff4;
--gold: #ffc857;
--green: #72f29a;
--red: #ff7a7a;
--muted: #8295a7;
--text: #edf8ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
body {
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.title-block {
top: 24px;
left: 26px;
max-width: 460px;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 6px;
font-size: clamp(26px, 3.2vw, 48px);
line-height: 0.95;
letter-spacing: -0.06em;
text-transform: uppercase;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.panel {
border: 1px solid var(--border);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
}
.controls {
top: 24px;
right: 24px;
width: 258px;
padding: 15px;
pointer-events: auto;
}
.control-row {
display: flex;
gap: 8px;
align-items: end;
}
.control-row + .control-row {
margin-top: 10px;
}
.field {
flex: 1;
}
.field label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
input[type="number"] {
width: 100%;
height: 38px;
padding: 0 10px;
border: 1px solid rgba(106, 231, 218, 0.32);
border-radius: 0;
outline: none;
background: rgba(3, 11, 18, 0.88);
color: var(--cyan-soft);
font: 800 15px inherit;
}
input[type="number"]:focus {
border-color: var(--cyan);
box-shadow: 0 0 0 2px rgba(106, 231, 218, 0.1);
}
button {
min-height: 38px;
padding: 0 12px;
border: 1px solid rgba(106, 231, 218, 0.34);
border-radius: 0;
background: rgba(106, 231, 218, 0.09);
color: var(--cyan-soft);
font: 800 11px inherit;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
background: rgba(106, 231, 218, 0.17);
border-color: var(--cyan);
}
button.secondary {
color: var(--muted);
border-color: rgba(130, 149, 167, 0.28);
background: rgba(130, 149, 167, 0.06);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(106, 231, 218, 0.45);
}
.swatch.gold {
background: var(--gold);
box-shadow: 0 0 10px rgba(255, 200, 87, 0.45);
}
.swatch.green {
background: var(--green);
box-shadow: 0 0 10px rgba(114, 242, 154, 0.45);
}
.swatch.gray {
background: #405264;
box-shadow: none;
}
.readout {
left: 26px;
right: 26px;
bottom: 24px;
display: grid;
grid-template-columns: 1.05fr 1.55fr 1fr;
gap: 1px;
max-width: 920px;
overflow: hidden;
pointer-events: auto;
}
.readout-cell {
min-height: 104px;
padding: 15px 17px;
background: rgba(10, 21, 32, 0.92);
}
.readout-label {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#phase {
color: var(--cyan);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#comparison {
min-height: 34px;
color: var(--text);
font-size: clamp(14px, 1.5vw, 19px);
font-weight: 800;
line-height: 1.45;
}
#comparison em {
color: var(--gold);
font-style: normal;
}
.metrics {
display: flex;
gap: 24px;
}
.metric strong {
display: block;
color: var(--cyan-soft);
font-size: 19px;
}
.metric span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.trace {
min-height: 58px;
max-height: 58px;
overflow: hidden;
color: var(--muted);
font-size: 10px;
line-height: 1.65;
}
.trace div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.trace div:last-child {
color: var(--cyan-soft);
}
.hint {
right: 26px;
bottom: 142px;
color: rgba(165, 255, 244, 0.56);
font-size: 9px;
letter-spacing: 0.08em;
text-align: right;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-block {
top: 16px;
left: 16px;
}
.subtitle {
display: none;
}
.controls {
top: 16px;
right: 16px;
width: 190px;
padding: 11px;
}
.legend {
display: none;
}
.control-row {
flex-wrap: wrap;
}
.field {
min-width: 100%;
}
button {
flex: 1;
}
.readout {
left: 12px;
right: 12px;
bottom: 12px;
grid-template-columns: 1fr 1fr;
}
.readout-cell {
min-height: 82px;
padding: 11px;
}
.trace-cell {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="hud title-block">
<div class="eyebrow">Algorithm chamber / 03</div>
<h1>Binary Search</h1>
<p class="subtitle">
An ordered document archive. Every move is calculated from a numeric
comparison—not from visual distance.
</p>
</header>
<aside class="hud controls panel">
<div class="control-row">
<div class="field">
<label for="targetInput">Target value</label>
<input id="targetInput" type="number" value="71" min="1" max="99" />
</div>
</div>
<div class="control-row">
<button id="replayButton">Replay</button>
<button id="pauseButton" class="secondary">Pause</button>
</div>
<div class="legend">
<span><i class="swatch"></i> Candidate</span>
<span><i class="swatch gold"></i> Midpoint</span>
<span><i class="swatch green"></i> Match</span>
<span><i class="swatch gray"></i> Excluded</span>
</div>
</aside>
<section class="hud readout panel" aria-live="polite">
<div class="readout-cell">
<div class="readout-label">Search phase</div>
<div id="phase">Ready</div>
<div class="metrics" style="margin-top: 13px">
<div class="metric">
<strong id="rangeValue">1–9</strong>
<span>Range</span>
</div>
<div class="metric">
<strong id="midValue">—</strong>
<span>Mid</span>
</div>
</div>
</div>
<div class="readout-cell">
<div class="readout-label">Direct comparison</div>
<div id="comparison">Waiting for target.</div>
</div>
<div class="readout-cell trace-cell">
<div class="readout-label">Comparison trace</div>
<div id="trace" class="trace"></div>
</div>
</section>
<div class="hud hint">Drag to orbit · scroll to zoom · click a drawer to target it</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core scene
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const targetInput = document.getElementById("targetInput");
const replayButton = document.getElementById("replayButton");
const pauseButton = document.getElementById("pauseButton");
const phaseElement = document.getElementById("phase");
const comparisonElement = document.getElementById("comparison");
const rangeElement = document.getElementById("rangeValue");
const midElement = document.getElementById("midValue");
const traceElement = document.getElementById("trace");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
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.08;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 24, 46);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.0, 0);
const orbit = {
theta: 0,
phi: Math.PI / 2.9,
radius: 21
};
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);
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const verticalSize = Math.max(9.5, 22 / aspect);
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9be9ff, 0x182030, 1.55));
const keyLight = new THREE.DirectionalLight(0xfff2d5, 3.0);
keyLight.position.set(-7, 12, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x46e6d3, 32, 17, 2);
cyanLight.position.set(5, 5, 5);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffa64d, 18, 14, 2);
warmLight.position.set(-7, 4, 3);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Voxel floor
// -------------------------------------------------------------------------
const floorBase = new THREE.Mesh(
new THREE.BoxGeometry(23, 0.35, 11.5),
new THREE.MeshStandardMaterial({
color: 0x0c1722,
roughness: 0.95,
metalness: 0.08
})
);
floorBase.position.y = -0.32;
floorBase.receiveShadow = true;
scene.add(floorBase);
const tileGeometry = new THREE.BoxGeometry(0.92, 0.14, 0.92);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.04
});
const tileColumns = 23;
const tileRows = 11;
const tileCount = tileColumns * tileRows;
const floorTiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const tileDummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let row = 0; row < tileRows; row++) {
for (let column = 0; column < tileColumns; column++) {
const x = (column - (tileColumns - 1) / 2) * 0.98;
const z = (row - (tileRows - 1) / 2) * 0.98 - 0.55;
tileDummy.position.set(x, -0.12, z);
tileDummy.updateMatrix();
floorTiles.setMatrixAt(tileIndex, tileDummy.matrix);
const checker = (row + column) % 2;
tileColor.setHSL(
0.55 + checker * 0.008,
0.22,
checker ? 0.105 : 0.085
);
floorTiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
floorTiles.instanceMatrix.needsUpdate = true;
if (floorTiles.instanceColor) floorTiles.instanceColor.needsUpdate = true;
floorTiles.receiveShadow = true;
scene.add(floorTiles);
// -------------------------------------------------------------------------
// Pixel-style label helpers
// -------------------------------------------------------------------------
function createLabelData(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
const data = {
canvas,
context,
texture,
options: {
foreground: "#edf8ff",
background: "rgba(7, 16, 25, 0.93)",
border: "#6ae7da",
fontSize: 62,
...options
}
};
drawLabel(data, text);
return data;
}
function drawLabel(data, text, foreground, border) {
const { canvas, context, texture, options } = data;
const fg = foreground || options.foreground;
const edge = border || options.border;
context.clearRect(0, 0, canvas.width, canvas.height);
if (options.background) {
context.fillStyle = options.background;
context.fillRect(0, 0, canvas.width, canvas.height);
}
if (edge) {
context.strokeStyle = edge;
context.lineWidth = 8;
context.strokeRect(4, 4, canvas.width - 8, canvas.height - 8);
}
context.font = `800 ${options.fontSize}px monospace`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = fg;
context.fillText(text, canvas.width / 2, canvas.height / 2 + 3);
texture.needsUpdate = true;
}
function createLabelPlane(text, width, height, options = {}) {
const data = createLabelData(text, options);
const material = new THREE.MeshBasicMaterial({
map: data.texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
mesh.userData.label = data;
return mesh;
}
function createLabelSprite(text, width, height, options = {}) {
const data = createLabelData(text, options);
const material = new THREE.SpriteMaterial({
map: data.texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.userData.label = data;
return sprite;
}
// -------------------------------------------------------------------------
// Ordered document drawers
// -------------------------------------------------------------------------
const values = [11, 24, 35, 48, 60, 71, 84, 92, 97];
const drawerSpacing = 1.42;
const collectionCenter = -1.12;
const drawerX = index =>
collectionCenter + (index - (values.length - 1) / 2) * drawerSpacing;
const candidateColor = new THREE.Color(0x54e6d2);
const pivotColor = new THREE.Color(0xffc857);
const foundColor = new THREE.Color(0x72f29a);
const excludedColor = new THREE.Color(0x344657);
const drawers = [];
const pickMeshes = [];
values.forEach((value, index) => {
const group = new THREE.Group();
group.position.x = drawerX(index);
scene.add(group);
const baseColor = new THREE.Color().setHSL(
0.56 + index * 0.012,
0.42,
0.42
);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: baseColor,
emissive: 0x000000,
emissiveIntensity: 0.2,
roughness: 0.62,
metalness: 0.12
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.12, 1.45, 0.78),
bodyMaterial
);
body.position.y = 0.72;
body.castShadow = true;
body.receiveShadow = true;
body.userData.drawerIndex = index;
group.add(body);
pickMeshes.push(body);
const frontMaterial = new THREE.MeshStandardMaterial({
color: 0x182b3a,
roughness: 0.72,
metalness: 0.08
});
const front = new THREE.Mesh(
new THREE.BoxGeometry(0.98, 1.1, 0.08),
frontMaterial
);
front.position.set(0, 0.77, 0.42);
front.castShadow = true;
front.userData.drawerIndex = index;
group.add(front);
pickMeshes.push(front);
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0x8db7c4,
roughness: 0.45,
metalness: 0.42
});
const topLip = new THREE.Mesh(
new THREE.BoxGeometry(1.2, 0.11, 0.86),
trimMaterial
);
topLip.position.y = 1.47;
topLip.castShadow = true;
group.add(topLip);
const handle = new THREE.Mesh(
new THREE.BoxGeometry(0.42, 0.09, 0.14),
new THREE.MeshStandardMaterial({
color: 0xc8e4e8,
roughness: 0.35,
metalness: 0.55
})
);
handle.position.set(0, 0.48, 0.51);
handle.castShadow = true;
group.add(handle);
const valuePlate = createLabelPlane(String(value), 0.78, 0.34, {
foreground: "#e9fbff",
background: "rgba(5, 13, 21, 0.94)",
border: "#69cfc9",
fontSize: 70
});
valuePlate.position.set(0, 1.02, 0.468);
group.add(valuePlate);
const indexPlate = createLabelPlane(
String(index + 1).padStart(2, "0"),
0.32,
0.18,
{
foreground: "#718da1",
background: "rgba(8, 18, 28, 0.9)",
border: "#355064",
fontSize: 54
}
);
indexPlate.position.set(0, 0.72, 0.468);
group.add(indexPlate);
const tile = new THREE.Mesh(
new THREE.BoxGeometry(1.2, 0.045, 1.16),
new THREE.MeshStandardMaterial({
color: 0x162938,
emissive: 0x0d2731,
emissiveIntensity: 0.25,
transparent: true,
opacity: 0.5,
roughness: 0.8
})
);
tile.position.set(drawerX(index), 0.025, 0);
tile.receiveShadow = true;
scene.add(tile);
drawers.push({
group,
body,
front,
bodyMaterial,
frontMaterial,
tile,
baseColor,
visual: {
candidate: index === 0 ? 1 : 0,
pivot: 0,
found: 0
}
});
});
// Archive title sign.
const archiveTitle = createLabelSprite(
"DOCUMENT DRAWERS / SORTED ASCENDING",
6.6,
0.62,
{
foreground: "#bafff5",
background: "rgba(7, 18, 27, 0.92)",
border: "#3b948e",
fontSize: 42
}
);
archiveTitle.position.set(collectionCenter, 3.18, -0.72);
scene.add(archiveTitle);
for (const x of [collectionCenter - 2.65, collectionCenter + 2.65]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.1, 1.15, 0.1),
new THREE.MeshStandardMaterial({
color: 0x355463,
roughness: 0.5,
metalness: 0.5
})
);
post.position.set(x, 2.1, -0.72);
scene.add(post);
}
// -------------------------------------------------------------------------
// Candidate range overlay
// -------------------------------------------------------------------------
const rangeMaterial = new THREE.MeshBasicMaterial({
color: 0x54e6d2,
transparent: true,
opacity: 0.13,
depthWrite: false
});
const rangeOverlay = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.035, 1.48),
rangeMaterial
);
rangeOverlay.position.y = 0.01;
scene.add(rangeOverlay);
const endpointMaterial = new THREE.MeshBasicMaterial({
color: 0x7ffff0,
transparent: true,
opacity: 0.85,
depthWrite: false
});
const leftEndpoint = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.3, 1.52),
endpointMaterial
);
const rightEndpoint = leftEndpoint.clone();
leftEndpoint.position.y = 0.13;
rightEndpoint.position.y = 0.13;
scene.add(leftEndpoint, rightEndpoint);
const rangeLabel = createLabelSprite(
"CANDIDATE RANGE 1–9",
3.3,
0.42,
{
foreground: "#9ffff2",
background: "rgba(7, 18, 27, 0.88)",
border: "#4dc8bc",
fontSize: 44
}
);
rangeLabel.position.set(collectionCenter, 0.35, 1.18);
scene.add(rangeLabel);
// -------------------------------------------------------------------------
// Target card
// -------------------------------------------------------------------------
const targetGroup = new THREE.Group();
targetGroup.position.set(7.15, 0, 0.3);
scene.add(targetGroup);
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x263846,
roughness: 0.72,
metalness: 0.18
});
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(2.05, 0.3, 1.8),
pedestalMaterial
);
pedestal.position.y = 0.13;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
targetGroup.add(pedestal);
const stem = new THREE.Mesh(
new THREE.BoxGeometry(0.35, 1.45, 0.35),
pedestalMaterial
);
stem.position.y = 0.98;
stem.castShadow = true;
targetGroup.add(stem);
const targetCardMaterial = new THREE.MeshStandardMaterial({
color: 0xf4fbf7,
emissive: 0x5cd9cd,
emissiveIntensity: 0.12,
roughness: 0.55,
metalness: 0.05
});
const targetCard = new THREE.Mesh(
new THREE.BoxGeometry(1.75, 2.15, 0.22),
targetCardMaterial
);
targetCard.position.y = 2.18;
targetCard.castShadow = true;
targetGroup.add(targetCard);
const targetCaption = createLabelPlane("TARGET", 1.22, 0.3, {
foreground: "#67d9cf",
background: "rgba(7, 18, 27, 0.94)",
border: "#4b9f99",
fontSize: 48
});
targetCaption.position.set(0, 2.73, 0.12);
targetGroup.add(targetCaption);
const targetValuePlate = createLabelPlane("71", 1.28, 0.66, {
foreground: "#ffffff",
background: "rgba(12, 29, 41, 0.96)",
border: "#70eee0",
fontSize: 86
});
targetValuePlate.position.set(0, 2.02, 0.12);
targetGroup.add(targetValuePlate);
const targetSubtitle = createLabelPlane("COMPARE KEY", 1.22, 0.28, {
foreground: "#8ba4b5",
background: "rgba(7, 18, 27, 0.94)",
border: "#3b5969",
fontSize: 43
});
targetSubtitle.position.set(0, 1.43, 0.12);
targetGroup.add(targetSubtitle);
const targetBeacon = new THREE.Group();
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x70eee0,
emissive: 0x3dd8cb,
emissiveIntensity: 1.2,
roughness: 0.25,
metalness: 0.2
});
const beaconCore = new THREE.Mesh(
new THREE.BoxGeometry(0.24, 0.24, 0.24),
beaconMaterial
);
targetBeacon.add(beaconCore);
const beaconTop = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.13, 0.13),
beaconMaterial
);
beaconTop.position.y = 0.22;
targetBeacon.add(beaconTop);
targetBeacon.position.set(0, 3.56, 0);
targetGroup.add(targetBeacon);
// -------------------------------------------------------------------------
// Pivot frame, pixel arrow and found beacon
// -------------------------------------------------------------------------
const pivotFrame = new THREE.Group();
const pivotMaterial = new THREE.MeshBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.9,
depthWrite: false
});
for (const x of [-0.61, 0.61]) {
for (const y of [0.02, 1.42]) {
const corner = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.14, 0.14),
pivotMaterial
);
corner.position.set(x, y, 0.53);
pivotFrame.add(corner);
}
}
scene.add(pivotFrame);
const pivotArrow = new THREE.Group();
const pivotArrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffd46e,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const arrowShaft = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.08, 0.08),
pivotArrowMaterial
);
arrowShaft.position.x = 0.25;
pivotArrow.add(arrowShaft);
const arrowPixels = [
[0.48, 0],
[0.58, -0.11],
[0.68, -0.22],
[0.58, 0.11],
[0.68, 0.22]
];
for (const [x, z] of arrowPixels) {
const pixel = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.08, 0.13),
pivotArrowMaterial
);
pixel.position.set(x, 0, z);
pivotArrow.add(pixel);
}
scene.add(pivotArrow);
const foundBeacon = new THREE.Group();
const foundMaterial = new THREE.MeshStandardMaterial({
color: 0x72f29a,
emissive: 0x42e879,
emissiveIntensity: 1.1,
roughness: 0.3
});
for (let i = 0; i < 3; i++) {
const block = new THREE.Mesh(
new THREE.BoxGeometry(0.25 - i * 0.05, 0.18, 0.25 - i * 0.05),
foundMaterial
);
block.position.y = i * 0.2;
foundBeacon.add(block);
}
scene.add(foundBeacon);
// -------------------------------------------------------------------------
// Decorative floating voxel data
// -------------------------------------------------------------------------
const floatingCubes = [];
const floatingData = [
[-8.7, 2.6, -3.5, 0x4abfb7],
[-7.5, 3.5, -4.2, 0x315c78],
[8.8, 1.2, -3.8, 0xffbd63],
[9.7, 3.3, -2.2, 0x3c7b90],
[5.4, 4.0, -4.0, 0x5f638f],
[-1.8, 4.4, -4.4, 0x446477]
];
floatingData.forEach(([x, y, z, color], index) => {
const cube = new THREE.Mesh(
new THREE.BoxGeometry(0.24 + (index % 3) * 0.06, 0.24, 0.24),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.15,
roughness: 0.7
})
);
cube.position.set(x, y, z);
cube.userData.baseY = y;
cube.userData.phase = index * 0.8;
scene.add(cube);
floatingCubes.push(cube);
});
// -------------------------------------------------------------------------
// Search state
// -------------------------------------------------------------------------
const state = {
low: 0,
high: values.length - 1,
pivot: -1,
found: -1,
direction: 0,
target: 71
};
const visualRange = {
low: 0,
high: values.length - 1,
opacity: 1
};
let paused = false;
let runToken = 0;
const traceEntries = [];
function updateRangeReadout() {
if (state.low > state.high) {
rangeElement.textContent = "empty";
} else {
rangeElement.textContent = `${state.low + 1}–${state.high + 1}`;
}
midElement.textContent =
state.pivot >= 0 ? String(values[state.pivot]) : "—";
}
function updateRangeLabel() {
if (state.low > state.high) {
drawLabel(rangeLabel.userData.label, "NO CANDIDATES");
return;
}
drawLabel(
rangeLabel.userData.label,
`CANDIDATE RANGE ${state.low + 1}–${state.high + 1}`
);
}
function setTargetCard(value, matched = false) {
drawLabel(
targetValuePlate.userData.label,
String(value),
matched ? "#a7ffbc" : "#ffffff",
matched ? "#72f29a" : "#70eee0"
);
targetCardMaterial.emissive.set(matched ? 0x42e879 : 0x5cd9cd);
targetCardMaterial.emissiveIntensity = matched ? 0.28 : 0.12;
}
function addTrace(text, kind = "") {
traceEntries.push({ text, kind });
if (traceEntries.length > 4) traceEntries.shift();
traceElement.replaceChildren();
for (const entry of traceEntries) {
const line = document.createElement("div");
line.textContent = entry.text;
if (entry.kind === "found") {
line.style.color = "#72f29a";
} else if (entry.kind === "failed") {
line.style.color = "#ff9a9a";
}
traceElement.appendChild(line);
}
}
function nextFrame() {
return new Promise(resolve => requestAnimationFrame(resolve));
}
async function delay(milliseconds) {
const start = performance.now();
let previous = start;
while (performance.now() - start < milliseconds) {
await nextFrame();
const now = performance.now();
if (!paused) {
milliseconds -= now - previous;
}
previous = now;
}
}
async function runSearch() {
const token = ++runToken;
const target = Math.max(
1,
Math.min(99, Number(targetInput.value) || 0)
);
state.target = target;
state.low = 0;
state.high = values.length - 1;
state.pivot = -1;
state.found = -1;
state.direction = 0;
visualRange.low = state.low;
visualRange.high = state.high;
visualRange.opacity = 1;
traceEntries.length = 0;
traceElement.replaceChildren();
setTargetCard(target, false);
updateRangeReadout();
updateRangeLabel();
phaseElement.textContent = "Initializing";
comparisonElement.textContent = `Target key ${target} entered into the archive.`;
await delay(350);
if (token !== runToken) return;
while (state.low <= state.high) {
// The midpoint is calculated from the current numeric candidate bounds.
const mid = Math.floor((state.low + state.high) / 2);
const drawerValue = values[mid];
state.pivot = mid;
state.direction = 0;
const relation =
drawerValue < target
? "<"
: drawerValue > target
? ">"
: "=";
phaseElement.textContent = `Comparing midpoint ${mid + 1}`;
comparisonElement.innerHTML =
`drawer[${mid + 1}] <em>${drawerValue}</em> ` +
`${relation} target <em>${target}</em>`;
updateRangeReadout();
await delay(850);
if (token !== runToken) return;
if (drawerValue === target) {
state.found = mid;
state.low = mid;
state.high = mid;
state.direction = 0;
phaseElement.textContent = "Match found";
comparisonElement.innerHTML =
`<em>${drawerValue} = ${target}</em> — exact match located`;
addTrace(`[${mid + 1}] ${drawerValue} = ${target} → FOUND`, "found");
setTargetCard(target, true);
updateRangeReadout();
updateRangeLabel();
await delay(700);
return;
}
if (drawerValue < target) {
// A smaller value can only discard the left half.
state.direction = 1;
phaseElement.textContent = "Less than target → search right";
comparisonElement.innerHTML =
`<em>${drawerValue} < ${target}</em> — discard indices ` +
`${state.low + 1}–${mid + 1}`;
addTrace(`[${mid + 1}] ${drawerValue} < ${target} → low = ${mid + 2}`);
await delay(520);
if (token !== runToken) return;
state.low = mid + 1;
} else {
// A larger value can only discard the right half.
state.direction = -1;
phaseElement.textContent = "Greater than target → search left";
comparisonElement.innerHTML =
`<em>${drawerValue} > ${target}</em> — discard indices ` +
`${mid + 2}–${state.high + 1}`;
addTrace(`[${mid + 1}] ${drawerValue} > ${target} → high = ${mid}`);
await delay(520);
if (token !== runToken) return;
state.high = mid - 1;
}
updateRangeReadout();
updateRangeLabel();
await delay(450);
if (token !== runToken) return;
}
state.pivot = -1;
state.direction = 0;
phaseElement.textContent = "Search complete";
comparisonElement.textContent = `No drawer equals target ${target}.`;
addTrace(`No exact match for ${target}`, "failed");
updateRangeReadout();
updateRangeLabel();
}
// -------------------------------------------------------------------------
// Direct camera controls: drag orbit and wheel zoom
// -------------------------------------------------------------------------
let pointerDown = null;
let pointerDragged = false;
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = {
x: event.clientX,
y: event.clientY,
theta: orbit.theta,
phi: orbit.phi
};
pointerDragged = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 4) {
pointerDragged = true;
}
orbit.theta = pointerDown.theta - dx * 0.005;
orbit.phi = THREE.MathUtils.clamp(
pointerDown.phi - dy * 0.004,
0.48,
1.42
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
if (pointerDown && !pointerDragged) {
selectDrawerFromPointer(event);
}
pointerDown = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * (event.deltaY > 0 ? 0.9 : 1.1),
0.7,
1.65
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function selectDrawerFromPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (hits.length) {
const index = hits[0].object.userData.drawerIndex;
targetInput.value = values[index];
runSearch();
}
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
replayButton.addEventListener("click", runSearch);
targetInput.addEventListener("change", runSearch);
targetInput.addEventListener("keydown", event => {
if (event.key === "Enter") runSearch();
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.style.color = paused ? "#ffc857" : "";
});
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") runSearch();
if (event.code === "Space" && document.activeElement !== targetInput) {
event.preventDefault();
pauseButton.click();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const tempColor = new THREE.Color();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const smoothing = 1 - Math.exp(-delta * 9);
// Drawer state is driven by the actual low/high bounds.
drawers.forEach((drawer, index) => {
const isCandidate =
state.found < 0 && index >= state.low && index <= state.high;
const isPivot = index === state.pivot;
const isFound = index === state.found;
drawer.visual.candidate +=
((isCandidate ? 1 : 0) - drawer.visual.candidate) * smoothing;
drawer.visual.pivot +=
((isPivot ? 1 : 0) - drawer.visual.pivot) * smoothing;
drawer.visual.found +=
((isFound ? 1 : 0) - drawer.visual.found) * smoothing;
tempColor.copy(drawer.baseColor);
if (drawer.visual.candidate > 0.001) {
tempColor.lerp(candidateColor, drawer.visual.candidate * 0.34);
}
if (drawer.visual.pivot > 0.001) {
tempColor.lerp(pivotColor, drawer.visual.pivot * 0.78);
}
if (drawer.visual.found > 0.001) {
tempColor.lerp(foundColor, drawer.visual.found * 0.88);
}
drawer.bodyMaterial.color.copy(tempColor);
if (drawer.visual.found > 0.01) {
drawer.bodyMaterial.emissive.copy(foundColor);
drawer.bodyMaterial.emissiveIntensity =
0.38 * drawer.visual.found;
} else if (drawer.visual.pivot > 0.01) {
drawer.bodyMaterial.emissive.copy(pivotColor);
drawer.bodyMaterial.emissiveIntensity =
0.32 * drawer.visual.pivot;
} else {
drawer.bodyMaterial.emissive.copy(candidateColor);
drawer.bodyMaterial.emissiveIntensity =
0.07 * drawer.visual.candidate;
}
drawer.frontMaterial.color.copy(excludedColor).lerp(
candidateColor,
drawer.visual.candidate * 0.17
);
drawer.group.position.y =
-0.075 * (1 - drawer.visual.candidate) +
0.11 * drawer.visual.pivot +
0.16 * drawer.visual.found;
drawer.tile.material.color
.set(0x142737)
.lerp(candidateColor, drawer.visual.candidate * 0.82);
drawer.tile.material.opacity = 0.32 + drawer.visual.candidate * 0.55;
drawer.tile.scale.y = 0.75 + drawer.visual.candidate * 0.55;
});
// Smoothly interpolate the visible candidate interval.
const rangeExists = state.low <= state.high;
if (rangeExists) {
visualRange.low += (state.low - visualRange.low) * smoothing;
visualRange.high += (state.high - visualRange.high) * smoothing;
visualRange.opacity += (1 - visualRange.opacity) * smoothing;
} else {
visualRange.opacity += (0 - visualRange.opacity) * smoothing;
}
const leftX = drawerX(visualRange.low);
const rightX = drawerX(visualRange.high);
const rangeWidth = Math.max(0.01, rightX - leftX + drawerSpacing * 0.84);
const rangeCenter = (leftX + rightX) / 2;
rangeOverlay.position.x = rangeCenter;
rangeOverlay.scale.x = rangeWidth;
rangeMaterial.opacity = 0.13 * visualRange.opacity;
leftEndpoint.position.x = leftX;
rightEndpoint.position.x = rightX;
leftEndpoint.material.opacity = 0.82 * visualRange.opacity;
rightEndpoint.material.opacity = 0.82 * visualRange.opacity;
leftEndpoint.visible = visualRange.opacity > 0.02;
rightEndpoint.visible = visualRange.opacity > 0.02;
rangeLabel.position.x += (rangeCenter - rangeLabel.position.x) * smoothing;
rangeLabel.material.opacity = visualRange.opacity;
// Midpoint marker.
pivotFrame.visible = state.pivot >= 0;
pivotArrow.visible = state.direction !== 0;
if (state.pivot >= 0) {
pivotFrame.position.x = drawerX(state.pivot);
pivotFrame.scale.setScalar(1 + Math.sin(elapsed * 8) * 0.035);
}
if (state.direction !== 0) {
pivotArrow.position.set(
drawerX(state.pivot),
0.13,
1.06
);
pivotArrow.rotation.y = state.direction < 0 ? Math.PI : 0;
pivotArrow.scale.setScalar(1 + Math.sin(elapsed * 7) * 0.04);
}
// Found beacon.
foundBeacon.visible = state.found >= 0;
if (state.found >= 0) {
foundBeacon.position.set(
drawerX(state.found),
2.08 + Math.sin(elapsed * 4) * 0.07,
0
);
foundBeacon.rotation.y = elapsed * 0.8;
}
// Target card beacon.
targetBeacon.position.y = 3.55 + Math.sin(elapsed * 3.2) * 0.1;
targetBeacon.rotation.y = elapsed * 0.9;
// Ambient floating cubes.
floatingCubes.forEach(cube => {
cube.position.y =
cube.userData.baseY + Math.sin(elapsed * 0.75 + cube.userData.phase) * 0.18;
cube.rotation.x = elapsed * 0.18 + cube.userData.phase;
cube.rotation.y = elapsed * 0.25;
});
renderer.render(scene, camera);
}
animate();
runSearch();
</script>
</body>
</html>
```