Prompt specification
Build or customize the Three.js 3D scene "Hardness versus brittleness display" 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: Hardness versus brittleness display
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hardness-versus-brittleness-display-x1162
Scene Description Prompt:
Contrast scratch resistance and impact breakage using a stylized crystal and tougher block, without numerical test claims. Three.js from cdnjs, one HTML file.
Working Three.js Starter (hardness-versus-brittleness-display-x1162):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Material Response — Stylized Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(12, 24, 34, 0.78);
--line: rgba(196, 229, 238, 0.14);
--text: #eef8fa;
--muted: #91a8b2;
--cyan: #73e7ff;
--amber: #ffb66d;
--danger: #ff8b91;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 20% 35%, rgba(62, 201, 236, 0.08), transparent 34%),
radial-gradient(circle at 80% 35%, rgba(255, 166, 91, 0.07), transparent 34%);
z-index: 0;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.topbar {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
display: flex;
align-items: center;
gap: 14px;
pointer-events: none;
}
.mark {
width: 38px;
height: 38px;
border: 1px solid rgba(115, 231, 255, 0.42);
background: rgba(115, 231, 255, 0.07);
clip-path: polygon(50% 0, 92% 25%, 78% 84%, 22% 84%, 8% 25%);
position: relative;
}
.mark::after {
content: "";
position: absolute;
inset: 8px;
background: linear-gradient(145deg, var(--cyan), rgba(115, 231, 255, 0.15));
clip-path: inherit;
}
.topbar h1 {
margin: 0;
font-size: clamp(16px, 2vw, 22px);
line-height: 1;
letter-spacing: 0.13em;
font-weight: 720;
}
.topbar p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.05em;
}
.disclaimer {
position: fixed;
z-index: 5;
top: 27px;
right: 28px;
max-width: 280px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(8, 17, 25, 0.56);
backdrop-filter: blur(12px);
font-size: 11px;
letter-spacing: 0.03em;
text-align: right;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 4;
min-width: 154px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(6, 15, 23, 0.74);
backdrop-filter: blur(10px);
transform: translate(-50%, -100%);
pointer-events: none;
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.22);
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -19px;
width: 1px;
height: 18px;
background: linear-gradient(var(--cyan), transparent);
opacity: 0.55;
}
.world-label.block::after {
background: linear-gradient(var(--amber), transparent);
}
.world-label strong {
display: block;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.05em;
}
.interface {
position: fixed;
z-index: 6;
left: 28px;
right: 28px;
bottom: 24px;
display: grid;
grid-template-columns: minmax(220px, 1fr) auto minmax(220px, 1fr);
align-items: end;
gap: 18px;
pointer-events: none;
}
.sample-card,
.controls {
pointer-events: auto;
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.28);
}
.sample-card {
width: min(100%, 310px);
padding: 16px 17px;
border-radius: 15px;
}
.sample-card.right { justify-self: end; }
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.right .eyebrow { color: var(--amber); }
.sample-card h2 {
margin: 7px 0 4px;
font-size: 18px;
letter-spacing: 0.01em;
}
.sample-card .trait {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.result {
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.result span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.result strong {
display: block;
min-height: 18px;
font-size: 12px;
font-weight: 600;
line-height: 1.45;
}
.result strong[data-tone="fracture"] { color: var(--danger); }
.result strong[data-tone="resilient"] { color: var(--amber); }
.result strong[data-tone="active"] { color: var(--cyan); }
.controls {
width: 248px;
padding: 12px;
border-radius: 16px;
}
.control-label {
display: block;
margin: 0 0 9px 2px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border: 1px solid rgba(255, 255, 255, 0.06);
border-radius: 11px;
background: rgba(0, 0, 0, 0.18);
}
button {
border: 0;
color: var(--muted);
background: transparent;
font: inherit;
cursor: pointer;
transition: background 160ms ease, color 160ms ease, transform 160ms ease;
}
.segmented button {
padding: 9px 8px;
border-radius: 8px;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.05em;
}
.segmented button:hover { color: var(--text); }
.segmented button.active {
color: #071019;
background: var(--cyan);
box-shadow: 0 5px 16px rgba(115, 231, 255, 0.18);
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 7px;
margin-top: 8px;
}
.actions button {
min-height: 36px;
border-radius: 9px;
font-size: 11px;
font-weight: 700;
}
.primary {
color: #061018;
background: linear-gradient(135deg, #b9f3ff, var(--cyan));
}
.primary:hover { transform: translateY(-1px); }
.reset {
padding: 0 12px;
border: 1px solid var(--line);
}
.reset:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.06);
}
#hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(190, 211, 218, 0.64);
font-size: 10px;
letter-spacing: 0.045em;
white-space: nowrap;
pointer-events: none;
opacity: 0;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #071019;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 900px) {
.interface {
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.controls {
grid-column: 1 / -1;
grid-row: 1;
width: min(100%, 430px);
justify-self: center;
}
.sample-card {
width: 100%;
padding: 12px 14px;
}
.sample-card.right { justify-self: stretch; }
.disclaimer {
top: 85px;
left: 28px;
right: auto;
text-align: left;
}
#hint { display: none; }
}
@media (max-width: 540px) {
.topbar { top: 18px; left: 18px; }
.disclaimer { left: 18px; top: 75px; max-width: 260px; }
.interface { left: 14px; right: 14px; bottom: 14px; }
.sample-card h2 { font-size: 15px; }
.sample-card .trait { display: none; }
.result { margin-top: 9px; padding-top: 9px; }
.world-label { min-width: 125px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive stylized material comparison"></canvas>
<header class="topbar">
<div class="mark" aria-hidden="true"></div>
<div>
<h1>MATERIAL / RESPONSE</h1>
<p>Surface marking and fracture behavior</p>
</div>
</header>
<div class="disclaimer">Illustrative behavior only — not measured test performance</div>
<div id="crystalLabel" class="world-label">
<strong>Stylized crystal</strong>
<span>Surface-sensitive · brittle response</span>
</div>
<div id="blockLabel" class="world-label block">
<strong>Tougher block</strong>
<span>Scratch-resistant · resilient response</span>
</div>
<main class="interface">
<article class="sample-card">
<div class="eyebrow">Crystal specimen</div>
<h2>Faceted, brittle form</h2>
<p class="trait">Visible scoring and sudden fracture define its stylized response.</p>
<div class="result">
<span>Observed response</span>
<strong id="crystalResult">Ready for a surface pass</strong>
</div>
</article>
<section class="controls" aria-label="Test controls">
<span class="control-label">Choose stress mode</span>
<div class="segmented">
<button type="button" class="active" data-mode="scratch" aria-pressed="true">Scratch</button>
<button type="button" data-mode="impact" aria-pressed="false">Impact</button>
</div>
<div class="actions">
<button type="button" id="compare" class="primary">Run comparison</button>
<button type="button" id="reset" class="reset">Reset</button>
</div>
</section>
<article class="sample-card right">
<div class="eyebrow">Tougher block</div>
<h2>Chamfered composite form</h2>
<p class="trait">Subtle surface marks and elastic movement define its stylized response.</p>
<div class="result">
<span>Observed response</span>
<strong id="blockResult">Ready for a surface pass</strong>
</div>
</article>
</main>
<div id="hint"></div>
<div id="loading">Loading material study</div>
<script type="module">
const loading = document.getElementById("loading");
let THREE;
try {
THREE = await import(
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js"
);
} catch (error) {
loading.textContent = "Unable to load the Three.js core module";
throw error;
}
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.045);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const clock = new THREE.Clock();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const orbit = {
theta: 0.28,
phi: 1.19,
radius: 9.6,
target: new THREE.Vector3(0, 1.35, 0)
};
let mode = "scratch";
let cameraShake = 0;
let dragState = null;
let manualStroke = null;
let manualTipVisible = false;
const crystalResult = document.getElementById("crystalResult");
const blockResult = document.getElementById("blockResult");
const hint = document.getElementById("hint");
const modeButtons = [...document.querySelectorAll("[data-mode]")];
function updateHint() {
hint.textContent = mode === "scratch"
? "Drag across a specimen to scratch · Drag empty space to orbit · Scroll to zoom"
: "Click a specimen to strike it · Drag empty space to orbit · Scroll to zoom";
}
function setOutcome(sample, text, tone = "") {
sample.result.textContent = text;
sample.result.dataset.tone = tone;
}
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function makeCrystalGeometry(radius, height, sides = 6) {
const positions = [];
const colors = [];
const baseColor = new THREE.Color(0x6fe4ff);
const rings = [
{ y: 0.00, r: radius * 0.58 },
{ y: height * 0.14, r: radius },
{ y: height * 0.71, r: radius },
{ y: height * 0.86, r: radius * 0.64 }
];
const point = (ring, index) => {
const angle = Math.PI / 6 + (index / sides) * Math.PI * 2;
return new THREE.Vector3(
Math.cos(angle) * ring.r,
ring.y,
Math.sin(angle) * ring.r
);
};
const bottom = new THREE.Vector3(0, -height * 0.035, 0);
const tip = new THREE.Vector3(0, height, 0);
function triangle(a, b, c, shade) {
const color = baseColor.clone().offsetHSL(0, 0, shade);
for (const p of [a, b, c]) {
positions.push(p.x, p.y, p.z);
colors.push(color.r, color.g, color.b);
}
}
for (let i = 0; i < sides; i++) {
const next = (i + 1) % sides;
const shade = (Math.sin(i * 2.31) * 0.5 + Math.random() * 0.5 - 0.25) * 0.13;
triangle(bottom, point(rings[0], next), point(rings[0], i), shade - 0.04);
for (let r = 0; r < rings.length - 1; r++) {
const a = point(rings[r], i);
const b = point(rings[r], next);
const c = point(rings[r + 1], next);
const d = point(rings[r + 1], i);
triangle(a, b, c, shade);
triangle(a, c, d, shade - 0.035);
}
triangle(point(rings[3], i), point(rings[3], next), tip, shade + 0.035);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function makeCrystalAssembly() {
const group = new THREE.Group();
const mainGeometry = makeCrystalGeometry(0.72, 3.2, 6);
const mainMaterial = new THREE.MeshPhysicalMaterial({
color: 0x78eaff,
vertexColors: true,
transparent: true,
opacity: 0.88,
roughness: 0.13,
metalness: 0.04,
clearcoat: 1,
clearcoatRoughness: 0.1,
emissive: 0x082f3d,
emissiveIntensity: 0.55,
flatShading: true,
side: THREE.DoubleSide,
depthWrite: true
});
const main = new THREE.Mesh(mainGeometry, mainMaterial);
main.castShadow = true;
main.receiveShadow = true;
group.add(main);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mainGeometry, 18),
new THREE.LineBasicMaterial({
color: 0xd9fbff,
transparent: true,
opacity: 0.42
})
);
group.add(edges);
const core = new THREE.Mesh(
mainGeometry,
new THREE.MeshBasicMaterial({
color: 0x7cecff,
transparent: true,
opacity: 0.12,
depthWrite: false,
side: THREE.DoubleSide
})
);
core.scale.set(0.66, 0.72, 0.66);
core.position.y = 0.18;
group.add(core);
const sideGeometry = makeCrystalGeometry(0.35, 1.9, 5);
const sideMaterial = mainMaterial.clone();
sideMaterial.opacity = 0.78;
sideMaterial.emissiveIntensity = 0.35;
const left = new THREE.Mesh(sideGeometry, sideMaterial);
left.position.set(-0.48, 0.02, 0.08);
left.rotation.z = -0.13;
left.rotation.y = 0.18;
left.scale.setScalar(0.92);
left.castShadow = true;
group.add(left);
const leftEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(sideGeometry, 18),
new THREE.LineBasicMaterial({
color: 0xd9fbff,
transparent: true,
opacity: 0.28
})
);
leftEdges.position.copy(left.position);
leftEdges.rotation.copy(left.rotation);
leftEdges.scale.copy(left.scale);
group.add(leftEdges);
const right = new THREE.Mesh(sideGeometry, sideMaterial);
right.position.set(0.52, 0.01, -0.08);
right.rotation.z = 0.18;
right.rotation.y = -0.22;
right.scale.set(0.78, 0.82, 0.78);
right.castShadow = true;
group.add(right);
const rightEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(sideGeometry, 18),
new THREE.LineBasicMaterial({
color: 0xd9fbff,
transparent: true,
opacity: 0.25
})
);
rightEdges.position.copy(right.position);
rightEdges.rotation.copy(right.rotation);
rightEdges.scale.copy(right.scale);
group.add(rightEdges);
return {
group,
main,
sideMeshes: [left, right],
edgeMeshes: [edges, leftEdges, rightEdges]
};
}
function makePedestal(x) {
const group = new THREE.Group();
group.position.x = x;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.22, 1.34, 0.18, 64),
new THREE.MeshStandardMaterial({
color: 0x14242d,
roughness: 0.48,
metalness: 0.66
})
);
base.position.y = 0.09;
base.receiveShadow = true;
base.castShadow = true;
group.add(base);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.12, 0.018, 8, 96),
new THREE.MeshBasicMaterial({
color: 0x62dff7,
transparent: true,
opacity: 0.35
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.185;
group.add(ring);
scene.add(group);
}
function makeBlockAssembly() {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshPhysicalMaterial({
color: 0x30474a,
roughness: 0.28,
metalness: 0.48,
clearcoat: 0.8,
clearcoatRoughness: 0.2,
emissive: 0x000000,
emissiveIntensity: 0
});
const bodyGeometry = new THREE.ExtrudeGeometry(
roundedRectShape(1.85, 2.15, 0.16),
{
depth: 0.86,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.07,
bevelThickness: 0.07,
curveSegments: 10,
steps: 1
}
);
bodyGeometry.center();
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 1.15;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const panelGeometry = new THREE.ExtrudeGeometry(
roundedRectShape(1.43, 1.66, 0.1),
{
depth: 0.025,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.018,
bevelThickness: 0.018,
curveSegments: 8,
steps: 1
}
);
panelGeometry.center();
const panel = new THREE.Mesh(
panelGeometry,
new THREE.MeshStandardMaterial({
color: 0x1b2c31,
roughness: 0.38,
metalness: 0.5
})
);
panel.position.set(0, 1.17, 0.51);
panel.castShadow = true;
group.add(panel);
const grooveMaterial = new THREE.MeshStandardMaterial({
color: 0x0f2025,
roughness: 0.4,
metalness: 0.45
});
for (let i = 0; i < 3; i++) {
const groove = new THREE.Mesh(
new THREE.BoxGeometry(0.84, 0.024, 0.026),
grooveMaterial
);
groove.position.set(0, 0.88 + i * 0.31, 0.552);
group.add(groove);
}
const accent = new THREE.Mesh(
new THREE.BoxGeometry(0.065, 1.58, 0.03),
new THREE.MeshStandardMaterial({
color: 0xffa85f,
emissive: 0x6b2e0d,
emissiveIntensity: 0.42,
roughness: 0.32,
metalness: 0.35
})
);
accent.position.set(-0.69, 1.17, 0.557);
group.add(accent);
return { group, body, panel, bodyMaterial };
}
const crystalAssembly = makeCrystalAssembly();
const blockAssembly = makeBlockAssembly();
makePedestal(-2.05);
makePedestal(2.05);
const samples = {
crystal: {
key: "crystal",
root: new THREE.Group(),
intact: crystalAssembly.group,
result: crystalResult,
action: null,
manualStrokes: [],
impactTriggered: false,
flashAge: 99,
targetY: 3.35,
scratchZ: 0.735
},
block: {
key: "block",
root: new THREE.Group(),
visual: blockAssembly.group,
bodyMaterial: blockAssembly.bodyMaterial,
result: blockResult,
action: null,
manualStrokes: [],
impactTriggered: false,
flashAge: 99,
targetY: 2.34,
scratchZ: 0.56
}
};
samples.crystal.root.position.set(-2.05, 0.18, 0);
samples.block.root.position.set(2.05, 0.18, 0);
samples.crystal.root.add(samples.crystal.intact);
samples.block.root.add(samples.block.visual);
scene.add(samples.crystal.root, samples.block.root);
const shardMaterial = new THREE.MeshPhysicalMaterial({
color: 0x75e8ff,
transparent: true,
opacity: 0.86,
roughness: 0.16,
metalness: 0.05,
clearcoat: 0.9,
emissive: 0x073542,
emissiveIntensity: 0.45,
flatShading: true,
side: THREE.DoubleSide
});
const shards = [];
for (let i = 0; i < 15; i++) {
const mesh = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.43 + Math.random() * 0.15, 0),
shardMaterial
);
mesh.scale.set(
0.45 + Math.random() * 0.55,
0.7 + Math.random() * 1.45,
0.45 + Math.random() * 0.5
);
mesh.rotation.set(Math.random() * 3, Math.random() * 3, Math.random() * 3);
mesh.visible = false;
mesh.castShadow = true;
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 12),
new THREE.LineBasicMaterial({
color: 0xe2fcff,
transparent: true,
opacity: 0.28
})
);
mesh.add(edge);
mesh.userData.velocity = new THREE.Vector3();
mesh.userData.born = 0;
mesh.userData.floorY = 0.12;
shards.push(mesh);
samples.crystal.root.add(mesh);
}
const scratchCylinder = new THREE.CylinderGeometry(1, 1, 1, 7);
scratchCylinder.translate(0, 0.5, 0);
function createScratchTool(sample) {
const group = new THREE.Group();
const tip = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 18, 12),
new THREE.MeshBasicMaterial({ color: 0xdffaff })
);
group.add(tip);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.032, 0.045, 0.76, 12),
new THREE.MeshStandardMaterial({
color: 0xb9c8cb,
metalness: 0.86,
roughness: 0.2
})
);
shaft.rotation.x = Math.PI / 2;
shaft.position.z = 0.42;
group.add(shaft);
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.075, 0.014, 8, 24),
new THREE.MeshBasicMaterial({ color: 0x73e7ff })
);
collar.position.z = 0.08;
group.add(collar);
group.visible = false;
sample.root.add(group);
return group;
}
function createStriker(sample) {
const group = new THREE.Group();
const steel = new THREE.MeshStandardMaterial({
color: 0x9caeb3,
metalness: 0.92,
roughness: 0.19
});
const ball = new THREE.Mesh(new THREE.SphereGeometry(0.23, 24, 16), steel);
ball.castShadow = true;
group.add(ball);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.265, 0.023, 8, 32),
new THREE.MeshStandardMaterial({
color: 0xffad67,
emissive: 0x6b2f0e,
emissiveIntensity: 0.5,
metalness: 0.55,
roughness: 0.3
})
);
group.add(ring);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.09, 0.88, 12),
steel
);
stem.position.y = 0.62;
stem.castShadow = true;
group.add(stem);
group.visible = false;
sample.root.add(group);
return group;
}
function createFlash(sample) {
const mesh = new THREE.Mesh(
new THREE.RingGeometry(0.16, 0.24, 48),
new THREE.MeshBasicMaterial({
color: sample.key === "crystal" ? 0xbaf8ff : 0xffc182,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.y = sample.targetY;
mesh.visible = false;
sample.root.add(mesh);
return mesh;
}
function prepareScratchPath(sample) {
if (!sample.scratchSegments) {
const points = [];
const count = 30;
for (let i = 0; i <= count; i++) {
const t = i / count;
points.push(new THREE.Vector3(
-0.64 + t * 1.28,
1.28 + Math.sin(t * Math.PI * 3) * 0.035,
sample.scratchZ
));
}
const material = new THREE.MeshBasicMaterial({
color: sample.key === "crystal" ? 0xe8ffff : 0xb9d0d0,
transparent: true,
opacity: sample.key === "crystal" ? 0.92 : 0.18,
depthWrite: false
});
const segments = [];
for (let i = 0; i < count; i++) {
const a = points[i];
const b = points[i + 1];
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(scratchCylinder, material);
mesh.position.copy(a);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
const radius = sample.key === "crystal" ? 0.014 : 0.007;
mesh.scale.set(radius, length, radius);
mesh.visible = false;
mesh.renderOrder = 4;
sample.root.add(mesh);
segments.push(mesh);
}
sample.scratchSegments = segments;
sample.scratchPoints = points;
sample.scratchTool = createScratchTool(sample);
}
}
Object.values(samples).forEach(prepareScratchPath);
Object.values(samples).forEach((sample) => {
sample.striker = createStriker(sample);
sample.flash = createFlash(sample);
});
const manualTip = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 18, 12),
new THREE.MeshBasicMaterial({
color: 0xe9ffff,
transparent: true,
opacity: 0.9
})
);
manualTip.visible = false;
scene.add(manualTip);
const ambient = new THREE.HemisphereLight(0xb9eaff, 0x1b1712, 1.65);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xd9f7ff, 3.1);
keyLight.position.set(-4, 8, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffa45f, 2.5);
rimLight.position.set(6, 4, -4);
scene.add(rimLight);
const cyanLight = new THREE.PointLight(0x43dfff, 13, 9, 2);
cyanLight.position.set(-3.2, 2.8, 2.8);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xff9d51, 10, 8, 2);
amberLight.position.set(3.4, 2.4, 2.5);
scene.add(amberLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x08131c,
roughness: 0.78,
metalness: 0.24
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(26, 34, 0x2b5864, 0x17313b);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.15;
scene.add(grid);
const backdropRing = new THREE.Mesh(
new THREE.RingGeometry(4.15, 4.17, 128),
new THREE.MeshBasicMaterial({
color: 0x6bd9ef,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide
})
);
backdropRing.position.set(0, 2.2, -3.4);
scene.add(backdropRing);
const particlePositions = [];
for (let i = 0; i < 90; i++) {
particlePositions.push(
(Math.random() - 0.5) * 15,
0.4 + Math.random() * 5.5,
-4 + Math.random() * 7
);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x8eddeb,
size: 0.018,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
scene.add(particles);
function clearSample(sample) {
sample.action = null;
sample.impactTriggered = false;
sample.flashAge = 99;
sample.flash.visible = false;
sample.flash.material.opacity = 0;
sample.striker.visible = false;
if (sample.scratchTool) sample.scratchTool.visible = false;
if (sample.scratchSegments) {
sample.scratchSegments.forEach((segment) => segment.visible = false);
}
for (const stroke of sample.manualStrokes) {
sample.root.remove(stroke.line);
stroke.geometry.dispose();
}
sample.manualStrokes.length = 0;
if (sample.key === "crystal") {
samples.crystal.intact.visible = true;
shards.forEach((shard) => {
shard.visible = false;
shard.userData.velocity.set(0, 0, 0);
});
} else {
sample.visual.scale.set(1, 1, 1);
sample.visual.rotation.set(0, 0, 0);
sample.bodyMaterial.emissive.setHex(0x000000);
sample.bodyMaterial.emissiveIntensity = 0;
}
setOutcome(
sample,
mode === "scratch"
? "Ready for a surface pass"
: "Ready for an impact",
""
);
}
function resetScene() {
Object.values(samples).forEach(clearSample);
manualStroke = null;
manualTip.visible = false;
cameraShake = 0;
}
function startScratch(sample, delay = 0) {
clearSample(sample);
sample.action = {
type: "scratch",
start: clock.elapsedTime + delay,
done: false
};
setOutcome(sample, "Surface probe moving across the specimen…", "active");
}
function startImpact(sample, delay = 0) {
clearSample(sample);
sample.action = {
type: "impact",
start: clock.elapsedTime + delay,
done: false
};
setOutcome(sample, "Striker approaching the specimen…", "active");
}
function triggerImpactVisual(sample) {
if (sample.impactTriggered) return;
sample.impactTriggered = true;
sample.flashAge = 0;
sample.flash.visible = true;
cameraShake = sample.key === "crystal" ? 0.14 : 0.055;
if (sample.key === "crystal") {
sample.intact.visible = false;
shards.forEach((shard, index) => {
const angle = (index / shards.length) * Math.PI * 2 + Math.random() * 0.4;
const radius = Math.random() * 0.48;
shard.visible = true;
shard.position.set(
Math.cos(angle) * radius,
0.75 + Math.random() * 1.85,
Math.sin(angle) * radius * 0.65
);
shard.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
shard.userData.born = clock.elapsedTime;
shard.userData.velocity.set(
Math.cos(angle) * (0.9 + Math.random() * 1.6),
1.0 + Math.random() * 2.8,
Math.sin(angle) * (0.7 + Math.random() * 1.4)
);
});
setOutcome(sample, "Brittle fracture releases sharp shards", "fracture");
} else {
setOutcome(sample, "Body deforms, then remains intact", "resilient");
}
}
function easeInCubic(t) {
return t * t * t;
}
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
function updateSample(sample, elapsed, delta) {
const action = sample.action;
if (!action) return;
const t = elapsed - action.start;
if (t < 0) return;
if (action.type === "scratch") {
const duration = 2.45;
const progress = THREE.MathUtils.clamp(t / duration, 0, 1);
const eased = easeOutCubic(progress);
const pointIndex = eased * (sample.scratchPoints.length - 1);
const index = Math.min(sample.scratchPoints.length - 1, Math.floor(pointIndex));
const nextIndex = Math.min(sample.scratchPoints.length - 1, index + 1);
const local = pointIndex - index;
const point = sample.scratchPoints[index].clone().lerp(
sample.scratchPoints[nextIndex],
local
);
sample.scratchTool.visible = progress < 1;
sample.scratchTool.position.copy(point);
sample.scratchTool.position.z += 0.2;
const visibleSegments = Math.floor(progress * sample.scratchSegments.length);
sample.scratchSegments.forEach((segment, i) => {
segment.visible = i < visibleSegments;
});
if (progress >= 1 && !action.done) {
action.done = true;
sample.scratchTool.visible = false;
setOutcome(
sample,
sample.key === "crystal"
? "Fine scoring remains clearly visible"
: "Only a faint surface trace remains",
sample.key === "crystal" ? "fracture" : "resilient"
);
}
}
if (action.type === "impact") {
const travel = 0.72;
const startY = sample.targetY + 2.75;
sample.striker.visible = true;
if (t < travel) {
const p = easeInCubic(t / travel);
sample.striker.position.set(0.18, THREE.MathUtils.lerp(startY, sample.targetY + 0.18, p), 0);
} else {
if (!sample.impactTriggered) triggerImpactVisual(sample);
const retreat = THREE.MathUtils.clamp((t - travel - 0.08) / 0.62, 0, 1);
sample.striker.position.set(
0.18 + retreat * 0.8,
sample.targetY + 0.18 + retreat * 1.25,
retreat * 0.15
);
}
if (sample.key === "block") {
let squash = 0;
let sway = 0;
if (t >= travel && t < travel + 0.2) {
const p = (t - travel) / 0.2;
squash = -0.115 * Math.sin(p * Math.PI);
sway = 0.035 * Math.sin(p * Math.PI);
} else if (t >= travel + 0.2 && t < travel + 0.62) {
const p = (t - travel - 0.2) / 0.42;
squash = 0.035 * Math.sin(p * Math.PI);
sway = -0.018 * Math.sin(p * Math.PI);
}
sample.visual.scale.y = 1 + squash;
sample.visual.rotation.z = sway;
const flash = Math.max(0, 1 - Math.abs(t - travel) * 7);
sample.bodyMaterial.emissive.setHex(0xff9e58);
sample.bodyMaterial.emissiveIntensity = flash * 0.55;
}
if (t > 1.55 && !action.done) {
action.done = true;
sample.striker.visible = false;
if (sample.key === "block") {
sample.visual.scale.set(1, 1, 1);
sample.visual.rotation.set(0, 0, 0);
sample.bodyMaterial.emissiveIntensity = 0;
}
}
}
if (sample.flashAge < 0.42) {
sample.flashAge += delta;
const p = THREE.MathUtils.clamp(sample.flashAge / 0.42, 0, 1);
sample.flash.scale.setScalar(0.5 + p * 5.5);
sample.flash.material.opacity = (1 - p) * 0.9;
} else {
sample.flash.visible = false;
}
}
function updateShards(delta) {
for (const shard of shards) {
if (!shard.visible) continue;
const velocity = shard.userData.velocity;
velocity.y -= 5.7 * delta;
shard.position.addScaledVector(velocity, delta);
shard.rotation.x += delta * 1.6;
shard.rotation.y += delta * 1.15;
if (shard.position.y < shard.userData.floorY) {
shard.position.y = shard.userData.floorY;
velocity.y = Math.abs(velocity.y) * 0.25;
velocity.x *= 0.78;
velocity.z *= 0.78;
shard.rotation.z += velocity.x * delta * 0.8;
}
}
}
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
const shakeX = cameraShake ? (Math.random() - 0.5) * cameraShake : 0;
const shakeY = cameraShake ? (Math.random() - 0.5) * cameraShake : 0;
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta) + shakeX,
orbit.target.y + orbit.radius * Math.cos(orbit.phi) + shakeY,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const crystalLabel = document.getElementById("crystalLabel");
const blockLabel = document.getElementById("blockLabel");
const crystalAnchor = new THREE.Vector3(0, 3.72, 0);
const blockAnchor = new THREE.Vector3(0, 2.83, 0);
function updateLabels() {
positionLabel(crystalLabel, samples.crystal.root.localToWorld(crystalAnchor.clone()));
positionLabel(blockLabel, samples.block.root.localToWorld(blockAnchor.clone()));
}
function setMode(nextMode) {
mode = nextMode;
modeButtons.forEach((button) => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
resetScene();
updateHint();
}
modeButtons.forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
document.getElementById("compare").addEventListener("click", () => {
resetScene();
if (mode === "scratch") {
startScratch(samples.crystal, 0);
startScratch(samples.block, 0.08);
} else {
startImpact(samples.crystal, 0);
startImpact(samples.block, 0.08);
}
});
document.getElementById("reset").addEventListener("click", resetScene);
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 isVisible(object) {
let current = object;
while (current) {
if (!current.visible) return false;
current = current.parent;
}
return true;
}
function pickables() {
return [
crystalAssembly.main,
...crystalAssembly.sideMeshes,
blockAssembly.body,
blockAssembly.panel
].filter(isVisible);
}
function pick(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables(), false);
return hits.length ? hits[0] : null;
}
function createManualStroke(sample) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(512 * 3);
const attribute = new THREE.BufferAttribute(positions, 3);
attribute.setUsage(THREE.DynamicDrawUsage);
geometry.setAttribute("position", attribute);
geometry.setDrawRange(0, 0);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: sample.key === "crystal" ? 0xefffff : 0xb7cbcc,
transparent: true,
opacity: sample.key === "crystal" ? 0.88 : 0.17,
depthWrite: false
})
);
line.renderOrder = 5;
sample.root.add(line);
const stroke = {
sample,
geometry,
line,
points: [],
attribute
};
sample.manualStrokes.push(stroke);
return stroke;
}
function addManualPoint(stroke, worldPoint, worldNormal) {
const localPoint = stroke.sample.root.worldToLocal(worldPoint.clone());
const inverseRoot = new THREE.Matrix4().copy(stroke.sample.root.matrixWorld).invert();
const localNormal = worldNormal.clone().transformDirection(inverseRoot).normalize();
localPoint.addScaledVector(localNormal, 0.018);
const last = stroke.points[stroke.points.length - 1];
if (last && last.distanceToSquared(localPoint) < 0.00035) return;
stroke.points.push(localPoint);
const index = stroke.points.length - 1;
stroke.attribute.setXYZ(index, localPoint.x, localPoint.y, localPoint.z);
stroke.attribute.needsUpdate = true;
stroke.geometry.setDrawRange(0, stroke.points.length);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
const hit = pick(event);
if (hit && hit.object.userData.sampleKey) {
const sample = samples[hit.object.userData.sampleKey];
if (mode === "scratch") {
clearSample(sample);
manualStroke = createManualStroke(sample);
manualTipVisible = true;
const normal = hit.face
? hit.face.normal.clone().applyNormalMatrix(
new THREE.Matrix3().getNormalMatrix(hit.object.matrixWorld)
).normalize()
: new THREE.Vector3(0, 0, 1);
addManualPoint(manualStroke, hit.point, normal);
manualTip.position.copy(hit.point).addScaledVector(normal, 0.1);
manualTip.visible = true;
} else {
startImpact(sample);
}
return;
}
dragState = {
x: event.clientX,
y: event.clientY,
theta: orbit.theta,
phi: orbit.phi
};
});
canvas.addEventListener("pointermove", (event) => {
if (manualStroke) {
const hit = pick(event);
if (hit && hit.object.userData.sampleKey === manualStroke.sample.key) {
const normal = hit.face
? hit.face.normal.clone().applyNormalMatrix(
new THREE.Matrix3().getNormalMatrix(hit.object.matrixWorld)
).normalize()
: new THREE.Vector3(0, 0, 1);
addManualPoint(manualStroke, hit.point, normal);
manualTip.position.copy(hit.point).addScaledVector(normal, 0.1);
}
return;
}
if (dragState) {
orbit.theta = dragState.theta - (event.clientX - dragState.x) * 0.005;
orbit.phi = THREE.MathUtils.clamp(
dragState.phi - (event.clientY - dragState.y) * 0.0045,
0.58,
1.43
);
}
});
function endPointer() {
if (manualStroke && manualStroke.points.length > 1) {
setOutcome(
manualStroke.sample,
manualStroke.sample.key === "crystal"
? "Fine scoring remains clearly visible"
: "Only a faint surface trace remains",
manualStroke.sample.key === "crystal" ? "fracture" : "resilient"
);
}
manualStroke = null;
dragState = null;
manualTip.visible = false;
manualTipVisible = false;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", () => {
if (!dragState && !manualStroke) manualTip.visible = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.006,
6.8,
13.2
);
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateHint();
updateCamera();
loading.remove();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateSample(samples.crystal, elapsed, delta);
updateSample(samples.block, elapsed, delta);
updateShards(delta);
if (cameraShake > 0) cameraShake = Math.max(0, cameraShake - delta * 0.42);
particles.rotation.y += delta * 0.008;
backdropRing.rotation.z = Math.sin(elapsed * 0.08) * 0.025;
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```