Prompt specification
Build or customize the Three.js 3D scene "Materials cabinet linked to elements" 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: Materials cabinet linked to elements
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/materials-cabinet-linked-to-elements-x1971
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a scene with familiar material objects beside element or compound labels, such as copper wire and silica glass; distinguish pure elements from compounds and mixtures. Voxel style.
Working Three.js Starter (materials-cabinet-linked-to-elements-x1971):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Chemistry — Elements, Compounds & Mixtures</title>
<style>
:root {
color-scheme: dark;
--background: #07111d;
--panel: rgba(10, 24, 39, 0.88);
--border: rgba(177, 213, 241, 0.16);
--text: #edf7ff;
--muted: #9db1c5;
--element: #5ee6c4;
--compound: #a78bfa;
--mixture: #ffc857;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--background);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#leaders,
#labels {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 4;
}
#leaders {
overflow: visible;
}
.header {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
max-width: min(460px, calc(100vw - 52px));
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: #7debd2;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.header h1 {
margin: 0;
font-size: clamp(26px, 3vw, 43px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.header p {
max-width: 430px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: fixed;
top: 24px;
right: 24px;
z-index: 12;
width: 218px;
padding: 14px;
border: 1px solid var(--border);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
}
.legend-title {
margin-bottom: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 12px 1fr;
gap: 3px 9px;
align-items: center;
padding: 6px 0;
}
.legend-row + .legend-row {
border-top: 1px solid rgba(255, 255, 255, 0.055);
}
.legend-dot {
grid-row: 1 / 3;
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.legend-row strong {
font-size: 12px;
}
.legend-row small {
color: var(--muted);
font-size: 10px;
}
.legend-actions {
display: flex;
gap: 8px;
margin-top: 11px;
}
#reset {
width: 100%;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
background 160ms ease,
transform 160ms ease;
}
#reset:hover {
background: rgba(255, 255, 255, 0.11);
transform: translateY(-1px);
}
.material-card {
--accent: #ffffff;
--tint: rgba(255, 255, 255, 0.1);
position: fixed;
top: 0;
left: 0;
width: 226px;
padding: 12px 13px 11px;
border: 1px solid rgba(181, 215, 242, 0.19);
border-top: 3px solid var(--accent);
border-radius: 12px;
background:
linear-gradient(145deg, rgba(20, 42, 63, 0.94), rgba(8, 20, 34, 0.94));
color: var(--text);
text-align: left;
white-space: normal;
pointer-events: auto;
cursor: pointer;
box-shadow: 0 15px 42px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
will-change: transform;
transition:
border-color 160ms ease,
box-shadow 160ms ease,
filter 160ms ease;
}
.material-card:hover,
.material-card.hovered {
border-color: var(--accent);
filter: brightness(1.08);
}
.material-card.selected {
border-color: var(--accent);
box-shadow:
0 0 0 1px var(--accent),
0 18px 50px rgba(0, 0, 0, 0.38),
0 0 32px var(--tint);
}
.badge {
display: inline-flex;
align-items: center;
padding: 4px 6px;
border-radius: 5px;
background: var(--tint);
color: var(--accent);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
.material-name {
display: block;
margin-top: 8px;
font-size: 15px;
font-weight: 800;
letter-spacing: -0.015em;
}
.formula {
display: block;
margin-top: 2px;
color: var(--accent);
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 700;
line-height: 1.1;
}
.material-explain {
display: block;
margin-top: 6px;
color: #c4d4e3;
font-size: 11px;
line-height: 1.35;
}
.atom-row {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 9px;
}
.atom-chip {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 6px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 5px;
background: rgba(255, 255, 255, 0.045);
color: #dceaf6;
font-size: 9px;
font-weight: 800;
}
.atom-chip i {
width: 6px;
height: 6px;
border-radius: 1px;
background: var(--atom-color);
box-shadow: 0 0 7px var(--atom-color);
}
.ratio {
display: block;
margin-top: 8px;
padding-top: 7px;
border-top: 1px solid rgba(255, 255, 255, 0.075);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.03em;
}
.definitions {
position: fixed;
left: 50%;
bottom: 18px;
z-index: 10;
display: grid;
grid-template-columns: repeat(3, 1fr);
width: min(860px, calc(100vw - 32px));
transform: translateX(-50%);
overflow: hidden;
border: 1px solid var(--border);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
pointer-events: none;
}
.definition {
position: relative;
min-width: 0;
padding: 13px 16px 14px;
}
.definition + .definition {
border-left: 1px solid var(--border);
}
.definition::before {
position: absolute;
top: 0;
right: 0;
left: 0;
height: 2px;
background: var(--definition-color);
content: "";
opacity: 0.85;
}
.definition strong {
display: block;
color: var(--definition-color);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.definition span {
display: block;
margin-top: 5px;
color: #edf7ff;
font-size: 12px;
font-weight: 700;
}
.definition small {
display: block;
margin-top: 3px;
overflow: hidden;
color: var(--muted);
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.instructions {
position: fixed;
right: 20px;
bottom: 124px;
z-index: 10;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(7, 17, 29, 0.68);
color: #9fb3c6;
font-size: 9px;
letter-spacing: 0.03em;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #07111d;
color: #a8c2d8;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 900px) {
.header {
top: 18px;
left: 18px;
}
.header p {
max-width: 330px;
}
.legend {
top: 18px;
right: 18px;
width: 185px;
}
}
@media (max-width: 700px) {
.header {
top: 14px;
left: 14px;
max-width: calc(100vw - 28px);
}
.header h1 {
max-width: 250px;
font-size: 25px;
}
.header p {
display: none;
}
.eyebrow {
margin-bottom: 5px;
font-size: 9px;
}
.legend {
top: 82px;
right: auto;
left: 14px;
display: flex;
width: auto;
padding: 7px;
gap: 7px;
border-radius: 10px;
}
.legend-title,
.legend-row small,
.legend-actions {
display: none;
}
.legend-row {
display: flex;
gap: 5px;
padding: 4px 5px;
}
.legend-row + .legend-row {
padding-left: 8px;
border-top: 0;
border-left: 1px solid rgba(255, 255, 255, 0.1);
}
.legend-dot {
width: 7px;
height: 7px;
}
.legend-row strong {
font-size: 9px;
}
.material-card {
width: 166px;
padding: 9px 10px;
border-radius: 9px;
}
.badge {
padding: 3px 5px;
font-size: 7px;
}
.material-name {
margin-top: 6px;
font-size: 12px;
}
.formula {
font-size: 20px;
}
.material-explain,
.atom-row,
.ratio {
display: none;
}
.definitions {
display: none;
}
.instructions {
right: 10px;
bottom: 10px;
max-width: calc(100vw - 20px);
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<svg id="leaders" aria-hidden="true">
<g id="leader-layer"></g>
</svg>
<div id="labels"></div>
<header class="header">
<div class="eyebrow">Voxel chemistry · Material library</div>
<h1>What is the material?</h1>
<p>
Explore familiar objects made from voxels. Select an object to identify
whether it is a pure element, a pure compound, or a mixture.
</p>
</header>
<aside class="legend" aria-label="Material classification key">
<div class="legend-title">Classification key</div>
<div class="legend-row">
<span class="legend-dot" style="color:#5ee6c4;background:#5ee6c4"></span>
<strong>Element</strong>
<small>One kind of atom</small>
</div>
<div class="legend-row">
<span class="legend-dot" style="color:#a78bfa;background:#a78bfa"></span>
<strong>Compound</strong>
<small>Fixed chemical ratio</small>
</div>
<div class="legend-row">
<span class="legend-dot" style="color:#ffc857;background:#ffc857"></span>
<strong>Mixture</strong>
<small>Variable composition</small>
</div>
<div class="legend-actions">
<button id="reset" type="button" aria-label="Reset the camera view">
↺ Reset view
</button>
</div>
</aside>
<section class="definitions" aria-label="Definitions">
<div
class="definition"
style="--definition-color:#5ee6c4"
>
<strong>Pure element</strong>
<span>One type of atom</span>
<small>Example: copper, Cu</small>
</div>
<div
class="definition"
style="--definition-color:#a78bfa"
>
<strong>Pure compound</strong>
<span>Two or more atom types in a fixed ratio</span>
<small>Examples: silica, SiO₂ and salt, NaCl</small>
</div>
<div
class="definition"
style="--definition-color:#ffc857"
>
<strong>Mixture</strong>
<span>Multiple substances with variable amounts</span>
<small>Example: sea water</small>
</div>
</section>
<div class="instructions">
Drag to orbit · Scroll or pinch to zoom · Click a voxel · Arrow keys / R
</div>
<div id="selection-status" class="sr-only" aria-live="polite"></div>
<div id="loading">Building voxel materials…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const labelHost = document.getElementById("labels");
const leaderLayer = document.getElementById("leader-layer");
const selectionStatus = document.getElementById("selection-status");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 16, 31);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional voxel chemistry scene"
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-7, 7, 4, -4, 0.1, 100);
const cameraState = {
target: new THREE.Vector3(0, 0.65, 0.65),
radius: 14,
theta: 0.64,
phi: 0.96,
zoom: 1
};
const initialCamera = { ...cameraState };
const hemisphere = new THREE.HemisphereLight(
0xbfe7ff,
0x172033,
1.9
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x55dfff, 18, 16, 2);
cyanLight.position.set(-7, 4, -5);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9b7cff, 13, 14, 2);
violetLight.position.set(6, 3, 5);
scene.add(violetLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x091725,
roughness: 0.96,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.135;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x29445e, 0x183148);
grid.position.y = -0.13;
grid.material.transparent = true;
grid.material.opacity = 0.36;
scene.add(grid);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeEdges = new THREE.EdgesGeometry(cubeGeometry);
const pickMeshes = [];
const trackedMaterials = new Map();
const objectGroups = new Map();
const floatingVoxels = [];
const definitions = [
{
id: "copper",
name: "Copper wire",
kind: "element",
badge: "PURE ELEMENT",
formula: "Cu",
explain: "Bare copper contains only one type of atom.",
atoms: [["Cu", "#ff8a4d"]],
ratio: "Composition: copper atoms only",
accent: "#5ee6c4",
tint: "rgba(94,230,196,0.13)",
world: [-3.15, 0, -0.35],
baseSize: [3.45, 1.65],
anchor: [0, 0.95, 0],
offset: [-220, -68]
},
{
id: "silica",
name: "Silica glass",
kind: "compound",
badge: "PURE COMPOUND",
formula: "SiO₂",
explain: "Silicon and oxygen are chemically bonded in a fixed ratio.",
atoms: [["Si", "#f4c95d"], ["O", "#ff6b6b"]],
ratio: "Atom ratio: 1 Si : 2 O",
accent: "#a78bfa",
tint: "rgba(167,139,250,0.14)",
world: [-0.65, 0, -1.65],
baseSize: [3.05, 2.45],
anchor: [0, 1.98, 0],
offset: [-108, -168]
},
{
id: "salt",
name: "Salt crystals",
kind: "compound",
badge: "PURE COMPOUND",
formula: "NaCl",
explain: "Sodium and chloride form an ionic compound.",
atoms: [["Na", "#8f8cff"], ["Cl", "#65d46e"]],
ratio: "Ion ratio: 1 Na : 1 Cl",
accent: "#a78bfa",
tint: "rgba(167,139,250,0.14)",
world: [2.75, 0, -0.25],
baseSize: [2.65, 2.45],
anchor: [0, 1.18, 0],
offset: [205, -24]
},
{
id: "seawater",
name: "Sea water",
kind: "mixture",
badge: "MIXTURE",
formula: "H₂O + salts",
explain: "Water contains dissolved substances in variable amounts.",
atoms: [["H₂O", "#69c7ff"], ["salts", "#ffd166"]],
ratio: "No fixed ratio between the substances",
accent: "#ffc857",
tint: "rgba(255,200,87,0.13)",
world: [0.65, 0, 2.65],
baseSize: [3.0, 2.35],
anchor: [0, 1.98, 0],
offset: [190, 72]
}
];
function trackMaterial(objectId, material) {
if (!trackedMaterials.has(objectId)) {
trackedMaterials.set(objectId, []);
}
trackedMaterials.get(objectId).push(material);
return material;
}
function voxel(
parent,
objectId,
x,
y,
z,
sx,
sy,
sz,
material,
options = {}
) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.edgeMaterial) {
const edges = new THREE.LineSegments(
cubeEdges,
options.edgeMaterial
);
edges.renderOrder = 2;
mesh.add(edges);
}
parent.add(mesh);
if (options.pickable !== false) {
mesh.userData.objectId = objectId;
pickMeshes.push(mesh);
}
return mesh;
}
function createObjectBase(definition) {
const group = new THREE.Group();
group.position.fromArray(definition.world);
const [width, depth] = definition.baseSize;
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x12263a,
roughness: 0.78,
metalness: 0.12
});
voxel(
group,
definition.id,
0,
-0.045,
0,
width,
0.17,
depth,
platformMaterial,
{ pickable: false, castShadow: false }
);
const stripeMaterial = new THREE.MeshBasicMaterial({
color: definition.accent,
transparent: true,
opacity: 0.78
});
voxel(
group,
definition.id,
-width * 0.23,
0.049,
depth / 2 - 0.035,
width * 0.44,
0.022,
0.055,
stripeMaterial,
{ pickable: false, castShadow: false }
);
const anchor = new THREE.Object3D();
anchor.position.fromArray(definition.anchor);
group.add(anchor);
definition.anchorObject = anchor;
definition.group = group;
objectGroups.set(definition.id, group);
scene.add(group);
return group;
}
// Copper wire: a voxelized coil.
{
const definition = definitions[0];
const group = createObjectBase(definition);
const copper = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0xd87332,
metalness: 0.78,
roughness: 0.27
})
);
const copperLight = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0xf39a52,
metalness: 0.72,
roughness: 0.3
})
);
const copperEdges = new THREE.LineBasicMaterial({
color: 0x6e2813,
transparent: true,
opacity: 0.5
});
for (let i = 0; i < 78; i++) {
const t = i / 77;
const angle = t * Math.PI * 6;
const x = -1.38 + t * 2.76;
const y = 0.36 + Math.cos(angle) * 0.23;
const z = Math.sin(angle) * 0.34;
voxel(
group,
definition.id,
x,
y,
z,
0.245,
0.245,
0.245,
i % 5 === 0 ? copperLight : copper,
{ edgeMaterial: copperEdges }
);
}
}
// Silica glass: a translucent block assembled from glass voxels.
{
const definition = definitions[1];
const group = createObjectBase(definition);
const glassMaterials = [
new THREE.MeshPhysicalMaterial({
color: 0x87d9ef,
roughness: 0.16,
metalness: 0,
transparent: true,
opacity: 0.5,
depthWrite: false,
side: THREE.DoubleSide
}),
new THREE.MeshPhysicalMaterial({
color: 0xa6e8f7,
roughness: 0.2,
metalness: 0,
transparent: true,
opacity: 0.43,
depthWrite: false,
side: THREE.DoubleSide
}),
new THREE.MeshPhysicalMaterial({
color: 0x6fc9e6,
roughness: 0.14,
metalness: 0,
transparent: true,
opacity: 0.47,
depthWrite: false,
side: THREE.DoubleSide
})
];
glassMaterials.forEach((material) =>
trackMaterial(definition.id, material)
);
const glassEdges = new THREE.LineBasicMaterial({
color: 0xc5f5ff,
transparent: true,
opacity: 0.44
});
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 4; y++) {
for (let z = 0; z < 3; z++) {
voxel(
group,
definition.id,
(x - 2) * 0.47,
0.27 + y * 0.47,
(z - 1) * 0.47,
0.43,
0.43,
0.43,
glassMaterials[(x + y + z) % glassMaterials.length],
{
edgeMaterial: glassEdges,
castShadow: false,
receiveShadow: false
}
);
}
}
}
}
// Salt: a stepped pile of cubic crystals.
{
const definition = definitions[2];
const group = createObjectBase(definition);
const salt = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0xf2f3ee,
roughness: 0.72,
metalness: 0
})
);
const saltCool = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0xdde9ee,
roughness: 0.78,
metalness: 0
})
);
const saltEdges = new THREE.LineBasicMaterial({
color: 0x8194a0,
transparent: true,
opacity: 0.35
});
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
const distance = Math.max(Math.abs(x), Math.abs(z));
const levels = Math.max(0, 3 - distance);
for (let y = 0; y < levels; y++) {
voxel(
group,
definition.id,
x * 0.37,
0.24 + y * 0.35,
z * 0.37,
0.335,
0.335,
0.335,
(x + z + y) % 3 === 0 ? saltCool : salt,
{ edgeMaterial: saltEdges }
);
}
}
}
voxel(
group,
definition.id,
0.88,
0.22,
0.72,
0.3,
0.3,
0.3,
salt,
{ edgeMaterial: saltEdges }
);
}
// Sea water: a voxel tank containing water blocks and dissolved ions.
{
const definition = definitions[3];
const group = createObjectBase(definition);
const glass = trackMaterial(
definition.id,
new THREE.MeshPhysicalMaterial({
color: 0xb8f3ff,
roughness: 0.08,
metalness: 0,
transparent: true,
opacity: 0.2,
depthWrite: false,
side: THREE.DoubleSide
})
);
const water = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0x299ed0,
roughness: 0.25,
metalness: 0,
transparent: true,
opacity: 0.31,
depthWrite: false
})
);
const waterLight = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: 0x55c5ea,
roughness: 0.3,
metalness: 0,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
// Tank bottom.
voxel(
group,
definition.id,
0,
0.09,
0,
2.4,
0.09,
1.9,
glass,
{ castShadow: false, receiveShadow: false }
);
// Side, front, and back panels.
voxel(group, definition.id, -1.17, 0.91, 0, 0.06, 1.72, 1.9, glass, {
castShadow: false,
receiveShadow: false
});
voxel(group, definition.id, 1.17, 0.91, 0, 0.06, 1.72, 1.9, glass, {
castShadow: false,
receiveShadow: false
});
voxel(group, definition.id, 0, 0.91, -0.92, 2.4, 1.72, 0.06, glass, {
castShadow: false,
receiveShadow: false
});
voxel(group, definition.id, 0, 0.91, 0.92, 2.4, 1.72, 0.06, glass, {
castShadow: false,
receiveShadow: false
});
// Voxelized water volume.
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 4; y++) {
for (let z = 0; z < 4; z++) {
voxel(
group,
definition.id,
(x - 2) * 0.41,
0.31 + y * 0.38,
(z - 1.5) * 0.4,
0.36,
0.36,
0.36,
(x + y + z) % 4 === 0 ? waterLight : water,
{
castShadow: false,
receiveShadow: false
}
);
}
}
}
// Tank rim.
const rim = new THREE.MeshStandardMaterial({
color: 0x78b9cc,
roughness: 0.4,
metalness: 0.12
});
voxel(group, definition.id, 0, 1.79, -0.92, 2.42, 0.07, 0.07, rim);
voxel(group, definition.id, 0, 1.79, 0.92, 2.42, 0.07, 0.07, rim);
voxel(group, definition.id, -1.17, 1.79, 0, 0.07, 0.07, 1.9, rim);
voxel(group, definition.id, 1.17, 1.79, 0, 0.07, 0.07, 1.9, rim);
const ionColors = [0xfff0a6, 0xf39cff, 0xffffff, 0x9fffd1];
const ionPositions = [
[-0.74, 0.65, -0.48],
[-0.29, 1.13, 0.37],
[0.15, 0.79, -0.32],
[0.68, 1.31, 0.12],
[-0.91, 1.37, 0.41],
[0.38, 0.47, 0.58],
[0.87, 0.61, -0.51],
[-0.48, 0.34, 0.54],
[0.02, 1.52, -0.18],
[-0.04, 0.96, 0.11],
[0.92, 1.06, 0.58],
[-0.95, 0.88, -0.05]
];
ionPositions.forEach((position, index) => {
const material = trackMaterial(
definition.id,
new THREE.MeshStandardMaterial({
color: ionColors[index % ionColors.length],
roughness: 0.38,
metalness: 0.05,
emissive: ionColors[index % ionColors.length],
emissiveIntensity: 0.08
})
);
const ion = voxel(
group,
definition.id,
position[0],
position[1],
position[2],
0.095,
0.095,
0.095,
material,
{ castShadow: false }
);
floatingVoxels.push({
mesh: ion,
baseY: position[1],
phase: index * 0.73
});
});
}
// A square, voxel-style selection marker.
const selectionMarker = new THREE.Group();
const selectionMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0x5ee6c4,
transparent: true,
opacity: 0.8,
depthWrite: false
});
for (let i = 0; i < 4; i++) {
const bar = new THREE.Mesh(cubeGeometry, selectionMarkerMaterial);
bar.userData.side = i;
selectionMarker.add(bar);
}
scene.add(selectionMarker);
function updateSelectionMarker(definition) {
const [width, depth] = definition.baseSize;
const group = selectionMarker.children;
group[0].position.set(0, 0.065, depth / 2);
group[0].scale.set(width, 0.035, 0.045);
group[1].position.set(0, 0.065, -depth / 2);
group[1].scale.set(width, 0.035, 0.045);
group[2].position.set(-width / 2, 0.065, 0);
group[2].scale.set(0.045, 0.035, depth);
group[3].position.set(width / 2, 0.065, 0);
group[3].scale.set(0.045, 0.035, depth);
}
// DOM labels and SVG leader lines.
const labelItems = definitions.map((definition) => {
const card = document.createElement("button");
card.type = "button";
card.className = "material-card";
card.style.setProperty("--accent", definition.accent);
card.style.setProperty("--tint", definition.tint);
card.setAttribute(
"aria-label",
`${definition.name}, ${definition.badge}, formula ${definition.formula}`
);
const atomChips = definition.atoms
.map(
([symbol, color]) =>
`<span class="atom-chip" style="--atom-color:${color}"><i></i>${symbol}</span>`
)
.join("");
card.innerHTML = `
<span class="badge">${definition.badge}</span>
<span class="material-name">${definition.name}</span>
<span class="formula">${definition.formula}</span>
<span class="material-explain">${definition.explain}</span>
<span class="atom-row">${atomChips}</span>
<span class="ratio">${definition.ratio}</span>
`;
labelHost.appendChild(card);
const line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
line.setAttribute("stroke", definition.accent);
line.setAttribute("stroke-width", "1.25");
line.setAttribute("stroke-opacity", "0.72");
line.setAttribute("vector-effect", "non-scaling-stroke");
leaderLayer.appendChild(line);
const dot = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle"
);
dot.setAttribute("r", "3");
dot.setAttribute("fill", definition.accent);
dot.setAttribute("fill-opacity", "0.9");
leaderLayer.appendChild(dot);
return {
definition,
card,
line,
dot,
width: 0,
height: 0
};
});
function measureLabels() {
for (const item of labelItems) {
item.width = item.card.offsetWidth;
item.height = item.card.offsetHeight;
}
}
let selectedId = "copper";
let hoveredId = null;
function setSelected(id) {
if (!objectGroups.has(id)) return;
selectedId = id;
for (const [materialId, materials] of trackedMaterials) {
const definition = definitions.find(
(item) => item.id === materialId
);
const selected = materialId === id;
for (const material of materials) {
material.emissive.set(
selected ? definition.accent : 0x000000
);
material.emissiveIntensity = selected ? 0.17 : 0;
}
}
for (const item of labelItems) {
item.card.classList.toggle(
"selected",
item.definition.id === id
);
}
const definition = definitions.find((item) => item.id === id);
const group = objectGroups.get(id);
selectionMarker.position.set(group.position.x, 0, group.position.z);
selectionMarkerMaterial.color.set(definition.accent);
updateSelectionMarker(definition);
selectionStatus.textContent =
`${definition.name}: ${definition.badge}, ${definition.formula}. ` +
definition.explain;
}
function setHovered(id) {
if (hoveredId === id) return;
hoveredId = id;
for (const item of labelItems) {
item.card.classList.toggle(
"hovered",
item.definition.id === id
);
}
}
for (const item of labelItems) {
item.card.addEventListener("click", () => {
setSelected(item.definition.id);
});
item.card.addEventListener("mouseenter", () => {
setHovered(item.definition.id);
});
item.card.addEventListener("mouseleave", () => {
setHovered(null);
});
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * cosPhi,
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function updateProjection() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewWidth = 12.8;
const viewHeight = Math.max(8.4, viewWidth / aspect);
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resetView() {
cameraState.target.copy(initialCamera.target);
cameraState.theta = initialCamera.theta;
cameraState.phi = initialCamera.phi;
cameraState.zoom = initialCamera.zoom;
updateCamera();
}
document.getElementById("reset").addEventListener("click", resetView);
function cardEdgePoint(rectangle, targetX, targetY) {
const centerX = rectangle.x + rectangle.width / 2;
const centerY = rectangle.y + rectangle.height / 2;
const deltaX = targetX - centerX;
const deltaY = targetY - centerY;
let edgeX = centerX;
let edgeY = centerY;
const horizontalRatio = Math.abs(deltaX) / rectangle.width;
const verticalRatio = Math.abs(deltaY) / rectangle.height;
if (horizontalRatio > verticalRatio) {
edgeX = centerX + Math.sign(deltaX || 1) * rectangle.width / 2;
edgeY = centerY + deltaY;
edgeY = Math.max(
rectangle.y,
Math.min(rectangle.y + rectangle.height, edgeY)
);
} else {
edgeY = centerY + Math.sign(deltaY || 1) * rectangle.height / 2;
edgeX = centerX + deltaX;
edgeX = Math.max(
rectangle.x,
Math.min(rectangle.x + rectangle.width, edgeX)
);
}
return { x: edgeX, y: edgeY };
}
const projectedPosition = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
const compact = width < 700;
const offsetFactor = compact ? 0.62 : 1;
const topLimit = compact ? 126 : 94;
const bottomMargin = compact ? 34 : 112;
const layouts = [];
for (const item of labelItems) {
item.definition.anchorObject.getWorldPosition(projectedPosition);
projectedPosition.project(camera);
const screenX =
(projectedPosition.x * 0.5 + 0.5) * width;
const screenY =
(-projectedPosition.y * 0.5 + 0.5) * height;
const visible =
projectedPosition.z >= -1 &&
projectedPosition.z <= 1;
let x = screenX + item.definition.offset[0] * offsetFactor;
let y = screenY + item.definition.offset[1] * offsetFactor;
x = Math.max(
10,
Math.min(width - item.width - 10, x)
);
y = Math.max(
topLimit,
Math.min(height - item.height - bottomMargin, y)
);
layouts.push({
item,
x,
y,
width: item.width,
height: item.height,
screenX,
screenY,
visible
});
}
// Reduce overlap between labels without detaching them from objects.
for (let pass = 0; pass < 3; pass++) {
for (let i = 0; i < layouts.length; i++) {
for (let j = 0; j < i; j++) {
const a = layouts[i];
const b = layouts[j];
const overlapX =
Math.min(a.x + a.width, b.x + b.width) -
Math.max(a.x, b.x);
const overlapY =
Math.min(a.y + a.height, b.y + b.height) -
Math.max(a.y, b.y);
if (overlapX > 0 && overlapY > 0) {
const shift = overlapY + 10;
a.y += a.y < b.y ? -shift : shift;
a.y = Math.max(
topLimit,
Math.min(
height - a.height - bottomMargin,
a.y
)
);
}
}
}
}
for (const layout of layouts) {
const { item, x, y, screenX, screenY, visible } = layout;
item.card.style.transform =
`translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)`;
item.card.style.visibility = visible ? "visible" : "hidden";
const edge = cardEdgePoint(layout, screenX, screenY);
item.line.setAttribute("x1", screenX);
item.line.setAttribute("y1", screenY);
item.line.setAttribute("x2", edge.x);
item.line.setAttribute("y2", edge.y);
item.line.style.display = visible ? "block" : "none";
item.dot.setAttribute("cx", screenX);
item.dot.setAttribute("cy", screenY);
item.dot.style.display = visible ? "block" : "none";
}
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickAt(clientX, clientY) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x =
((clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y =
-((clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
return hits.length ? hits[0].object.userData.objectId : null;
}
let dragState = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
theta: cameraState.theta,
phi: cameraState.phi,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (
dragState &&
dragState.pointerId === event.pointerId
) {
const deltaX = event.clientX - dragState.x;
const deltaY = event.clientY - dragState.y;
if (
Math.abs(deltaX) + Math.abs(deltaY) > 4
) {
dragState.moved = true;
}
cameraState.theta =
dragState.theta - deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
dragState.phi - deltaY * 0.006,
0.43,
1.38
);
return;
}
const id = pickAt(event.clientX, event.clientY);
setHovered(id);
renderer.domElement.style.cursor = id ? "pointer" : "grab";
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (
dragState &&
dragState.pointerId === event.pointerId
) {
if (!dragState.moved) {
const id = pickAt(event.clientX, event.clientY);
if (id) setSelected(id);
}
dragState = null;
}
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
dragState = null;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.25
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowLeft":
cameraState.theta += 0.08;
break;
case "ArrowRight":
cameraState.theta -= 0.08;
break;
case "ArrowUp":
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - 0.06,
0.43,
1.38
);
break;
case "ArrowDown":
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + 0.06,
0.43,
1.38
);
break;
case "+":
case "=":
cameraState.zoom = Math.min(
2.25,
cameraState.zoom * 1.08
);
break;
case "-":
cameraState.zoom = Math.max(
0.65,
cameraState.zoom / 1.08
);
break;
case "r":
case "R":
resetView();
break;
default:
return;
}
event.preventDefault();
});
function resize() {
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
updateProjection();
measureLabels();
}
window.addEventListener("resize", resize);
updateCamera();
updateProjection();
measureLabels();
setSelected(selectedId);
let firstFrame = true;
function animate(time) {
requestAnimationFrame(animate);
const seconds = time / 1000;
updateCamera();
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
for (const floating of floatingVoxels) {
floating.mesh.position.y =
floating.baseY +
Math.sin(seconds * 1.45 + floating.phase) * 0.025;
}
selectionMarkerMaterial.opacity =
0.57 + Math.sin(seconds * 2.7) * 0.2;
scene.updateMatrixWorld(true);
updateLabels();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
document.getElementById("loading")?.remove();
}
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```