Prompt specification
Build or customize the Three.js 3D scene "Salt crystal museum cabinet" 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: Salt crystal museum cabinet
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/salt-crystal-museum-cabinet-x1955
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: salt crystal museum cabinet. Compose a macroscopic cubic crystal beside an enlarged lattice inset and dissolved-ion panel; distinguish crystal shape, lattice arrangement, and independent solvated ions. Return one self-contained HTML file.
Working Three.js Starter (salt-crystal-museum-cabinet-x1955):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>NaCl — The Salt Museum</title>
<style>
:root {
color-scheme: dark;
--ink: #07111b;
--panel: rgba(8, 18, 29, 0.82);
--line: rgba(204, 226, 237, 0.2);
--text: #eaf4f7;
--muted: #91a9b5;
--gold: #d7b274;
--sodium: #ad88ff;
--chloride: #53dfbd;
--water: #70cfff;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
color: var(--text);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.brand {
position: absolute;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 14px;
padding: 11px 15px 11px 12px;
background: linear-gradient(135deg, rgba(10, 24, 36, 0.92), rgba(10, 24, 36, 0.55));
border: 1px solid var(--line);
border-left: 3px solid var(--gold);
backdrop-filter: blur(10px);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.25);
}
.formula {
display: grid;
place-items: center;
width: 72px;
height: 48px;
color: #101b25;
background: linear-gradient(135deg, #f3f7f7, #cbdde2);
font-size: 24px;
font-weight: 900;
letter-spacing: -2px;
}
.brand-title {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.16em;
}
.brand-sub {
margin-top: 5px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.13em;
}
.legend {
position: absolute;
top: 22px;
right: 24px;
min-width: 190px;
padding: 12px 14px;
background: var(--panel);
border: 1px solid var(--line);
backdrop-filter: blur(10px);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.22);
}
.legend-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.15em;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 6px;
font-size: 11px;
}
.swatch {
width: 12px;
height: 12px;
box-shadow: 0 0 12px currentColor;
}
.sodium {
color: var(--sodium);
background: var(--sodium);
}
.chloride {
color: var(--chloride);
background: var(--chloride);
}
.bulk {
color: #f3dfca;
background: #f3dfca;
}
.views {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
gap: 6px;
padding: 6px;
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--line);
backdrop-filter: blur(12px);
box-shadow: 0 16px 42px rgba(0, 0, 0, 0.35);
}
button {
appearance: none;
border: 1px solid transparent;
background: transparent;
color: var(--muted);
padding: 10px 13px;
font: inherit;
font-size: 10px;
letter-spacing: 0.08em;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.06);
}
button.active {
color: #111a21;
background: var(--gold);
border-color: #f1d39d;
}
button.auto.active {
color: var(--text);
background: rgba(112, 207, 255, 0.16);
border-color: rgba(112, 207, 255, 0.45);
}
.hint {
position: absolute;
left: 24px;
bottom: 27px;
color: var(--muted);
font-size: 9px;
line-height: 1.7;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hint strong {
color: var(--text);
font-weight: 600;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #07111b;
color: var(--gold);
font-size: 11px;
letter-spacing: 0.2em;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
padding: 8px;
}
.brand-copy { display: none; }
.formula { width: 64px; height: 42px; font-size: 21px; }
.legend {
top: 12px;
right: 12px;
min-width: 146px;
padding: 9px 10px;
}
.legend-row { font-size: 9px; }
.views {
bottom: 12px;
width: calc(100% - 24px);
justify-content: center;
overflow-x: auto;
}
button {
padding: 9px 8px;
font-size: 8px;
white-space: nowrap;
}
.hint { display: none; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="hud">
<div class="brand">
<div class="formula">NaCl</div>
<div class="brand-copy">
<div class="brand-title">THE SALT MUSEUM</div>
<div class="brand-sub">ATOMIC SCALE · GALLERY 04</div>
</div>
</div>
<div class="legend">
<div class="legend-title">COLOR KEY / ION IDENTITY</div>
<div class="legend-row"><span class="swatch sodium"></span><span>Na⁺ · sodium ion</span></div>
<div class="legend-row"><span class="swatch chloride"></span><span>Cl⁻ · chloride ion</span></div>
<div class="legend-row"><span class="swatch bulk"></span><span>bulk salt crystal</span></div>
</div>
<nav class="views" aria-label="Camera views">
<button data-view="overview" class="active">OVERVIEW</button>
<button data-view="crystal">01 CRYSTAL</button>
<button data-view="lattice">02 LATTICE</button>
<button data-view="solution">03 SOLUTION</button>
<button id="auto" class="auto">AUTO ORBIT</button>
</nav>
<div class="hint">
<strong>DRAG</strong> TO ORBIT · <strong>SCROLL</strong> TO ZOOM<br>
KEYS 1–4 SELECT VIEWS · ARROWS MOVE CAMERA
</div>
</div>
<div id="loading">ASSEMBLING THE EXHIBIT…</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
const stage = document.getElementById('stage');
const loading = document.getElementById('loading');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050b13);
scene.fog = new THREE.Fog(0x050b13, 25, 52);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const tempObject = new THREE.Object3D();
let randomSeed = 0x5a17c0de;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function roundedRect(ctx, x, y, w, h, r) {
r = Math.min(r, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function canvasTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(4, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function addBox(parent, size, position, material, shadows = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = shadows;
mesh.receiveShadow = shadows;
parent.add(mesh);
return mesh;
}
function addEdgeBox(parent, size, position, color = 0xd7b274, opacity = 0.7) {
const geometry = new THREE.EdgesGeometry(new THREE.BoxGeometry(size[0], size[1], size[2]));
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const lines = new THREE.LineSegments(geometry, material);
lines.position.set(position[0], position[1], position[2]);
parent.add(lines);
return lines;
}
function makeSectionSprite(title, subtitle, accent) {
const canvas = document.createElement('canvas');
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 5, 5, 1014, 246, 18);
ctx.fillStyle = 'rgba(6, 15, 24, 0.88)';
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(28, 30, 8, 196);
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#edf6f7';
ctx.font = '700 49px ui-monospace, monospace';
ctx.fillText(title, 62, 91);
ctx.fillStyle = '#91a9b5';
ctx.font = '500 25px ui-monospace, monospace';
ctx.fillText(subtitle, 64, 165);
const texture = canvasTexture(canvas);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(4.65, 1.16, 1);
sprite.renderOrder = 30;
return sprite;
}
function makeMainPlaque() {
const canvas = document.createElement('canvas');
canvas.width = 1400;
canvas.height = 260;
const ctx = canvas.getContext('2d');
roundedRect(ctx, 4, 4, 1392, 252, 22);
ctx.fillStyle = 'rgba(7, 15, 23, 0.9)';
ctx.fill();
ctx.strokeStyle = '#d7b274';
ctx.lineWidth = 4;
ctx.stroke();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#f2f6f5';
ctx.font = '800 60px ui-monospace, monospace';
ctx.fillText('NaCl / THREE SCALES OF SALT', 700, 88);
ctx.fillStyle = '#d7b274';
ctx.font = '500 28px ui-monospace, monospace';
ctx.fillText('FORM → STRUCTURE → SOLUTION', 700, 171);
const texture = canvasTexture(canvas);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(10.2, 1.9, 1);
sprite.position.set(0, 7.65, -3.05);
sprite.renderOrder = 31;
return sprite;
}
function makePlaque(number, word) {
const canvas = document.createElement('canvas');
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext('2d');
roundedRect(ctx, 2, 2, 508, 124, 10);
ctx.fillStyle = '#091520';
ctx.fill();
ctx.strokeStyle = '#b9955b';
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = '#d7b274';
ctx.font = '700 53px ui-monospace, monospace';
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.fillText(number, 24, 65);
ctx.fillStyle = '#dce9ed';
ctx.font = '600 28px ui-monospace, monospace';
ctx.fillText(word, 125, 65);
const texture = canvasTexture(canvas);
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
toneMapped: false
});
return new THREE.Mesh(new THREE.PlaneGeometry(1.72, 0.43), material);
}
function makeIonTexture(baseColor, symbol) {
const canvas = document.createElement('canvas');
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext('2d');
ctx.fillStyle = baseColor;
ctx.fillRect(0, 0, 128, 128);
ctx.strokeStyle = 'rgba(255,255,255,0.38)';
ctx.lineWidth = 5;
ctx.strokeRect(7, 7, 114, 114);
ctx.fillStyle = '#ffffff';
ctx.font = '700 45px ui-monospace, monospace';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(symbol, 64, 66);
return canvasTexture(canvas);
}
const materials = {
wall: new THREE.MeshStandardMaterial({ color: 0x091521, roughness: 0.94, metalness: 0.02 }),
cabinet: new THREE.MeshStandardMaterial({ color: 0x101d28, roughness: 0.65, metalness: 0.25 }),
pedestal: new THREE.MeshStandardMaterial({ color: 0x14222e, roughness: 0.72, metalness: 0.16 }),
floor: new THREE.MeshStandardMaterial({ color: 0x0b1721, roughness: 0.48, metalness: 0.28 }),
brass: new THREE.MeshStandardMaterial({ color: 0xc49b5d, roughness: 0.28, metalness: 0.72 }),
steel: new THREE.MeshStandardMaterial({ color: 0x30424e, roughness: 0.4, metalness: 0.68 }),
glass: new THREE.MeshBasicMaterial({
color: 0x91ddff,
transparent: true,
opacity: 0.025,
side: THREE.DoubleSide,
depthWrite: false
}),
water: new THREE.MeshBasicMaterial({
color: 0x43bdf0,
transparent: true,
opacity: 0.055,
side: THREE.DoubleSide,
depthWrite: false
}),
sodiumSolid: new THREE.MeshStandardMaterial({
color: 0xad88ff,
roughness: 0.36,
metalness: 0.08,
emissive: 0x321c67,
emissiveIntensity: 0.28
}),
chlorideSolid: new THREE.MeshStandardMaterial({
color: 0x53dfbd,
roughness: 0.36,
metalness: 0.08,
emissive: 0x124e42,
emissiveIntensity: 0.25
}),
waterMote: new THREE.MeshBasicMaterial({
color: 0x8ddfff,
transparent: true,
opacity: 0.52
})
};
const crystalMaterials = [
new THREE.MeshStandardMaterial({ color: 0xf1e6d9, roughness: 0.47, metalness: 0.03 }),
new THREE.MeshStandardMaterial({ color: 0xe7d8ca, roughness: 0.5, metalness: 0.02 }),
new THREE.MeshStandardMaterial({ color: 0xf8eee3, roughness: 0.43, metalness: 0.04 }),
new THREE.MeshStandardMaterial({ color: 0xdcc9bd, roughness: 0.54, metalness: 0.02 })
];
const architecture = new THREE.Group();
scene.add(architecture);
addBox(architecture, [31, 0.35, 19], [0, -0.38, 2.5], materials.floor);
addBox(architecture, [25, 10.5, 0.4], [0, 4.45, -4.35], materials.wall);
for (let x = -12; x <= 12; x += 2) {
addBox(architecture, [0.025, 10.1, 0.025], [x, 4.45, -4.13], materials.steel, false);
}
for (let y = -0.5; y <= 9.5; y += 1.5) {
addBox(architecture, [24.5, 0.025, 0.025], [0, y, -4.13], materials.steel, false);
}
addBox(architecture, [22, 0.72, 4.85], [0, 0.32, -0.55], materials.cabinet);
addBox(architecture, [21.65, 8.2, 0.24], [0, 4.32, -3.25], materials.wall);
addBox(architecture, [21.9, 0.25, 0.28], [0, 8.32, 1.72], materials.brass);
addBox(architecture, [21.9, 0.28, 0.28], [0, 1.02, 1.72], materials.steel);
addBox(architecture, [21.8, 0.12, 0.12], [0, 8.15, 1.72], materials.brass);
[-10.8, -3.6, 3.6, 10.8].forEach((x) => {
addBox(architecture, [0.19, 7.4, 0.3], [x, 4.65, 1.72], materials.steel);
addBox(architecture, [0.055, 7.35, 0.055], [x, 4.65, 1.89], materials.brass);
});
const frontGlass = new THREE.Mesh(new THREE.PlaneGeometry(21.45, 7.12), materials.glass);
frontGlass.position.set(0, 4.66, 1.76);
frontGlass.renderOrder = 10;
architecture.add(frontGlass);
[-10.72, 10.72].forEach((x) => {
const side = new THREE.Mesh(new THREE.PlaneGeometry(4.65, 7.12), materials.glass);
side.rotation.y = Math.PI / 2;
side.position.set(x, 4.66, -0.7);
side.renderOrder = 9;
architecture.add(side);
});
const exhibitCenters = [-6.6, 0, 6.6];
exhibitCenters.forEach((x, index) => {
addBox(architecture, [6.25, 1.16, 3.45], [x, 0.94, -0.25], materials.pedestal);
addBox(architecture, [6.12, 0.08, 3.34], [x, 1.56, -0.25], materials.cabinet);
addBox(architecture, [5.65, 0.045, 0.06], [x, 1.17, 1.5], materials.brass);
const plaque = makePlaque(['01', '02', '03'][index], ['FORM', 'ORDER', 'FREEDOM'][index]);
plaque.position.set(x - 2.15, 0.87, 1.505);
architecture.add(plaque);
});
scene.add(makeMainPlaque());
const labels = [
{
sprite: makeSectionSprite('01 / CRYSTAL', 'FORM · the macroscopic cubic silhouette', '#d7b274'),
x: -6.6
},
{
sprite: makeSectionSprite('02 / LATTICE', 'ORDER · alternating ions in a rock-salt array', '#53dfbd'),
x: 0
},
{
sprite: makeSectionSprite('03 / SOLUTION', 'FREEDOM · independent, hydrated ions', '#ad88ff'),
x: 6.6
}
];
labels.forEach(({ sprite, x }) => {
sprite.position.set(x, 6.38, -0.7);
scene.add(sprite);
});
const macroGroup = new THREE.Group();
macroGroup.position.set(-6.6, 0, -0.25);
scene.add(macroGroup);
const crystalBuckets = [[], [], [], []];
const crystalSize = 6;
const cell = 0.65;
const voxelSize = 0.608;
for (let ix = 0; ix < crystalSize; ix++) {
for (let iy = 0; iy < crystalSize; iy++) {
for (let iz = 0; iz < crystalSize; iz++) {
tempObject.position.set(
(ix - (crystalSize - 1) / 2) * cell,
1.54 + ((crystalSize - 1) / 2 - iy) * cell,
(iz - (crystalSize - 1) / 2) * cell
);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.setScalar(voxelSize);
tempObject.updateMatrix();
const surface = ix === 0 || ix === crystalSize - 1 ||
iy === 0 || iy === crystalSize - 1 ||
iz === 0 || iz === crystalSize - 1;
const bucket = surface ? Math.floor(random() * 4) : 2;
crystalBuckets[bucket].push(tempObject.matrix.clone());
}
}
}
crystalBuckets.forEach((matrices, index) => {
if (!matrices.length) return;
const mesh = new THREE.InstancedMesh(unitBox, crystalMaterials[index], matrices.length);
matrices.forEach((matrix, i) => mesh.setMatrixAt(i, matrix));
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
macroGroup.add(mesh);
});
addEdgeBox(macroGroup, [3.98, 3.98, 3.98], [0, 3.47, 0], 0xffefd2, 0.38);
const rulerMaterial = materials.brass;
addBox(macroGroup, [0.035, 3.85, 0.035], [-2.28, 3.47, 0.8], rulerMaterial);
for (let i = 0; i < 8; i++) {
const tickWidth = i % 2 === 0 ? 0.23 : 0.14;
addBox(macroGroup, [tickWidth, 0.025, 0.035], [-2.28 - tickWidth / 2, 1.67 + i * 0.5, 0.8], rulerMaterial);
}
const latticeGroup = new THREE.Group();
latticeGroup.position.set(0, 0, -0.25);
scene.add(latticeGroup);
const sodiumTexture = makeIonTexture('#7354a8', 'Na⁺');
const chlorideTexture = makeIonTexture('#258d78', 'Cl⁻');
const latticeSodiumMaterial = new THREE.MeshStandardMaterial({
map: sodiumTexture,
color: 0xffffff,
roughness: 0.38,
metalness: 0.06,
emissive: 0x2b1758,
emissiveIntensity: 0.28
});
const latticeChlorideMaterial = new THREE.MeshStandardMaterial({
map: chlorideTexture,
color: 0xffffff,
roughness: 0.38,
metalness: 0.06,
emissive: 0x0b3e35,
emissiveIntensity: 0.26
});
const latticeSodiumMatrices = [];
const latticeChlorideMatrices = [];
const latticePositions = [];
const gridLines = [];
const latticeCount = 5;
const latticeSpacing = 0.78;
function latticePoint(ix, iy, iz) {
return new THREE.Vector3(
(ix - 2) * latticeSpacing,
3.58 + (2 - iy) * latticeSpacing,
(iz - 2) * latticeSpacing
);
}
for (let ix = 0; ix < latticeCount; ix++) {
for (let iy = 0; iy < latticeCount; iy++) {
for (let iz = 0; iz < latticeCount; iz++) {
const point = latticePoint(ix, iy, iz);
latticePositions.push(point);
tempObject.position.copy(point);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.setScalar(0.285);
tempObject.updateMatrix();
if ((ix + iy + iz) % 2 === 0) {
latticeChlorideMatrices.push(tempObject.matrix.clone());
} else {
latticeSodiumMatrices.push(tempObject.matrix.clone());
}
if (ix < latticeCount - 1) gridLines.push(point, latticePoint(ix + 1, iy, iz));
if (iy < latticeCount - 1) gridLines.push(point, latticePoint(ix, iy + 1, iz));
if (iz < latticeCount - 1) gridLines.push(point, latticePoint(ix, iy, iz + 1));
}
}
}
function makeInstanced(parent, matrices, material) {
const mesh = new THREE.InstancedMesh(unitBox, material, matrices.length);
matrices.forEach((matrix, index) => mesh.setMatrixAt(index, matrix));
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
makeInstanced(latticeGroup, latticeSodiumMatrices, latticeSodiumMaterial);
makeInstanced(latticeGroup, latticeChlorideMatrices, latticeChlorideMaterial);
const gridGeometry = new THREE.BufferGeometry().setFromPoints(gridLines);
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x86a9b8,
transparent: true,
opacity: 0.22,
depthWrite: false
});
latticeGroup.add(new THREE.LineSegments(gridGeometry, gridMaterial));
addEdgeBox(latticeGroup, [3.74, 3.74, 3.74], [0, 3.58, 0], 0x678c9b, 0.16);
addEdgeBox(latticeGroup, [1.56, 1.56, 1.56], [0, 3.58, 0], 0xf0c579, 0.95);
const solutionGroup = new THREE.Group();
solutionGroup.position.set(6.6, 0, -0.25);
scene.add(solutionGroup);
const tankCenterY = 3.54;
addBox(solutionGroup, [4.22, 4.1, 1.94], [0, tankCenterY, 0], materials.water, false);
const surface = new THREE.Mesh(
new THREE.PlaneGeometry(4.02, 1.76),
new THREE.MeshBasicMaterial({
color: 0x6bd4ff,
transparent: true,
opacity: 0.11,
side: THREE.DoubleSide,
depthWrite: false
})
);
surface.rotation.x = -Math.PI / 2;
surface.position.set(0, 5.49, 0);
solutionGroup.add(surface);
addEdgeBox(solutionGroup, [4.3, 4.18, 2.06], [0, tankCenterY, 0], 0x83cce8, 0.52);
[-1, 1].forEach((x) => {
[-1, 1].forEach((z) => {
addBox(solutionGroup, [0.075, 4.24, 0.075], [x * 2.13, tankCenterY, z * 1.01], materials.brass);
});
});
addBox(solutionGroup, [4.34, 0.075, 0.075], [0, 5.58, 1.01], materials.brass);
addBox(solutionGroup, [4.34, 0.075, 0.075], [0, 1.5, 1.01], materials.brass);
addBox(solutionGroup, [0.075, 0.075, 2.08], [-2.13, 5.58, 0], materials.brass);
addBox(solutionGroup, [0.075, 0.075, 2.08], [2.13, 5.58, 0], materials.brass);
const shellGeometry = new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1));
const shellMaterial = new THREE.LineBasicMaterial({
color: 0x8bdfff,
transparent: true,
opacity: 0.18,
depthWrite: false
});
const dissolvedIons = [];
const attempted = [];
function tooClose(x, y, z) {
return attempted.some((p) => {
const dx = p.x - x;
const dy = p.y - y;
const dz = p.z - z;
return dx * dx + dy * dy + dz * dz < 0.23;
});
}
for (let i = 0; i < 44; i++) {
let x, y, z;
let valid = false;
for (let attempt = 0; attempt < 80; attempt++) {
x = (random() - 0.5) * 3.72;
y = tankCenterY + (random() - 0.5) * 3.45;
z = (random() - 0.5) * 1.5;
if (!tooClose(x, y, z)) {
valid = true;
break;
}
}
if (!valid) continue;
attempted.push({ x, y, z });
const group = new THREE.Group();
group.position.set(x, y, z);
const sodium = random() > 0.5;
const ion = new THREE.Mesh(
unitBox,
sodium ? materials.sodiumSolid : materials.chlorideSolid
);
const ionScale = 0.18 + random() * 0.055;
ion.scale.setScalar(ionScale);
ion.castShadow = true;
group.add(ion);
const hydrationShell = new THREE.LineSegments(shellGeometry, shellMaterial);
hydrationShell.scale.setScalar(0.51 + random() * 0.08);
group.add(hydrationShell);
group.userData = {
baseX: x,
baseY: y,
baseZ: z,
phase: random() * Math.PI * 2,
speed: 0.3 + random() * 0.35,
spin: (random() - 0.5) * 0.35
};
dissolvedIons.push(group);
solutionGroup.add(group);
}
const waterMoteMatrices = [];
for (let i = 0; i < 100; i++) {
tempObject.position.set(
(random() - 0.5) * 3.8,
tankCenterY + (random() - 0.5) * 3.5,
(random() - 0.5) * 1.55
);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.set(0.028 + random() * 0.045, 0.028 + random() * 0.045, 0.028 + random() * 0.045);
tempObject.updateMatrix();
waterMoteMatrices.push(tempObject.matrix.clone());
}
makeInstanced(solutionGroup, waterMoteMatrices, materials.waterMote);
const dust = [];
const dustMaterial = new THREE.MeshBasicMaterial({
color: 0xc9e7ef,
transparent: true,
opacity: 0.23,
depthWrite: false
});
for (let i = 0; i < 42; i++) {
const mote = new THREE.Mesh(unitBox, dustMaterial);
const size = 0.018 + random() * 0.035;
mote.scale.setScalar(size);
mote.position.set(
-10.6 + random() * 21.2,
1.2 + random() * 6.7,
-2.9 + random() * 4.4
);
mote.userData.phase = random() * Math.PI * 2;
mote.userData.speed = 0.04 + random() * 0.06;
dust.push(mote);
scene.add(mote);
}
const hemisphere = new THREE.HemisphereLight(0xa9d9ee, 0x15202a, 1.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.25);
keyLight.position.set(-5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00015;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6bbcff, 1.6);
rimLight.position.set(8, 7, -8);
scene.add(rimLight);
exhibitCenters.forEach((x, index) => {
const spot = new THREE.SpotLight(
index === 0 ? 0xffe5c2 : index === 1 ? 0xc5fff0 : 0xc8b5ff,
105,
25,
0.48,
0.72,
2
);
spot.position.set(x, 9.2, 5.8);
spot.target.position.set(x, 3.1, -0.4);
scene.add(spot, spot.target);
});
class CameraRig {
constructor(aspect) {
this.currentTarget = new THREE.Vector3(0, 3.65, -0.2);
this.desiredTarget = this.currentTarget.clone();
this.currentRadius = innerWidth < 760 ? 30 : 24;
this.desiredRadius = this.currentRadius;
this.currentTheta = 0.105;
this.desiredTheta = this.currentTheta;
this.currentPhi = 1.28;
this.desiredPhi = this.currentPhi;
this.auto = false;
this.pointers = new Map();
this.lastPinchDistance = 0;
}
clamp() {
this.desiredRadius = THREE.MathUtils.clamp(this.desiredRadius, 8.5, 36);
this.desiredPhi = THREE.MathUtils.clamp(this.desiredPhi, 0.48, 1.48);
}
orbit(dx, dy) {
this.desiredTheta -= dx * 0.0055;
this.desiredPhi -= dy * 0.0045;
this.clamp();
}
zoom(delta) {
this.desiredRadius *= Math.exp(delta * 0.0011);
this.clamp();
}
setView(name) {
this.auto = false;
autoButton.classList.remove('active');
const views = {
overview: {
target: new THREE.Vector3(0, 3.65, -0.2),
radius: innerWidth < 760 ? 30 : 24,
theta: 0.105,
phi: 1.28
},
crystal: {
target: new THREE.Vector3(-6.6, 3.35, -0.25),
radius: 10.6,
theta: 0.02,
phi: 1.29
},
lattice: {
target: new THREE.Vector3(0, 3.6, -0.25),
radius: 10.4,
theta: 0.11,
phi: 1.27
},
solution: {
target: new THREE.Vector3(6.6, 3.5, -0.25),
radius: 10.5,
theta: 0.08,
phi: 1.3
}
};
const view = views[name];
this.desiredTarget.copy(view.target);
this.desiredRadius = view.radius;
this.desiredTheta = view.theta;
this.desiredPhi = view.phi;
this.clamp();
}
update(dt) {
if (this.auto) this.desiredTheta += dt * 0.13;
const blend = 1 - Math.exp(-dt * 8.5);
this.currentTarget.lerp(this.desiredTarget, blend);
this.currentRadius += (this.desiredRadius - this.currentRadius) * blend;
this.currentTheta += (this.desiredTheta - this.currentTheta) * blend;
this.currentPhi += (this.desiredPhi - this.currentPhi) * blend;
const sinPhi = Math.sin(this.currentPhi);
const offset = new THREE.Vector3(
sinPhi * Math.sin(this.currentTheta),
Math.cos(this.currentPhi),
sinPhi * Math.cos(this.currentTheta)
).multiplyScalar(this.currentRadius);
camera.position.copy(this.currentTarget).add(offset);
camera.lookAt(this.currentTarget);
}
}
const rig = new CameraRig(camera.aspect);
const canvas = renderer.domElement;
canvas.addEventListener('pointerdown', (event) => {
canvas.setPointerCapture(event.pointerId);
rig.pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (rig.pointers.size === 2) {
const points = [...rig.pointers.values()];
rig.lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener('pointermove', (event) => {
if (!rig.pointers.has(event.pointerId)) return;
const previous = rig.pointers.get(event.pointerId);
const current = { x: event.clientX, y: event.clientY };
rig.pointers.set(event.pointerId, current);
if (rig.pointers.size === 1) {
rig.orbit(current.x - previous.x, current.y - previous.y);
} else if (rig.pointers.size === 2) {
const points = [...rig.pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (rig.lastPinchDistance > 0 && distance > 0) {
rig.desiredRadius *= rig.lastPinchDistance / distance;
rig.clamp();
}
rig.lastPinchDistance = distance;
}
});
function releasePointer(event) {
rig.pointers.delete(event.pointerId);
rig.lastPinchDistance = 0;
}
canvas.addEventListener('pointerup', releasePointer);
canvas.addEventListener('pointercancel', releasePointer);
canvas.addEventListener('contextmenu', (event) => event.preventDefault());
canvas.addEventListener('wheel', (event) => {
event.preventDefault();
rig.zoom(event.deltaY);
}, { passive: false });
const viewButtons = [...document.querySelectorAll('[data-view]')];
viewButtons.forEach((button) => {
button.addEventListener('click', () => {
viewButtons.forEach((item) => item.classList.remove('active'));
button.classList.add('active');
rig.setView(button.dataset.view);
});
});
const autoButton = document.getElementById('auto');
autoButton.addEventListener('click', () => {
rig.auto = !rig.auto;
autoButton.classList.toggle('active', rig.auto);
if (rig.auto) {
viewButtons.forEach((item) => item.classList.remove('active'));
}
});
addEventListener('keydown', (event) => {
const key = event.key.toLowerCase();
if (key === '1') document.querySelector('[data-view="overview"]').click();
if (key === '2') document.querySelector('[data-view="crystal"]').click();
if (key === '3') document.querySelector('[data-view="lattice"]').click();
if (key === '4') document.querySelector('[data-view="solution"]').click();
if (event.key === 'ArrowLeft') rig.desiredTheta += 0.12;
if (event.key === 'ArrowRight') rig.desiredTheta -= 0.12;
if (event.key === 'ArrowUp') rig.desiredPhi -= 0.07;
if (event.key === 'ArrowDown') rig.desiredPhi += 0.07;
if (key === '+' || key === '=') rig.desiredRadius -= 0.8;
if (key === '-' || key === '_') rig.desiredRadius += 0.8;
rig.clamp();
});
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
rig.update(dt);
dissolvedIons.forEach((group) => {
const data = group.userData;
group.position.set(
data.baseX + Math.sin(elapsed * data.speed + data.phase) * 0.055,
data.baseY + Math.cos(elapsed * data.speed * 0.72 + data.phase) * 0.065,
data.baseZ + Math.sin(elapsed * data.speed * 0.56 + data.phase * 1.7) * 0.045
);
group.rotation.x += dt * data.spin;
group.rotation.y += dt * data.spin * 0.7;
});
dust.forEach((mote) => {
mote.position.y += mote.userData.speed * dt;
mote.position.x += Math.sin(elapsed * 0.35 + mote.userData.phase) * dt * 0.015;
if (mote.position.y > 8.2) mote.position.y = 1.1;
});
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add('hidden');
setTimeout(() => loading.remove(), 600);
});
</script>
</body>
</html>
```