Prompt specification
Build or customize the Three.js 3D scene "Enzyme occupancy toy simulator" 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: Enzyme occupancy toy simulator
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/enzyme-occupancy-toy-simulator-x0848
Scene Description Prompt:
Use Three.js from cdnjs to let users adjust substrate availability around a fixed number of simplified enzyme sites; show saturation of occupancy and label the kinetics illustrative rather than experimentally fitted. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (enzyme-occupancy-toy-simulator-x0848):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Illustrative Enzyme Occupancy</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 21, 38, 0.88);
--panel-border: rgba(143, 188, 231, 0.2);
--text: #eaf4ff;
--muted: #9cb0c8;
--cyan: #59d8ff;
--amber: #ffc857;
--purple: #a78bfa;
--warning: #ffcf70;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 35% 30%, #142b46 0%, transparent 46%),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
top: 18px;
right: 18px;
width: min(360px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
margin: 0 0 5px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 1.55rem;
line-height: 1.1;
letter-spacing: -0.025em;
}
.intro {
margin: 10px 0 17px;
color: var(--muted);
font-size: 0.87rem;
line-height: 1.45;
}
.disclaimer {
margin-bottom: 18px;
padding: 11px 12px;
border: 1px solid rgba(255, 207, 112, 0.35);
border-radius: 11px;
background: rgba(255, 207, 112, 0.08);
color: var(--warning);
font-size: 0.76rem;
font-weight: 700;
line-height: 1.4;
}
.control-header {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.control-header label {
font-size: 0.9rem;
font-weight: 700;
}
#substrate-value {
color: var(--amber);
font-variant-numeric: tabular-nums;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 24px;
margin: 0;
accent-color: var(--amber);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 2px;
color: #7f95ae;
font-size: 0.7rem;
}
.presets {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin: 12px 0 18px;
}
.preset,
.action {
border: 1px solid rgba(148, 181, 220, 0.2);
border-radius: 9px;
background: rgba(121, 160, 205, 0.08);
color: var(--text);
font: inherit;
font-size: 0.76rem;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.preset {
padding: 7px 4px;
}
.preset:hover,
.action:hover {
border-color: rgba(89, 216, 255, 0.55);
background: rgba(89, 216, 255, 0.12);
}
.preset:active,
.action:active {
transform: translateY(1px);
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 16px;
}
.stat {
padding: 11px;
border: 1px solid rgba(143, 188, 231, 0.13);
border-radius: 11px;
background: rgba(4, 12, 25, 0.28);
}
.stat span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 0.69rem;
line-height: 1.2;
}
.stat strong {
font-size: 1.12rem;
font-variant-numeric: tabular-nums;
}
.chart-card {
padding: 12px 10px 8px;
border: 1px solid rgba(143, 188, 231, 0.13);
border-radius: 12px;
background: rgba(4, 12, 25, 0.26);
}
.chart-title {
margin: 0 0 4px 5px;
color: var(--muted);
font-size: 0.73rem;
font-weight: 700;
}
.chart-card svg {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
.axis {
fill: #8198b2;
font-size: 9px;
}
.formula {
margin: 13px 0 8px;
text-align: center;
color: #d6e8fb;
font-family: Georgia, "Times New Roman", serif;
font-size: 1.02rem;
}
.formula small {
display: block;
margin-top: 3px;
color: var(--muted);
font-family: inherit;
font-size: 0.7rem;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin: 13px 0 16px;
color: var(--muted);
font-size: 0.72rem;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.swatch.enzyme {
background: #647bd2;
}
.swatch.bound {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 200, 87, 0.55);
}
.swatch.free {
background: #ff8f4d;
transform: rotate(45deg);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action {
padding: 9px 8px;
}
.help {
margin: 13px 0 0;
color: #7f95ae;
font-size: 0.7rem;
line-height: 1.45;
text-align: center;
}
.footer-note {
position: fixed;
left: 18px;
bottom: 16px;
max-width: min(440px, calc(100vw - 36px));
padding: 9px 12px;
border-left: 3px solid var(--warning);
border-radius: 0 8px 8px 0;
background: rgba(7, 17, 31, 0.76);
color: #d5b878;
font-size: 0.72rem;
line-height: 1.35;
pointer-events: none;
}
@media (max-width: 720px) {
.panel {
top: 10px;
right: 10px;
width: min(340px, calc(100vw - 20px));
max-height: 56vh;
padding: 16px;
}
.footer-note {
left: 10px;
bottom: 10px;
}
h1 {
font-size: 1.3rem;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D illustration of substrate occupancy at twelve enzyme sites"
></canvas>
<aside class="panel" aria-label="Enzyme occupancy controls">
<p class="eyebrow">Interactive molecular illustration</p>
<h1>Enzyme Occupancy</h1>
<p class="intro">
Adjust substrate availability around twelve fixed, simplified enzyme sites.
As substrate increases, occupied sites approach saturation.
</p>
<div class="disclaimer">
ILLUSTRATIVE KINETICS — Parameters are arbitrary and are not fitted to
experimental data. This is a conceptual visualization, not a predictive model.
</div>
<div class="control-header">
<label for="substrate">Substrate availability [S]</label>
<output id="substrate-value" for="substrate">20 a.u.</output>
</div>
<input
id="substrate"
type="range"
min="0"
max="300"
step="1"
value="20"
aria-describedby="substrate-help"
/>
<div class="range-labels" aria-hidden="true">
<span>0</span>
<span>Kₘ = 20</span>
<span>300 a.u.</span>
</div>
<div class="presets" aria-label="Substrate presets">
<button class="preset" type="button" data-value="0">Empty</button>
<button class="preset" type="button" data-value="20">At Kₘ</button>
<button class="preset" type="button" data-value="100">High</button>
<button class="preset" type="button" data-value="300">Near max</button>
</div>
<div class="stats">
<div class="stat">
<span>Expected equilibrium occupancy</span>
<strong id="equilibrium">50.0%</strong>
</div>
<div class="stat">
<span>Visualized occupied sites</span>
<strong id="occupied-count">6 / 12</strong>
</div>
<div class="stat">
<span>Illustrative Kₘ</span>
<strong>20 a.u.</strong>
</div>
<div class="stat">
<span>Free substrate glyphs</span>
<strong id="glyph-count">4</strong>
</div>
</div>
<div class="chart-card">
<p class="chart-title">Expected occupancy versus substrate</p>
<svg viewBox="0 0 280 125" role="img" aria-label="Saturating occupancy curve">
<defs>
<linearGradient id="curve-fill" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="#59d8ff" stop-opacity="0.34" />
<stop offset="100%" stop-color="#59d8ff" stop-opacity="0.02" />
</linearGradient>
</defs>
<line
x1="28"
y1="15"
x2="265"
y2="15"
stroke="rgba(143,188,231,.13)"
/>
<line
x1="28"
y1="96"
x2="265"
y2="96"
stroke="rgba(143,188,231,.25)"
/>
<line
x1="28"
y1="15"
x2="28"
y2="96"
stroke="rgba(143,188,231,.25)"
/>
<path
id="curve-area"
fill="url(#curve-fill)"
d=""
/>
<path
id="curve-line"
fill="none"
stroke="#59d8ff"
stroke-width="2.6"
stroke-linecap="round"
stroke-linejoin="round"
d=""
/>
<line
id="marker-line"
x1="28"
x2="28"
y1="15"
y2="96"
stroke="#ffc857"
stroke-width="1.2"
stroke-dasharray="3 3"
/>
<circle
id="marker"
cx="28"
cy="96"
r="4.7"
fill="#ffc857"
stroke="#fff0bf"
stroke-width="1.2"
/>
<text class="axis" x="28" y="115">0</text>
<text class="axis" x="140" y="115" text-anchor="middle">150</text>
<text class="axis" x="265" y="115" text-anchor="end">300 a.u.</text>
<text class="axis" x="23" y="19" text-anchor="end">100%</text>
<text class="axis" x="23" y="99" text-anchor="end">0%</text>
</svg>
</div>
<div class="formula">
θ<sub>eq</sub> = [S] / (Kₘ + [S])
<small>Stylized Michaelis–Menten equilibrium relation</small>
</div>
<div class="legend" aria-label="Scene legend">
<span class="legend-item">
<span class="swatch enzyme"></span>
Enzyme site
</span>
<span class="legend-item">
<span class="swatch bound"></span>
Bound substrate
</span>
<span class="legend-item">
<span class="swatch free"></span>
Free substrate
</span>
</div>
<div class="actions">
<button id="pause" class="action" type="button">Pause drift</button>
<button id="reset-view" class="action" type="button">Reset view</button>
</div>
<p id="substrate-help" class="help">
Drag to orbit · wheel to zoom · arrow keys to rotate · +/− to zoom
</p>
</aside>
<div class="footer-note">
Conceptual low-poly model. Occupancy and Kₘ are illustrative and not
experimentally fitted.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and state
// -------------------------------------------------------------------------
const SITE_COUNT = 12;
const KM = 20; // Arbitrary illustrative units
const MAX_SUBSTRATE = 300; // Arbitrary illustrative units
const MAX_FREE_GLYPHS = 64;
const canvas = document.querySelector("#scene");
const slider = document.querySelector("#substrate");
const substrateValue = document.querySelector("#substrate-value");
const equilibriumValue = document.querySelector("#equilibrium");
const occupiedCountValue = document.querySelector("#occupied-count");
const glyphCountValue = document.querySelector("#glyph-count");
const pauseButton = document.querySelector("#pause");
const resetViewButton = document.querySelector("#reset-view");
const chart = {
left: 28,
right: 265,
top: 15,
bottom: 96
};
let substrate = Number(slider.value);
let expectedOccupancy = substrate / (KM + substrate);
let animationTime = 0;
let paused = false;
// Evenly distributed thresholds turn the continuous expected occupancy into
// a stable, discrete set of occupied sites.
const occupancyThresholds = Array.from(
{ length: SITE_COUNT },
(_, i) => 0.02 + i * 0.078
);
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 12, 27);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
azimuth: 0.72,
elevation: 0.48,
distance: 14.8
};
const cameraGoal = {
azimuth: 0.72,
elevation: 0.48,
distance: 14.8
};
const cameraTarget = new THREE.Vector3(0, 0.2, 0);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9ddff, 0x07101d, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(7, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x45d8ff, 12, 12, 2);
cyanLight.position.set(-4, 3.5, 3);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffa83d, 10, 10, 2);
amberLight.position.set(4, 1.2, -3);
scene.add(amberLight);
// -------------------------------------------------------------------------
// Low-poly platform and substrate pool
// -------------------------------------------------------------------------
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1c30,
roughness: 0.88,
metalness: 0.12,
flatShading: true
});
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(6.25, 6.5, 0.34, 12),
platformMaterial
);
platform.position.y = -1.12;
platform.receiveShadow = true;
scene.add(platform);
const platformRim = new THREE.Mesh(
new THREE.TorusGeometry(6.13, 0.045, 4, 36),
new THREE.MeshBasicMaterial({ color: 0x2e6c91 })
);
platformRim.rotation.x = Math.PI / 2;
platformRim.position.y = -0.94;
scene.add(platformRim);
const grid = new THREE.GridHelper(12, 24, 0x2b5875, 0x18344c);
grid.position.y = -0.935;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
const poolGeometry = new THREE.IcosahedronGeometry(1.12, 1);
const poolMaterial = new THREE.MeshStandardMaterial({
color: 0xffa83d,
emissive: 0xff7a22,
emissiveIntensity: 0.55,
roughness: 0.38,
metalness: 0.05,
transparent: true,
opacity: 0.1,
depthWrite: false,
flatShading: true
});
const substratePool = new THREE.Mesh(poolGeometry, poolMaterial);
substratePool.position.y = 0.28;
scene.add(substratePool);
const poolWireMaterial = new THREE.MeshBasicMaterial({
color: 0xffcf70,
wireframe: true,
transparent: true,
opacity: 0.22
});
const substratePoolWire = new THREE.Mesh(poolGeometry, poolWireMaterial);
substratePoolWire.position.copy(substratePool.position);
substratePoolWire.scale.setScalar(1.035);
scene.add(substratePoolWire);
// -------------------------------------------------------------------------
// Fixed enzyme sites
// -------------------------------------------------------------------------
const enzymeGeometry = new THREE.IcosahedronGeometry(0.72, 1);
const pedestalGeometry = new THREE.CylinderGeometry(0.77, 0.9, 0.18, 8);
const cavityGeometry = new THREE.OctahedronGeometry(0.23, 0);
const pocketGeometry = new THREE.TorusGeometry(0.25, 0.055, 4, 8);
const boundGeometry = new THREE.OctahedronGeometry(0.18, 0);
const enzymeMaterials = [
new THREE.MeshStandardMaterial({
color: 0x5874c8,
roughness: 0.48,
metalness: 0.12,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x397fa0,
roughness: 0.5,
metalness: 0.12,
flatShading: true
})
];
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x132a42,
roughness: 0.72,
metalness: 0.15,
flatShading: true
});
const cavityMaterial = new THREE.MeshStandardMaterial({
color: 0x07101b,
roughness: 0.9,
metalness: 0,
flatShading: true
});
const boundMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0xff8c1a,
emissiveIntensity: 0.85,
roughness: 0.38,
metalness: 0.05,
flatShading: true
});
const sites = [];
const siteRadius = 4.35;
for (let i = 0; i < SITE_COUNT; i++) {
const angle = (i / SITE_COUNT) * Math.PI * 2;
const group = new THREE.Group();
group.position.set(
Math.sin(angle) * siteRadius,
0,
Math.cos(angle) * siteRadius
);
// The local +Z direction points radially outward from the center.
group.rotation.y = angle;
const pedestal = new THREE.Mesh(pedestalGeometry, pedestalMaterial);
pedestal.position.y = -0.74;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const enzyme = new THREE.Mesh(
enzymeGeometry,
enzymeMaterials[i % enzymeMaterials.length]
);
enzyme.rotation.set(
0.12 * Math.sin(i),
0.28 * Math.cos(i * 1.7),
0.08 * Math.sin(i * 0.9)
);
enzyme.castShadow = true;
enzyme.receiveShadow = true;
group.add(enzyme);
const cavity = new THREE.Mesh(cavityGeometry, cavityMaterial);
cavity.position.z = 0.58;
cavity.scale.set(1, 1, 0.42);
group.add(cavity);
const pocketMaterial = new THREE.MeshStandardMaterial({
color: 0x4bd8ff,
emissive: 0x36c8f4,
emissiveIntensity: 0.12,
roughness: 0.4,
metalness: 0.2,
flatShading: true
});
const pocket = new THREE.Mesh(pocketGeometry, pocketMaterial);
pocket.position.z = 0.66;
group.add(pocket);
const bound = new THREE.Mesh(boundGeometry, boundMaterial);
bound.position.z = 0.79;
bound.scale.setScalar(0.001);
bound.castShadow = true;
group.add(bound);
scene.add(group);
sites.push({
group,
pocket,
bound,
threshold: occupancyThresholds[i],
boundTarget: false,
boundScale: 0
});
}
// -------------------------------------------------------------------------
// Free substrate glyphs
// -------------------------------------------------------------------------
const freeGeometry = new THREE.TetrahedronGeometry(0.17, 0);
const freeMaterial = new THREE.MeshStandardMaterial({
color: 0xff914d,
emissive: 0xff6a21,
emissiveIntensity: 0.52,
roughness: 0.42,
metalness: 0.03,
flatShading: true
});
const freeSubstrates = [];
for (let i = 0; i < MAX_FREE_GLYPHS; i++) {
const mesh = new THREE.Mesh(freeGeometry, freeMaterial);
mesh.visible = false;
mesh.castShadow = true;
scene.add(mesh);
freeSubstrates.push({
mesh,
phase: Math.random() * Math.PI * 2,
radius: 1.55 + Math.random() * 4.2,
height: -0.25 + Math.random() * 3.0,
speed: 0.12 + Math.random() * 0.28,
verticalSpeed: 0.7 + Math.random() * 0.8,
eccentricity: 0.72 + Math.random() * 0.32
});
}
// -------------------------------------------------------------------------
// Occupancy and UI updates
// -------------------------------------------------------------------------
function countOccupiedSites(probability) {
return occupancyThresholds.filter(
(threshold) => probability > threshold
).length;
}
function chartX(value) {
return chart.left +
(value / MAX_SUBSTRATE) * (chart.right - chart.left);
}
function chartY(value) {
return chart.bottom -
value * (chart.bottom - chart.top);
}
function buildCurve() {
let line = "";
let area = "";
for (let s = 0; s <= MAX_SUBSTRATE; s += 2) {
const p = s / (KM + s);
const x = chartX(s);
const y = chartY(p);
const command = s === 0 ? "M" : "L";
line += `${command} ${x.toFixed(2)} ${y.toFixed(2)} `;
}
area =
line +
`L ${chart.right} ${chart.bottom} ` +
`L ${chart.left} ${chart.bottom} Z`;
document.querySelector("#curve-line").setAttribute("d", line);
document.querySelector("#curve-area").setAttribute("d", area);
}
function updateUI() {
expectedOccupancy = substrate / (KM + substrate);
const occupied = countOccupiedSites(expectedOccupancy);
// Glyph count is a visual density proxy, not a molecule count.
const glyphCount = Math.round(
(substrate / MAX_SUBSTRATE) * MAX_FREE_GLYPHS
);
substrateValue.textContent = `${substrate} a.u.`;
equilibriumValue.textContent = `${(expectedOccupancy * 100).toFixed(1)}%`;
occupiedCountValue.textContent = `${occupied} / ${SITE_COUNT}`;
glyphCountValue.textContent = String(glyphCount);
for (const site of sites) {
site.boundTarget = expectedOccupancy > site.threshold;
}
for (let i = 0; i < freeSubstrates.length; i++) {
freeSubstrates[i].mesh.visible = i < glyphCount;
}
const markerX = chartX(substrate);
const markerY = chartY(expectedOccupancy);
const marker = document.querySelector("#marker");
marker.setAttribute("cx", markerX.toFixed(2));
marker.setAttribute("cy", markerY.toFixed(2));
const markerLine = document.querySelector("#marker-line");
markerLine.setAttribute("x1", markerX.toFixed(2));
markerLine.setAttribute("x2", markerX.toFixed(2));
}
buildCurve();
updateUI();
slider.addEventListener("input", () => {
substrate = Number(slider.value);
updateUI();
});
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
slider.value = button.dataset.value;
substrate = Number(slider.value);
updateUI();
});
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume drift" : "Pause drift";
pauseButton.setAttribute("aria-pressed", String(paused));
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraGoal.azimuth -= deltaX * 0.006;
cameraGoal.elevation = THREE.MathUtils.clamp(
cameraGoal.elevation + deltaY * 0.005,
0.16,
1.18
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined) {
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.distance = THREE.MathUtils.clamp(
cameraGoal.distance * Math.exp(event.deltaY * 0.001),
8.5,
23
);
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowLeft":
cameraGoal.azimuth -= 0.12;
break;
case "ArrowRight":
cameraGoal.azimuth += 0.12;
break;
case "ArrowUp":
cameraGoal.elevation = THREE.MathUtils.clamp(
cameraGoal.elevation + 0.08,
0.16,
1.18
);
break;
case "ArrowDown":
cameraGoal.elevation = THREE.MathUtils.clamp(
cameraGoal.elevation - 0.08,
0.16,
1.18
);
break;
case "+":
case "=":
cameraGoal.distance = Math.max(8.5, cameraGoal.distance - 1);
break;
case "-":
case "_":
cameraGoal.distance = Math.min(23, cameraGoal.distance + 1);
break;
case "r":
case "R":
resetCamera();
break;
default:
return;
}
event.preventDefault();
});
function resetCamera() {
cameraGoal.azimuth = 0.72;
cameraGoal.elevation = 0.48;
cameraGoal.distance = 14.8;
}
resetViewButton.addEventListener("click", resetCamera);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-10 * dt);
cameraState.azimuth +=
(cameraGoal.azimuth - cameraState.azimuth) * smoothing;
cameraState.elevation +=
(cameraGoal.elevation - cameraState.elevation) * smoothing;
cameraState.distance +=
(cameraGoal.distance - cameraState.distance) * smoothing;
const horizontalDistance =
cameraState.distance * Math.cos(cameraState.elevation);
camera.position.set(
cameraTarget.x +
Math.sin(cameraState.azimuth) * horizontalDistance,
cameraTarget.y +
Math.sin(cameraState.elevation) * cameraState.distance,
cameraTarget.z +
Math.cos(cameraState.azimuth) * horizontalDistance
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
animationTime += dt;
}
updateCamera(dt);
// Smoothly animate binding and unbinding transitions.
const siteSmoothing = 1 - Math.exp(-9 * dt);
for (const site of sites) {
const targetScale = site.boundTarget ? 1 : 0;
site.boundScale +=
(targetScale - site.boundScale) * siteSmoothing;
if (site.boundScale < 0.008) {
site.bound.visible = false;
} else {
site.bound.visible = true;
site.bound.scale.setScalar(site.boundScale);
site.bound.position.z = 0.69 + site.boundScale * 0.1;
}
site.pocket.material.emissiveIntensity =
0.12 + site.boundScale * 1.25;
const pulse = paused
? 0
: Math.sin(animationTime * 2.4 + site.threshold * 8) *
0.025 *
site.boundScale;
site.pocket.scale.setScalar(1 + pulse);
}
// Low-poly substrate pool expands and brightens with availability.
const poolScale =
0.66 +
expectedOccupancy * 0.58 +
(paused ? 0 : Math.sin(animationTime * 1.7) * 0.025);
substratePool.scale.setScalar(poolScale);
substratePoolWire.scale.setScalar(poolScale * 1.035);
substratePool.rotation.y = animationTime * 0.12;
substratePool.rotation.x = animationTime * 0.07;
substratePoolWire.rotation.y = substratePool.rotation.y;
substratePoolWire.rotation.x = substratePool.rotation.x;
poolMaterial.opacity = 0.045 + expectedOccupancy * 0.16;
poolWireMaterial.opacity = 0.11 + expectedOccupancy * 0.25;
poolMaterial.emissiveIntensity = 0.3 + expectedOccupancy * 0.75;
// Animate the visible free substrate glyphs.
for (const particle of freeSubstrates) {
if (!particle.mesh.visible) continue;
const angle =
particle.phase + animationTime * particle.speed;
const x =
Math.cos(angle) *
particle.radius *
particle.eccentricity;
const z =
Math.sin(angle) *
particle.radius;
const y =
particle.height +
Math.sin(
animationTime * particle.verticalSpeed + particle.phase
) * 0.36;
particle.mesh.position.set(x, y, z);
particle.mesh.rotation.x = animationTime * 0.8 + particle.phase;
particle.mesh.rotation.y = animationTime * 1.1 + particle.phase * 0.5;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```