Prompt specification
Build or customize the Three.js 3D scene "Pressure and layering demonstration tray" 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: Pressure and layering demonstration tray
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pressure-and-layering-demonstration-tray-x1178
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing the rock cycle. Arrange layered soft material beneath a compression plate beside an uncompressed sample; animate deformation as a qualitative analogy, explicitly avoiding claims of actual rock metamorphism. Stylized solid forms.
Working Three.js Starter (pressure-and-layering-demonstration-tray-x1178):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>Rock Cycle — Qualitative Compression Analogy</title>
<style>
:root {
--ink: #26383d;
--muted: #66777a;
--paper: #f3eee6;
--accent: #b76558;
--teal: #719a9a;
--ochre: #d5a164;
--panel: rgba(255, 252, 247, 0.9);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: 24px;
left: 28px;
max-width: 480px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--accent);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.75rem, 3.2vw, 3rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 430px;
margin: 12px 0 0;
color: var(--muted);
font-size: 0.98rem;
line-height: 1.5;
}
.legend {
position: absolute;
top: 26px;
right: 28px;
display: flex;
align-items: center;
gap: 16px;
padding: 10px 14px;
border: 1px solid rgba(38, 56, 61, 0.1);
border-radius: 999px;
background: var(--panel);
box-shadow: 0 8px 30px rgba(64, 49, 35, 0.06);
color: var(--muted);
font-size: 0.78rem;
backdrop-filter: blur(12px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 50%;
background: var(--ochre);
box-shadow:
13px 0 0 var(--teal),
26px 0 0 var(--accent);
margin-right: 26px;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid rgba(38, 56, 61, 0.12);
border-radius: 9px;
background: rgba(255, 252, 247, 0.86);
box-shadow: 0 7px 24px rgba(64, 49, 35, 0.08);
color: var(--ink);
font-size: 0.75rem;
font-weight: 750;
letter-spacing: 0.02em;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(10px);
}
.scene-label::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(183, 101, 88, 0.13);
}
#referenceLabel::before {
background: var(--teal);
box-shadow: 0 0 0 4px rgba(113, 154, 154, 0.15);
}
.controls {
position: absolute;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
width: max-content;
max-width: calc(100vw - 32px);
padding: 10px;
border: 1px solid rgba(38, 56, 61, 0.12);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(64, 49, 35, 0.12);
pointer-events: auto;
transform: translateX(-50%);
backdrop-filter: blur(14px);
}
button {
min-height: 38px;
padding: 0 15px;
border: 1px solid rgba(38, 56, 61, 0.16);
border-radius: 10px;
background: #fffdf9;
color: var(--ink);
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
background: white;
transform: translateY(-1px);
box-shadow: 0 5px 12px rgba(64, 49, 35, 0.09);
}
button:active {
transform: translateY(0);
}
button.primary {
min-width: 84px;
border-color: var(--ink);
background: var(--ink);
color: #fffdf9;
}
button.primary:hover {
background: #344b50;
}
.speed {
display: grid;
grid-template-columns: auto 96px;
align-items: center;
gap: 5px 9px;
min-width: 145px;
padding: 0 7px;
color: var(--muted);
font-size: 0.72rem;
font-weight: 750;
}
.speed label {
display: flex;
justify-content: space-between;
grid-column: 1 / -1;
}
.speed output {
color: var(--ink);
}
input[type="range"] {
width: 100%;
accent-color: var(--accent);
cursor: pointer;
}
.load-meter {
display: grid;
grid-template-columns: auto 68px 34px;
align-items: center;
gap: 8px;
padding: 0 8px 0 4px;
color: var(--muted);
font-size: 0.7rem;
font-weight: 750;
white-space: nowrap;
}
.meter-track {
width: 68px;
height: 6px;
overflow: hidden;
border-radius: 999px;
background: rgba(38, 56, 61, 0.11);
}
.meter-fill {
display: block;
width: 0%;
height: 100%;
border-radius: inherit;
background: var(--accent);
transition: width 80ms linear;
}
#loadValue {
color: var(--ink);
text-align: right;
font-variant-numeric: tabular-nums;
}
.gesture {
padding: 0 8px 0 2px;
color: #849092;
font-size: 0.69rem;
white-space: nowrap;
}
.disclaimer {
position: absolute;
right: 24px;
bottom: 24px;
max-width: 315px;
padding: 14px 16px;
border: 1px solid rgba(38, 56, 61, 0.1);
border-left: 4px solid var(--accent);
border-radius: 12px;
background: rgba(255, 252, 247, 0.92);
box-shadow: 0 12px 36px rgba(64, 49, 35, 0.1);
color: var(--muted);
font-size: 0.77rem;
line-height: 1.5;
backdrop-filter: blur(12px);
}
.disclaimer strong {
display: block;
margin-bottom: 3px;
color: var(--ink);
font-size: 0.8rem;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: var(--paper);
color: var(--muted);
font-size: 0.85rem;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.legend,
.gesture {
display: none;
}
.disclaimer {
right: 16px;
bottom: 104px;
max-width: 285px;
}
}
@media (max-width: 650px) {
header {
top: 16px;
left: 16px;
right: 16px;
}
.subtitle {
max-width: 340px;
font-size: 0.82rem;
}
.disclaimer {
left: 12px;
right: 12px;
bottom: 111px;
max-width: none;
padding: 10px 12px;
font-size: 0.7rem;
}
.controls {
bottom: 12px;
flex-wrap: wrap;
justify-content: center;
width: calc(100vw - 24px);
gap: 7px;
padding: 8px;
}
button {
min-height: 34px;
padding: 0 12px;
font-size: 0.78rem;
}
.speed {
min-width: 120px;
grid-template-columns: auto 78px;
}
.load-meter {
grid-template-columns: auto 52px 31px;
font-size: 0.65rem;
}
.meter-track {
width: 52px;
}
.scene-label {
padding: 6px 8px;
font-size: 0.65rem;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header>
<div class="eyebrow">Rock-cycle context</div>
<h1>Compression analogy</h1>
<p class="subtitle">
A stylized comparison of layered soft material beneath a moving
compression plate beside an uncompressed reference sample.
</p>
</header>
<div class="legend" aria-hidden="true">
<div class="legend-item">
<span class="swatch"></span>
Three stylized soft layers
</div>
</div>
<div id="loadLabel" class="scene-label">Compression setup</div>
<div id="referenceLabel" class="scene-label">Uncompressed reference</div>
<section class="controls" aria-label="Animation controls">
<button id="toggleButton" class="primary" type="button" aria-pressed="false">
Pause
</button>
<button id="resetButton" type="button">Reset</button>
<div class="speed">
<label for="speedInput">
<span>Speed</span>
<output id="speedOutput">1.0×</output>
</label>
<input
id="speedInput"
type="range"
min="0.4"
max="1.6"
step="0.1"
value="1"
>
</div>
<div class="load-meter">
<span>Visual load</span>
<div class="meter-track" aria-hidden="true">
<i id="loadFill" class="meter-fill"></i>
</div>
<output id="loadValue">0%</output>
</div>
<div class="gesture">Drag to orbit · Scroll to zoom</div>
</section>
<aside class="disclaimer">
<strong>Qualitative analogy — not actual metamorphism.</strong>
This visual shows how stylized material might shorten and bulge under load.
It is not a geological simulation and does not claim that these layers are
rock, that heat or mineral recrystallization are occurring, or that the
animation demonstrates actual rock metamorphism or the complete rock cycle.
</aside>
</div>
<div id="loading">Loading the compression diagram…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const loading = document.getElementById("loading");
const toggleButton = document.getElementById("toggleButton");
const resetButton = document.getElementById("resetButton");
const speedInput = document.getElementById("speedInput");
const speedOutput = document.getElementById("speedOutput");
const loadFill = document.getElementById("loadFill");
const loadValue = document.getElementById("loadValue");
const loadLabel = document.getElementById("loadLabel");
const referenceLabel = document.getElementById("referenceLabel");
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
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("role", "img");
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D diagram of layered material being compressed beside an uncompressed reference."
);
renderer.domElement.tabIndex = 0;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3eee6);
scene.fog = new THREE.Fog(0xf3eee6, 17, 30);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemisphere = new THREE.HemisphereLight(
0xfff8ec,
0x786a5c,
2.0
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd8, 3.0);
keyLight.position.set(-5.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc9e1e4, 1.05);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(32, 22),
new THREE.MeshStandardMaterial({
color: 0xe9e1d6,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Stylized platforms
// ---------------------------------------------------------------------
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0xd8cec0,
roughness: 0.92,
metalness: 0
});
const platformTopMaterial = new THREE.MeshStandardMaterial({
color: 0xeee6da,
roughness: 0.86,
metalness: 0
});
function createPlatform(x) {
const group = new THREE.Group();
group.position.x = x;
const base = new THREE.Mesh(
new THREE.BoxGeometry(3.35, 0.18, 2.7),
platformMaterial
);
base.position.y = 0.08;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(
new THREE.BoxGeometry(3.03, 0.05, 2.38),
platformTopMaterial
);
top.position.y = 0.195;
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
scene.add(group);
return group;
}
const LEFT_X = -1.95;
const RIGHT_X = 1.95;
const BASE_Y = 0.225;
createPlatform(LEFT_X);
createPlatform(RIGHT_X);
// ---------------------------------------------------------------------
// Rounded, solid layer geometry
// ---------------------------------------------------------------------
const layerSpecs = [
{
height: 1.03,
bulge: 0.20,
color: 0xd5a164
},
{
height: 1.13,
bulge: 0.27,
color: 0x719a9a
},
{
height: 1.10,
bulge: 0.23,
color: 0xb96f62
}
];
function createLayerGeometry() {
const profile = [
new THREE.Vector2(0.00, 0.00),
new THREE.Vector2(0.78, 0.00),
new THREE.Vector2(0.94, 0.025),
new THREE.Vector2(1.01, 0.09),
new THREE.Vector2(1.03, 0.20),
new THREE.Vector2(1.03, 0.80),
new THREE.Vector2(1.01, 0.91),
new THREE.Vector2(0.94, 0.975),
new THREE.Vector2(0.78, 1.00),
new THREE.Vector2(0.00, 1.00)
];
const geometry = new THREE.LatheGeometry(profile, 64);
geometry.attributes.position.setUsage(THREE.DynamicDrawUsage);
return geometry;
}
function createStack(x) {
const group = new THREE.Group();
group.position.set(x, BASE_Y, 0);
const records = [];
for (const spec of layerSpecs) {
const geometry = createLayerGeometry();
const material = new THREE.MeshStandardMaterial({
color: spec.color,
roughness: 0.74,
metalness: 0,
flatShading: false
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
group.add(mesh);
records.push({
mesh,
geometry,
basePositions: new Float32Array(
geometry.attributes.position.array
),
height: spec.height,
bulge: spec.bulge
});
}
scene.add(group);
return { group, records };
}
const animatedStack = createStack(LEFT_X);
const referenceStack = createStack(RIGHT_X);
function updateStack(stack, pressure) {
let cursor = 0;
for (const record of stack.records) {
const height = record.height * (1 - 0.34 * pressure);
record.mesh.position.y = cursor;
record.mesh.scale.set(1, height, 1);
const position = record.geometry.attributes.position;
const vertices = position.array;
const base = record.basePositions;
for (let i = 0; i < vertices.length; i += 3) {
const t = base[i + 1];
const middleWeight = Math.pow(Math.sin(Math.PI * t), 1.4);
const radialScale = 1 + pressure * record.bulge * middleWeight;
vertices[i] = base[i] * radialScale;
vertices[i + 1] = t;
vertices[i + 2] = base[i + 2] * radialScale;
}
position.needsUpdate = true;
record.geometry.computeVertexNormals();
record.geometry.computeBoundingSphere();
cursor += height;
}
return cursor;
}
updateStack(animatedStack, 0);
updateStack(referenceStack, 0);
// ---------------------------------------------------------------------
// Compression plate, guide rails, and downward arrows
// ---------------------------------------------------------------------
const plateGroup = new THREE.Group();
plateGroup.position.x = LEFT_X;
scene.add(plateGroup);
const plateMaterial = new THREE.MeshStandardMaterial({
color: 0x3f5862,
roughness: 0.48,
metalness: 0.28
});
const plateTopMaterial = new THREE.MeshStandardMaterial({
color: 0x71878e,
roughness: 0.5,
metalness: 0.24
});
const plateBottomMaterial = new THREE.MeshStandardMaterial({
color: 0x293d45,
roughness: 0.68,
metalness: 0.18
});
const plate = new THREE.Mesh(
new THREE.BoxGeometry(2.85, 0.24, 2.35),
plateMaterial
);
plate.castShadow = true;
plate.receiveShadow = true;
plateGroup.add(plate);
const plateTop = new THREE.Mesh(
new THREE.BoxGeometry(2.5, 0.08, 2.0),
plateTopMaterial
);
plateTop.position.y = 0.16;
plateTop.castShadow = true;
plateTop.receiveShadow = true;
plateGroup.add(plateTop);
const pressurePad = new THREE.Mesh(
new THREE.BoxGeometry(2.55, 0.08, 2.05),
plateBottomMaterial
);
pressurePad.position.y = -0.16;
pressurePad.castShadow = true;
pressurePad.receiveShadow = true;
plateGroup.add(pressurePad);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xbcc5c5,
roughness: 0.32,
metalness: 0.58
});
for (const x of [-1.08, 1.08]) {
for (const z of [-0.82, 0.82]) {
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.07, 0.06, 20),
boltMaterial
);
bolt.position.set(x, 0.225, z);
bolt.castShadow = true;
plateGroup.add(bolt);
}
}
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0xb76558,
roughness: 0.7,
metalness: 0,
transparent: true,
opacity: 0.65,
depthWrite: false
});
for (const x of [-0.83, 0, 0.83]) {
const arrow = new THREE.Group();
arrow.position.x = x;
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.42, 16),
arrowMaterial
);
shaft.position.y = 1.18;
shaft.castShadow = true;
arrow.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.28, 20),
arrowMaterial
);
head.position.y = 0.82;
head.rotation.z = Math.PI;
head.castShadow = true;
arrow.add(head);
plateGroup.add(arrow);
}
const guideGroup = new THREE.Group();
guideGroup.position.x = LEFT_X;
const guideMaterial = new THREE.MeshStandardMaterial({
color: 0x68777b,
roughness: 0.42,
metalness: 0.48
});
for (const x of [-1.23, 1.23]) {
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 5.2, 18),
guideMaterial
);
rod.position.set(x, 2.82, -1.02);
rod.castShadow = true;
guideGroup.add(rod);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.16, 0.12, 20),
plateBottomMaterial
);
foot.position.set(x, 0.27, -1.02);
foot.castShadow = true;
guideGroup.add(foot);
}
scene.add(guideGroup);
// ---------------------------------------------------------------------
// Custom orbit and zoom controls, implemented without addons
// ---------------------------------------------------------------------
const initialCamera = {
radius: 14.2,
theta: 0.12,
phi: 1.13
};
const orbit = {
radius: initialCamera.radius,
theta: initialCamera.theta,
phi: initialCamera.phi,
targetRadius: initialCamera.radius,
targetTheta: initialCamera.theta,
targetPhi: initialCamera.phi,
target: new THREE.Vector3(0, 1.8, 0)
};
function applyCamera() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * cosPhi,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
let dragging = false;
let activePointer = null;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 || activePointer !== null) return;
activePointer = event.pointerId;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.targetTheta -= dx * 0.0055;
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - dy * 0.0045,
0.55,
1.43
);
});
function stopDragging(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * Math.exp(event.deltaY * 0.001),
8.5,
20
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function resetCamera() {
orbit.targetRadius = initialCamera.radius;
orbit.targetTheta = initialCamera.theta;
orbit.targetPhi = initialCamera.phi;
}
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Animation state
// ---------------------------------------------------------------------
const CYCLE_DURATION = 5;
let cycleTime = 0;
let speed = 1;
let paused = false;
let lastTime = performance.now();
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function pressureAt(time) {
const phase = (time % CYCLE_DURATION) / CYCLE_DURATION;
if (phase < 0.20) {
return smoothstep(phase / 0.20);
}
if (phase < 0.58) {
return 1;
}
if (phase < 0.88) {
return 1 - smoothstep((phase - 0.58) / 0.30);
}
return 0;
}
function setPaused(nextPaused) {
paused = nextPaused;
toggleButton.textContent = paused ? "Resume" : "Pause";
toggleButton.setAttribute("aria-pressed", String(paused));
}
toggleButton.addEventListener("click", () => {
setPaused(!paused);
});
resetButton.addEventListener("click", () => {
cycleTime = 0;
setPaused(false);
resetCamera();
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedOutput.value = `${speed.toFixed(1)}×`;
});
window.addEventListener("keydown", (event) => {
const tag = event.target?.tagName;
if (tag === "INPUT" || tag === "BUTTON") return;
if (event.code === "Space") {
event.preventDefault();
setPaused(!paused);
}
if (event.key.toLowerCase() === "r") {
cycleTime = 0;
setPaused(false);
resetCamera();
}
});
// ---------------------------------------------------------------------
// Labels and responsive behavior
// ---------------------------------------------------------------------
const loadLabelPosition = new THREE.Vector3(LEFT_X, 5.65, 0);
const referenceLabelPosition = new THREE.Vector3(RIGHT_X, 4.18, 0);
const projected = new THREE.Vector3();
function updateLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = width < 650 ? 52 : 42;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
applyCamera();
// ---------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (!paused) {
cycleTime = (cycleTime + delta * speed) % CYCLE_DURATION;
}
const pressure = pressureAt(cycleTime);
const compressedHeight = updateStack(animatedStack, pressure);
// The plate begins slightly above the stack, approaches it, and then
// retreats while the layers return to their original shape.
const plateBottom =
compressedHeight +
0.42 * (1 - pressure) +
0.05 * pressure;
plateGroup.position.y = plateBottom + 0.20;
arrowMaterial.opacity = 0.48 + pressure * 0.46;
const percentage = Math.round(pressure * 100);
loadFill.style.width = `${percentage}%`;
loadValue.value = `${percentage}%`;
// Smooth the direct camera controls.
orbit.theta += (orbit.targetTheta - orbit.theta) * 0.09;
orbit.phi += (orbit.targetPhi - orbit.phi) * 0.09;
orbit.radius += (orbit.targetRadius - orbit.radius) * 0.08;
applyCamera();
updateLabel(loadLabel, loadLabelPosition);
updateLabel(referenceLabel, referenceLabelPosition);
renderer.render(scene, camera);
}
renderer.render(scene, camera);
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
requestAnimationFrame(animate);
</script>
</body>
</html>
```