Prompt specification
Build or customize the Three.js 3D scene "Scratch-test mineral sample holder" 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: Scratch-test mineral sample holder
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scratch-test-mineral-sample-holder-x1161
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a rack of differently shaped mineral specimens and a stylus over a replaceable test plate; identify hardness as scratch resistance rather than toughness. Low-poly.
Working Three.js Starter (scratch-test-mineral-sample-holder-x1161):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mineral Mechanics — The Scratch Test</title>
<style>
:root {
--ink: #eaf3f4;
--muted: #9fb2b8;
--cyan: #62d8d1;
--amber: #f3b45b;
--panel: rgba(13, 24, 32, 0.82);
--line: rgba(255, 255, 255, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101a22;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.eyebrow {
margin: 0 0 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 530px;
font-size: clamp(30px, 4.2vw, 58px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 480px;
margin: 16px 0 0;
color: #c2d0d4;
font-size: 15px;
line-height: 1.55;
}
.definition {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding: 8px 11px;
border: 1px solid rgba(98, 216, 209, 0.3);
border-radius: 999px;
background: rgba(98, 216, 209, 0.08);
color: #bff8f3;
font-size: 12px;
font-weight: 700;
}
.definition::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
.title-card {
position: absolute;
top: 28px;
left: 30px;
}
.reference {
position: absolute;
top: 28px;
right: 28px;
width: 238px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
}
.reference h2 {
margin: 0 0 4px;
font-size: 13px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.reference p {
margin: 0 0 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.scale {
display: grid;
gap: 8px;
}
.scale-row {
display: grid;
grid-template-columns: 74px 1fr 18px;
align-items: center;
gap: 8px;
color: #cad6d9;
font-size: 11px;
}
.track {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.track i {
display: block;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #4bafa9, var(--cyan));
}
.not-toughness {
margin-top: 15px !important;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #f3c47d !important;
}
.station {
position: absolute;
right: 28px;
bottom: 28px;
width: 270px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.station-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.station-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live-dot {
display: flex;
align-items: center;
gap: 6px;
color: #b8c7ca;
font-size: 10px;
}
.live-dot::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 10px rgba(243, 180, 91, 0.8);
content: "";
}
#plate-name {
margin: 7px 0 2px;
font-size: 20px;
font-weight: 750;
letter-spacing: -0.025em;
}
#plate-meta {
color: var(--muted);
font-size: 11px;
}
.actions {
display: flex;
gap: 8px;
margin-top: 14px;
}
button {
flex: 1;
min-height: 38px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 9px;
background: rgba(255, 255, 255, 0.07);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease,
border-color 160ms ease;
}
button:hover {
border-color: rgba(98, 216, 209, 0.45);
background: rgba(98, 216, 209, 0.12);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: rgba(98, 216, 209, 0.35);
background: rgba(98, 216, 209, 0.16);
color: #d7fffb;
}
button:disabled {
opacity: 0.45;
cursor: wait;
transform: none;
}
.help {
position: absolute;
bottom: 28px;
left: 50%;
max-width: min(610px, calc(100vw - 620px));
padding: 11px 16px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(13, 24, 32, 0.72);
color: #b7c5c9;
font-size: 11px;
line-height: 1.4;
text-align: center;
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
.help strong {
color: #e8f5f5;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #101a22;
color: var(--cyan);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.title-card {
top: 18px;
left: 18px;
}
.reference {
display: none;
}
.station {
right: 14px;
bottom: 14px;
width: 240px;
}
.help {
left: 14px;
bottom: 14px;
max-width: calc(100vw - 280px);
border-radius: 12px;
transform: none;
}
}
@media (max-width: 560px) {
h1 {
font-size: 34px;
}
.subtitle {
max-width: 320px;
font-size: 12px;
}
.definition {
font-size: 10px;
}
.station {
left: 14px;
right: 14px;
width: auto;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="overlay">
<section class="title-card">
<p class="eyebrow">Mineral Mechanics Laboratory</p>
<h1>The Scratch Test</h1>
<p class="subtitle">
Compare mineral specimens against a replaceable reference plate.
Drag the stylus across the plate to record a scratch.
</p>
<div class="definition">Hardness means resistance to scratching</div>
</section>
<aside class="reference">
<h2>Hardness scale</h2>
<p>Mohs values rank resistance to being scratched.</p>
<div class="scale">
<div class="scale-row">
<span>Quartz</span>
<span class="track"><i style="width:70%"></i></span>
<b>7</b>
</div>
<div class="scale-row">
<span>Feldspar</span>
<span class="track"><i style="width:60%"></i></span>
<b>6</b>
</div>
<div class="scale-row">
<span>Calcite</span>
<span class="track"><i style="width:30%"></i></span>
<b>3</b>
</div>
<div class="scale-row">
<span>Mica</span>
<span class="track"><i style="width:20%"></i></span>
<b>2</b>
</div>
</div>
<p class="not-toughness">
<strong>Hardness is not toughness.</strong><br>
It does not measure impact strength, fracture resistance, or the
ability to withstand crushing.
</p>
</aside>
<section class="station">
<div class="station-top">
<span class="station-label">Test station</span>
<span class="live-dot">Interactive</span>
</div>
<div id="plate-name">Ceramic tile</div>
<div id="plate-meta">Replaceable plate · scratches: 0</div>
<div class="actions">
<button id="replace" class="primary" type="button">Replace plate</button>
<button id="reset" type="button">Clear scratches</button>
</div>
</section>
<div class="help">
<strong>Drag on plate</strong> to scratch ·
<strong>Right-drag</strong> to orbit ·
<strong>Scroll</strong> to zoom ·
<strong>R</strong> replaces plate
</div>
</div>
<div id="loading">Preparing mineral laboratory…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101a22);
scene.fog = new THREE.Fog(0x101a22, 13, 28);
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.15;
document.getElementById("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb8e5ef, 0x382c25, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d8, 3.4);
keyLight.position.set(5, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const warmLight = new THREE.PointLight(0xffaa55, 30, 10, 2);
warmLight.position.set(2.8, 4.3, 2.5);
scene.add(warmLight);
const rackLight = new THREE.PointLight(0x65d8d1, 19, 8, 2);
rackLight.position.set(-4.5, 3.5, 1.5);
scene.add(rackLight);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const material = (color, roughness = 0.7, metalness = 0) =>
new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
});
const wood = material(0x493229, 0.78);
const darkWood = material(0x2e211d, 0.82);
const brass = material(0xc28b48, 0.34, 0.72);
const steel = material(0xbfc8c9, 0.24, 0.85);
const darkMetal = material(0x263238, 0.42, 0.66);
const orangeGrip = material(0xe68b3d, 0.56, 0.08);
const floorMaterial = material(0x17232b, 0.93);
const wallMaterial = material(0x1b2932, 0.95);
function addBox(parent, size, position, mat, shadows = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = shadows;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
segments,
position,
mat,
shadows = true
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = shadows;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Room and floor
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 60, 0x43606a, 0x29404a);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
const backWall = addBox(
scene,
[22, 9, 0.2],
[0, 4.5, -3.9],
wallMaterial,
false
);
const wallBand = addBox(
scene,
[22, 0.12, 0.24],
[0, 2.0, -3.76],
brass,
false
);
// -------------------------------------------------------------------------
// Canvas texture helpers
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, r) {
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 createLabelTexture(title, subtitle, accent = "#62d8d1") {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 150;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 4, 4, 632, 142, 20);
ctx.fillStyle = "rgba(14, 24, 30, 0.92)";
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.17)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(24, 25, 7, 100);
ctx.fillStyle = "#f1f7f7";
ctx.font = "700 43px system-ui, sans-serif";
ctx.fillText(title, 52, 67);
ctx.fillStyle = "#aebfc4";
ctx.font = "500 26px system-ui, sans-serif";
ctx.fillText(subtitle, 52, 108);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function createLabelPlane(title, subtitle, accent) {
const texture = createLabelTexture(title, subtitle, accent);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(1.55, 0.36),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
return mesh;
}
function createHardnessSign() {
const canvas = document.createElement("canvas");
canvas.width = 1200;
canvas.height = 360;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 4, 4, 1192, 352, 28);
ctx.fillStyle = "#12212a";
ctx.fill();
ctx.strokeStyle = "#62d8d1";
ctx.lineWidth = 5;
ctx.stroke();
ctx.fillStyle = "#f2b761";
ctx.font = "800 42px system-ui, sans-serif";
ctx.fillText("HARDNESS", 65, 85);
ctx.fillStyle = "#f4f9f9";
ctx.font = "800 68px system-ui, sans-serif";
ctx.fillText("SCRATCH RESISTANCE", 62, 174);
ctx.fillStyle = "#afc4c9";
ctx.font = "500 31px system-ui, sans-serif";
ctx.fillText("Resistance to being scratched — not impact strength", 65, 238);
ctx.fillStyle = "#62d8d1";
ctx.font = "700 27px system-ui, sans-serif";
ctx.fillText("HARDNESS ≠ TOUGHNESS", 65, 302);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(4.75, 1.43),
new THREE.MeshBasicMaterial({ map: texture })
);
sign.position.set(2.8, 4.25, -3.76);
scene.add(sign);
addBox(
scene,
[5.0, 1.65, 0.12],
[2.8, 4.25, -3.84],
darkWood,
false
);
}
createHardnessSign();
// -------------------------------------------------------------------------
// Mineral reference rack
// -------------------------------------------------------------------------
const rack = new THREE.Group();
scene.add(rack);
addBox(rack, [5.2, 4.18, 0.12], [-2.0, 2.4, -1.31], darkWood);
[-4.58, 0.58].forEach((x) => {
addBox(rack, [0.16, 4.3, 1.22], [x, 2.4, -0.72], wood);
});
[0.35, 1.72, 3.09, 4.46].forEach((y) => {
addBox(rack, [5.32, 0.14, 1.22], [-2.0, y, -0.72], wood);
});
addBox(rack, [0.13, 4.08, 1.13], [-2.0, 2.4, -0.72], wood);
const shelfLevels = [0.35, 1.72, 3.09];
const columnXs = [-3.28, -0.72];
function addPedestal(parent, x, y, z, radius = 0.55) {
addCylinder(
parent,
radius,
radius * 1.08,
0.07,
12,
[x, y + 0.035, z],
material(0x26363d, 0.82)
);
}
function makeQuartzCluster() {
const group = new THREE.Group();
const quartz = new THREE.MeshStandardMaterial({
color: 0xbceaf0,
roughness: 0.22,
metalness: 0.05,
transparent: true,
opacity: 0.86,
flatShading: true
});
const crystals = [
[-0.25, -0.08, 0.95, 0.16, -0.08],
[0.02, -0.02, 1.25, 0.19, 0.04],
[0.25, 0.08, 0.78, 0.13, 0.14],
[-0.04, 0.18, 0.68, 0.12, -0.08],
[0.34, -0.14, 0.52, 0.10, 0.22]
];
crystals.forEach(([x, z, h, r, lean]) => {
const crystal = new THREE.Group();
crystal.position.set(x, 0, z);
crystal.rotation.z = lean;
const body = new THREE.Mesh(
new THREE.CylinderGeometry(r * 0.88, r, h * 0.66, 6),
quartz
);
body.position.y = h * 0.33;
body.castShadow = true;
crystal.add(body);
const tip = new THREE.Mesh(
new THREE.ConeGeometry(r * 0.88, h * 0.34, 6),
quartz
);
tip.position.y = h * 0.66 + h * 0.17;
tip.castShadow = true;
crystal.add(tip);
group.add(crystal);
});
return group;
}
function makeCorundum() {
const group = new THREE.Group();
const red = material(0xa94035, 0.62, 0.03);
const barrel = new THREE.Mesh(
new THREE.CylinderGeometry(0.35, 0.47, 0.83, 7),
red
);
barrel.position.y = 0.44;
barrel.rotation.z = 0.08;
barrel.castShadow = true;
group.add(barrel);
const cap = new THREE.Mesh(
new THREE.ConeGeometry(0.35, 0.38, 7),
red
);
cap.position.set(-0.035, 1.03, 0);
cap.rotation.z = 0.08;
cap.castShadow = true;
group.add(cap);
const chip = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.21, 0),
material(0xc85a47, 0.7)
);
chip.position.set(0.39, 0.12, 0.15);
chip.castShadow = true;
group.add(chip);
return group;
}
function makeRhombohedron() {
const width = 0.9;
const height = 0.92;
const depth = 0.75;
const skew = 0.18;
const positions = new Float32Array([
-width / 2, 0, -depth / 2,
width / 2, 0, -depth / 2,
width / 2, 0, depth / 2,
-width / 2, 0, depth / 2,
-width / 2 + skew, height, -depth / 2,
width / 2 + skew, height, -depth / 2,
width / 2 + skew, height, depth / 2,
-width / 2 + skew, height, depth / 2
]);
const indices = [
0, 2, 1, 0, 3, 2,
4, 5, 6, 4, 6, 7,
0, 1, 5, 0, 5, 4,
1, 2, 6, 1, 6, 5,
2, 3, 7, 2, 7, 6,
3, 0, 4, 3, 4, 7
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
material(0xe0d8c5, 0.68)
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const group = new THREE.Group();
group.add(mesh);
return group;
}
function makeFeldspar() {
const group = new THREE.Group();
const pink = material(0xc78372, 0.88);
const main = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.58, 0),
pink
);
main.scale.set(1.18, 0.92, 0.82);
main.position.y = 0.49;
main.rotation.set(0.18, 0.28, -0.09);
main.castShadow = true;
group.add(main);
const chip = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.22, 0),
material(0xda9a87, 0.9)
);
chip.position.set(0.43, 0.18, 0.16);
chip.castShadow = true;
group.add(chip);
return group;
}
function makeFluorite() {
const group = new THREE.Group();
const green = new THREE.MeshStandardMaterial({
color: 0x65c987,
roughness: 0.3,
metalness: 0.04,
transparent: true,
opacity: 0.9,
flatShading: true
});
const main = new THREE.Mesh(
new THREE.BoxGeometry(0.76, 0.76, 0.76),
green
);
main.position.y = 0.43;
main.rotation.y = 0.2;
main.castShadow = true;
group.add(main);
const small = new THREE.Mesh(
new THREE.BoxGeometry(0.42, 0.42, 0.42),
green
);
small.position.set(0.43, 0.22, 0.18);
small.rotation.set(0.1, 0.4, 0.12);
small.castShadow = true;
group.add(small);
return group;
}
function makeMica() {
const group = new THREE.Group();
const gold = material(0xd3a758, 0.42, 0.25);
for (let i = 0; i < 7; i++) {
const layer = new THREE.Mesh(
new THREE.BoxGeometry(0.9 - i * 0.035, 0.035, 0.61 - i * 0.02),
gold
);
layer.position.set(
Math.sin(i * 1.8) * 0.035,
0.06 + i * 0.085,
Math.cos(i * 1.5) * 0.025
);
layer.rotation.y = (i - 3) * 0.06;
layer.castShadow = true;
layer.receiveShadow = true;
group.add(layer);
}
return group;
}
const specimenDefinitions = [
{
row: 2,
column: 0,
title: "QUARTZ",
subtitle: "Hardness 7 · pointed prisms",
accent: "#78d9ee",
create: makeQuartzCluster
},
{
row: 2,
column: 1,
title: "FELDSPAR",
subtitle: "Hardness 6 · blocky crystal",
accent: "#ee9a85",
create: makeFeldspar
},
{
row: 1,
column: 0,
title: "CORUNDUM",
subtitle: "Hardness 9 · barrel crystal",
accent: "#ee7566",
create: makeCorundum
},
{
row: 1,
column: 1,
title: "CALCITE",
subtitle: "Hardness 3 · rhombohedron",
accent: "#ead8b2",
create: makeRhombohedron
},
{
row: 0,
column: 0,
title: "FLUORITE",
subtitle: "Hardness 4 · cubic crystal",
accent: "#7bdf9a",
create: makeFluorite
},
{
row: 0,
column: 1,
title: "MICA",
subtitle: "Hardness 2 · layered sheets",
accent: "#edc96f",
create: makeMica
}
];
specimenDefinitions.forEach((definition) => {
const x = columnXs[definition.column];
const shelfY = shelfLevels[definition.row];
addPedestal(scene, x, shelfY + 0.07, -0.76);
const specimen = definition.create();
specimen.position.set(x, shelfY + 0.07, -0.76);
scene.add(specimen);
const label = createLabelPlane(
definition.title,
definition.subtitle,
definition.accent
);
label.position.set(x, shelfY + 0.15, -0.075);
scene.add(label);
});
// -------------------------------------------------------------------------
// Test bench and replaceable plates
// -------------------------------------------------------------------------
const bench = new THREE.Group();
scene.add(bench);
addBox(
bench,
[4.25, 0.28, 3.0],
[2.35, 0.43, 0.7],
material(0x35454c, 0.72, 0.18)
);
addBox(
bench,
[3.8, 0.08, 2.65],
[2.35, 0.61, 0.7],
material(0x506169, 0.62, 0.24)
);
for (const x of [0.58, 4.12]) {
for (const z of [-0.45, 1.82]) {
addCylinder(
bench,
0.12,
0.14,
0.52,
10,
[x, 0.23, z],
darkMetal
);
}
}
const plateBase = new THREE.Vector3(2.35, 0.72, 0.7);
const plateSpecs = [
{
name: "Ceramic tile",
meta: "Replaceable plate",
color: 0xd8cfbb,
roughness: 0.76,
metalness: 0.03,
scratch: 0x574f47
},
{
name: "Copper alloy",
meta: "Replaceable plate",
color: 0xb76f42,
roughness: 0.38,
metalness: 0.72,
scratch: 0x4c2418
},
{
name: "Acrylic panel",
meta: "Replaceable plate",
color: 0x58bdb8,
roughness: 0.25,
metalness: 0.05,
scratch: 0x174d50
}
];
const plateVariants = plateSpecs.map((spec, index) => {
const group = new THREE.Group();
group.position.copy(plateBase);
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: spec.color,
roughness: spec.roughness,
metalness: spec.metalness,
flatShading: true
});
const surface = new THREE.Mesh(
new THREE.BoxGeometry(2.45, 0.14, 1.62),
surfaceMaterial
);
surface.castShadow = true;
surface.receiveShadow = true;
group.add(surface);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.25
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(surface.geometry),
edgeMaterial
);
group.add(edges);
[
[-1.04, -0.62],
[1.04, -0.62],
[-1.04, 0.62],
[1.04, 0.62]
].forEach(([x, z]) => {
const screw = new THREE.Mesh(
new THREE.CylinderGeometry(0.047, 0.047, 0.035, 8),
brass
);
screw.position.set(x, 0.086, z);
screw.castShadow = true;
group.add(screw);
const slot = addBox(
group,
[0.055, 0.006, 0.012],
[x, 0.106, z],
darkMetal,
false
);
slot.rotation.y = index * 0.35;
});
const scratchRoot = new THREE.Group();
group.add(scratchRoot);
const scratchMaterial = new THREE.MeshBasicMaterial({
color: spec.scratch,
transparent: true,
opacity: 0.9
});
scene.add(group);
return {
group,
surface,
scratchRoot,
scratchMaterial,
spec
};
});
let activePlateIndex = 0;
let swapAnimation = null;
let scratchCount = 0;
function activePlate() {
return plateVariants[activePlateIndex];
}
function clearScratches(plate) {
while (plate.scratchRoot.children.length) {
const child = plate.scratchRoot.children.pop();
child.geometry?.dispose();
}
scratchCount = 0;
updatePlateUI();
}
function updatePlateUI() {
const plate = activePlate();
document.getElementById("plate-name").textContent = plate.spec.name;
document.getElementById("plate-meta").textContent =
`${plate.spec.meta} · scratches: ${scratchCount}`;
}
// -------------------------------------------------------------------------
// Stylus
// -------------------------------------------------------------------------
const stylus = new THREE.Group();
scene.add(stylus);
const tipGeometry = new THREE.ConeGeometry(0.085, 0.22, 8);
tipGeometry.rotateX(Math.PI);
const stylusTip = new THREE.Mesh(tipGeometry, steel);
stylusTip.position.y = 0.11;
stylusTip.castShadow = true;
stylus.add(stylusTip);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.055, 1.2, 10),
steel
);
shaft.position.y = 0.82;
shaft.castShadow = true;
stylus.add(shaft);
const grip = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.075, 0.67, 10),
orangeGrip
);
grip.position.y = 1.66;
grip.castShadow = true;
stylus.add(grip);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.092, 10, 6),
orangeGrip
);
cap.position.y = 2.0;
cap.castShadow = true;
stylus.add(cap);
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.083, 0.014, 6, 14),
brass
);
collar.rotation.x = Math.PI / 2;
collar.position.y = 1.32;
stylus.add(collar);
const stylusDirection = new THREE.Vector3(-0.3, 1, 0.32).normalize();
stylus.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
stylusDirection
);
const stylusAim = new THREE.Vector3(
plateBase.x - 0.2,
plateBase.y + 0.34,
plateBase.z + 0.22
);
stylus.position.copy(stylusAim);
// -------------------------------------------------------------------------
// Scratch geometry
// -------------------------------------------------------------------------
let scratchMode = false;
let lastScratchPoint = null;
function addScratchSegment(plate, a, b) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.012) return;
const scratch = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.006, 0.017),
plate.scratchMaterial
);
scratch.position.set(
(a.x + b.x) * 0.5,
0.076,
(a.z + b.z) * 0.5
);
scratch.rotation.y = -Math.atan2(dz, dx);
plate.scratchRoot.add(scratch);
scratchCount++;
updatePlateUI();
}
// -------------------------------------------------------------------------
// Direct camera controls
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.2, 2.35, 0);
const spherical = new THREE.Spherical(10.8, 1.16, 0.48);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
let dragState = null;
function updateCamera() {
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.62, 1.42);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 6.2, 16);
spherical.makeSafe();
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function plateIntersection(event) {
if (swapAnimation) return null;
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const plate = activePlate();
return raycaster.intersectObject(plate.surface, false)[0] || null;
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
const hit = event.button === 0 ? plateIntersection(event) : null;
if (hit) {
scratchMode = true;
lastScratchPoint = activePlate().group.worldToLocal(
hit.point.clone()
);
stylusAim.copy(
activePlate().group.localToWorld(
new THREE.Vector3(
lastScratchPoint.x,
0.078,
lastScratchPoint.z
)
)
);
canvas.style.cursor = "crosshair";
} else {
scratchMode = false;
dragState = {
mode: "orbit",
x: event.clientX,
y: event.clientY
};
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", (event) => {
if (scratchMode) {
const hit = plateIntersection(event);
if (!hit) return;
const point = activePlate().group.worldToLocal(
hit.point.clone()
);
if (lastScratchPoint) {
addScratchSegment(
activePlate(),
lastScratchPoint,
point
);
}
lastScratchPoint = point;
stylusAim.copy(
activePlate().group.localToWorld(
new THREE.Vector3(point.x, 0.078, point.z)
)
);
return;
}
if (dragState?.mode === "orbit") {
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.005;
dragState.x = event.clientX;
dragState.y = event.clientY;
updateCamera();
}
});
function endPointer(event) {
scratchMode = false;
lastScratchPoint = null;
dragState = null;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
updateCamera();
// -------------------------------------------------------------------------
// Plate replacement
// -------------------------------------------------------------------------
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function replacePlate() {
if (swapAnimation) return;
const oldPlate = plateVariants[activePlateIndex];
const nextIndex = (activePlateIndex + 1) % plateVariants.length;
const newPlate = plateVariants[nextIndex];
clearScratches(oldPlate);
clearScratches(newPlate);
activePlateIndex = nextIndex;
newPlate.group.visible = true;
newPlate.group.position.set(
plateBase.x + 3.2,
plateBase.y,
plateBase.z
);
swapAnimation = {
start: performance.now(),
duration: 1100,
oldPlate,
newPlate,
announced: false
};
const replaceButton = document.getElementById("replace");
replaceButton.disabled = true;
replaceButton.textContent = "Swapping…";
}
function updateSwap(now) {
if (!swapAnimation) return;
const raw = THREE.MathUtils.clamp(
(now - swapAnimation.start) / swapAnimation.duration,
0,
1
);
const eased = easeInOutCubic(raw);
const { oldPlate, newPlate } = swapAnimation;
oldPlate.group.position.set(
plateBase.x - 1.8 * eased,
plateBase.y - 1.25 * eased,
plateBase.z
);
oldPlate.group.rotation.z = -0.18 * eased;
const incoming = THREE.MathUtils.clamp((raw - 0.18) / 0.82, 0, 1);
const incomingEase = easeInOutCubic(incoming);
newPlate.group.position.set(
plateBase.x + 3.2 * (1 - incomingEase),
plateBase.y,
plateBase.z
);
newPlate.group.rotation.z = 0.12 * (1 - incomingEase);
if (raw > 0.42 && !swapAnimation.announced) {
swapAnimation.announced = true;
updatePlateUI();
}
if (raw >= 1) {
oldPlate.group.visible = false;
oldPlate.group.position.copy(plateBase);
oldPlate.group.rotation.set(0, 0, 0);
newPlate.group.position.copy(plateBase);
newPlate.group.rotation.set(0, 0, 0);
swapAnimation = null;
document.getElementById("replace").disabled = false;
document.getElementById("replace").textContent = "Replace plate";
updatePlateUI();
}
}
document
.getElementById("replace")
.addEventListener("click", replacePlate);
document
.getElementById("reset")
.addEventListener("click", () => {
clearScratches(activePlate());
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
replacePlate();
}
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
updateSwap(now);
if (!scratchMode) {
const localHover = new THREE.Vector3(
-0.18 + Math.sin(elapsed * 0.8) * 0.07,
0.31 + Math.sin(elapsed * 2.2) * 0.025,
0.2 + Math.cos(elapsed * 0.7) * 0.05
);
stylusAim.copy(
activePlate().group.localToWorld(localHover)
);
}
stylus.position.lerp(stylusAim, 0.16);
renderer.render(scene, camera);
}
updatePlateUI();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```