Prompt specification
Build or customize the Three.js 3D scene "Lamp power-state comparison" 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: Lamp power-state comparison
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/lamp-power-state-comparison-x3897
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a voxel table lamp and battery lantern on the same small table. Run a visible cord from the lamp to a wall socket and place a separate battery symbol beside the lantern. Begin with both devices lighting their immediate surroundings. Add a room-supply switch that extinguishes only the plugged-in lamp; the lantern should continue to shine from its independent source. Switching the supply back on restores the lamp without altering the lantern's state, while the source symbols remain visible beside their respective devices.
Working Three.js Starter (lamp-power-state-comparison-x3897):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Table Lamp and Battery Lantern</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101923;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 20px;
left: 22px;
z-index: 5;
max-width: min(430px, calc(100vw - 44px));
padding: 15px 18px;
color: #edf7ff;
background: rgba(12, 20, 29, 0.78);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 14px;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
.brand h1 {
margin: 0 0 5px;
font-size: 18px;
letter-spacing: 0.02em;
}
.brand p {
margin: 0;
color: #a9bac8;
font-size: 13px;
line-height: 1.4;
}
.controls {
position: fixed;
top: 20px;
right: 22px;
z-index: 6;
width: 270px;
padding: 16px;
color: #edf7ff;
background: rgba(12, 20, 29, 0.86);
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 14px;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(12px);
}
.supply-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-bottom: 13px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.supply-label {
color: #c9d6df;
font-size: 13px;
font-weight: 650;
}
#supplyToggle {
min-width: 82px;
padding: 9px 12px;
border: 1px solid #3e725f;
border-radius: 9px;
color: #10251e;
background: #6ce0aa;
font: inherit;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.06em;
cursor: pointer;
transition: background 150ms, border-color 150ms, color 150ms;
}
#supplyToggle:hover {
filter: brightness(1.08);
}
#supplyToggle:focus-visible {
outline: 3px solid rgba(108, 224, 170, 0.35);
outline-offset: 2px;
}
#supplyToggle.off {
color: #fff0ec;
background: #e56754;
border-color: #9b4438;
}
.device-row {
display: grid;
grid-template-columns: 12px 1fr auto;
align-items: center;
gap: 9px;
padding-top: 12px;
font-size: 13px;
}
.device-row + .device-row {
margin-top: 8px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.lamp {
color: #ffc36b;
background: #ffc36b;
}
.dot.lantern {
color: #ffe58b;
background: #ffe58b;
}
.device-row strong {
font-size: 11px;
letter-spacing: 0.08em;
}
.device-row strong.off {
color: #83919c;
}
.hint {
margin: 14px 0 0;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
color: #8fa2b2;
font-size: 11px;
line-height: 1.45;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 650px) {
.brand {
top: 12px;
left: 12px;
padding: 12px 14px;
}
.brand h1 {
font-size: 15px;
}
.brand p {
font-size: 11px;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: min(270px, calc(100vw - 24px));
padding: 13px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive voxel room containing a table lamp and battery lantern"
></canvas>
<section class="brand">
<h1>Voxel Light Lab</h1>
<p>One table, two independent light sources.</p>
</section>
<section class="controls" aria-label="Lighting controls">
<div class="supply-row">
<span class="supply-label">Room supply</span>
<button id="supplyToggle" type="button" aria-pressed="true">
ON
</button>
</div>
<div class="device-row">
<span class="dot lamp"></span>
<span>Table lamp · mains</span>
<strong id="lampState">ON</strong>
</div>
<div class="device-row">
<span class="dot lantern"></span>
<span>Lantern · battery</span>
<strong id="lanternState">ON</strong>
</div>
<p class="hint">
Click the wall switch or use the button above.<br>
Drag to orbit · scroll to zoom.
</p>
</section>
<div id="liveStatus" class="sr-only" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const supplyToggle = document.querySelector("#supplyToggle");
const lampState = document.querySelector("#lampState");
const lanternState = document.querySelector("#lanternState");
const liveStatus = document.querySelector("#liveStatus");
// -------------------------------------------------------------------------
// Renderer and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
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(0x101923);
scene.fog = new THREE.Fog(0x101923, 11, 21);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(-0.08, 1.52, 0.18);
let cameraRadius = 9.1;
let cameraTheta = 0.59;
let cameraPhi = 1.08;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, roughness = 0.8, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
function addBox(
parent,
size,
position,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
const floorMaterial = standardMaterial(0x293640, 0.96);
const wallMaterial = standardMaterial(0x46545b, 0.93);
const trimMaterial = standardMaterial(0x253039, 0.9);
const woodMaterial = standardMaterial(0x845638, 0.78);
const darkWoodMaterial = standardMaterial(0x503429, 0.85);
const outletMaterial = standardMaterial(0xd8ddd9, 0.58);
const darkMaterial = standardMaterial(0x182129, 0.58, 0.18);
const tealMaterial = standardMaterial(0x245760, 0.48, 0.22);
// -------------------------------------------------------------------------
// Room
// -------------------------------------------------------------------------
addBox(
scene,
[9, 0.2, 9],
[0, -0.1, 0.4],
floorMaterial,
false,
true
);
addBox(
scene,
[9, 5, 0.2],
[0, 2.4, -3.55],
wallMaterial,
false,
true
);
addBox(
scene,
[0.2, 5, 8.2],
[-3.72, 2.4, 0.45],
wallMaterial,
false,
true
);
addBox(
scene,
[9, 0.16, 0.08],
[0, 0.08, -3.43],
trimMaterial,
false,
true
);
addBox(
scene,
[0.08, 0.16, 8.2],
[-3.60, 0.08, 0.45],
trimMaterial,
false,
true
);
// A subtle block-built rug under the table.
addBox(
scene,
[5.8, 0.035, 3.5],
[0, 0.018, 0.45],
standardMaterial(0x334c54, 0.98),
false,
true
);
addBox(
scene,
[5.3, 0.018, 3.0],
[0, 0.043, 0.45],
standardMaterial(0x3a5960, 0.98),
false,
true
);
// -------------------------------------------------------------------------
// Table
// -------------------------------------------------------------------------
const table = new THREE.Group();
scene.add(table);
addBox(
table,
[4.8, 0.22, 2.35],
[0, 1.55, 0.35],
woodMaterial
);
addBox(
table,
[4.55, 0.13, 2.1],
[0, 1.39, 0.35],
darkWoodMaterial
);
for (const x of [-2.05, 2.05]) {
for (const z of [-0.52, 1.22]) {
addBox(
table,
[0.25, 1.38, 0.25],
[x, 0.69, z],
darkWoodMaterial
);
}
}
addBox(
table,
[4.1, 0.15, 0.15],
[0, 0.48, 1.22],
darkWoodMaterial
);
// -------------------------------------------------------------------------
// Voxel table lamp
// -------------------------------------------------------------------------
const lamp = new THREE.Group();
lamp.position.set(-1.05, 0, 0.30);
scene.add(lamp);
const lampBaseMaterial = standardMaterial(0x34434a, 0.48, 0.3);
const lampShadeMaterial = new THREE.MeshStandardMaterial({
color: 0xe89a42,
roughness: 0.72,
metalness: 0.03,
emissive: 0xff9b35,
emissiveIntensity: 0.18
});
const lampBulbMaterial = new THREE.MeshStandardMaterial({
color: 0xffe3a1,
roughness: 0.3,
emissive: 0xffb44f,
emissiveIntensity: 5.0
});
addBox(lamp, [0.82, 0.14, 0.72], [0, 1.73, 0], lampBaseMaterial);
addBox(lamp, [0.60, 0.12, 0.54], [0, 1.86, 0], lampBaseMaterial);
addBox(lamp, [0.17, 0.96, 0.17], [0, 2.39, 0], lampBaseMaterial);
// The shade remains voxel-shaped and does not block the point light.
addBox(
lamp,
[1.12, 0.18, 1.02],
[0, 2.74, 0],
lampShadeMaterial,
false,
true
);
addBox(
lamp,
[0.94, 0.25, 0.86],
[0, 2.94, 0],
lampShadeMaterial,
false,
true
);
addBox(
lamp,
[0.72, 0.24, 0.66],
[0, 3.18, 0],
lampShadeMaterial,
false,
true
);
addBox(
lamp,
[0.38, 0.12, 0.38],
[0, 3.36, 0],
lampBaseMaterial,
false,
true
);
addBox(
lamp,
[0.38, 0.14, 0.38],
[0, 2.61, 0],
lampBulbMaterial,
false,
false
);
const lampLight = new THREE.PointLight(0xffb55e, 65, 5.8, 2);
lampLight.position.set(-1.05, 2.56, 0.30);
lampLight.castShadow = true;
lampLight.shadow.mapSize.set(1024, 1024);
lampLight.shadow.camera.near = 0.08;
lampLight.shadow.camera.far = 6;
lampLight.shadow.bias = -0.003;
scene.add(lampLight);
// -------------------------------------------------------------------------
// Voxel battery lantern
// -------------------------------------------------------------------------
const lantern = new THREE.Group();
lantern.position.set(1.12, 0, 0.30);
scene.add(lantern);
const lanternGlassMaterial = new THREE.MeshStandardMaterial({
color: 0xffe49a,
roughness: 0.25,
metalness: 0,
emissive: 0xffc94e,
emissiveIntensity: 3.2,
transparent: true,
opacity: 0.82
});
addBox(lantern, [0.78, 0.14, 0.66], [0, 1.73, 0], tealMaterial);
addBox(lantern, [0.68, 0.10, 0.58], [0, 1.84, 0], darkMaterial);
addBox(
lantern,
[0.50, 0.60, 0.42],
[0, 2.18, 0],
lanternGlassMaterial,
false,
false
);
for (const x of [-0.285, 0.285]) {
for (const z of [-0.235, 0.235]) {
addBox(
lantern,
[0.07, 0.68, 0.07],
[x, 2.18, z],
tealMaterial
);
}
}
addBox(lantern, [0.72, 0.14, 0.62], [0, 2.56, 0], tealMaterial);
addBox(lantern, [0.54, 0.10, 0.50], [0, 2.65, 0], darkMaterial);
// Block-built handle.
addBox(lantern, [0.08, 0.42, 0.08], [-0.25, 2.84, 0], darkMaterial);
addBox(lantern, [0.08, 0.42, 0.08], [0.25, 2.84, 0], darkMaterial);
addBox(lantern, [0.58, 0.08, 0.08], [0, 3.05, 0], darkMaterial);
const lanternLight = new THREE.PointLight(0xffda73, 38, 4.8, 2);
lanternLight.position.set(1.12, 2.18, 0.30);
lanternLight.castShadow = true;
lanternLight.shadow.mapSize.set(512, 512);
lanternLight.shadow.camera.near = 0.08;
lanternLight.shadow.camera.far = 5;
lanternLight.shadow.bias = -0.003;
scene.add(lanternLight);
// -------------------------------------------------------------------------
// Wall outlet and visible block-built power cord
// -------------------------------------------------------------------------
const outlet = new THREE.Group();
outlet.position.set(-3.53, 1.86, -0.90);
scene.add(outlet);
addBox(outlet, [0.14, 0.54, 0.42], [0, 0, 0], outletMaterial);
addBox(
outlet,
[0.025, 0.075, 0.065],
[0.078, 0.055, -0.09],
darkMaterial,
false,
false
);
addBox(
outlet,
[0.025, 0.075, 0.065],
[0.078, 0.055, 0.09],
darkMaterial,
false,
false
);
const plugMaterial = new THREE.MeshStandardMaterial({
color: 0xf18a3d,
roughness: 0.58,
metalness: 0.08,
emissive: 0x3d1605,
emissiveIntensity: 0.25
});
addBox(outlet, [0.20, 0.18, 0.23], [0.16, -0.02, 0], plugMaterial);
const cableMaterial = new THREE.MeshStandardMaterial({
color: 0xf29b48,
roughness: 0.68,
metalness: 0.03,
emissive: 0x321202,
emissiveIntensity: 0.22
});
const cablePoints = [
new THREE.Vector3(-1.40, 1.73, 0.30),
new THREE.Vector3(-2.28, 1.73, 0.30),
new THREE.Vector3(-2.48, 1.73, 0.30),
new THREE.Vector3(-2.58, 1.62, 0.30),
new THREE.Vector3(-2.58, 0.12, 0.30),
new THREE.Vector3(-2.58, 0.12, -0.78),
new THREE.Vector3(-3.12, 0.12, -0.90),
new THREE.Vector3(-3.28, 0.12, -0.90),
new THREE.Vector3(-3.28, 1.78, -0.90),
new THREE.Vector3(-3.28, 1.84, -0.90)
];
function createVoxelCable(points, material) {
const spacing = 0.072;
const cubeSize = 0.078;
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
total += Math.ceil(points[i].distanceTo(points[i + 1]) / spacing);
}
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const cable = new THREE.InstancedMesh(geometry, material, total);
const dummy = new THREE.Object3D();
let instance = 0;
for (let i = 0; i < points.length - 1; i++) {
const start = points[i];
const end = points[i + 1];
const distance = start.distanceTo(end);
const steps = Math.ceil(distance / spacing);
for (let step = 0; step < steps; step++) {
const t = step / steps;
dummy.position.lerpVectors(start, end, t);
dummy.updateMatrix();
cable.setMatrixAt(instance++, dummy.matrix);
}
}
cable.instanceMatrix.needsUpdate = true;
cable.castShadow = true;
cable.receiveShadow = true;
scene.add(cable);
}
createVoxelCable(cablePoints, cableMaterial);
// -------------------------------------------------------------------------
// Wall-mounted room supply switch
// -------------------------------------------------------------------------
const switchGroup = new THREE.Group();
switchGroup.position.set(-3.53, 1.95, 0.35);
scene.add(switchGroup);
addBox(
switchGroup,
[0.14, 0.60, 0.40],
[0, 0, 0],
outletMaterial
);
const rockerMaterial = new THREE.MeshStandardMaterial({
color: 0x65d6a7,
roughness: 0.5,
emissive: 0x0d3b28,
emissiveIntensity: 0.4
});
const rocker = new THREE.Group();
rocker.position.set(0.14, 0, 0);
switchGroup.add(rocker);
addBox(
rocker,
[0.11, 0.27, 0.25],
[0, 0, 0],
rockerMaterial
);
const switchLedMaterial = new THREE.MeshStandardMaterial({
color: 0x65ffb4,
emissive: 0x2aff99,
emissiveIntensity: 1.4
});
addBox(
switchGroup,
[0.045, 0.055, 0.075],
[0.085, 0.21, 0],
switchLedMaterial,
false,
false
);
// A larger invisible target makes the small wall switch easy to click.
const invisibleMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const switchHitTarget = new THREE.Mesh(
new THREE.BoxGeometry(0.45, 0.90, 0.72),
invisibleMaterial
);
switchHitTarget.position.set(-3.25, 1.95, 0.35);
scene.add(switchHitTarget);
// -------------------------------------------------------------------------
// Source-symbol badges
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function createSourceBadge(kind) {
const badgeCanvas = document.createElement("canvas");
badgeCanvas.width = 512;
badgeCanvas.height = 240;
const ctx = badgeCanvas.getContext("2d");
roundedRect(ctx, 8, 8, 496, 224, 28);
ctx.fillStyle = "rgba(9, 17, 25, 0.92)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = kind === "mains" ? "#f5ad4d" : "#ffe27a";
ctx.stroke();
ctx.save();
ctx.translate(76, 118);
if (kind === "mains") {
// Plug prongs.
ctx.strokeStyle = "#ffd074";
ctx.lineWidth = 10;
ctx.lineCap = "square";
ctx.beginPath();
ctx.moveTo(-20, -20);
ctx.lineTo(-20, -53);
ctx.moveTo(20, -20);
ctx.lineTo(20, -53);
ctx.stroke();
// Plug body.
roundedRect(ctx, -38, -22, 76, 52, 10);
ctx.fillStyle = "#f3a044";
ctx.fill();
// Cord.
ctx.strokeStyle = "#ffd074";
ctx.lineWidth = 10;
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(0, 30);
ctx.bezierCurveTo(0, 55, -34, 52, -34, 75);
ctx.stroke();
} else {
// Battery outline.
ctx.strokeStyle = "#ffe27a";
ctx.lineWidth = 9;
roundedRect(ctx, -42, -29, 76, 58, 7);
ctx.stroke();
ctx.fillStyle = "#ffe27a";
ctx.fillRect(35, -12, 10, 24);
// Lightning bolt.
ctx.beginPath();
ctx.moveTo(7, -25);
ctx.lineTo(-17, 4);
ctx.lineTo(0, 4);
ctx.lineTo(-8, 27);
ctx.lineTo(21, -7);
ctx.lineTo(4, -7);
ctx.closePath();
ctx.fill();
}
ctx.restore();
ctx.fillStyle = "#f4f8fb";
ctx.font = "700 34px system-ui, sans-serif";
ctx.textBaseline = "middle";
ctx.fillText(kind === "mains" ? "MAINS" : "BATTERY", 136, 101);
ctx.fillStyle = "#9fb1bf";
ctx.font = "600 23px system-ui, sans-serif";
ctx.fillText(
kind === "mains" ? "WALL POWER" : "INDEPENDENT",
136,
143
);
const texture = new THREE.CanvasTexture(badgeCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.62, 0.76, 1);
sprite.renderOrder = 20;
return sprite;
}
const mainsBadge = createSourceBadge("mains");
mainsBadge.position.set(-1.97, 1.97, 0.78);
scene.add(mainsBadge);
const batteryBadge = createSourceBadge("battery");
batteryBadge.position.set(2.02, 2.00, 0.78);
scene.add(batteryBadge);
// A small wall label makes the physical switch's purpose explicit.
function createWallLabel() {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 8, 8, 496, 112, 18);
ctx.fillStyle = "rgba(14, 24, 32, 0.88)";
ctx.fill();
ctx.fillStyle = "#dbe8ee";
ctx.font = "700 30px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("ROOM SUPPLY", 256, 66);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true
})
);
sprite.scale.set(1.48, 0.37, 1);
sprite.position.set(-3.43, 2.52, 0.35);
scene.add(sprite);
}
createWallLabel();
// -------------------------------------------------------------------------
// Ambient lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0x9bbbd1, 0x3b2b22, 0.48);
scene.add(hemisphere);
const directional = new THREE.DirectionalLight(0xdcecff, 0.72);
directional.position.set(4.5, 7, 5.5);
directional.castShadow = true;
directional.shadow.mapSize.set(1536, 1536);
directional.shadow.camera.left = -6;
directional.shadow.camera.right = 6;
directional.shadow.camera.top = 6;
directional.shadow.camera.bottom = -6;
directional.shadow.camera.near = 0.5;
directional.shadow.camera.far = 18;
directional.shadow.bias = -0.002;
scene.add(directional);
// -------------------------------------------------------------------------
// Supply logic
// -------------------------------------------------------------------------
let supplyOn = true;
function setSupply(on) {
supplyOn = on;
// Only the mains-powered table lamp responds to room supply.
lampLight.intensity = on ? 65 : 0;
lampBulbMaterial.emissiveIntensity = on ? 5 : 0.015;
lampShadeMaterial.emissiveIntensity = on ? 0.18 : 0;
rocker.rotation.z = on ? -0.28 : 0.28;
rockerMaterial.color.setHex(on ? 0x65d6a7 : 0xff705b);
rockerMaterial.emissive.setHex(on ? 0x0d3b28 : 0x401007);
switchLedMaterial.color.setHex(on ? 0x65ffb4 : 0xff705b);
switchLedMaterial.emissive.setHex(on ? 0x2aff99 : 0xff3f25);
supplyToggle.textContent = on ? "ON" : "OFF";
supplyToggle.classList.toggle("off", !on);
supplyToggle.setAttribute("aria-pressed", String(on));
lampState.textContent = on ? "ON" : "OFF";
lampState.classList.toggle("off", !on);
// The battery lantern deliberately remains on and unchanged.
lanternState.textContent = "ON";
lanternState.classList.remove("off");
liveStatus.textContent = on
? "Room supply on. Table lamp and battery lantern are both on."
: "Room supply off. The table lamp is off; the battery lantern remains on.";
}
supplyToggle.addEventListener("click", () => {
setSupply(!supplyOn);
});
setSupply(true);
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit and wheel to zoom
// -------------------------------------------------------------------------
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let dragDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
dragDistance = 0;
canvas.setPointerCapture(pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
dragDistance += Math.abs(dx) + Math.abs(dy);
previousX = event.clientX;
previousY = event.clientY;
cameraTheta = THREE.MathUtils.clamp(
cameraTheta - dx * 0.006,
0.16,
1.13
);
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.73,
1.36
);
updateCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function clickedWallSwitch(event) {
const rect = canvas.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);
return raycaster.intersectObject(switchHitTarget, false).length > 0;
}
function endPointer(event) {
if (!dragging || event.pointerId !== pointerId) return;
if (dragDistance < 6 && clickedWallSwitch(event)) {
setSupply(!supplyOn);
}
dragging = false;
if (canvas.hasPointerCapture(pointerId)) {
canvas.releasePointerCapture(pointerId);
}
pointerId = null;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.007,
6.4,
12.0
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```