Prompt specification
Build or customize the Three.js 3D scene "Compare street and interior sightlines through a screen" 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: Compare street and interior sightlines through a screen
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-street-and-interior-sightlines-through-a-scree-x2992
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Hawa Mahal's screened facade and streets in Jaipur. Let users move a viewing point behind a simplified facade screen. Show which street regions remain visible and separate geometric visibility from claims about historical social practice. Voxel style.
Working Three.js Starter (compare-street-and-interior-sightlines-through-a-scree-x2992):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hawa Mahal — Voxel Sightline Study</title>
<style>
:root {
--ink: #211a1a;
--paper: rgba(255, 244, 221, 0.92);
--line: rgba(54, 34, 32, 0.2);
--pink: #d86670;
--teal: #25d6bd;
--amber: #efb45e;
--blocked: #9c625d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7c49c;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
width: 100%;
height: 100%;
cursor: grab;
outline: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 10px 35px rgba(45, 27, 23, 0.14);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title {
top: 18px;
left: 18px;
width: min(380px, calc(100vw - 36px));
padding: 18px 20px 16px;
border-top: 5px solid var(--pink);
}
.eyebrow {
margin-bottom: 6px;
color: #9a3f48;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 4vw, 42px);
font-weight: 700;
line-height: 0.98;
}
.subtitle {
margin: 8px 0 0;
color: #604c48;
font-size: 13px;
line-height: 1.45;
}
.badge {
display: inline-block;
margin-top: 12px;
padding: 5px 8px;
border: 1px solid rgba(154, 63, 72, 0.3);
background: rgba(216, 102, 112, 0.12);
color: #873d44;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#controls {
top: 18px;
right: 18px;
width: min(350px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 16px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 0 0 10px;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status {
padding: 12px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.35);
}
.status-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.metric strong {
display: block;
font-size: 24px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric span {
display: block;
margin-top: 4px;
color: #69534f;
font-size: 10px;
line-height: 1.25;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.position {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: #604c48;
font-size: 11px;
line-height: 1.5;
}
.position b {
color: var(--ink);
font-variant-numeric: tabular-nums;
}
.control-group {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.control-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
font-size: 11px;
font-weight: 700;
}
.control-label output {
color: #795d58;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--pink);
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
margin: 10px 0;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
.toggle input {
width: 16px;
height: 16px;
accent-color: var(--teal);
}
.buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 10px;
}
button {
min-height: 34px;
border: 1px solid rgba(80, 48, 43, 0.25);
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
touch-action: none;
}
button:hover,
button:active {
background: rgba(216, 102, 112, 0.18);
}
.nudge {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 5px;
margin-top: 8px;
}
.nudge button {
min-width: 0;
padding: 4px;
font-size: 14px;
}
.note {
margin-top: 12px;
padding: 12px;
border-left: 4px solid var(--teal);
background: rgba(37, 214, 189, 0.09);
font-size: 11px;
line-height: 1.55;
}
.history-note {
border-left-color: #9c5372;
background: rgba(156, 83, 114, 0.09);
}
.note strong {
display: block;
margin-bottom: 5px;
font-size: 11px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.note p {
margin: 0;
}
#map-panel {
bottom: 18px;
left: 18px;
width: 300px;
padding: 11px;
}
#map-title {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#map {
width: 100%;
height: auto;
display: block;
border: 1px solid rgba(255, 255, 255, 0.25);
background: #272120;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px 12px;
margin-top: 9px;
font-size: 9px;
line-height: 1.25;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 9px;
height: 9px;
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.18);
}
.swatch.current {
background: var(--teal);
}
.swatch.possible {
background: var(--amber);
}
.swatch.blocked {
background: var(--blocked);
}
#hint {
position: fixed;
z-index: 9;
bottom: 18px;
left: 50%;
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.22);
background: rgba(38, 27, 26, 0.78);
color: #fff3df;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
font-size: 10px;
line-height: 1.35;
text-align: center;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e7c49c;
color: #6b3d40;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#loading[hidden] {
display: none;
}
@media (max-width: 820px) {
#title {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 15px;
}
#title .subtitle,
#title .badge {
display: none;
}
#controls {
top: auto;
right: 10px;
bottom: 10px;
width: min(330px, calc(100vw - 20px));
max-height: 48vh;
}
#map-panel {
display: none;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="title" class="panel">
<div class="eyebrow">Jaipur · Architectural Model</div>
<h1>Hawa Mahal</h1>
<p class="subtitle">
Move an observer behind the simplified façade screen. Teal street cells
are geometrically visible through the modeled jharokha openings in the
current frame.
</p>
<span class="badge">Geometry study · not a historical reconstruction</span>
</section>
<section id="controls" class="panel">
<h2 class="section-title">
<span>Observer / Geometry</span>
<span id="cellTotal"></span>
</h2>
<div class="status">
<div class="status-grid">
<div class="metric">
<strong id="visibleCount">0</strong>
<span>Visible in current frame</span>
</div>
<div class="metric">
<strong id="possibleCount">0</strong>
<span>Additional cells visible if turned</span>
</div>
</div>
<div class="position" id="positionReadout"></div>
</div>
<div class="control-group">
<label class="toggle">
<input id="overlayToggle" type="checkbox" checked>
Show street-cell analysis
</label>
<div class="control-label">
<span>Sideways position</span>
<output id="xOutput"></output>
</div>
<input id="xSlider" type="range" min="-10" max="10" step="0.1" value="0">
<div class="control-label">
<span>Viewing height</span>
<output id="yOutput"></output>
</div>
<input id="ySlider" type="range" min="2" max="16" step="0.1" value="8.5">
<div class="control-label">
<span>Distance behind façade</span>
<output id="zOutput"></output>
</div>
<input id="zSlider" type="range" min="1.2" max="14" step="0.1" value="7.5">
<div class="buttons">
<button id="resetButton" type="button">Reset view</button>
<button id="frontButton" type="button">Face screen</button>
<button id="overButton" type="button">Look down street</button>
</div>
<div class="nudge" aria-label="Viewpoint movement controls">
<button type="button" data-move="KeyA" aria-label="Move left">←</button>
<button type="button" data-move="KeyW" aria-label="Move forward">↑</button>
<button type="button" data-move="KeyD" aria-label="Move right">→</button>
<button type="button" data-move="KeyS" aria-label="Move backward">↓</button>
<button type="button" data-move="KeyQ" aria-label="Move down">−</button>
<button type="button" data-move="KeyE" aria-label="Move up">+</button>
</div>
</div>
<div class="note">
<strong>Geometric result only</strong>
<p>
Each street cell is sampled at a human-height point of 1.4 m. A cell is
marked when a line from the observer passes through an opening in the
simplified façade and is also inside the current camera frame. The
model treats the modeled wall and lattice bars as opaque; street
furniture and people do not alter the sightline test.
</p>
</div>
<div class="note history-note">
<strong>Historical social practice · context only</strong>
<p>
Common interpretations associate Hawa Mahal’s screened openings with
privacy and with royal women observing street processions. That context
is <em>not</em> inferred from this geometric model. The scene does not
establish who used a particular opening, whether a street space was
occupied, or the practical reach of <em>purdah</em>. Those are
historical and social questions requiring period evidence.
</p>
</div>
</section>
<section id="map-panel" class="panel">
<div id="map-title">
<span>Street plan / sightlines</span>
<span>▲ observer</span>
</div>
<canvas id="map" width="320" height="230"></canvas>
<div class="legend">
<span><i class="swatch current"></i>Current frame</span>
<span><i class="swatch possible"></i>LOS if turned</span>
<span><i class="swatch blocked"></i>Screen blocked</span>
</div>
</section>
<div id="hint">
Drag to look · WASD / arrows move · Q/E lower or raise · Wheel dollies ·
Shift moves faster
</div>
<div id="loading">Building the voxel street…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
let renderer;
try {
renderer = new THREE.WebGLRenderer({ antialias: true });
} catch (error) {
loading.textContent = "WebGL could not be initialized";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel model of Hawa Mahal and the Jaipur street"
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7c69e);
scene.fog = new THREE.Fog(0xe7c69e, 38, 108);
const camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
0.1,
180
);
const initialPosition = new THREE.Vector3(0, 8.5, 7.5);
const cameraState = {
yaw: 0,
pitch: -0.09
};
camera.position.copy(initialPosition);
camera.rotation.order = "YXZ";
camera.rotation.set(cameraState.pitch, cameraState.yaw, 0);
const hemisphere = new THREE.HemisphereLight(0xffefd6, 0x684b49, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe3bc, 2.3);
keyLight.position.set(-18, 31, 24);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -38;
keyLight.shadow.camera.right = 38;
keyLight.shadow.camera.top = 38;
keyLight.shadow.camera.bottom = -38;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 100;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const streetLight = new THREE.DirectionalLight(0xfff0d2, 1.05);
streetLight.position.set(20, 20, -30);
scene.add(streetLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.9,
metalness: options.metalness ?? 0,
flatShading: true,
...options
});
}
const materials = {
ground: material(0xd4b37d),
road: material(0xc5a075),
sidewalk: material(0xe1c39b),
facade: material(0xd86670),
facadeDark: material(0xa54450),
facadeLight: material(0xeb7f7d),
trim: material(0xf1c88d),
paleTrim: material(0xf8dfb1),
lattice: material(0x82404a),
interior: material(0x78484a),
window: material(0x49363a),
stone: material(0xc78d70),
cart: material(0x9f6548),
skin: material(0xb87851),
trunk: material(0x6f4935),
leaves: material(0x71835a),
leafLight: material(0x879761)
};
const buildingMaterials = [
material(0xd7a36f),
material(0xe1b47d),
material(0xc9826c),
material(0xe5c18f),
material(0xcc927d),
material(0xb96f66)
];
const awningMaterials = [
material(0xc65e5d),
material(0x4f8790),
material(0xe4a94f),
material(0x6e7650),
material(0x9c5c79)
];
function addBox(parent, mat, size, position, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Ground and street
// ---------------------------------------------------------------------
addBox(scene, materials.ground, [100, 0.4, 110], [0, -0.28, -24], false);
addBox(scene, materials.road, [17.8, 0.12, 70], [0, -0.02, -27], false);
addBox(scene, materials.sidewalk, [4.2, 0.14, 70], [7.1, 0.03, -27], false);
addBox(scene, materials.sidewalk, [4.2, 0.14, 70], [-7.1, 0.03, -27], false);
addBox(scene, materials.sidewalk, [27, 0.14, 3], [0, 0.03, -1.7], false);
// Cross-streets preserve Jaipur's gridded street impression.
for (const z of [-19.5, -37.5]) {
addBox(scene, materials.road, [55, 0.11, 3.4], [0, 0, z], false);
}
// ---------------------------------------------------------------------
// Hawa Mahal: an opaque voxel wall assembled around real openings
// ---------------------------------------------------------------------
const facadeGroup = new THREE.Group();
facadeGroup.name = "Simplified screened facade";
scene.add(facadeGroup);
const FACADE = {
bays: 11,
baySpacing: 2.05,
openingHalf: 0.6,
rows: [],
totalWidth: 11 * 2.05
};
const rowBases = [1, 3.8, 6.6, 9.4, 12.2];
for (const base of rowBases) {
const row = {
base,
bottom: base + 0.62,
top: base + 1.96,
middle: base + 1.26
};
FACADE.rows.push(row);
addBox(
facadeGroup,
materials.facade,
[FACADE.totalWidth, 0.56, 0.72],
[0, base + 0.28, 0]
);
addBox(
facadeGroup,
materials.facadeLight,
[FACADE.totalWidth, 0.48, 0.76],
[0, base + 2.2, 0]
);
for (let i = 0; i < FACADE.bays; i++) {
const center = (i - (FACADE.bays - 1) / 2) * FACADE.baySpacing;
// Side piers leave a genuine 1.2 m wide aperture.
for (const side of [-1, 1]) {
addBox(
facadeGroup,
materials.facade,
[0.42, 1.4, 0.76],
[center + side * (FACADE.openingHalf + 0.21), base + 1.26, 0]
);
addBox(
facadeGroup,
materials.paleTrim,
[0.1, 1.48, 0.82],
[center + side * 0.64, base + 1.26, 0]
);
}
// Pale sill and lintel emphasize the voxel jharokha rhythm.
addBox(
facadeGroup,
materials.trim,
[1.34, 0.12, 0.82],
[center, base + 0.56, 0]
);
addBox(
facadeGroup,
materials.trim,
[1.34, 0.12, 0.82],
[center, base + 2.02, 0]
);
// Simplified opaque jali bars.
for (const barOffset of [-0.22, 0.22]) {
addBox(
facadeGroup,
materials.lattice,
[0.045, 1.38, 0.79],
[center + barOffset, base + 1.26, 0],
false
);
}
addBox(
facadeGroup,
materials.lattice,
[1.18, 0.04, 0.79],
[center, base + 1.26, 0],
false
);
// Small colored inset beneath each opening.
addBox(
facadeGroup,
i % 2 === 0 ? materials.facadeDark : materials.facadeLight,
[0.66, 0.28, 0.79],
[center, base + 0.25, 0]
);
}
}
// Strong outer frame.
for (const side of [-1, 1]) {
addBox(
facadeGroup,
materials.facadeDark,
[0.62, 14.65, 0.95],
[side * 11.47, 8.05, 0]
);
}
// Stepped, block-built crown.
addBox(
facadeGroup,
materials.facadeDark,
[FACADE.totalWidth + 0.8, 0.42, 1.05],
[0, 14.88, 0]
);
for (const x of [-8.2, -4.1, 0, 4.1, 8.2]) {
addBox(facadeGroup, materials.facade, [2.3, 0.8, 1.05], [x, 15.48, 0]);
addBox(facadeGroup, materials.trim, [1.5, 0.32, 1.12], [x, 16.03, 0]);
addBox(facadeGroup, materials.facadeLight, [0.9, 0.62, 0.92], [x, 16.48, 0]);
addBox(facadeGroup, materials.paleTrim, [0.35, 0.45, 0.84], [x, 16.98, 0]);
}
addBox(facadeGroup, materials.facadeDark, [3.2, 0.55, 1.1], [0, 15.3, 0]);
addBox(facadeGroup, materials.trim, [2.1, 0.38, 1.14], [0, 15.75, 0]);
addBox(facadeGroup, materials.facade, [1.2, 0.8, 1.02], [0, 16.32, 0]);
// A darker interior edge makes the observer's side legible.
addBox(scene, materials.interior, [24.2, 0.28, 3.2], [0, 0.25, 3.2]);
// ---------------------------------------------------------------------
// Jaipur street: pastel façades, shops, trees, carts and people
// ---------------------------------------------------------------------
const people = [];
function randomBetween(min, max) {
return min + Math.random() * (max - min);
}
for (const side of [-1, 1]) {
for (let i = 0; i < 8; i++) {
const z = -5.5 - i * 6.65;
const width = randomBetween(7.2, 9.2);
const depth = randomBetween(5.1, 6.1);
const height = randomBetween(4.6, 9.1);
const x = side * randomBetween(17.2, 19.2);
const mat = buildingMaterials[(i + (side > 0 ? 2 : 0)) % buildingMaterials.length];
addBox(scene, mat, [width, height, depth], [x, height / 2, z]);
addBox(
scene,
materials.facadeLight,
[width + 0.3, 0.26, depth + 0.3],
[x, height + 0.13, z]
);
// Street-facing windows are shallow voxel recesses.
const innerX = x - side * (width / 2 + 0.025);
for (let y = 1.45; y < height - 0.55; y += 1.55) {
for (const dz of [-1.55, 0, 1.55]) {
addBox(
scene,
materials.window,
[0.09, 0.68, 0.68],
[innerX, y, z + dz],
false
);
}
}
if (i % 3 === 0) {
addBox(
scene,
materials.trim,
[0.62, 0.24, 2.25],
[innerX - side * 0.15, 2.85, z],
false
);
}
// Rooftop parapet blocks and a small water tank.
addBox(
scene,
materials.stone,
[1.4, 0.65, 1.2],
[x + side * 1.4, height + 0.55, z + 1.1]
);
}
}
// Market stalls and striped, block-built awnings.
for (const side of [-1, 1]) {
for (let i = 0; i < 7; i++) {
const x = side * 6.7;
const z = -8 - i * 6.45;
const awning = awningMaterials[(i + (side > 0 ? 2 : 0)) % awningMaterials.length];
addBox(scene, materials.cart, [1.45, 1.1, 2.15], [x, 0.58, z]);
addBox(scene, awning, [2.15, 0.14, 2.75], [x, 2.62, z]);
for (const dx of [-0.82, 0.82]) {
addBox(scene, materials.trunk, [0.09, 2.55, 0.09], [x + dx, 1.3, z], false);
}
for (let stripe = 0; stripe < 4; stripe++) {
addBox(
scene,
stripe % 2 === 0 ? awning : materials.paleTrim,
[0.53, 0.045, 2.72],
[x - 0.81 + stripe * 0.54, 2.72, z],
false
);
}
for (let item = 0; item < 3; item++) {
addBox(
scene,
item % 2 ? materials.trim : buildingMaterials[(i + item) % buildingMaterials.length],
[0.28, 0.28, 0.3],
[x - 0.4 + item * 0.4, 1.28, z - 0.45 + (item % 2) * 0.45],
false
);
}
}
}
// Voxel trees.
for (const side of [-1, 1]) {
for (const z of [-11, -27, -43]) {
const x = side * 10.2;
addBox(scene, materials.trunk, [0.38, 2.5, 0.38], [x, 1.25, z]);
addBox(scene, materials.leaves, [2.4, 1.8, 2.2], [x, 3.15, z]);
addBox(scene, materials.leafLight, [1.65, 1.25, 1.6], [x + 0.35, 4.1, z - 0.2]);
}
}
// Street lamps.
for (const side of [-1, 1]) {
for (let i = 0; i < 6; i++) {
const z = -7 - i * 8.2;
const x = side * 5.55;
addBox(scene, materials.lattice, [0.13, 3.3, 0.13], [x, 1.65, z], false);
addBox(scene, materials.trim, [0.48, 0.2, 0.48], [x, 3.35, z], false);
}
}
// A handcart.
addBox(scene, materials.cart, [1.8, 0.45, 2.5], [3.25, 0.8, -14.5]);
addBox(scene, materials.trim, [1.65, 0.65, 1.2], [3.25, 1.35, -14.7]);
for (const x of [2.65, 3.85]) {
for (const z of [-15.25, -13.75]) {
addBox(scene, materials.lattice, [0.25, 0.55, 0.55], [x, 0.42, z], false);
}
}
addBox(scene, materials.trunk, [0.13, 0.13, 2.2], [3.25, 1.15, -12.9], false);
// Simplified pedestrians.
const clothingMaterials = [
material(0x4f7188),
material(0xa95158),
material(0xcf9b4c),
material(0x68755a),
material(0x765479),
material(0xd4b680)
];
for (let i = 0; i < 22; i++) {
const group = new THREE.Group();
const x = randomBetween(-4.7, 4.7);
const z = randomBetween(-5.5, -51);
const clothing = clothingMaterials[i % clothingMaterials.length];
addBox(group, clothing, [0.48, 0.85, 0.36], [0, 0.92, 0], false);
addBox(group, materials.skin, [0.34, 0.34, 0.34], [0, 1.52, 0], false);
addBox(group, materials.lattice, [0.48, 0.22, 0.38], [0, 1.77, 0], false);
addBox(group, materials.lattice, [0.16, 0.48, 0.18], [-0.14, 0.25, 0], false);
addBox(group, materials.lattice, [0.16, 0.48, 0.18], [0.14, 0.25, 0], false);
group.position.set(x, 0, z);
group.rotation.y = randomBetween(-0.4, 0.4);
scene.add(group);
people.push({
group,
baseY: 0,
phase: randomBetween(0, Math.PI * 2)
});
}
// ---------------------------------------------------------------------
// Street-cell visibility layer
// ---------------------------------------------------------------------
const cells = [];
const tileGeometry = new THREE.BoxGeometry(1.88, 0.035, 1.88);
const tileMaterial = new THREE.MeshBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.58,
depthWrite: false
});
for (let x = -14; x <= 14; x += 2) {
for (let z = -4; z >= -52; z -= 2) {
cells.push({
x,
z,
target: new THREE.Vector3(x, 1.4, z),
state: 0
});
}
}
const tileMesh = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
cells.length
);
tileMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
tileMesh.frustumCulled = false;
tileMesh.renderOrder = 2;
const matrix = new THREE.Matrix4();
cells.forEach((cell, index) => {
matrix.makeTranslation(cell.x, 0.145, cell.z);
tileMesh.setMatrixAt(index, matrix);
tileMesh.setColorAt(index, new THREE.Color(0x9c625d));
});
scene.add(tileMesh);
const cellColors = {
blocked: new THREE.Color(0x9c625d),
possible: new THREE.Color(0xefb45e),
visible: new THREE.Color(0x25d6bd)
};
const projected = new THREE.Vector3();
const toTarget = new THREE.Vector3();
const cameraForward = new THREE.Vector3();
function lineOfSightPasses(target) {
const origin = camera.position;
const denominator = origin.z - target.z;
if (denominator <= 0.01) {
return false;
}
// Intersection of the observer-to-sample ray with the façade plane z = 0.
const t = origin.z / denominator;
const crossingX = origin.x + (target.x - origin.x) * t;
const crossingY = origin.y + (target.y - origin.y) * t;
// Beyond the ends or above the modeled screen is geometrically open.
if (
crossingX < -11.78 ||
crossingX > 11.78 ||
crossingY < 0.72 ||
crossingY > 15.7
) {
return true;
}
for (const row of FACADE.rows) {
if (crossingY < row.bottom || crossingY > row.top) {
continue;
}
const rawBay = Math.round(
crossingX / FACADE.baySpacing + (FACADE.bays - 1) / 2
);
if (rawBay < 0 || rawBay >= FACADE.bays) {
continue;
}
const center =
(rawBay - (FACADE.bays - 1) / 2) * FACADE.baySpacing;
const localX = crossingX - center;
if (Math.abs(localX) <= FACADE.openingHalf) {
// Opaque modeled lattice bars.
const hitsVerticalBar =
Math.abs(Math.abs(localX) - 0.22) < 0.032;
const hitsHorizontalBar = Math.abs(crossingY - row.middle) < 0.029;
return !(hitsVerticalBar || hitsHorizontalBar);
}
}
// The ray meets opaque wall.
return false;
}
function targetIsInCurrentFrame(target) {
camera.getWorldDirection(cameraForward);
toTarget.copy(target).sub(camera.position);
if (cameraForward.dot(toTarget) <= 0) {
return false;
}
projected.copy(target).project(camera);
return (
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) <= 0.985 &&
Math.abs(projected.y) <= 0.985
);
}
let visibleCount = 0;
let possibleCount = 0;
let visibilityDirty = true;
function updateVisibility() {
camera.updateMatrixWorld(true);
visibleCount = 0;
possibleCount = 0;
cells.forEach((cell, index) => {
const hasLOS = lineOfSightPasses(cell.target);
const inFrame = hasLOS && targetIsInCurrentFrame(cell.target);
if (inFrame) {
cell.state = 2;
visibleCount++;
} else if (hasLOS) {
cell.state = 1;
possibleCount++;
} else {
cell.state = 0;
}
const color =
cell.state === 2
? cellColors.visible
: cell.state === 1
? cellColors.possible
: cellColors.blocked;
tileMesh.setColorAt(index, color);
});
if (tileMesh.instanceColor) {
tileMesh.instanceColor.needsUpdate = true;
}
document.getElementById("visibleCount").textContent = visibleCount;
document.getElementById("possibleCount").textContent = possibleCount;
drawMap();
}
// ---------------------------------------------------------------------
// Minimap
// ---------------------------------------------------------------------
const mapCanvas = document.getElementById("map");
const mapContext = mapCanvas.getContext("2d");
const mapBounds = {
minX: -16,
maxX: 16,
minZ: -54,
maxZ: 9
};
function mapX(x) {
return ((x - mapBounds.minX) / (mapBounds.maxX - mapBounds.minX)) *
mapCanvas.width;
}
function mapY(z) {
return ((z - mapBounds.maxZ) / (mapBounds.minZ - mapBounds.maxZ)) *
mapCanvas.height;
}
function drawMap() {
const ctx = mapContext;
const width = mapCanvas.width;
const height = mapCanvas.height;
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "#292321";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#745d4f";
ctx.fillRect(
mapX(-8),
mapY(-54),
mapX(8) - mapX(-8),
mapY(-3) - mapY(-54)
);
ctx.fillStyle = "#9c8067";
for (const z of [-19.5, -37.5]) {
ctx.fillRect(
mapX(-15),
mapY(z + 1.7),
mapX(15) - mapX(-15),
mapY(z - 1.7) - mapY(z + 1.7)
);
}
if (document.getElementById("overlayToggle").checked) {
for (const cell of cells) {
ctx.fillStyle =
cell.state === 2
? "#25d6bd"
: cell.state === 1
? "#efb45e"
: "#8a5b56";
ctx.fillRect(
mapX(cell.x - 0.9),
mapY(cell.z + 0.9),
mapX(cell.x + 0.9) - mapX(cell.x - 0.9),
mapY(cell.z - 0.9) - mapY(cell.z + 0.9)
);
}
}
// Façade line.
ctx.fillStyle = "#e47b7d";
ctx.fillRect(mapX(-11.7), mapY(0) - 3, mapX(11.7) - mapX(-11.7), 6);
// Camera cone.
const px = mapX(camera.position.x);
const py = mapY(camera.position.z);
const angle = Math.atan2(Math.cos(cameraState.yaw), -Math.sin(cameraState.yaw));
const halfAngle = THREE.MathUtils.degToRad(27);
const coneLength = 74;
ctx.beginPath();
ctx.moveTo(px, py);
ctx.lineTo(
px + Math.cos(angle - halfAngle) * coneLength,
py + Math.sin(angle - halfAngle) * coneLength
);
ctx.lineTo(
px + Math.cos(angle + halfAngle) * coneLength,
py + Math.sin(angle + halfAngle) * coneLength
);
ctx.closePath();
ctx.fillStyle = "rgba(255, 239, 205, 0.16)";
ctx.fill();
ctx.beginPath();
ctx.arc(px, py, 5, 0, Math.PI * 2);
ctx.fillStyle = "#fff1d3";
ctx.fill();
ctx.strokeStyle = "#fff1d3";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(px, py);
ctx.lineTo(
px + Math.cos(angle) * 12,
py + Math.sin(angle) * 12
);
ctx.stroke();
}
// ---------------------------------------------------------------------
// Direct camera controls, implemented without Three.js add-ons
// ---------------------------------------------------------------------
const keys = Object.create(null);
const xSlider = document.getElementById("xSlider");
const ySlider = document.getElementById("ySlider");
const zSlider = document.getElementById("zSlider");
const xOutput = document.getElementById("xOutput");
const yOutput = document.getElementById("yOutput");
const zOutput = document.getElementById("zOutput");
const positionReadout = document.getElementById("positionReadout");
function syncPositionUI() {
xSlider.value = camera.position.x.toFixed(1);
ySlider.value = camera.position.y.toFixed(1);
zSlider.value = camera.position.z.toFixed(1);
xOutput.textContent = `${camera.position.x.toFixed(1)} m`;
yOutput.textContent = `${camera.position.y.toFixed(1)} m`;
zOutput.textContent = `${camera.position.z.toFixed(1)} m`;
positionReadout.innerHTML =
`Observer <b>x ${camera.position.x.toFixed(1)} m</b> · ` +
`<b>y ${camera.position.y.toFixed(1)} m</b> · ` +
`<b>${camera.position.z.toFixed(1)} m behind façade</b>`;
}
function positionFromSliders() {
camera.position.set(
Number(xSlider.value),
Number(ySlider.value),
Number(zSlider.value)
);
syncPositionUI();
visibilityDirty = true;
}
xSlider.addEventListener("input", positionFromSliders);
ySlider.addEventListener("input", positionFromSliders);
zSlider.addEventListener("input", positionFromSliders);
function resetView() {
camera.position.copy(initialPosition);
cameraState.yaw = 0;
cameraState.pitch = -0.09;
syncPositionUI();
visibilityDirty = true;
}
document.getElementById("resetButton").addEventListener("click", resetView);
document.getElementById("frontButton").addEventListener("click", () => {
cameraState.yaw = 0;
cameraState.pitch = -0.09;
visibilityDirty = true;
});
document.getElementById("overButton").addEventListener("click", () => {
cameraState.yaw = 0;
cameraState.pitch = -0.24;
visibilityDirty = true;
});
document.getElementById("overlayToggle").addEventListener("change", (event) => {
tileMesh.visible = event.target.checked;
drawMap();
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
if (
["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Space"].includes(event.code)
) {
event.preventDefault();
}
keys[event.code] = true;
});
window.addEventListener("keyup", (event) => {
keys[event.code] = false;
});
window.addEventListener("blur", () => {
for (const key of Object.keys(keys)) {
keys[key] = false;
}
});
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.yaw -= dx * 0.004;
cameraState.pitch -= dy * 0.0034;
cameraState.pitch = THREE.MathUtils.clamp(cameraState.pitch, -1.12, 0.85);
visibilityDirty = true;
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const forward = new THREE.Vector3(
-Math.sin(cameraState.yaw),
0,
-Math.cos(cameraState.yaw)
);
camera.position.addScaledVector(forward, -event.deltaY * 0.012);
camera.position.z = THREE.MathUtils.clamp(camera.position.z, 1.2, 14);
camera.position.x = THREE.MathUtils.clamp(camera.position.x, -10, 10);
camera.position.y = THREE.MathUtils.clamp(camera.position.y, 2, 16);
syncPositionUI();
visibilityDirty = true;
},
{ passive: false }
);
document.querySelectorAll("[data-move]").forEach((button) => {
const code = button.dataset.move;
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
keys[code] = true;
button.setPointerCapture(event.pointerId);
});
const release = (event) => {
keys[code] = false;
if (button.hasPointerCapture(event.pointerId)) {
button.releasePointerCapture(event.pointerId);
}
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("pointerleave", release);
});
function updateMovement(delta) {
const forwardInput =
(keys.KeyW || keys.ArrowUp ? 1 : 0) -
(keys.KeyS || keys.ArrowDown ? 1 : 0);
const strafeInput =
(keys.KeyD || keys.ArrowRight ? 1 : 0) -
(keys.KeyA || keys.ArrowLeft ? 1 : 0);
const verticalInput =
(keys.KeyE ? 1 : 0) -
(keys.KeyQ ? 1 : 0);
if (!forwardInput && !strafeInput && !verticalInput) {
return;
}
const speed = keys.ShiftLeft || keys.ShiftRight ? 9 : 4.2;
const forward = new THREE.Vector3(
-Math.sin(cameraState.yaw),
0,
-Math.cos(cameraState.yaw)
);
const right = new THREE.Vector3(
Math.cos(cameraState.yaw),
0,
-Math.sin(cameraState.yaw)
);
camera.position.addScaledVector(forward, forwardInput * speed * delta);
camera.position.addScaledVector(right, strafeInput * speed * delta);
camera.position.y += verticalInput * speed * 0.75 * delta;
camera.position.x = THREE.MathUtils.clamp(camera.position.x, -10, 10);
camera.position.y = THREE.MathUtils.clamp(camera.position.y, 2, 16);
camera.position.z = THREE.MathUtils.clamp(camera.position.z, 1.2, 14);
syncPositionUI();
visibilityDirty = true;
}
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
visibilityDirty = true;
});
document.getElementById("cellTotal").textContent = `${cells.length} cells`;
syncPositionUI();
updateVisibility();
const clock = new THREE.Clock();
let lastVisibilityUpdate = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateMovement(delta);
camera.rotation.set(cameraState.pitch, cameraState.yaw, 0, "YXZ");
camera.updateMatrixWorld();
if (visibilityDirty && elapsed - lastVisibilityUpdate > 0.11) {
updateVisibility();
lastVisibilityUpdate = elapsed;
visibilityDirty = false;
}
for (const person of people) {
person.group.position.y =
person.baseY + Math.sin(elapsed * 2.1 + person.phase) * 0.025;
}
renderer.render(scene, camera);
}
loading.hidden = true;
animate();
</script>
</body>
</html>
```