Prompt specification
Build or customize the Three.js 3D scene "Solid liquid and gas particle chambers" 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: Solid liquid and gas particle chambers
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/solid-liquid-and-gas-particle-chambers-x1965
Scene Description Prompt:
Solid liquid and gas particle chambers. Build three equal transparent boxes containing schematic particles with ordered vibration, close mobile arrangement, and widely spaced motion; label particle size and spacing exaggerated. Please build this with Three.js from cdnjs in one self-contained HTML file, using a 3D cutaway.
Working Three.js Starter (solid-liquid-and-gas-particle-chambers-x1965):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matter in Motion — Solid, Liquid and Gas</title>
<style>
:root {
color-scheme: dark;
--bg: #050816;
--text: #eef5ff;
--muted: #95a8c4;
--solid: #43d7ff;
--liquid: #aa8cff;
--gas: #ffc857;
--glass: rgba(10, 18, 37, 0.72);
--border: rgba(154, 190, 235, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 28%, #142246 0%, #090e20 34%, #050816 72%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
z-index: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#viewport canvas.dragging {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(
to bottom,
rgba(3, 7, 18, 0.32),
transparent 26%,
transparent 72%,
rgba(3, 7, 18, 0.5)
),
radial-gradient(
ellipse at center,
transparent 48%,
rgba(2, 5, 14, 0.38) 100%
);
}
header {
position: fixed;
top: 28px;
left: 32px;
z-index: 5;
max-width: 600px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 10px;
color: #7edfff;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(31px, 4.2vw, 58px);
font-weight: 720;
line-height: 0.98;
letter-spacing: -0.045em;
background: linear-gradient(115deg, #ffffff 5%, #a9c9ee 92%);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
.subtitle {
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.scale-note {
position: fixed;
top: 31px;
right: 30px;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 13, 29, 0.62);
color: #b9c9df;
font-size: 10px;
letter-spacing: 0.04em;
backdrop-filter: blur(14px);
pointer-events: none;
}
.scale-note::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #7edfff;
box-shadow: 0 0 12px #43d7ff;
content: "";
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.phase-label {
position: absolute;
width: 218px;
padding: 12px 14px 13px;
transform: translate(-50%, -100%);
border: 1px solid color-mix(in srgb, var(--accent) 27%, transparent);
border-radius: 12px;
background:
linear-gradient(
135deg,
color-mix(in srgb, var(--accent) 8%, transparent),
rgba(7, 12, 27, 0.78)
);
box-shadow:
0 14px 40px rgba(0, 0, 0, 0.25),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
opacity: 0;
transition: opacity 160ms ease;
backdrop-filter: blur(14px);
}
.phase-label::after {
position: absolute;
bottom: -21px;
left: 50%;
width: 1px;
height: 21px;
background: linear-gradient(var(--accent), transparent);
content: "";
}
.phase-label span {
display: block;
margin-bottom: 4px;
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.phase-label strong {
display: block;
color: #f5f9ff;
font-size: 15px;
font-weight: 680;
letter-spacing: -0.01em;
}
.phase-label small {
display: block;
margin-top: 4px;
color: #91a3bd;
font-size: 10px;
line-height: 1.45;
}
.legend {
position: fixed;
bottom: 25px;
left: 28px;
z-index: 5;
display: flex;
gap: 8px;
padding: 7px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--glass);
box-shadow: 0 15px 50px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(16px);
pointer-events: none;
}
.legend-item {
display: grid;
grid-template-columns: 8px auto;
column-gap: 8px;
align-items: center;
min-width: 154px;
padding: 7px 10px;
}
.legend-item + .legend-item {
border-left: 1px solid rgba(157, 190, 231, 0.11);
}
.legend-dot {
grid-row: 1 / span 2;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
}
.legend-item b {
color: #eaf2ff;
font-size: 11px;
font-weight: 680;
}
.legend-item small {
margin-top: 2px;
color: #8295b1;
font-size: 9px;
}
.controls {
position: fixed;
right: 28px;
bottom: 25px;
z-index: 6;
display: flex;
gap: 8px;
}
button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 7px;
min-height: 40px;
padding: 0 13px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(10, 18, 37, 0.82);
color: #cbd9ec;
font: inherit;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
cursor: pointer;
backdrop-filter: blur(14px);
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(126, 223, 255, 0.4);
background: rgba(17, 31, 57, 0.9);
color: white;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 2px solid #43d7ff;
outline-offset: 3px;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #050816;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
display: grid;
place-items: center;
gap: 13px;
color: #91a8c8;
font-size: 10px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.loader-ring {
width: 36px;
height: 36px;
border: 1px solid rgba(126, 223, 255, 0.18);
border-top-color: #43d7ff;
border-radius: 50%;
animation: spin 850ms linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 900px) {
header {
top: 22px;
left: 22px;
}
.scale-note {
top: 22px;
right: 20px;
}
.legend {
left: 18px;
bottom: 18px;
}
.legend-item {
min-width: auto;
}
.controls {
right: 18px;
bottom: 18px;
}
}
@media (max-width: 759px) {
header {
top: 18px;
left: 18px;
max-width: calc(100vw - 36px);
}
h1 {
font-size: 34px;
}
.subtitle {
max-width: 280px;
font-size: 11px;
}
.scale-note {
top: 116px;
right: 16px;
padding: 8px 10px;
font-size: 8px;
}
#labels {
display: none;
}
.legend {
right: 12px;
bottom: 12px;
left: 12px;
display: grid;
grid-template-columns: repeat(3, 1fr);
padding: 5px;
}
.legend-item {
grid-template-columns: 6px 1fr;
gap: 6px;
padding: 7px 5px;
}
.legend-item + .legend-item {
border-left: 1px solid rgba(157, 190, 231, 0.1);
}
.legend-dot {
width: 5px;
height: 5px;
}
.legend-item b {
font-size: 9px;
}
.legend-item small {
display: none;
}
.controls {
top: 112px;
right: 14px;
bottom: auto;
flex-direction: column;
}
button {
min-height: 34px;
padding: 0 10px;
font-size: 9px;
}
}
@media (max-width: 420px) {
.scale-note {
max-width: 160px;
line-height: 1.4;
}
}
</style>
</head>
<body>
<main id="viewport"></main>
<div id="vignette"></div>
<header>
<div class="eyebrow">Particle models · 3D cutaway</div>
<h1>Three states<br>of matter</h1>
<p class="subtitle">
The same microscopic rules produce three very different kinds of motion.
Drag to orbit, scroll or pinch to zoom.
</p>
</header>
<div class="scale-note">Particle size & spacing exaggerated</div>
<div id="labels"></div>
<footer class="legend" aria-label="State of matter legend">
<div class="legend-item" style="--accent: var(--solid)">
<span class="legend-dot"></span>
<b>Solid</b>
<small>Ordered vibration</small>
</div>
<div class="legend-item" style="--accent: var(--liquid)">
<span class="legend-dot"></span>
<b>Liquid</b>
<small>Close mobile arrangement</small>
</div>
<div class="legend-item" style="--accent: var(--gas)">
<span class="legend-dot"></span>
<b>Gas</b>
<small>Widely spaced motion</small>
</div>
</footer>
<nav class="controls" aria-label="Animation controls">
<button id="motion" type="button" aria-pressed="false">Ⅱ Pause motion</button>
<button id="reset" type="button">↺ Reset view</button>
</nav>
<div id="loading">
<div class="loader">
<div class="loader-ring"></div>
<span>Building particle chambers</span>
</div>
</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 viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
} catch (error) {
loading.innerHTML = `
<div class="loader">
WebGL could not be initialized in this browser.
</div>
`;
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D model of solid, liquid, and gas particle chambers"
);
renderer.domElement.tabIndex = 0;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x050816, 0.022);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const clock = new THREE.Clock();
const colors = {
solid: 0x43d7ff,
liquid: 0xaa8cff,
gas: 0xffc857
};
const cssColors = {
solid: "#43d7ff",
liquid: "#aa8cff",
gas: "#ffc857"
};
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb6e5ff, 0x090b18, 1.35));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(-4, 8, 7);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6ea8ff, 1.4);
rimLight.position.set(6, 3, -6);
scene.add(rimLight);
[
[colors.solid, -3.55],
[colors.liquid, 0],
[colors.gas, 3.55]
].forEach(([color, x]) => {
const light = new THREE.PointLight(color, 7, 6, 2);
light.position.set(x, 2.4, 1.5);
scene.add(light);
});
// -------------------------------------------------------------------------
// Procedural helpers
// -------------------------------------------------------------------------
function seededRandom(seed) {
return function random() {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(20250308);
function createGlowTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(
64, 64, 0,
64, 64, 64
);
gradient.addColorStop(0, "rgba(255,255,255,0.7)");
gradient.addColorStop(0.28, "rgba(255,255,255,0.25)");
gradient.addColorStop(1, "rgba(255,255,255,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const glowTexture = createGlowTexture();
// -------------------------------------------------------------------------
// Floor and ambient dust
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshBasicMaterial({
color: 0x050816,
transparent: true,
opacity: 0.72,
depthWrite: false
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.66;
scene.add(floor);
const floorGrid = new THREE.GridHelper(
30,
30,
0x385274,
0x1c2942
);
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.16;
floorGrid.material.depthWrite = false;
floorGrid.position.y = -1.65;
scene.add(floorGrid);
const dustPositions = [];
for (let i = 0; i < 180; i++) {
dustPositions.push(
(random() - 0.5) * 28,
(random() - 0.25) * 12,
-2 - random() * 11
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8eb9e8,
size: 0.022,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
scene.add(dust);
// -------------------------------------------------------------------------
// Chamber construction
// -------------------------------------------------------------------------
const CHAMBER_SIZE = 2.9;
const HALF = CHAMBER_SIZE / 2;
const coreGeometry = new THREE.SphereGeometry(1, 18, 14);
const haloGeometry = new THREE.SphereGeometry(1, 12, 9);
const chambers = [];
function createChamber(type, accent) {
const group = new THREE.Group();
const panelMaterial = new THREE.MeshBasicMaterial({
color: 0x071426,
transparent: true,
opacity: 0.34,
side: THREE.DoubleSide,
depthWrite: false
});
const sideMaterial = new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.035,
side: THREE.DoubleSide,
depthWrite: false
});
// Back wall.
const back = new THREE.Mesh(
new THREE.PlaneGeometry(CHAMBER_SIZE, CHAMBER_SIZE),
panelMaterial
);
back.position.z = -HALF + 0.012;
group.add(back);
// Side walls remain translucent.
const left = new THREE.Mesh(
new THREE.PlaneGeometry(CHAMBER_SIZE, CHAMBER_SIZE),
sideMaterial
);
left.position.x = -HALF;
left.rotation.y = Math.PI / 2;
group.add(left);
const right = left.clone();
right.position.x = HALF;
group.add(right);
// Bottom wall; front and top are omitted for the cutaway view.
const bottom = new THREE.Mesh(
new THREE.PlaneGeometry(CHAMBER_SIZE, CHAMBER_SIZE),
sideMaterial
);
bottom.rotation.x = -Math.PI / 2;
bottom.position.y = -HALF;
group.add(bottom);
// Full wireframe preserves the equal box shape.
const boxGeometry = new THREE.BoxGeometry(
CHAMBER_SIZE,
CHAMBER_SIZE,
CHAMBER_SIZE
);
const edgeMaterial = new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.48
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(boxGeometry),
edgeMaterial
);
boxGeometry.dispose();
group.add(edges);
// Grid on the back wall.
const backGrid = new THREE.GridHelper(
CHAMBER_SIZE,
6,
accent,
accent
);
backGrid.rotation.x = Math.PI / 2;
backGrid.position.z = -HALF + 0.025;
backGrid.material.transparent = true;
backGrid.material.opacity = 0.11;
backGrid.material.depthWrite = false;
group.add(backGrid);
// Grid on the floor of the chamber.
const bottomGrid = new THREE.GridHelper(
CHAMBER_SIZE,
6,
accent,
accent
);
bottomGrid.position.y = -HALF + 0.012;
bottomGrid.material.transparent = true;
bottomGrid.material.opacity = 0.08;
bottomGrid.material.depthWrite = false;
group.add(bottomGrid);
// Soft colored pool beneath the chamber.
const glow = new THREE.Mesh(
new THREE.PlaneGeometry(4.1, 4.1),
new THREE.MeshBasicMaterial({
map: glowTexture,
color: accent,
transparent: true,
opacity: 0.28,
depthWrite: false,
blending: THREE.AdditiveBlending,
toneMapped: false
})
);
glow.rotation.x = -Math.PI / 2;
glow.position.y = -HALF - 0.025;
group.add(glow);
const coreMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(accent).lerp(
new THREE.Color(0xffffff),
0.18
),
emissive: accent,
emissiveIntensity: 0.38,
roughness: 0.24,
metalness: 0.12
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.075,
depthWrite: false,
blending: THREE.AdditiveBlending,
toneMapped: false
});
scene.add(group);
const chamber = {
type,
accent,
group,
coreMaterial,
haloMaterial,
particles: []
};
chambers.push(chamber);
return chamber;
}
function addParticle(chamber, radius, position) {
const object = new THREE.Group();
const halo = new THREE.Mesh(haloGeometry, chamber.haloMaterial);
halo.scale.setScalar(radius * 1.8);
const core = new THREE.Mesh(coreGeometry, chamber.coreMaterial);
core.scale.setScalar(radius);
object.add(halo, core);
object.position.copy(position);
chamber.group.add(object);
return object;
}
const solidChamber = createChamber("solid", colors.solid);
const liquidChamber = createChamber("liquid", colors.liquid);
const gasChamber = createChamber("gas", colors.gas);
// -------------------------------------------------------------------------
// Solid: a synchronized, repeating lattice
// -------------------------------------------------------------------------
const solidParticles = [];
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
for (let z = 0; z < 4; z++) {
const position = new THREE.Vector3(
(x - 1.5) * 0.62,
(y - 1.5) * 0.62,
(z - 1.5) * 0.62
);
const object = addParticle(solidChamber, 0.145, position);
solidParticles.push({
object,
base: position.clone(),
phase:
position.x * 1.2 +
position.y * 0.8 +
position.z * 0.6
});
}
}
}
// -------------------------------------------------------------------------
// Position sampling for liquid and gas
// -------------------------------------------------------------------------
function samplePositions(count, minimumSpacing, boundary) {
const positions = [];
for (let i = 0; i < count; i++) {
let accepted = false;
for (let attempt = 0; attempt < 5000 && !accepted; attempt++) {
const candidate = new THREE.Vector3(
(random() * 2 - 1) * boundary,
(random() * 2 - 1) * boundary,
(random() * 2 - 1) * boundary
);
accepted = positions.every((position) => {
return position.distanceToSquared(candidate) >
minimumSpacing * minimumSpacing;
});
if (accepted) {
positions.push(candidate);
}
}
}
return positions;
}
// -------------------------------------------------------------------------
// Liquid: close particles with mobile, jostling motion
// -------------------------------------------------------------------------
const liquidParticles = [];
const liquidPositions = samplePositions(34, 0.43, 1.18);
for (const position of liquidPositions) {
const object = addParticle(liquidChamber, 0.19, position);
const velocity = new THREE.Vector3(
random() - 0.5,
random() - 0.5,
random() - 0.5
)
.normalize()
.multiplyScalar(0.15 + random() * 0.25);
liquidParticles.push({
object,
position: position.clone(),
velocity,
seed: random() * Math.PI * 2
});
}
// -------------------------------------------------------------------------
// Gas: widely separated particles with long free paths
// -------------------------------------------------------------------------
const gasParticles = [];
const gasPositions = samplePositions(14, 1.02, 1.18);
const gasTrailMaterial = new THREE.LineBasicMaterial({
color: colors.gas,
transparent: true,
opacity: 0.28,
depthWrite: false,
blending: THREE.AdditiveBlending
});
for (const position of gasPositions) {
const object = addParticle(gasChamber, 0.235, position);
const velocity = new THREE.Vector3(
random() - 0.5,
random() - 0.5,
random() - 0.5
)
.normalize()
.multiplyScalar(0.38 + random() * 0.34);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
position.x,
position.y,
position.z,
position.x,
position.y,
position.z
],
3
)
);
const trail = new THREE.Line(trailGeometry, gasTrailMaterial);
gasChamber.group.add(trail);
gasParticles.push({
object,
position: position.clone(),
velocity,
trail
});
}
// -------------------------------------------------------------------------
// Labels projected from 3D positions
// -------------------------------------------------------------------------
const labelsElement = document.getElementById("labels");
const labelDefinitions = [
{
chamber: solidChamber,
number: "01",
title: "Solid",
subtitle: "Ordered vibration",
detail: "Fixed neighbors · synchronized motion",
color: cssColors.solid
},
{
chamber: liquidChamber,
number: "02",
title: "Liquid",
subtitle: "Close mobile arrangement",
detail: "Neighbors continually rearrange",
color: cssColors.liquid
},
{
chamber: gasChamber,
number: "03",
title: "Gas",
subtitle: "Widely spaced motion",
detail: "Long, free paths between particles",
color: cssColors.gas
}
];
const labels = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = "phase-label";
element.style.setProperty("--accent", definition.color);
element.innerHTML = `
<span>${definition.number} / ${definition.title}</span>
<strong>${definition.subtitle}</strong>
<small>${definition.detail}</small>
`;
labelsElement.appendChild(element);
return element;
});
// -------------------------------------------------------------------------
// Animation systems
// -------------------------------------------------------------------------
function updateSolid(time) {
for (const particle of solidParticles) {
const p = particle.phase;
particle.object.position.set(
particle.base.x + Math.sin(time * 2.5 + p) * 0.025,
particle.base.y + Math.sin(time * 3.1 + p * 0.9) * 0.045,
particle.base.z + Math.cos(time * 2.7 + p) * 0.025
);
}
}
function constrainParticle(particle, radius) {
const limit = HALF - radius - 0.045;
if (particle.position.x < -limit) {
particle.position.x = -limit;
particle.velocity.x = Math.abs(particle.velocity.x);
} else if (particle.position.x > limit) {
particle.position.x = limit;
particle.velocity.x = -Math.abs(particle.velocity.x);
}
if (particle.position.y < -limit) {
particle.position.y = -limit;
particle.velocity.y = Math.abs(particle.velocity.y);
} else if (particle.position.y > limit) {
particle.position.y = limit;
particle.velocity.y = -Math.abs(particle.velocity.y);
}
if (particle.position.z < -limit) {
particle.position.z = -limit;
particle.velocity.z = Math.abs(particle.velocity.z);
} else if (particle.position.z > limit) {
particle.position.z = limit;
particle.velocity.z = -Math.abs(particle.velocity.z);
}
}
function updateLiquid(dt, time) {
const damping = Math.pow(0.992, dt * 60);
for (const particle of liquidParticles) {
particle.velocity.x +=
(
Math.sin(particle.position.y * 2.2 + time * 0.8 + particle.seed) +
Math.cos(particle.position.z * 1.7 - time * 0.5)
) *
0.1 *
dt;
particle.velocity.y +=
(
Math.sin(particle.position.z * 2.0 + time * 0.7 + particle.seed) -
0.15
) *
0.07 *
dt;
particle.velocity.z +=
Math.cos(
particle.position.x * 2.1 + time * 0.6 + particle.seed
) *
0.1 *
dt;
particle.velocity.multiplyScalar(damping);
particle.position.addScaledVector(particle.velocity, dt);
constrainParticle(particle, 0.19);
}
// Equal-mass collision response keeps the particles close but distinct.
const minimumDistance = 0.19 * 2 * 0.98;
const minimumDistanceSquared = minimumDistance * minimumDistance;
for (let i = 0; i < liquidParticles.length; i++) {
const a = liquidParticles[i];
for (let j = i + 1; j < liquidParticles.length; j++) {
const b = liquidParticles[j];
const dx = b.position.x - a.position.x;
const dy = b.position.y - a.position.y;
const dz = b.position.z - a.position.z;
const distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared >= minimumDistanceSquared) {
continue;
}
let distance = Math.sqrt(distanceSquared);
if (distance < 0.00001) {
distance = 0.00001;
}
const nx = dx / distance;
const ny = dy / distance;
const nz = dz / distance;
const overlap = (minimumDistance - distance) * 0.51;
a.position.x -= nx * overlap;
a.position.y -= ny * overlap;
a.position.z -= nz * overlap;
b.position.x += nx * overlap;
b.position.y += ny * overlap;
b.position.z += nz * overlap;
const relativeVelocityX = b.velocity.x - a.velocity.x;
const relativeVelocityY = b.velocity.y - a.velocity.y;
const relativeVelocityZ = b.velocity.z - a.velocity.z;
const normalVelocity =
relativeVelocityX * nx +
relativeVelocityY * ny +
relativeVelocityZ * nz;
if (normalVelocity < 0) {
const impulse = -((1 + 0.72) * normalVelocity) / 2;
a.velocity.x -= nx * impulse;
a.velocity.y -= ny * impulse;
a.velocity.z -= nz * impulse;
b.velocity.x += nx * impulse;
b.velocity.y += ny * impulse;
b.velocity.z += nz * impulse;
}
}
}
for (const particle of liquidParticles) {
constrainParticle(particle, 0.19);
particle.object.position.copy(particle.position);
}
}
function updateGas(dt, time) {
for (const particle of gasParticles) {
particle.velocity.x +=
Math.sin(time * 0.4 + particle.position.y) * 0.025 * dt;
particle.velocity.y +=
Math.cos(time * 0.35 + particle.position.z) * 0.025 * dt;
particle.velocity.z +=
Math.sin(time * 0.3 + particle.position.x) * 0.02 * dt;
particle.position.addScaledVector(particle.velocity, dt);
constrainParticle(particle, 0.235);
}
// Resolve occasional close encounters without turning gas into a fluid.
const minimumDistance = 0.49;
const minimumDistanceSquared = minimumDistance * minimumDistance;
for (let i = 0; i < gasParticles.length; i++) {
const a = gasParticles[i];
for (let j = i + 1; j < gasParticles.length; j++) {
const b = gasParticles[j];
const dx = b.position.x - a.position.x;
const dy = b.position.y - a.position.y;
const dz = b.position.z - a.position.z;
const distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared >= minimumDistanceSquared) {
continue;
}
let distance = Math.sqrt(distanceSquared);
distance = Math.max(distance, 0.00001);
const nx = dx / distance;
const ny = dy / distance;
const nz = dz / distance;
const overlap = (minimumDistance - distance) * 0.5;
a.position.x -= nx * overlap;
a.position.y -= ny * overlap;
a.position.z -= nz * overlap;
b.position.x += nx * overlap;
b.position.y += ny * overlap;
b.position.z += nz * overlap;
const relativeVelocityX = b.velocity.x - a.velocity.x;
const relativeVelocityY = b.velocity.y - a.velocity.y;
const relativeVelocityZ = b.velocity.z - a.velocity.z;
const normalVelocity =
relativeVelocityX * nx +
relativeVelocityY * ny +
relativeVelocityZ * nz;
if (normalVelocity < 0) {
const impulse = -normalVelocity;
a.velocity.x -= nx * impulse;
a.velocity.y -= ny * impulse;
a.velocity.z -= nz * impulse;
b.velocity.x += nx * impulse;
b.velocity.y += ny * impulse;
b.velocity.z += nz * impulse;
}
}
}
for (const particle of gasParticles) {
constrainParticle(particle, 0.235);
particle.object.position.copy(particle.position);
const speed = particle.velocity.length();
const direction = particle.velocity.clone();
if (speed > 0.001) {
direction.divideScalar(speed);
}
const trailLength = 0.25 + speed * 0.7;
const positions =
particle.trail.geometry.attributes.position.array;
positions[0] = particle.position.x;
positions[1] = particle.position.y;
positions[2] = particle.position.z;
positions[3] = particle.position.x - direction.x * trailLength;
positions[4] = particle.position.y - direction.y * trailLength;
positions[5] = particle.position.z - direction.z * trailLength;
particle.trail.geometry.attributes.position.needsUpdate = true;
}
}
// -------------------------------------------------------------------------
// Custom camera controls: no examples or addons
// -------------------------------------------------------------------------
let compactLayout = false;
let baseRadius = 10.4;
let zoom = 1;
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
let yaw = 0.08;
let pitch = 0.13;
const pointers = new Map();
let lastPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.classList.add("dragging");
if (pointers.size === 2) {
const [a, b] = Array.from(pointers.values());
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) {
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
yaw -= dx * 0.006;
pitch += -dy * 0.0045;
pitch = THREE.MathUtils.clamp(pitch, -0.32, 0.72);
} else if (pointers.size === 2) {
const [a, b] = Array.from(pointers.values());
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance > 0 && distance > 0) {
zoom *= lastPinchDistance / distance;
zoom = THREE.MathUtils.clamp(zoom, 0.72, 1.65);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoom *= Math.exp(event.deltaY * 0.001);
zoom = THREE.MathUtils.clamp(zoom, 0.72, 1.65);
},
{ passive: false }
);
function resetView() {
yaw = 0.08;
pitch = 0.13;
zoom = 1;
}
document.getElementById("reset").addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Responsive layout
// -------------------------------------------------------------------------
function updateLayout() {
compactLayout = window.innerWidth < 760;
if (compactLayout) {
// Stack the chambers vertically on narrow screens.
solidChamber.group.position.set(0, 2.65, 0);
liquidChamber.group.position.set(0, 0, 0);
gasChamber.group.position.set(0, -2.65, 0);
floor.position.y = -4.19;
floorGrid.position.y = -4.18;
cameraTarget.set(0, 0.05, 0);
baseRadius = 14.8;
labelsElement.style.display = "none";
} else {
solidChamber.group.position.set(-3.55, 0, 0);
liquidChamber.group.position.set(0, 0, 0);
gasChamber.group.position.set(3.55, 0, 0);
floor.position.y = -1.66;
floorGrid.position.y = -1.65;
cameraTarget.set(0, 0.05, 0);
baseRadius = 10.4;
labelsElement.style.display = "block";
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
updateLayout();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Playback controls
// -------------------------------------------------------------------------
let paused = false;
let elapsed = 0;
const motionButton = document.getElementById("motion");
function toggleMotion() {
paused = !paused;
motionButton.setAttribute("aria-pressed", String(paused));
motionButton.textContent = paused ? "▶ Resume motion" : "Ⅱ Pause motion";
}
motionButton.addEventListener("click", toggleMotion);
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
document.activeElement?.tagName !== "BUTTON"
) {
event.preventDefault();
toggleMotion();
}
if (event.key.toLowerCase() === "r") {
resetView();
}
});
// -------------------------------------------------------------------------
// Rendering
// -------------------------------------------------------------------------
const projectedLabel = new THREE.Vector3();
function updateCamera() {
const radius = baseRadius * zoom;
const cosPitch = Math.cos(pitch);
const sinPitch = Math.sin(pitch);
camera.position.set(
cameraTarget.x + Math.sin(yaw) * cosPitch * radius,
cameraTarget.y + sinPitch * radius,
cameraTarget.z + Math.cos(yaw) * cosPitch * radius
);
camera.lookAt(cameraTarget);
}
function updateLabels() {
if (compactLayout) {
return;
}
for (let i = 0; i < labels.length; i++) {
const chamber = labelDefinitions[i].chamber;
projectedLabel.set(
chamber.group.position.x,
chamber.group.position.y + HALF + 0.42,
chamber.group.position.z
);
projectedLabel.project(camera);
const visible =
projectedLabel.z >= -1 &&
projectedLabel.z <= 1 &&
Math.abs(projectedLabel.x) < 1.15 &&
Math.abs(projectedLabel.y) < 1.15;
labels[i].style.left =
`${(projectedLabel.x * 0.5 + 0.5) * window.innerWidth}px`;
labels[i].style.top =
`${(-projectedLabel.y * 0.5 + 0.5) * window.innerHeight}px`;
labels[i].style.opacity = visible ? "1" : "0";
}
}
let firstFrame = true;
function animate() {
const dt = Math.min(clock.getDelta(), 0.033);
if (!paused) {
elapsed += dt;
updateSolid(elapsed);
updateLiquid(dt, elapsed);
updateGas(dt, elapsed);
dust.rotation.y = elapsed * 0.006;
}
updateCamera();
updateLabels();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
window.setTimeout(() => {
loading.style.display = "none";
}, 450);
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```