Prompt specification
Build or customize the Three.js 3D scene "Compression comparison across states" 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: Compression comparison across states
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compression-comparison-across-states-x1966
Scene Description Prompt:
Please use Three.js from cdnjs for a cutaway scene based on this idea: compression comparison across states. Apply a moving wall to three toy particle chambers and show qualitatively different volume responses; distinguish this illustration from a complete molecular-dynamics calculation. Return one self-contained HTML file.
Working Three.js Starter (compression-comparison-across-states-x1966):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Compression Comparison — Qualitative Cutaway Illustration</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 24, 40, 0.82);
--panel-border: rgba(145, 199, 231, 0.17);
--text: #eef8ff;
--muted: #94aabd;
--gas: #61ddff;
--liquid: #71efb1;
--solid: #ffbd73;
--load: #ff7165;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active { cursor: grabbing; }
.vignette {
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 34%, rgba(2, 8, 16, 0.22) 76%),
linear-gradient(180deg, rgba(3, 9, 17, 0.08), transparent 45%, rgba(3, 9, 17, 0.42));
}
.masthead {
position: absolute;
z-index: 5;
top: 24px;
left: 28px;
max-width: 560px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: #86a9bf;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 1px;
content: "";
background: var(--gas);
box-shadow: 0 0 10px rgba(97, 221, 255, 0.8);
}
h1 {
max-width: 530px;
margin: 0;
font-size: clamp(25px, 3vw, 39px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1.03;
}
.masthead p {
max-width: 510px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.masthead p strong { color: #d4e9f6; }
.notice {
position: absolute;
z-index: 5;
top: 22px;
right: 24px;
width: min(390px, calc(100vw - 48px));
padding: 12px 14px 13px;
border: 1px solid rgba(255, 181, 111, 0.24);
border-radius: 12px;
background: rgba(20, 25, 36, 0.74);
box-shadow: 0 14px 36px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(14px);
}
.notice-title {
display: flex;
align-items: center;
gap: 8px;
color: #ffd29a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.notice-title::before {
width: 7px;
height: 7px;
flex: 0 0 auto;
content: "";
border-radius: 50%;
background: #ffb766;
box-shadow: 0 0 12px rgba(255, 183, 102, 0.8);
}
.notice p {
margin: 7px 0 0;
color: #a9b4c2;
font-size: 10.5px;
line-height: 1.45;
}
#labels {
position: absolute;
inset: 0;
z-index: 3;
pointer-events: none;
}
.scene-tag {
position: absolute;
min-width: 142px;
padding: 8px 10px;
transform: translate(-50%, -100%);
border: 1px solid var(--accent-border);
border-radius: 9px;
background: rgba(5, 16, 28, 0.78);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(10px);
white-space: nowrap;
}
.scene-tag strong {
display: flex;
align-items: center;
gap: 7px;
color: var(--accent);
font-size: 11px;
letter-spacing: 0.04em;
}
.scene-tag strong::before {
width: 6px;
height: 6px;
content: "";
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 9px var(--accent);
}
.scene-tag span {
display: block;
margin-top: 3px;
padding-left: 13px;
color: #8fa5b6;
font-size: 9.5px;
}
#compare {
position: absolute;
z-index: 5;
left: 50%;
bottom: 122px;
width: min(930px, calc(100vw - 40px));
transform: translateX(-50%);
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 9px;
pointer-events: none;
}
.state-card {
--accent: var(--gas);
min-width: 0;
padding: 11px 13px 10px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.19);
backdrop-filter: blur(14px);
}
.state-card[data-state="liquid"] { --accent: var(--liquid); }
.state-card[data-state="solid"] { --accent: var(--solid); }
.card-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.card-title strong {
overflow: hidden;
color: var(--accent);
font-size: 11px;
letter-spacing: 0.04em;
text-overflow: ellipsis;
white-space: nowrap;
}
.card-title b {
color: #eef8ff;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.bar {
position: relative;
height: 4px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(163, 203, 226, 0.11);
}
.bar i {
position: absolute;
inset: 0 auto 0 0;
width: 100%;
border-radius: inherit;
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
transition: width 80ms linear;
}
.card-meta {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 6px;
color: #8299ab;
font-size: 9px;
}
.card-meta em {
color: #b8c9d5;
font-style: normal;
font-variant-numeric: tabular-nums;
}
#controls {
position: absolute;
z-index: 6;
left: 50%;
bottom: 18px;
width: min(930px, calc(100vw - 40px));
transform: translateX(-50%);
padding: 12px 14px 13px;
border: 1px solid var(--panel-border);
border-radius: 15px;
background: rgba(7, 18, 31, 0.9);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(18px);
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 10px;
}
.control-heading strong {
font-size: 12px;
font-weight: 700;
}
.control-heading small {
margin-left: 8px;
color: #7f98aa;
font-size: 9.5px;
}
#status {
overflow: hidden;
color: #9eb4c5;
font-size: 9.5px;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
.control-row {
display: grid;
grid-template-columns: 112px minmax(120px, 1fr) auto;
align-items: center;
gap: 14px;
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(153, 199, 224, 0.17);
border-radius: 9px;
background: rgba(139, 188, 216, 0.07);
color: #c9dce9;
font-size: 10.5px;
font-weight: 700;
cursor: pointer;
transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(153, 199, 224, 0.34);
background: rgba(139, 188, 216, 0.13);
}
button:active { transform: translateY(1px); }
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--gas);
outline-offset: 3px;
}
#playBtn {
border-color: rgba(255, 113, 101, 0.4);
background: rgba(255, 113, 101, 0.12);
color: #ffd2ce;
}
.slider-block {
display: grid;
grid-template-columns: 1fr 42px;
align-items: center;
gap: 9px;
}
.slider-label {
display: flex;
align-items: center;
justify-content: space-between;
color: #9eb4c5;
font-size: 9.5px;
}
.slider-label output {
color: #e7f4fb;
font-size: 11px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
#load {
grid-column: 1 / -1;
width: 100%;
height: 18px;
margin: 0;
appearance: none;
background: transparent;
cursor: ew-resize;
}
#load::-webkit-slider-runnable-track {
height: 4px;
border-radius: 999px;
background: linear-gradient(90deg, #ff7165 0%, #ff7165 var(--fill, 0%), rgba(146, 191, 218, 0.14) var(--fill, 0%));
}
#load::-moz-range-track {
height: 4px;
border-radius: 999px;
background: rgba(146, 191, 218, 0.14);
}
#load::-moz-range-progress {
height: 4px;
border-radius: 999px;
background: #ff7165;
}
#load::-webkit-slider-thumb {
width: 14px;
height: 14px;
margin-top: -5px;
appearance: none;
border: 2px solid #fff0ed;
border-radius: 50%;
background: #ff7165;
box-shadow: 0 0 14px rgba(255, 113, 101, 0.7);
}
#load::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #fff0ed;
border-radius: 50%;
background: #ff7165;
box-shadow: 0 0 14px rgba(255, 113, 101, 0.7);
}
.secondary-actions {
display: flex;
gap: 7px;
}
.secondary-actions button { min-width: 62px; }
.camera-help {
position: absolute;
z-index: 4;
right: 24px;
bottom: 25px;
color: rgba(154, 181, 198, 0.58);
font-size: 9px;
pointer-events: none;
}
#loading {
position: absolute;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: #9db4c5;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading.hidden { display: none; }
@media (max-width: 980px) {
.masthead { top: 18px; left: 18px; max-width: 490px; }
.notice { top: 18px; right: 18px; width: 320px; }
.camera-help { display: none; }
.control-heading small { display: none; }
}
@media (max-width: 760px) {
.masthead { max-width: calc(100vw - 36px); }
h1 { max-width: 430px; }
.masthead p { max-width: 430px; }
.notice {
top: auto;
right: 14px;
bottom: 190px;
width: min(340px, calc(100vw - 28px));
}
#compare {
bottom: 112px;
width: calc(100vw - 20px);
gap: 5px;
}
.state-card { padding: 9px 8px 8px; border-radius: 9px; }
.card-title { display: block; }
.card-title b { display: block; margin-top: 2px; font-size: 14px; }
.card-meta span:last-child { display: none; }
#controls {
bottom: 10px;
width: calc(100vw - 20px);
padding: 10px;
}
.control-row {
grid-template-columns: 84px minmax(100px, 1fr);
gap: 8px;
}
.secondary-actions {
grid-column: 1 / -1;
justify-content: flex-end;
}
.secondary-actions button { min-height: 30px; }
#playBtn { min-height: 36px; }
}
@media (max-width: 520px) {
.masthead p { display: none; }
.eyebrow { margin-bottom: 5px; }
h1 { font-size: 24px; }
.notice { bottom: 192px; padding: 9px 11px; }
.notice p { font-size: 9.5px; }
.scene-tag { min-width: 116px; padding: 6px 8px; }
.scene-tag span { display: none; }
#compare { bottom: 108px; }
.card-title strong { font-size: 9px; }
.card-title b { font-size: 12px; }
.card-meta { font-size: 8px; }
.control-heading { margin-bottom: 7px; }
#status { max-width: 48%; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
scroll-behavior: auto !important;
transition-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" tabindex="0" aria-label="Interactive three-dimensional cutaway comparison of three particle chambers under the same moving-wall load"></canvas>
<div class="vignette" aria-hidden="true"></div>
<header class="masthead">
<div class="eyebrow">Cutaway compression study</div>
<h1>Same wall load.<br>Three very different responses.</h1>
<p>
Open-front toy chambers apply an identical load to three prescribed
<strong>gas-like, liquid-like, and solid-like</strong> states. Observe wall travel,
particle rearrangement, and the volume that remains.
</p>
</header>
<aside class="notice" aria-label="Model disclaimer">
<div class="notice-title">Qualitative illustration — not molecular dynamics</div>
<p>
Particles and response curves are visual stand-ins. No pair potentials, force integration,
thermostat, ensemble averaging, phase prediction, or thermodynamic calculation is performed.
</p>
</aside>
<div id="labels" aria-hidden="true"></div>
<section id="compare" aria-label="Live volume comparison">
<article class="state-card" data-state="gas">
<div class="card-title">
<strong>Gas-like</strong>
<b class="volume-value">100%</b>
</div>
<div class="bar"><i></i></div>
<div class="card-meta">
<span>volume left</span>
<span>wall travel <em class="travel-value">0%</em></span>
</div>
</article>
<article class="state-card" data-state="liquid">
<div class="card-title">
<strong>Liquid-like</strong>
<b class="volume-value">100%</b>
</div>
<div class="bar"><i></i></div>
<div class="card-meta">
<span>volume left</span>
<span>wall travel <em class="travel-value">0%</em></span>
</div>
</article>
<article class="state-card" data-state="solid">
<div class="card-title">
<strong>Solid-like</strong>
<b class="volume-value">100%</b>
</div>
<div class="bar"><i></i></div>
<div class="card-meta">
<span>volume left</span>
<span>wall travel <em class="travel-value">0%</em></span>
</div>
</article>
</section>
<section id="controls" aria-label="Compression controls">
<div class="control-heading">
<div>
<strong>Applied wall load</strong>
<small>One driver; three prescribed displacement responses</small>
</div>
<div id="status" role="status" aria-live="polite">Preparing illustration…</div>
</div>
<div class="control-row">
<button id="playBtn" type="button">Pause</button>
<div class="slider-block">
<label class="slider-label" for="load">
<span>Compression request</span>
<output id="loadValue" for="load">0%</output>
</label>
<input id="load" type="range" min="0" max="100" step="0.1" value="0"
aria-label="Applied wall load percentage">
</div>
<div class="secondary-actions">
<button id="resetBtn" type="button">Reset</button>
<button id="viewBtn" type="button">Reset view</button>
</div>
</div>
</section>
<div class="camera-help">Drag to orbit · scroll to zoom · double-click to reset view</div>
<div id="loading">Loading Three.js scene…</div>
<noscript>This illustration requires JavaScript.</noscript>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const loading = document.getElementById("loading");
const loadInput = document.getElementById("load");
const loadValue = document.getElementById("loadValue");
const playButton = document.getElementById("playBtn");
const resetButton = document.getElementById("resetBtn");
const viewButton = document.getElementById("viewBtn");
const statusElement = document.getElementById("status");
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.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.027);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 120);
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
const orbit = {
theta: 0.075,
phi: 1.17,
radius: 17.4
};
let portraitLayout = false;
let progress = 0;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let lastRenderedLoad = -1;
let lastStatus = "";
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
const stateDefinitions = [
{
key: "gas",
name: "Gas-like",
note: "large prescribed volume response",
accent: 0x61ddff,
css: "#61ddff",
maxTravel: 1.45,
count: 56
},
{
key: "liquid",
name: "Liquid-like",
note: "small prescribed volume response",
accent: 0x71efb1,
css: "#71efb1",
maxTravel: 0.34,
count: 56
},
{
key: "solid",
name: "Solid-like",
note: "nearly fixed chamber volume",
accent: 0xffbd73,
css: "#ffbd73",
maxTravel: 0.055,
count: 56
}
];
const states = [];
const labelsRoot = document.getElementById("labels");
function mulberry32(seed) {
return function random() {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function range(random, min, max) {
return min + (max - min) * random();
}
function gaussian(random) {
return ((random() + random() + random() + random()) - 2) * 0.56;
}
function createBackGrid() {
const points = [];
for (let x = -1.4; x <= 1.401; x += 0.4) {
points.push(x, -1.27, -0.735, x, 1.27, -0.735);
}
for (let y = -1.2; y <= 1.201; y += 0.4) {
points.push(-1.58, y, -0.735, 1.58, y, -0.735);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(points, 3));
const material = new THREE.LineBasicMaterial({
color: 0x4d7892,
transparent: true,
opacity: 0.16,
depthWrite: false
});
return new THREE.LineSegments(geometry, material);
}
function createDashedRectangle(corners, color, opacity = 0.28) {
const geometry = new THREE.BufferGeometry().setFromPoints(corners);
const material = new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.08,
gapSize: 0.055,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
return line;
}
function createParticleSystem(definition, random) {
const geometry = new THREE.IcosahedronGeometry(1, 1);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.3,
metalness: 0.12,
emissive: definition.accent,
emissiveIntensity: 0.2
});
const mesh = new THREE.InstancedMesh(geometry, material, definition.count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
const dummy = new THREE.Object3D();
const baseColor = new THREE.Color(definition.accent);
for (let i = 0; i < definition.count; i++) {
const color = baseColor.clone().offsetHSL(
range(random, -0.018, 0.018),
range(random, -0.05, 0.05),
range(random, -0.11, 0.11)
);
mesh.setColorAt(i, color);
}
mesh.instanceColor.needsUpdate = true;
let data = [];
let current = [];
let solidPairs = [];
let bonds = null;
if (definition.key === "gas") {
data = [];
let attempts = 0;
while (data.length < definition.count && attempts < 12000) {
attempts++;
const p = new THREE.Vector3(
range(random, -1.45, 1.45),
range(random, -1.16, 1.16),
range(random, -0.62, 0.48)
);
if (data.every(q => Math.hypot(q.u * 2.9 - 1.45 - p.x, q.y - p.y, q.z - p.z) > 0.19)) {
data.push({
u: (p.x + 1.45) / 2.9,
y: p.y,
z: p.z,
radius: range(random, 0.048, 0.059)
});
}
}
} else if (definition.key === "liquid") {
data = [];
const centers = [
[-0.9, -0.34, -0.2], [-0.3, 0.25, 0.08], [0.55, -0.28, -0.08],
[0.95, 0.32, 0.2], [-0.15, -0.78, 0.15], [-0.98, 0.65, 0.05],
[0.43, 0.72, -0.25]
];
let attempts = 0;
while (data.length < definition.count && attempts < 18000) {
attempts++;
const center = centers[Math.floor(random() * centers.length)];
const p = new THREE.Vector3(
THREE.MathUtils.clamp(center[0] + gaussian(random) * 0.46, -1.4, 1.4),
THREE.MathUtils.clamp(center[1] + gaussian(random) * 0.38, -1.13, 1.13),
THREE.MathUtils.clamp(center[2] + gaussian(random) * 0.32, -0.58, 0.46)
);
if (data.every(q => Math.hypot(q.x - p.x, q.y - p.y, q.z - p.z) > 0.145)) {
data.push({ x: p.x, y: p.y, z: p.z, radius: range(random, 0.058, 0.069) });
}
}
} else {
data = [];
const columns = 8;
const rows = 7;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
data.push({
x: (column - (columns - 1) / 2) * 0.36,
y: (row - (rows - 1) / 2) * 0.36,
z: ((row + column) % 2 === 0 ? -0.055 : 0.055),
radius: 0.061,
gridColumn: column,
gridRow: row
});
}
}
const bondPositions = new Float32Array(data.length * 3 * 2);
const bondGeometry = new THREE.BufferGeometry();
bondGeometry.setAttribute("position", new THREE.BufferAttribute(bondPositions, 3));
const bondMaterial = new THREE.LineBasicMaterial({
color: definition.accent,
transparent: true,
opacity: 0.48,
depthWrite: false
});
bonds = new THREE.LineSegments(bondGeometry, bondMaterial);
bonds.frustumCulled = false;
const lookup = new Map();
data.forEach((particle, index) => lookup.set(`${particle.gridColumn},${particle.gridRow}`, index));
for (const particle of data) {
const right = lookup.get(`${particle.gridColumn + 1},${particle.gridRow}`);
const above = lookup.get(`${particle.gridColumn},${particle.gridRow + 1}`);
if (right !== undefined) solidPairs.push([lookup.get(`${particle.gridColumn},${particle.gridRow}`), right]);
if (above !== undefined) solidPairs.push([lookup.get(`${particle.gridColumn},${particle.gridRow}`), above]);
}
}
current = data.map(() => new THREE.Vector3());
function update(displacement) {
if (definition.key === "gas") {
const right = 1.47 - displacement;
const left = -1.47;
for (let i = 0; i < data.length; i++) {
const particle = data[i];
current[i].set(
left + particle.u * (right - left),
particle.y,
particle.z
);
}
} else if (definition.key === "liquid") {
const xs = data.map(p => p.x);
const minX = Math.min(...xs);
const maxX = Math.max(...xs);
const ratio = 1 - displacement / 3.2;
for (let i = 0; i < data.length; i++) {
const particle = data[i];
current[i].set(
minX + (particle.x - minX) * ratio,
particle.y,
particle.z
);
}
} else {
const xs = data.map(p => p.x);
const minX = Math.min(...xs);
const maxX = Math.max(...xs);
const ratio = 1 - displacement / 3.2;
for (let i = 0; i < data.length; i++) {
const particle = data[i];
current[i].set(
minX + (particle.x - minX) * ratio,
particle.y,
particle.z
);
}
}
for (let i = 0; i < current.length; i++) {
dummy.position.copy(current[i]);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(data[i].radius);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
}
mesh.instanceMatrix.needsUpdate = true;
if (bonds) {
const positions = bonds.geometry.attributes.position.array;
let offset = 0;
for (const [a, b] of solidPairs) {
positions[offset++] = current[a].x;
positions[offset++] = current[a].y;
positions[offset++] = current[a].z;
positions[offset++] = current[b].x;
positions[offset++] = current[b].y;
positions[offset++] = current[b].z;
}
bonds.geometry.attributes.position.needsUpdate = true;
bonds.geometry.computeBoundingSphere();
}
}
return { mesh, data, current, update, bonds };
}
function createChamber(definition, index) {
const random = mulberry32(7193 + index * 101);
const group = new THREE.Group();
scene.add(group);
const backMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1e31,
roughness: 0.76,
metalness: 0.12
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x29475b,
roughness: 0.38,
metalness: 0.58
});
const glassMaterial = new THREE.MeshStandardMaterial({
color: definition.accent,
roughness: 0.24,
metalness: 0.02,
transparent: true,
opacity: 0.055,
depthWrite: false,
side: THREE.DoubleSide
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: definition.accent,
emissive: definition.accent,
emissiveIntensity: 0.55,
roughness: 0.32,
metalness: 0.3
});
const back = new THREE.Mesh(new THREE.BoxGeometry(3.62, 2.96, 0.15), backMaterial);
back.position.z = -0.82;
back.receiveShadow = true;
group.add(back);
group.add(createBackGrid());
const topRail = new THREE.Mesh(new THREE.BoxGeometry(3.58, 0.12, 1.54), railMaterial);
topRail.position.set(0, 1.43, -0.04);
topRail.castShadow = true;
group.add(topRail);
const bottomRail = topRail.clone();
bottomRail.position.y = -1.43;
group.add(bottomRail);
const leftWall = new THREE.Mesh(new THREE.BoxGeometry(0.055, 2.76, 1.48), glassMaterial);
leftWall.position.set(-1.70, 0, -0.02);
group.add(leftWall);
const rightWall = leftWall.clone();
rightWall.position.x = 1.70;
group.add(rightWall);
const base = new THREE.Mesh(
new THREE.BoxGeometry(3.86, 0.16, 2.02),
new THREE.MeshStandardMaterial({ color: 0x10263a, roughness: 0.58, metalness: 0.42 })
);
base.position.set(0, -1.62, -0.04);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const lightStrip = new THREE.Mesh(new THREE.BoxGeometry(3.34, 0.035, 0.055), accentMaterial);
lightStrip.position.set(0, -1.515, 0.86);
group.add(lightStrip);
const nominalEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(3.2, 2.6, 1.42)),
new THREE.LineBasicMaterial({
color: 0xb8d5e6,
transparent: true,
opacity: 0.13,
depthWrite: false
})
);
nominalEdges.position.z = -0.02;
group.add(nominalEdges);
const ghostRightFace = createDashedRectangle([
new THREE.Vector3(1.60, -1.30, -0.72),
new THREE.Vector3(1.60, 1.30, -0.72),
new THREE.Vector3(1.60, 1.30, 0.67),
new THREE.Vector3(1.60, -1.30, 0.67),
new THREE.Vector3(1.60, -1.30, -0.72)
], definition.accent, 0.28);
group.add(ghostRightFace);
const piston = new THREE.Group();
const pistonBody = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 2.57, 1.38),
new THREE.MeshStandardMaterial({ color: 0x42586a, roughness: 0.31, metalness: 0.72 })
);
pistonBody.position.x = 0.025;
pistonBody.castShadow = true;
piston.add(pistonBody);
const pistonFace = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 2.50, 1.33),
new THREE.MeshStandardMaterial({
color: 0xff7165,
emissive: 0xff5148,
emissiveIntensity: 0.38,
roughness: 0.3,
metalness: 0.36
})
);
pistonFace.position.x = -0.078;
pistonFace.castShadow = true;
piston.add(pistonFace);
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 1.82, 18),
new THREE.MeshStandardMaterial({ color: 0x9bb0be, roughness: 0.24, metalness: 0.85 })
);
rod.rotation.z = Math.PI / 2;
rod.position.set(1.02, 0, -0.06);
rod.castShadow = true;
piston.add(rod);
const loadCap = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.47, 0.55),
new THREE.MeshStandardMaterial({
color: 0xff7165,
emissive: 0xff5148,
emissiveIntensity: 0.23,
roughness: 0.42,
metalness: 0.42
})
);
loadCap.position.set(1.96, 0, -0.06);
loadCap.castShadow = true;
piston.add(loadCap);
group.add(piston);
const arrows = [];
for (const y of [0.87, -0.82]) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(),
0.8,
0xff7165,
0.18,
0.12
);
arrow.position.set(2.55, y, 0.82);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.9;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.95;
group.add(arrow);
arrows.push(arrow);
}
const particles = createParticleSystem(definition, random);
group.add(particles.mesh);
if (particles.bonds) group.add(particles.bonds);
const accentLight = new THREE.PointLight(definition.accent, 14, 5.2, 2);
accentLight.position.set(0, 0.25, 2.0);
group.add(accentLight);
const tag = document.createElement("div");
tag.className = "scene-tag";
tag.style.setProperty("--accent", definition.css);
tag.style.setProperty("--accent-border", `${definition.css}45`);
tag.innerHTML = `<strong>${definition.name}</strong><span>${definition.note}</span>`;
labelsRoot.appendChild(tag);
return {
definition,
group,
piston,
arrows,
particles,
tag,
anchor: new THREE.Vector3(0, 1.88, 0.15),
card: document.querySelector(`.state-card[data-state="${definition.key}"]`)
};
}
stateDefinitions.forEach((definition, index) => {
states.push(createChamber(definition, index));
});
const ambient = new THREE.HemisphereLight(0x8fc8ea, 0x08101a, 1.25);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xcceaff, 2.7);
keyLight.position.set(-5, 9, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3d8dff, 1.7);
rimLight.position.set(8, 4, -7);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(44, 30),
new THREE.MeshStandardMaterial({
color: 0x071523,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.72;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(42, 42, 0x23445b, 0x10293b);
grid.position.y = -1.705;
grid.material.transparent = true;
grid.material.opacity = 0.34;
grid.material.depthWrite = false;
scene.add(grid);
function updateCamera() {
const fit = portraitLayout ? 1 : Math.max(1, 1.16 / camera.aspect);
const radius = orbit.radius * fit;
camera.position.set(
cameraTarget.x + radius * Math.sin(orbit.phi) * Math.sin(orbit.theta),
cameraTarget.y + radius * Math.cos(orbit.phi),
cameraTarget.z + radius * Math.sin(orbit.phi) * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetView() {
orbit.theta = 0.075;
orbit.phi = 1.17;
orbit.radius = portraitLayout ? 13.7 : 17.4;
updateCamera();
}
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();
const nextPortrait = camera.aspect < 0.82;
if (nextPortrait !== portraitLayout) {
portraitLayout = nextPortrait;
resetView();
}
if (portraitLayout) {
const positions = [2.28, 0, -2.28];
states.forEach((state, index) => {
state.group.position.set(0, positions[index], 0);
state.group.scale.setScalar(0.73);
});
cameraTarget.set(0, 0.05, 0);
} else {
const positions = [-4.55, 0, 4.55];
states.forEach((state, index) => {
state.group.position.set(positions[index], 0, 0);
state.group.scale.setScalar(1);
});
cameraTarget.set(0, 0.05, 0);
}
updateCamera();
}
window.addEventListener("resize", resize);
function updatePlayButton() {
if (progress >= 0.9999) {
playButton.textContent = "Replay";
} else if (playing) {
playButton.textContent = "Pause";
} else {
playButton.textContent = progress <= 0.0001 ? "Apply load" : "Resume";
}
}
function updateStatus() {
let text;
if (progress >= 0.9999) {
text = "Full load reached · compare the settled volumes";
} else if (playing) {
text = "Compressing · identical load on all three walls";
} else if (progress <= 0.0001) {
text = "Ready · apply the moving-wall load";
} else {
text = `Paused at ${Math.round(progress * 100)}% applied load`;
}
if (text !== lastStatus) {
statusElement.textContent = text;
lastStatus = text;
}
}
function updateScene(load) {
for (const state of states) {
const displacement = state.definition.maxTravel * load;
const faceX = 1.58 - displacement;
state.piston.position.x = faceX;
for (const arrow of state.arrows) {
arrow.position.x = faceX + 1.05;
arrow.setLength(0.50 + 0.62 * load, 0.18, 0.12);
arrow.visible = load > 0.008;
}
state.particles.update(displacement);
const remaining = Math.max(0, (3.2 - displacement) / 3.2 * 100);
const travel = displacement / 3.2 * 100;
state.card.querySelector(".volume-value").textContent = `${Math.round(remaining)}%`;
state.card.querySelector(".travel-value").textContent = `${Math.round(travel)}%`;
state.card.querySelector(".bar i").style.width = `${remaining}%`;
}
}
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const state of states) {
const world = state.group.localToWorld(state.anchor.clone());
const projected = world.project(camera);
const visible = projected.z > -1 && projected.z < 1;
state.tag.style.display = visible ? "block" : "none";
state.tag.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
state.tag.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
playButton.addEventListener("click", () => {
if (progress >= 0.9999) {
progress = 0;
playing = true;
} else {
playing = !playing;
}
updatePlayButton();
updateStatus();
});
resetButton.addEventListener("click", () => {
progress = 0;
playing = false;
loadInput.value = "0";
updatePlayButton();
updateStatus();
});
viewButton.addEventListener("click", resetView);
loadInput.addEventListener("input", () => {
progress = Number(loadInput.value) / 100;
playing = false;
updatePlayButton();
updateStatus();
});
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - deltaY * 0.005, 0.58, 1.42);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
portraitLayout ? 10.5 : 12.0,
portraitLayout ? 18 : 25
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
});
reducedMotion.addEventListener?.("change", event => {
if (event.matches) playing = false;
updatePlayButton();
});
resize();
updatePlayButton();
updateStatus();
updateScene(progress);
loading.classList.add("hidden");
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
progress = Math.min(1, progress + delta / 7.5);
if (progress >= 1) {
playing = false;
updatePlayButton();
}
}
const percent = progress * 100;
loadInput.value = percent.toFixed(1);
loadInput.style.setProperty("--fill", `${percent}%`);
loadValue.textContent = `${Math.round(percent)}%`;
if (Math.abs(progress - lastRenderedLoad) > 0.00005) {
updateScene(progress);
lastRenderedLoad = progress;
}
updateStatus();
updateCamera();
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```