Prompt specification
Build or customize the Three.js 3D scene "Protected and exposed surface comparison" 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: Protected and exposed surface comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/protected-and-exposed-surface-comparison-x1942
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring metal corrosion and protective surfaces. Place matching metal samples with intact and scratched coating regions in a qualitative display; show access pathways for environmental agents without specifying hazardous chemicals or treatment procedures. A 3D cutaway view.
Working Three.js Starter (protected-and-exposed-surface-comparison-x1942):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Metal Surface Barrier — Qualitative 3D Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #07121b;
--panel: rgba(12, 29, 40, 0.78);
--panel-border: rgba(165, 218, 231, 0.16);
--text: #edf7fa;
--muted: #91aab5;
--cyan: #54d7ee;
--coating: #20a9bb;
--metal: #a9b6bd;
--rust: #c76c35;
--line: rgba(137, 209, 222, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 55% 25%, #173343 0%, #0a1822 42%, #061019 100%);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.header {
position: fixed;
z-index: 10;
top: 28px;
left: 32px;
max-width: min(520px, calc(100vw - 64px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 26px;
height: 1px;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 54px);
font-weight: 580;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 13px 0 0;
max-width: 430px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.scope-badge {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 15px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 22, 30, 0.62);
color: #b7cad2;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.scope-badge span {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
.key-panel,
.controls-panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(18px);
}
.key-panel {
left: 32px;
bottom: 30px;
width: 245px;
padding: 17px 18px;
}
.panel-title {
margin-bottom: 13px;
color: #dcebef;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-item {
display: grid;
grid-template-columns: 14px 1fr;
align-items: center;
gap: 10px;
margin: 9px 0;
color: #adc0c8;
font-size: 12px;
}
.swatch {
width: 12px;
height: 12px;
border-radius: 3px;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
}
.swatch.coating {
background: linear-gradient(135deg, #39c2d0, #117d8f);
}
.swatch.metal {
background: linear-gradient(135deg, #d0d8dc, #66757d);
}
.swatch.rust {
background: linear-gradient(135deg, #e49a59, #82401f);
}
.swatch.agent {
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px rgba(84, 215, 238, 0.8);
}
.key-note {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #718993;
font-size: 10px;
line-height: 1.5;
}
.controls-panel {
right: 30px;
bottom: 30px;
width: 218px;
padding: 15px;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 2px 1px 13px;
color: #c6d8de;
font-size: 12px;
cursor: pointer;
user-select: none;
}
.switch {
position: relative;
width: 38px;
height: 21px;
flex: 0 0 auto;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
border: 1px solid rgba(141, 193, 203, 0.28);
border-radius: 999px;
background: #142733;
transition: 180ms ease;
}
.switch-track::after {
content: "";
position: absolute;
width: 13px;
height: 13px;
top: 3px;
left: 4px;
border-radius: 50%;
background: #78909b;
transition: 180ms ease;
}
.switch input:checked + .switch-track {
border-color: rgba(84, 215, 238, 0.7);
background: rgba(35, 132, 153, 0.52);
}
.switch input:checked + .switch-track::after {
transform: translateX(16px);
background: var(--cyan);
box-shadow: 0 0 10px rgba(84, 215, 238, 0.75);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 34px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(19, 42, 54, 0.8);
color: #bdd0d7;
font: inherit;
font-size: 11px;
cursor: pointer;
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(84, 215, 238, 0.48);
background: rgba(27, 66, 82, 0.85);
color: white;
}
button:active {
transform: translateY(1px);
}
button:focus-visible,
input:focus-visible + .switch-track {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.button-row .wide {
grid-column: 1 / -1;
}
.scene-label {
position: fixed;
z-index: 8;
min-width: 185px;
padding: 11px 13px;
border: 1px solid rgba(164, 218, 229, 0.2);
border-radius: 11px;
background: rgba(8, 23, 32, 0.82);
box-shadow: 0 12px 36px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 28px;
background: linear-gradient(var(--cyan), transparent);
opacity: 0.65;
}
.label-index {
display: block;
margin-bottom: 4px;
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.label-title {
display: block;
color: #edf7fa;
font-size: 12px;
font-weight: 720;
letter-spacing: 0.02em;
}
.label-detail {
display: block;
margin-top: 4px;
color: #8fa8b2;
font-size: 10px;
line-height: 1.4;
}
.scene-label.breach {
border-color: rgba(199, 108, 53, 0.32);
}
.scene-label.breach .label-index {
color: #e49a68;
}
.scene-label.breach::after {
background: linear-gradient(#e49a68, transparent);
}
.instructions {
position: fixed;
z-index: 9;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
color: rgba(170, 197, 206, 0.56);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
white-space: nowrap;
}
.loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #07121b;
color: #91aab5;
font-size: 11px;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 780px) {
.header {
top: 20px;
left: 20px;
}
.subtitle {
max-width: 310px;
font-size: 12px;
}
.scope-badge {
display: none;
}
.key-panel {
left: 14px;
bottom: 14px;
width: auto;
padding: 11px 13px;
}
.key-panel .panel-title,
.key-note,
.legend-item:nth-of-type(4),
.legend-item:nth-of-type(5) {
display: none;
}
.legend-item {
display: inline-grid;
width: 25px;
margin: 0 3px;
}
.legend-item span:last-child {
display: none;
}
.controls-panel {
right: 14px;
bottom: 14px;
width: 178px;
padding: 11px;
}
.instructions {
display: none;
}
.scene-label {
min-width: 145px;
padding: 9px 10px;
}
.label-detail {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
* {
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="loading" id="loading">Loading sectioned comparison…</div>
<header class="header">
<div class="eyebrow">Qualitative materials study</div>
<h1>Surface Barrier / Local Breach</h1>
<p class="subtitle">
Matching sectioned metal coupons compare a continuous protective surface
with a localized coating discontinuity and the resulting access pathway
to the underlying metal.
</p>
<div class="scope-badge">
<span></span>
Illustrative only · no chemical or process conditions
</div>
</header>
<aside class="key-panel" aria-label="Scene legend">
<div class="panel-title">Visual key</div>
<div class="legend-item">
<i class="swatch coating"></i>
<span>Protective coating</span>
</div>
<div class="legend-item">
<i class="swatch metal"></i>
<span>Metal substrate</span>
</div>
<div class="legend-item">
<i class="swatch rust"></i>
<span>Corrosion indication</span>
</div>
<div class="legend-item">
<i class="swatch agent"></i>
<span>Environmental agent pathway</span>
</div>
<p class="key-note">
The front-right section corner is removed on both coupons to reveal
relative layer thickness and the coating-to-metal interface.
</p>
</aside>
<aside class="controls-panel" aria-label="Scene controls">
<label class="switch-row" for="pathToggle">
<span>Access pathways</span>
<span class="switch">
<input id="pathToggle" type="checkbox" checked />
<span class="switch-track"></span>
</span>
</label>
<div class="button-row">
<button id="focusIntact" type="button">Intact</button>
<button id="focusBreach" type="button">Breach</button>
<button id="overview" class="wide" type="button">Overview / reset view</button>
</div>
</aside>
<div class="scene-label" id="intactLabel">
<span class="label-index">Sample A</span>
<span class="label-title">Continuous surface barrier</span>
<span class="label-detail">Incoming agents terminate at the coating.</span>
</div>
<div class="scene-label breach" id="breachLabel">
<span class="label-index">Sample B</span>
<span class="label-title">Local coating discontinuity</span>
<span class="label-detail">The pathway reaches the exposed substrate.</span>
</div>
<div class="instructions">
Drag to orbit · Scroll or pinch to zoom · Shift/right-drag to pan
</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 loading = document.getElementById("loading");
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D comparison of intact and breached protective coatings"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07121b, 10, 23);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
function makeBrushedMetalTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = "#9aa6ac";
context.fillRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < canvas.height; y++) {
const value = 125 + Math.random() * 85;
context.strokeStyle = `rgba(${value},${value + 5},${value + 8},0.18)`;
context.lineWidth = Math.random() * 1.5 + 0.2;
context.beginPath();
context.moveTo(0, y + Math.random());
context.lineTo(canvas.width, y + Math.random());
context.stroke();
}
for (let i = 0; i < 250; i++) {
context.fillStyle = `rgba(255,255,255,${Math.random() * 0.07})`;
context.fillRect(
Math.random() * canvas.width,
Math.random() * canvas.height,
20 + Math.random() * 150,
0.5
);
}
const texture = new THREE.CanvasTexture(canvas);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(3.5, 1.5);
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
const brushedTexture = makeBrushedMetalTexture();
const steelMaterial = new THREE.MeshStandardMaterial({
color: 0xa7b1b6,
map: brushedTexture,
metalness: 0.88,
roughness: 0.31,
envMapIntensity: 0.9
});
const steelEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x66757d,
map: brushedTexture,
metalness: 0.9,
roughness: 0.39
});
const coatingMaterial = new THREE.MeshPhysicalMaterial({
color: 0x159aac,
metalness: 0.18,
roughness: 0.26,
clearcoat: 0.85,
clearcoatRoughness: 0.2
});
const coatingEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x0b6778,
metalness: 0.22,
roughness: 0.38
});
const rustMaterial = new THREE.MeshStandardMaterial({
color: 0xb85d2d,
metalness: 0.08,
roughness: 0.92
});
const darkRustMaterial = new THREE.MeshStandardMaterial({
color: 0x63331f,
metalness: 0.05,
roughness: 0.98
});
const scratchMaterial = new THREE.MeshStandardMaterial({
color: 0x273138,
metalness: 0.7,
roughness: 0.48
});
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x10232e,
metalness: 0.35,
roughness: 0.58
});
// ------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8ecff, 0x111820, 1.7));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 7.5, 5.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x62d7f4, 1.45);
rimLight.position.set(-5, 3.5, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb274, 16, 8, 2);
warmLight.position.set(3.2, 2.5, 1.8);
scene.add(warmLight);
// ------------------------------------------------------------
// Stage
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.CircleGeometry(12, 96),
new THREE.MeshStandardMaterial({
color: 0x091720,
metalness: 0.08,
roughness: 0.95
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 36, 0x315262, 0x203744);
grid.position.y = -0.02;
grid.material.transparent = true;
grid.material.opacity = 0.15;
scene.add(grid);
const stage = new THREE.Mesh(
new THREE.CylinderGeometry(5.35, 5.55, 0.14, 96),
new THREE.MeshStandardMaterial({
color: 0x0d202b,
metalness: 0.32,
roughness: 0.64
})
);
stage.position.y = -0.08;
stage.receiveShadow = true;
scene.add(stage);
const stageRing = new THREE.Mesh(
new THREE.RingGeometry(5.12, 5.17, 96),
new THREE.MeshBasicMaterial({
color: 0x3bb9ca,
transparent: true,
opacity: 0.25,
side: THREE.DoubleSide
})
);
stageRing.rotation.x = -Math.PI / 2;
stageRing.position.y = 0.001;
scene.add(stageRing);
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
const WIDTH = 2.7;
const DEPTH = 1.85;
const SUBSTRATE_HEIGHT = 0.34;
const COATING_HEIGHT = 0.075;
const SECTION_CUT = 0.58;
function polygonShape(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return shape;
}
function substrateShape() {
return polygonShape([
[-WIDTH / 2, -DEPTH / 2],
[WIDTH / 2 - SECTION_CUT, -DEPTH / 2],
[WIDTH / 2, -DEPTH / 2 + SECTION_CUT],
[WIDTH / 2, DEPTH / 2],
[-WIDTH / 2, DEPTH / 2]
]);
}
function coatingShape(breached) {
const points = [[-WIDTH / 2, -DEPTH / 2]];
if (breached) {
// An irregular open indentation in the coating begins at the front edge.
points.push(
[-0.34, -DEPTH / 2],
[-0.27, -0.76],
[-0.19, -0.61],
[-0.12, -0.40],
[-0.04, -0.15],
[0.06, 0.08],
[0.17, -0.02],
[0.25, -0.25],
[0.33, -0.53],
[0.41, -0.78],
[0.47, -DEPTH / 2]
);
}
points.push(
[WIDTH / 2 - SECTION_CUT, -DEPTH / 2],
[WIDTH / 2, -DEPTH / 2 + SECTION_CUT],
[WIDTH / 2, DEPTH / 2],
[-WIDTH / 2, DEPTH / 2]
);
return polygonShape(points);
}
function extrudedLayer(shape, depth, capMaterial, sideMaterial, y) {
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.012,
bevelThickness: 0.009,
curveSegments: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
sideMaterial === capMaterial
? capMaterial
: [capMaterial, sideMaterial]
);
mesh.position.y = y;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function irregularBlob(radius, elongation = 1) {
const shape = new THREE.Shape();
const segments = 13;
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * Math.PI * 2;
const variation = 0.73 + Math.random() * 0.27;
const x = Math.cos(angle) * radius * variation;
const y =
Math.sin(angle) * radius * variation * elongation;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
return shape;
}
// ------------------------------------------------------------
// Environment pathway visualization
// ------------------------------------------------------------
const pathways = [];
const pathwayRoots = [];
const pathwayLineMaterial = new THREE.LineDashedMaterial({
color: 0x56d9ef,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.72
});
const pathwayParticleMaterial = new THREE.MeshBasicMaterial({
color: 0x8befff,
transparent: true,
opacity: 0.95
});
const pathwayArrowMaterial = new THREE.MeshBasicMaterial({
color: 0x56d9ef,
transparent: true,
opacity: 0.9
});
function addPathway(parent, pointArray, options = {}) {
const points = pointArray.map((point) => new THREE.Vector3(...point));
const curve = new THREE.CatmullRomCurve3(
points,
false,
"catmullrom",
0.35
);
const lineGeometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(90)
);
const line = new THREE.Line(lineGeometry, pathwayLineMaterial);
line.computeLineDistances();
parent.add(line);
const particles = [];
for (let i = 0; i < 4; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(options.small ? 0.028 : 0.035, 10, 10),
pathwayParticleMaterial
);
parent.add(particle);
particles.push(particle);
}
const movingArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.052, 0.15, 12),
pathwayArrowMaterial
);
parent.add(movingArrow);
const impact = new THREE.Mesh(
new THREE.RingGeometry(0.085, 0.115, 32),
new THREE.MeshBasicMaterial({
color: options.blocked ? 0x61dfed : 0xf0a267,
transparent: true,
opacity: 0.6,
side: THREE.DoubleSide
})
);
impact.rotation.x = -Math.PI / 2;
impact.position.copy(curve.getPointAt(1));
impact.position.y += 0.008;
parent.add(impact);
pathways.push({
curve,
particles,
movingArrow,
impact,
speed: options.speed ?? 0.18,
phase: options.phase ?? Math.random(),
blocked: options.blocked ?? false
});
}
// ------------------------------------------------------------
// Sample construction
// ------------------------------------------------------------
const sampleGroups = [];
function createSample(xPosition, breached) {
const group = new THREE.Group();
group.position.set(xPosition, 0.27, 0);
scene.add(group);
const substrate = extrudedLayer(
substrateShape(),
SUBSTRATE_HEIGHT,
steelMaterial,
steelEdgeMaterial,
0
);
group.add(substrate);
const coating = extrudedLayer(
coatingShape(breached),
COATING_HEIGHT,
coatingMaterial,
coatingEdgeMaterial,
SUBSTRATE_HEIGHT
);
group.add(coating);
const corrosionGroup = new THREE.Group();
group.add(corrosionGroup);
// A fine interface line emphasizes the section without changing geometry.
const interfaceShape = substrateShape();
const interfaceLine = extrudedLayer(
interfaceShape,
0.008,
scratchMaterial,
scratchMaterial,
SUBSTRATE_HEIGHT - 0.004
);
interfaceLine.scale.set(0.997, 1, 0.997);
group.add(interfaceLine);
if (breached) {
const rustPatches = [
{ x: -0.25, z: 0.83, r: 0.18, e: 0.52 },
{ x: -0.14, z: 0.65, r: 0.15, e: 0.62 },
{ x: -0.03, z: 0.43, r: 0.13, e: 0.72 },
{ x: 0.09, z: 0.23, r: 0.11, e: 0.74 },
{ x: 0.16, z: 0.04, r: 0.09, e: 0.66 }
];
for (const patch of rustPatches) {
const mesh = new THREE.Mesh(
new THREE.ShapeGeometry(irregularBlob(patch.r, patch.e), 18),
rustMaterial
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(patch.x, SUBSTRATE_HEIGHT + 0.004, patch.z);
mesh.receiveShadow = true;
corrosionGroup.add(mesh);
}
const darkPatch = new THREE.Mesh(
new THREE.ShapeGeometry(irregularBlob(0.065, 0.9), 14),
darkRustMaterial
);
darkPatch.rotation.x = -Math.PI / 2;
darkPatch.position.set(-0.12, SUBSTRATE_HEIGHT + 0.006, 0.52);
corrosionGroup.add(darkPatch);
const blisterGeometry = new THREE.SphereGeometry(0.045, 14, 10);
for (const blister of [
[-0.18, 0.72],
[-0.07, 0.38],
[0.08, 0.18]
]) {
const mesh = new THREE.Mesh(blisterGeometry, rustMaterial);
mesh.scale.set(1, 0.22, 1.1);
mesh.position.set(
blister[0],
SUBSTRATE_HEIGHT + 0.012,
blister[1]
);
mesh.castShadow = true;
corrosionGroup.add(mesh);
}
// Small dark scoring marks emphasize the irregular coating edge.
const scoring = [
[-0.34, 0.82, 0.025, 0.25],
[-0.25, 0.61, 0.025, 0.28],
[-0.13, 0.39, 0.022, 0.26],
[0.03, 0.16, 0.02, 0.23],
[0.29, -0.17, 0.018, 0.18]
];
for (const mark of scoring) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(mark[2], 0.006, mark[3]),
scratchMaterial
);
mesh.position.set(
mark[0],
SUBSTRATE_HEIGHT + COATING_HEIGHT + 0.002,
mark[1]
);
mesh.rotation.y = mark[0] * 0.8;
group.add(mesh);
}
}
const pathwayRoot = new THREE.Group();
group.add(pathwayRoot);
pathwayRoots.push(pathwayRoot);
if (!breached) {
// Incoming paths remain above the continuous coating.
addPathway(
pathwayRoot,
[
[-0.72, 2.25, -0.12],
[-0.69, 1.55, -0.08],
[-0.68, 0.78, 0.03],
[-0.68, 0.425, 0.04]
],
{ blocked: true, speed: 0.17, phase: 0.05 }
);
addPathway(
pathwayRoot,
[
[0.02, 2.35, -0.2],
[0.02, 1.62, -0.13],
[0.01, 0.83, 0.02],
[0.01, 0.425, 0.03]
],
{ blocked: true, speed: 0.15, phase: 0.37 }
);
addPathway(
pathwayRoot,
[
[0.68, 2.2, -0.1],
[0.65, 1.5, -0.04],
[0.65, 0.78, 0.06],
[0.64, 0.425, 0.05]
],
{ blocked: true, speed: 0.18, phase: 0.68 }
);
} else {
// Main path enters the open discontinuity and reaches the substrate.
addPathway(
pathwayRoot,
[
[-0.26, 2.3, 0.35],
[-0.25, 1.45, 0.57],
[-0.18, 0.72, 0.85],
[-0.08, 0.39, 0.66],
[0.04, 0.35, 0.39],
[0.13, 0.35, 0.16]
],
{ speed: 0.19, phase: 0.06 }
);
// A secondary qualitative branch spreads along the exposed region.
addPathway(
pathwayRoot,
[
[-0.22, 1.55, 0.45],
[-0.17, 0.82, 0.68],
[-0.08, 0.38, 0.48],
[0.13, 0.35, 0.28],
[0.31, 0.35, 0.05]
],
{ speed: 0.15, phase: 0.43, small: true }
);
addPathway(
pathwayRoot,
[
[-0.18, 0.63, 0.77],
[-0.10, 0.39, 0.55],
[-0.02, 0.35, 0.33],
[0.03, 0.35, 0.13]
],
{ speed: 0.23, phase: 0.72, small: true }
);
}
sampleGroups.push(group);
return group;
}
function createPlinth(xPosition, breached) {
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(3.03, 0.24, 2.17),
platformMaterial
);
plinth.position.set(xPosition, 0.12, 0);
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(plinth.geometry),
new THREE.LineBasicMaterial({
color: 0x4e7a89,
transparent: true,
opacity: 0.34
})
);
edges.position.copy(plinth.position);
scene.add(edges);
const indicator = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.025, 0.025),
new THREE.MeshBasicMaterial({
color: breached ? 0xd67b45 : 0x35bdce,
transparent: true,
opacity: 0.8
})
);
indicator.position.set(xPosition, 0.19, 1.091);
scene.add(indicator);
}
createPlinth(-1.75, false);
createPlinth(1.75, true);
const intactSample = createSample(-1.75, false);
const breachedSample = createSample(1.75, true);
// ------------------------------------------------------------
// Custom camera controls — no external control modules
// ------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 0.75, 0),
theta: 0.54,
phi: 0.98,
radius: 8.9
};
const cameraState = {
target: controls.target.clone(),
theta: controls.theta,
phi: controls.phi,
radius: controls.radius
};
let cameraTween = null;
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);
}
function startCameraTween(target, radius, theta, phi) {
cameraTween = {
startTime: performance.now(),
duration: reducedMotion ? 1 : 850,
fromTarget: controls.target.clone(),
toTarget: target.clone(),
fromRadius: controls.radius,
toRadius: radius,
fromTheta: controls.theta,
toTheta: theta,
fromPhi: controls.phi,
toPhi: phi
};
}
function focusView(targetX, radius = 6.25) {
startCameraTween(
new THREE.Vector3(targetX, 0.72, 0),
radius,
0.49,
1.02
);
}
function resetView() {
startCameraTween(
new THREE.Vector3(0, 0.75, 0),
8.9,
0.54,
0.98
);
}
updateCamera();
const pointers = new Map();
let lastPointer = null;
let previousPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 1) {
lastPointer = {
x: event.clientX,
y: event.clientY,
button: event.button,
shift: event.shiftKey
};
} else if (pointers.size === 2) {
const values = [...pointers.values()];
previousPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
cameraTween = null;
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size >= 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (previousPinchDistance > 0) {
controls.radius *= previousPinchDistance / distance;
controls.radius = THREE.MathUtils.clamp(
controls.radius,
4.7,
13.5
);
}
previousPinchDistance = distance;
return;
}
if (!lastPointer) return;
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
if (lastPointer.button === 2 || lastPointer.shift) {
const right = new THREE.Vector3();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, camera.up).normalize();
controls.target.addScaledVector(
right,
-dx * controls.radius * 0.0012
);
controls.target.y += dy * controls.radius * 0.0012;
controls.target.x = THREE.MathUtils.clamp(
controls.target.x,
-4,
4
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
0.1,
3
);
} else {
controls.theta -= dx * 0.005;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.0045,
0.36,
1.43
);
}
lastPointer = {
x: event.clientX,
y: event.clientY,
button: event.button,
shift: event.shiftKey
};
});
function removePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
lastPointer = null;
previousPinchDistance = 0;
} else if (pointers.size === 1) {
const value = [...pointers.values()][0];
lastPointer = {
x: value.x,
y: value.y,
button: value.button,
shift: false
};
}
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraTween = null;
controls.radius *= Math.exp(event.deltaY * 0.001);
controls.radius = THREE.MathUtils.clamp(
controls.radius,
4.7,
13.5
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") controls.theta += 0.08;
if (event.key === "ArrowRight") controls.theta -= 0.08;
if (event.key === "ArrowUp") controls.phi -= 0.06;
if (event.key === "ArrowDown") controls.phi += 0.06;
if (event.key === "+" || event.key === "=") {
controls.radius *= 0.9;
}
if (event.key === "-") {
controls.radius *= 1.1;
}
if (event.key === "Escape") resetView();
});
document.getElementById("focusIntact").addEventListener("click", () => {
focusView(-1.75);
});
document.getElementById("focusBreach").addEventListener("click", () => {
focusView(1.75);
});
document.getElementById("overview").addEventListener("click", resetView);
const pathToggle = document.getElementById("pathToggle");
pathToggle.addEventListener("change", () => {
for (const root of pathwayRoots) {
root.visible = pathToggle.checked;
}
});
// ------------------------------------------------------------
// Screen-space annotation anchors
// ------------------------------------------------------------
const intactLabel = document.getElementById("intactLabel");
const breachLabel = document.getElementById("breachLabel");
const intactAnchor = new THREE.Vector3(-0.42, 0.72, -0.67);
const breachAnchor = new THREE.Vector3(0.38, 0.72, -0.67);
const projected = new THREE.Vector3();
function placeLabel(element, worldPoint) {
projected.copy(worldPoint).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const upward = new THREE.Vector3(0, 1, 0);
const tangent = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (cameraTween) {
const t = Math.min(
(performance.now() - cameraTween.startTime) /
cameraTween.duration,
1
);
const eased = 1 - Math.pow(1 - t, 3);
controls.target.lerpVectors(
cameraTween.fromTarget,
cameraTween.toTarget,
eased
);
controls.radius = THREE.MathUtils.lerp(
cameraTween.fromRadius,
cameraTween.toRadius,
eased
);
controls.theta = THREE.MathUtils.lerp(
cameraTween.fromTheta,
cameraTween.toTheta,
eased
);
controls.phi = THREE.MathUtils.lerp(
cameraTween.fromPhi,
cameraTween.toPhi,
eased
);
if (t >= 1) cameraTween = null;
}
updateCamera();
if (pathToggle.checked) {
for (const path of pathways) {
const time = reducedMotion
? 0.55
: (elapsed * path.speed + path.phase) % 1;
path.particles.forEach((particle, index) => {
const particleTime =
(time + index * 0.17) % 1;
particle.position.copy(
path.curve.getPointAt(particleTime)
);
const scale = 0.82 + Math.sin(elapsed * 4 + index) * 0.12;
particle.scale.setScalar(scale);
});
path.movingArrow.position.copy(
path.curve.getPointAt(time)
);
path.curve.getTangentAt(time, tangent);
path.movingArrow.quaternion.setFromUnitVectors(
upward,
tangent.normalize()
);
const pulse = 1 + Math.sin(elapsed * 2.4) * 0.08;
path.impact.scale.setScalar(pulse);
}
}
placeLabel(
intactLabel,
intactSample.localToWorld(intactAnchor.clone())
);
placeLabel(
breachLabel,
breachedSample.localToWorld(breachAnchor.clone())
);
renderer.render(scene, camera);
}
// ------------------------------------------------------------
// Resize and startup
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 550);
});
</script>
</body>
</html>
```