Prompt specification
Build or customize the Three.js 3D scene "Four Material Samples With Explicit Labels" 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: Four Material Samples With Explicit Labels
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/four-material-samples-with-explicit-labels-x3989
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Display glass, paper, metal, and mixed-material sample objects with provided material labels. Do not infer composition from color or claim one sorting rule applies everywhere.
Working Three.js Starter (four-material-samples-with-explicit-labels-x3989):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Material Samples</title>
<style>
:root {
--ink: #172220;
--muted: #66716d;
--paper: rgba(250, 251, 248, 0.92);
--line: rgba(23, 34, 32, 0.14);
--accent: #315f58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 28%, #fbfcf8 0%, #e9eee8 50%, #dce3dc 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#scene canvas:focus-visible {
outline: 2px solid var(--accent);
outline-offset: -4px;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.header {
top: 26px;
left: 28px;
max-width: 430px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 39px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1;
}
.header p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
top: 26px;
right: 28px;
width: 210px;
pointer-events: auto;
}
.legend-heading {
margin: 0 0 10px 4px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.legend-list {
display: grid;
gap: 6px;
}
.legend button {
display: flex;
align-items: center;
width: 100%;
min-height: 42px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(250, 251, 248, 0.72);
color: var(--ink);
font: inherit;
font-size: 13px;
font-weight: 650;
text-align: left;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.legend button:hover,
.legend button:focus-visible {
transform: translateX(-3px);
border-color: rgba(49, 95, 88, 0.45);
outline: none;
}
.legend button.active {
border-color: rgba(49, 95, 88, 0.65);
background: rgba(245, 250, 247, 0.96);
}
.legend-index {
display: grid;
place-items: center;
width: 27px;
height: 27px;
margin-right: 10px;
border: 1px solid var(--line);
border-radius: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.panel {
bottom: 26px;
left: 28px;
width: min(360px, calc(100vw - 56px));
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 18px 55px rgba(34, 49, 44, 0.12);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.panel h2 {
margin: 4px 0 8px;
font-size: 25px;
font-weight: 720;
letter-spacing: -0.035em;
}
.panel-description {
min-height: 58px;
margin: 0 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.facts {
display: grid;
grid-template-columns: 92px 1fr;
gap: 8px 12px;
margin: 0;
padding-top: 14px;
border-top: 1px solid var(--line);
font-size: 12px;
line-height: 1.45;
}
.facts dt {
color: var(--muted);
font-weight: 700;
}
.facts dd {
margin: 0;
font-weight: 620;
}
.disclaimer {
margin: 15px 0 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10.5px;
line-height: 1.55;
}
.controls {
right: 28px;
bottom: 28px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(250, 251, 248, 0.76);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(12px);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e9eee8;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.04em;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.header {
top: 15px;
left: 15px;
}
.header p {
display: none;
}
.legend {
top: 74px;
right: 15px;
left: 15px;
width: auto;
}
.legend-heading {
display: none;
}
.legend-list {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
.legend button {
justify-content: center;
min-height: 36px;
padding: 6px 4px;
border-radius: 10px;
font-size: 10px;
text-align: center;
}
.legend-index {
display: none;
}
.panel {
bottom: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 15px;
}
.panel h2 {
font-size: 20px;
}
.panel-description {
min-height: 0;
margin-bottom: 10px;
}
.facts {
grid-template-columns: 78px 1fr;
}
.controls {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="hud header">
<div class="eyebrow">Explicit material labels</div>
<h1>Voxel material samples</h1>
<p>
Four separately labeled assemblies:
Glass, Paper, Metal, and Mixed materials.
</p>
</header>
<nav class="hud legend" aria-label="Material sample labels">
<div class="legend-heading">Provided labels</div>
<div class="legend-list">
<button type="button" data-sample="glass" aria-pressed="true">
<span class="legend-index">01</span>
<span>Glass</span>
</button>
<button type="button" data-sample="paper" aria-pressed="false">
<span class="legend-index">02</span>
<span>Paper</span>
</button>
<button type="button" data-sample="metal" aria-pressed="false">
<span class="legend-index">03</span>
<span>Metal</span>
</button>
<button type="button" data-sample="mixed" aria-pressed="false">
<span class="legend-index">04</span>
<span>Mixed materials</span>
</button>
</div>
</nav>
<section class="hud panel" aria-live="polite">
<div class="eyebrow">Selected sample</div>
<h2 id="sample-title">Glass</h2>
<p class="panel-description" id="sample-description"></p>
<dl class="facts">
<dt>Assigned label</dt>
<dd id="sample-label">Glass</dd>
<dt>Assembly</dt>
<dd id="sample-count">26 voxels</dd>
<dt>Treatment</dt>
<dd id="sample-treatment">Transparent, low roughness</dd>
</dl>
<p class="disclaimer">
Color is illustrative only. Material identity comes from the provided
labels; the left-to-right placement is for presentation, not a universal
sorting rule.
</p>
</section>
<div class="hud controls">
Drag to orbit · Scroll to zoom · Click a sample · Double-click to reset
</div>
<div id="loading">Building voxel samples…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneElement = document.querySelector("#scene");
const loadingElement = document.querySelector("#loading");
const sampleInfo = {
glass: {
title: "Glass",
description:
"This sample is explicitly labeled Glass and uses a transparent, " +
"low-roughness material treatment.",
count: "26 voxels",
treatment: "Transparent, low roughness"
},
paper: {
title: "Paper",
description:
"This sample is explicitly labeled Paper and uses a matte, " +
"non-metallic material treatment.",
count: "27 voxels",
treatment: "Matte, non-metallic"
},
metal: {
title: "Metal",
description:
"This sample is explicitly labeled Metal and uses a reflective, " +
"metallic material treatment.",
count: "27 voxels",
treatment: "Reflective, metallic"
},
mixed: {
title: "Mixed materials",
description:
"This sample is explicitly labeled Mixed materials. Its separately " +
"labeled components include Glass, Paper, and Metal.",
count: "32 voxels · Glass + Paper + Metal",
treatment: "Three labeled component treatments"
}
};
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D scene containing labeled Glass, Paper, Metal, and Mixed materials voxel samples."
);
sceneElement.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe8ede7, 19, 37);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
theta: 0.48,
phi: 1.02,
radius: window.innerWidth < 600 ? 32 : 15.5,
target: new THREE.Vector3(0, 1.35, 0),
userAdjusted: false
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
// A small procedural studio environment for metal reflections.
function createEnvironment() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 512;
const context = canvas.getContext("2d");
const gradient = context.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, "#596763");
gradient.addColorStop(0.35, "#e9eee9");
gradient.addColorStop(0.6, "#aab6b0");
gradient.addColorStop(1, "#26312f");
context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < 8; i++) {
const x = 35 + i * 128;
const panelGradient = context.createLinearGradient(x, 0, x + 65, 0);
panelGradient.addColorStop(0, "rgba(255,255,255,0)");
panelGradient.addColorStop(0.5, "rgba(255,255,255,0.82)");
panelGradient.addColorStop(1, "rgba(255,255,255,0)");
context.fillStyle = panelGradient;
context.fillRect(x, 75, 65, 285);
}
const texture = new THREE.CanvasTexture(canvas);
texture.mapping = THREE.EquirectangularReflectionMapping;
texture.colorSpace = THREE.SRGBColorSpace;
const pmrem = new THREE.PMREMGenerator(renderer);
const target = pmrem.fromEquirectangular(texture);
texture.dispose();
pmrem.dispose();
return target.texture;
}
scene.environment = createEnvironment();
const hemisphereLight = new THREE.HemisphereLight(
0xf7fff9,
0x87958e,
2.1
);
scene.add(hemisphereLight);
const keyLight = new THREE.DirectionalLight(0xfffbef, 3.3);
keyLight.position.set(-7, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 32;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcce9ef, 1.25);
fillLight.position.set(8, 6, -7);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffefce, 32, 24, 2);
rimLight.position.set(0, 7, 5);
scene.add(rimLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xdce2dc,
roughness: 0.96,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.012;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x8d9b94, 0xaeb8b2);
grid.position.y = 0.003;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
// Procedural paper bump texture.
function createPaperBumpTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
const image = context.createImageData(128, 128);
for (let i = 0; i < image.data.length; i += 4) {
const value = 175 + Math.floor(Math.random() * 75);
image.data[i] = value;
image.data[i + 1] = value;
image.data[i + 2] = value;
image.data[i + 3] = 255;
}
context.putImageData(image, 0, 0);
const texture = new THREE.CanvasTexture(canvas);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(5, 5);
return texture;
}
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa9d9df,
roughness: 0.07,
metalness: 0,
transparent: true,
opacity: 0.34,
transmission: 0.08,
thickness: 0.35,
ior: 1.45,
clearcoat: 0.65,
clearcoatRoughness: 0.12,
side: THREE.DoubleSide,
depthWrite: false,
envMapIntensity: 1.35
});
const paperMaterial = new THREE.MeshStandardMaterial({
color: 0xe8e1ce,
roughness: 0.96,
metalness: 0,
bumpMap: createPaperBumpTexture(),
bumpScale: 0.012,
envMapIntensity: 0.35
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xb7c0bd,
roughness: 0.19,
metalness: 0.94,
envMapIntensity: 1.7
});
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0xf2f3ee,
roughness: 0.72,
metalness: 0.03
});
const pedestalTrimMaterial = new THREE.MeshStandardMaterial({
color: 0x78908a,
roughness: 0.28,
metalness: 0.72
});
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const voxelEdgesGeometry = new THREE.EdgesGeometry(voxelGeometry);
const glassEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x4b8e98,
transparent: true,
opacity: 0.28,
depthWrite: false
});
const pickableMeshes = [];
const sampleRings = {};
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function createLabelSprite(text, compact = false) {
const canvas = document.createElement("canvas");
canvas.width = compact ? 420 : 680;
canvas.height = compact ? 120 : 180;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.shadowColor = "rgba(20, 36, 31, 0.16)";
context.shadowBlur = 18;
context.shadowOffsetY = 7;
roundedRect(
context,
8,
8,
canvas.width - 16,
canvas.height - 16,
compact ? 24 : 30
);
context.fillStyle = "rgba(252, 252, 248, 0.96)";
context.fill();
context.shadowColor = "transparent";
context.strokeStyle = "rgba(23, 34, 32, 0.18)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = "#315f58";
context.fillRect(30, 30, 8, canvas.height - 60);
context.fillStyle = "#66716d";
context.font = "700 19px system-ui, sans-serif";
context.letterSpacing = "2px";
context.fillText(
compact ? "COMPONENT LABEL" : "PROVIDED MATERIAL LABEL",
58,
compact ? 40 : 48
);
context.fillStyle = "#172220";
context.font = compact
? "700 42px system-ui, sans-serif"
: "700 56px system-ui, sans-serif";
context.fillText(text, 58, compact ? 91 : 124);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(
compact ? 1.35 : 2.5,
compact ? 0.386 : 0.662,
1
);
sprite.renderOrder = 20;
return sprite;
}
function addPedestal(x, key) {
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.38, 1.48, 0.32, 64),
pedestalMaterial
);
pedestal.position.set(x, 0.16, 0);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const trim = new THREE.Mesh(
new THREE.TorusGeometry(1.31, 0.018, 8, 96),
pedestalTrimMaterial
);
trim.rotation.x = Math.PI / 2;
trim.position.set(x, 0.326, 0);
scene.add(trim);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x315f58,
transparent: true,
opacity: key === "glass" ? 0.9 : 0.12,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.22, 0.024, 8, 96),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.337, 0);
scene.add(ring);
sampleRings[key] = ringMaterial;
}
function addVoxels(root, cells, material, key, options = {}) {
const pitch = options.pitch ?? 0.43;
const size = options.size ?? 0.38;
const xs = cells.map((cell) => cell[0]);
const ys = cells.map((cell) => cell[1]);
const zs = cells.map((cell) => cell[2]);
const centerX = (Math.min(...xs) + Math.max(...xs)) / 2;
const centerZ = (Math.min(...zs) + Math.max(...zs)) / 2;
for (const [x, y, z] of cells) {
const mesh = new THREE.Mesh(voxelGeometry, material);
mesh.scale.setScalar(size);
mesh.position.set(
(x - centerX) * pitch,
(y + 0.5) * pitch,
(z - centerZ) * pitch
);
mesh.castShadow = material !== glassMaterial;
mesh.receiveShadow = true;
mesh.userData.sampleKey = key;
root.add(mesh);
pickableMeshes.push(mesh);
if (material === glassMaterial) {
const edges = new THREE.LineSegments(
voxelEdgesGeometry,
glassEdgeMaterial
);
edges.scale.copy(mesh.scale);
edges.position.copy(mesh.position);
edges.renderOrder = 4;
root.add(edges);
}
}
}
function addLeader(root, bottomY, topY) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, bottomY, 0),
new THREE.Vector3(0, topY, 0)
]);
const material = new THREE.LineBasicMaterial({
color: 0x6c817a,
transparent: true,
opacity: 0.55
});
root.add(new THREE.Line(geometry, material));
}
function createSample(key, label, x) {
const root = new THREE.Group();
root.position.set(x, 0.36, 0);
root.userData.sampleKey = key;
scene.add(root);
addPedestal(x, key);
if (key === "glass") {
const cells = [];
// Hollow 3 × 3 × 3 voxel shell.
for (let xCell = 0; xCell < 3; xCell++) {
for (let y = 0; y < 3; y++) {
for (let z = 0; z < 3; z++) {
const isCenter =
xCell === 1 && y === 1 && z === 1;
if (!isCenter) {
cells.push([xCell, y, z]);
}
}
}
}
addVoxels(root, cells, glassMaterial, key);
const labelSprite = createLabelSprite("Glass");
labelSprite.position.set(0, 2.02, 0);
root.add(labelSprite);
addLeader(root, 1.53, 1.77);
}
if (key === "paper") {
const cells = [];
for (let x = 0; x < 3; x++) {
for (let y = 0; y < 3; y++) {
for (let z = 0; z < 3; z++) {
cells.push([x, y, z]);
}
}
}
addVoxels(root, cells, paperMaterial, key);
const labelSprite = createLabelSprite("Paper");
labelSprite.position.set(0, 2.02, 0);
root.add(labelSprite);
addLeader(root, 1.53, 1.77);
}
if (key === "metal") {
const cells = [];
for (let x = 0; x < 3; x++) {
for (let y = 0; y < 3; y++) {
for (let z = 0; z < 3; z++) {
cells.push([x, y, z]);
}
}
}
addVoxels(root, cells, metalMaterial, key);
const labelSprite = createLabelSprite("Metal");
labelSprite.position.set(0, 2.02, 0);
root.add(labelSprite);
addLeader(root, 1.53, 1.77);
}
if (key === "mixed") {
const glassCells = [];
const paperCells = [];
const metalCells = [];
// Four 2 × 2 × 2 component blocks, separated by a central aisle.
for (let quadrantX = 0; quadrantX < 2; quadrantX++) {
for (let quadrantZ = 0; quadrantZ < 2; quadrantZ++) {
for (let localX = 0; localX < 2; localX++) {
for (let localZ = 0; localZ < 2; localZ++) {
for (let y = 0; y < 2; y++) {
const cell = [
quadrantX * 3 + localX,
y,
quadrantZ * 3 + localZ
];
if (quadrantX === 0 && quadrantZ === 0) {
glassCells.push(cell);
} else if (quadrantX === 0 && quadrantZ === 1) {
metalCells.push(cell);
} else {
paperCells.push(cell);
}
}
}
}
}
}
addVoxels(root, glassCells, glassMaterial, key);
addVoxels(root, paperCells, paperMaterial, key);
addVoxels(root, metalCells, metalMaterial, key);
const mainLabel = createLabelSprite("Mixed materials");
mainLabel.position.set(0, 2.12, 0);
root.add(mainLabel);
const componentLabels = [
{
text: "Glass",
position: [-0.645, 1.35, -0.645]
},
{
text: "Paper",
position: [0.645, 1.35, -0.645]
},
{
text: "Metal",
position: [-0.645, 1.35, 0.645]
}
];
for (const component of componentLabels) {
const sprite = createLabelSprite(component.text, true);
sprite.position.set(...component.position);
sprite.renderOrder = 21;
root.add(sprite);
}
addLeader(root, 1.12, 1.87);
}
return root;
}
createSample("glass", "Glass", -4.8);
createSample("paper", "Paper", -1.6);
createSample("metal", "Metal", 1.6);
createSample("mixed", "Mixed materials", 4.8);
const titleElement = document.querySelector("#sample-title");
const descriptionElement = document.querySelector("#sample-description");
const labelElement = document.querySelector("#sample-label");
const countElement = document.querySelector("#sample-count");
const treatmentElement = document.querySelector("#sample-treatment");
const legendButtons = [
...document.querySelectorAll("[data-sample]")
];
function selectSample(key) {
const info = sampleInfo[key];
titleElement.textContent = info.title;
descriptionElement.textContent = info.description;
labelElement.textContent = info.title;
countElement.textContent = info.count;
treatmentElement.textContent = info.treatment;
for (const [ringKey, material] of Object.entries(sampleRings)) {
material.opacity = ringKey === key ? 0.9 : 0.12;
}
for (const button of legendButtons) {
const isActive = button.dataset.sample === key;
button.classList.toggle("active", isActive);
button.setAttribute("aria-pressed", String(isActive));
}
}
for (const button of legendButtons) {
button.addEventListener("click", () => {
selectSample(button.dataset.sample);
});
}
selectSample("glass");
// Direct pointer controls; no camera-control addons are used.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerActive = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let downX = 0;
let downY = 0;
let dragDistance = 0;
function selectFromPointer(event) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y =
-((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
pickableMeshes,
false
);
if (intersections.length > 0) {
const key = intersections[0].object.userData.sampleKey;
if (sampleInfo[key]) {
selectSample(key);
}
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerActive = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
downX = event.clientX;
downY = event.clientY;
dragDistance = 0;
renderer.domElement.setPointerCapture(pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive || event.pointerId !== pointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
dragDistance += Math.abs(deltaX) + Math.abs(deltaY);
controls.theta -= deltaX * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - deltaY * 0.005,
0.45,
1.42
);
controls.userAdjusted = true;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerActive || event.pointerId !== pointerId) return;
pointerActive = false;
renderer.domElement.style.cursor = "grab";
if (
dragDistance < 6 &&
Math.abs(event.clientX - downX) < 6 &&
Math.abs(event.clientY - downY) < 6
) {
selectFromPointer(event);
}
if (renderer.domElement.hasPointerCapture(pointerId)) {
renderer.domElement.releasePointerCapture(pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerActive = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
9,
38
);
controls.userAdjusted = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
controls.theta = 0.48;
controls.phi = 1.02;
controls.radius = window.innerWidth < 600 ? 32 : 15.5;
controls.userAdjusted = false;
updateCamera();
});
renderer.domElement.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") {
controls.theta += 0.12;
} else if (key === "arrowright") {
controls.theta -= 0.12;
} else if (key === "arrowup") {
controls.phi = Math.max(0.45, controls.phi - 0.08);
} else if (key === "arrowdown") {
controls.phi = Math.min(1.42, controls.phi + 0.08);
} else if (key === "+" || key === "=") {
controls.radius = Math.max(9, controls.radius - 1);
} else if (key === "-" || key === "_") {
controls.radius = Math.min(38, controls.radius + 1);
} else if (key === "0") {
controls.theta = 0.48;
controls.phi = 1.02;
controls.radius = window.innerWidth < 600 ? 32 : 15.5;
} else {
return;
}
event.preventDefault();
controls.userAdjusted = true;
updateCamera();
});
renderer.domElement.style.cursor = "grab";
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!controls.userAdjusted) {
controls.radius = width < 600 ? 32 : 15.5;
}
updateCamera();
}
window.addEventListener("resize", resize);
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loadingElement.classList.add("hidden");
setTimeout(() => loadingElement.remove(), 350);
});
</script>
</body>
</html>
```