Prompt specification
Build or customize the Three.js 3D scene "Bread crust and crumb slice" 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: Bread crust and crumb slice
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bread-crust-and-crumb-slice-x0085
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs with a voxel bread slice: dark crust and deep, irregular crumb cavities.
Verify the behavior against the actual geometry and data in each relevant state. Use schematic labels for deliberately synthetic elements.
Technical checks: Show deep, clearly lit crumb cavities on both cut faces. Give the cavities measurable geometric depth.
Return one complete HTML document. The file must run in a sandbox allowing inline scripts and the cdnjs Three.js r160 module. Construct procedural assets, use local event handlers and avoid remote assets, runtime network requests besides Three.js, pointer lock and native form submission. Keep the main requested geometry visible in the initial view.
Working Three.js Starter (bread-crust-and-crumb-slice-x0085):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#17110d">
<title>Voxel Bread Slice — Recessed Crumb Study</title>
<link rel="icon" href="data:,">
<style>
:root {
color-scheme: dark;
--ink: #f4dfbd;
--muted: #b7a58a;
--line: rgba(244, 223, 189, 0.18);
--panel: rgba(24, 17, 12, 0.82);
--amber: #e6ad60;
--cyan: #77eadb;
--crust: #4b2412;
--crumb: #d9aa68;
--recess: #96572b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #17110d;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#stage canvas.dragging {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#identity {
position: fixed;
z-index: 5;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 15px 16px 14px;
border-radius: 3px;
pointer-events: none;
}
.eyebrow {
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 4px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(22px, 3vw, 31px);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
#identity p {
margin: 8px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#readout {
position: fixed;
z-index: 5;
top: 154px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 15px 16px;
border-radius: 3px;
pointer-events: none;
}
.readout-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#auditBadge {
padding: 4px 6px;
border: 1px solid rgba(119, 234, 219, 0.42);
color: var(--cyan);
background: rgba(119, 234, 219, 0.08);
font-size: 8px;
letter-spacing: 0.12em;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px 16px;
}
.metric {
min-width: 0;
}
.metric-label {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric-value {
display: block;
color: #fff0d4;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 16px;
line-height: 1.1;
}
.metric-value small {
color: var(--muted);
font-size: 9px;
}
#auditCopy {
margin: 13px 0 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.55;
}
#depthBars {
display: grid;
gap: 5px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.depth-row {
display: grid;
grid-template-columns: 48px 1fr 26px;
align-items: center;
gap: 7px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 8px;
}
.depth-track {
height: 4px;
overflow: hidden;
background: rgba(255, 255, 255, 0.07);
}
.depth-fill {
height: 100%;
background: linear-gradient(90deg, #9c5b2d, #e1aa63);
}
#controls {
position: fixed;
z-index: 7;
top: 18px;
right: 18px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: min(460px, calc(100vw - 36px));
}
.control-button {
min-height: 34px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 2px;
color: var(--ink);
background: rgba(24, 17, 12, 0.82);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
transition: border-color 160ms ease, color 160ms ease, background 160ms ease;
}
.control-button:hover,
.control-button:focus-visible {
border-color: rgba(230, 173, 96, 0.72);
color: #fff2d8;
outline: none;
}
.control-button.active {
border-color: var(--amber);
color: #24160c;
background: var(--amber);
}
#overlayButton[aria-pressed="true"] {
border-color: var(--cyan);
color: var(--cyan);
background: rgba(119, 234, 219, 0.1);
}
#legend {
position: fixed;
z-index: 5;
right: 18px;
bottom: 18px;
padding: 11px 13px;
border-radius: 3px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin: 5px 0;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.swatch {
width: 11px;
height: 11px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: var(--crumb);
}
.swatch.crust {
background: var(--crust);
}
.swatch.recess {
background: var(--recess);
}
#diagnosticLegend {
display: none;
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--line);
}
body.diagnostic #diagnosticLegend {
display: block;
}
.swatch.lattice {
border-color: var(--cyan);
background:
linear-gradient(135deg, transparent 42%, var(--cyan) 43%, var(--cyan) 50%, transparent 51%),
linear-gradient(45deg, transparent 42%, var(--cyan) 43%, var(--cyan) 50%, transparent 51%);
}
#hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 18px;
padding: 9px 12px;
border-radius: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.13em;
text-align: center;
text-transform: uppercase;
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
}
.annotation {
position: fixed;
z-index: 4;
max-width: 210px;
padding: 8px 10px;
border-left: 1px solid rgba(244, 223, 189, 0.46);
color: #f5dfbc;
background: rgba(20, 14, 10, 0.72);
backdrop-filter: blur(7px);
-webkit-backdrop-filter: blur(7px);
pointer-events: none;
opacity: 0;
transition: opacity 120ms linear;
}
.annotation.visible {
opacity: 1;
}
.annotation::after {
position: absolute;
top: 50%;
width: 44px;
border-top: 1px dashed rgba(244, 223, 189, 0.55);
content: "";
}
.annotation.crust {
transform: translate(calc(-100% - 48px), -50%);
}
.annotation.crust::after {
right: -45px;
}
.annotation.cavity {
transform: translate(52px, calc(-100% - 28px));
border-left-color: rgba(230, 173, 96, 0.78);
}
.annotation.cavity::after {
left: -45px;
transform: rotate(18deg);
transform-origin: right center;
}
.annotation b {
display: block;
font-size: 9px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.annotation span {
display: block;
margin-top: 4px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 8px;
line-height: 1.4;
}
#syntheticTag {
position: fixed;
z-index: 5;
left: 18px;
bottom: 18px;
padding: 8px 10px;
border: 1px solid var(--line);
color: var(--muted);
background: rgba(24, 17, 12, 0.72);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 720px) {
#identity {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#identity p {
display: none;
}
#readout {
top: 102px;
left: 12px;
width: 245px;
padding: 11px 12px;
}
#depthBars,
#auditCopy {
display: none;
}
.metric-grid {
gap: 8px 12px;
}
.metric-value {
font-size: 13px;
}
#controls {
top: auto;
right: 12px;
bottom: 50px;
max-width: calc(100vw - 24px);
}
.control-button {
min-height: 30px;
padding: 0 9px;
font-size: 8px;
}
#legend {
right: 12px;
bottom: 12px;
}
#hint,
#syntheticTag {
display: none;
}
.annotation {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional voxel bread slice"></main>
<header id="identity" class="glass">
<div class="eyebrow">Procedural food geometry / cut 01</div>
<h1>Voxel Bread Slice</h1>
<p>Dark crust shell · true recessed crumb cavities · front and rear geometry</p>
</header>
<section id="readout" class="glass" aria-label="Geometry measurements">
<div class="readout-title">
<span>Live occupancy audit</span>
<span id="auditBadge">CHECKING</span>
</div>
<div class="metric-grid">
<div class="metric">
<span class="metric-label">Front recess zones</span>
<span id="frontZones" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Rear recess zones</span>
<span id="backZones" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Maximum depth</span>
<span id="maxDepth" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Removed volume</span>
<span id="removedVolume" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Occupied voxels</span>
<span id="occupiedVoxels" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Through-cuts</span>
<span id="throughCuts" class="metric-value">—</span>
</div>
</div>
<div id="depthBars" aria-label="Front recess depth distribution"></div>
<p id="auditCopy"></p>
</section>
<nav id="controls" aria-label="Camera and diagnostic controls">
<button class="control-button" type="button" data-view="front">Front</button>
<button class="control-button active" type="button" data-view="angle">3/4</button>
<button class="control-button" type="button" data-view="back">Back</button>
<button id="overlayButton" class="control-button" type="button" aria-pressed="false">
Cavity lattice
</button>
</nav>
<aside id="legend" class="glass" aria-label="Material legend">
<div class="legend-row"><span class="swatch crust"></span>Dark crust shell</div>
<div class="legend-row"><span class="swatch"></span>Golden crumb body</div>
<div class="legend-row"><span class="swatch recess"></span>Recessed cavity floor</div>
<div id="diagnosticLegend">
<div class="legend-row"><span class="swatch lattice"></span>Removed voxels / synthetic overlay</div>
</div>
</aside>
<div id="hint" class="glass">Drag to orbit · scroll to zoom · arrows to rotate · 1 / 2 / 3 views</div>
<div id="syntheticTag">Schematic display surface / procedural asset</div>
<div id="crustAnnotation" class="annotation crust">
<b>Dark crust</b>
<span>One-voxel perimeter shell</span>
</div>
<div id="cavityAnnotation" class="annotation cavity">
<b id="cavityTitle">Front crumb recess</b>
<span id="cavityDetail">Actual removed voxel layers</span>
</div>
<noscript>This scene requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const NX = 29;
const NY = 29;
const NZ = 9;
const VOXEL = 0.13;
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({ antialias: true, 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.06;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Orbitable voxel bread slice with recessed crumb cavities");
renderer.domElement.tabIndex = 0;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x17110d);
scene.fog = new THREE.Fog(0x17110d, 10, 21);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemisphere = new THREE.HemisphereLight(0xffe7bf, 0x21130d, 1.55);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffd19a, 3.25);
keyLight.position.set(4.7, 7.4, 6.2);
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 = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.018;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9cb8cf, 0.72);
fillLight.position.set(-4, 3, -5);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(26, 26),
new THREE.MeshStandardMaterial({ color: 0x1b1511, roughness: 0.96, metalness: 0.02 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
/*
The bread is an occupancy grid, not a textured solid. A front or rear
depth value removes that many voxel layers from the corresponding face.
*/
const shape = new Uint8Array(NX * NY);
const rowMin = new Int16Array(NY);
const rowMax = new Int16Array(NY);
const maxYAtX = new Int16Array(NX);
for (let y = 0; y < NY; y++) {
const t = y / (NY - 1);
let half;
if (t <= 0.72) {
half = 13;
} else {
const u = (t - 0.72) / 0.28;
half = Math.floor(13 * Math.sqrt(Math.max(0, 1 - u * u)));
}
if (t < 0.1) {
half -= Math.round(2 * (1 - t / 0.1));
}
half = Math.max(0, half);
const center = Math.floor((NX - 1) / 2);
rowMin[y] = center - half;
rowMax[y] = center + half;
for (let x = rowMin[y]; x <= rowMax[y]; x++) {
shape[y * NX + x] = 1;
}
}
for (let x = 0; x < NX; x++) {
for (let y = NY - 1; y >= 0; y--) {
if (shape[y * NX + x]) {
maxYAtX[x] = y;
break;
}
}
}
function isCrust(x, y) {
return (
y === 0 ||
y === maxYAtX[x] ||
x === rowMin[y] ||
x === rowMax[y]
);
}
// x, y, radiusX, radiusY, maximum depth, angular phase
const frontCavities = [
[-9, 8, 3.1, 2.0, 6, 0.4],
[ 8, 9, 3.0, 2.0, 6, 2.1],
[-4, 14, 2.7, 2.2, 7, 0.9],
[ 5, 16, 2.4, 2.0, 6, 2.7],
[ 0, 20, 2.5, 1.8, 5, 1.4],
[-8, 19, 2.2, 1.6, 5, 3.2],
[ 8, 20, 2.0, 1.6, 4, 0.2],
[-5, 24, 1.8, 1.4, 4, 2.4],
[ 4, 24, 1.8, 1.4, 4, 4.0],
[-10, 13, 1.7, 1.5, 4, 1.7],
[ 10, 13, 1.7, 1.5, 4, 5.2],
[-1, 8, 1.6, 1.8, 4, 2.8],
[ 2, 10, 1.5, 1.7, 3, 5.0],
[ 0, 25, 1.5, 1.2, 3, 1.1]
];
const backCavities = [
[-10, 10, 2.4, 1.7, 5, 0.8],
[ 9, 12, 2.2, 1.7, 5, 2.5],
[ -5, 17, 2.4, 1.8, 6, 1.7],
[ 4, 12, 2.3, 1.8, 5, 3.4],
[ -7, 22, 1.9, 1.5, 4, 0.3],
[ 7, 23, 1.8, 1.4, 4, 2.8],
[ 0, 12, 1.8, 1.7, 4, 4.8],
[ -2, 26, 1.5, 1.2, 3, 1.9],
[ 10, 15, 1.5, 1.4, 3, 3.0],
[-10, 18, 1.7, 1.4, 4, 2.2],
[ 2, 21, 2.0, 1.5, 5, 5.5]
];
function buildCavityMap(specifications, blockedMap = null) {
const map = new Uint8Array(NX * NY);
for (let y = 1; y < NY - 1; y++) {
for (let x = 1; x < NX - 1; x++) {
const index = y * NX + x;
if (!shape[index] || isCrust(x, y) || (blockedMap && blockedMap[index] > 0)) continue;
let deepest = 0;
for (const spec of specifications) {
const [sx, sy, rx, ry, maxDepth, phase] = spec;
const dx = x - sx;
const dy = y - sy;
const angle = phase + 0.37 * (sx - sy);
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
const u = (dx * cosine + dy * sine) / rx;
const v = (-dx * sine + dy * cosine) / ry;
const q = Math.hypot(u, v);
const polar = Math.atan2(v, u);
const irregularEdge =
1 +
0.16 * Math.sin(3 * polar + phase) +
0.09 * Math.sin(7 * polar + phase * 1.7) +
0.045 * Math.sin(11 * polar - phase);
if (q < irregularEdge) {
const normalizedDepth = 1 - 0.72 * (q / irregularEdge);
const depth = Math.max(1, Math.min(NZ - 2, Math.round(maxDepth * normalizedDepth)));
deepest = Math.max(deepest, depth);
}
}
map[index] = deepest;
}
}
return map;
}
const frontDepth = buildCavityMap(frontCavities);
const backDepth = buildCavityMap(backCavities, frontDepth);
function maximumDepth(map) {
let maximum = 0;
for (const depth of map) maximum = Math.max(maximum, depth);
return maximum;
}
function countZones(map) {
const visited = new Uint8Array(map.length);
let zones = 0;
for (let start = 0; start < map.length; start++) {
if (!map[start] || visited[start]) continue;
zones++;
const queue = [start];
visited[start] = 1;
while (queue.length) {
const index = queue.pop();
const x = index % NX;
const y = Math.floor(index / NX);
const neighbors = [];
if (x > 0) neighbors.push(index - 1);
if (x < NX - 1) neighbors.push(index + 1);
if (y > 0) neighbors.push(index - NX);
if (y < NY - 1) neighbors.push(index + NX);
for (const neighbor of neighbors) {
if (!visited[neighbor] && map[neighbor] > 0) {
visited[neighbor] = 1;
queue.push(neighbor);
}
}
}
}
return zones;
}
function representativeCell(map) {
let best = { x: 0, y: 0, depth: 0 };
for (let y = 0; y < NY; y++) {
for (let x = 0; x < NX; x++) {
const depth = map[y * NX + x];
if (depth > best.depth) best = { x, y, depth };
}
}
return best;
}
const frontRepresentative = representativeCell(frontDepth);
const backRepresentative = representativeCell(backDepth);
function hash01(x, y, z) {
const value = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453123;
return value - Math.floor(value);
}
function neighborMaximum(map, x, y) {
let maximum = 0;
const candidates = [
map[y * NX + x - 1],
map[y * NX + x + 1],
map[(y - 1) * NX + x],
map[(y + 1) * NX + x]
];
for (const value of candidates) {
if (Number.isFinite(value)) maximum = Math.max(maximum, value);
}
return maximum;
}
const occupied = new Set();
const instances = [];
const removedCells = [];
function cellKey(x, y, z) {
return `${x},${y},${z}`;
}
for (let y = 0; y < NY; y++) {
for (let x = 0; x < NX; x++) {
const mapIndex = y * NX + x;
if (!shape[mapIndex]) continue;
const fd = frontDepth[mapIndex];
const bd = backDepth[mapIndex];
for (let z = 0; z < NZ; z++) {
const removedByFront = z >= NZ - fd;
const removedByBack = z < bd;
if (removedByFront || removedByBack) {
removedCells.push({ x, y, z });
continue;
}
occupied.add(cellKey(x, y, z));
const crust = isCrust(x, y);
const color = new THREE.Color();
const variation = hash01(x, y, z);
if (crust) {
color.setHex(variation > 0.5 ? 0x542611 : 0x3b190d);
color.offsetHSL((variation - 0.5) * 0.015, 0, (variation - 0.5) * 0.045);
} else {
const frontWallDepth = neighborMaximum(frontDepth, x, y);
const backWallDepth = neighborMaximum(backDepth, x, y);
const frontFloorZ = NZ - fd - 1;
if (fd > 0 && z === frontFloorZ) {
color.setHex(0xa6642f);
color.multiplyScalar(0.91 + fd * 0.018);
} else if (bd > 0 && z === bd) {
color.setHex(0xa6642f);
color.multiplyScalar(0.91 + bd * 0.018);
} else if (
(frontWallDepth > 0 && z >= NZ - frontWallDepth) ||
(backWallDepth > 0 && z < backWallDepth)
) {
color.setHex(0xe3bc78);
color.offsetHSL(0, (variation - 0.5) * 0.035, (variation - 0.5) * 0.055);
} else {
color.setHex(0xd9aa67);
color.offsetHSL(
(variation - 0.5) * 0.018,
(variation - 0.5) * 0.055,
(variation - 0.5) * 0.065
);
}
}
instances.push({
x,
y,
z,
color
});
}
}
}
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const voxelMaterial = new THREE.MeshStandardMaterial({
roughness: 0.88,
metalness: 0.0
});
const bread = new THREE.InstancedMesh(voxelGeometry, voxelMaterial, instances.length);
bread.castShadow = true;
bread.receiveShadow = true;
bread.frustumCulled = false;
const dummy = new THREE.Object3D();
const centerX = (NX - 1) / 2;
const centerZ = (NZ - 1) / 2;
const gapScale = 0.965;
instances.forEach((instance, index) => {
dummy.position.set(
(instance.x - centerX) * VOXEL,
instance.y * VOXEL + VOXEL * 0.5,
(instance.z - centerZ) * VOXEL
);
dummy.scale.setScalar(VOXEL * gapScale);
dummy.updateMatrix();
bread.setMatrixAt(index, dummy.matrix);
bread.setColorAt(index, instance.color);
});
bread.instanceMatrix.needsUpdate = true;
if (bread.instanceColor) bread.instanceColor.needsUpdate = true;
scene.add(bread);
/*
Diagnostic wireframe: one outlined lattice cell for every voxel
actually omitted from the solid. It is hidden in the natural view.
*/
const latticePositions = [];
function addBoxEdges(x, y, z) {
const x0 = (x - centerX) * VOXEL - VOXEL * 0.5;
const x1 = x0 + VOXEL;
const y0 = y * VOXEL;
const y1 = y0 + VOXEL;
const z0 = (z - centerZ) * VOXEL - VOXEL * 0.5;
const z1 = z0 + VOXEL;
const p = [
[x0, y0, z0], [x1, y0, z0], [x1, y1, z0], [x0, y1, z0],
[x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1]
];
const edges = [
[0,1], [1,2], [2,3], [3,0],
[4,5], [5,6], [6,7], [7,4],
[0,4], [1,5], [2,6], [3,7]
];
for (const [a, b] of edges) {
latticePositions.push(...p[a], ...p[b]);
}
}
for (const cell of removedCells) addBoxEdges(cell.x, cell.y, cell.z);
const latticeGeometry = new THREE.BufferGeometry();
latticeGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(latticePositions, 3)
);
const lattice = new THREE.LineSegments(
latticeGeometry,
new THREE.LineBasicMaterial({
color: 0x77eadb,
transparent: true,
opacity: 0.3,
depthWrite: false
})
);
lattice.visible = false;
lattice.frustumCulled = false;
lattice.renderOrder = 4;
scene.add(lattice);
// Verify the rendered occupancy against the two cavity maps.
let auditPass = true;
let frontRemoved = 0;
let backRemoved = 0;
let shapeCells = 0;
let throughColumns = 0;
for (let y = 0; y < NY; y++) {
for (let x = 0; x < NX; x++) {
const index = y * NX + x;
if (!shape[index]) continue;
shapeCells++;
const fd = frontDepth[index];
const bd = backDepth[index];
frontRemoved += fd;
backRemoved += bd;
let occupiedInColumn = 0;
for (let z = 0; z < NZ; z++) {
const shouldBeOccupied = z < NZ - fd && z >= bd;
const isOccupied = occupied.has(cellKey(x, y, z));
if (shouldBeOccupied !== isOccupied) auditPass = false;
if (isOccupied) occupiedInColumn++;
}
if (occupiedInColumn === 0) throughColumns++;
}
}
const frontZones = countZones(frontDepth);
const backZones = countZones(backDepth);
const frontMaximum = maximumDepth(frontDepth);
const backMaximum = maximumDepth(backDepth);
const maximumRecess = Math.max(frontMaximum, backMaximum);
const removedVolume = frontRemoved + backRemoved;
auditPass =
auditPass &&
frontRemoved > 0 &&
backRemoved > 0 &&
frontMaximum >= 5 &&
backMaximum >= 4 &&
throughColumns === 0;
document.getElementById("frontZones").innerHTML =
`${frontZones} <small>zones</small>`;
document.getElementById("backZones").innerHTML =
`${backZones} <small>zones</small>`;
document.getElementById("maxDepth").innerHTML =
`${maximumRecess} <small>/ ${NZ} layers</small>`;
document.getElementById("removedVolume").innerHTML =
`${removedVolume} <small>cells</small>`;
document.getElementById("occupiedVoxels").innerHTML =
`${occupied.size.toLocaleString()} <small>/ ${(shapeCells * NZ).toLocaleString()}</small>`;
document.getElementById("throughCuts").innerHTML =
`${throughColumns} <small>columns</small>`;
const auditBadge = document.getElementById("auditBadge");
auditBadge.textContent = auditPass ? "PASS" : "FAIL";
auditBadge.style.color = auditPass ? "var(--cyan)" : "#ff8c78";
auditBadge.style.borderColor = auditPass
? "rgba(119,234,219,.42)"
: "rgba(255,140,120,.55)";
document.getElementById("auditCopy").textContent =
`${frontRemoved} front and ${backRemoved} rear voxels are absent from the occupancy set. ` +
`Maximum recess: ${frontMaximum} front / ${backMaximum} rear layers. ` +
`The two cavity faces are genuine omissions, not painted depressions.`;
const histogram = new Array(NZ).fill(0);
for (const depth of frontDepth) {
if (depth > 0) histogram[depth]++;
}
const histogramMaximum = Math.max(...histogram);
document.getElementById("depthBars").innerHTML = histogram
.slice(1)
.map((count, index) => {
const depth = index + 1;
const width = histogramMaximum ? (count / histogramMaximum) * 100 : 0;
return `
<div class="depth-row">
<span>${depth} deep</span>
<span class="depth-track"><span class="depth-fill" style="width:${width}%"></span></span>
<span>${count}</span>
</div>`;
})
.join("");
// Camera controls are implemented locally without external control modules.
const controls = {
theta: 0.48,
phi: 1.46,
radius: 6.45,
targetY: 1.88
};
let cameraTween = null;
const cameraTarget = new THREE.Vector3();
function distanceScale() {
const aspect = window.innerWidth / window.innerHeight;
return aspect < 0.9 ? 0.9 / aspect : 1;
}
function applyCamera() {
const distance = controls.radius * distanceScale();
const horizontal = Math.sin(controls.phi) * distance;
cameraTarget.set(0, controls.targetY, 0);
camera.position.set(
horizontal * Math.sin(controls.theta),
controls.targetY + Math.cos(controls.phi) * distance,
horizontal * Math.cos(controls.theta)
);
camera.lookAt(cameraTarget);
}
const viewPresets = {
front: { theta: 0, phi: 1.49, radius: 6.55, targetY: 1.88 },
angle: { theta: 0.48, phi: 1.46, radius: 6.45, targetY: 1.88 },
back: { theta: Math.PI, phi: 1.49, radius: 6.55, targetY: 1.88 }
};
function setView(name, immediate = false) {
const preset = viewPresets[name];
if (!preset) return;
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === name);
});
if (immediate) {
Object.assign(controls, preset);
cameraTween = null;
applyCamera();
return;
}
cameraTween = {
start: performance.now(),
duration: 650,
from: {
theta: controls.theta,
phi: controls.phi,
radius: controls.radius,
targetY: controls.targetY
},
to: { ...preset }
};
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => setView(button.dataset.view));
});
const overlayButton = document.getElementById("overlayButton");
let latticeVisible = false;
overlayButton.addEventListener("click", () => {
latticeVisible = !latticeVisible;
lattice.visible = latticeVisible;
overlayButton.setAttribute("aria-pressed", String(latticeVisible));
overlayButton.textContent = latticeVisible ? "Hide lattice" : "Cavity lattice";
document.body.classList.toggle("diagnostic", latticeVisible);
});
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTween = null;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(controls.phi + dy * 0.005, 0.62, 2.12);
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraTween = null;
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.0035,
4.65,
9.5
);
}, { passive: false });
canvas.addEventListener("dblclick", () => setView("angle"));
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "1") setView("front");
if (key === "2") setView("angle");
if (key === "3") setView("back");
if (key === "d") overlayButton.click();
if (key === "arrowleft") {
cameraTween = null;
controls.theta += 0.12;
} else if (key === "arrowright") {
cameraTween = null;
controls.theta -= 0.12;
} else if (key === "arrowup") {
cameraTween = null;
controls.phi = THREE.MathUtils.clamp(controls.phi - 0.08, 0.62, 2.12);
} else if (key === "arrowdown") {
cameraTween = null;
controls.phi = THREE.MathUtils.clamp(controls.phi + 0.08, 0.62, 2.12);
}
});
const crustAnnotation = document.getElementById("crustAnnotation");
const cavityAnnotation = document.getElementById("cavityAnnotation");
const cavityTitle = document.getElementById("cavityTitle");
const cavityDetail = document.getElementById("cavityDetail");
function worldPosition(x, y, z) {
return new THREE.Vector3(
(x - centerX) * VOXEL,
y * VOXEL + VOXEL * 0.5,
(z - centerZ) * VOXEL
);
}
function placeAnnotation(element, point) {
const projected = point.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1 &&
Math.abs(projected.x) < 1.15 && Math.abs(projected.y) < 1.15;
element.classList.toggle("visible", visible);
if (!visible) return;
const left = (projected.x * 0.5 + 0.5) * window.innerWidth;
const top = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${left}px`;
element.style.top = `${top}px`;
}
function updateAnnotations() {
const viewingBack = Math.cos(controls.theta) < 0;
const representative = viewingBack ? backRepresentative : frontRepresentative;
const frontPlane = NZ * VOXEL - centerZ * VOXEL;
const backPlane = -frontPlane;
const crustPoint = worldPosition(
rowMin[10],
10,
viewingBack ? 0 : NZ - 1
);
crustPoint.z = viewingBack ? backPlane : frontPlane;
const cavityPoint = worldPosition(representative.x, representative.y, 0);
cavityPoint.z = viewingBack ? backPlane : frontPlane;
const sideName = viewingBack ? "Rear" : "Front";
cavityTitle.textContent = `${sideName} crumb recess`;
cavityDetail.textContent =
`${representative.depth} actual voxel layers removed · ${NZ - representative.depth} remain`;
placeAnnotation(crustAnnotation, crustPoint);
placeAnnotation(cavityAnnotation, cavityPoint);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height);
applyCamera();
}
window.addEventListener("resize", resize);
resize();
function animate(now) {
requestAnimationFrame(animate);
if (cameraTween) {
const t = THREE.MathUtils.clamp(
(now - cameraTween.start) / cameraTween.duration,
0,
1
);
const eased = t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
for (const key of ["theta", "phi", "radius", "targetY"]) {
controls[key] = THREE.MathUtils.lerp(
cameraTween.from[key],
cameraTween.to[key],
eased
);
}
if (t >= 1) cameraTween = null;
}
applyCamera();
updateAnnotations();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```