Prompt specification
Build or customize the Three.js 3D scene "Public plaza paving design scene" 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: Public plaza paving design scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/public-plaza-paving-design-scene-x1743
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a voxel public plaza with two geometric paving patterns. Divide a bounded rectangular square into two adjoining regions, one using a simple grid and the other an offset repeating arrangement. Keep the nominal tile scale consistent. Along the outer edge, add a drainage border and show cut tiles where the repeated pattern meets that boundary. Place only a few benches and trees so most of the pavement remains visible. A switch can remove the furnishings for a clear overhead comparison. Use contrasting but restrained stone colors, and make gaps or overlaps in the paving impossible rather than concealing them with a ground texture.
Working Three.js Starter (public-plaza-paving-design-scene-x1743):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Plaza — Paving Pattern Study</title>
<style>
:root {
color-scheme: light;
--ink: #282b28;
--muted: #686d67;
--panel: rgba(248, 246, 240, 0.88);
--line: rgba(40, 43, 40, 0.14);
--grid: #c7bba5;
--bond: #9da8a5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9d5cc;
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;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 24px;
left: 24px;
width: min(330px, calc(100vw - 48px));
padding: 22px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 48px rgba(48, 50, 45, 0.14),
inset 0 0 0 1px rgba(40, 43, 40, 0.04);
backdrop-filter: blur(18px) saturate(0.9);
-webkit-backdrop-filter: blur(18px) saturate(0.9);
pointer-events: auto;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: #656b64;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 2px;
background: #667064;
content: "";
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 35px;
font-weight: 500;
letter-spacing: -0.035em;
line-height: 1;
}
.intro {
margin: 10px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: grid;
gap: 9px;
padding: 14px 0;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
font-size: 12px;
}
.swatch {
width: 20px;
height: 20px;
flex: 0 0 auto;
border: 1px solid rgba(30, 35, 31, 0.12);
border-radius: 5px;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.28);
}
.swatch.grid {
background-color: var(--grid);
background-image:
linear-gradient(to right, transparent 47%, rgba(61, 62, 57, 0.28) 48%, rgba(61, 62, 57, 0.28) 52%, transparent 53%),
linear-gradient(to bottom, transparent 47%, rgba(61, 62, 57, 0.28) 48%, rgba(61, 62, 57, 0.28) 52%, transparent 53%);
background-size: 10px 10px;
}
.swatch.bond {
background-color: var(--bond);
background-image:
linear-gradient(to right, transparent 47%, rgba(61, 62, 57, 0.28) 48%, rgba(61, 62, 57, 0.28) 52%, transparent 53%);
background-size: 10px 10px;
background-position: 0 0;
}
.legend-row span:last-child {
margin-left: auto;
color: #777c76;
font-size: 10px;
letter-spacing: 0.04em;
}
.module-note {
margin: 11px 0 17px;
color: #747971;
font-size: 10px;
line-height: 1.45;
}
.switch-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 16px;
font-size: 12px;
font-weight: 650;
}
.switch {
position: relative;
display: inline-flex;
align-items: center;
cursor: pointer;
}
.switch input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
}
.slider {
position: relative;
width: 42px;
height: 24px;
border-radius: 999px;
background: #a7aaa4;
transition: background 160ms ease;
}
.slider::after {
position: absolute;
top: 3px;
left: 3px;
width: 18px;
height: 18px;
border-radius: 50%;
background: #fff;
box-shadow: 0 2px 6px rgba(30, 35, 31, 0.22);
content: "";
transition: transform 160ms ease;
}
.switch input:checked + .slider {
background: #657064;
}
.switch input:checked + .slider::after {
transform: translateX(18px);
}
.switch input:focus-visible + .slider {
outline: 3px solid rgba(91, 111, 91, 0.28);
outline-offset: 2px;
}
.views {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 38px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.48);
color: #535951;
font: inherit;
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition:
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.84);
transform: translateY(-1px);
}
button.active {
border-color: #616b61;
background: #616b61;
color: #fff;
}
.help {
margin: 14px 0 0;
color: #7a7f78;
font-size: 9px;
letter-spacing: 0.02em;
line-height: 1.5;
}
#modeBadge {
position: absolute;
top: 24px;
right: 24px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 999px;
background: rgba(248, 246, 240, 0.72);
color: #60675e;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
box-shadow: 0 8px 24px rgba(48, 50, 45, 0.1);
backdrop-filter: blur(12px);
}
@media (max-width: 680px) {
.panel {
top: 12px;
left: 12px;
width: min(310px, calc(100vw - 24px));
padding: 17px;
border-radius: 15px;
}
h1 {
font-size: 29px;
}
.intro {
margin-bottom: 12px;
}
#modeBadge {
top: auto;
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D voxel plaza with two paving patterns"></canvas>
<div id="hud">
<section class="panel">
<div class="eyebrow">Urban material study 01</div>
<h1>Common Ground</h1>
<p class="intro">
A bounded 12 × 12 m plaza comparing a regular grid with an offset
square bond at the same nominal scale.
</p>
<div class="legend" aria-label="Paving pattern legend">
<div class="legend-row">
<i class="swatch grid"></i>
<strong>Regular grid</strong>
<span>LEFT</span>
</div>
<div class="legend-row">
<i class="swatch bond"></i>
<strong>Offset bond</strong>
<span>RIGHT</span>
</div>
</div>
<div class="module-note">
1 × 1 m nominal module. Alternate rows shift by ½ module.
Perimeter and center-joint pieces are cut exactly to the boundary.
</div>
<div class="switch-line">
<span>Show furnishings</span>
<label class="switch" aria-label="Show or remove plaza furnishings">
<input id="furnitureToggle" type="checkbox" checked>
<span class="slider"></span>
</label>
</div>
<div class="views" aria-label="Camera views">
<button id="perspectiveButton" class="active" type="button">Perspective</button>
<button id="overheadButton" type="button">Overhead</button>
</div>
<p class="help">
Drag to orbit · Right-drag or Shift-drag to pan · Scroll to zoom<br>
Turning off furnishings also moves to the overhead comparison.
</p>
</section>
<div id="modeBadge">Furnished view</div>
</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9d5cc);
scene.fog = new THREE.Fog(0xd9d5cc, 28, 58);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xf5f2e9, 0x777970, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d7, 3.25);
sun.position.set(-9, 15, 10);
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 = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0003;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const PAVING_MIN = -6;
const PAVING_MAX = 6;
const TILE_HEIGHT = 0.14;
const TILE_MODULE = 1;
const furnitureGroups = [];
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const material = (color, roughness = 0.9, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const pavingMaterial = material(0xffffff, 0.96);
const foundationMaterial = material(0x6d706a, 1);
const subbaseMaterial = material(0x62645f, 1);
const drainMaterial = material(0x464c49, 0.82, 0.18);
const grateMaterial = material(0x737a75, 0.68, 0.34);
const curbMaterial = material(0x87877e, 0.96);
const woodMaterial = material(0x8d6c4c, 0.88);
const woodLightMaterial = material(0xa0805d, 0.88);
const metalMaterial = material(0x484d48, 0.7, 0.25);
const foliageMaterials = [
material(0x6f8067, 0.96),
material(0x7f9072, 0.96),
material(0x63745f, 0.96)
];
const trunkMaterial = material(0x74604a, 0.98);
// -------------------------------------------------------------------------
// Base, subbase and perimeter drainage
// -------------------------------------------------------------------------
const outerCurb = 6.64;
const foundation = new THREE.Mesh(
new THREE.BoxGeometry(outerCurb * 2, 0.34, outerCurb * 2),
foundationMaterial
);
foundation.position.y = -0.25;
foundation.receiveShadow = true;
scene.add(foundation);
// This solid subbase occupies precisely the paved square. It is structural,
// not a texture used to hide seams.
const subbase = new THREE.Mesh(
new THREE.BoxGeometry(12, 0.08, 12),
subbaseMaterial
);
subbase.position.y = -0.04;
subbase.receiveShadow = true;
scene.add(subbase);
function addBox(width, height, depth, x, y, z, mat, castShadow = false) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Four non-overlapping strips form a continuous recessed drainage ring.
addBox(13, 0.105, 0.5, 0, -0.0275, 6.25, drainMaterial);
addBox(13, 0.105, 0.5, 0, -0.0275, -6.25, drainMaterial);
addBox(0.5, 0.105, 12, 6.25, -0.0275, 0, drainMaterial);
addBox(0.5, 0.105, 12, -6.25, -0.0275, 0, drainMaterial);
// A narrow raised curb defines the square's outer limit.
addBox(outerCurb * 2, 0.2, 0.14, 0, 0.02, 6.57, curbMaterial);
addBox(outerCurb * 2, 0.2, 0.14, 0, 0.02, -6.57, curbMaterial);
addBox(0.14, 0.2, 13, 6.57, 0.02, 0, curbMaterial);
addBox(0.14, 0.2, 13, -6.57, 0.02, 0, curbMaterial);
// Instanced transverse grate bars. They sit directly on the channel surface.
const grateTransforms = [];
for (let x = -6.28; x <= 6.28; x += 0.28) {
grateTransforms.push({
position: [x, 0.034, 6.25],
scale: [0.045, 0.018, 0.34]
});
grateTransforms.push({
position: [x, 0.034, -6.25],
scale: [0.045, 0.018, 0.34]
});
}
for (let z = -5.88; z <= 5.88; z += 0.28) {
grateTransforms.push({
position: [6.25, 0.034, z],
scale: [0.34, 0.018, 0.045]
});
grateTransforms.push({
position: [-6.25, 0.034, z],
scale: [0.34, 0.018, 0.045]
});
}
const grates = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
grateMaterial,
grateTransforms.length
);
const dummy = new THREE.Object3D();
grateTransforms.forEach((transform, index) => {
dummy.position.set(...transform.position);
dummy.scale.set(...transform.scale);
dummy.updateMatrix();
grates.setMatrixAt(index, dummy.matrix);
});
grates.instanceMatrix.needsUpdate = true;
grates.receiveShadow = true;
grates.frustumCulled = false;
scene.add(grates);
// -------------------------------------------------------------------------
// Exact paving construction
//
// partitionAxis returns clipped intervals that collectively cover the
// requested range exactly. Every interval starts where the previous one
// ends, so there can be neither footprint gaps nor overlaps.
// -------------------------------------------------------------------------
function partitionAxis(start, end, moduleSize, phase = 0) {
const first = Math.floor((start - phase) / moduleSize) - 1;
const last = Math.ceil((end - phase) / moduleSize) + 1;
const intervals = [];
for (let k = first; k <= last; k++) {
const rawStart = phase + k * moduleSize;
const rawEnd = rawStart + moduleSize;
const a = Math.max(start, rawStart);
const b = Math.min(end, rawEnd);
if (b - a > 1e-7) {
intervals.push([a, b]);
}
}
return intervals;
}
let randomSeed = 73421;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function tileColor(base, width, depth) {
const color = base.clone();
color.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.042
);
// Cut pieces are subtly lighter so their geometry remains legible.
if (width < 0.999 || depth < 0.999) {
color.offsetHSL(0, 0, 0.022);
}
return color;
}
function buildPavingMesh(tiles, baseColor) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
pavingMaterial,
tiles.length
);
const transform = new THREE.Object3D();
tiles.forEach((tile, index) => {
const width = tile.x1 - tile.x0;
const depth = tile.z1 - tile.z0;
transform.position.set(
(tile.x0 + tile.x1) * 0.5,
TILE_HEIGHT * 0.5,
(tile.z0 + tile.z1) * 0.5
);
transform.scale.set(width, TILE_HEIGHT, depth);
transform.updateMatrix();
mesh.setMatrixAt(index, transform.matrix);
mesh.setColorAt(index, tileColor(baseColor, width, depth));
});
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
return mesh;
}
// Left region: ordinary aligned 1 × 1 m square grid.
const gridTiles = [];
const gridX = partitionAxis(PAVING_MIN, 0, TILE_MODULE, 0);
const gridZ = partitionAxis(PAVING_MIN, PAVING_MAX, TILE_MODULE, 0);
for (const [x0, x1] of gridX) {
for (const [z0, z1] of gridZ) {
gridTiles.push({ x0, x1, z0, z1 });
}
}
buildPavingMesh(gridTiles, new THREE.Color(0xc7bba5));
// Right region: the same square module in alternating offset rows.
// A half-module z phase creates visible cut courses at the outer edge.
const bondTiles = [];
const bondZ = partitionAxis(PAVING_MIN, PAVING_MAX, TILE_MODULE, 0.5);
bondZ.forEach(([z0, z1], rowIndex) => {
const rowPhase = rowIndex % 2 === 0 ? 0.5 : 0;
const bondX = partitionAxis(0, PAVING_MAX, TILE_MODULE, rowPhase);
for (const [x0, x1] of bondX) {
bondTiles.push({ x0, x1, z0, z1 });
}
});
buildPavingMesh(bondTiles, new THREE.Color(0x9da8a5));
// -------------------------------------------------------------------------
// Sparse voxel furnishings
// -------------------------------------------------------------------------
function addLocalBox(group, width, height, depth, x, y, z, mat) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
function createBench(x, z, rotation) {
const group = new THREE.Group();
group.position.set(x, TILE_HEIGHT, z);
group.rotation.y = rotation;
addLocalBox(group, 2.1, 0.16, 0.64, 0, 0.53, 0, woodMaterial);
addLocalBox(group, 0.18, 0.46, 0.5, -0.78, 0.23, 0, metalMaterial);
addLocalBox(group, 0.18, 0.46, 0.5, 0.78, 0.23, 0, metalMaterial);
addLocalBox(group, 2.1, 0.43, 0.13, 0, 0.82, -0.25, woodLightMaterial);
scene.add(group);
furnitureGroups.push(group);
}
function createTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, TILE_HEIGHT, z);
group.scale.setScalar(scale);
addLocalBox(group, 0.42, 1.35, 0.42, 0, 0.675, 0, trunkMaterial);
addLocalBox(
group, 1.82, 1.16, 1.78,
0, 1.58, 0,
foliageMaterials[0]
);
addLocalBox(
group, 1.26, 1.02, 1.28,
0.12, 2.48, -0.05,
foliageMaterials[1]
);
addLocalBox(
group, 1.08, 0.86, 1.06,
0.58, 1.92, 0.14,
foliageMaterials[2]
);
addLocalBox(
group, 0.92, 0.78, 0.94,
-0.57, 1.82, -0.22,
foliageMaterials[1]
);
scene.add(group);
furnitureGroups.push(group);
}
createBench(-4.45, 0.55, Math.PI * 0.5);
createBench(2.3, -4.65, 0);
createBench(4.85, 2.65, Math.PI * 0.5);
createTree(-4.65, -4.55, 1.0);
createTree(-2.2, 4.45, 0.92);
createTree(4.35, -3.85, 0.96);
createTree(3.55, 4.45, 0.88);
// Plain presentation ground. No texture is used to conceal paving seams.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
material(0xd9d5cc, 1)
);
ground.rotation.x = -Math.PI * 0.5;
ground.position.y = -0.42;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Camera and direct camera controls
// No examples/addons are imported.
// -------------------------------------------------------------------------
const currentView = {
theta: 0.68,
phi: 0.80,
radius: 20,
target: new THREE.Vector3(0, 0.2, 0)
};
const desiredView = {
theta: currentView.theta,
phi: currentView.phi,
radius: currentView.radius,
target: currentView.target.clone()
};
function setView(viewName) {
if (viewName === "overhead") {
desiredView.theta = 0;
desiredView.phi = 0.001;
desiredView.radius = 17.2;
desiredView.target.set(0, 0, 0);
} else {
desiredView.theta = 0.68;
desiredView.phi = 0.80;
desiredView.radius = 20;
desiredView.target.set(0, 0.2, 0);
}
perspectiveButton.classList.toggle("active", viewName === "perspective");
overheadButton.classList.toggle("active", viewName === "overhead");
}
const perspectiveButton = document.querySelector("#perspectiveButton");
const overheadButton = document.querySelector("#overheadButton");
const furnitureToggle = document.querySelector("#furnitureToggle");
const modeBadge = document.querySelector("#modeBadge");
perspectiveButton.addEventListener("click", () => setView("perspective"));
overheadButton.addEventListener("click", () => setView("overhead"));
furnitureToggle.addEventListener("change", () => {
const showFurniture = furnitureToggle.checked;
furnitureGroups.forEach(group => {
group.visible = showFurniture;
});
modeBadge.textContent = showFurniture ? "Furnished view" : "Clear paving view";
setView(showFurniture ? "perspective" : "overhead");
});
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "o") setView("overhead");
if (event.key.toLowerCase() === "p") setView("perspective");
});
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function panView(dx, dy) {
const scale =
2 * desiredView.radius *
Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5)) /
window.innerHeight;
const right = new THREE.Vector3(
Math.cos(desiredView.theta),
0,
-Math.sin(desiredView.theta)
);
const towardCamera = new THREE.Vector3(
Math.sin(desiredView.theta),
0,
Math.cos(desiredView.theta)
);
desiredView.target.addScaledVector(right, -dx * scale);
desiredView.target.addScaledVector(towardCamera, dy * scale);
desiredView.target.x = clamp(desiredView.target.x, -8, 8);
desiredView.target.z = clamp(desiredView.target.z, -8, 8);
}
const pointers = new Map();
let dragState = null;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
if (pointers.size === 1) {
dragState = {
type: event.button === 2 || event.shiftKey ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
dragState = {
type: "pinch",
distance: Math.hypot(a.x - b.x, a.y - b.y),
x: (a.x + b.x) * 0.5,
y: (a.y + b.y) * 0.5
};
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && dragState?.type === "orbit") {
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
desiredView.theta -= dx * 0.006;
desiredView.phi = clamp(
desiredView.phi - dy * 0.006,
0.001,
1.45
);
dragState.x = event.clientX;
dragState.y = event.clientY;
} else if (pointers.size === 1 && dragState?.type === "pan") {
panView(
event.clientX - dragState.x,
event.clientY - dragState.y
);
dragState.x = event.clientX;
dragState.y = event.clientY;
} else if (pointers.size >= 2 && dragState?.type === "pinch") {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
const middleX = (a.x + b.x) * 0.5;
const middleY = (a.y + b.y) * 0.5;
if (dragState.distance > 0 && distance > 0) {
desiredView.radius = clamp(
desiredView.radius * dragState.distance / distance,
10,
34
);
}
panView(middleX - dragState.x, middleY - dragState.y);
dragState.distance = distance;
dragState.x = middleX;
dragState.y = middleY;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
dragState = {
type: "orbit",
x: remaining.x,
y: remaining.y
};
} else {
dragState = null;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
desiredView.radius = clamp(
desiredView.radius * Math.exp(event.deltaY * 0.001),
10,
34
);
}, { passive: false });
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const cameraSpherical = new THREE.Spherical();
function damp(current, target, lambda, delta) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-lambda * delta)
);
}
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
currentView.theta = damp(currentView.theta, desiredView.theta, 7, delta);
currentView.phi = damp(currentView.phi, desiredView.phi, 7, delta);
currentView.radius = damp(currentView.radius, desiredView.radius, 7, delta);
currentView.target.lerp(desiredView.target, 1 - Math.exp(-7 * delta));
cameraSpherical.set(
currentView.radius,
currentView.phi,
currentView.theta
);
camera.position.setFromSpherical(cameraSpherical);
camera.position.add(currentView.target);
camera.lookAt(currentView.target);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```