Prompt specification
Build or customize the Three.js 3D scene "Directed light preserves walkway" 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: Directed light preserves walkway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/directed-light-preserves-walkway-x0518
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring night-market lighting. Rotate shaded lamps toward the counter and compare spill onto the pedestrian path; use visible cone guides to explain where the light is being directed. Voxel style.
Working Three.js Starter (directed-light-preserves-walkway-x0518):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Night Market Light Lab</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf4ff;
--muted: #91a5b8;
--panel: rgba(8, 16, 29, 0.86);
--line: rgba(170, 205, 235, 0.16);
--amber: #ffb55f;
--cyan: #69e5ed;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07101c;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 26px;
left: 28px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 16px #000;
}
.eyebrow {
margin-bottom: 7px;
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 38px);
line-height: 0.95;
letter-spacing: -0.055em;
}
.brand p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
}
.panel {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: min(330px, calc(100vw - 32px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(23, 37, 56, 0.93), var(--panel));
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.45),
inset 0 1px rgba(255, 255, 255, 0.045);
backdrop-filter: blur(18px);
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.panel-title strong {
font-size: 13px;
letter-spacing: 0.02em;
}
.voxel-badge {
padding: 5px 7px;
border: 1px solid rgba(105, 229, 237, 0.25);
border-radius: 6px;
color: var(--cyan);
background: rgba(105, 229, 237, 0.07);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 18px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 10px;
padding: 10px 12px;
color: #c8d6e4;
background: rgba(255, 255, 255, 0.035);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
color: white;
border-color: rgba(255, 255, 255, 0.28);
background: rgba(255, 255, 255, 0.07);
}
button:active {
transform: translateY(1px);
}
button.active {
color: #121b24;
border-color: var(--amber);
background: var(--amber);
box-shadow: 0 5px 20px rgba(255, 181, 95, 0.18);
}
button[data-aim="1"].active {
border-color: var(--cyan);
background: var(--cyan);
}
#compareButton.active {
color: #07101c;
border-color: #d7e9ff;
background: #d7e9ff;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #70869b;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 0;
border-radius: 999px;
outline: none;
appearance: none;
background:
linear-gradient(
90deg,
var(--amber) 0%,
var(--amber) var(--value, 0%),
var(--cyan) var(--value, 0%),
var(--cyan) 100%
);
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #172434;
border-radius: 5px;
appearance: none;
background: white;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.22);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #172434;
border-radius: 5px;
background: white;
}
#aimReadout {
margin-top: 9px;
color: #d9e7f4;
font-size: 11px;
text-align: right;
}
.divider {
height: 1px;
margin: 17px 0;
background: var(--line);
}
#compareButton {
width: 100%;
margin-bottom: 16px;
}
.metric {
margin-top: 12px;
}
.metric-header {
display: flex;
justify-content: space-between;
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.metric-header b {
color: var(--ink);
font-size: 11px;
}
.track {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.075);
}
.fill {
width: 0;
height: 100%;
border-radius: inherit;
transition: width 180ms ease;
}
#pathFill {
background: var(--cyan);
box-shadow: 0 0 12px rgba(105, 229, 237, 0.6);
}
#counterFill {
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 181, 95, 0.5);
}
.panel-note {
margin: 15px 0 0;
color: #74899e;
font-size: 10px;
line-height: 1.55;
}
.legend {
display: flex;
gap: 14px;
margin-top: 14px;
color: #879caf;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 2px;
}
.dot.amber {
background: var(--amber);
}
.dot.cyan {
background: var(--cyan);
}
#abStats {
display: none;
gap: 8px;
margin-top: 13px;
}
#abStats.visible {
display: grid;
}
.ab-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
background: rgba(255, 255, 255, 0.025);
font-size: 10px;
}
.ab-row b {
color: var(--ink);
}
.hint {
position: fixed;
z-index: 5;
bottom: 22px;
left: 28px;
display: flex;
align-items: center;
gap: 9px;
color: #8397a9;
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
.mouse {
position: relative;
width: 17px;
height: 25px;
border: 1px solid #7890a5;
border-radius: 9px;
}
.mouse::before {
position: absolute;
top: 5px;
left: 7px;
width: 2px;
height: 5px;
border-radius: 2px;
background: #b5c6d6;
content: "";
}
.world-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid currentColor;
border-radius: 5px;
background: rgba(5, 12, 22, 0.76);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.35);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
pointer-events: none;
text-transform: uppercase;
will-change: transform;
}
#counterLabel {
color: var(--amber);
}
#pathLabel {
color: var(--cyan);
}
#compareOverlay {
position: fixed;
z-index: 6;
inset: 0;
display: none;
pointer-events: none;
}
#compareOverlay.visible {
display: block;
}
.compare-divider {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: rgba(220, 239, 255, 0.3);
box-shadow: 0 0 22px rgba(0, 0, 0, 0.8);
}
.compare-tag {
position: absolute;
top: 90px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(7, 16, 28, 0.82);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.35);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#compareA {
left: calc(25% - 68px);
color: var(--amber);
}
#compareB {
left: calc(75% - 68px);
color: var(--cyan);
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.panel-note,
.legend,
.hint {
display: none;
}
.compare-tag {
top: 72px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="brand">
<div class="eyebrow">Voxel lighting study</div>
<h1>Night Market<br>Light Lab</h1>
<p>Turn the shaded lamps. Watch the spill change.</p>
</header>
<section class="panel" aria-label="Lamp controls">
<div class="panel-title">
<strong>Lamp direction</strong>
<span class="voxel-badge">3 pixel lamps</span>
</div>
<div class="control-label">
<span>Quick aim</span>
<span>Shaded heads</span>
</div>
<div class="segmented">
<button class="active" data-aim="0">Counter</button>
<button data-aim="1">Pedestrian path</button>
</div>
<div class="control-label">
<span>Rotation</span>
<span>Continuous</span>
</div>
<input
id="aimSlider"
type="range"
min="0"
max="100"
value="0"
aria-label="Rotate lamps from counter toward pedestrian path"
>
<div class="range-labels">
<span>Counter</span>
<span>Path</span>
</div>
<div id="aimReadout">0% toward path</div>
<div class="divider"></div>
<button id="compareButton">Compare counter vs. path</button>
<div id="liveStats">
<div class="metric">
<div class="metric-header">
<span>Spill on pedestrian path</span>
<b id="pathValue">0%</b>
</div>
<div class="track"><div class="fill" id="pathFill"></div></div>
</div>
<div class="metric">
<div class="metric-header">
<span>Spill on counter</span>
<b id="counterValue">0%</b>
</div>
<div class="track"><div class="fill" id="counterFill"></div></div>
</div>
</div>
<div id="abStats">
<div class="ab-row">
<span>A · Counter aim → path</span>
<b id="statA">0%</b>
</div>
<div class="ab-row">
<span>B · Path aim → path</span>
<b id="statB">0%</b>
</div>
</div>
<p class="panel-note">
The translucent pyramid is the visible light cone. Floor and counter
voxels brighten according to the estimated spill.
</p>
<div class="legend">
<span><i class="dot amber"></i> Counter zone</span>
<span><i class="dot cyan"></i> Path zone</span>
</div>
</section>
<div id="counterLabel" class="world-label">Counter zone</div>
<div id="pathLabel" class="world-label">Pedestrian path</div>
<div id="compareOverlay">
<div class="compare-divider"></div>
<div id="compareA" class="compare-tag">A · Counter aim</div>
<div id="compareB" class="compare-tag">B · Path aim</div>
</div>
<div class="hint">
<span class="mouse"></span>
Drag to orbit · Wheel to zoom · Double-click to reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101c);
scene.fog = new THREE.Fog(0x07101c, 19, 42);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
47,
window.innerWidth / window.innerHeight,
0.1,
80
);
const clock = new THREE.Clock();
const cameraTarget = new THREE.Vector3(0, 2.1, -0.5);
const cameraState = {
azimuth: 0.58,
elevation: 0.47,
radius: 25
};
const appState = {
aim: 0,
targetAim: 0,
draggingSlider: false,
compare: false
};
const amber = new THREE.Color(0xffb55f);
const cyan = new THREE.Color(0x69e5ed);
const lampColor = new THREE.Color(0xffbd72);
const tileRecords = {
path: [],
counter: []
};
const lamps = [];
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.06,
flatShading: true,
...options
});
}
const mats = {
ground: material(0x151e29),
path: material(0x293745),
pathEdge: material(0x111923),
wood: material(0x69412b),
darkWood: material(0x39251e),
wall: material(0x202d3d),
roof: material(0x263b4b),
metal: material(0x20313b, {
roughness: 0.42,
metalness: 0.72
}),
lampShell: material(0x27404b, {
roughness: 0.42,
metalness: 0.65
}),
produceA: material(0xdf6a3f),
produceB: material(0xd8b84f),
produceC: material(0x6cae68)
};
function box(
width,
height,
depth,
mat,
x,
y,
z,
parent = scene,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function makeOutline(width, depth, color, x, y, z) {
const mat = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.75
});
box(width, 0.035, 0.045, mat, x, y, z - depth / 2, scene, false, false);
box(width, 0.035, 0.045, mat, x, y, z + depth / 2, scene, false, false);
box(0.045, 0.035, depth, mat, x - width / 2, y, z, scene, false, false);
box(0.045, 0.035, depth, mat, x + width / 2, y, z, scene, false, false);
}
function signTexture(text, background = "#152536", foreground = "#ffd28b") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = foreground;
ctx.lineWidth = 5;
ctx.strokeRect(10, 10, canvas.width - 20, canvas.height - 20);
ctx.fillStyle = foreground;
ctx.font = "800 54px sans-serif";
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.LinearFilter;
return texture;
}
function makeSign(text, x, y, z, width = 3.4) {
const tex = signTexture(text);
const mat = new THREE.MeshBasicMaterial({ map: tex });
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 0.25),
mat
);
sign.position.set(x, y, z);
scene.add(sign);
return sign;
}
// Night lighting.
scene.add(new THREE.HemisphereLight(0x7e9dc4, 0x11141c, 0.72));
scene.add(new THREE.AmbientLight(0x36506d, 0.24));
const moon = new THREE.DirectionalLight(0x91adff, 0.82);
moon.position.set(-9, 15, 10);
moon.castShadow = true;
moon.shadow.mapSize.set(1024, 1024);
moon.shadow.camera.left = -16;
moon.shadow.camera.right = 16;
moon.shadow.camera.top = 14;
moon.shadow.camera.bottom = -12;
moon.shadow.camera.far = 40;
moon.shadow.bias = -0.0004;
scene.add(moon);
// Ground and pedestrian paving.
box(28, 0.5, 16, mats.ground, 0, -0.27, 0, scene, false, true);
box(27.2, 0.16, 5.1, mats.pathEdge, 0, -0.02, 1.1, scene, false, true);
const pathBaseColors = [0x283744, 0x2d3d49, 0x23323f];
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 20; col++) {
const x = -11.85 + col * 1.25;
const z = 0.55 + row * 1.28;
const baseColor = new THREE.Color(
pathBaseColors[(row * 3 + col) % pathBaseColors.length]
);
const mat = material(baseColor, {
emissive: 0xffa955,
emissiveIntensity: 0
});
const tile = box(
1.16,
0.1,
1.16,
mat,
x,
0.055,
z,
scene,
false,
true
);
tileRecords.path.push({
mesh: tile,
point: new THREE.Vector3(x, 0.12, z),
baseColor
});
}
}
makeOutline(25.2, 3.1, 0x48dbe5, 0, 0.14, 1.18);
// Background buildings and windows.
const buildingMat = material(0x182535);
const buildingMat2 = material(0x213143);
const windowMats = [
new THREE.MeshBasicMaterial({ color: 0xe9a75c }),
new THREE.MeshBasicMaterial({ color: 0x52788f }),
new THREE.MeshBasicMaterial({ color: 0x39546a })
];
for (let i = 0; i < 8; i++) {
const x = -14 + i * 4;
const height = 5.8 + ((i * 7) % 5) * 0.85;
const mat = i % 2 ? buildingMat : buildingMat2;
box(3.8, height, 3.2, mat, x, height / 2 - 0.1, -7.1, scene, true, true);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 2; col++) {
if ((row + col + i) % 3 === 0) continue;
box(
0.45,
0.55,
0.035,
windowMats[(i + row + col) % windowMats.length],
x - 0.75 + col * 1.5,
1.4 + row * 1.45,
-5.47,
scene,
false,
false
);
}
}
}
// Market stalls.
const stallXs = [-7, 0, 7];
const stallNames = ["NOODLES", "TEA BAR", "FRUIT BOX"];
stallXs.forEach((x, stallIndex) => {
box(4.1, 1.25, 1.35, mats.wood, x, 0.62, -3.2, scene, true, true);
box(4.35, 0.18, 1.6, mats.darkWood, x, 1.32, -3.15, scene, true, true);
box(0.16, 3.3, 0.16, mats.metal, x - 2, 1.65, -3.9);
box(0.16, 3.3, 0.16, mats.metal, x + 2, 1.65, -3.9);
// Striped voxel awning.
for (let stripe = 0; stripe < 6; stripe++) {
const stripeMat =
stripe % 2 === 0
? material(0xc86442)
: material(0xe5d5b8);
box(
0.72,
0.14,
2.2,
stripeMat,
x - 1.8 + stripe * 0.72,
3.25,
-3.1,
scene,
true,
true
);
}
makeSign(stallNames[stallIndex], x, 4.0, -2.54, 3.3);
// Crates and blocky produce.
box(0.65, 0.55, 0.65, mats.darkWood, x - 1.35, 1.68, -3.4);
box(0.65, 0.55, 0.65, mats.darkWood, x + 1.35, 1.68, -3.4);
const produceMats = [
mats.produceA,
mats.produceB,
mats.produceC
];
for (let i = 0; i < 9; i++) {
box(
0.22,
0.22,
0.22,
produceMats[(i + stallIndex) % produceMats.length],
x - 1.55 + (i % 3) * 0.27,
2.08 + Math.floor(i / 9) * 0.2,
-3.45 + Math.floor(i / 3) * 0.28,
scene,
true,
true
);
}
// Counter-surface sample voxels.
for (let ix = 0; ix < 4; ix++) {
for (let iz = 0; iz < 2; iz++) {
const tx = x - 1.35 + ix * 0.9;
const tz = -3.48 + iz * 0.66;
const baseColor = new THREE.Color(
(ix + iz + stallIndex) % 2 ? 0x65432d : 0x755036
);
const mat = material(baseColor, {
emissive: 0xffa955,
emissiveIntensity: 0
});
const tile = box(
0.78,
0.055,
0.52,
mat,
tx,
1.435,
tz,
scene,
false,
true
);
tileRecords.counter.push({
mesh: tile,
point: new THREE.Vector3(tx, 1.47, tz),
baseColor
});
}
}
makeOutline(4.5, 1.85, 0xffa34f, x, 1.46, -3.15);
});
// Overhead gantry.
box(0.22, 6.6, 0.22, mats.metal, -10.6, 3.2, -1.05);
box(0.22, 6.6, 0.22, mats.metal, 10.6, 3.2, -1.05);
box(21.5, 0.2, 0.22, mats.metal, 0, 6.35, -1.05);
// Pixel-style string lights.
const bulbMat = new THREE.MeshBasicMaterial({ color: 0xffd185 });
for (let stringIndex = 0; stringIndex < 3; stringIndex++) {
const z = -1.75 + stringIndex * 0.38;
const points = [];
for (let i = 0; i <= 20; i++) {
const x = -11 + i * 1.1;
const y = 4.82 - Math.sin((i / 20) * Math.PI) * 0.48;
points.push(new THREE.Vector3(x, y, z));
if (i % 2 === 0) {
box(
0.11,
0.14,
0.11,
bulbMat,
x,
y - 0.1,
z,
scene,
false,
false
);
}
}
const stringGeo = new THREE.BufferGeometry().setFromPoints(points);
const stringLine = new THREE.Line(
stringGeo,
new THREE.LineBasicMaterial({ color: 0x516171 })
);
scene.add(stringLine);
}
const stringLight = new THREE.PointLight(0xffb76c, 24, 9, 2);
stringLight.position.set(-7, 4.5, -1.5);
scene.add(stringLight);
const stringLight2 = new THREE.PointLight(0xffc57b, 22, 9, 2);
stringLight2.position.set(7, 4.5, -1.5);
scene.add(stringLight2);
// Truncated rectangular lamp shade.
function createShadeGeometry() {
const positions = new Float32Array([
-0.35, 0.18, -0.27,
0.35, 0.18, -0.27,
0.35, 0.18, 0.27,
-0.35, 0.18, 0.27,
-0.78, -0.68, -0.58,
0.78, -0.68, -0.58,
0.78, -0.68, 0.58,
-0.78, -0.68, 0.58
]);
const indices = [
0, 1, 5, 0, 5, 4,
1, 2, 6, 1, 6, 5,
2, 3, 7, 2, 7, 6,
3, 0, 4, 3, 4, 7,
0, 3, 2, 0, 2, 1,
4, 5, 6, 4, 6, 7
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
// Four-sided pyramid cone, pointing along local -Y.
function createGuideGeometry(length, radius) {
const positions = new Float32Array([
0, 0, 0,
-radius, -length, -radius,
radius, -length, -radius,
radius, -length, radius,
-radius, -length, radius
]);
const indices = [
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 1
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createGuideLines(length, radius) {
const points = [
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(-radius, -length, -radius),
new THREE.Vector3(radius, -length, -radius),
new THREE.Vector3(radius, -length, radius),
new THREE.Vector3(-radius, -length, radius)
];
const vertices = [];
for (let i = 0; i < 4; i++) {
vertices.push(points[0], points[i + 1]);
}
vertices.push(
points[1], points[2],
points[2], points[3],
points[3], points[4],
points[4], points[1]
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
return geometry;
}
const shadeGeometry = createShadeGeometry();
const lampXs = [-7, 0, 7];
lampXs.forEach((x, index) => {
const pivot = new THREE.Group();
pivot.position.set(x, 5.72, -1.08);
scene.add(pivot);
// Static suspension.
box(0.075, 0.65, 0.075, mats.metal, x, 6.0, -1.05);
const shade = new THREE.Mesh(shadeGeometry, mats.lampShell);
shade.castShadow = true;
shade.receiveShadow = true;
pivot.add(shade);
const emitterMat = new THREE.MeshBasicMaterial({ color: 0xffd18a });
const emitter = box(
1.16,
0.055,
0.76,
emitterMat,
0,
-0.69,
0,
pivot,
false,
false
);
const rimMat = new THREE.MeshBasicMaterial({ color: 0xffb55f });
box(1.42, 0.045, 0.045, rimMat, 0, -0.64, -0.62, pivot, false, false);
box(1.42, 0.045, 0.045, rimMat, 0, -0.64, 0.62, pivot, false, false);
const spot = new THREE.SpotLight(
0xffbd72,
220,
15,
0.29,
0.42,
1.55
);
spot.castShadow = true;
spot.shadow.mapSize.set(1024, 1024);
spot.shadow.camera.near = 0.15;
spot.shadow.camera.far = 16;
spot.shadow.bias = -0.00015;
spot.shadow.normalBias = 0.025;
const emitterLocal = new THREE.Vector3(0, -0.72, 0);
spot.position.copy(emitterLocal);
pivot.add(spot);
const spotTarget = new THREE.Object3D();
scene.add(spotTarget);
spot.target = spotTarget;
const guideLength = 7.1;
const guideRadius = Math.tan(spot.angle) * guideLength;
const guideMat = new THREE.MeshBasicMaterial({
color: 0xffb55f,
transparent: true,
opacity: 0.095,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const guide = new THREE.Mesh(
createGuideGeometry(guideLength, guideRadius),
guideMat
);
guide.position.copy(emitterLocal);
guide.renderOrder = 3;
pivot.add(guide);
const lineMat = new THREE.LineBasicMaterial({
color: 0xffc476,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const guideLines = new THREE.LineSegments(
createGuideLines(guideLength, guideRadius),
lineMat
);
guideLines.position.copy(emitterLocal);
guideLines.renderOrder = 4;
pivot.add(guideLines);
const counterDirection = new THREE.Vector3(
x + 0.1,
1.48,
-3.13
).sub(pivot.position).normalize();
const pathDirection = new THREE.Vector3(
x + 0.1,
0.1,
1.35
).sub(pivot.position).normalize();
const down = new THREE.Vector3(0, -1, 0);
const counterQuaternion = new THREE.Quaternion().setFromUnitVectors(
down,
counterDirection
);
const pathQuaternion = new THREE.Quaternion().setFromUnitVectors(
down,
pathDirection
);
lamps.push({
index,
pivot,
spot,
spotTarget,
guide,
guideLines,
emitterLocal,
counterQuaternion,
pathQuaternion,
source: new THREE.Vector3(),
direction: new THREE.Vector3()
});
});
// Blocky pedestrians.
const pedestrians = [];
function createPerson(index) {
const group = new THREE.Group();
const shirt = material(
[0xd66c4f, 0x4c8e9c, 0xb69a59, 0x6c78ad, 0x8c6e8f][index % 5]
);
const pants = material(0x253142);
const skin = material([0xc99168, 0x8e5d45, 0xe0b087][index % 3]);
box(0.28, 0.72, 0.31, pants, -0.18, 0.38, 0, group);
box(0.28, 0.72, 0.31, pants, 0.18, 0.38, 0, group);
box(0.72, 0.82, 0.42, shirt, 0, 1.12, 0, group);
box(0.23, 0.74, 0.25, shirt, -0.49, 1.08, 0, group);
box(0.23, 0.74, 0.25, shirt, 0.49, 1.08, 0, group);
box(0.5, 0.48, 0.48, skin, 0, 1.78, 0, group);
box(0.54, 0.18, 0.52, material(0x20202a), 0, 2.02, 0, group);
const scale = 0.78 + (index % 3) * 0.08;
group.scale.setScalar(scale);
group.position.set(
-10 + index * 4.3,
0.1,
0.72 + (index % 2) * 1.12
);
scene.add(group);
pedestrians.push({
group,
speed: 0.32 + index * 0.07,
phase: index * 1.3
});
}
for (let i = 0; i < 6; i++) createPerson(i);
// Illumination estimation over the voxel surfaces.
function contributionAt(point, lamp, direction) {
const toPoint = new THREE.Vector3().subVectors(point, lamp.source);
const distance = toPoint.length();
if (distance < 0.001 || distance > 15) return 0;
toPoint.divideScalar(distance);
const cosine = toPoint.dot(direction);
const edge = Math.cos(lamp.spot.angle);
if (cosine <= edge) return 0;
const cone = THREE.MathUtils.smoothstep(cosine, edge, 1);
const falloff = 1 / (1 + 0.055 * distance * distance);
return cone * falloff * 2.35;
}
function updateTile(record, amount) {
const mat = record.mesh.material;
const clamped = THREE.MathUtils.clamp(amount, 0, 1);
mat.emissive.copy(lampColor);
mat.emissiveIntensity = clamped * 1.65;
mat.color
.copy(record.baseColor)
.lerp(lampColor, clamped * 0.22);
}
function applyLighting(aim) {
const direction = new THREE.Vector3();
const down = new THREE.Vector3(0, -1, 0);
for (const lamp of lamps) {
lamp.pivot.quaternion.slerpQuaternions(
lamp.counterQuaternion,
lamp.pathQuaternion,
aim
);
direction.copy(down).applyQuaternion(lamp.pivot.quaternion);
lamp.direction.copy(direction);
lamp.pivot.updateMatrixWorld(true);
lamp.source.copy(lamp.emitterLocal);
lamp.pivot.localToWorld(lamp.source);
lamp.spotTarget.position
.copy(lamp.source)
.addScaledVector(direction, 8);
const guideColor = amber.clone().lerp(
cyan,
THREE.MathUtils.smoothstep(aim, 0, 1)
);
lamp.guide.material.color.copy(guideColor);
lamp.guideLines.material.color.copy(guideColor);
}
let pathTotal = 0;
let counterTotal = 0;
for (const record of tileRecords.path) {
let amount = 0;
for (const lamp of lamps) {
amount += contributionAt(
record.point,
lamp,
lamp.direction
);
}
amount = THREE.MathUtils.clamp(amount, 0, 1);
pathTotal += amount;
updateTile(record, amount);
}
for (const record of tileRecords.counter) {
let amount = 0;
for (const lamp of lamps) {
amount += contributionAt(
record.point,
lamp,
lamp.direction
);
}
amount = THREE.MathUtils.clamp(amount, 0, 1);
counterTotal += amount;
updateTile(record, amount);
}
return {
path:
pathTotal / Math.max(1, tileRecords.path.length),
counter:
counterTotal / Math.max(1, tileRecords.counter.length)
};
}
// Custom orbit controls; no external control modules.
let pointerDown = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.azimuth -= dx * 0.005;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.004,
0.2,
1.02
);
});
function stopDragging(event) {
pointerDown = false;
if (event.pointerId !== undefined) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
16,
35
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.azimuth = 0.58;
cameraState.elevation = 0.47;
cameraState.radius = 25;
});
function updateCamera(aspect, compare) {
const radius =
cameraState.radius * (compare ? 1.18 : 1);
const horizontal = Math.cos(cameraState.elevation) * radius;
camera.position.set(
Math.sin(cameraState.azimuth) * horizontal,
cameraTarget.y + Math.sin(cameraState.elevation) * radius,
cameraTarget.z + Math.cos(cameraState.azimuth) * horizontal
);
camera.aspect = aspect;
camera.fov = compare ? 70 : 47;
camera.updateProjectionMatrix();
camera.lookAt(cameraTarget);
}
// UI.
const aimSlider = document.querySelector("#aimSlider");
const aimReadout = document.querySelector("#aimReadout");
const compareButton = document.querySelector("#compareButton");
const compareOverlay = document.querySelector("#compareOverlay");
const liveStats = document.querySelector("#liveStats");
const abStats = document.querySelector("#abStats");
const pathFill = document.querySelector("#pathFill");
const counterFill = document.querySelector("#counterFill");
const pathValue = document.querySelector("#pathValue");
const counterValue = document.querySelector("#counterValue");
const statA = document.querySelector("#statA");
const statB = document.querySelector("#statB");
const aimButtons = [
document.querySelector('[data-aim="0"]'),
document.querySelector('[data-aim="1"]')
];
function setAim(value) {
appState.targetAim = THREE.MathUtils.clamp(value, 0, 1);
aimSlider.value = Math.round(appState.targetAim * 100);
}
aimButtons.forEach((button) => {
button.addEventListener("click", () => {
setAim(Number(button.dataset.aim));
});
});
aimSlider.addEventListener("pointerdown", () => {
appState.draggingSlider = true;
});
aimSlider.addEventListener("input", () => {
const value = Number(aimSlider.value) / 100;
appState.targetAim = value;
appState.aim = value;
});
window.addEventListener("pointerup", () => {
appState.draggingSlider = false;
});
compareButton.addEventListener("click", () => {
appState.compare = !appState.compare;
compareButton.classList.toggle("active", appState.compare);
compareButton.textContent = appState.compare
? "Exit comparison"
: "Compare counter vs. path";
compareOverlay.classList.toggle("visible", appState.compare);
liveStats.style.display = appState.compare ? "none" : "block";
abStats.classList.toggle("visible", appState.compare);
});
function updateUI(stats, counterStats, pathStats) {
const sliderPercent = Math.round(appState.aim * 100);
aimSlider.style.setProperty("--value", `${sliderPercent}%`);
aimReadout.textContent = `${sliderPercent}% toward path`;
aimButtons[0].classList.toggle("active", appState.targetAim < 0.5);
aimButtons[1].classList.toggle("active", appState.targetAim >= 0.5);
pathValue.textContent = `${Math.round(stats.path * 100)}%`;
counterValue.textContent = `${Math.round(stats.counter * 100)}%`;
pathFill.style.width = `${stats.path * 100}%`;
counterFill.style.width = `${stats.counter * 100}%`;
statA.textContent = `${Math.round(counterStats.path * 100)}%`;
statB.textContent = `${Math.round(pathStats.path * 100)}%`;
}
const counterLabel = document.querySelector("#counterLabel");
const pathLabel = document.querySelector("#pathLabel");
const annotations = [
{
element: counterLabel,
position: new THREE.Vector3(-7.1, 1.7, -2.5)
},
{
element: pathLabel,
position: new THREE.Vector3(7.5, 0.3, 2.25)
}
];
function positionAnnotations() {
if (appState.compare) {
counterLabel.style.display = "none";
pathLabel.style.display = "none";
return;
}
for (const annotation of annotations) {
const projected = annotation.position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.08 &&
Math.abs(projected.y) < 1.08;
annotation.element.style.display = visible ? "block" : "none";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
annotation.element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -50%)`;
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
let lastCompareCounterStats = { path: 0, counter: 0 };
let lastComparePathStats = { path: 0, counter: 0 };
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!appState.draggingSlider) {
const smoothing = 1 - Math.exp(-dt * 7);
appState.aim +=
(appState.targetAim - appState.aim) * smoothing;
}
for (const person of pedestrians) {
person.group.position.x += person.speed * dt;
person.phase += dt * person.speed * 5;
person.group.position.y =
0.1 + Math.abs(Math.sin(person.phase)) * 0.025;
person.group.children[0].rotation.x =
Math.sin(person.phase) * 0.2;
person.group.children[1].rotation.x =
-Math.sin(person.phase) * 0.2;
if (person.group.position.x > 12.5) {
person.group.position.x = -12.5;
}
}
const width = window.innerWidth;
const height = window.innerHeight;
const halfWidth = Math.floor(width / 2);
if (appState.compare) {
renderer.setScissorTest(true);
// Left: counter aim.
updateCamera(halfWidth / height, true);
lastCompareCounterStats = applyLighting(0);
renderer.setViewport(0, 0, halfWidth, height);
renderer.setScissor(0, 0, halfWidth, height);
renderer.render(scene, camera);
// Right: path aim.
updateCamera((width - halfWidth) / height, true);
lastComparePathStats = applyLighting(1);
renderer.setViewport(halfWidth, 0, width - halfWidth, height);
renderer.setScissor(halfWidth, 0, width - halfWidth, height);
renderer.render(scene, camera);
renderer.setScissorTest(false);
renderer.setViewport(0, 0, width, height);
updateUI(
lastComparePathStats,
lastCompareCounterStats,
lastComparePathStats
);
} else {
updateCamera(width / height, false);
const stats = applyLighting(appState.aim);
renderer.setViewport(0, 0, width, height);
renderer.render(scene, camera);
updateUI(stats, stats, stats);
}
positionAnnotations();
}
animate();
</script>
</body>
</html>
```