Prompt specification
Build or customize the Three.js 3D scene "Market awning slope section" 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: Market awning slope section
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/market-awning-slope-section-x0481
Scene Description Prompt:
A voxel market awning slopes toward its gutter above a counter. Show its side section using Three.js from cdnjs in one HTML file.
Working Three.js Starter (market-awning-slope-section-x0481):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Market Awning — Side Section</title>
<style>
:root {
--ink: #25332f;
--paper: #f4efe5;
--accent: #b95336;
--teal: #287c80;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 22%, rgba(255, 255, 255, 0.95), transparent 38%),
linear-gradient(145deg, #e9e4d8 0%, #d6d9d0 100%);
color: var(--ink);
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;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title-card {
position: fixed;
top: 28px;
left: 30px;
z-index: 10;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 3px;
background: var(--accent);
content: "";
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 48px);
font-weight: 760;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 330px;
margin: 13px 0 0;
color: rgba(37, 51, 47, 0.68);
font-size: 13px;
line-height: 1.55;
}
.section-badge {
position: fixed;
top: 30px;
right: 30px;
z-index: 10;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid rgba(37, 51, 47, 0.15);
border-radius: 999px;
background: rgba(250, 247, 239, 0.72);
box-shadow: 0 10px 35px rgba(45, 55, 47, 0.08);
backdrop-filter: blur(12px);
color: rgba(37, 51, 47, 0.75);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
}
.section-badge span {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(40, 124, 128, 0.13);
}
#reset {
position: fixed;
right: 30px;
bottom: 28px;
z-index: 20;
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 14px;
border: 1px solid rgba(37, 51, 47, 0.16);
border-radius: 10px;
background: rgba(250, 247, 239, 0.84);
box-shadow: 0 8px 28px rgba(45, 55, 47, 0.09);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
transform 150ms ease,
background 150ms ease;
}
#reset:hover {
transform: translateY(-2px);
background: #fffaf1;
}
#reset svg {
width: 14px;
height: 14px;
}
.help {
position: fixed;
bottom: 31px;
left: 30px;
z-index: 10;
color: rgba(37, 51, 47, 0.57);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
pointer-events: none;
}
.help span {
margin: 0 8px;
color: var(--accent);
}
.callout {
position: fixed;
z-index: 8;
padding: 7px 10px;
border: 1px solid rgba(37, 51, 47, 0.12);
border-radius: 7px;
background: rgba(250, 247, 239, 0.9);
box-shadow: 0 7px 22px rgba(45, 55, 47, 0.09);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(8px);
transform: translate(-50%, -50%);
}
.callout::before {
position: absolute;
top: 50%;
left: -6px;
width: 7px;
height: 7px;
border: 2px solid var(--paper);
border-radius: 50%;
background: var(--teal);
content: "";
transform: translateY(-50%);
}
#gutter-label::before {
background: var(--accent);
}
#counter-label::before {
background: #99713e;
}
.corner-note {
position: fixed;
right: 30px;
bottom: 80px;
z-index: 10;
color: rgba(37, 51, 47, 0.35);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.14em;
pointer-events: none;
}
@media (max-width: 720px) {
.title-card {
top: 20px;
left: 20px;
}
.section-badge {
top: auto;
right: 20px;
bottom: 76px;
}
.help {
bottom: 23px;
left: 20px;
}
#reset {
right: 20px;
bottom: 18px;
}
.callout,
.corner-note {
display: none;
}
.subtitle {
max-width: 270px;
}
}
</style>
</head>
<body>
<div class="title-card">
<div class="eyebrow">Market detail</div>
<h1>Voxel Market<br />Awning</h1>
<p class="subtitle">
A side section showing the stepped canvas slope, open metal gutter,
supporting posts, and produce counter beneath.
</p>
</div>
<div class="section-badge"><span></span>SIDE SECTION · A–A</div>
<div id="slope-label" class="callout">CANOPY FALLS TOWARD GUTTER</div>
<div id="gutter-label" class="callout">OPEN GUTTER</div>
<div id="counter-label" class="callout">PRODUCE COUNTER</div>
<div class="help">DRAG TO ORBIT <span>•</span> SCROLL TO ZOOM</div>
<div class="corner-note">VOXEL STUDY / 01</div>
<button id="reset" type="button" aria-label="Reset camera view">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path
d="M4.7 8.2A8 8 0 1 1 4 14"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
/>
<path
d="M4.7 3.8v4.4h4.4"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
RESET VIEW
</button>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe4e4da, 25, 43);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional side section of a voxel market awning"
);
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
camera.position.set(7.5, 6.3, 14);
const target = new THREE.Vector3(0.05, 2.65, 0);
const cameraState = {
theta: 0.48,
phi: 1.19,
radius: 17.2,
zoom: 1,
targetTheta: 0.48,
targetPhi: 1.19,
targetZoom: 1
};
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xf4fbff, 0x72553d, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe4bd, 3.1);
sun.position.set(-5, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9dce4, 1.1);
fill.position.set(8, 5, -9);
scene.add(fill);
// ---------------------------------------------------------------------
// Materials and voxel helpers
// ---------------------------------------------------------------------
const material = (color, roughness = 0.82, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const materials = {
base: material(0xb9b6a9),
floor: material(0xd8d3c5),
grout: material(0xb6b2a7),
wall: material(0x77776e),
mortar: material(0xc2b9a6),
brickDark: material(0x8d6f5a),
brickWarm: material(0xa87d60),
brickLight: material(0xb89272),
brickPale: material(0xc2a183),
darkFrame: material(0x34433e, 0.72),
window: material(0x3c5d5d, 0.3, 0.05),
sign: material(0x315f55, 0.7),
signText: material(0xf2dca8),
canvasRed: material(0xc76543),
canvasRedDark: material(0xaa4d35),
canvasCream: material(0xefd3a2),
canvasCreamDark: material(0xd9ba86),
beam: material(0x6c4d36),
gutter: material(0x46524f, 0.54, 0.42),
gutterLight: material(0x64716c, 0.48, 0.48),
gutterInside: material(0x252f2e, 0.64, 0.28),
metal: material(0x36413e, 0.5, 0.52),
metalLight: material(0x5a6762, 0.48, 0.48),
counter: material(0x87613f),
counterDark: material(0x67472f),
counterTop: material(0xb08353),
slatA: material(0xa77648),
slatB: material(0x95653d),
crate: material(0xa97945),
crateDark: material(0x76502f),
apple: material(0xc84f3f),
appleLight: material(0xe16a50),
lemon: material(0xe3b747),
leaf: material(0x4c8250),
leafLight: material(0x6a9b58),
orange: material(0xe08235),
ceramic: material(0xd8d3c3),
ceramicBlue: material(0x608d96)
};
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function addBox(
width,
height,
depth,
mat,
x,
y,
z,
{
cast = true,
receive = true,
parent = scene,
rotation = null
} = {}
) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
if (rotation) {
mesh.rotation.set(rotation.x, rotation.y, rotation.z);
}
parent.add(mesh);
return mesh;
}
function beamBetween(start, end, thickness, mat) {
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const direction = new THREE.Vector3()
.subVectors(end, start);
const length = direction.length();
const beam = new THREE.Mesh(unitBox, mat);
beam.scale.set(thickness, length, thickness);
beam.position.copy(midpoint);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
return beam;
}
// ---------------------------------------------------------------------
// Foundation and pavement
// ---------------------------------------------------------------------
addBox(10.2, 0.48, 10.4, materials.base, 0.2, -0.25, 0);
addBox(9.8, 0.06, 10.0, materials.floor, 0.2, 0.01, 0);
for (let x = -4.2; x <= 4.6; x += 1.1) {
addBox(0.025, 0.018, 9.9, materials.grout, x, 0.052, 0, {
cast: false
});
}
for (let z = -4.4; z <= 4.5; z += 1.1) {
addBox(9.7, 0.018, 0.025, materials.grout, 0.2, 0.054, z, {
cast: false
});
}
// ---------------------------------------------------------------------
// Voxel masonry wall
// ---------------------------------------------------------------------
addBox(0.68, 6.35, 9.5, materials.wall, -4.05, 3.175, 0);
const brickPalette = [
materials.brickDark,
materials.brickWarm,
materials.brickLight,
materials.brickPale,
materials.brickWarm
];
for (let row = 0; row < 10; row++) {
const y = 0.34 + row * 0.59;
const offset = row % 2 === 0 ? 0 : 0.36;
for (let col = -1; col < 14; col++) {
const z = -4.32 + offset + col * 0.72;
if (z > 4.8 || z < -4.8) continue;
const mat = brickPalette[(row * 3 + col + 12) % brickPalette.length];
addBox(
0.18,
0.51,
0.66,
mat,
-3.66,
y,
z,
{ cast: true }
);
}
}
addBox(0.82, 0.22, 9.65, materials.mortar, -4.0, 6.34, 0);
// Recessed shop window
addBox(0.12, 2.45, 3.2, materials.window, -3.54, 2.05, 0.15);
addBox(0.14, 0.12, 3.42, materials.signText, -3.43, 3.31, 0.15);
addBox(0.14, 0.12, 3.42, materials.signText, -3.43, 0.79, 0.15);
for (const z of [-1.48, 0.15, 1.78]) {
addBox(0.14, 2.4, 0.10, materials.signText, -3.42, 2.05, z);
}
addBox(0.14, 0.10, 3.1, materials.signText, -3.42, 2.05, 0.15);
// ---------------------------------------------------------------------
// Pixel-letter MARKET sign
// ---------------------------------------------------------------------
addBox(0.18, 0.82, 3.7, materials.sign, -3.47, 5.74, 0);
addBox(0.08, 0.94, 3.86, materials.beam, -3.57, 5.74, 0);
const pixelFont = {
M: [
"10001",
"11011",
"10101",
"10101",
"10001",
"10001",
"10001"
],
A: [
"01110",
"10001",
"10001",
"11111",
"10001",
"10001",
"10001"
],
R: [
"11110",
"10001",
"10001",
"11110",
"10100",
"10010",
"10001"
],
K: [
"10001",
"10010",
"10100",
"11000",
"10100",
"10010",
"10001"
],
E: [
"11111",
"10000",
"10000",
"11110",
"10000",
"10000",
"11111"
],
T: [
"11111",
"00100",
"00100",
"00100",
"00100",
"00100",
"00100"
]
};
const signWord = "MARKET";
const pixel = 0.078;
const totalColumns = signWord.length * 6 - 1;
[...signWord].forEach((letter, letterIndex) => {
const glyph = pixelFont[letter];
glyph.forEach((row, rowIndex) => {
[...row].forEach((cell, columnIndex) => {
if (cell !== "1") return;
const globalColumn = letterIndex * 6 + columnIndex;
addBox(
0.055,
pixel * 0.82,
pixel * 0.82,
materials.signText,
-3.345,
5.74 + (3 - rowIndex) * pixel,
(globalColumn - (totalColumns - 1) / 2) * pixel
);
});
});
});
// ---------------------------------------------------------------------
// Stepped voxel awning
// ---------------------------------------------------------------------
const awningStartX = -3.5;
const awningEndX = 4.05;
const awningStartY = 5.08;
const awningEndY = 3.22;
const awningStartZ = -3.42;
const awningEndZ = 3.42;
const xSegments = 16;
const zSegments = 14;
const segmentX = (awningEndX - awningStartX) / xSegments;
const segmentZ = (awningEndZ - awningStartZ) / zSegments;
for (let ix = 0; ix < xSegments; ix++) {
const x = awningStartX + (ix + 0.5) * segmentX;
const t = (ix + 0.5) / xSegments;
const topY = THREE.MathUtils.lerp(awningStartY, awningEndY, t);
for (let iz = 0; iz < zSegments; iz++) {
const z = awningStartZ + (iz + 0.5) * segmentZ;
const stripe = Math.floor(iz / 2) % 2;
const alternate = (ix + iz) % 7 === 0;
let mat;
if (stripe === 0) {
mat = alternate ? materials.canvasRedDark : materials.canvasRed;
} else {
mat = alternate ? materials.canvasCreamDark : materials.canvasCream;
}
addBox(
segmentX - 0.025,
0.18,
segmentZ - 0.025,
mat,
x,
topY - 0.09,
z
);
}
}
// Exposed underside rafters
for (const z of [-3.0, -1.0, 1.0, 3.0]) {
beamBetween(
new THREE.Vector3(-3.45, 4.91, z),
new THREE.Vector3(4.02, 3.08, z),
0.13,
materials.beam
);
}
addBox(0.14, 0.15, 6.95, materials.beam, -3.43, 4.88, 0);
addBox(0.14, 0.15, 6.95, materials.beam, 4.0, 3.07, 0);
// Wall mounting bracket
addBox(0.74, 0.16, 0.16, materials.metal, -3.18, 4.85, 0);
beamBetween(
new THREE.Vector3(-3.55, 4.25, 0),
new THREE.Vector3(-2.55, 4.82, 0),
0.12,
materials.metal
);
// ---------------------------------------------------------------------
// Open U-channel gutter at the low edge
// ---------------------------------------------------------------------
addBox(0.52, 0.12, 7.45, materials.gutter, 4.27, 3.02, 0);
addBox(0.12, 0.32, 7.45, materials.gutterLight, 4.50, 3.18, 0);
addBox(0.10, 0.26, 7.45, materials.gutter, 4.04, 3.15, 0);
addBox(0.34, 0.035, 7.25, materials.gutterInside, 4.27, 3.10, 0);
// Gutter seams
for (let z = -3.35; z <= 3.4; z += 0.84) {
addBox(0.045, 0.27, 0.055, materials.metalLight, 4.565, 3.19, z);
}
// Square downpipe at the far end
addBox(0.16, 2.75, 0.16, materials.gutter, 4.49, 1.73, -3.72);
addBox(0.42, 0.16, 0.16, materials.gutter, 4.72, 0.43, -3.72);
for (const y of [0.95, 2.35]) {
addBox(0.22, 0.08, 0.22, materials.metalLight, 4.49, y, -3.72);
}
// ---------------------------------------------------------------------
// Front support posts and braces
// ---------------------------------------------------------------------
for (const z of [-3.42, 3.42]) {
addBox(0.16, 3.0, 0.16, materials.metal, 3.88, 1.52, z);
addBox(0.28, 0.10, 0.28, materials.metalLight, 3.88, 3.02, z);
addBox(0.28, 0.09, 0.28, materials.metalLight, 3.88, 0.07, z);
beamBetween(
new THREE.Vector3(3.87, 2.35, z),
new THREE.Vector3(3.08, 3.30, z),
0.10,
materials.metal
);
}
// ---------------------------------------------------------------------
// Market counter
// ---------------------------------------------------------------------
addBox(3.05, 1.27, 5.1, materials.counterDark, 1.2, 0.72, 0);
addBox(3.36, 0.22, 5.45, materials.counterTop, 1.2, 1.465, 0);
// Front vertical boards
for (let i = 0; i < 10; i++) {
const z = -2.29 + i * 0.51;
addBox(
0.12,
1.08,
0.45,
i % 2 === 0 ? materials.slatA : materials.slatB,
2.78,
0.76,
z
);
}
addBox(0.16, 0.13, 5.08, materials.counterTop, 2.80, 0.80, 0);
addBox(0.16, 0.13, 5.08, materials.counterTop, 2.80, 1.24, 0);
// Counter side panels
addBox(2.9, 1.15, 0.12, materials.counter, 1.2, 0.73, -2.58);
addBox(2.9, 1.15, 0.12, materials.counter, 1.2, 0.73, 2.58);
// ---------------------------------------------------------------------
// Produce crates and blocky fruit
// ---------------------------------------------------------------------
function addCrate(x, z, fruitMaterial) {
const y = 1.72;
addBox(0.78, 0.10, 0.84, materials.crateDark, x, y - 0.12, z);
addBox(0.08, 0.28, 0.84, materials.crate, x - 0.35, y, z);
addBox(0.08, 0.28, 0.84, materials.crate, x + 0.35, y, z);
addBox(0.70, 0.28, 0.08, materials.crate, x, y, z - 0.38);
addBox(0.70, 0.28, 0.08, materials.crate, x, y, z + 0.38);
const offsets = [
[-0.18, -0.18],
[0.12, -0.19],
[-0.16, 0.14],
[0.16, 0.15],
[0.02, 0.02]
];
offsets.forEach(([ox, oz], index) => {
const mat =
index === 4 ? materials.leafLight : fruitMaterial;
addBox(
0.19,
0.19,
0.19,
mat,
x + ox,
y + 0.19 + (index === 4 ? 0.16 : 0),
z + oz
);
});
}
addCrate(-0.05, -1.35, materials.apple);
addCrate(0.92, 0.22, materials.lemon);
addCrate(1.92, -0.66, materials.orange);
addCrate(1.60, 1.70, materials.appleLight);
// A few stacked jars on the rear portion of the counter
const jarColors = [
materials.ceramicBlue,
materials.ceramic,
materials.ceramicBlue
];
for (let i = 0; i < 3; i++) {
const x = -0.1 + i * 0.36;
const z = 1.42;
addBox(0.27, 0.32, 0.27, jarColors[i], x, 1.74, z);
addBox(0.17, 0.10, 0.17, materials.crateDark, x, 1.95, z);
}
// ---------------------------------------------------------------------
// Small section-direction arrow above the sloping canopy
// ---------------------------------------------------------------------
const fallDirection = new THREE.Vector3(
awningEndX - awningStartX,
awningEndY - awningStartY,
0
).normalize();
const fallArrow = new THREE.ArrowHelper(
fallDirection,
new THREE.Vector3(-2.75, 5.47, 3.66),
6.75,
0x287c80,
0.34,
0.19
);
fallArrow.line.material.transparent = true;
fallArrow.line.material.opacity = 0.68;
fallArrow.cone.material.transparent = true;
fallArrow.cone.material.opacity = 0.82;
scene.add(fallArrow);
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset
// ---------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.48,
1.46
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetZoom = THREE.MathUtils.clamp(
cameraState.targetZoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.85
);
},
{ passive: false }
);
function resetView() {
cameraState.targetTheta = 0.48;
cameraState.targetPhi = 1.19;
cameraState.targetZoom = 1;
}
document.getElementById("reset").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
target.y + cameraState.radius * cosPhi,
target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(target);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfHeight = 4.75;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// ---------------------------------------------------------------------
// Projected HTML annotations
// ---------------------------------------------------------------------
const annotationData = [
{
element: document.getElementById("slope-label"),
point: new THREE.Vector3(-0.15, 4.42, 3.55),
offset: new THREE.Vector2(0, -54)
},
{
element: document.getElementById("gutter-label"),
point: new THREE.Vector3(4.5, 3.18, 2.5),
offset: new THREE.Vector2(85, -18)
},
{
element: document.getElementById("counter-label"),
point: new THREE.Vector3(2.84, 0.82, -1.3),
offset: new THREE.Vector2(82, 18)
}
];
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotationData.forEach(({ element, point, offset }) => {
const projected = point.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width + offset.x;
const y = (-projected.y * 0.5 + 0.5) * height + offset.y;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity =
projected.z > -1 && projected.z < 1 ? "1" : "0";
});
}
// ---------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------
let previousTime = performance.now();
renderer.setAnimationLoop((time) => {
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
const smoothing = 1 - Math.exp(-10 * delta);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * smoothing;
cameraState.zoom +=
(cameraState.targetZoom - cameraState.zoom) * smoothing;
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
updateCamera();
updateAnnotations();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```