Prompt specification
Build or customize the Three.js 3D scene "State-model assumption explorer" 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: State-model assumption explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/state-model-assumption-explorer-x1968
Scene Description Prompt:
Adjust temperature and confinement in a voxel particle box, showing qualitative phase changes through arrangement and movement. Use Three.js from cdnjs in standalone HTML.
Working Three.js Starter (state-model-assumption-explorer-x1968):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Phase Chamber</title>
<style>
:root {
color-scheme: dark;
--bg: #060b13;
--panel: rgba(10, 19, 31, 0.76);
--border: rgba(142, 203, 231, 0.16);
--text: #e8f4fb;
--muted: #8ba2b5;
--cyan: #67dcff;
--amber: #ffbd6b;
--pink: #ff7298;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 45% 35%, #102438 0%, transparent 42%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 24px rgba(0, 0, 0, 0.6);
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 38px);
font-weight: 680;
letter-spacing: -0.045em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.02em;
}
.panel {
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 22px 70px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(125%);
}
.controls {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: 320px;
padding: 19px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live-indicator {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live-indicator::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #67e8a7;
box-shadow: 0 0 12px #67e8a7;
content: "";
}
.control {
margin-bottom: 23px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 16px;
margin-bottom: 10px;
}
.control-label label {
color: #c9d8e3;
font-size: 12px;
font-weight: 650;
}
output {
color: white;
font-variant-numeric: tabular-nums;
font-size: 18px;
font-weight: 650;
}
input[type="range"] {
--fill: 50%;
--accent: var(--cyan);
width: 100%;
height: 5px;
margin: 0;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--accent) 0,
var(--accent) var(--fill),
rgba(255, 255, 255, 0.1) var(--fill),
rgba(255, 255, 255, 0.1) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #08121e;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 16px var(--accent);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #08121e;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 16px var(--accent);
}
#confinement {
--accent: var(--amber);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #62798b;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 8px;
margin-top: 2px;
}
button {
min-height: 37px;
padding: 8px 9px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #c5d3de;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
border-color: rgba(103, 220, 255, 0.38);
background: rgba(103, 220, 255, 0.08);
color: white;
}
button.active {
border-color: rgba(255, 189, 107, 0.55);
background: rgba(255, 189, 107, 0.12);
color: var(--amber);
}
.phase-card {
--phase: var(--amber);
position: fixed;
z-index: 8;
bottom: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
padding: 20px 22px;
pointer-events: none;
}
.phase-top {
display: flex;
align-items: center;
gap: 12px;
}
.phase-badge {
padding: 6px 9px;
border: 1px solid currentColor;
border-radius: 7px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
}
.phase-card h2 {
margin: 0;
font-size: 22px;
font-weight: 670;
letter-spacing: -0.03em;
}
.phase-card p {
min-height: 42px;
margin: 12px 0 17px;
color: #9bb0c0;
font-size: 12px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.metric span {
display: block;
margin-bottom: 5px;
color: #687f91;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric strong {
color: #d8e7ef;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.legend {
position: fixed;
z-index: 7;
right: 26px;
bottom: 28px;
display: flex;
gap: 8px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 99px;
background: rgba(5, 12, 20, 0.55);
color: #8499aa;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.hint {
position: fixed;
z-index: 6;
top: 31px;
left: 50%;
color: rgba(174, 199, 215, 0.55);
font-size: 10px;
letter-spacing: 0.08em;
pointer-events: none;
transform: translateX(-50%);
}
.model-note {
position: fixed;
z-index: 5;
right: 28px;
bottom: 79px;
max-width: 320px;
color: rgba(139, 162, 181, 0.55);
font-size: 9px;
line-height: 1.45;
text-align: right;
pointer-events: none;
}
@media (max-width: 760px) {
.header {
top: 17px;
left: 17px;
}
.subtitle,
.hint,
.legend,
.model-note {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 15px;
}
.panel-heading {
margin-bottom: 13px;
}
.control {
margin-bottom: 15px;
}
.phase-card {
top: 104px;
bottom: auto;
left: 12px;
width: min(330px, calc(100vw - 24px));
padding: 15px 17px;
}
.phase-card p {
min-height: 0;
margin: 9px 0 12px;
}
.metrics {
padding-top: 11px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="header">
<div class="eyebrow">Interactive Matter Study</div>
<h1>Voxel Phase Chamber</h1>
<p class="subtitle">Temperature × confinement · qualitative particle model</p>
</header>
<aside class="controls panel">
<div class="panel-heading">
<strong>Chamber controls</strong>
<div class="live-indicator">Simulation live</div>
</div>
<div class="control">
<div class="control-label">
<label for="temperature">Temperature</label>
<output id="temperatureValue" for="temperature">520 K</output>
</div>
<input id="temperature" type="range" min="40" max="900" step="1" value="520" />
<div class="range-ends">
<span>Low energy</span>
<span>High energy</span>
</div>
</div>
<div class="control">
<div class="control-label">
<label for="confinement">Confinement</label>
<output id="confinementValue" for="confinement">38%</output>
</div>
<input id="confinement" type="range" min="0" max="100" step="1" value="38" />
<div class="range-ends">
<span>Open volume</span>
<span>Compressed</span>
</div>
</div>
<div class="button-row">
<button id="pauseButton" type="button">Pause</button>
<button id="cycleButton" type="button">Cycle</button>
<button id="resetButton" type="button">Reset</button>
</div>
</aside>
<section id="phaseCard" class="phase-card panel">
<div class="phase-top">
<div id="phaseBadge" class="phase-badge">LIQUID</div>
<h2 id="phaseName">Condensed motion</h2>
</div>
<p id="phaseDescription">
Neighbors remain close, but thermal motion lets the voxels slide past one another.
</p>
<div class="metrics">
<div class="metric">
<span>Thermal energy</span>
<strong id="thermalMetric">56%</strong>
</div>
<div class="metric">
<span>Accessible volume</span>
<strong id="volumeMetric">39%</strong>
</div>
<div class="metric">
<span>Voxel count</span>
<strong>180</strong>
</div>
</div>
</section>
<div class="legend">
<div class="legend-item">
<span class="legend-dot" style="color:#67dcff;background:#67dcff"></span>
Solid
</div>
<div class="legend-item">
<span class="legend-dot" style="color:#ffbd6b;background:#ffbd6b"></span>
Liquid
</div>
<div class="legend-item">
<span class="legend-dot" style="color:#ff7298;background:#ff7298"></span>
Gas
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div class="model-note">
Illustrative behavior only — not a molecular-dynamics or thermodynamic calculation.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and state
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const temperatureInput = document.getElementById("temperature");
const confinementInput = document.getElementById("confinement");
const temperatureValue = document.getElementById("temperatureValue");
const confinementValue = document.getElementById("confinementValue");
const phaseCard = document.getElementById("phaseCard");
const phaseBadge = document.getElementById("phaseBadge");
const phaseName = document.getElementById("phaseName");
const phaseDescription = document.getElementById("phaseDescription");
const thermalMetric = document.getElementById("thermalMetric");
const volumeMetric = document.getElementById("volumeMetric");
const pauseButton = document.getElementById("pauseButton");
const cycleButton = document.getElementById("cycleButton");
const resetButton = document.getElementById("resetButton");
const PARTICLE_COUNT = 180;
const GRID_X = 6;
const GRID_Y = 5;
const GRID_Z = 6;
const OUTER_WIDTH = 12;
const OUTER_HEIGHT = 7.2;
const OUTER_DEPTH = 9;
const INNER_CENTER_Y = 0.15;
const state = {
temperature: 520,
confinement: 38,
paused: false,
cycling: false,
cycleStart: 0
};
const colors = {
solid: new THREE.Color("#67dcff"),
liquid: new THREE.Color("#ffbd6b"),
gas: new THREE.Color("#ff7298")
};
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function smoothstep(edge0, edge1, value) {
const x = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return x * x * (3 - 2 * x);
}
function getThermalFraction() {
return clamp((state.temperature - 40) / 860, 0, 1);
}
function getConfinementFraction() {
return state.confinement / 100;
}
function getPhaseWeights() {
const thermal = getThermalFraction();
const confinement = getConfinementFraction();
const solidity = 1 - smoothstep(0.23, 0.37, thermal);
const gasness =
smoothstep(0.59, 0.75, thermal) *
(1 - smoothstep(0.42, 0.82, confinement));
const liquidness = Math.max(0, 1 - solidity - gasness);
return { thermal, confinement, solidity, liquidness, gasness };
}
function getPhase() {
const thermal = getThermalFraction();
const confinement = getConfinementFraction();
if (thermal < 0.29) {
return {
key: "solid",
badge: "SOLID",
title: "Lattice order",
color: "#67dcff",
description:
"Low thermal energy lets neighboring voxels settle into a repeating crystal-like lattice."
};
}
if (thermal > 0.66 && confinement < 0.56) {
return {
key: "gas",
badge: "GAS",
title: "Free expansion",
color: "#ff7298",
description:
"The particles overcome their neighbors and sweep through the available chamber as a diffuse gas."
};
}
return {
key: "liquid",
badge: "LIQUID",
title: "Condensed motion",
color: "#ffbd6b",
description:
"Neighbors remain close, but thermal motion lets the voxels slide past one another."
};
}
function getDesiredExtents() {
const confinement = getConfinementFraction();
return {
hx: lerp(5.8, 2.45, confinement),
hy: lerp(3.45, 1.55, confinement),
hz: lerp(4.35, 2.15, confinement)
};
}
function updateSliderFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
input.style.setProperty("--fill", `${((value - min) / (max - min)) * 100}%`);
}
function updateReadouts() {
const phase = getPhase();
const weights = getPhaseWeights();
const desired = getDesiredExtents();
const outerHalfX = OUTER_WIDTH / 2;
const outerHalfY = OUTER_HEIGHT / 2;
const outerHalfZ = OUTER_DEPTH / 2;
const volumeFraction =
(desired.hx * desired.hy * desired.hz) /
(outerHalfX * outerHalfY * outerHalfZ);
temperatureValue.textContent = `${Math.round(state.temperature)} K`;
confinementValue.textContent = `${Math.round(state.confinement)}%`;
phaseBadge.textContent = phase.badge;
phaseName.textContent = phase.title;
phaseDescription.textContent = phase.description;
phaseBadge.style.color = phase.color;
phaseBadge.style.background = `${phase.color}18`;
phaseCard.style.setProperty("--phase", phase.color);
thermalMetric.textContent = `${Math.round(weights.thermal * 100)}%`;
volumeMetric.textContent = `${Math.round(volumeFraction * 100)}%`;
updateSliderFill(temperatureInput);
updateSliderFill(confinementInput);
}
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color("#060b13");
scene.fog = new THREE.FogExp2("#060b13", 0.025);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive 3D voxel phase chamber");
viewport.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xb9e7ff, 0x13202c, 1.65));
const keyLight = new THREE.DirectionalLight(0xdaf4ff, 3.1);
keyLight.position.set(6, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0xff8c65, 45, 30, 2);
rimLight.position.set(-7, 2, -6);
scene.add(rimLight);
const blueLight = new THREE.PointLight(0x4abfff, 38, 28, 2);
blueLight.position.set(6, -1, 5);
scene.add(blueLight);
// Subtle background motes.
const starPositions = new Float32Array(180 * 3);
for (let i = 0; i < 180; i++) {
const radius = 15 + Math.random() * 18;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
starPositions[i * 3] =
radius * Math.sin(phi) * Math.cos(theta);
starPositions[i * 3 + 1] =
radius * Math.cos(phi);
starPositions[i * 3 + 2] =
radius * Math.sin(phi) * Math.sin(theta);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x76b9d6,
size: 0.055,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Chamber geometry
// -------------------------------------------------------------------------
const chamber = new THREE.Group();
scene.add(chamber);
const outerBoxGeometry = new THREE.BoxGeometry(
OUTER_WIDTH,
OUTER_HEIGHT,
OUTER_DEPTH
);
const outerFill = new THREE.Mesh(
outerBoxGeometry,
new THREE.MeshBasicMaterial({
color: 0x1b6b88,
transparent: true,
opacity: 0.025,
side: THREE.DoubleSide,
depthWrite: false
})
);
const outerEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(outerBoxGeometry),
new THREE.LineBasicMaterial({
color: 0x5ab8d5,
transparent: true,
opacity: 0.36,
depthWrite: false
})
);
chamber.add(outerFill, outerEdges);
const innerBoxGeometry = new THREE.BoxGeometry(1, 1, 1);
const innerFill = new THREE.Mesh(
innerBoxGeometry,
new THREE.MeshBasicMaterial({
color: 0xffb55f,
transparent: true,
opacity: 0.05,
side: THREE.DoubleSide,
depthWrite: false
})
);
const innerEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(innerBoxGeometry),
new THREE.LineBasicMaterial({
color: 0xffbd6b,
transparent: true,
opacity: 0.52,
depthWrite: false
})
);
const innerCage = new THREE.Group();
innerCage.position.y = INNER_CENTER_Y;
innerCage.add(innerFill, innerEdges);
chamber.add(innerCage);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(OUTER_WIDTH, OUTER_DEPTH),
new THREE.MeshStandardMaterial({
color: 0x09141e,
roughness: 0.9,
metalness: 0.1,
transparent: true,
opacity: 0.82
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -OUTER_HEIGHT / 2 - 0.01;
floor.receiveShadow = true;
chamber.add(floor);
const gridVertices = [];
const gridDivisions = 18;
for (let i = 0; i <= gridDivisions; i++) {
const t = i / gridDivisions;
const x = lerp(-OUTER_WIDTH / 2, OUTER_WIDTH / 2, t);
const z = lerp(-OUTER_DEPTH / 2, OUTER_DEPTH / 2, t);
gridVertices.push(
x, -OUTER_HEIGHT / 2 + 0.008, -OUTER_DEPTH / 2,
x, -OUTER_HEIGHT / 2 + 0.008, OUTER_DEPTH / 2,
-OUTER_WIDTH / 2, -OUTER_HEIGHT / 2 + 0.008, z,
OUTER_WIDTH / 2, -OUTER_HEIGHT / 2 + 0.008, z
);
}
const floorGrid = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
),
new THREE.LineBasicMaterial({
color: 0x28657a,
transparent: true,
opacity: 0.22
})
);
chamber.add(floorGrid);
// -------------------------------------------------------------------------
// Voxel particles
// -------------------------------------------------------------------------
const particleGeometry = new THREE.BoxGeometry(0.44, 0.44, 0.44);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.42,
metalness: 0.12,
emissive: 0x17202b,
emissiveIntensity: 1,
vertexColors: true
});
const particlesMesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
PARTICLE_COUNT
);
particlesMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particlesMesh.castShadow = true;
particlesMesh.receiveShadow = true;
particlesMesh.frustumCulled = false;
chamber.add(particlesMesh);
const particles = [];
const slots = Array.from(
{ length: PARTICLE_COUNT },
(_, index) => index
);
// Shuffle which voxel occupies each lattice position.
for (let i = slots.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[slots[i], slots[j]] = [slots[j], slots[i]];
}
for (let i = 0; i < PARTICLE_COUNT; i++) {
particles.push({
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
target: new THREE.Vector3(),
offset: new THREE.Vector3(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
),
rotation: new THREE.Vector3(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
),
slot: slots[i],
variation: 0.78 + Math.random() * 0.3
});
}
const glowPositions = new Float32Array(PARTICLE_COUNT * 3);
const glowGeometry = new THREE.BufferGeometry();
glowGeometry.setAttribute(
"position",
new THREE.BufferAttribute(glowPositions, 3)
);
const glowMaterial = new THREE.PointsMaterial({
color: colors.liquid,
size: 0.105,
transparent: true,
opacity: 0.52,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const glowPoints = new THREE.Points(glowGeometry, glowMaterial);
glowPoints.frustumCulled = false;
chamber.add(glowPoints);
const trailPositions = new Float32Array(PARTICLE_COUNT * 6);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3)
);
const trailMaterial = new THREE.LineBasicMaterial({
color: colors.liquid,
transparent: true,
opacity: 0.22,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const trails = new THREE.LineSegments(trailGeometry, trailMaterial);
trails.frustumCulled = false;
chamber.add(trails);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
const renderColor = new THREE.Color();
const currentExtents = {
...getDesiredExtents()
};
function computeTargets() {
const weights = getPhaseWeights();
const confinement = weights.confinement;
const packScale = lerp(1, 0.78, confinement);
const solidHalfX = Math.min(
currentExtents.hx * 0.96,
2.34 * packScale
);
const solidHalfY = Math.min(
currentExtents.hy * 0.96,
1.82 * packScale
);
const solidHalfZ = Math.min(
currentExtents.hz * 0.96,
2.34 * packScale
);
const liquidHalfX = Math.min(
currentExtents.hx * 0.99,
2.34 * packScale * 1.18
);
const liquidHalfY = Math.min(
currentExtents.hy * 0.99,
1.82 * packScale * 1.18
);
const liquidHalfZ = Math.min(
currentExtents.hz * 0.99,
2.34 * packScale * 1.18
);
const orderedWeight = weights.solidity + weights.liquidness;
const safeWeight = Math.max(orderedWeight, 0.0001);
const halfX =
(solidHalfX * weights.solidity +
liquidHalfX * weights.liquidness) /
safeWeight;
const halfY =
(solidHalfY * weights.solidity +
liquidHalfY * weights.liquidness) /
safeWeight;
const halfZ =
(solidHalfZ * weights.solidity +
liquidHalfZ * weights.liquidness) /
safeWeight;
const jitter =
(0.025 * weights.solidity +
0.29 * weights.liquidness) *
(0.55 + weights.thermal);
for (const particle of particles) {
const slot = particle.slot;
const ix = slot % GRID_X;
const iy = Math.floor(slot / GRID_X) % GRID_Y;
const iz = Math.floor(slot / (GRID_X * GRID_Y));
const nx = (ix + 0.5) / GRID_X * 2 - 1;
const ny = (iy + 0.5) / GRID_Y * 2 - 1;
const nz = (iz + 0.5) / GRID_Z * 2 - 1;
particle.target.set(
nx * halfX + particle.offset.x * jitter,
INNER_CENTER_Y + ny * halfY + particle.offset.y * jitter,
nz * halfZ + particle.offset.z * jitter
);
}
}
function resetParticles() {
const weights = getPhaseWeights();
Object.assign(currentExtents, getDesiredExtents());
computeTargets();
for (const particle of particles) {
if (weights.gasness > 0.5) {
particle.position.set(
(Math.random() * 2 - 1) * currentExtents.hx * 0.88,
INNER_CENTER_Y +
(Math.random() * 2 - 1) * currentExtents.hy * 0.88,
(Math.random() * 2 - 1) * currentExtents.hz * 0.88
);
} else {
const spread = 0.05 + weights.liquidness * 0.26;
particle.position.copy(particle.target).addScaledVector(
particle.offset,
spread
);
}
particle.velocity.set(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
).normalize();
const speed = weights.gasness > 0.5 ? 3.4 : 0.75;
particle.velocity.multiplyScalar(speed);
}
}
function simulate(dt, elapsed) {
const weights = getPhaseWeights();
const desired = getDesiredExtents();
// Smoothly resize the accessible volume.
const extentBlend = 1 - Math.exp(-5 * dt);
currentExtents.hx = lerp(currentExtents.hx, desired.hx, extentBlend);
currentExtents.hy = lerp(currentExtents.hy, desired.hy, extentBlend);
currentExtents.hz = lerp(currentExtents.hz, desired.hz, extentBlend);
innerCage.scale.set(
currentExtents.hx * 2,
currentExtents.hy * 2,
currentExtents.hz * 2
);
innerFill.material.opacity = 0.025 + weights.confinement * 0.055;
innerEdges.material.opacity = 0.32 + weights.confinement * 0.36;
computeTargets();
const springStrength =
weights.solidity * 34 +
weights.liquidness * (2.5 + 6.2 * weights.confinement);
const damping =
weights.solidity * 10.5 +
weights.liquidness * 1.8 +
weights.gasness * 0.12;
const dampingFactor = Math.exp(-damping * dt);
const kickStrength =
(0.25 + weights.thermal * 12.5) *
(1 - weights.solidity * 0.94);
const randomDirection = new THREE.Vector3();
for (const particle of particles) {
randomDirection.set(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
);
if (randomDirection.lengthSq() < 0.0001) {
randomDirection.set(1, 0, 0);
} else {
randomDirection.normalize();
}
particle.velocity.addScaledVector(
randomDirection,
kickStrength * dt
);
if (weights.solidity + weights.liquidness > 0.001) {
particle.velocity.addScaledVector(
particle.target.clone().sub(particle.position),
springStrength * dt
);
}
particle.velocity.multiplyScalar(dampingFactor);
particle.position.addScaledVector(particle.velocity, dt);
}
// Lightweight separation prevents most visible overlaps.
const separationWeight =
weights.liquidness * 0.17 + weights.gasness * 0.08;
if (separationWeight > 0.001) {
const minimumDistance = 0.49;
for (let i = 0; i < particles.length; i++) {
const a = particles[i];
for (let j = i + 1; j < particles.length; j++) {
const b = particles[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 > 0.000001 &&
distanceSquared < minimumDistance * minimumDistance
) {
const distance = Math.sqrt(distanceSquared);
const correction =
(minimumDistance - distance) *
separationWeight *
0.5;
const invDistance = 1 / distance;
const cx = dx * invDistance * correction;
const cy = dy * invDistance * correction;
const cz = dz * invDistance * correction;
a.position.x -= cx;
a.position.y -= cy;
a.position.z -= cz;
b.position.x += cx;
b.position.y += cy;
b.position.z += cz;
}
}
}
}
const radius = 0.235;
for (const particle of particles) {
const xLimit = currentExtents.hx - radius;
const yLimit = currentExtents.hy - radius;
const zLimit = currentExtents.hz - radius;
if (particle.position.x > xLimit) {
particle.position.x = xLimit;
particle.velocity.x = -Math.abs(particle.velocity.x) * 0.92;
} else if (particle.position.x < -xLimit) {
particle.position.x = -xLimit;
particle.velocity.x = Math.abs(particle.velocity.x) * 0.92;
}
if (particle.position.y > INNER_CENTER_Y + yLimit) {
particle.position.y = INNER_CENTER_Y + yLimit;
particle.velocity.y = -Math.abs(particle.velocity.y) * 0.92;
} else if (particle.position.y < INNER_CENTER_Y - yLimit) {
particle.position.y = INNER_CENTER_Y - yLimit;
particle.velocity.y = Math.abs(particle.velocity.y) * 0.92;
}
if (particle.position.z > zLimit) {
particle.position.z = zLimit;
particle.velocity.z = -Math.abs(particle.velocity.z) * 0.92;
} else if (particle.position.z < -zLimit) {
particle.position.z = -zLimit;
particle.velocity.z = Math.abs(particle.velocity.z) * 0.92;
}
}
// Blend the three phase colors during transitions.
renderColor.setRGB(
colors.solid.r * weights.solidity +
colors.liquid.r * weights.liquidness +
colors.gas.r * weights.gasness,
colors.solid.g * weights.solidity +
colors.liquid.g * weights.liquidness +
colors.gas.g * weights.gasness,
colors.solid.b * weights.solidity +
colors.liquid.b * weights.liquidness +
colors.gas.b * weights.gasness
);
particleMaterial.emissive.copy(renderColor).multiplyScalar(0.18);
glowMaterial.color.copy(renderColor);
trailMaterial.color.copy(renderColor);
trailMaterial.opacity = 0.08 + weights.gasness * 0.24;
const spin =
weights.gasness * 0.95 +
weights.liquidness * 0.13;
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
dummy.position.copy(particle.position);
dummy.rotation.set(
particle.rotation.x * spin,
particle.rotation.y * spin,
particle.rotation.z * spin
);
const pulse =
1 +
Math.sin(elapsed * 3.5 + i * 0.37) *
0.025 *
weights.thermal *
(1 - weights.solidity);
dummy.scale.setScalar(pulse);
dummy.updateMatrix();
particlesMesh.setMatrixAt(i, dummy.matrix);
tempColor.setRGB(
renderColor.r * particle.variation,
renderColor.g * particle.variation,
renderColor.b * particle.variation
);
particlesMesh.setColorAt(i, tempColor);
glowPositions[i * 3] = particle.position.x;
glowPositions[i * 3 + 1] = particle.position.y;
glowPositions[i * 3 + 2] = particle.position.z;
const trailLength =
weights.gasness * 0.075 +
weights.liquidness * 0.018;
const offset = i * 6;
trailPositions[offset] =
particle.position.x - particle.velocity.x * trailLength;
trailPositions[offset + 1] =
particle.position.y - particle.velocity.y * trailLength;
trailPositions[offset + 2] =
particle.position.z - particle.velocity.z * trailLength;
trailPositions[offset + 3] = particle.position.x;
trailPositions[offset + 4] = particle.position.y;
trailPositions[offset + 5] = particle.position.z;
}
particlesMesh.instanceMatrix.needsUpdate = true;
if (particlesMesh.instanceColor) {
particlesMesh.instanceColor.needsUpdate = true;
}
glowGeometry.attributes.position.needsUpdate = true;
trailGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Camera controls implemented directly
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
const cameraState = {
theta: 0.72,
phi: 1.02,
radius: 18
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.theta = 0.72;
cameraState.phi = 1.02;
cameraState.radius = 18;
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = clamp(
cameraState.phi - dy * 0.005,
0.35,
1.46
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.012,
10,
28
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Controls and automatic phase tour
// -------------------------------------------------------------------------
temperatureInput.addEventListener("input", () => {
state.temperature = Number(temperatureInput.value);
state.cycling = false;
cycleButton.classList.remove("active");
updateReadouts();
});
confinementInput.addEventListener("input", () => {
state.confinement = Number(confinementInput.value);
state.cycling = false;
cycleButton.classList.remove("active");
updateReadouts();
});
pauseButton.addEventListener("click", () => {
state.paused = !state.paused;
pauseButton.textContent = state.paused ? "Resume" : "Pause";
pauseButton.classList.toggle("active", state.paused);
});
cycleButton.addEventListener("click", () => {
state.cycling = !state.cycling;
state.cycleStart = performance.now();
cycleButton.classList.toggle("active", state.cycling);
});
resetButton.addEventListener("click", () => {
state.temperature = 520;
state.confinement = 38;
state.paused = false;
state.cycling = false;
temperatureInput.value = state.temperature;
confinementInput.value = state.confinement;
pauseButton.textContent = "Pause";
pauseButton.classList.remove("active");
cycleButton.classList.remove("active");
updateReadouts();
resetParticles();
resetCamera();
});
const cycleKeyframes = [
{ temperature: 175, confinement: 32 },
{ temperature: 510, confinement: 48 },
{ temperature: 770, confinement: 16 },
{ temperature: 510, confinement: 48 }
];
function updateCycle(now) {
const segmentDuration = 9000;
const elapsed = now - state.cycleStart;
const segment = Math.floor(elapsed / segmentDuration) % cycleKeyframes.length;
const next = (segment + 1) % cycleKeyframes.length;
let fraction =
(elapsed % segmentDuration) / segmentDuration;
fraction = fraction * fraction * (3 - 2 * fraction);
const from = cycleKeyframes[segment];
const to = cycleKeyframes[next];
state.temperature = Math.round(
lerp(from.temperature, to.temperature, fraction)
);
state.confinement = Math.round(
lerp(from.confinement, to.confinement, fraction)
);
temperatureInput.value = state.temperature;
confinementInput.value = state.confinement;
updateReadouts();
}
// -------------------------------------------------------------------------
// Resize and animation loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateReadouts();
resetParticles();
updateCamera();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.033);
previousTime = now;
if (state.cycling) {
updateCycle(now);
}
if (!state.paused) {
simulate(dt, now / 1000);
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```