Prompt specification
Build or customize the Three.js 3D scene "Roman loaf mold and grain measure" 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: Roman loaf mold and grain measure
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/roman-loaf-mold-and-grain-measure-x2309
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a stylized Roman bread-and-grain study. On a plain counter, place a round scored loaf, an open grain measure, and a small storage vessel with grain visible near its rim. Use a source-informed loaf form and vessel silhouette, without inventing an exact ancient capacity. Model the loaf's raised crust, score grooves, and a few loose kernels so the raw material and finished food remain visually connected. Arrange the three objects in a shallow triangle with unobstructed silhouettes. Warm clay, muted stone, and golden grain should define the palette. A small cut section of the loaf may reveal its interior texture using geometry.
Working Three.js Starter (roman-loaf-mold-and-grain-measure-x2309):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Panis — Roman Bread & Grain Study</title>
<style>
:root {
color-scheme: light;
--ink: #35271e;
--muted: #756154;
--clay: #a75f43;
--grain: #d5a247;
--stone: #bba98e;
--paper: rgba(244, 235, 218, 0.82);
--line: rgba(66, 43, 28, 0.18);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9c8af;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, summary { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
canvas:active { cursor: grabbing; }
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.masthead {
position: absolute;
top: 28px;
left: 32px;
max-width: 430px;
text-shadow: 0 1px 18px rgba(255, 246, 230, 0.75);
}
.eyebrow {
margin: 0 0 7px;
color: var(--clay);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(29px, 4vw, 54px);
font-weight: 500;
letter-spacing: -0.035em;
line-height: 0.96;
}
.subtitle {
max-width: 350px;
margin: 12px 0 0;
color: #59483b;
font-size: 12px;
line-height: 1.55;
}
.toolbar {
position: absolute;
top: 26px;
right: 28px;
display: flex;
gap: 8px;
pointer-events: auto;
}
.toolbar button {
min-height: 36px;
padding: 8px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
color: var(--ink);
cursor: pointer;
backdrop-filter: blur(12px);
box-shadow: 0 5px 22px rgba(63, 39, 23, 0.08);
font-size: 11px;
font-weight: 650;
transition: background 150ms ease, transform 150ms ease;
}
.toolbar button:hover {
background: rgba(255, 248, 235, 0.96);
transform: translateY(-1px);
}
.toolbar button:focus-visible,
summary:focus-visible {
outline: 2px solid var(--clay);
outline-offset: 3px;
}
.palette {
position: absolute;
left: 32px;
bottom: 29px;
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.chip {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(244, 235, 218, 0.72);
backdrop-filter: blur(12px);
color: #5d4d40;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}
.swatch.clay { background: #a75f43; }
.swatch.stone { background: #b8a487; }
.swatch.grain { background: #d6a347; }
.notes {
position: absolute;
right: 28px;
bottom: 28px;
width: min(365px, calc(100vw - 56px));
pointer-events: auto;
}
.notes summary {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--paper);
color: var(--ink);
cursor: pointer;
list-style: none;
backdrop-filter: blur(14px);
box-shadow: 0 8px 30px rgba(63, 39, 23, 0.08);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
}
.notes summary::-webkit-details-marker { display: none; }
.notes summary::after {
content: "+";
margin-left: 4px;
color: var(--clay);
font-size: 17px;
font-weight: 400;
}
.notes[open] summary::after { content: "−"; }
.note-body {
margin-top: 8px;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(244, 235, 218, 0.92);
color: #59483b;
backdrop-filter: blur(16px);
font-size: 11px;
line-height: 1.65;
box-shadow: 0 10px 34px rgba(63, 39, 23, 0.1);
}
.note-body p { margin: 0 0 9px; }
.note-body p:last-child { margin-bottom: 0; }
.note-body strong {
color: #3e2e23;
}
.labels {
position: absolute;
inset: 0;
pointer-events: none;
transition: opacity 180ms ease;
}
.labels.hidden { opacity: 0; }
.object-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
transform: translate(-50%, -50%);
white-space: nowrap;
color: #4b392c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
text-shadow: 0 1px 9px #f0dfc4;
}
.object-label::before {
content: "";
width: 6px;
height: 6px;
border: 1px solid rgba(83, 51, 31, 0.65);
border-radius: 50%;
background: rgba(248, 231, 198, 0.75);
box-shadow: 0 0 0 3px rgba(248, 231, 198, 0.28);
}
.hint {
position: absolute;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
padding: 7px 11px;
border-radius: 999px;
background: rgba(244, 235, 218, 0.62);
color: #695547;
font-size: 10px;
letter-spacing: 0.04em;
backdrop-filter: blur(10px);
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #d9c8af;
color: #604635;
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
transition: opacity 350ms ease;
z-index: 10;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 720px) {
.masthead { top: 22px; left: 21px; }
.subtitle { max-width: 250px; }
.toolbar { top: 20px; right: 18px; }
.toolbar button { padding: 8px 10px; }
#toggle-labels { display: none; }
.palette { left: 20px; bottom: 20px; }
.chip { font-size: 8px; padding: 6px 8px; }
.notes { right: 18px; bottom: 66px; }
.hint { display: none; }
.object-label { display: none; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive Roman bread and grain still life"></main>
<div class="ui">
<header class="masthead">
<p class="eyebrow">A source-informed material study</p>
<h1>Panis & Grain</h1>
<p class="subtitle">
A compact Roman-style loaf, an open grain measure, and a small storage
vessel. Warm clay, muted stone, and golden grain.
</p>
</header>
<nav class="toolbar" aria-label="Scene controls">
<button id="toggle-labels" type="button" aria-pressed="true">Labels</button>
<button id="reset-view" type="button">Reset view</button>
</nav>
<div id="labels" class="labels" aria-hidden="true">
<div id="label-loaf" class="object-label">Scored panis</div>
<div id="label-measure" class="object-label">Open grain measure</div>
<div id="label-jar" class="object-label">Storage vessel</div>
</div>
<div class="palette" aria-label="Color palette">
<span class="chip"><span class="swatch clay"></span>Clay</span>
<span class="chip"><span class="swatch stone"></span>Stone</span>
<span class="chip"><span class="swatch grain"></span>Grain</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<details class="notes">
<summary>Source & interpretation</summary>
<div class="note-body">
<p>
<strong>Loaf form:</strong> the compact, circular panis form references
carbonized loaves recovered at Pompeii and Herculaneum, including
collections documented by the Museo Archeologico Nazionale di Napoli
and the British Museum.
</p>
<p>
<strong>Vessel form:</strong> the ceramic silhouettes broadly follow
Roman household vessel profiles. Their proportions are illustrative.
No exact ancient type, recipe, or capacity is claimed.
</p>
<p>
The scoring, contents, arrangement, and exposed crumb are modern
interpretive choices connecting the raw grain to the finished loaf.
</p>
</div>
</details>
</div>
<p class="sr-only">
A warm, studio-lit arrangement on a stone counter. A round scored brown
loaf sits at the left, an open clay bowl heaped with golden grain sits at
the right, and a narrow-necked storage jar with grain near its rim stands
behind them. Loose kernels connect the loaf and bowl.
</p>
<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";
// ---------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ---------------------------------------------------------------------
const stage = document.querySelector("#stage");
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional Roman bread and grain scene. Drag to orbit, scroll to zoom."
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9c8af);
scene.fog = new THREE.Fog(0xd9c8af, 8.5, 15.5);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
40
);
const controls = {
target: new THREE.Vector3(-0.05, 0.43, 0.05),
yaw: 0.24,
pitch: 0.47,
distance: 6.65,
desiredYaw: 0.24,
desiredPitch: 0.47,
desiredDistance: 6.65
};
function updateCamera() {
const cosPitch = Math.cos(controls.pitch);
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * cosPitch * controls.distance,
controls.target.y + Math.sin(controls.pitch) * controls.distance,
controls.target.z + Math.cos(controls.yaw) * cosPitch * controls.distance
);
camera.lookAt(controls.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff0d7, 0x715744, 2.05);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffd6a0, 3.45);
key.position.set(-3.8, 6.3, 4.6);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 5;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 15;
key.shadow.bias = -0.00025;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0xbfd0d5, 1.0);
fill.position.set(4.5, 3.6, -2.5);
scene.add(fill);
const rim = new THREE.PointLight(0xffb263, 12, 10, 2);
rim.position.set(2.6, 3.0, 2.2);
scene.add(rim);
// ---------------------------------------------------------------------
// Deterministic procedural textures
// ---------------------------------------------------------------------
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6D2B79F5;
let result = value;
result = Math.imul(result ^ (result >>> 15), result | 1);
result ^= result + Math.imul(result ^ (result >>> 7), result | 61);
return ((result ^ (result >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(190273);
function makeSurfaceTexture(base, variation, fleckCount, seed) {
const size = 256;
const canvas = document.createElement("canvas");
canvas.width = canvas.height = size;
const context = canvas.getContext("2d");
const image = context.createImageData(size, size);
const localRandom = seededRandom(seed);
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const index = (y * size + x) * 4;
const wave =
Math.sin(x * 0.071 + Math.sin(y * 0.025) * 2.0) * 2.4 +
Math.sin(y * 0.13 + x * 0.018) * 1.2;
const noise = (localRandom() - 0.5) * variation + wave;
image.data[index] = Math.max(0, Math.min(255, base[0] + noise));
image.data[index + 1] = Math.max(0, Math.min(255, base[1] + noise));
image.data[index + 2] = Math.max(0, Math.min(255, base[2] + noise));
image.data[index + 3] = 255;
}
}
context.putImageData(image, 0, 0);
for (let i = 0; i < fleckCount; i++) {
const light = localRandom() > 0.48;
const alpha = 0.035 + localRandom() * 0.075;
const radius = 0.25 + localRandom() * 1.35;
context.fillStyle = light
? `rgba(255,245,220,${alpha})`
: `rgba(54,31,18,${alpha})`;
context.beginPath();
context.arc(
localRandom() * size,
localRandom() * size,
radius,
0,
Math.PI * 2
);
context.fill();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const stoneTexture = makeSurfaceTexture(
[232, 225, 213], 15, 1700, 41
);
stoneTexture.repeat.set(2.4, 1.7);
const ceramicTexture = makeSurfaceTexture(
[232, 216, 200], 12, 1200, 83
);
ceramicTexture.repeat.set(2.2, 1.5);
const crustTexture = makeSurfaceTexture(
[232, 211, 185], 26, 2600, 127
);
crustTexture.repeat.set(1.8, 1.5);
const crumbTexture = makeSurfaceTexture(
[240, 224, 194], 20, 3400, 211
);
crumbTexture.repeat.set(1.5, 1.5);
// ---------------------------------------------------------------------
// Counter and backdrop
// ---------------------------------------------------------------------
const counterMaterial = new THREE.MeshStandardMaterial({
color: 0xb7a181,
map: stoneTexture,
roughness: 0.95,
metalness: 0
});
const counter = new THREE.Mesh(
new THREE.BoxGeometry(8.2, 0.24, 5.2),
counterMaterial
);
counter.position.y = -0.12;
counter.receiveShadow = true;
scene.add(counter);
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xd5c3a8,
roughness: 1
})
);
backdrop.rotation.x = -Math.PI / 2;
backdrop.position.y = -0.245;
backdrop.receiveShadow = true;
scene.add(backdrop);
// ---------------------------------------------------------------------
// Shared materials
// ---------------------------------------------------------------------
const bowlMaterial = new THREE.MeshStandardMaterial({
color: 0xbd7552,
map: ceramicTexture,
roughness: 0.78,
metalness: 0
});
const jarMaterial = new THREE.MeshStandardMaterial({
color: 0xa85f43,
map: ceramicTexture,
roughness: 0.8,
metalness: 0
});
const darkClayMaterial = new THREE.MeshStandardMaterial({
color: 0x70412f,
roughness: 0.9
});
const crustMaterial = new THREE.MeshStandardMaterial({
color: 0xb5682e,
map: crustTexture,
roughness: 0.9,
metalness: 0,
vertexColors: true
});
const sideCrustMaterial = new THREE.MeshStandardMaterial({
color: 0xa85c29,
map: crustTexture,
roughness: 0.94,
side: THREE.DoubleSide
});
const scoreMaterial = new THREE.MeshStandardMaterial({
color: 0x633018,
roughness: 1,
side: THREE.DoubleSide
});
const crumbMaterial = new THREE.MeshStandardMaterial({
color: 0xe3bd79,
map: crumbTexture,
roughness: 1
});
const sliceCrustMaterial = new THREE.MeshStandardMaterial({
color: 0xa75b27,
map: crustTexture,
roughness: 0.95
});
// ---------------------------------------------------------------------
// Loaf geometry: custom domed mesh with modeled score depressions
// ---------------------------------------------------------------------
const LOAF_RX = 1.12;
const LOAF_RZ = 0.94;
const SCORE_PATHS = [
[
[-0.78, -0.32],
[-0.42, -0.18],
[0.00, 0.00],
[0.42, 0.16],
[0.78, 0.29]
],
[
[-0.74, 0.29],
[-0.36, 0.13],
[0.00, 0.00],
[0.38, -0.15],
[0.75, -0.31]
]
];
const scoreSegments = [];
for (const path of SCORE_PATHS) {
for (let i = 0; i < path.length - 1; i++) {
scoreSegments.push({
ax: path[i][0] * LOAF_RX,
az: path[i][1] * LOAF_RZ,
bx: path[i + 1][0] * LOAF_RX,
bz: path[i + 1][1] * LOAF_RZ
});
}
}
function distanceToScore(x, z) {
let minimum = Infinity;
for (const segment of scoreSegments) {
const vx = segment.bx - segment.ax;
const vz = segment.bz - segment.az;
const wx = x - segment.ax;
const wz = z - segment.az;
const lengthSquared = vx * vx + vz * vz;
const t = lengthSquared > 0
? THREE.MathUtils.clamp(
(wx * vx + wz * vz) / lengthSquared,
0,
1
)
: 0;
const dx = x - (segment.ax + vx * t);
const dz = z - (segment.az + vz * t);
minimum = Math.min(minimum, Math.hypot(dx, dz));
}
return minimum;
}
function loafHeight(x, z) {
const normalizedRadius = Math.min(
1,
Math.hypot(x / LOAF_RX, z / LOAF_RZ)
);
const dome = Math.sqrt(Math.max(0, 1 - normalizedRadius * normalizedRadius));
const edgeFade = THREE.MathUtils.clamp(
(1 - normalizedRadius) * 7,
0,
1
);
const noise =
Math.sin(x * 15.3 + z * 5.1) * 0.006 +
Math.sin(x * 33.7 - z * 24.1) * 0.0035 +
Math.sin((x + z) * 48.0) * 0.0015;
const distance = distanceToScore(x, z);
const groove = 0.092 * Math.exp(-Math.pow(distance / 0.052, 2));
const raisedLip = 0.026 *
Math.exp(-Math.pow((distance - 0.09) / 0.036, 2));
return 0.16 + dome * 0.50 + noise - groove * edgeFade +
raisedLip * edgeFade;
}
function createLoafTop() {
const radialSegments = 38;
const angularSegments = 128;
const positions = [];
const colors = [];
const uvs = [];
const indices = [];
const outerRing = [];
positions.push(0, loafHeight(0, 0), 0);
colors.push(0.96, 0.90, 0.79);
uvs.push(0.5, 0.5);
for (let ring = 1; ring <= radialSegments; ring++) {
const radius = ring / radialSegments;
for (let segment = 0; segment < angularSegments; segment++) {
const angle = segment / angularSegments * Math.PI * 2;
const edgeVariation =
1 +
Math.sin(angle * 5 + 0.4) * 0.016 +
Math.sin(angle * 9 - 0.8) * 0.009;
const x = Math.cos(angle) * LOAF_RX * radius * edgeVariation;
const z = Math.sin(angle) * LOAF_RZ * radius * edgeVariation;
const y = loafHeight(x, z);
const distance = distanceToScore(x, z);
const warmth = 0.94 + random() * 0.09;
let color = new THREE.Color(warmth, warmth * 0.95, warmth * 0.88);
if (distance < 0.055) {
color.lerp(new THREE.Color(0.55, 0.29, 0.14), 0.66);
} else if (Math.abs(distance - 0.09) < 0.04) {
color.lerp(new THREE.Color(1.05, 0.88, 0.60), 0.42);
}
positions.push(x, y, z);
colors.push(color.r, color.g, color.b);
uvs.push(
x / (LOAF_RX * 2) + 0.5,
z / (LOAF_RZ * 2) + 0.5
);
if (ring === radialSegments) {
outerRing.push(new THREE.Vector3(x, y, z));
}
}
}
for (let segment = 0; segment < angularSegments; segment++) {
const current = 1 + segment;
const next = 1 + (segment + 1) % angularSegments;
indices.push(0, next, current);
}
for (let ring = 1; ring < radialSegments; ring++) {
const innerStart = 1 + (ring - 1) * angularSegments;
const outerStart = 1 + ring * angularSegments;
for (let segment = 0; segment < angularSegments; segment++) {
const nextSegment = (segment + 1) % angularSegments;
const innerCurrent = innerStart + segment;
const innerNext = innerStart + nextSegment;
const outerCurrent = outerStart + segment;
const outerNext = outerStart + nextSegment;
indices.push(
innerCurrent, innerNext, outerCurrent,
innerNext, outerNext, outerCurrent
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return { geometry, outerRing };
}
function createLoafSide(outerRing) {
const positions = [];
const uvs = [];
const indices = [];
const count = outerRing.length;
for (const point of outerRing) {
positions.push(point.x, point.y, point.z);
uvs.push(
point.x / (LOAF_RX * 2) + 0.5,
point.z / (LOAF_RZ * 2) + 0.5
);
}
for (const point of outerRing) {
positions.push(point.x * 0.965, 0.025, point.z * 0.965);
uvs.push(
point.x / (LOAF_RX * 2) + 0.5,
0.02
);
}
for (let i = 0; i < count; i++) {
const next = (i + 1) % count;
indices.push(
i, count + i, next,
next, count + i, count + next
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createScoreRibbon(path) {
const curve = new THREE.CatmullRomCurve3(
path.map(([x, z]) => new THREE.Vector3(x * LOAF_RX, 0, z * LOAF_RZ))
);
const points = curve.getPoints(90);
const positions = [];
const uvs = [];
const indices = [];
const halfWidth = 0.027;
for (let i = 0; i < points.length; i++) {
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 1)];
const tangent = next.clone().sub(previous).normalize();
const perpendicular = new THREE.Vector3(-tangent.z, 0, tangent.x);
for (const side of [-1, 1]) {
const x = points[i].x + perpendicular.x * halfWidth * side;
const z = points[i].z + perpendicular.z * halfWidth * side;
positions.push(x, loafHeight(x, z) + 0.005, z);
uvs.push(side < 0 ? 0 : 1, i / (points.length - 1));
}
if (i < points.length - 1) {
const base = i * 2;
indices.push(
base, base + 1, base + 2,
base + 1, base + 3, base + 2
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const loaf = new THREE.Group();
loaf.position.set(-1.35, 0.025, 0.28);
scene.add(loaf);
const loafTopData = createLoafTop();
const loafTop = new THREE.Mesh(loafTopData.geometry, crustMaterial);
loafTop.castShadow = true;
loafTop.receiveShadow = true;
loaf.add(loafTop);
const loafSide = new THREE.Mesh(
createLoafSide(loafTopData.outerRing),
sideCrustMaterial
);
loafSide.castShadow = true;
loafSide.receiveShadow = true;
loaf.add(loafSide);
for (const path of SCORE_PATHS) {
const ribbon = new THREE.Mesh(
createScoreRibbon(path),
scoreMaterial
);
ribbon.castShadow = true;
loaf.add(ribbon);
}
// ---------------------------------------------------------------------
// Open grain measure
// ---------------------------------------------------------------------
const measure = new THREE.Group();
measure.position.set(1.32, 0.015, 0.46);
scene.add(measure);
const measureProfile = [
new THREE.Vector2(0.00, 0.025),
new THREE.Vector2(0.20, 0.025),
new THREE.Vector2(0.24, 0.055),
new THREE.Vector2(0.28, 0.105),
new THREE.Vector2(0.40, 0.19),
new THREE.Vector2(0.53, 0.36),
new THREE.Vector2(0.60, 0.51),
new THREE.Vector2(0.615, 0.56),
new THREE.Vector2(0.595, 0.585),
new THREE.Vector2(0.565, 0.56),
new THREE.Vector2(0.53, 0.43),
new THREE.Vector2(0.44, 0.28),
new THREE.Vector2(0.31, 0.17),
new THREE.Vector2(0.19, 0.14),
new THREE.Vector2(0.00, 0.14)
];
const measureBody = new THREE.Mesh(
new THREE.LatheGeometry(measureProfile, 96),
bowlMaterial
);
measureBody.castShadow = true;
measureBody.receiveShadow = true;
measure.add(measureBody);
const measureFoot = new THREE.Mesh(
new THREE.TorusGeometry(0.225, 0.025, 12, 80),
bowlMaterial
);
measureFoot.rotation.x = Math.PI / 2;
measureFoot.position.y = 0.055;
measureFoot.castShadow = true;
measure.add(measureFoot);
const measureRim = new THREE.Mesh(
new THREE.TorusGeometry(0.584, 0.023, 12, 96),
bowlMaterial
);
measureRim.rotation.x = Math.PI / 2;
measureRim.position.y = 0.565;
measureRim.castShadow = true;
measure.add(measureRim);
const measureGrainSurface = new THREE.Mesh(
new THREE.CircleGeometry(0.535, 96),
new THREE.MeshStandardMaterial({
color: 0xb98535,
roughness: 0.95
})
);
measureGrainSurface.rotation.x = -Math.PI / 2;
measureGrainSurface.position.y = 0.495;
measure.add(measureGrainSurface);
// ---------------------------------------------------------------------
// Small open storage vessel
// ---------------------------------------------------------------------
const storageJar = new THREE.Group();
storageJar.position.set(0.18, 0.015, -1.05);
storageJar.scale.setScalar(0.91);
scene.add(storageJar);
const jarProfile = [
new THREE.Vector2(0.00, 0.02),
new THREE.Vector2(0.27, 0.02),
new THREE.Vector2(0.35, 0.07),
new THREE.Vector2(0.43, 0.18),
new THREE.Vector2(0.465, 0.34),
new THREE.Vector2(0.44, 0.50),
new THREE.Vector2(0.35, 0.64),
new THREE.Vector2(0.285, 0.76),
new THREE.Vector2(0.265, 0.88),
new THREE.Vector2(0.29, 0.975),
new THREE.Vector2(0.315, 1.025),
new THREE.Vector2(0.285, 1.045),
new THREE.Vector2(0.25, 1.015),
new THREE.Vector2(0.235, 0.91),
new THREE.Vector2(0.25, 0.78),
new THREE.Vector2(0.315, 0.65),
new THREE.Vector2(0.385, 0.49),
new THREE.Vector2(0.40, 0.32),
new THREE.Vector2(0.34, 0.15),
new THREE.Vector2(0.24, 0.10),
new THREE.Vector2(0.00, 0.10)
];
const jarBody = new THREE.Mesh(
new THREE.LatheGeometry(jarProfile, 96),
jarMaterial
);
jarBody.castShadow = true;
jarBody.receiveShadow = true;
storageJar.add(jarBody);
const jarRim = new THREE.Mesh(
new THREE.TorusGeometry(0.283, 0.027, 12, 96),
jarMaterial
);
jarRim.rotation.x = Math.PI / 2;
jarRim.position.y = 1.027;
jarRim.castShadow = true;
storageJar.add(jarRim);
for (const [radius, height] of [
[0.421, 0.34],
[0.397, 0.49],
[0.286, 0.83]
]) {
const band = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.009, 8, 80),
darkClayMaterial
);
band.rotation.x = Math.PI / 2;
band.position.y = height;
band.castShadow = true;
storageJar.add(band);
}
const jarGrainSurface = new THREE.Mesh(
new THREE.CircleGeometry(0.243, 64),
new THREE.MeshStandardMaterial({
color: 0xa9792d,
roughness: 1
})
);
jarGrainSurface.rotation.x = -Math.PI / 2;
jarGrainSurface.position.y = 0.895;
storageJar.add(jarGrainSurface);
// ---------------------------------------------------------------------
// Kernel geometry and instanced grain
// ---------------------------------------------------------------------
function createKernelGeometry() {
const geometry = new THREE.SphereGeometry(1, 12, 8);
const position = geometry.attributes.position;
for (let i = 0; i < position.count; i++) {
let x = position.getX(i);
let y = position.getY(i);
let z = position.getZ(i);
x *= 0.041;
y *= 0.026;
z *= 0.066;
// A small longitudinal depression suggests the kernel's crease.
if (Math.abs(x) < 0.014) {
y -= 0.006 * (1 - Math.abs(x) / 0.014);
}
position.setXYZ(i, x, y, z);
}
geometry.computeVertexNormals();
return geometry;
}
const kernelGeometry = createKernelGeometry();
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
metalness: 0
});
const dummy = new THREE.Object3D();
const grainColor = new THREE.Color();
function addGrains(parent, options) {
const {
count,
radius,
height,
minScale = 0.82,
maxScale = 1.18,
mound = 0.025
} = options;
const mesh = new THREE.InstancedMesh(
kernelGeometry,
grainMaterial,
count
);
for (let i = 0; i < count; i++) {
const angle = random() * Math.PI * 2;
const radial = Math.sqrt(random()) * radius;
const x = Math.cos(angle) * radial;
const z = Math.sin(angle) * radial;
const y = height + (1 - radial / radius) * mound + random() * 0.008;
const scale = minScale + random() * (maxScale - minScale);
dummy.position.set(x, y, z);
dummy.rotation.set(
(random() - 0.5) * 0.36,
random() * Math.PI * 2,
(random() - 0.5) * 0.30
);
dummy.scale.setScalar(scale);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
const hue = 0.105 + random() * 0.025;
const saturation = 0.55 + random() * 0.16;
const lightness = 0.40 + random() * 0.13;
grainColor.setHSL(hue, saturation, lightness);
mesh.setColorAt(i, grainColor);
}
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
addGrains(measure, {
count: 285,
radius: 0.505,
height: 0.511,
mound: 0.026
});
addGrains(storageJar, {
count: 76,
radius: 0.225,
height: 0.91,
mound: 0.014,
minScale: 0.78,
maxScale: 1.05
});
// Loose kernels form a visual trail between the raw and finished foods.
const looseGrains = new THREE.Group();
scene.add(looseGrains);
const looseCount = 24;
const looseMesh = new THREE.InstancedMesh(
kernelGeometry,
grainMaterial,
looseCount
);
for (let i = 0; i < looseCount; i++) {
const t = i / (looseCount - 1);
const x = -0.27 + t * 1.35 + (random() - 0.5) * 0.28;
const z = 0.84 + Math.sin(t * Math.PI * 1.3) * 0.18 +
(random() - 0.5) * 0.26;
const scale = 0.82 + random() * 0.34;
dummy.position.set(x, 0.034, z);
dummy.rotation.set(
(random() - 0.5) * 0.28,
random() * Math.PI * 2,
(random() - 0.5) * 0.26
);
dummy.scale.setScalar(scale);
dummy.updateMatrix();
looseMesh.setMatrixAt(i, dummy.matrix);
grainColor.setHSL(
0.108 + random() * 0.018,
0.55 + random() * 0.17,
0.40 + random() * 0.13
);
looseMesh.setColorAt(i, grainColor);
}
looseMesh.instanceMatrix.needsUpdate = true;
if (looseMesh.instanceColor) looseMesh.instanceColor.needsUpdate = true;
looseMesh.castShadow = true;
looseMesh.receiveShadow = true;
looseGrains.add(looseMesh);
// ---------------------------------------------------------------------
// Small cut sample with geometric crumb pores
// ---------------------------------------------------------------------
function createCrumbSlice() {
const group = new THREE.Group();
const front = [
new THREE.Vector3(-0.42, -0.22, 0),
new THREE.Vector3(0.48, -0.18, 0),
new THREE.Vector3(0.05, 0.40, 0)
];
const back = front.map(point =>
new THREE.Vector3(point.x, point.y, point.z - 0.25)
);
const positions = [];
const uvs = [];
const indices = [];
const groups = [];
function addTriangle(a, b, c, groupIndex) {
const start = indices.length;
indices.push(a, b, c);
groups.push({ start, count: 3, materialIndex: groupIndex });
}
for (const point of front) {
positions.push(point.x, point.y, point.z);
uvs.push(point.x + 0.5, point.y + 0.5);
}
for (const point of back) {
positions.push(point.x, point.y, point.z);
uvs.push(point.x + 0.5, point.y + 0.5);
}
addTriangle(0, 1, 2, 0);
addTriangle(5, 4, 3, 0);
addTriangle(0, 3, 4, 1);
addTriangle(0, 4, 1, 1);
addTriangle(1, 4, 5, 1);
addTriangle(1, 5, 2, 1);
addTriangle(2, 5, 3, 1);
addTriangle(2, 3, 0, 1);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
for (const group of groups) {
geometry.addGroup(group.start, group.count, group.materialIndex);
}
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, [
crumbMaterial,
sliceCrustMaterial
]);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
// Pores are actual shallow geometry rather than a flat texture.
const poreGeometry = new THREE.SphereGeometry(1, 10, 7);
const poreMaterial = new THREE.MeshStandardMaterial({
color: 0x9c703a,
roughness: 1
});
const porePositions = [
[-0.24, -0.05, 0.026],
[-0.10, 0.08, 0.018],
[0.07, -0.03, 0.035],
[0.25, 0.02, 0.021],
[-0.19, 0.17, 0.019],
[0.02, 0.23, 0.014],
[0.21, 0.18, 0.027],
[-0.02, -0.13, 0.022],
[0.36, -0.08, 0.014],
[-0.30, -0.13, 0.013],
[0.11, 0.10, 0.012],
[-0.10, -0.02, 0.014]
];
for (const [x, y, radius] of porePositions) {
const pore = new THREE.Mesh(poreGeometry, poreMaterial);
pore.position.set(x, y, 0.007);
pore.scale.set(radius, radius * 0.68, 0.009);
pore.castShadow = false;
group.add(pore);
}
group.position.set(-0.57, 0.285, 0.96);
group.rotation.set(-0.10, -0.16, -0.035);
group.scale.setScalar(0.86);
return group;
}
const crumbSlice = createCrumbSlice();
scene.add(crumbSlice);
// ---------------------------------------------------------------------
// Direct camera controls: pointer, wheel, touch, keyboard, and double click
// ---------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
let pointerId = null;
function resetView() {
controls.desiredYaw = 0.24;
controls.desiredPitch = 0.47;
controls.desiredDistance = 6.65;
}
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", event => {
if (!dragging || event.pointerId !== pointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
controls.desiredYaw -= deltaX * 0.006;
controls.desiredPitch = THREE.MathUtils.clamp(
controls.desiredPitch + deltaY * 0.0045,
0.22,
1.20
);
});
function endDrag(event) {
if (event.pointerId !== pointerId) return;
dragging = false;
pointerId = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
controls.desiredDistance = THREE.MathUtils.clamp(
controls.desiredDistance + event.deltaY * 0.0022,
4.4,
9.2
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (key === "arrowleft") {
controls.desiredYaw -= 0.12;
event.preventDefault();
} else if (key === "arrowright") {
controls.desiredYaw += 0.12;
event.preventDefault();
} else if (key === "arrowup") {
controls.desiredPitch = THREE.MathUtils.clamp(
controls.desiredPitch + 0.08,
0.22,
1.20
);
event.preventDefault();
} else if (key === "arrowdown") {
controls.desiredPitch = THREE.MathUtils.clamp(
controls.desiredPitch - 0.08,
0.22,
1.20
);
event.preventDefault();
} else if (key === "+" || key === "=") {
controls.desiredDistance = Math.max(
4.4,
controls.desiredDistance - 0.45
);
event.preventDefault();
} else if (key === "-") {
controls.desiredDistance = Math.min(
9.2,
controls.desiredDistance + 0.45
);
event.preventDefault();
} else if (key === "r") {
resetView();
}
});
document.querySelector("#reset-view").addEventListener("click", resetView);
const labelsLayer = document.querySelector("#labels");
const labelsButton = document.querySelector("#toggle-labels");
labelsButton.addEventListener("click", () => {
const isHidden = labelsLayer.classList.toggle("hidden");
labelsButton.setAttribute("aria-pressed", String(!isHidden));
});
// ---------------------------------------------------------------------
// Projected object labels
// ---------------------------------------------------------------------
const labelAnchors = [
{
element: document.querySelector("#label-loaf"),
position: new THREE.Vector3(-1.35, 0.62, 0.28)
},
{
element: document.querySelector("#label-measure"),
position: new THREE.Vector3(1.32, 0.63, 0.46)
},
{
element: document.querySelector("#label-jar"),
position: new THREE.Vector3(0.18, 1.02, -1.05)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const anchor of labelAnchors) {
projected.copy(anchor.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
anchor.element.style.visibility = visible ? "visible" : "hidden";
anchor.element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
anchor.element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
renderer.setAnimationLoop(() => {
const damping = reducedMotion ? 1 : 0.085;
controls.yaw = THREE.MathUtils.lerp(
controls.yaw,
controls.desiredYaw,
damping
);
controls.pitch = THREE.MathUtils.lerp(
controls.pitch,
controls.desiredPitch,
damping
);
controls.distance = THREE.MathUtils.lerp(
controls.distance,
controls.desiredDistance,
damping
);
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("done");
});
</script>
</body>
</html>
```