Prompt specification
Build or customize the Three.js 3D scene "Reusable brick beside damaged fragments" 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: Reusable brick beside damaged fragments
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reusable-brick-beside-damaged-fragments-x2181
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a stylized solid study of reclaimed masonry. On three shallow trays, place one intact brick, one brick with a missing corner, and several smaller rubble fragments. Give the intact piece clear fired-clay faces and softened wear, while the broken surfaces reveal rough, lighter interior material. Use a common scale so the difference between a reusable component and loose fragments is apparent. Add small tags for observed shape and visible damage, leaving strength or suitability unassigned.
Working Three.js Starter (reusable-brick-beside-damaged-fragments-x2181):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reclaimed Masonry — Solid Study</title>
<style>
:root {
--ink: #302c27;
--muted: #777065;
--paper: rgba(249, 247, 242, 0.92);
--line: rgba(67, 58, 49, 0.24);
--clay: #a6573e;
--fracture: #c8b49a;
--fragment: #707875;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e4db;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
header {
position: fixed;
z-index: 5;
top: 28px;
left: 30px;
max-width: 470px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(29px, 3.1vw, 47px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
header p {
max-width: 420px;
margin: 13px 0 0;
color: #686158;
font-size: 12px;
line-height: 1.55;
}
.instructions {
position: fixed;
z-index: 5;
top: 30px;
right: 30px;
color: #777066;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
#annotations {
position: fixed;
z-index: 4;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.tag {
position: absolute;
width: 190px;
padding: 11px 12px 10px;
transform: translate(-50%, -100%) translateY(-21px);
border: 1px solid rgba(69, 59, 49, 0.22);
border-radius: 3px;
background: var(--paper);
box-shadow:
0 8px 25px rgba(56, 45, 35, 0.09),
0 1px 2px rgba(56, 45, 35, 0.08);
backdrop-filter: blur(8px);
}
.tag::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 20px;
background: rgba(67, 58, 49, 0.42);
}
.tag::before {
content: "";
position: absolute;
left: calc(50% - 2px);
top: calc(100% + 18px);
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--accent, var(--clay));
box-shadow: 0 0 0 3px rgba(250, 248, 243, 0.75);
}
.tag-title {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
padding-bottom: 7px;
border-bottom: 1px solid rgba(67, 58, 49, 0.14);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.tag-title i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent, var(--clay));
}
.tag-row {
display: grid;
grid-template-columns: 74px 1fr;
gap: 7px;
margin-top: 4px;
font-size: 10px;
line-height: 1.35;
}
.tag-row span {
color: #8b8379;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.tag-row b {
font-weight: 600;
}
.scale-note {
position: fixed;
z-index: 5;
left: 30px;
bottom: 25px;
display: flex;
align-items: center;
gap: 12px;
color: #777066;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.scale-mark {
position: relative;
width: 76px;
height: 8px;
border-bottom: 2px solid #696159;
}
.scale-mark::before,
.scale-mark::after {
content: "";
position: absolute;
bottom: -2px;
width: 2px;
height: 8px;
background: #696159;
}
.scale-mark::before { left: 0; }
.scale-mark::after { right: 0; }
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #e8e4db;
color: #777066;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
header {
top: 18px;
left: 18px;
max-width: 76vw;
}
header p {
font-size: 10px;
max-width: 300px;
}
.instructions {
top: auto;
right: 16px;
bottom: 18px;
}
.tag {
width: 150px;
padding: 8px 9px;
}
.tag-row {
grid-template-columns: 1fr;
gap: 1px;
}
.tag-row span {
margin-top: 4px;
}
.scale-note {
left: 18px;
bottom: 18px;
}
.scale-note > span:last-child {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header>
<div class="eyebrow">Comparative object study / 03 trays</div>
<h1>Reclaimed<br>Masonry</h1>
<p>
Observed form and visible damage only. Strength and suitability remain
unassigned.
</p>
</header>
<div class="instructions">Drag to orbit · Scroll to zoom</div>
<div id="annotations" aria-label="Object observation tags">
<article class="tag" id="tag-intact" style="--accent: var(--clay)">
<div class="tag-title"><i></i>01 · Intact form</div>
<div class="tag-row">
<span>Observed shape</span>
<b>Rectangular prism</b>
</div>
<div class="tag-row">
<span>Visible damage</span>
<b>Softened edge wear; no visible loss</b>
</div>
</article>
<article class="tag" id="tag-broken" style="--accent: var(--fracture)">
<div class="tag-title"><i></i>02 · Truncated form</div>
<div class="tag-row">
<span>Observed shape</span>
<b>Prismatic body with corner removed</b>
</div>
<div class="tag-row">
<span>Visible damage</span>
<b>Upper corner missing; exposed fracture</b>
</div>
</article>
<article class="tag" id="tag-fragments" style="--accent: var(--fragment)">
<div class="tag-title"><i></i>03 · Loose fragments</div>
<div class="tag-row">
<span>Observed shape</span>
<b>Multiple irregular solids</b>
</div>
<div class="tag-row">
<span>Visible damage</span>
<b>Exposed fracture faces throughout</b>
</div>
</article>
</div>
<div class="scale-note">
<span class="scale-mark"></span>
<span>Common scale · full brick 3.0 units</span>
</div>
<div id="loading">Preparing the study</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const host = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.03;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional comparative study of reclaimed masonry"
);
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e4db);
scene.fog = new THREE.Fog(0xe8e4db, 27, 54);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.42, 0);
let azimuth = 0.055;
let polar = 1.04;
let zoom = 1;
let baseRadius = 18.5;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const radius = baseRadius * zoom;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = aspect;
camera.updateProjectionMatrix();
// Keep all three trays visible on narrow displays.
baseRadius = 18.5 * Math.max(1, 1.22 / aspect);
updateCamera();
}
window.addEventListener("resize", resize);
// ------------------------------------------------------------
// Direct camera interaction — no external controls addon
// ------------------------------------------------------------
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
azimuth -= dx * 0.0055;
polar = THREE.MathUtils.clamp(polar + dy * 0.0045, 0.48, 1.34);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(
zoom * Math.exp(event.deltaY * 0.001),
0.72,
1.65
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
azimuth = 0.055;
polar = 1.04;
zoom = 1;
updateCamera();
});
// ------------------------------------------------------------
// Lighting and ground
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xfff8ea,
0x7b766d,
2.15
);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff1d8, 3.25);
key.position.set(-5.5, 11.5, 8.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -11;
key.shadow.camera.right = 11;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 32;
key.shadow.bias = -0.00035;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0xdce5eb, 0.85);
fill.position.set(8, 5, -7);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xe8e4db,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Procedural surface textures
// ------------------------------------------------------------
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6D2B79F5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function hexToRgb(hex) {
return {
r: (hex >> 16) & 255,
g: (hex >> 8) & 255,
b: hex & 255
};
}
function makeMasonryTexture({
base,
light,
dark,
seed,
size = 256,
repeatX = 1.7,
repeatY = 1.35
}) {
const random = seededRandom(seed);
const colorCanvas = document.createElement("canvas");
const bumpCanvas = document.createElement("canvas");
colorCanvas.width = colorCanvas.height = size;
bumpCanvas.width = bumpCanvas.height = size;
const colorContext = colorCanvas.getContext("2d");
const bumpContext = bumpCanvas.getContext("2d");
const colorImage = colorContext.createImageData(size, size);
const bumpImage = bumpContext.createImageData(size, size);
const baseRgb = hexToRgb(base);
for (let i = 0; i < size * size; i++) {
const n = (random() - 0.5) * 24;
const speck = random() < 0.022 ? (random() - 0.5) * 62 : 0;
const p = i * 4;
colorImage.data[p] = THREE.MathUtils.clamp(baseRgb.r + n + speck, 0, 255);
colorImage.data[p + 1] = THREE.MathUtils.clamp(baseRgb.g + n * 0.8 + speck * 0.55, 0, 255);
colorImage.data[p + 2] = THREE.MathUtils.clamp(baseRgb.b + n * 0.7 + speck * 0.35, 0, 255);
colorImage.data[p + 3] = 255;
const grain = 128 + (random() - 0.5) * 92 + speck * 0.25;
bumpImage.data[p] = bumpImage.data[p + 1] = bumpImage.data[p + 2] =
THREE.MathUtils.clamp(grain, 0, 255);
bumpImage.data[p + 3] = 255;
}
colorContext.putImageData(colorImage, 0, 0);
bumpContext.putImageData(bumpImage, 0, 0);
// Small inclusions and firing flecks.
for (let i = 0; i < 170; i++) {
const x = random() * size;
const y = random() * size;
const radius = 0.25 + random() * 1.4;
colorContext.fillStyle = random() > 0.48
? `rgba(${hexToRgb(light).r},${hexToRgb(light).g},${hexToRgb(light).b},0.18)`
: `rgba(${hexToRgb(dark).r},${hexToRgb(dark).g},${hexToRgb(dark).b},0.16)`;
colorContext.beginPath();
colorContext.arc(x, y, radius, 0, Math.PI * 2);
colorContext.fill();
}
const colorTexture = new THREE.CanvasTexture(colorCanvas);
colorTexture.colorSpace = THREE.SRGBColorSpace;
colorTexture.wrapS = colorTexture.wrapT = THREE.RepeatWrapping;
colorTexture.repeat.set(repeatX, repeatY);
colorTexture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const bumpTexture = new THREE.CanvasTexture(bumpCanvas);
bumpTexture.colorSpace = THREE.NoColorSpace;
bumpTexture.wrapS = bumpTexture.wrapT = THREE.RepeatWrapping;
bumpTexture.repeat.set(repeatX, repeatY);
bumpTexture.anisotropy = colorTexture.anisotropy;
return { colorTexture, bumpTexture };
}
const clayTextures = makeMasonryTexture({
base: 0xa9553c,
light: 0xd3835f,
dark: 0x713426,
seed: 19,
repeatX: 1.75,
repeatY: 1.25
});
const interiorTextures = makeMasonryTexture({
base: 0xc6b198,
light: 0xe2d1b7,
dark: 0x8e7965,
seed: 47,
size: 256,
repeatX: 2.3,
repeatY: 2.0
});
const clayMaterial = new THREE.MeshStandardMaterial({
map: clayTextures.colorTexture,
bumpMap: clayTextures.bumpTexture,
bumpScale: 0.035,
roughness: 0.86,
metalness: 0,
side: THREE.DoubleSide
});
const clayTopMaterial = clayMaterial.clone();
clayTopMaterial.roughness = 0.8;
clayTopMaterial.bumpScale = 0.026;
const interiorMaterial = new THREE.MeshStandardMaterial({
map: interiorTextures.colorTexture,
bumpMap: interiorTextures.bumpTexture,
bumpScale: 0.115,
roughness: 0.98,
metalness: 0,
side: THREE.DoubleSide
});
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
function roundedBoxGeometry(width, height, depth, radius, segments = 6) {
const geometry = new THREE.BoxGeometry(
width,
height,
depth,
segments,
segments,
segments
);
const position = geometry.attributes.position;
const hx = width / 2;
const hy = height / 2;
const hz = depth / 2;
const ix = hx - radius;
const iy = hy - radius;
const iz = hz - radius;
for (let i = 0; i < position.count; i++) {
let x = position.getX(i);
let y = position.getY(i);
let z = position.getZ(i);
const cx = THREE.MathUtils.clamp(x, -ix, ix);
const cy = THREE.MathUtils.clamp(y, -iy, iy);
const cz = THREE.MathUtils.clamp(z, -iz, iz);
const dx = x - cx;
const dy = y - cy;
const dz = z - cz;
const length = Math.hypot(dx, dy, dz);
if (length > 0.00001) {
x = cx + (dx / length) * radius;
y = cy + (dy / length) * radius;
z = cz + (dz / length) * radius;
}
position.setXYZ(i, x, y, z);
}
position.needsUpdate = true;
geometry.computeVertexNormals();
return geometry;
}
function makeContactShadowTexture() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 128;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(64, 64, 4, 64, 64, 62);
gradient.addColorStop(0, "rgba(38,29,20,0.42)");
gradient.addColorStop(0.48, "rgba(38,29,20,0.20)");
gradient.addColorStop(1, "rgba(38,29,20,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
return new THREE.CanvasTexture(canvas);
}
const contactShadowTexture = makeContactShadowTexture();
const contactShadowMaterial = new THREE.MeshBasicMaterial({
map: contactShadowTexture,
transparent: true,
depthWrite: false,
opacity: 0.62
});
function addContactShadow(parent, x, z, width, depth, y = 0.245) {
const shadow = new THREE.Mesh(
new THREE.PlaneGeometry(width, depth),
contactShadowMaterial
);
shadow.rotation.x = -Math.PI / 2;
shadow.position.set(x, y, z);
parent.add(shadow);
}
// ------------------------------------------------------------
// Three shallow trays, all using the same dimensions
// ------------------------------------------------------------
const traySurfaceY = 0.235;
function buildTray(x, accentColor) {
const tray = new THREE.Group();
tray.position.x = x;
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0xd4d0c7,
roughness: 0.92
});
const insetMaterial = new THREE.MeshStandardMaterial({
color: 0xe5e1d8,
roughness: 0.97
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xb8b1a6,
roughness: 0.88
});
const base = new THREE.Mesh(
roundedBoxGeometry(4.72, 0.18, 3.18, 0.065, 5),
baseMaterial
);
base.position.y = 0.09;
base.castShadow = true;
base.receiveShadow = true;
tray.add(base);
const inset = new THREE.Mesh(
roundedBoxGeometry(4.34, 0.055, 2.80, 0.035, 4),
insetMaterial
);
inset.position.y = 0.207;
inset.receiveShadow = true;
tray.add(inset);
const frontRim = new THREE.Mesh(
roundedBoxGeometry(4.72, 0.31, 0.13, 0.055, 5),
rimMaterial
);
frontRim.position.set(0, 0.31, 1.525);
frontRim.castShadow = true;
tray.add(frontRim);
const backRim = frontRim.clone();
backRim.position.z = -1.525;
tray.add(backRim);
const sideRim = new THREE.Mesh(
roundedBoxGeometry(0.13, 0.31, 2.91, 0.055, 5),
rimMaterial
);
sideRim.position.set(2.295, 0.31, 0);
sideRim.castShadow = true;
tray.add(sideRim);
const otherSideRim = sideRim.clone();
otherSideRim.position.x = -2.295;
tray.add(otherSideRim);
const marker = new THREE.Mesh(
roundedBoxGeometry(0.38, 0.16, 0.035, 0.035, 4),
new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.76
})
);
marker.position.set(1.97, 0.31, 1.608);
marker.castShadow = true;
tray.add(marker);
scene.add(tray);
return tray;
}
const intactTray = buildTray(-4.7, 0xa6553d);
const brokenTray = buildTray(0, 0xc3ae90);
const fragmentTray = buildTray(4.7, 0x69736f);
// ------------------------------------------------------------
// Intact fired-clay brick with softened, rounded wear
// ------------------------------------------------------------
const brickWidth = 3.1;
const brickHeight = 1.22;
const brickDepth = 1.52;
const intactBrick = new THREE.Mesh(
roundedBoxGeometry(
brickWidth,
brickHeight,
brickDepth,
0.115,
10
),
[
clayMaterial,
clayMaterial,
clayTopMaterial,
clayMaterial,
clayMaterial,
clayMaterial
]
);
intactBrick.position.set(-4.7, traySurfaceY + brickHeight / 2, 0);
intactBrick.rotation.y = -0.075;
intactBrick.castShadow = true;
intactBrick.receiveShadow = true;
scene.add(intactBrick);
addContactShadow(
intactTray,
0,
0,
3.35,
1.85,
traySurfaceY + 0.004
);
// ------------------------------------------------------------
// Broken brick: beveled exterior with a separately assigned,
// lighter and rougher fracture around the missing corner
// ------------------------------------------------------------
const footprint = [
new THREE.Vector2(-brickWidth / 2, -brickDepth / 2),
new THREE.Vector2( brickWidth / 2, -brickDepth / 2),
new THREE.Vector2( brickWidth / 2, -0.02),
new THREE.Vector2( 1.12, 0.22),
new THREE.Vector2( 0.86, 0.46),
new THREE.Vector2( 1.06, brickDepth / 2),
new THREE.Vector2(-brickWidth / 2, brickDepth / 2)
];
const brokenShape = new THREE.Shape();
brokenShape.moveTo(footprint[0].x, footprint[0].y);
for (let i = 1; i < footprint.length; i++) {
brokenShape.lineTo(footprint[i].x, footprint[i].y);
}
brokenShape.closePath();
const brokenGeometry = new THREE.ExtrudeGeometry(brokenShape, {
depth: brickHeight,
steps: 1,
curveSegments: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.055,
bevelThickness: 0.055
});
// Shape XY becomes world XZ; extrusion becomes world Y.
brokenGeometry.rotateX(Math.PI / 2);
brokenGeometry.translate(0, brickHeight / 2, 0);
brokenGeometry.computeVertexNormals();
function distanceToSegment(px, pz, a, b) {
const abx = b.x - a.x;
const abz = b.y - a.y;
const apx = px - a.x;
const apz = pz - a.y;
const denominator = abx * abx + abz * abz;
const t = denominator > 0
? THREE.MathUtils.clamp(
(apx * abx + apz * abz) / denominator,
0,
1
)
: 0;
const dx = px - (a.x + abx * t);
const dz = pz - (a.y + abz * t);
return Math.hypot(dx, dz);
}
const fractureSegments = [
[footprint[2], footprint[3]],
[footprint[3], footprint[4]],
[footprint[4], footprint[5]]
];
brokenGeometry.clearGroups();
const brokenPosition = brokenGeometry.attributes.position;
let groupStart = 0;
let activeMaterial = -1;
for (let i = 0; i < brokenPosition.count; i += 3) {
const cx = (
brokenPosition.getX(i) +
brokenPosition.getX(i + 1) +
brokenPosition.getX(i + 2)
) / 3;
const cz = (
brokenPosition.getZ(i) +
brokenPosition.getZ(i + 1) +
brokenPosition.getZ(i + 2)
) / 3;
let isFracture = false;
for (const segment of fractureSegments) {
if (distanceToSegment(cx, cz, segment[0], segment[1]) < 0.14) {
isFracture = true;
break;
}
}
const materialIndex = isFracture ? 1 : 0;
if (activeMaterial !== materialIndex) {
if (activeMaterial !== -1) {
brokenGeometry.addGroup(
groupStart,
i - groupStart,
activeMaterial
);
}
groupStart = i;
activeMaterial = materialIndex;
}
}
if (activeMaterial !== -1) {
brokenGeometry.addGroup(
groupStart,
brokenPosition.count - groupStart,
activeMaterial
);
}
const brokenBrick = new THREE.Mesh(
brokenGeometry,
[clayMaterial, interiorMaterial]
);
brokenBrick.position.set(0, traySurfaceY, 0);
brokenBrick.rotation.y = 0.035;
brokenBrick.castShadow = true;
brokenBrick.receiveShadow = true;
scene.add(brokenBrick);
addContactShadow(
brokenTray,
-0.05,
0,
3.28,
1.82,
traySurfaceY + 0.004
);
// A few tiny crumbs emphasize the newly exposed corner without
// turning the broken piece into a second rubble group.
for (let i = 0; i < 3; i++) {
const crumb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.035 + i * 0.008, 0),
interiorMaterial
);
crumb.position.set(
0.78 + i * 0.12,
traySurfaceY + 0.035,
0.68 - i * 0.09
);
crumb.scale.y = 0.55;
crumb.castShadow = true;
scene.add(crumb);
}
// ------------------------------------------------------------
// Irregular rubble fragments
// ------------------------------------------------------------
function createFragmentGeometry(seed) {
const random = seededRandom(seed);
const t = (1 + Math.sqrt(5)) / 2;
const vertices = [
[-1, t, 0], [1, t, 0], [-1, -t, 0], [1, -t, 0],
[0, -1, t], [0, 1, t], [0, -1, -t], [0, 1, -t],
[t, 0, -1], [t, 0, 1], [-t, 0, -1], [-t, 0, 1]
];
const indices = [
0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
];
const positions = [];
const uvs = [];
for (const vertex of vertices) {
const length = Math.hypot(...vertex);
const variation = 0.82 + random() * 0.36;
const x = vertex[0] / length * variation;
const y = vertex[1] / length * variation;
const z = vertex[2] / length * variation;
positions.push(x, y, z);
uvs.push(
Math.atan2(z, x) / (Math.PI * 2) + 0.5,
y * 0.5 + 0.5
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
const breakAxis = new THREE.Vector3(
random() * 1.8 - 0.9,
random() * 1.2 - 0.6,
random() * 1.8 - 0.9
).normalize();
geometry.clearGroups();
for (let i = 0; i < indices.length; i += 3) {
const a = indices[i];
const b = indices[i + 1];
const c = indices[i + 2];
const center = new THREE.Vector3(
(positions[a * 3] + positions[b * 3] + positions[c * 3]) / 3,
(positions[a * 3 + 1] + positions[b * 3 + 1] + positions[c * 3 + 1]) / 3,
(positions[a * 3 + 2] + positions[b * 3 + 2] + positions[c * 3 + 2]) / 3
).normalize();
const materialIndex = center.dot(breakAxis) > 0.27 ? 1 : 0;
geometry.addGroup(i, 3, materialIndex);
}
geometry.computeVertexNormals();
return geometry;
}
const fragmentData = [
[-0.67, 0.12, 0.56, 0.39, 0.46],
[ 0.22, 0.37, 0.50, 0.34, 0.44],
[ 0.83, -0.34, 0.43, 0.29, 0.37],
[-0.16, -0.48, 0.40, 0.27, 0.34],
[-1.23, -0.42, 0.31, 0.21, 0.29],
[ 1.15, 0.48, 0.29, 0.20, 0.27],
[-0.83, 0.76, 0.24, 0.17, 0.22],
[ 0.18, 0.93, 0.22, 0.16, 0.20],
[-0.45, 0.53, 0.16, 0.12, 0.15],
[ 0.57, 0.04, 0.14, 0.10, 0.13],
[-1.05, 0.10, 0.12, 0.09, 0.11],
[ 0.38, -0.92, 0.13, 0.09, 0.12]
];
fragmentData.forEach((data, index) => {
const [x, z, sx, sy, sz] = data;
const random = seededRandom(300 + index * 17);
const fragment = new THREE.Mesh(
createFragmentGeometry(300 + index * 17),
[clayMaterial, interiorMaterial]
);
fragment.position.set(
x,
traySurfaceY + sy * 0.70,
z
);
fragment.scale.set(sx, sy, sz);
fragment.rotation.set(
random() * Math.PI,
random() * Math.PI * 2,
random() * Math.PI
);
fragment.castShadow = true;
fragment.receiveShadow = true;
fragmentTray.add(fragment);
addContactShadow(
fragmentTray,
x,
z,
sx * 2.1,
sz * 2.1,
traySurfaceY + 0.003
);
});
// ------------------------------------------------------------
// Screen-space annotation anchors
// ------------------------------------------------------------
const annotationData = [
{
element: document.querySelector("#tag-intact"),
position: new THREE.Vector3(-4.7, 1.73, 0)
},
{
element: document.querySelector("#tag-broken"),
position: new THREE.Vector3(0, 1.78, 0.04)
},
{
element: document.querySelector("#tag-fragments"),
position: new THREE.Vector3(4.7, 1.30, 0.08)
}
];
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const annotation of annotationData) {
const projected = annotation.position.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
annotation.element.style.left = `${x}px`;
annotation.element.style.top = `${y}px`;
annotation.element.style.opacity =
projected.z > -1 && projected.z < 1 ? "1" : "0";
}
}
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
resize();
updateCamera();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
updateAnnotations();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => loading.classList.add("hidden"));
}
}
animate();
</script>
</body>
</html>
```