Prompt specification
Build or customize the Three.js 3D scene "Binary place-value light row" 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: Binary place-value light row
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/binary-place-value-light-row-x1845
Scene Description Prompt:
Toggle eight low-poly binary lamps labeled with powers of two; update their decimal sum. Three.js from cdnjs, one HTML file.
Working Three.js Starter (binary-place-value-light-row-x1845):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Binary Lamps</title>
<style>
:root {
color-scheme: dark;
--cyan: #74f5ff;
--amber: #ffc66d;
--panel: rgba(7, 18, 24, 0.82);
--line: rgba(116, 245, 255, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071117;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas:focus-visible {
box-shadow: inset 0 0 0 2px rgba(116, 245, 255, 0.7);
}
.brand,
.readout,
.hint {
position: fixed;
z-index: 2;
pointer-events: none;
user-select: none;
}
.brand {
top: 28px;
left: 30px;
color: #e9fbff;
}
.brand h1 {
margin: 0;
font-size: 20px;
font-weight: 750;
letter-spacing: 0.22em;
}
.brand h1 span {
color: var(--cyan);
}
.brand p {
margin: 8px 0 0;
color: #77939c;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.19em;
}
.readout {
top: 24px;
right: 26px;
min-width: 190px;
padding: 16px 18px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
}
.readout-label {
display: block;
color: #78949d;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.22em;
}
.sum-row {
display: flex;
align-items: baseline;
justify-content: flex-end;
gap: 8px;
margin-top: 1px;
}
#sum {
color: var(--amber);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 52px;
font-weight: 750;
line-height: 1.1;
letter-spacing: -0.05em;
text-shadow: 0 0 24px rgba(255, 166, 62, 0.3);
font-variant-numeric: tabular-nums;
}
.max {
color: #526a72;
font-family: ui-monospace, monospace;
font-size: 12px;
}
#binary {
display: block;
margin-top: 9px;
padding-top: 9px;
border-top: 1px solid rgba(116, 245, 255, 0.12);
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 15px;
letter-spacing: 0.24em;
text-align: right;
}
.hint {
left: 50%;
bottom: 22px;
transform: translateX(-50%);
width: max-content;
max-width: calc(100vw - 30px);
padding: 9px 14px;
border: 1px solid rgba(116, 245, 255, 0.12);
border-radius: 999px;
background: rgba(5, 14, 19, 0.65);
color: #78939c;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-align: center;
backdrop-filter: blur(10px);
}
.hint b {
color: #b8d5dc;
}
@media (max-width: 650px) {
.brand {
top: 18px;
left: 18px;
}
.brand h1 {
font-size: 14px;
letter-spacing: 0.15em;
}
.brand p {
display: none;
}
.readout {
top: 13px;
right: 13px;
min-width: 142px;
padding: 12px 13px;
}
#sum {
font-size: 38px;
}
#binary {
font-size: 12px;
}
.hint {
bottom: 13px;
font-size: 8px;
letter-spacing: 0.08em;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="brand">
<h1>BINARY <span>LAMPS</span></h1>
<p id="arrayStatus">8 CHANNELS · 0 / 8 ACTIVE · RANGE 0–255</p>
</header>
<section class="readout" aria-label="Binary lamp total">
<span class="readout-label">DECIMAL SUM</span>
<div class="sum-row">
<output id="sum" aria-live="polite">0</output>
<span class="max">/ 255</span>
</div>
<code id="binary">00000000</code>
</section>
<div class="hint">
<b>CLICK</b> A LAMP · <b>1–8</b> KEYS · <b>DRAG</b> TO ORBIT · <b>SCROLL</b> TO ZOOM
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// Binary channels are arranged from the most significant bit to the least.
const powers = [128, 64, 32, 16, 8, 4, 2, 1];
const states = Array(8).fill(false);
const lamps = [];
const pickMeshes = [];
const stage = document.querySelector("#stage");
const sumElement = document.querySelector("#sum");
const binaryElement = document.querySelector("#binary");
const statusElement = document.querySelector("#arrayStatus");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071117);
scene.fog = new THREE.Fog(0x071117, 17, 36);
const camera = new THREE.PerspectiveCamera(
52,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Eight interactive binary lamps. Click a lamp or press keys 1 through 8 to toggle it."
);
stage.appendChild(renderer.domElement);
// Lighting
scene.add(new THREE.HemisphereLight(0x9eeaff, 0x071014, 1.65));
const keyLight = new THREE.DirectionalLight(0xfff4df, 3.0);
keyLight.position.set(4.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x4be8ff, 24, 18, 2);
cyanLight.position.set(-7, 4, -3);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xff8e48, 18, 16, 2);
warmLight.position.set(7, 3, 3);
scene.add(warmLight);
// Floor and grid
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x081218,
roughness: 0.82,
metalness: 0.18
});
const floor = new THREE.Mesh(new THREE.PlaneGeometry(44, 44), floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x1e5662, 0x102d35);
grid.position.y = -0.185;
grid.material.transparent = true;
grid.material.opacity = 0.5;
scene.add(grid);
// Platform and circuit details
const platformGroup = new THREE.Group();
scene.add(platformGroup);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x111e25,
metalness: 0.58,
roughness: 0.38
});
const platform = new THREE.Mesh(
new THREE.BoxGeometry(12.8, 0.32, 3.1),
platformMaterial
);
platform.receiveShadow = true;
platform.castShadow = true;
platformGroup.add(platform);
const platformTopMaterial = new THREE.MeshStandardMaterial({
color: 0x172831,
metalness: 0.42,
roughness: 0.48
});
const platformTop = new THREE.Mesh(
new THREE.BoxGeometry(12.15, 0.045, 2.55),
platformTopMaterial
);
platformTop.position.y = 0.18;
platformTop.receiveShadow = true;
platformGroup.add(platformTop);
const traceMaterial = new THREE.MeshStandardMaterial({
color: 0x17444d,
emissive: 0x0c6772,
emissiveIntensity: 0.75,
metalness: 0.35,
roughness: 0.45
});
for (const z of [-1.47, 1.47]) {
const edge = new THREE.Mesh(
new THREE.BoxGeometry(12.15, 0.035, 0.035),
traceMaterial
);
edge.position.set(0, 0.225, z);
platformGroup.add(edge);
}
const trunk = new THREE.Mesh(
new THREE.BoxGeometry(10.7, 0.035, 0.04),
traceMaterial
);
trunk.position.set(0, 0.225, -1.18);
platformGroup.add(trunk);
for (let i = 0; i < 8; i++) {
const x = (i - 3.5) * 1.5;
const trace = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.035, 0.73),
traceMaterial
);
trace.position.set(x, 0.225, -0.82);
platformGroup.add(trace);
}
// Shared lamp materials
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x1c2d35,
metalness: 0.72,
roughness: 0.32,
flatShading: true
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1419,
metalness: 0.8,
roughness: 0.28,
flatShading: true
});
const collarMaterial = new THREE.MeshStandardMaterial({
color: 0x39515b,
metalness: 0.82,
roughness: 0.24,
flatShading: true
});
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
}
function createLabel(index, power) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(0, 2.22, 0);
sprite.scale.set(1.3, 0.65, 1);
sprite.renderOrder = 20;
const label = { canvas, texture, sprite, index, power };
updateLabel(label);
return label;
}
function updateLabel(label) {
const context = label.canvas.getContext("2d");
const { canvas, index, power } = label;
const active = states[index];
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 8, 8, 496, 240, 26);
context.fillStyle = active
? "rgba(49, 31, 13, 0.94)"
: "rgba(6, 19, 25, 0.92)";
context.fill();
context.lineWidth = active ? 7 : 4;
context.strokeStyle = active
? "rgba(255, 183, 75, 0.9)"
: "rgba(93, 225, 237, 0.5)";
context.stroke();
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = active ? "#d7a75f" : "#71959e";
context.font = "700 27px ui-monospace, monospace";
context.fillText(
`CHANNEL ${index + 1} · 2^${7 - index}`,
canvas.width / 2,
55
);
context.fillStyle = active ? "#ffd187" : "#d9faff";
context.font = "800 78px ui-monospace, monospace";
context.fillText(String(power), canvas.width / 2, 128);
context.fillStyle = active ? "#ffad48" : "#52717a";
context.font = "750 29px ui-monospace, monospace";
context.fillText(active ? "ON" : "OFF", canvas.width / 2, 197);
label.texture.needsUpdate = true;
}
function registerPickable(mesh, index) {
mesh.userData.lampIndex = index;
pickMeshes.push(mesh);
}
function createLamp(index, power) {
const group = new THREE.Group();
scene.add(group);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.54, 0.59, 0.24, 8),
baseMaterial
);
base.position.y = 0.32;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
registerPickable(base, index);
const lowerBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.49, 0.16, 8),
darkMetalMaterial
);
lowerBase.position.y = 0.49;
lowerBase.castShadow = true;
group.add(lowerBase);
registerPickable(lowerBase, index);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.2, 0.43, 8),
baseMaterial
);
stem.position.y = 0.74;
stem.castShadow = true;
group.add(stem);
registerPickable(stem, index);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.28, 0.18, 8),
collarMaterial
);
collar.position.y = 1.0;
collar.castShadow = true;
group.add(collar);
registerPickable(collar, index);
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0x18363c,
emissive: 0x052329,
emissiveIntensity: 0.5,
metalness: 0.12,
roughness: 0.3,
flatShading: true
});
const bulb = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.43, 0),
bulbMaterial
);
bulb.position.y = 1.39;
bulb.castShadow = true;
group.add(bulb);
registerPickable(bulb, index);
const indicatorMaterial = new THREE.MeshBasicMaterial({
color: 0x18363c
});
const indicator = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.065, 0),
indicatorMaterial
);
indicator.position.set(0, 0.57, 0.4);
group.add(indicator);
registerPickable(indicator, index);
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x74f5ff,
transparent: true,
opacity: 0,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.6, 0.025, 4, 20),
haloMaterial
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.47;
group.add(halo);
const glow = new THREE.PointLight(0xff8a32, 0, 3.5, 2);
glow.position.set(0, 1.45, 0.05);
group.add(glow);
const label = createLabel(index, power);
group.add(label.sprite);
registerPickable(label.sprite, index);
return {
index,
power,
group,
bulb,
bulbMaterial,
indicatorMaterial,
haloMaterial,
glow,
label
};
}
powers.forEach((power, index) => {
lamps.push(createLamp(index, power));
});
let compactLayout = false;
function layoutLamps() {
compactLayout = innerWidth < 720 || camera.aspect < 0.72;
platformGroup.scale.x = compactLayout ? 0.68 : 1;
lamps.forEach((lamp, index) => {
if (compactLayout) {
const column = index % 4;
const row = Math.floor(index / 4);
lamp.group.position.set(
(column - 1.5) * 1.5,
0,
row === 0 ? -0.72 : 0.72
);
} else {
lamp.group.position.set((index - 3.5) * 1.5, 0, 0);
}
});
}
function updateReadout() {
const sum = states.reduce(
(total, isOn, index) => total + (isOn ? powers[index] : 0),
0
);
const activeCount = states.filter(Boolean).length;
sumElement.textContent = String(sum);
binaryElement.textContent = states.map(Boolean).map(Number).join("");
statusElement.textContent =
`8 CHANNELS · ${activeCount} / 8 ACTIVE · RANGE 0–255`;
}
function applyLampState(index) {
const lamp = lamps[index];
const active = states[index];
lamp.bulbMaterial.color.setHex(active ? 0xffc568 : 0x18363c);
lamp.bulbMaterial.emissive.setHex(active ? 0xff5b00 : 0x052329);
lamp.indicatorMaterial.color.setHex(active ? 0xffe29a : 0x18363c);
lamp.glow.color.setHex(active ? 0xff8a32 : 0x000000);
lamp.glow.intensity = active ? 4 : 0;
updateLabel(lamp.label);
updateReadout();
}
function toggleLamp(index) {
states[index] = !states[index];
applyLampState(index);
}
// Custom camera controls, implemented without external controls modules.
const cameraTarget = new THREE.Vector3(0, 0.92, 0);
const orbit = { theta: 0.1, phi: 1.16, radius: 13.8 };
const orbitSmooth = { ...orbit };
let cameraInteracted = false;
function cameraPreset(immediate = false) {
orbit.theta = compactLayout ? 0 : 0.1;
orbit.phi = compactLayout ? 0.98 : 1.16;
orbit.radius = compactLayout ? 16.5 : 13.8;
if (immediate) {
orbitSmooth.theta = orbit.theta;
orbitSmooth.phi = orbit.phi;
orbitSmooth.radius = orbit.radius;
}
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 9);
orbitSmooth.theta += (orbit.theta - orbitSmooth.theta) * smoothing;
orbitSmooth.phi += (orbit.phi - orbitSmooth.phi) * smoothing;
orbitSmooth.radius += (orbit.radius - orbitSmooth.radius) * smoothing;
const spherical = new THREE.Spherical(
orbitSmooth.radius,
orbitSmooth.phi,
orbitSmooth.theta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
const wasCompact = compactLayout;
layoutLamps();
if (!cameraInteracted && wasCompact !== compactLayout) {
cameraPreset(false);
}
}
window.addEventListener("resize", resize);
layoutLamps();
cameraPreset(true);
// Raycasting and pointer interaction
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredLamp = -1;
let pointerDown = null;
function pickLamp(clientX, clientY) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((clientY - bounds.top) / bounds.height) * 2 + 1;
camera.updateMatrixWorld();
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
return hits.length ? hits[0].object.userData.lampIndex : -1;
}
function setHovered(index) {
hoveredLamp = index;
renderer.domElement.style.cursor = index >= 0 ? "pointer" : "grab";
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
renderer.domElement.focus({ preventScroll: true });
renderer.domElement.setPointerCapture(event.pointerId);
pointerDown = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerDown && pointerDown.id === event.pointerId) {
const totalDistance = Math.hypot(
event.clientX - pointerDown.startX,
event.clientY - pointerDown.startY
);
if (totalDistance > 5) {
pointerDown.moved = true;
renderer.domElement.style.cursor = "grabbing";
}
if (pointerDown.moved) {
const deltaX = event.clientX - pointerDown.lastX;
const deltaY = event.clientY - pointerDown.lastY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.005,
0.62,
1.5
);
cameraInteracted = true;
}
pointerDown.lastX = event.clientX;
pointerDown.lastY = event.clientY;
} else {
setHovered(pickLamp(event.clientX, event.clientY));
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (!pointerDown || pointerDown.id !== event.pointerId) return;
if (!pointerDown.moved) {
const index = pickLamp(event.clientX, event.clientY);
if (index >= 0) toggleLamp(index);
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
pointerDown = null;
setHovered(pickLamp(event.clientX, event.clientY));
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
setHovered(-1);
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) setHovered(-1);
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
compactLayout ? 11 : 8.5,
25
);
cameraInteracted = true;
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
window.addEventListener("keydown", event => {
if (event.ctrlKey || event.metaKey || event.altKey) return;
if (/^[1-8]$/.test(event.key)) {
toggleLamp(Number(event.key) - 1);
event.preventDefault();
} else if (event.key.toLowerCase() === "r") {
cameraInteracted = false;
cameraPreset(false);
} else if (event.key === "ArrowLeft") {
orbit.theta -= 0.12;
cameraInteracted = true;
} else if (event.key === "ArrowRight") {
orbit.theta += 0.12;
cameraInteracted = true;
} else if (event.key === "ArrowUp") {
orbit.phi = Math.max(0.62, orbit.phi - 0.08);
cameraInteracted = true;
} else if (event.key === "ArrowDown") {
orbit.phi = Math.min(1.5, orbit.phi + 0.08);
cameraInteracted = true;
}
});
// Animation
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
lamps.forEach((lamp, index) => {
const active = states[index];
const pulse = Math.sin(time * 2.2 + index * 0.7);
lamp.bulbMaterial.emissiveIntensity = active
? 2.25 + pulse * 0.18
: hoveredLamp === index
? 0.72
: 0.45;
lamp.glow.intensity = active ? 4 + pulse * 0.35 : 0;
const targetScale = hoveredLamp === index ? 1.065 : 1;
const scaleAmount = 1 - Math.exp(-dt * 12);
lamp.bulb.scale.x += (targetScale - lamp.bulb.scale.x) * scaleAmount;
lamp.bulb.scale.y += (targetScale - lamp.bulb.scale.y) * scaleAmount;
lamp.bulb.scale.z += (targetScale - lamp.bulb.scale.z) * scaleAmount;
const targetHaloOpacity = hoveredLamp === index ? 0.8 : 0;
lamp.haloMaterial.opacity +=
(targetHaloOpacity - lamp.haloMaterial.opacity) * scaleAmount;
});
updateCamera(dt);
renderer.render(scene, camera);
}
updateReadout();
lamps.forEach((_, index) => applyLampState(index));
animate();
</script>
</body>
</html>
```