Prompt specification
Build or customize the Three.js 3D scene "Corrosion-factor conceptual explorer" 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: Corrosion-factor conceptual explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/corrosion-factor-conceptual-explorer-x1944
Scene Description Prompt:
Use Three.js from cdnjs to allow toggling moisture access and coating continuity in a toy surface model; show illustrative affected regions and state that real corrosion depends on material and environmental chemistry. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (corrosion-factor-conceptual-explorer-x1944):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Surface Corrosion Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #08111f;
--panel: #101b2d;
--panel-2: #142238;
--line: rgba(255, 255, 255, 0.1);
--text: #edf7ff;
--muted: #91a5bd;
--cyan: #67dcff;
--teal: #3aa7a0;
--rust: #e47a3d;
--rust-dark: #6f3328;
--safe: #62d6a7;
--warning: #e5bd61;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
.app {
display: grid;
grid-template-columns: 320px minmax(0, 1fr);
grid-template-rows: 72px minmax(0, 1fr);
width: 100%;
height: 100%;
}
header {
grid-column: 1 / -1;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(58, 167, 160, 0.12), transparent 42%),
#0b1526;
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 36px;
height: 36px;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
width: 16px;
height: 16px;
border-radius: 4px;
background: var(--teal);
box-shadow: 0 0 18px rgba(58, 167, 160, 0.35);
}
.brand-mark i:nth-child(1) {
left: 0;
top: 10px;
}
.brand-mark i:nth-child(2) {
left: 10px;
top: 0;
background: var(--cyan);
}
.brand-mark i:nth-child(3) {
left: 20px;
top: 10px;
}
.brand-mark i:nth-child(4) {
left: 10px;
top: 20px;
background: var(--rust);
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.15;
letter-spacing: 0.01em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.header-badge {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid rgba(103, 220, 255, 0.25);
border-radius: 999px;
background: rgba(103, 220, 255, 0.07);
color: #a9eaff;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
aside {
min-height: 0;
overflow: auto;
padding: 20px;
border-right: 1px solid var(--line);
background:
radial-gradient(circle at 20% 0%, rgba(58, 167, 160, 0.08), transparent 30%),
var(--panel);
z-index: 5;
}
.eyebrow {
margin: 0 0 7px;
color: #76dfd8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
aside h2 {
margin: 0 0 8px;
font-size: 21px;
line-height: 1.2;
}
.intro {
margin: 0 0 19px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
display: grid;
gap: 10px;
}
.control {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
min-height: 78px;
padding: 13px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease;
}
.control:hover {
border-color: rgba(255, 255, 255, 0.19);
background: rgba(255, 255, 255, 0.045);
}
.control-copy {
display: block;
min-width: 0;
}
.control-title {
display: block;
font-size: 13px;
font-weight: 750;
}
.control-desc {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.control input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
pointer-events: none;
}
.switch {
position: relative;
width: 47px;
height: 26px;
flex: 0 0 auto;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 999px;
background: #24334a;
transition: 180ms ease;
}
.switch::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 18px;
height: 18px;
border-radius: 50%;
background: #a9b8ca;
box-shadow: 0 2px 7px rgba(0, 0, 0, 0.35);
transition: 180ms ease;
}
.control input:checked + .switch {
border-color: rgba(103, 220, 255, 0.42);
background: var(--teal);
}
.control input:checked + .switch::after {
transform: translateX(21px);
background: white;
}
.control input:focus-visible + .switch {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.state-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 17px;
padding-bottom: 9px;
border-bottom: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
}
.state-line span:last-child {
color: #d7e9f8;
}
.status-card {
display: flex;
align-items: flex-start;
gap: 11px;
margin-top: 17px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel-2);
}
.status-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
margin-top: 4px;
border-radius: 50%;
background: var(--safe);
box-shadow: 0 0 14px currentColor;
color: var(--safe);
}
.status-card[data-state="rust"] .status-dot {
background: var(--rust);
color: var(--rust);
}
.status-card[data-state="dry"] .status-dot {
background: var(--warning);
color: var(--warning);
}
.status-card small {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status-card strong {
display: block;
margin-top: 4px;
font-size: 14px;
}
.status-card p {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.metric {
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 5px;
font-size: 13px;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 12px;
margin-top: 0;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: #b8c8d8;
font-size: 11px;
}
.swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}
.swatch.coating {
background: var(--teal);
}
.swatch.bare {
background: #9aa9b8;
}
.swatch.rust {
background: linear-gradient(135deg, #f1aa48, #8b382b);
}
.swatch.water {
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px rgba(103, 220, 255, 0.55);
}
.disclaimer {
margin-top: 19px;
padding: 13px;
border: 1px solid rgba(229, 189, 97, 0.2);
border-radius: 12px;
background: rgba(229, 189, 97, 0.055);
color: #c9cfd2;
font-size: 10.5px;
line-height: 1.55;
}
.disclaimer strong {
color: #f1d38b;
}
.viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
isolation: isolate;
background:
radial-gradient(circle at 50% 42%, rgba(38, 78, 111, 0.34), transparent 42%),
radial-gradient(circle at 18% 12%, rgba(58, 167, 160, 0.12), transparent 28%),
linear-gradient(145deg, #0a1424, #07101c 70%);
}
.webgl {
position: absolute;
inset: 0;
z-index: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.scene-ui {
position: absolute;
z-index: 2;
pointer-events: none;
}
.scene-top-left {
top: 18px;
left: 20px;
}
.scene-top-right {
top: 18px;
right: 20px;
display: flex;
align-items: flex-start;
gap: 8px;
}
.scene-chip {
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background: rgba(7, 15, 28, 0.72);
backdrop-filter: blur(10px);
color: #c6d7e7;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
}
.scene-chip strong {
color: white;
}
#sceneState {
color: var(--rust);
}
#resetView {
pointer-events: auto;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background: rgba(7, 15, 28, 0.72);
color: #c6d7e7;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
cursor: pointer;
backdrop-filter: blur(10px);
}
#resetView:hover {
border-color: rgba(103, 220, 255, 0.35);
color: white;
}
.scene-bottom-left {
bottom: 20px;
left: 20px;
}
.scene-bottom-right {
right: 20px;
bottom: 20px;
color: rgba(203, 219, 234, 0.65);
font-size: 10px;
text-align: right;
line-height: 1.6;
}
.region-note {
max-width: 310px;
padding: 10px 12px;
border: 1px solid rgba(103, 220, 255, 0.12);
border-radius: 10px;
background: rgba(7, 15, 28, 0.65);
backdrop-filter: blur(10px);
color: #a8bacb;
font-size: 10px;
line-height: 1.5;
}
.region-note strong {
color: #e4f3ff;
}
.corner-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px var(--cyan);
}
@media (max-width: 820px) {
body {
overflow: auto;
}
.app {
grid-template-columns: 1fr;
grid-template-rows: 64px 58vh auto;
height: auto;
min-height: 100%;
}
header {
padding: 0 16px;
}
.brand p,
.header-badge {
display: none;
}
.viewport {
grid-row: 2;
}
aside {
grid-row: 3;
overflow: visible;
border-right: 0;
border-top: 1px solid var(--line);
}
.scene-bottom-right {
display: none;
}
}
@media (max-width: 480px) {
.scene-top-right {
top: 12px;
right: 12px;
}
.scene-top-left {
top: 12px;
left: 12px;
}
.scene-bottom-left {
right: 12px;
bottom: 12px;
left: 12px;
}
.region-note {
max-width: none;
}
.scene-chip {
padding: 7px 8px;
font-size: 9px;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i>
</div>
<div>
<h1>Voxel Surface Lab</h1>
<p>Interactive coating, moisture, and oxidation study</p>
</div>
</div>
<div class="header-badge">ILLUSTRATIVE MODEL</div>
</header>
<aside>
<p class="eyebrow">Surface conditions</p>
<h2>Test the exposure path</h2>
<p class="intro">
Toggle the two schematic conditions and inspect how a voxel toy rover
changes. Orange regions represent illustrative affected areas.
</p>
<div class="controls">
<label class="control">
<span class="control-copy">
<span class="control-title">Moisture access</span>
<span class="control-desc">
Allows water to reach a breach in the protective coating.
</span>
</span>
<input id="moistureToggle" type="checkbox" checked>
<span class="switch" aria-hidden="true"></span>
</label>
<label class="control">
<span class="control-copy">
<span class="control-title">Coating continuity</span>
<span class="control-desc">
Off reveals three discontinuous coating-breach regions.
</span>
</span>
<input id="coatingToggle" type="checkbox">
<span class="switch" aria-hidden="true"></span>
</label>
</div>
<div class="state-line">
<span>Moisture pathway</span>
<span id="moistureState">PATH OPEN</span>
</div>
<div class="state-line">
<span>Coating condition</span>
<span id="coatingState">BREACHED</span>
</div>
<section id="statusCard" class="status-card" data-state="rust" aria-live="polite">
<span class="status-dot" aria-hidden="true"></span>
<div>
<small>Current outcome</small>
<strong id="statusTitle">Affected regions active</strong>
<p id="statusText">
Coating loss and moisture access combine in three schematic patches.
</p>
</div>
</section>
<div class="metrics">
<div class="metric">
<span>Illustrative zones</span>
<strong id="zoneReadout">3</strong>
</div>
<div class="metric">
<span>Marked voxels</span>
<strong id="voxelReadout">0</strong>
</div>
</div>
<p class="eyebrow" style="margin-top: 20px;">Visual key</p>
<div class="legend">
<div class="legend-item">
<span class="swatch coating"></span>
<span>Coating</span>
</div>
<div class="legend-item">
<span class="swatch bare"></span>
<span>Bare substrate</span>
</div>
<div class="legend-item">
<span class="swatch rust"></span>
<span>Oxidation</span>
</div>
<div class="legend-item">
<span class="swatch water"></span>
<span>Moisture</span>
</div>
</div>
<div class="disclaimer">
<strong>Schematic only.</strong>
Real corrosion depends on the substrate and coating materials,
environmental chemistry, water chemistry, salts, pH, temperature,
humidity, and exposure time. This view is not a corrosion prediction.
</div>
</aside>
<section id="viewport" class="viewport" aria-label="Interactive three-dimensional voxel model">
<div class="scene-ui scene-top-left">
<div class="scene-chip">
<strong>TOY ROVER</strong> · VOXEL SURFACE
</div>
</div>
<div class="scene-ui scene-top-right">
<div class="scene-chip">
SCENE: <span id="sceneState">WET + BREACHED</span>
</div>
<button id="resetView" type="button">RESET VIEW</button>
</div>
<div class="scene-ui scene-bottom-left">
<div class="region-note">
<span class="corner-dot"></span>
<strong>A–C mark illustrative affected regions.</strong>
Colors communicate a conceptual pathway, not measured corrosion.
</div>
</div>
<div class="scene-ui scene-bottom-right">
Drag to orbit · Wheel to zoom<br>
Shift-drag or right-drag to pan
</div>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const moistureToggle = document.getElementById("moistureToggle");
const coatingToggle = document.getElementById("coatingToggle");
const moistureState = document.getElementById("moistureState");
const coatingState = document.getElementById("coatingState");
const statusCard = document.getElementById("statusCard");
const statusTitle = document.getElementById("statusTitle");
const statusText = document.getElementById("statusText");
const zoneReadout = document.getElementById("zoneReadout");
const voxelReadout = document.getElementById("voxelReadout");
const sceneState = document.getElementById("sceneState");
const state = {
moisture: moistureToggle.checked,
coating: coatingToggle.checked
};
// ---------- Renderer, scene, and camera ----------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.className = "webgl";
renderer.domElement.setAttribute(
"aria-label",
"A voxel toy rover with coating and illustrative corrosion regions"
);
viewport.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x08111f, 22, 40);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const orbit = {
theta: 0.72,
phi: 0.90,
radius: 18.5,
target: new THREE.Vector3(0, 0.55, 0)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.radius * Math.cos(orbit.phi),
orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0x9edcff, 0x142038, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 4.2);
keyLight.position.set(5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55ddd8, 2.1);
rimLight.position.set(-8, 5, -8);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff9a5a, 1.0);
warmLight.position.set(9, 3, -5);
scene.add(warmLight);
// ---------- Stage ----------
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x111e32,
roughness: 0.86,
metalness: 0.06
});
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(16.4, 0.24, 7.4),
plinthMaterial
);
plinth.position.y = -0.58;
plinth.receiveShadow = true;
scene.add(plinth);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.ShadowMaterial({
color: 0x000000,
opacity: 0.24
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.71;
floor.receiveShadow = true;
scene.add(floor);
// ---------- Voxel rover geometry ----------
const BLOCK = 0.48;
const SPACING = 0.54;
const LAYER_STEP = 0.52;
const worldX = gridX => (gridX - 14) * SPACING;
const worldZ = gridZ => (gridZ - 8) * SPACING;
const surfaceY = layer =>
layer * LAYER_STEP + BLOCK + 0.05;
const maxLayer = new Map();
const layerKey = (x, z) => `${x},${z}`;
function fillVolume(x0, x1, z0, z1, layer) {
for (let x = x0; x <= x1; x++) {
for (let z = z0; z <= z1; z++) {
maxLayer.set(layerKey(x, z), layer);
}
}
}
// Chassis, hood, cabin, and roof are made entirely from voxel blocks.
fillVolume(1, 27, 3, 13, 0);
fillVolume(2, 12, 4, 12, 1);
fillVolume(14, 24, 4, 12, 1);
fillVolume(16, 22, 6, 10, 2);
const zones = [
{
id: "A",
x: 6,
z: 8,
rx: 2.8,
rz: 2.4,
layer: 1,
seed: 1.7
},
{
id: "B",
x: 19,
z: 8,
rx: 2.4,
rz: 2.1,
layer: 2,
seed: 4.3
},
{
id: "C",
x: 25,
z: 8,
rx: 2.4,
rz: 2.4,
layer: 0,
seed: 8.9
}
];
function hash2(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
function metricFor(x, z) {
let minimum = Infinity;
for (const zone of zones) {
const nx = (x - zone.x) / zone.rx;
const nz = (z - zone.z) / zone.rz;
const irregularity =
(hash2(x + zone.seed, z - zone.seed) - 0.5) * 0.24;
const distance = Math.sqrt(nx * nx + nz * nz) + irregularity;
minimum = Math.min(minimum, distance);
}
return minimum;
}
const bodyCells = [];
for (const [key, topLayer] of maxLayer) {
const [x, z] = key.split(",").map(Number);
for (let layer = 0; layer <= topLayer; layer++) {
bodyCells.push({
x,
z,
layer,
exposed: layer === topLayer,
metric: metricFor(x, z),
current: new THREE.Color(),
target: new THREE.Color()
});
}
}
const COATING = new THREE.Color(0x3aa7a0);
const COATING_DARK = new THREE.Color(0x246f73);
const BARE = new THREE.Color(0x98a8b8);
const PRIMER = new THREE.Color(0xb7a47b);
const RUST_DARK = new THREE.Color(0x603027);
const RUST = new THREE.Color(0xb84d31);
const RUST_LIGHT = new THREE.Color(0xe58a3d);
const AMBER = new THREE.Color(0xc99043);
function targetColorFor(cell) {
if (!cell.exposed) {
return COATING_DARK;
}
if (state.coating) {
const variation = 0.94 + hash2(cell.x * 1.7, cell.z * 2.3) * 0.11;
return COATING.clone().multiplyScalar(variation);
}
const metric = cell.metric;
if (state.moisture) {
if (metric < 0.28) return RUST_DARK;
if (metric < 0.54) return RUST;
if (metric < 0.78) return RUST_LIGHT;
if (metric < 0.96) return AMBER;
return COATING;
}
if (metric < 0.48) return BARE;
if (metric < 0.73) return BARE.clone().lerp(PRIMER, 0.45);
if (metric < 0.91) return PRIMER;
return COATING;
}
const bodyGeometry = new THREE.BoxGeometry(BLOCK, BLOCK, BLOCK);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.59,
metalness: 0.10
});
const bodyMesh = new THREE.InstancedMesh(
bodyGeometry,
bodyMaterial,
bodyCells.length
);
bodyMesh.castShadow = true;
bodyMesh.receiveShadow = true;
const dummy = new THREE.Object3D();
bodyCells.forEach((cell, index) => {
dummy.position.set(
worldX(cell.x),
cell.layer * LAYER_STEP + BLOCK / 2 + 0.05,
worldZ(cell.z)
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
bodyMesh.setMatrixAt(index, dummy.matrix);
cell.target.copy(targetColorFor(cell));
cell.current.copy(cell.target);
bodyMesh.setColorAt(index, cell.current);
});
bodyMesh.instanceMatrix.needsUpdate = true;
bodyMesh.instanceColor.needsUpdate = true;
scene.add(bodyMesh);
// ---------- Voxel wheels ----------
const wheelCells = [];
function addWheel(gridX, gridZ) {
const x = worldX(gridX);
const z = worldZ(gridZ);
wheelCells.push({ x, y: 0.30, z, hub: true });
wheelCells.push({ x: x - 0.52, y: 0.30, z, hub: false });
wheelCells.push({ x: x + 0.52, y: 0.30, z, hub: false });
wheelCells.push({ x, y: 0.82, z, hub: false });
wheelCells.push({ x, y: -0.22, z, hub: false });
}
addWheel(6, 2.25);
addWheel(22, 2.25);
addWheel(6, 13.75);
addWheel(22, 13.75);
const wheelGeometry = new THREE.BoxGeometry(0.46, 0.46, 0.46);
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.8,
metalness: 0.05
});
const wheelMesh = new THREE.InstancedMesh(
wheelGeometry,
wheelMaterial,
wheelCells.length
);
wheelMesh.castShadow = true;
wheelMesh.receiveShadow = true;
wheelCells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.y, cell.z);
dummy.updateMatrix();
wheelMesh.setMatrixAt(index, dummy.matrix);
wheelMesh.setColorAt(
index,
new THREE.Color(cell.hub ? 0xd8a855 : 0x17243a)
);
});
wheelMesh.instanceMatrix.needsUpdate = true;
wheelMesh.instanceColor.needsUpdate = true;
scene.add(wheelMesh);
// ---------- Region outlines, labels, and water droplets ----------
function makeLabelSprite(letter) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.beginPath();
context.arc(64, 64, 47, 0, Math.PI * 2);
context.fillStyle = "rgba(8, 17, 31, 0.88)";
context.fill();
context.lineWidth = 6;
context.strokeStyle = "#ffb05e";
context.stroke();
context.fillStyle = "#ffffff";
context.font = "700 56px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(letter, 64, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.62, 0.62, 1);
sprite.renderOrder = 20;
return sprite;
}
const dropGeometry = new THREE.SphereGeometry(0.075, 12, 8);
const dropMaterial = new THREE.MeshStandardMaterial({
color: 0x7ae8ff,
emissive: 0x168fff,
emissiveIntensity: 1.25,
roughness: 0.22,
metalness: 0.05,
transparent: true,
opacity: 0.88
});
const regionVisuals = [];
for (const zone of zones) {
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xffa455,
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.94, 1, 64),
ringMaterial
);
ring.rotation.x = -Math.PI / 2;
ring.scale.set(zone.rx * SPACING, zone.rz * SPACING, 1);
ring.position.set(
worldX(zone.x),
surfaceY(zone.layer) + 0.14,
worldZ(zone.z)
);
ring.renderOrder = 4;
scene.add(ring);
const label = makeLabelSprite(zone.id);
label.position.set(
worldX(zone.x),
surfaceY(zone.layer) + 0.86,
worldZ(zone.z)
);
scene.add(label);
const drops = new THREE.Group();
drops.position.set(
worldX(zone.x),
surfaceY(zone.layer) + 0.10,
worldZ(zone.z)
);
for (let index = 0; index < 7; index++) {
const angle = (index / 7) * Math.PI * 2 + zone.seed;
const radius = 0.18 + hash2(index + zone.seed, zone.x) * 0.48;
const drop = new THREE.Mesh(dropGeometry, dropMaterial);
drop.position.set(
Math.cos(angle) * radius,
0.18 + hash2(index, zone.z) * 0.22,
Math.sin(angle) * radius
);
drop.userData.baseY = drop.position.y;
drop.userData.phase = index * 0.8 + zone.seed;
drops.add(drop);
}
scene.add(drops);
regionVisuals.push({ zone, ring, label, drops });
}
// ---------- State updates ----------
function updateTargets() {
let affectedCount = 0;
for (const cell of bodyCells) {
cell.metric = metricFor(cell.x, cell.z);
cell.target.copy(targetColorFor(cell));
if (
cell.exposed &&
!state.coating &&
state.moisture &&
cell.metric < 0.96
) {
affectedCount++;
}
}
const activeRisk = !state.coating && state.moisture;
moistureState.textContent = state.moisture ? "PATH OPEN" : "PATH CLOSED";
coatingState.textContent = state.coating ? "CONTINUOUS" : "BREACHED";
zoneReadout.textContent = state.coating ? "0" : "3";
voxelReadout.textContent = activeRisk
? `${affectedCount} blocks`
: "0 blocks";
sceneState.textContent =
state.coating
? "PROTECTED"
: state.moisture
? "WET + BREACHED"
: "DRY + BREACHED";
if (state.coating && state.moisture) {
statusCard.dataset.state = "safe";
statusTitle.textContent = "Barrier is holding";
statusText.textContent =
"A moisture pathway exists, but the continuous coating isolates the substrate.";
} else if (state.coating) {
statusCard.dataset.state = "safe";
statusTitle.textContent = "Dry protected surface";
statusText.textContent =
"The coating is continuous and no moisture pathway is selected.";
} else if (state.moisture) {
statusCard.dataset.state = "rust";
statusTitle.textContent = "Affected regions active";
statusText.textContent =
"Coating loss and moisture access combine in three schematic patches.";
} else {
statusCard.dataset.state = "dry";
statusTitle.textContent = "Dry breach footprint";
statusText.textContent =
"Breaches are visible, but no moisture pathway is selected in this model.";
}
}
moistureToggle.addEventListener("change", () => {
state.moisture = moistureToggle.checked;
updateTargets();
});
coatingToggle.addEventListener("change", () => {
state.coating = coatingToggle.checked;
updateTargets();
});
updateTargets();
// ---------- Custom camera controls ----------
const pointers = new Map();
let dragMode = null;
let lastX = 0;
let lastY = 0;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
dragMode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
dragMode = "pinch";
previousPinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1 && dragMode === "orbit") {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.28,
1.38
);
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 1 && dragMode === "pan") {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
updateCamera();
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(
camera.quaternion
);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(
camera.quaternion
);
orbit.target.addScaledVector(
right,
-dx * orbit.radius * 0.00125
);
orbit.target.addScaledVector(
up,
dy * orbit.radius * 0.00125
);
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size >= 2 && dragMode === "pinch") {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (previousPinchDistance / distance),
9,
29
);
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
dragMode = null;
} else if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
dragMode = "orbit";
lastX = remaining.x;
lastY = remaining.y;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("lostpointercapture", endPointer);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
9,
29
);
},
{ passive: false }
);
function resetView() {
orbit.theta = 0.72;
orbit.phi = 0.90;
orbit.radius = 18.5;
orbit.target.set(0, 0.55, 0);
}
document.getElementById("resetView").addEventListener("click", resetView);
renderer.domElement.addEventListener("dblclick", resetView);
// ---------- Resize and animation ----------
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.getElapsedTime();
const colorEase = 1 - Math.exp(-delta * 7);
for (const cell of bodyCells) {
cell.current.lerp(cell.target, colorEase);
bodyMesh.setColorAt(bodyCells.indexOf(cell), cell.current);
}
bodyMesh.instanceColor.needsUpdate = true;
for (const visual of regionVisuals) {
const showBreach = !state.coating;
const activeRisk = showBreach && state.moisture;
visual.ring.visible = showBreach;
visual.label.visible = showBreach;
visual.drops.visible = activeRisk;
visual.ring.material.color.set(
activeRisk ? 0xffa455 : 0x9aa9b8
);
visual.ring.material.opacity = activeRisk
? 0.64 + Math.sin(elapsed * 2.2) * 0.16
: 0.38;
const labelScale = activeRisk
? 0.62 + Math.sin(elapsed * 2 + visual.zone.seed) * 0.025
: 0.56;
visual.label.scale.set(labelScale, labelScale, 1);
for (const drop of visual.drops.children) {
const pulse =
1 + Math.sin(elapsed * 2.6 + drop.userData.phase) * 0.16;
drop.scale.setScalar(pulse);
drop.position.y =
drop.userData.baseY +
Math.sin(elapsed * 1.9 + drop.userData.phase) * 0.035;
}
}
updateCamera();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```