Prompt specification
Build or customize the Three.js 3D scene "Attachment and microcolony sequence" 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: Attachment and microcolony sequence
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/attachment-and-microcolony-sequence-x0806
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts biofilm layers. Show isolated surface attachment followed by a small cluster and matrix accumulation in staged panels; label growth as qualitative and keep the example non-species-specific. Stylized solid forms.
Working Three.js Starter (attachment-and-microcolony-sequence-x0806):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Biofilm Layers — Qualitative Sequence</title>
<style>
:root {
--ink: #17343a;
--muted: #64777a;
--paper: rgba(250, 252, 247, 0.88);
--border: rgba(23, 52, 58, 0.13);
--teal: #2eb3a7;
--gold: #dba64f;
--slate: #708587;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #edf1eb;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.intro {
position: fixed;
top: 30px;
left: 34px;
z-index: 5;
width: min(470px, calc(100vw - 68px));
pointer-events: none;
}
.eyebrow {
margin: 0 0 10px;
color: #39736f;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 440px;
font-size: clamp(28px, 3.2vw, 48px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 750;
}
.intro-copy {
max-width: 430px;
margin: 14px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.qualitative {
display: inline-flex;
align-items: center;
gap: 9px;
margin-top: 17px;
padding: 8px 11px;
border: 1px solid rgba(23, 52, 58, 0.14);
border-radius: 999px;
background: rgba(250, 252, 247, 0.72);
color: #52696c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.qualitative::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--gold);
content: "";
box-shadow: 0 0 0 4px rgba(219, 166, 79, 0.14);
}
.legend {
position: fixed;
top: 30px;
right: 30px;
z-index: 5;
width: 220px;
padding: 16px 17px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(34, 57, 59, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-title {
margin-bottom: 12px;
color: #718184;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr;
align-items: center;
gap: 10px;
margin-top: 10px;
font-size: 12px;
font-weight: 650;
}
.swatch {
width: 12px;
height: 12px;
border-radius: 4px;
box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.09);
}
.swatch.units {
background: var(--teal);
border-radius: 50%;
}
.swatch.matrix {
background: var(--gold);
}
.swatch.surface {
background: var(--slate);
}
.stage-label {
position: fixed;
top: 0;
left: 0;
z-index: 4;
width: 218px;
padding: 13px 14px 14px;
transform: translate(-50%, -50%);
border: 1px solid var(--border);
border-radius: 14px;
background: rgba(250, 252, 247, 0.91);
box-shadow: 0 12px 30px rgba(30, 53, 56, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
will-change: left, top;
}
.stage-topline {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
}
.stage-number {
display: grid;
width: 25px;
height: 25px;
place-items: center;
border-radius: 8px;
background: #e2eeea;
color: #28766f;
font-size: 10px;
font-weight: 850;
}
.stage-label:nth-of-type(3) .stage-number {
background: #f3e6ca;
color: #95651e;
}
.stage-kind {
color: #7a8a8c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stage-label h2 {
margin: 0;
font-size: 15px;
line-height: 1.15;
letter-spacing: -0.02em;
}
.stage-label p {
margin: 6px 0 0;
color: #6b7b7d;
font-size: 11px;
line-height: 1.45;
}
.controls {
position: fixed;
right: 28px;
bottom: 24px;
z-index: 6;
display: flex;
align-items: center;
gap: 12px;
}
.hint {
color: #657779;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
pointer-events: none;
}
button {
appearance: none;
border: 1px solid rgba(23, 52, 58, 0.15);
border-radius: 999px;
padding: 10px 14px;
background: rgba(250, 252, 247, 0.9);
color: var(--ink);
box-shadow: 0 8px 22px rgba(30, 53, 56, 0.08);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
backdrop-filter: blur(10px);
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:focus-visible,
#scene:focus-visible {
outline: 2px solid var(--teal);
outline-offset: 3px;
}
.mobile-sequence {
display: none;
position: fixed;
right: 10px;
bottom: 10px;
left: 10px;
z-index: 7;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.mobile-step {
min-width: 0;
padding: 10px 8px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(250, 252, 247, 0.92);
box-shadow: 0 8px 20px rgba(30, 53, 56, 0.07);
backdrop-filter: blur(10px);
}
.mobile-step strong {
display: block;
overflow: hidden;
color: #2b625e;
font-size: 9px;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.mobile-step span {
display: block;
margin-top: 4px;
color: #748486;
font-size: 9px;
line-height: 1.3;
}
@media (max-width: 850px) {
.legend {
display: none;
}
.intro {
top: 20px;
left: 20px;
width: min(430px, calc(100vw - 40px));
}
.intro-copy {
max-width: 390px;
font-size: 12px;
}
}
@media (max-width: 680px), (max-aspect-ratio: 0.8) {
.intro {
top: 15px;
left: 15px;
width: calc(100vw - 30px);
}
h1 {
max-width: 330px;
font-size: 29px;
}
.intro-copy {
max-width: 330px;
margin-top: 9px;
font-size: 11px;
}
.qualitative {
margin-top: 10px;
padding: 6px 9px;
font-size: 8px;
}
.stage-label,
.hint {
display: none;
}
.mobile-sequence {
display: grid;
}
.controls {
top: 18px;
right: 12px;
bottom: auto;
}
button {
padding: 8px 10px;
font-size: 9px;
}
}
@media (prefers-reduced-motion: reduce) {
button {
transition: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Abstract three-panel biofilm sequence showing surface attachment, a small cluster, and matrix accumulation."
aria-describedby="scene-description"
></canvas>
<p id="scene-description" hidden>
A non-species-specific qualitative sequence. Teal solid forms represent generic
attached units, gold solid forms represent accumulating matrix, and slate forms
represent the surface. Drag to orbit and scroll to zoom.
</p>
<header class="intro">
<p class="eyebrow">Surface colonization / abstract study</p>
<h1>Biofilm layers,<br>built in sequence.</h1>
<p class="intro-copy">
A qualitative, non-species-specific model of isolated attachment,
cluster formation, and matrix accumulation. Generic solid forms only.
</p>
<div class="qualitative">Qualitative growth · Not to scale</div>
</header>
<aside class="legend" aria-label="Scene legend">
<div class="legend-title">Solid-form key</div>
<div class="legend-row">
<span class="swatch units"></span>
<span>Generic attached units</span>
</div>
<div class="legend-row">
<span class="swatch matrix"></span>
<span>Accumulating matrix</span>
</div>
<div class="legend-row">
<span class="swatch surface"></span>
<span>Surface substrate</span>
</div>
</aside>
<div class="stage-label" id="label-0">
<div class="stage-topline">
<span class="stage-number">01</span>
<span class="stage-kind">Attachment</span>
</div>
<h2>Isolated surface contact</h2>
<p>A single generic unit establishes the first attachment.</p>
</div>
<div class="stage-label" id="label-1">
<div class="stage-topline">
<span class="stage-number">02</span>
<span class="stage-kind">Cluster</span>
</div>
<h2>Small attached cluster</h2>
<p>Additional units gather into a compact surface-associated group.</p>
</div>
<div class="stage-label" id="label-2">
<div class="stage-topline">
<span class="stage-number">03</span>
<span class="stage-kind">Accumulation</span>
</div>
<h2>Matrix surrounds the cluster</h2>
<p>Material builds around, beneath, and over the attached units.</p>
</div>
<div class="mobile-sequence" aria-label="Qualitative growth stages">
<div class="mobile-step">
<strong>01 · Attachment</strong>
<span>One isolated unit</span>
</div>
<div class="mobile-step">
<strong>02 · Cluster</strong>
<span>A compact group</span>
</div>
<div class="mobile-step">
<strong>03 · Matrix</strong>
<span>Layered accumulation</span>
</div>
</div>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xedf1eb);
scene.fog = new THREE.Fog(0xedf1eb, 25, 48);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(
0xffffff,
0x879a94,
2.15
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.4);
keyLight.position.set(-8, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9edfdc, 1.35);
fillLight.position.set(11, 6, -8);
scene.add(fillLight);
const frontLight = new THREE.DirectionalLight(0xffffff, 0.75);
frontLight.position.set(0, 3, 14);
scene.add(frontLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xe4e9e2,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.22;
floor.receiveShadow = true;
scene.add(floor);
// -------------------------------------------------------------------------
// Shared materials and geometries
// -------------------------------------------------------------------------
const surfaceY = 0.25;
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x263f43,
roughness: 0.72,
metalness: 0.03
}),
surface: new THREE.MeshStandardMaterial({
color: 0x708587,
roughness: 0.86,
metalness: 0
}),
unitA: new THREE.MeshStandardMaterial({
color: 0x28ada3,
roughness: 0.42,
metalness: 0.02
}),
unitB: new THREE.MeshStandardMaterial({
color: 0x49c2b5,
roughness: 0.46,
metalness: 0.01
}),
unitC: new THREE.MeshStandardMaterial({
color: 0x218f8b,
roughness: 0.48,
metalness: 0.01
}),
matrixDark: new THREE.MeshStandardMaterial({
color: 0xc58d3d,
roughness: 0.78,
metalness: 0
}),
matrix: new THREE.MeshStandardMaterial({
color: 0xdba64f,
roughness: 0.73,
metalness: 0
}),
matrixLight: new THREE.MeshStandardMaterial({
color: 0xe8bd70,
roughness: 0.7,
metalness: 0
}),
accentOne: new THREE.MeshStandardMaterial({
color: 0x65d0c4,
roughness: 0.65
}),
accentTwo: new THREE.MeshStandardMaterial({
color: 0x2a9f98,
roughness: 0.65
}),
accentThree: new THREE.MeshStandardMaterial({
color: 0xd8a14c,
roughness: 0.65
}),
arrow: new THREE.MeshStandardMaterial({
color: 0x9ba9a4,
roughness: 0.8
})
};
const unitGeometry = new THREE.SphereGeometry(1, 28, 20);
const matrixGeometry = new THREE.IcosahedronGeometry(1, 3);
const unitMaterials = [
materials.unitA,
materials.unitB,
materials.unitC
];
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function roundedRectangleShape(width, depth, radius) {
const x = -width / 2;
const y = -depth / 2;
const r = Math.min(radius, width / 2, depth / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + depth - r);
shape.quadraticCurveTo(
x + width,
y + depth,
x + width - r,
y + depth
);
shape.lineTo(x + r, y + depth);
shape.quadraticCurveTo(x, y + depth, x, y + depth - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function makeRoundedSlab(width, depth, height, radius) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectangleShape(width, depth, radius),
{
depth: height,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: Math.min(0.055, height * 0.3),
bevelThickness: Math.min(0.045, height * 0.25),
curveSegments: 10
}
);
geometry.center();
geometry.rotateX(-Math.PI / 2);
return geometry;
}
const baseGeometry = makeRoundedSlab(5.5, 4.35, 0.38, 0.24);
const surfaceGeometry = makeRoundedSlab(5.28, 4.13, 0.08, 0.2);
function addPlatform(group, accentMaterial) {
const base = new THREE.Mesh(baseGeometry, materials.base);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(surfaceGeometry, materials.surface);
top.position.y = 0.205;
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
const frontMarker = new THREE.Mesh(
new THREE.BoxGeometry(1.18, 0.035, 0.09),
accentMaterial
);
frontMarker.position.set(0, 0.275, 1.81);
frontMarker.castShadow = true;
group.add(frontMarker);
}
function addUnit(
group,
x,
z,
radius,
material,
elevation = 0,
verticalScale = 0.82
) {
const unit = new THREE.Mesh(unitGeometry, material);
unit.position.set(
x,
surfaceY + radius * verticalScale + elevation,
z
);
unit.scale.set(radius, radius * verticalScale, radius);
unit.rotation.set(
(Math.random() - 0.5) * 0.08,
Math.random() * Math.PI,
(Math.random() - 0.5) * 0.08
);
unit.castShadow = true;
unit.receiveShadow = true;
group.add(unit);
return unit;
}
function addMatrixBlob(
group,
x,
y,
z,
sx,
sy,
sz,
material
) {
const blob = new THREE.Mesh(matrixGeometry, material);
blob.position.set(x, y, z);
blob.scale.set(sx, sy, sz);
blob.rotation.set(
0.08,
Math.random() * Math.PI,
(Math.random() - 0.5) * 0.12
);
blob.castShadow = true;
blob.receiveShadow = true;
group.add(blob);
return blob;
}
// -------------------------------------------------------------------------
// Stage groups
// -------------------------------------------------------------------------
const stageGroups = [];
// Stage 1: a single isolated attached unit.
const stageOne = new THREE.Group();
addPlatform(stageOne, materials.accentOne);
addUnit(
stageOne,
0,
0,
0.62,
materials.unitB,
0,
0.84
);
stageGroups.push(stageOne);
scene.add(stageOne);
// Stage 2: a compact cluster of generic attached units.
const stageTwo = new THREE.Group();
addPlatform(stageTwo, materials.accentTwo);
const cluster = [
{ x: -0.58, z: 0.35, r: 0.46, dy: 0.00, m: 0 },
{ x: 0.30, z: 0.45, r: 0.40, dy: 0.01, m: 1 },
{ x: 0.75, z: 0.02, r: 0.42, dy: 0.02, m: 0 },
{ x: 0.13, z: -0.05, r: 0.50, dy: 0.05, m: 1 },
{ x: -0.48, z: -0.42, r: 0.41, dy: 0.00, m: 2 },
{ x: 0.50, z: -0.50, r: 0.38, dy: 0.00, m: 0 },
{ x: -0.04, z: 0.16, r: 0.44, dy: 0.18, m: 1 }
];
cluster.forEach((item) => {
addUnit(
stageTwo,
item.x,
item.z,
item.r,
unitMaterials[item.m],
item.dy,
0.84
);
});
stageGroups.push(stageTwo);
scene.add(stageTwo);
// Stage 3: matrix accumulation around and over embedded units.
const stageThree = new THREE.Group();
addPlatform(stageThree, materials.accentThree);
// Broad basal film.
addMatrixBlob(
stageThree,
0,
surfaceY + 0.10,
0,
1.83,
0.20,
1.34,
materials.matrixDark
);
// Lower matrix body.
addMatrixBlob(
stageThree,
-0.60,
surfaceY + 0.31,
0.10,
0.87,
0.43,
0.76,
materials.matrix
);
addMatrixBlob(
stageThree,
0.57,
surfaceY + 0.37,
-0.08,
0.96,
0.52,
0.80,
materials.matrix
);
addMatrixBlob(
stageThree,
0.12,
surfaceY + 0.39,
0.57,
0.92,
0.46,
0.70,
materials.matrixLight
);
addMatrixBlob(
stageThree,
-0.73,
surfaceY + 0.34,
-0.54,
0.68,
0.40,
0.61,
materials.matrix
);
// Upper matrix layer.
addMatrixBlob(
stageThree,
0.02,
surfaceY + 0.75,
-0.04,
0.67,
0.43,
0.56,
materials.matrixLight
);
// Units remain visible at the edges and above the matrix.
const matrixUnits = [
{ x: -1.00, z: 0.48, r: 0.43, dy: 0.38, m: 1 },
{ x: 0.91, z: 0.42, r: 0.42, dy: 0.32, m: 0 },
{ x: -0.22, z: 0.13, r: 0.45, dy: 0.84, m: 1 },
{ x: 0.49, z: -0.26, r: 0.43, dy: 0.78, m: 0 },
{ x: -0.58, z: -0.57, r: 0.42, dy: 0.47, m: 2 },
{ x: 0.84, z: -0.65, r: 0.39, dy: 0.40, m: 1 },
{ x: 0.02, z: -0.78, r: 0.40, dy: 0.20, m: 0 }
];
matrixUnits.forEach((item) => {
addUnit(
stageThree,
item.x,
item.z,
item.r,
unitMaterials[item.m],
item.dy,
0.84
);
});
stageGroups.push(stageThree);
scene.add(stageThree);
// -------------------------------------------------------------------------
// Small directional markers between the desktop panels
// -------------------------------------------------------------------------
const arrows = [];
function createArrow(x) {
const group = new THREE.Group();
group.position.set(x, 0.265, 1.78);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 0.42, 12),
materials.arrow
);
shaft.rotation.z = -Math.PI / 2;
shaft.position.x = -0.14;
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.30, 3),
materials.arrow
);
head.rotation.z = -Math.PI / 2;
head.position.x = 0.23;
head.castShadow = true;
group.add(head);
scene.add(group);
arrows.push(group);
}
createArrow(-3.15);
createArrow(3.15);
// -------------------------------------------------------------------------
// Custom camera controls, implemented without external controls addons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.25, 0);
const spherical = new THREE.Spherical(20.5, 1.05, 0.1);
const desired = {
radius: spherical.radius,
phi: spherical.phi,
theta: spherical.theta
};
let portraitLayout = false;
let userHasMovedCamera = false;
let pinchDistance = 0;
const pointers = new Map();
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = width / height;
camera.updateProjectionMatrix();
portraitLayout = width < 680 || width / height < 0.8;
if (portraitLayout) {
stageGroups[0].position.set(0, 0, -6.3);
stageGroups[1].position.set(0, 0, 0);
stageGroups[2].position.set(0, 0, 6.3);
arrows.forEach((arrow) => {
arrow.visible = false;
});
} else {
stageGroups[0].position.set(-6.3, 0, 0);
stageGroups[1].position.set(0, 0, 0);
stageGroups[2].position.set(6.3, 0, 0);
arrows.forEach((arrow) => {
arrow.visible = true;
});
}
if (!userHasMovedCamera) {
resetCamera(false);
}
}
function resetCamera(animate = true) {
userHasMovedCamera = false;
if (portraitLayout) {
desired.radius = 22.5;
desired.phi = 0.78;
desired.theta = 0;
} else {
desired.radius = 20.5;
desired.phi = 1.05;
desired.theta = 0.1;
}
if (!animate) {
spherical.radius = desired.radius;
spherical.phi = desired.phi;
spherical.theta = desired.theta;
}
}
function clampCamera() {
desired.radius = THREE.MathUtils.clamp(desired.radius, 12, 32);
desired.phi = THREE.MathUtils.clamp(desired.phi, 0.48, 1.42);
desired.theta = THREE.MathUtils.clamp(desired.theta, -1.2, 1.2);
}
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
userHasMovedCamera = true;
if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
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) {
desired.theta -= dx * 0.005;
desired.phi -= dy * 0.0045;
} else if (pointers.size === 2) {
const nextDistance = pointerDistance();
if (pinchDistance > 0 && nextDistance > 0) {
desired.radius *= pinchDistance / nextDistance;
}
pinchDistance = nextDistance;
}
clampCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
userHasMovedCamera = true;
desired.radius *= Math.exp(event.deltaY * 0.001);
clampCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
let handled = true;
if (key === "arrowleft") desired.theta -= 0.08;
else if (key === "arrowright") desired.theta += 0.08;
else if (key === "arrowup") desired.phi -= 0.06;
else if (key === "arrowdown") desired.phi += 0.06;
else if (key === "+" || key === "=") desired.radius -= 0.7;
else if (key === "-" || key === "_") desired.radius += 0.7;
else if (key === "r") resetCamera();
else handled = false;
if (handled) {
event.preventDefault();
userHasMovedCamera = true;
clampCamera();
}
});
document.querySelector("#reset").addEventListener("click", () => {
resetCamera();
});
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Projected HTML stage labels
// -------------------------------------------------------------------------
const labels = [
document.querySelector("#label-0"),
document.querySelector("#label-1"),
document.querySelector("#label-2")
];
const projectedPoint = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
stageGroups.forEach((group, index) => {
if (portraitLayout) {
labels[index].style.visibility = "hidden";
return;
}
projectedPoint.set(0, -0.58, 2.35);
group.localToWorld(projectedPoint);
projectedPoint.project(camera);
const visible =
projectedPoint.z > -1 &&
projectedPoint.z < 1 &&
Math.abs(projectedPoint.x) < 1.25 &&
Math.abs(projectedPoint.y) < 1.25;
labels[index].style.visibility = visible ? "visible" : "hidden";
const x = (projectedPoint.x * 0.5 + 0.5) * width;
const y = (-projectedPoint.y * 0.5 + 0.5) * height;
labels[index].style.left = `${x}px`;
labels[index].style.top = `${y}px`;
});
}
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
resize();
resetCamera(false);
function animate() {
const damping = 0.085;
spherical.radius +=
(desired.radius - spherical.radius) * damping;
spherical.phi +=
(desired.phi - spherical.phi) * damping;
spherical.theta +=
(desired.theta - spherical.theta) * damping;
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
scene.updateMatrixWorld();
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```