Prompt specification
Build or customize the Three.js 3D scene "Old San Juan wall embrasure fragment" 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: Old San Juan wall embrasure fragment
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/old-san-juan-wall-embrasure-fragment-x2785
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: Old San Juan wall embrasure fragment. Build a referenced fortification-wall fragment with one embrasure and walkway edge. Show wall thickness and the difference between narrow outer opening and wider interior recess. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (old-san-juan-wall-embrasure-fragment-x2785):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Old San Juan — Embrasure Fragment</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(28, 35, 36, 0.88);
--panel-border: rgba(255, 255, 255, 0.14);
--text: #f5efe3;
--muted: #c7c0b3;
--gold: #e7bd73;
--teal: #7dd4c4;
--clay: #ee987a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #aab8b8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--text);
}
.title-panel {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px 17px;
border: 1px solid var(--panel-border);
border-radius: 8px;
background: var(--panel);
box-shadow: 0 12px 40px rgba(20, 24, 24, 0.2);
backdrop-filter: blur(10px);
}
.eyebrow {
margin-bottom: 8px;
color: var(--gold);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0 0 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(23px, 3vw, 31px);
font-weight: 500;
line-height: 1.08;
}
.description {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid var(--panel-border);
border-radius: 8px;
background: var(--panel);
pointer-events: auto;
box-shadow: 0 12px 40px rgba(20, 24, 24, 0.2);
backdrop-filter: blur(10px);
}
.controls button {
min-width: 76px;
padding: 9px 10px;
border: 1px solid transparent;
border-radius: 5px;
background: transparent;
color: var(--muted);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, color 160ms ease, border-color 160ms ease;
}
.controls button:hover,
.controls button:focus-visible {
border-color: rgba(255, 255, 255, 0.2);
color: var(--text);
outline: none;
}
.controls button.active {
border-color: rgba(231, 189, 115, 0.45);
background: rgba(231, 189, 115, 0.14);
color: #ffe0a5;
}
.legend {
position: absolute;
left: 22px;
bottom: 22px;
display: grid;
gap: 8px;
padding: 13px 15px;
border: 1px solid var(--panel-border);
border-radius: 8px;
background: var(--panel);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.025em;
backdrop-filter: blur(10px);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 18px;
height: 2px;
background: currentColor;
box-shadow: 0 0 7px currentColor;
}
.swatch.gold {
color: var(--gold);
}
.swatch.teal {
color: var(--teal);
}
.swatch.clay {
color: var(--clay);
}
.help {
position: absolute;
right: 22px;
bottom: 22px;
padding: 9px 12px;
border-radius: 6px;
background: rgba(28, 35, 36, 0.68);
color: rgba(255, 255, 255, 0.72);
font-size: 10px;
letter-spacing: 0.04em;
backdrop-filter: blur(8px);
}
.tag {
position: absolute;
top: 0;
left: 0;
display: none;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 4px;
background: rgba(24, 31, 32, 0.88);
color: var(--text);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
transform: translate(-50%, -100%);
}
.tag::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 14px;
background: currentColor;
content: "";
opacity: 0.65;
}
#outerTag {
color: var(--gold);
}
#innerTag {
color: var(--teal);
}
#thicknessTag {
color: var(--clay);
}
.note {
margin-top: 11px;
color: rgba(255, 255, 255, 0.48);
font-size: 9px;
line-height: 1.45;
}
@media (max-width: 760px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
.description {
max-width: 85%;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
justify-content: center;
}
.controls button {
min-width: 0;
flex: 1;
padding: 9px 5px;
font-size: 8px;
}
.legend {
display: none;
}
.help {
right: auto;
bottom: 70px;
left: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly model of an old San Juan fortification wall embrasure"></canvas>
<div id="hud">
<section class="title-panel">
<div class="eyebrow">Castillo San Felipe del Morro · Interpretive Study</div>
<h1>Wall Embrasure Fragment</h1>
<p class="description">
A stepped embrasure inspired by the thick masonry ramparts of Old San Juan:
a narrow exterior firing aperture opens into a broader interior recess.
</p>
<div class="note">Low-poly reconstruction · Dimensions are illustrative, not survey measurements.</div>
</section>
<nav class="controls" aria-label="Camera views">
<button type="button" data-view="exterior" class="active">Exterior</button>
<button type="button" data-view="interior">Interior</button>
<button type="button" data-view="walkway">Walkway</button>
<button type="button" data-view="section">Section</button>
</nav>
<div class="legend" aria-label="Dimension legend">
<div class="legend-row">
<span class="swatch gold"></span>
<span>Exterior aperture · 0.72 m</span>
</div>
<div class="legend-row">
<span class="swatch teal"></span>
<span>Interior recess · 2.56 m</span>
</div>
<div class="legend-row">
<span class="swatch clay"></span>
<span>Wall depth · 1.80 m</span>
</div>
</div>
<div class="help">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="outerTag" class="tag">Exterior aperture · 0.72 m</div>
<div id="innerTag" class="tag">Interior recess · 2.56 m</div>
<div id="thicknessTag" class="tag">Wall core · 1.80 m</div>
</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 renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaebbb9);
scene.fog = new THREE.Fog(0xaebbb9, 19, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xd9edf0, 0x776650, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1b7, 3.4);
sun.position.set(-7, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x8eb8c9, 0.8);
coolFill.position.set(7, 4, -8);
scene.add(coolFill);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.95,
metalness: 0,
flatShading: true,
...options
});
}
const stoneMaterials = [
standardMaterial(0xc2ad88),
standardMaterial(0xb9a27d),
standardMaterial(0xcdb997),
standardMaterial(0xad9876),
standardMaterial(0xd2c09d),
standardMaterial(0xa89070)
];
const trimMaterials = [
standardMaterial(0xd4c09b),
standardMaterial(0xc0aa83),
standardMaterial(0xb69f79)
];
const mortarMaterial = standardMaterial(0x746754);
const revealMaterial = standardMaterial(0x8d7d64, {
side: THREE.DoubleSide
});
const revealDarkMaterial = standardMaterial(0x746751, {
side: THREE.DoubleSide
});
const sectionMaterials = [
standardMaterial(0xb59c76),
standardMaterial(0xc6af87),
standardMaterial(0xa98f6c),
standardMaterial(0xd0b992)
];
const walkwayMaterials = [
standardMaterial(0x9f9986),
standardMaterial(0xaaa28d),
standardMaterial(0x958e7d),
standardMaterial(0xb1a894)
];
const wall = new THREE.Group();
const exteriorMasonry = new THREE.Group();
const interiorMasonry = new THREE.Group();
const architecturalDetails = new THREE.Group();
const walkway = new THREE.Group();
scene.add(wall);
wall.add(exteriorMasonry, interiorMasonry, architecturalDetails, walkway);
let randomSeed = 421337;
function random() {
randomSeed |= 0;
randomSeed = (randomSeed + 0x6d2b79f5) | 0;
let t = Math.imul(randomSeed ^ (randomSeed >>> 15), 1 | randomSeed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function randomRange(min, max) {
return min + (max - min) * random();
}
function addBox(width, height, depth, x, y, z, material, parent = wall) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addChamferedStone(
width,
height,
depth,
x,
y,
z,
material,
parent = wall
) {
const shape = new THREE.Shape();
shape.moveTo(-width / 2, -height / 2);
shape.lineTo(width / 2, -height / 2);
shape.lineTo(width / 2, height / 2);
shape.lineTo(-width / 2, height / 2);
shape.closePath();
const bevel = Math.min(0.035, width * 0.1, height * 0.12, depth * 0.14);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
curveSegments: 1,
bevelEnabled: bevel > 0.007,
bevelSegments: 1,
bevelSize: bevel,
bevelThickness: bevel * 0.6
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const wallMinX = -4.2;
const wallMaxX = 4.2;
const wallMinY = 0;
const wallMaxY = 3.62;
const wallHalfDepth = 0.9;
const exteriorOpening = {
x0: -0.36,
x1: 0.36,
y0: 0.95,
y1: 1.88
};
const interiorOpening = {
x0: -1.28,
x1: 1.28,
y0: 0.55,
y1: 2.35
};
const masonryRows = [
[0.0, 0.55],
[0.55, 0.95],
[0.95, 1.42],
[1.42, 1.88],
[1.88, 2.35],
[2.35, 2.82],
[2.82, 3.3],
[3.3, 3.62]
];
function addMortarRing(opening, z, depth) {
const leftWidth = opening.x0 - wallMinX;
const rightWidth = wallMaxX - opening.x1;
const bottomHeight = opening.y0 - wallMinY;
const topHeight = wallMaxY - opening.y1;
const openingWidth = opening.x1 - opening.x0;
addBox(
leftWidth,
wallMaxY - wallMinY,
depth,
wallMinX + leftWidth / 2,
(wallMinY + wallMaxY) / 2,
z,
mortarMaterial
);
addBox(
rightWidth,
wallMaxY - wallMinY,
depth,
opening.x1 + rightWidth / 2,
(wallMinY + wallMaxY) / 2,
z,
mortarMaterial
);
addBox(
openingWidth,
bottomHeight,
depth,
(opening.x0 + opening.x1) / 2,
wallMinY + bottomHeight / 2,
z,
mortarMaterial
);
addBox(
openingWidth,
topHeight,
depth,
(opening.x0 + opening.x1) / 2,
opening.y1 + topHeight / 2,
z,
mortarMaterial
);
}
function addMasonryFacade(opening, side, parent) {
const zCenter = side > 0 ? 0.81 : -0.81;
masonryRows.forEach((row, rowIndex) => {
const rowHeight = row[1] - row[0];
const rowMidpoint = (row[0] + row[1]) / 2;
const crossesOpening =
rowMidpoint > opening.y0 && rowMidpoint < opening.y1;
let x = wallMinX;
const cuts = [x];
while (x < wallMaxX - 0.35) {
x += randomRange(0.72, 1.25);
cuts.push(Math.min(x, wallMaxX));
}
cuts[cuts.length - 1] = wallMaxX;
for (let i = 0; i < cuts.length - 1; i++) {
const a = cuts[i];
const b = cuts[i + 1];
const nominalWidth = b - a;
const pieces = [];
if (crossesOpening) {
if (a < opening.x0) {
pieces.push([a, Math.min(b, opening.x0)]);
}
if (b > opening.x1) {
pieces.push([Math.max(a, opening.x1), b]);
}
} else {
pieces.push([a, b]);
}
pieces.forEach((piece) => {
const pieceWidth = piece[1] - piece[0];
if (pieceWidth < 0.09) return;
const material =
stoneMaterials[
Math.floor(random() * stoneMaterials.length)
];
const depth = randomRange(0.145, 0.175);
const zOffset = randomRange(-0.009, 0.009);
addChamferedStone(
pieceWidth - 0.026,
rowHeight - 0.025,
depth,
(piece[0] + piece[1]) / 2,
rowMidpoint,
zCenter + zOffset,
material,
parent
);
});
}
});
}
addMortarRing(exteriorOpening, 0.715, 0.09);
addMortarRing(interiorOpening, -0.715, 0.09);
addMasonryFacade(exteriorOpening, 1, exteriorMasonry);
addMasonryFacade(interiorOpening, -1, interiorMasonry);
function addQuad(a, b, c, d, material, parent = wall) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[...a, ...b, ...c, ...d],
3
)
);
geometry.setIndex([0, 1, 2, 0, 2, 3]);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const outerZ = 0.715;
const innerZ = -0.715;
const outerLoop = [
[exteriorOpening.x0, exteriorOpening.y0, outerZ],
[exteriorOpening.x1, exteriorOpening.y0, outerZ],
[exteriorOpening.x1, exteriorOpening.y1, outerZ],
[exteriorOpening.x0, exteriorOpening.y1, outerZ]
];
const innerLoop = [
[interiorOpening.x0, interiorOpening.y0, innerZ],
[interiorOpening.x1, interiorOpening.y0, innerZ],
[interiorOpening.x1, interiorOpening.y1, innerZ],
[interiorOpening.x0, interiorOpening.y1, innerZ]
];
addQuad(
outerLoop[0],
outerLoop[1],
innerLoop[1],
innerLoop[0],
revealDarkMaterial
);
addQuad(
outerLoop[3],
outerLoop[2],
innerLoop[2],
innerLoop[3],
revealMaterial
);
addQuad(
outerLoop[0],
innerLoop[0],
innerLoop[3],
outerLoop[3],
revealMaterial
);
addQuad(
outerLoop[1],
outerLoop[2],
innerLoop[2],
innerLoop[1],
revealDarkMaterial
);
// Solid outer envelope, top cap and exposed fragment end.
addBox(
wallMaxX - wallMinX,
0.16,
wallHalfDepth * 2,
0,
wallMaxY + 0.06,
0,
sectionMaterials[1]
);
addBox(
0.11,
wallMaxY,
wallHalfDepth * 2,
wallMinX - 0.03,
wallMaxY / 2,
0,
sectionMaterials[0]
);
masonryRows.forEach((row, index) => {
const height = row[1] - row[0];
addBox(
0.105,
height - 0.012,
wallHalfDepth * 2,
wallMaxX + 0.025,
(row[0] + row[1]) / 2,
0,
sectionMaterials[index % sectionMaterials.length]
);
});
addBox(
wallMaxX - wallMinX + 0.08,
0.25,
wallHalfDepth * 2 + 0.05,
0,
0.13,
0,
sectionMaterials[0]
);
// Exterior dressed-stone frame.
addChamferedStone(
1.12,
0.26,
0.28,
0,
2.015,
0.825,
trimMaterials[0],
architecturalDetails
);
addChamferedStone(
0.24,
0.96,
0.28,
-0.48,
1.415,
0.825,
trimMaterials[1],
architecturalDetails
);
addChamferedStone(
0.24,
0.96,
0.28,
0.48,
1.415,
0.825,
trimMaterials[2],
architecturalDetails
);
addChamferedStone(
1.08,
0.18,
0.28,
0,
0.855,
0.825,
trimMaterials[1],
architecturalDetails
);
// Broad interior frame emphasizes the larger recessed opening.
addChamferedStone(
3.08,
0.32,
0.30,
0,
2.51,
-0.825,
trimMaterials[0],
architecturalDetails
);
addChamferedStone(
0.28,
1.82,
0.30,
-1.42,
1.45,
-0.825,
trimMaterials[2],
architecturalDetails
);
addChamferedStone(
0.28,
1.82,
0.30,
1.42,
1.45,
-0.825,
trimMaterials[1],
architecturalDetails
);
addChamferedStone(
2.86,
0.22,
0.30,
0,
0.435,
-0.825,
trimMaterials[1],
architecturalDetails
);
// Uneven coping stones along the exterior wall edge.
let copingX = wallMinX;
while (copingX < wallMaxX - 0.25) {
const width = Math.min(randomRange(0.72, 1.1), wallMaxX - copingX);
addChamferedStone(
width - 0.025,
randomRange(0.16, 0.22),
randomRange(0.34, 0.43),
copingX + width / 2,
3.76,
0.64,
trimMaterials[Math.floor(random() * trimMaterials.length)],
architecturalDetails
);
copingX += width;
}
// Raised rampart walkway behind the wall.
addBox(
wallMaxX - wallMinX,
0.18,
2.4,
0,
3.68,
-2.09,
walkwayMaterials[2],
walkway
);
const pavingRows = [
[-1.15, -1.55],
[-1.58, -2.02],
[-2.05, -2.49],
[-2.52, -2.95]
];
pavingRows.forEach((row, rowIndex) => {
let x = wallMinX + (rowIndex % 2 ? 0.25 : 0);
while (x < wallMaxX - 0.2) {
const width = Math.min(randomRange(0.78, 1.18), wallMaxX - x);
const depth = row[1] - row[0];
addBox(
width - 0.025,
0.095,
depth - 0.025,
x + width / 2,
3.805,
(row[0] + row[1]) / 2,
walkwayMaterials[Math.floor(random() * walkwayMaterials.length)],
walkway
);
x += width;
}
});
// Low protective edge along the far side of the walkway.
let parapetX = wallMinX;
while (parapetX < wallMaxX - 0.2) {
const width = Math.min(randomRange(0.75, 1.12), wallMaxX - parapetX);
addChamferedStone(
width - 0.025,
0.47,
0.30,
parapetX + width / 2,
4.02,
-3.09,
stoneMaterials[Math.floor(random() * stoneMaterials.length)],
walkway
);
parapetX += width;
}
// Ground and a few low-poly rocks.
const groundMaterial = standardMaterial(0x9b9c8e);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const rockMaterial = standardMaterial(0x7f8177);
[
[-5.1, 0.12, 1.25, 0.25],
[5.0, 0.10, -0.8, 0.18],
[3.9, 0.08, 2.7, 0.15],
[-3.8, 0.09, -2.2, 0.13]
].forEach(([x, y, z, scale]) => {
const rock = new THREE.Mesh(
new THREE.IcosahedronGeometry(scale, 0),
rockMaterial
);
rock.position.set(x, y, z);
rock.rotation.set(random(), random(), random());
rock.scale.set(1.3, 0.65, 1);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
});
// Technical dimension arrows.
function setAlwaysVisible(object) {
object.traverse((child) => {
if (child.material) {
child.material.depthTest = false;
child.material.transparent = true;
child.material.opacity = 0.95;
child.renderOrder = 20;
}
});
}
function makeDoubleArrow(start, end, color, headLength = 0.11) {
const group = new THREE.Group();
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
direction.normalize();
const headWidth = headLength * 0.65;
const forward = new THREE.ArrowHelper(
direction,
start.clone(),
length,
color,
headLength,
headWidth
);
const backward = new THREE.ArrowHelper(
direction.clone().negate(),
end.clone(),
length,
color,
headLength,
headWidth
);
group.add(forward, backward);
setAlwaysVisible(group);
scene.add(group);
return group;
}
const outerDimensions = makeDoubleArrow(
new THREE.Vector3(exteriorOpening.x0, 2.2, 1.06),
new THREE.Vector3(exteriorOpening.x1, 2.2, 1.06),
0xe7bd73,
0.08
);
const innerDimensions = makeDoubleArrow(
new THREE.Vector3(interiorOpening.x0, 2.72, -1.06),
new THREE.Vector3(interiorOpening.x1, 2.72, -1.06),
0x7dd4c4,
0.12
);
const thicknessDimensions = makeDoubleArrow(
new THREE.Vector3(4.5, 2.65, 0.9),
new THREE.Vector3(4.5, 2.65, -0.9),
0xee987a,
0.13
);
const outerTag = document.querySelector("#outerTag");
const innerTag = document.querySelector("#innerTag");
const thicknessTag = document.querySelector("#thicknessTag");
function positionTag(element, worldPosition, visible) {
if (!visible) {
element.style.display = "none";
return;
}
const projected = worldPosition.clone().project(camera);
const inFront = projected.z > -1 && projected.z < 1;
const onScreen =
projected.x > -1.08 &&
projected.x < 1.08 &&
projected.y > -1.08 &&
projected.y < 1.08;
if (!inFront || !onScreen) {
element.style.display = "none";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.display = "block";
element.style.transform = `translate(${x}px, ${y}px) translate(-50%, -100%)`;
}
// Camera controls implemented directly without external examples/addons.
const views = {
exterior: {
target: new THREE.Vector3(0, 1.65, -0.05),
radius: 10.4,
phi: 1.15,
theta: 0.63
},
interior: {
target: new THREE.Vector3(0, 1.55, -0.25),
radius: 10.2,
phi: 1.12,
theta: 2.52
},
walkway: {
target: new THREE.Vector3(0, 2.5, -1.55),
radius: 9.2,
phi: 0.58,
theta: 0.5
},
section: {
target: new THREE.Vector3(1.7, 1.75, 0),
radius: 8.2,
phi: 1.02,
theta: 1.05
}
};
const desiredTarget = views.exterior.target.clone();
const currentTarget = views.exterior.target.clone();
const desiredSpherical = new THREE.Spherical(
views.exterior.radius,
views.exterior.phi,
views.exterior.theta
);
const currentSpherical = desiredSpherical.clone();
camera.position.setFromSpherical(currentSpherical).add(currentTarget);
camera.lookAt(currentTarget);
const buttons = [...document.querySelectorAll("[data-view]")];
function activateButton(name) {
buttons.forEach((button) => {
const active = button.dataset.view === name;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
buttons.forEach((button) => {
button.setAttribute("aria-pressed", String(button.classList.contains("active")));
button.addEventListener("click", () => {
const view = views[button.dataset.view];
desiredTarget.copy(view.target);
desiredSpherical.radius = view.radius;
desiredSpherical.phi = view.phi;
desiredSpherical.theta = view.theta;
activateButton(button.dataset.view);
});
});
let dragging = false;
let previousX = 0;
let previousY = 0;
let activePointer = null;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
activePointer = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
activateButton(null);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredSpherical.theta -= deltaX * 0.006;
desiredSpherical.phi = THREE.MathUtils.clamp(
desiredSpherical.phi - deltaY * 0.005,
0.28,
1.48
);
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredSpherical.radius = THREE.MathUtils.clamp(
desiredSpherical.radius * Math.exp(event.deltaY * 0.001),
5.2,
16
);
activateButton(null);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
const view = views.exterior;
desiredTarget.copy(view.target);
desiredSpherical.radius = view.radius;
desiredSpherical.phi = view.phi;
desiredSpherical.theta = view.theta;
activateButton("exterior");
});
window.addEventListener("keydown", (event) => {
const step = 0.08;
if (event.key === "ArrowLeft") {
desiredSpherical.theta += step;
activateButton(null);
} else if (event.key === "ArrowRight") {
desiredSpherical.theta -= step;
activateButton(null);
} else if (event.key === "ArrowUp") {
desiredSpherical.phi = Math.max(0.28, desiredSpherical.phi - step);
activateButton(null);
} else if (event.key === "ArrowDown") {
desiredSpherical.phi = Math.min(1.48, desiredSpherical.phi + step);
activateButton(null);
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const easing = 1 - Math.exp(-delta * 7);
currentTarget.lerp(desiredTarget, easing);
currentSpherical.radius = THREE.MathUtils.lerp(
currentSpherical.radius,
desiredSpherical.radius,
easing
);
currentSpherical.phi = THREE.MathUtils.lerp(
currentSpherical.phi,
desiredSpherical.phi,
easing
);
currentSpherical.theta = THREE.MathUtils.lerp(
currentSpherical.theta,
desiredSpherical.theta,
easing
);
camera.position.setFromSpherical(currentSpherical).add(currentTarget);
camera.lookAt(currentTarget);
const viewSide = camera.position.z - currentTarget.z;
const viewingExterior = viewSide > 0.12;
const viewingInterior = viewSide < -0.12;
outerDimensions.visible = viewingExterior;
innerDimensions.visible = viewingInterior;
positionTag(
outerTag,
new THREE.Vector3(0, 2.34, 1.08),
viewingExterior
);
positionTag(
innerTag,
new THREE.Vector3(0, 2.9, -1.08),
viewingInterior
);
positionTag(
thicknessTag,
new THREE.Vector3(4.62, 2.86, 0.1),
true
);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```