Prompt specification
Build or customize the Three.js 3D scene "Two Blocks Casting One Joined Shadow" 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: Two Blocks Casting One Joined Shadow
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-blocks-casting-one-joined-shadow-x3497
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts two stylized solid blocks casting a single joined shadow. Put a tall narrow block and a shorter wide block at different depths between one light and a pale receiving wall. Mark one useful alignment on the floor where their shadows touch, while an overhead view clearly reveals the gap between the actual blocks. Let the viewer slide the nearer block sideways and watch the shadow separate again. Use simple contrasting materials so solid geometry and projected shapes remain distinguishable.
Working Three.js Starter (two-blocks-casting-one-joined-shadow-x3497):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Light — Two Blocks — One Shadow</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(15, 22, 28, 0.86);
--border: rgba(255, 255, 255, 0.13);
--text: #f3f4ef;
--muted: #aebbc0;
--amber: #ffc857;
--coral: #ef6848;
--teal: #2a8299;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #111820;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
position: fixed;
z-index: 5;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px 19px 16px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--amber);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.4vw, 30px);
line-height: 1.06;
letter-spacing: -0.035em;
}
#intro p {
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
margin-top: 13px;
color: #dce3e3;
font-size: 11px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
background: var(--teal);
}
.swatch.near {
background: var(--coral);
}
.swatch.light {
background: var(--amber);
border-radius: 50%;
box-shadow: 0 0 8px rgba(255, 200, 87, 0.7);
}
#state-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
font-size: 12px;
}
#state {
padding: 4px 8px;
border-radius: 999px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#state.joined {
color: #132019;
background: #85d6a4;
}
#state.separated {
color: #24140f;
background: var(--amber);
}
#state.overlapping {
color: white;
background: var(--coral);
}
#state-detail {
color: var(--muted);
}
#plan-panel {
position: fixed;
z-index: 5;
top: 20px;
right: 20px;
width: 320px;
overflow: hidden;
pointer-events: none;
}
.plan-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 11px 13px;
border-bottom: 1px solid var(--border);
color: #dfe6e5;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.plan-head span:last-child {
color: var(--amber);
}
#plan-wrap {
position: relative;
width: 100%;
height: 190px;
background: #d9dfdc;
}
#plan-canvas {
display: block;
width: 100%;
height: 100%;
}
#measure {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#measure-line {
stroke: #d64c2d;
stroke-width: 1.7;
}
#measure-text {
fill: #a8321c;
stroke: rgba(255, 255, 255, 0.9);
stroke-width: 3px;
paint-order: stroke;
font-family: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
}
.plan-foot {
padding: 10px 13px 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#controls {
position: fixed;
z-index: 6;
left: 50%;
bottom: 20px;
width: min(650px, calc(100vw - 32px));
padding: 14px 16px 13px;
transform: translateX(-50%);
}
.control-row {
display: grid;
grid-template-columns: 150px 1fr 68px;
align-items: center;
gap: 14px;
}
.control-label {
font-size: 12px;
font-weight: 700;
}
#position-output {
color: var(--amber);
font-variant-numeric: tabular-nums;
text-align: right;
font-size: 12px;
}
input[type="range"] {
width: 100%;
accent-color: var(--coral);
cursor: ew-resize;
}
.button-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-top: 10px;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 9px;
padding: 7px 10px;
color: #e8eeee;
background: rgba(255, 255, 255, 0.07);
font: inherit;
font-size: 11px;
cursor: pointer;
}
button:hover {
background: rgba(255, 255, 255, 0.13);
}
button.primary {
border-color: rgba(255, 200, 87, 0.4);
color: var(--amber);
}
#hint {
margin-left: auto;
color: var(--muted);
font-size: 10px;
}
#floor-label {
position: fixed;
z-index: 4;
display: none;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(255, 200, 87, 0.65);
border-radius: 8px;
color: #ffe5a3;
background: rgba(20, 25, 28, 0.88);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
pointer-events: none;
white-space: nowrap;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#floor-label::after {
position: absolute;
left: 50%;
bottom: -6px;
width: 1px;
height: 6px;
background: var(--amber);
content: "";
}
@media (max-width: 720px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
#intro p {
display: none;
}
.legend {
margin-top: 10px;
}
#plan-panel {
top: auto;
right: 12px;
bottom: 126px;
width: 214px;
}
#plan-wrap {
height: 130px;
}
.plan-foot {
display: none;
}
#controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
.control-row {
grid-template-columns: 118px 1fr 52px;
gap: 9px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive Three.js shadow scene"></canvas>
<section id="intro" class="panel">
<div class="eyebrow">Geometry / Light / Projection</div>
<h1>One light.<br>Two solids. One silhouette.</h1>
<p>
At the starting position, the two cast edges meet on the pale wall.
Slide the coral block right and the single shadow separates again.
</p>
<div class="legend">
<span><i class="swatch"></i> Tall block</span>
<span><i class="swatch near"></i> Near block</span>
<span><i class="swatch light"></i> Single key light</span>
</div>
<div id="state-row" aria-live="polite">
<span id="state" class="joined">Joined</span>
<span id="state-detail">Wall shadow edges meet</span>
</div>
</section>
<aside id="plan-panel" class="panel">
<div class="plan-head">
<span>Overhead / plan</span>
<span>Actual solids</span>
</div>
<div id="plan-wrap">
<canvas id="plan-canvas"></canvas>
<svg id="measure" aria-hidden="true">
<defs>
<marker
id="arrow"
markerWidth="7"
markerHeight="7"
refX="3.5"
refY="3.5"
orient="auto-start-reverse"
>
<path d="M 0 0 L 7 3.5 L 0 7 z" fill="#d64c2d"></path>
</marker>
</defs>
<line
id="measure-line"
x1="0"
y1="0"
x2="0"
y2="0"
marker-start="url(#arrow)"
marker-end="url(#arrow)"
></line>
<text id="measure-text" x="0" y="0" text-anchor="middle"></text>
</svg>
</div>
<div class="plan-foot">
The orange dimension measures the real floor gap between the two blocks,
not their projected shadows.
</div>
</aside>
<div id="floor-label">Floor alignment / wall-shadow seam</div>
<section id="controls" class="panel">
<div class="control-row">
<label class="control-label" for="near-slider">
Slide nearer block
</label>
<input
id="near-slider"
type="range"
min="-1.2"
max="1.2"
step="0.01"
value="0"
aria-label="Slide the nearer block sideways"
>
<output id="position-output" for="near-slider">X 2.10</output>
</div>
<div class="button-row">
<button id="separate-button" class="primary">Separate shadow</button>
<button id="join-button">Rejoin shadow</button>
<button id="reset-button">Reset camera</button>
<span id="hint">Drag the coral block or orbit the empty scene</span>
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const mainCanvas = document.querySelector("#scene");
const planCanvas = document.querySelector("#plan-canvas");
const slider = document.querySelector("#near-slider");
const positionOutput = document.querySelector("#position-output");
const stateChip = document.querySelector("#state");
const stateDetail = document.querySelector("#state-detail");
const floorLabel = document.querySelector("#floor-label");
const TALL = {
x: 0.8,
z: -2.4,
width: 0.8,
height: 3.2,
depth: 0.9
};
const NEAR = {
x: 2.1,
z: -1.8,
width: 1.4,
height: 1.55,
depth: 1.0
};
const INITIAL_NEAR_X = NEAR.x;
const WALL_Z = -6;
const LIGHT_POSITION = new THREE.Vector3(4, 0.35, 6);
const LIGHT_TARGET = new THREE.Vector3(0, 0.35, WALL_Z);
// Main renderer.
const renderer = new THREE.WebGLRenderer({
canvas: mainCanvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setClearColor(0x111820, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0.65, 1.25, -2.2);
const spherical = new THREE.Spherical(15.8, 1.22, 0.62);
function updateMainCamera() {
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.42, 1.48);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 9, 24);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateMainCamera();
// Lighting: one shadow-casting directional key light.
scene.add(new THREE.AmbientLight(0xffffff, 0.62));
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.0);
keyLight.position.copy(LIGHT_POSITION);
keyLight.target.position.copy(LIGHT_TARGET);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00025;
keyLight.shadow.normalBias = 0.012;
keyLight.shadow.radius = 2;
keyLight.shadow.camera.updateProjectionMatrix();
scene.add(keyLight, keyLight.target);
// Pale receiving wall.
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xeee9dd,
roughness: 1,
metalness: 0
});
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(18, 8),
wallMaterial
);
wall.position.set(0, 4, WALL_Z);
wall.receiveShadow = true;
scene.add(wall);
// Floor.
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x687477,
roughness: 0.96,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(18, 14),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(0, 0, 1);
floor.receiveShadow = true;
scene.add(floor);
// Subtle floor grid.
const gridVertices = [];
for (let x = -8; x <= 8; x += 1) {
gridVertices.push(x, 0.012, WALL_Z, x, 0.012, 8);
}
for (let z = WALL_Z; z <= 8; z += 1) {
gridVertices.push(-8, 0.012, z, 8, 0.012, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0xc3ced0,
transparent: true,
opacity: 0.16
})
);
scene.add(grid);
// Solid blocks.
const tallMaterial = new THREE.MeshStandardMaterial({
color: 0x20778d,
roughness: 0.68,
metalness: 0
});
const nearMaterial = new THREE.MeshStandardMaterial({
color: 0xe96647,
roughness: 0.64,
metalness: 0
});
const tallGeometry = new THREE.BoxGeometry(
TALL.width,
TALL.height,
TALL.depth
);
const nearGeometry = new THREE.BoxGeometry(
NEAR.width,
NEAR.height,
NEAR.depth
);
const tallBlock = new THREE.Mesh(tallGeometry, tallMaterial);
tallBlock.position.set(TALL.x, TALL.height / 2, TALL.z);
tallBlock.castShadow = true;
tallBlock.receiveShadow = true;
const nearBlock = new THREE.Mesh(nearGeometry, nearMaterial);
nearBlock.position.set(NEAR.x, NEAR.height / 2, NEAR.z);
nearBlock.castShadow = true;
nearBlock.receiveShadow = true;
const mainEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x10262d,
transparent: true,
opacity: 0.38
});
tallBlock.add(
new THREE.LineSegments(
new THREE.EdgesGeometry(tallGeometry),
mainEdgeMaterial
)
);
nearBlock.add(
new THREE.LineSegments(
new THREE.EdgesGeometry(nearGeometry),
mainEdgeMaterial
)
);
scene.add(tallBlock, nearBlock);
// Visible representation of the key light.
const lamp = new THREE.Group();
lamp.position.copy(LIGHT_POSITION);
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 24, 16),
new THREE.MeshBasicMaterial({ color: 0xffedaf })
);
const bulbRing = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.025, 8, 40),
new THREE.MeshBasicMaterial({ color: 0xffc857 })
);
lamp.add(bulb, bulbRing);
scene.add(lamp);
// The floor marker is directly below the wall-shadow contact point.
// With the default positions, the projected wall intervals are:
// tall: [-0.8, 0], near: [0, 1.4], so they touch at x = 0.
const wallSeamX =
TALL.x + TALL.width / 2 - (TALL.z - WALL_Z) / 3;
const alignmentMarker = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.025, 8, 40),
new THREE.MeshBasicMaterial({ color: 0xffc857 })
);
alignmentMarker.rotation.x = Math.PI / 2;
alignmentMarker.position.set(wallSeamX, 0.035, WALL_Z + 0.16);
scene.add(alignmentMarker);
// A separate overhead scene avoids sharing shadow-map resources between
// two WebGL renderers. Its block positions mirror the actual main blocks.
const planRenderer = new THREE.WebGLRenderer({
canvas: planCanvas,
antialias: true
});
planRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
planRenderer.setClearColor(0xd9dfdc, 1);
planRenderer.outputColorSpace = THREE.SRGBColorSpace;
planRenderer.toneMapping = THREE.ACESFilmicToneMapping;
planRenderer.toneMappingExposure = 1.1;
const planScene = new THREE.Scene();
planScene.add(new THREE.HemisphereLight(0xffffff, 0x667275, 2.4));
const planFloor = new THREE.Mesh(
new THREE.PlaneGeometry(8, 5),
new THREE.MeshStandardMaterial({
color: 0xd9dfdc,
roughness: 1
})
);
planFloor.rotation.x = -Math.PI / 2;
planFloor.position.set(1.6, -0.01, -2);
planScene.add(planFloor);
const planGrid = new THREE.GridHelper(8, 16, 0x839092, 0x9ca7a8);
planGrid.position.set(1.6, 0.006, -2);
planGrid.material.transparent = true;
planGrid.material.opacity = 0.23;
planScene.add(planGrid);
const planTall = new THREE.Mesh(tallGeometry, tallMaterial);
planTall.position.set(TALL.x, TALL.height / 2, TALL.z);
const planNear = new THREE.Mesh(nearGeometry, nearMaterial);
planNear.position.set(NEAR.x, NEAR.height / 2, NEAR.z);
const planEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x203238,
transparent: true,
opacity: 0.72
});
planTall.add(
new THREE.LineSegments(
new THREE.EdgesGeometry(tallGeometry),
planEdgeMaterial
)
);
planNear.add(
new THREE.LineSegments(
new THREE.EdgesGeometry(nearGeometry),
planEdgeMaterial
)
);
planScene.add(planTall, planNear);
const planCamera = new THREE.OrthographicCamera(
-1.5, 1.5, 1, -1, 0.1, 30
);
planCamera.up.set(0, 0, -1);
function updatePlanFraming() {
const width = planCanvas.clientWidth || 320;
const height = planCanvas.clientHeight || 190;
const aspect = width / height;
const leftEdge = Math.min(TALL.x - TALL.width / 2, planNear.position.x - NEAR.width / 2) - 0.28;
const rightEdge = Math.max(TALL.x + TALL.width / 2, planNear.position.x + NEAR.width / 2) + 0.28;
const centerX = (leftEdge + rightEdge) / 2;
const halfWidth = Math.max(1.45, (rightEdge - leftEdge) / 2);
const halfHeight = Math.max(1.05, halfWidth / aspect);
planCamera.left = -halfWidth;
planCamera.right = halfWidth;
planCamera.top = halfHeight;
planCamera.bottom = -halfHeight;
planCamera.position.set(centerX, 12, -2);
planCamera.lookAt(centerX, 0, -2);
planCamera.updateProjectionMatrix();
planCamera.updateMatrixWorld();
}
function resizePlan() {
const width = Math.max(1, planCanvas.clientWidth);
const height = Math.max(1, planCanvas.clientHeight);
planRenderer.setSize(width, height, false);
document.querySelector("#measure").setAttribute(
"viewBox",
`0 0 ${width} ${height}`
);
updatePlanFraming();
}
const planResizeObserver = new ResizeObserver(resizePlan);
planResizeObserver.observe(planCanvas);
resizePlan();
// State and interaction.
function projectedWallShift(z) {
// Parallel rays from (4, 0.35, 6) toward (0, 0.35, -6).
return -(z - WALL_Z) / 3;
}
function setNearX(x) {
const clampedX = THREE.MathUtils.clamp(x, 0.9, 3.4);
nearBlock.position.x = clampedX;
planNear.position.x = clampedX;
const offset = clampedX - INITIAL_NEAR_X;
slider.value = offset.toFixed(2);
positionOutput.textContent = `X ${clampedX.toFixed(2)}`;
const tallWallRight =
TALL.x + TALL.width / 2 + projectedWallShift(TALL.z);
const nearWallLeft =
clampedX - NEAR.width / 2 + projectedWallShift(NEAR.z);
const shadowGap = nearWallLeft - tallWallRight;
const actualGap =
(clampedX - NEAR.width / 2) -
(TALL.x + TALL.width / 2);
let state;
let detail;
if (Math.abs(shadowGap) < 0.018) {
state = "joined";
detail = "Wall shadow edges meet";
} else if (shadowGap > 0) {
state = "separated";
detail = `${shadowGap.toFixed(2)} wall-shadow gap`;
} else {
state = "overlapping";
detail = `${Math.abs(shadowGap).Fixed ? "" : ""}`;
detail = `${Math.abs(shadowGap).toFixed(2)} shadow overlap`;
}
stateChip.className = state;
stateChip.textContent =
state === "joined"
? "Joined"
: state === "separated"
? "Separated"
: "Overlapping";
stateDetail.textContent = detail;
document.querySelector("#measure-text").dataset.actualGap =
actualGap.toFixed(2);
updatePlanFraming();
}
slider.addEventListener("input", () => {
setNearX(INITIAL_NEAR_X + Number(slider.value));
});
document.querySelector("#separate-button").addEventListener("click", () => {
setNearX(INITIAL_NEAR_X + 0.9);
});
document.querySelector("#join-button").addEventListener("click", () => {
setNearX(INITIAL_NEAR_X);
});
document.querySelector("#reset-button").addEventListener("click", () => {
spherical.set(15.8, 1.22, 0.62);
updateMainCamera();
});
// Direct manipulation: drag the coral block sideways, or drag empty space
// to orbit. Camera behavior is implemented here without any addons.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(
new THREE.Vector3(0, 0, 1),
-NEAR.z
);
const dragPoint = new THREE.Vector3();
let dragMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
let dragOffsetX = 0;
function updatePointer(event) {
const rect = mainCanvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
mainCanvas.addEventListener("pointerdown", (event) => {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(nearBlock, false);
if (hits.length) {
dragMode = "block";
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffsetX = dragPoint.x - nearBlock.position.x;
}
} else {
dragMode = "orbit";
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
mainCanvas.setPointerCapture(event.pointerId);
mainCanvas.style.cursor = dragMode === "block" ? "ew-resize" : "grabbing";
event.preventDefault();
});
mainCanvas.addEventListener("pointermove", (event) => {
if (!dragMode) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hovering =
raycaster.intersectObject(nearBlock, false).length > 0;
mainCanvas.style.cursor = hovering ? "ew-resize" : "grab";
return;
}
if (dragMode === "orbit") {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.005;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateMainCamera();
} else if (dragMode === "block") {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
setNearX(dragPoint.x - dragOffsetX);
}
}
event.preventDefault();
});
function endDrag(event) {
dragMode = null;
mainCanvas.style.cursor = "grab";
if (mainCanvas.hasPointerCapture(event.pointerId)) {
mainCanvas.releasePointerCapture(event.pointerId);
}
}
mainCanvas.addEventListener("pointerup", endDrag);
mainCanvas.addEventListener("pointercancel", endDrag);
mainCanvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
updateMainCamera();
},
{ passive: false }
);
// Project HTML labels and the overhead dimension line.
const labelWorldPosition = new THREE.Vector3(
wallSeamX,
0.16,
WALL_Z + 0.38
);
function updateFloorLabel() {
const projected = labelWorldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
floorLabel.style.display = visible ? "block" : "none";
if (visible) {
floorLabel.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
floorLabel.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
const measureLine = document.querySelector("#measure-line");
const measureText = document.querySelector("#measure-text");
function projectToPlan(worldPosition) {
const projected = worldPosition.clone().project(planCamera);
const width = planCanvas.clientWidth;
const height = planCanvas.clientHeight;
return {
x: (projected.x * 0.5 + 0.5) * width,
y: (-projected.y * 0.5 + 0.5) * height
};
}
function updateMeasurement() {
const tallRight = TALL.x + TALL.width / 2;
const nearLeft = planNear.position.x - NEAR.width / 2;
const gap = nearLeft - tallRight;
const p1 = projectToPlan(new THREE.Vector3(tallRight, 0.04, -1.02));
const p2 = projectToPlan(new THREE.Vector3(nearLeft, 0.04, -1.02));
measureLine.setAttribute("x1", p1.x);
measureLine.setAttribute("y1", p1.y);
measureLine.setAttribute("x2", p2.x);
measureLine.setAttribute("y2", p2.y);
measureLine.style.display = gap > 0.015 ? "" : "none";
measureText.setAttribute("x", (p1.x + p2.x) / 2);
measureText.setAttribute("y", Math.min(p1.y, p2.y) - 8);
measureText.textContent =
gap > 0.015 ? `${gap.toFixed(2)} GAP` : "OVERLAP";
}
function resizeMain() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resizeMain);
setNearX(INITIAL_NEAR_X);
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
planRenderer.render(planScene, planCamera);
updateFloorLabel();
updateMeasurement();
}
animate();
</script>
</body>
</html>
```