Prompt specification
Build or customize the Three.js 3D scene "Perspective Frame and Three Posts" 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: Perspective Frame and Three Posts
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/perspective-frame-and-three-posts-x3317
Scene Description Prompt:
Perspective frame and three posts. Place three identical posts behind a movable viewing frame. Show how apparent size changes with depth while actual world-space dimensions remain equal. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (perspective-frame-and-three-posts-x3317):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Perspective Frame — Equal Posts, Changing Apparent Size</title>
<style>
:root {
color-scheme: dark;
--bg: #07141d;
--panel: rgba(9, 28, 38, 0.86);
--line: rgba(149, 214, 220, 0.18);
--text: #edf9f8;
--muted: #91aeb4;
--cyan: #55e0ce;
--orange: #ffb45c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 52% 40%, #123342 0%, #07141d 56%, #040c12 100%);
color: var(--text);
}
button,
input {
font: inherit;
}
#stage,
#stage canvas,
#labels {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas.dragging {
cursor: grabbing;
}
#labels {
pointer-events: none;
overflow: hidden;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
header {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
max-width: 470px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 2px;
content: "";
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(28px, 3.2vw, 48px);
font-weight: 720;
line-height: 1.03;
letter-spacing: -0.045em;
}
header p {
max-width: 430px;
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.controls {
bottom: 24px;
left: 24px;
width: 350px;
padding: 18px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.control-head strong {
font-size: 13px;
letter-spacing: 0.02em;
}
.depth-pill {
padding: 5px 9px;
border: 1px solid rgba(255, 180, 92, 0.25);
border-radius: 999px;
color: var(--orange);
background: rgba(255, 180, 92, 0.08);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 8px;
appearance: none;
border-radius: 99px;
outline: none;
background: linear-gradient(90deg, #244b59, var(--orange));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #102b35;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 1px var(--orange), 0 0 18px rgba(255, 180, 92, 0.42);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #102b35;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 1px var(--orange);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.button-row {
display: flex;
gap: 7px;
}
button {
flex: 1;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(85, 224, 206, 0.5);
background: rgba(85, 224, 206, 0.08);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(85, 224, 206, 0.55);
color: var(--cyan);
background: rgba(85, 224, 206, 0.11);
}
.camera-help {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.metrics {
top: 24px;
right: 24px;
width: 326px;
padding: 17px;
}
.metrics-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.metrics-title strong {
font-size: 13px;
}
.equal-size {
padding: 5px 8px;
border: 1px solid rgba(85, 224, 206, 0.26);
border-radius: 999px;
color: var(--cyan);
background: rgba(85, 224, 206, 0.08);
font-size: 10px;
font-weight: 800;
white-space: nowrap;
}
.dimension-card {
margin-bottom: 13px;
padding: 11px 12px;
border: 1px solid rgba(85, 224, 206, 0.16);
border-radius: 10px;
background: rgba(85, 224, 206, 0.045);
}
.dimension-card small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.dimension-card b {
color: var(--text);
font-size: 15px;
font-variant-numeric: tabular-nums;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
th {
padding: 0 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-align: right;
text-transform: uppercase;
}
th:first-child,
td:first-child {
text-align: left;
}
td {
padding: 9px 0;
border-top: 1px solid rgba(149, 214, 220, 0.1);
text-align: right;
}
.post-id {
display: inline-flex;
align-items: center;
gap: 7px;
font-weight: 800;
}
.post-id::before {
width: 7px;
height: 7px;
content: "";
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(85, 224, 206, 0.5);
}
.bar {
display: inline-block;
width: 54px;
height: 4px;
margin-right: 7px;
overflow: hidden;
vertical-align: middle;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.bar i {
display: block;
width: 50%;
height: 100%;
border-radius: inherit;
background: var(--cyan);
transition: width 100ms linear;
}
.ratio {
color: var(--muted);
font-size: 10px;
}
.legend {
position: fixed;
z-index: 5;
right: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 15px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(9, 28, 38, 0.74);
font-size: 10px;
backdrop-filter: blur(12px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
}
.legend .frame-key {
background: var(--orange);
}
.world-label {
position: absolute;
z-index: 4;
display: flex;
flex-direction: column;
gap: 3px;
min-width: 88px;
padding: 7px 9px;
border: 1px solid rgba(85, 224, 206, 0.24);
border-radius: 8px;
color: var(--text);
background: rgba(5, 19, 26, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
opacity: 0;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
backdrop-filter: blur(8px);
}
.world-label::after {
position: absolute;
bottom: -7px;
left: 50%;
width: 8px;
height: 8px;
content: "";
border-right: 1px solid rgba(85, 224, 206, 0.24);
border-bottom: 1px solid rgba(85, 224, 206, 0.24);
background: rgba(5, 19, 26, 0.82);
transform: translateX(-50%) rotate(45deg);
}
.world-label strong {
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.08em;
}
.world-label small {
color: #a9c0c4;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.world-label.frame-label {
border-color: rgba(255, 180, 92, 0.3);
}
.world-label.frame-label strong {
color: var(--orange);
}
.world-label.frame-label::after {
border-color: rgba(255, 180, 92, 0.3);
}
@media (max-width: 760px) {
header {
top: 14px;
left: 14px;
right: 14px;
}
h1 {
max-width: 330px;
font-size: 29px;
}
header p {
display: none;
}
.metrics {
top: 104px;
right: 12px;
width: 230px;
padding: 12px;
}
.dimension-card {
padding: 8px 9px;
}
.dimension-card b {
font-size: 12px;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 14px;
}
.legend {
display: none;
}
.world-label {
min-width: 72px;
padding: 5px 7px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels"></div>
<header>
<div class="eyebrow">Perspective demonstration</div>
<h1>Equal in the world.<br>Different on screen.</h1>
<p>
Three posts have identical world-space dimensions. Moving the viewing frame
reveals how perspective changes their apparent size as depth increases.
</p>
</header>
<section class="panel controls" aria-label="Scene controls">
<div class="control-head">
<strong>Move viewing frame</strong>
<span class="depth-pill" id="frameReadout">z +0.0</span>
</div>
<input
id="frameRange"
type="range"
min="0"
max="100"
value="24"
aria-label="Viewing frame depth"
>
<div class="range-ends">
<span>Farther back</span>
<span>Closer to camera</span>
</div>
<div class="button-row">
<button id="farButton" type="button">Move back</button>
<button id="nearButton" type="button">Move forward</button>
<button id="autoButton" type="button">Auto sweep</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<p class="camera-help">
Drag to orbit · Scroll to zoom · Double-click to reset<br>
The frame and all three posts remain equal in world size.
</p>
</section>
<section class="panel metrics" aria-label="Post measurements">
<div class="metrics-title">
<strong>Live measurements</strong>
<span class="equal-size">World size = equal</span>
</div>
<div class="dimension-card">
<small>Each post — width × height × depth</small>
<b>0.80 × 3.60 × 0.80 units</b>
</div>
<table>
<thead>
<tr>
<th>Post</th>
<th>Camera depth</th>
<th>Screen height</th>
<th>Apparent ratio</th>
</tr>
</thead>
<tbody id="measurementRows"></tbody>
</table>
</section>
<div class="legend">
<span><i></i>Identical posts</span>
<span><i class="frame-key"></i>Movable frame</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const labelLayer = document.querySelector("#labels");
const frameRange = document.querySelector("#frameRange");
const frameReadout = document.querySelector("#frameReadout");
const autoButton = document.querySelector("#autoButton");
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
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.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.7, -5);
let currentYaw = 0;
let currentPitch = 0.08;
let currentRadius = 22;
let targetYaw = currentYaw;
let targetPitch = currentPitch;
let targetRadius = currentRadius;
const hemisphere = new THREE.HemisphereLight(0xc8f3ff, 0x071018, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(8, 14, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x2ad8c5, 26, 34, 2);
rimLight.position.set(-9, 5, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa84d, 18, 28, 2);
warmLight.position.set(8, 3, 3);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: 0x091b25,
roughness: 0.96,
metalness: 0.04
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.012;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(80, 40, 0x315a69, 0x193744);
grid.position.y = 0.004;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
// Deterministic, low-poly atmospheric particles.
let seed = 9137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
const dustPositions = new Float32Array(150 * 3);
for (let i = 0; i < 150; i++) {
dustPositions[i * 3] = (random() - 0.5) * 42;
dustPositions[i * 3 + 1] = random() * 11 + 0.2;
dustPositions[i * 3 + 2] = random() * 38 - 28;
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8ed6d3,
size: 0.035,
transparent: true,
opacity: 0.35,
depthWrite: false
})
);
scene.add(dust);
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x39d8c1,
roughness: 0.38,
metalness: 0.16,
emissive: 0x063d37,
emissiveIntensity: 0.35
});
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x183845,
roughness: 0.72,
metalness: 0.25
});
const dimensionMaterial = new THREE.LineBasicMaterial({
color: 0xffc16f,
transparent: true,
opacity: 0.72
});
const postDefinitions = [
{ id: "A", x: -4.1, z: -4.5 },
{ id: "B", x: 0, z: -9.0 },
{ id: "C", x: 4.1, z: -13.5 }
];
const postHeight = 3.6;
function makePost(definition) {
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
// The post itself is exactly 0.8 × 3.6 × 0.8 units.
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 3.4, 0.8),
postMaterial
);
shaft.position.y = 1.7;
shaft.castShadow = true;
shaft.receiveShadow = true;
group.add(shaft);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 0.2, 0.8),
postMaterial
);
cap.position.y = 3.5;
cap.castShadow = true;
cap.receiveShadow = true;
group.add(cap);
// A separate ground platform does not count as part of the post.
const platform = new THREE.Mesh(
new THREE.BoxGeometry(1.15, 0.08, 1.15),
platformMaterial
);
platform.position.y = -0.04;
platform.receiveShadow = true;
group.add(platform);
// World-space vertical dimension line.
const lineX = definition.x + 0.63;
const lineZ = definition.z;
const positions = [
lineX, 0, lineZ,
lineX, postHeight, lineZ,
lineX - 0.13, 0, lineZ,
lineX + 0.13, 0, lineZ,
lineX - 0.13, postHeight, lineZ,
lineX + 0.13, postHeight, lineZ
];
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const dimensions = new THREE.LineSegments(
dimensionGeometry,
dimensionMaterial
);
scene.add(dimensions);
scene.add(group);
const label = document.createElement("div");
label.className = "world-label";
label.innerHTML = `
<strong>POST ${definition.id}</strong>
<small>3.6 u tall</small>
`;
labelLayer.appendChild(label);
return { ...definition, group, label };
}
const posts = postDefinitions.map(makePost);
// Moving rectangular viewing frame.
const frameGroup = new THREE.Group();
const openingWidth = 8.6;
const openingHeight = 5.6;
const barThickness = 0.24;
const frameCenterY = 2.95;
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xe4f0f1,
roughness: 0.3,
metalness: 0.5,
emissive: 0x15343b,
emissiveIntensity: 0.18
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: 0xffad51,
roughness: 0.38,
metalness: 0.28,
emissive: 0x6d2c05,
emissiveIntensity: 0.2
});
const horizontalGeometry = new THREE.BoxGeometry(
openingWidth + barThickness * 2,
barThickness,
barThickness
);
const verticalGeometry = new THREE.BoxGeometry(
barThickness,
openingHeight + barThickness * 2,
barThickness
);
const topBar = new THREE.Mesh(horizontalGeometry, frameMaterial);
topBar.position.y =
frameCenterY + openingHeight / 2 + barThickness / 2;
topBar.castShadow = true;
frameGroup.add(topBar);
const bottomBar = new THREE.Mesh(horizontalGeometry, frameMaterial);
bottomBar.position.y =
frameCenterY - openingHeight / 2 - barThickness / 2;
bottomBar.castShadow = true;
frameGroup.add(bottomBar);
const leftBar = new THREE.Mesh(verticalGeometry, frameMaterial);
leftBar.position.x =
-openingWidth / 2 - barThickness / 2;
leftBar.position.y = frameCenterY;
leftBar.castShadow = true;
frameGroup.add(leftBar);
const rightBar = new THREE.Mesh(verticalGeometry, frameMaterial);
rightBar.position.x =
openingWidth / 2 + barThickness / 2;
rightBar.position.y = frameCenterY;
rightBar.castShadow = true;
frameGroup.add(rightBar);
const cornerGeometry = new THREE.BoxGeometry(0.42, 0.42, 0.42);
const cornerPositions = [
[-1, -1],
[-1, 1],
[1, -1],
[1, 1]
];
for (const [xSign, ySign] of cornerPositions) {
const corner = new THREE.Mesh(cornerGeometry, accentMaterial);
corner.position.set(
xSign * (openingWidth / 2 + barThickness / 2),
frameCenterY + ySign * (openingHeight / 2 + barThickness / 2),
0
);
corner.castShadow = true;
frameGroup.add(corner);
}
scene.add(frameGroup);
const frameLabel = document.createElement("div");
frameLabel.className = "world-label frame-label";
frameLabel.innerHTML = `
<strong>MOVING FRAME</strong>
<small>Perspective aperture</small>
`;
labelLayer.appendChild(frameLabel);
const guideGeometry = new THREE.BufferGeometry();
guideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[0, 0.025, 0, 0, 0.025, -14.5],
3
)
);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0xffb45c,
dashSize: 0.28,
gapSize: 0.18,
transparent: true,
opacity: 0.55
})
);
scene.add(guide);
// Build the live measurement rows.
const measurementRows = document.querySelector("#measurementRows");
const measurementRefs = {};
for (const post of postDefinitions) {
const row = document.createElement("tr");
row.innerHTML = `
<td><span class="post-id">${post.id}</span></td>
<td id="depth-${post.id}">—</td>
<td id="height-${post.id}">—</td>
<td>
<span class="bar"><i id="bar-${post.id}"></i></span>
<span class="ratio" id="ratio-${post.id}">—</span>
</td>
`;
measurementRows.appendChild(row);
measurementRefs[post.id] = {
depth: row.querySelector(`#depth-${post.id}`),
height: row.querySelector(`#height-${post.id}`),
bar: row.querySelector(`#bar-${post.id}`),
ratio: row.querySelector(`#ratio-${post.id}`)
};
}
const FAR_Z = -1.2;
const NEAR_Z = 4.0;
let frameZ = THREE.MathUtils.lerp(
FAR_Z,
NEAR_Z,
Number(frameRange.value) / 100
);
let targetFrameZ = frameZ;
let autoSweep = false;
function setAutoSweep(enabled) {
autoSweep = enabled;
autoButton.classList.toggle("active", enabled);
autoButton.textContent = enabled ? "Stop sweep" : "Auto sweep";
}
frameRange.addEventListener("input", () => {
setAutoSweep(false);
targetFrameZ = THREE.MathUtils.lerp(
FAR_Z,
NEAR_Z,
Number(frameRange.value) / 100
);
});
document.querySelector("#farButton").addEventListener("click", () => {
setAutoSweep(false);
targetFrameZ = FAR_Z;
frameRange.value = 0;
});
document.querySelector("#nearButton").addEventListener("click", () => {
setAutoSweep(false);
targetFrameZ = NEAR_Z;
frameRange.value = 100;
});
autoButton.addEventListener("click", () => {
setAutoSweep(!autoSweep);
});
function resetCamera() {
targetYaw = 0;
targetPitch = 0.08;
targetRadius = 22;
}
document.querySelector("#resetButton").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// Camera controls are implemented directly without Three.js addons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
targetYaw = THREE.MathUtils.clamp(
targetYaw - deltaX * 0.0035,
-0.5,
0.5
);
targetPitch = THREE.MathUtils.clamp(
targetPitch + deltaY * 0.0025,
-0.02,
0.25
);
});
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(
"wheel",
(event) => {
event.preventDefault();
targetRadius = THREE.MathUtils.clamp(
targetRadius + event.deltaY * 0.012,
21,
28
);
},
{ passive: false }
);
function damp(current, target, speed, deltaTime) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-speed * deltaTime)
);
}
function updateCamera(deltaTime) {
currentYaw = damp(currentYaw, targetYaw, 8, deltaTime);
currentPitch = damp(currentPitch, targetPitch, 8, deltaTime);
currentRadius = damp(currentRadius, targetRadius, 8, deltaTime);
const horizontalRadius =
currentRadius * Math.cos(currentPitch);
camera.position.set(
cameraTarget.x +
Math.sin(currentYaw) * horizontalRadius,
cameraTarget.y + Math.sin(currentPitch) * currentRadius,
cameraTarget.z +
Math.cos(currentYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
Math.abs(projected.x) < 1.3 &&
Math.abs(projected.y) < 1.3;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const projectedTop = new THREE.Vector3();
const projectedBottom = new THREE.Vector3();
const projectedDepth = new THREE.Vector3();
const frameAnchor = new THREE.Vector3();
function updateMeasurements() {
const heights = [];
for (const post of posts) {
projectedTop.set(post.x, postHeight, post.z).project(camera);
projectedBottom.set(post.x, 0, post.z).project(camera);
const screenHeight =
Math.abs(projectedTop.y - projectedBottom.y) *
window.innerHeight *
0.5;
heights.push(screenHeight);
projectedDepth
.set(post.x, postHeight / 2, post.z)
.applyMatrix4(camera.matrixWorldInverse);
const cameraDepth = -projectedDepth.z;
const references = measurementRefs[post.id];
references.depth.textContent = `${cameraDepth.toFixed(1)} u`;
references.height.textContent = `${Math.round(screenHeight)} px`;
post.label.querySelector("small").textContent =
`3.6 u · ${Math.round(screenHeight)} px`;
placeLabel(
post.label,
projectedTop.set(post.x, postHeight + 0.18, post.z)
);
}
if (heights[0] > 0) {
for (let i = 0; i < posts.length; i++) {
const post = posts[i];
const ratio = heights[i] / heights[0];
const references = measurementRefs[post.id];
references.bar.style.width = `${Math.max(6, ratio * 54)}px`;
references.ratio.textContent = `${Math.round(ratio * 100)}%`;
}
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = camera.aspect < 0.8 ? 64 : 45;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const elapsedTime = clock.elapsedTime;
if (autoSweep) {
frameZ = 1.4 + 2.6 * Math.sin(elapsedTime * 0.7);
frameRange.value = String(
((frameZ - FAR_Z) / (NEAR_Z - FAR_Z)) * 100
);
} else {
frameZ = damp(frameZ, targetFrameZ, 7, deltaTime);
}
frameGroup.position.z = frameZ;
const guidePositions = guide.geometry.attributes.position;
guidePositions.setZ(0, frameZ);
guidePositions.setZ(1, -14.5);
guidePositions.needsUpdate = true;
guide.computeLineDistances();
frameAnchor.set(0, frameCenterY, frameZ);
const cameraDistance = camera.position.distanceTo(frameAnchor);
frameReadout.textContent =
`z ${frameZ >= 0 ? "+" : ""}${frameZ.toFixed(1)} · ` +
`${cameraDistance.toFixed(1)} u away`;
placeLabel(
frameLabel,
new THREE.Vector3(
0,
frameCenterY + openingHeight / 2 + 0.34,
frameZ
)
);
updateCamera(deltaTime);
updateMeasurements();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```