Prompt specification
Build or customize the Three.js 3D scene "Square Mosaic Border Unit" 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: Square Mosaic Border Unit
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/square-mosaic-border-unit-x3305
Scene Description Prompt:
Square mosaic border unit. Model a small original mosaic unit with distinct corner and edge tiles. Show its repeat boundary and preserve equal tile spacing on a flat support. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (square-mosaic-border-unit-x3305):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Square Mosaic Border Unit</title>
<style>
:root {
--ink: #183c3b;
--muted: #66807c;
--panel: rgba(255, 253, 247, 0.88);
--line: rgba(24, 60, 59, 0.13);
--teal: #168f8c;
--gold: #d69b31;
--coral: #dc7053;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #f8f5ee 0%, #e9ece5 70%, #dfe5df 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(31, 62, 59, 0.10),
inset 0 1px 0 rgba(255, 255, 255, 0.7);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.info {
top: 24px;
left: 24px;
width: min(340px, calc(100vw - 48px));
padding: 22px 22px 20px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 36px);
font-weight: 500;
line-height: 1.03;
letter-spacing: -0.025em;
}
.subtitle {
margin: 12px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.legend-item {
min-width: 0;
}
.legend-item strong {
display: block;
margin-top: 7px;
font-size: 11px;
font-weight: 750;
}
.legend-item span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.swatch {
width: 22px;
height: 12px;
border-radius: 4px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.07);
}
.swatch.corner {
background: var(--coral);
clip-path: polygon(18% 0, 82% 0, 100% 18%, 100% 82%, 82% 100%, 18% 100%, 0 82%, 0 18%);
}
.swatch.edge {
background: #287c75;
}
.swatch.boundary {
border: 1px dashed var(--teal);
background: transparent;
}
.controls {
top: 24px;
right: 24px;
width: 230px;
padding: 17px;
}
.control-title {
margin-bottom: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.view-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-bottom: 14px;
}
button {
appearance: none;
border: 1px solid rgba(24, 60, 59, 0.13);
border-radius: 9px;
padding: 9px 6px;
background: rgba(255, 255, 255, 0.62);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease,
border-color 160ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(22, 143, 140, 0.42);
}
button.active {
border-color: rgba(22, 143, 140, 0.28);
background: #dcefeb;
color: #126c69;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 9px 0;
color: var(--ink);
font-size: 11px;
font-weight: 650;
cursor: pointer;
}
.toggle + .toggle {
border-top: 1px solid var(--line);
}
.toggle input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch {
position: relative;
width: 34px;
height: 19px;
flex: 0 0 auto;
border-radius: 20px;
background: #cbd5d1;
transition: background 160ms ease;
}
.switch::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 13px;
height: 13px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
transition: transform 160ms ease;
}
.toggle input:checked + .switch {
background: var(--teal);
}
.toggle input:checked + .switch::after {
transform: translateX(15px);
}
.metrics {
bottom: 24px;
left: 24px;
display: grid;
grid-template-columns: repeat(4, auto);
gap: 0;
padding: 14px 8px;
}
.metric {
min-width: 92px;
padding: 0 14px;
border-right: 1px solid var(--line);
}
.metric:last-child {
border-right: 0;
}
.metric span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 4px;
font-size: 13px;
font-weight: 750;
}
.hint {
right: 24px;
bottom: 24px;
padding: 11px 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.world-label {
position: fixed;
z-index: 8;
display: none;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 7px;
background: rgba(255, 253, 247, 0.90);
box-shadow: 0 4px 15px rgba(24, 60, 59, 0.10);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
}
.world-label.gap {
border-color: rgba(214, 155, 49, 0.35);
color: #996814;
}
.world-label.boundary {
border-color: rgba(22, 143, 140, 0.35);
color: #117c79;
}
.world-label.dimension {
color: var(--muted);
font-weight: 700;
}
@media (max-width: 760px) {
.info {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.subtitle,
.legend {
display: none;
}
.controls {
top: 126px;
right: 12px;
width: 186px;
padding: 12px;
}
.metrics {
left: 12px;
bottom: 12px;
right: 12px;
grid-template-columns: repeat(2, 1fr);
}
.metric {
min-width: 0;
padding: 8px 12px;
}
.metric:nth-child(2) {
border-right: 0;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel info">
<div class="eyebrow">Solid study / Mosaic 04</div>
<h1>Square Border Unit</h1>
<p class="subtitle">
A compact 5 × 5 perimeter module with clipped corner tiles,
directional edge tiles, and a precisely repeated square joint.
</p>
<div class="legend">
<div class="legend-item">
<div class="swatch corner"></div>
<strong>Corner</strong>
<span>Diamond relief</span>
</div>
<div class="legend-item">
<div class="swatch edge"></div>
<strong>Edge</strong>
<span>Bar relief</span>
</div>
<div class="legend-item">
<div class="swatch boundary"></div>
<strong>Boundary</strong>
<span>Repeat seam</span>
</div>
</div>
</section>
<section class="panel controls">
<div class="control-title">Camera</div>
<div class="view-buttons">
<button class="active" data-view="iso">ISO</button>
<button data-view="top">TOP</button>
<button data-view="front">FRONT</button>
</div>
<label class="toggle">
Repeat boundary
<input id="boundaryToggle" type="checkbox" checked />
<span class="switch"></span>
</label>
<label class="toggle">
Spacing guides
<input id="spacingToggle" type="checkbox" checked />
<span class="switch"></span>
</label>
</section>
<section class="panel metrics">
<div class="metric">
<span>Tile</span>
<strong>0.86 sq.</strong>
</div>
<div class="metric">
<span>Joint</span>
<strong>0.14</strong>
</div>
<div class="metric">
<span>Pitch</span>
<strong>1.00</strong>
</div>
<div class="metric">
<span>Envelope</span>
<strong>4.86 sq.</strong>
</div>
</section>
<div class="panel hint">
Drag to orbit · Scroll or pinch to zoom<br />
Top view verifies the square repeat and uniform joints.
</div>
<div id="gapLabel" class="world-label gap">0.14 JOINT</div>
<div id="boundaryLabel" class="world-label boundary">REPEAT / 4.86 × 4.86</div>
<div id="xLabel" class="world-label dimension">X REPEAT</div>
<div id="zLabel" class="world-label dimension">Z REPEAT</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f1eb);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
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 square mosaic border model");
document.querySelector("#scene").appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-5, 5, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x78908c, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d8, 3.8);
keyLight.position.set(5.5, 9, 4.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0003;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbde4e1, 1.35);
fillLight.position.set(-5, 4, -5);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Flat support and lower plinth
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xe2e7e1,
roughness: 1
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.315;
floor.receiveShadow = true;
scene.add(floor);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0xe9dfce,
roughness: 0.92,
metalness: 0
});
const support = new THREE.Mesh(
new THREE.BoxGeometry(6.3, 0.2, 6.3),
supportMaterial
);
support.position.y = -0.1;
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0xc9bba4,
roughness: 0.95
});
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(6.48, 0.1, 6.48),
plinthMaterial
);
plinth.position.y = -0.25;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
// -------------------------------------------------------------------------
// Mosaic construction
//
// The spacing is generated from a single pitch rather than positioned by
// hand. This guarantees equal orthogonal joints around the whole ring.
// -------------------------------------------------------------------------
const UNIT_CELLS = 5;
const TILE_SIZE = 0.86;
const GAP = 0.14;
const PITCH = TILE_SIZE + GAP;
const HALF_CELLS = (UNIT_CELLS - 1) / 2;
const TILE_HEIGHT = 0.14;
const BEVEL = 0.018;
const OUTER_EXTENT = HALF_CELLS * PITCH + TILE_SIZE / 2;
const mosaic = new THREE.Group();
scene.add(mosaic);
const cornerMaterial = new THREE.MeshStandardMaterial({
color: 0xdc7053,
roughness: 0.67,
metalness: 0.02
});
const edgeMaterials = [
new THREE.MeshStandardMaterial({
color: 0x27766f,
roughness: 0.68,
metalness: 0.03
}),
new THREE.MeshStandardMaterial({
color: 0x286e78,
roughness: 0.68,
metalness: 0.03
}),
new THREE.MeshStandardMaterial({
color: 0x32847a,
roughness: 0.68,
metalness: 0.03
}),
new THREE.MeshStandardMaterial({
color: 0x3a8675,
roughness: 0.68,
metalness: 0.03
})
];
const cornerReliefMaterial = new THREE.MeshStandardMaterial({
color: 0xf5e4c5,
roughness: 0.72
});
const edgeReliefMaterial = new THREE.MeshStandardMaterial({
color: 0xbfe1cc,
roughness: 0.74
});
function chamferedSquare(size, cut) {
const half = size / 2;
const shape = new THREE.Shape();
shape.moveTo(-half + cut, -half);
shape.lineTo(half - cut, -half);
shape.lineTo(half, -half + cut);
shape.lineTo(half, half - cut);
shape.lineTo(half - cut, half);
shape.lineTo(-half + cut, half);
shape.lineTo(-half, half - cut);
shape.lineTo(-half, -half + cut);
shape.closePath();
return shape;
}
function horizontalTileGeometry(cut) {
const geometry = new THREE.ExtrudeGeometry(
chamferedSquare(TILE_SIZE, cut),
{
depth: TILE_HEIGHT,
steps: 1,
curveSegments: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.018,
bevelThickness: BEVEL
}
);
// Extrusion now rises vertically from the support.
geometry.rotateX(-Math.PI / 2);
return geometry;
}
const cornerGeometry = horizontalTileGeometry(0.14);
const edgeGeometry = horizontalTileGeometry(0.065);
const cornerReliefGeometry = new THREE.CylinderGeometry(
0.12,
0.12,
0.035,
4
);
cornerReliefGeometry.rotateY(Math.PI / 4);
const edgeReliefGeometry = new THREE.BoxGeometry(
0.30,
0.03,
0.075
);
const topOfTile = BEVEL + TILE_HEIGHT + BEVEL;
function addCornerTile(x, z) {
const tile = new THREE.Mesh(cornerGeometry, cornerMaterial);
tile.position.set(x, BEVEL, z);
tile.castShadow = true;
tile.receiveShadow = true;
mosaic.add(tile);
const relief = new THREE.Mesh(
cornerReliefGeometry,
cornerReliefMaterial
);
relief.position.set(x, topOfTile + 0.016, z);
relief.castShadow = true;
mosaic.add(relief);
}
function addEdgeTile(x, z, sideIndex, alongZ = false) {
const tile = new THREE.Mesh(
edgeGeometry,
edgeMaterials[sideIndex]
);
tile.position.set(x, BEVEL, z);
if (alongZ) {
tile.rotation.y = Math.PI / 2;
}
tile.castShadow = true;
tile.receiveShadow = true;
mosaic.add(tile);
const relief = new THREE.Mesh(
edgeReliefGeometry,
edgeReliefMaterial
);
if (alongZ) {
relief.rotation.y = Math.PI / 2;
}
relief.position.set(x, topOfTile + 0.014, z);
relief.castShadow = true;
mosaic.add(relief);
}
// Create the 5 × 5 perimeter ring.
for (let row = 0; row < UNIT_CELLS; row++) {
for (let column = 0; column < UNIT_CELLS; column++) {
const isBorder =
row === 0 ||
row === UNIT_CELLS - 1 ||
column === 0 ||
column === UNIT_CELLS - 1;
if (!isBorder) continue;
const x = (column - HALF_CELLS) * PITCH;
const z = (row - HALF_CELLS) * PITCH;
const isCorner =
(row === 0 || row === UNIT_CELLS - 1) &&
(column === 0 || column === UNIT_CELLS - 1);
if (isCorner) {
addCornerTile(x, z);
} else if (row === 0) {
addEdgeTile(x, z, 0);
} else if (row === UNIT_CELLS - 1) {
addEdgeTile(x, z, 2);
} else if (column === 0) {
addEdgeTile(x, z, 3, true);
} else {
addEdgeTile(x, z, 1, true);
}
}
}
// -------------------------------------------------------------------------
// Repeat boundary: dashed square plus outward repeat chevrons
// -------------------------------------------------------------------------
const boundaryGroup = new THREE.Group();
scene.add(boundaryGroup);
const boundaryMaterial = new THREE.LineDashedMaterial({
color: 0x168f8c,
dashSize: 0.11,
gapSize: 0.075,
transparent: true,
opacity: 0.9
});
const boundaryPoints = [
new THREE.Vector3(-OUTER_EXTENT, 0.014, -OUTER_EXTENT),
new THREE.Vector3(OUTER_EXTENT, 0.014, -OUTER_EXTENT),
new THREE.Vector3(OUTER_EXTENT, 0.014, OUTER_EXTENT),
new THREE.Vector3(-OUTER_EXTENT, 0.014, OUTER_EXTENT),
new THREE.Vector3(-OUTER_EXTENT, 0.014, -OUTER_EXTENT)
];
const boundaryLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(boundaryPoints),
boundaryMaterial
);
boundaryLine.computeLineDistances();
boundaryGroup.add(boundaryLine);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x168f8c,
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide
});
function addFlatTriangle(vertices) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
const triangle = new THREE.Mesh(geometry, arrowMaterial);
boundaryGroup.add(triangle);
}
const a = OUTER_EXTENT;
const y = 0.018;
addFlatTriangle([
a + 0.14, y, 0,
a + 0.02, y, 0.075,
a + 0.02, y, -0.075
]);
addFlatTriangle([
-a - 0.14, y, 0,
-a - 0.02, y, 0.075,
-a - 0.02, y, -0.075
]);
addFlatTriangle([
0, y, -a - 0.14,
0.075, y, -a - 0.02,
-0.075, y, -a - 0.02
]);
addFlatTriangle([
0, y, a + 0.14,
0.075, y, a + 0.02,
-0.075, y, a + 0.02
]);
// -------------------------------------------------------------------------
// Equal-spacing guides: highlighted joint dimensions on all four sides
// -------------------------------------------------------------------------
const spacingGroup = new THREE.Group();
scene.add(spacingGroup);
const spacingVertices = [];
const guideY = topOfTile + 0.025;
function addGuideSegment(x1, y1, z1, x2, y2, z2) {
spacingVertices.push(x1, y1, z1, x2, y2, z2);
}
const gapCenter = 0.5;
const gapHalf = GAP / 2;
const cap = 0.055;
// Bottom and top gaps, measured along X.
for (const z of [-2, 2]) {
addGuideSegment(
gapCenter - gapHalf, guideY, z,
gapCenter + gapHalf, guideY, z
);
addGuideSegment(
gapCenter - gapHalf, guideY, z - cap,
gapCenter - gapHalf, guideY, z + cap
);
addGuideSegment(
gapCenter + gapHalf, guideY, z - cap,
gapCenter + gapHalf, guideY, z + cap
);
}
// Left and right gaps, measured along Z.
for (const x of [-2, 2]) {
addGuideSegment(
x, guideY, gapCenter - gapHalf,
x, guideY, gapCenter + gapHalf
);
addGuideSegment(
x - cap, guideY, gapCenter - gapHalf,
x + cap, guideY, gapCenter - gapHalf
);
addGuideSegment(
x - cap, guideY, gapCenter + gapHalf,
x + cap, guideY, gapCenter + gapHalf
);
}
const spacingGeometry = new THREE.BufferGeometry();
spacingGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(spacingVertices, 3)
);
const spacingLines = new THREE.LineSegments(
spacingGeometry,
new THREE.LineBasicMaterial({
color: 0xd69b31,
transparent: true,
opacity: 0.95
})
);
spacingGroup.add(spacingLines);
// -------------------------------------------------------------------------
// Envelope dimensions
// -------------------------------------------------------------------------
const dimensionVertices = [];
const dimensionY = 0.018;
const dimensionOffset = 2.76;
dimensionVertices.push(
-OUTER_EXTENT, dimensionY, dimensionOffset,
OUTER_EXTENT, dimensionY, dimensionOffset,
-OUTER_EXTENT, dimensionY, dimensionOffset - 0.07,
-OUTER_EXTENT, dimensionY, dimensionOffset + 0.07,
OUTER_EXTENT, dimensionY, dimensionOffset - 0.07,
OUTER_EXTENT, dimensionY, dimensionOffset + 0.07,
dimensionOffset, dimensionY, -OUTER_EXTENT,
dimensionOffset, dimensionY, OUTER_EXTENT,
dimensionOffset - 0.07, dimensionY, -OUTER_EXTENT,
dimensionOffset + 0.07, dimensionY, -OUTER_EXTENT,
dimensionOffset - 0.07, dimensionY, OUTER_EXTENT,
dimensionOffset + 0.07, dimensionY, OUTER_EXTENT
);
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dimensionVertices, 3)
);
const dimensions = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0x78918d,
transparent: true,
opacity: 0.72
})
);
boundaryGroup.add(dimensions);
// -------------------------------------------------------------------------
// Labels projected from world positions
// -------------------------------------------------------------------------
const labels = {
gap: {
element: document.querySelector("#gapLabel"),
position: new THREE.Vector3(0.5, 0.26, 2.23),
visible: () => spacingGroup.visible
},
boundary: {
element: document.querySelector("#boundaryLabel"),
position: new THREE.Vector3(-2.52, 0.08, 2.57),
visible: () => boundaryGroup.visible
},
x: {
element: document.querySelector("#xLabel"),
position: new THREE.Vector3(0, 0.06, 2.94),
visible: () => boundaryGroup.visible
},
z: {
element: document.querySelector("#zLabel"),
position: new THREE.Vector3(2.94, 0.06, 0),
visible: () => boundaryGroup.visible
}
};
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of Object.values(labels)) {
if (!label.visible()) {
label.element.style.display = "none";
continue;
}
projected.copy(label.position).project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
if (!inView) {
label.element.style.display = "none";
continue;
}
label.element.style.display = "block";
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and two-finger pinch
// -------------------------------------------------------------------------
const pose = {
azimuth: 0.82,
elevation: 0.72,
size: 8.2
};
const desiredPose = { ...pose };
const pointers = new Map();
let lastPinchDistance = 0;
let dragging = false;
const canvas = renderer.domElement;
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function setView(name) {
if (name === "top") {
desiredPose.azimuth = 0;
desiredPose.elevation = 1.48;
desiredPose.size = 7.15;
} else if (name === "front") {
desiredPose.azimuth = 0;
desiredPose.elevation = 0.29;
desiredPose.size = 7.35;
} else {
desiredPose.azimuth = 0.82;
desiredPose.elevation = 0.72;
desiredPose.size = 8.2;
}
document.querySelectorAll("[data-view]").forEach(button => {
button.classList.toggle("active", button.dataset.view === name);
});
}
document.querySelectorAll("[data-view]").forEach(button => {
button.addEventListener("click", () => setView(button.dataset.view));
});
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
dragging = true;
if (pointers.size === 2) {
const points = [...pointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
desiredPose.azimuth -= dx * 0.006;
desiredPose.elevation = clamp(
desiredPose.elevation + dy * 0.005,
0.22,
1.49
);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0) {
desiredPose.size = clamp(
desiredPose.size * lastPinchDistance / distance,
5.2,
14
);
}
lastPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size > 0;
if (pointers.size < 2) {
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
desiredPose.size = clamp(
desiredPose.size * Math.exp(event.deltaY * 0.001),
5.2,
14
);
},
{ passive: false }
);
document.querySelector("#boundaryToggle").addEventListener("change", event => {
boundaryGroup.visible = event.target.checked;
});
document.querySelector("#spacingToggle").addEventListener("change", event => {
spacingGroup.visible = event.target.checked;
});
function updateCamera() {
const smoothing = dragging ? 0.22 : 0.11;
pose.azimuth +=
(desiredPose.azimuth - pose.azimuth) * smoothing;
pose.elevation +=
(desiredPose.elevation - pose.elevation) * smoothing;
pose.size +=
(desiredPose.size - pose.size) * smoothing;
const radius = 12;
const horizontalRadius = Math.sin(pose.elevation) * radius;
camera.position.set(
Math.sin(pose.azimuth) * horizontalRadius,
Math.cos(pose.elevation) * radius,
Math.cos(pose.azimuth) * horizontalRadius
);
camera.lookAt(cameraTarget);
const aspect = window.innerWidth / window.innerHeight;
const visibleHeight = Math.max(pose.size, 8.6 / aspect);
const visibleWidth = visibleHeight * aspect;
camera.left = -visibleWidth / 2;
camera.right = visibleWidth / 2;
camera.top = visibleHeight / 2;
camera.bottom = -visibleHeight / 2;
camera.updateProjectionMatrix();
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
renderer.setAnimationLoop(() => {
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```