Prompt specification
Build or customize the Three.js 3D scene "Compost bin material layers" 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: Compost bin material layers
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compost-bin-material-layers-x1045
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Show an open-sided compost bin with coarse twigs, leaves, and food scraps as distinct layers; expose air spaces and label materials without prescribing a practical recipe.
Working Three.js Starter (compost-bin-material-layers-x1045):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compost Bin — Open-Sided Material Study</title>
<style>
:root {
--ink: #23342c;
--muted: #65756c;
--paper: #f7f5eb;
--glass: rgba(250, 249, 241, 0.9);
--line: rgba(35, 52, 44, 0.18);
--teal: #277f82;
--twig: #9a5f35;
--leaf: #778b42;
--food: #db6b3e;
--air: #328d94;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8efe8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#stage canvas:active {
cursor: grabbing;
}
.masthead {
position: fixed;
z-index: 4;
top: 26px;
left: 30px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 9px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 3.2vw, 52px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.subtitle strong {
color: var(--ink);
font-weight: 650;
}
.info-panel {
position: fixed;
z-index: 5;
top: 26px;
right: 28px;
width: 292px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: var(--glass);
box-shadow:
0 18px 50px rgba(43, 64, 52, 0.12),
inset 0 0 0 1px rgba(35, 52, 44, 0.04);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-bottom: 12px;
border-bottom: 1px solid var(--line);
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-badge {
flex: 0 0 auto;
padding: 5px 8px;
border-radius: 999px;
background: #e4eee7;
color: #47705d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.legend {
display: grid;
gap: 11px;
margin: 15px 0;
}
.legend-item {
display: grid;
grid-template-columns: 12px 1fr;
align-items: start;
gap: 10px;
}
.swatch {
width: 10px;
height: 10px;
margin-top: 4px;
border-radius: 50%;
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.7);
}
.legend-item strong {
display: block;
margin-bottom: 1px;
font-size: 12px;
}
.legend-item small {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.panel-note {
margin: 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
#leaders {
position: fixed;
z-index: 2;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
.leader-path {
fill: none;
stroke-width: 1.4;
stroke-linecap: round;
stroke-dasharray: 3 5;
opacity: 0.72;
}
.leader-dot {
stroke-width: 2;
fill: #f9f7ee;
}
.callout {
position: fixed;
z-index: 3;
display: flex;
align-items: center;
gap: 10px;
min-width: 168px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.85);
border-radius: 12px;
background: rgba(250, 249, 242, 0.88);
box-shadow: 0 9px 25px rgba(39, 59, 47, 0.1);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
.callout::before {
content: "";
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 17%, transparent);
}
.callout strong {
display: block;
color: var(--accent);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.11em;
line-height: 1.2;
text-transform: uppercase;
}
.callout small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.25;
}
#label-scraps {
--accent: var(--food);
top: 35%;
right: 28px;
}
#label-leaves {
--accent: var(--leaf);
top: 49%;
right: 28px;
}
#label-twigs {
--accent: var(--twig);
top: 63%;
right: 28px;
}
#label-air {
--accent: var(--air);
top: 55%;
left: 30px;
}
.labels-off .callout {
opacity: 0 !important;
transform: translateY(5px);
}
.labels-off #leaders {
opacity: 0;
}
.footer-note {
position: fixed;
z-index: 4;
bottom: 27px;
left: 30px;
max-width: 430px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
pointer-events: none;
}
.footer-note strong {
color: var(--ink);
}
.controls {
position: fixed;
z-index: 6;
right: 28px;
bottom: 24px;
display: flex;
gap: 8px;
padding: 7px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 14px;
background: rgba(250, 249, 242, 0.9);
box-shadow: 0 12px 35px rgba(39, 59, 47, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.controls button {
min-height: 34px;
padding: 0 12px;
border: 0;
border-radius: 9px;
background: transparent;
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: background 150ms ease, color 150ms ease;
}
.controls button:hover {
background: #e6eee7;
}
.controls button[aria-pressed="true"] {
background: var(--ink);
color: #f8f7ef;
}
.controls button:focus-visible {
outline: 2px solid var(--teal);
outline-offset: 2px;
}
.interaction-hint {
position: fixed;
z-index: 3;
bottom: 73px;
left: 50%;
color: rgba(35, 52, 44, 0.62);
font-size: 10px;
letter-spacing: 0.05em;
pointer-events: none;
transform: translateX(-50%);
white-space: nowrap;
}
@media (max-width: 1080px) {
.info-panel {
width: 250px;
}
.callout {
min-width: 145px;
}
#label-scraps,
#label-leaves,
#label-twigs {
right: 18px;
}
}
@media (max-width: 760px) {
.masthead {
top: 18px;
left: 18px;
}
h1 {
font-size: 31px;
}
.subtitle {
max-width: 250px;
font-size: 12px;
}
.info-panel {
display: none;
}
.callout {
min-width: 0;
max-width: 145px;
padding: 8px 10px;
}
.callout small {
display: none;
}
.callout strong {
font-size: 9px;
}
#label-scraps,
#label-leaves,
#label-twigs {
right: 12px;
}
#label-air {
left: 12px;
}
.footer-note {
right: 18px;
bottom: 76px;
left: 18px;
max-width: 250px;
font-size: 9px;
}
.controls {
right: 12px;
bottom: 14px;
left: 12px;
justify-content: center;
}
.interaction-hint {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.callout {
transition: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive open-sided compost bin scene"></main>
<header class="masthead">
<p class="eyebrow">Open-sided material study</p>
<h1>Compost,<br>opened up.</h1>
<p class="subtitle">
A stylized cutaway showing <strong>twigs, leaves, and food scraps</strong>
as distinct visible materials.
</p>
</header>
<aside class="info-panel" aria-label="Material index">
<div class="panel-heading">
<strong>Material index</strong>
<span class="panel-badge">Cutaway</span>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch" style="background:#9a5f35"></span>
<div>
<strong>Coarse twigs</strong>
<small>Thick woody branches and smaller offshoots.</small>
</div>
</div>
<div class="legend-item">
<span class="swatch" style="background:#778b42"></span>
<div>
<strong>Leaf litter</strong>
<small>Broad leaves in olive, ochre, and rust tones.</small>
</div>
</div>
<div class="legend-item">
<span class="swatch" style="background:#db6b3e"></span>
<div>
<strong>Food scraps</strong>
<small>Colorful peels, slices, and irregular pieces.</small>
</div>
</div>
<div class="legend-item">
<span class="swatch" style="background:#328d94"></span>
<div>
<strong>Air space</strong>
<small>Open gaps remain visible between the pieces.</small>
</div>
</div>
</div>
<p class="panel-note">
Appearance study only. Labels describe what is visible; no proportions,
preparation method, or practical recipe is implied.
</p>
</aside>
<svg id="leaders" aria-hidden="true"></svg>
<div class="callout" id="label-scraps">
<div>
<strong>Food scraps</strong>
<small>Colorful, irregular pieces</small>
</div>
</div>
<div class="callout" id="label-leaves">
<div>
<strong>Leaf litter</strong>
<small>Broad, layered leaves</small>
</div>
</div>
<div class="callout" id="label-twigs">
<div>
<strong>Coarse twigs</strong>
<small>Woody branches and offshoots</small>
</div>
</div>
<div class="callout" id="label-air">
<div>
<strong>Air space</strong>
<small>Open gap between pieces</small>
</div>
</div>
<p class="footer-note">
<strong>Visual cutaway, not a build guide.</strong>
The separate forms and visible gaps are illustrative rather than instructional.
</p>
<div class="interaction-hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset</div>
<nav class="controls" aria-label="Scene controls">
<button id="reset-view" type="button">Reset view</button>
<button id="top-view" type="button">Top view</button>
<button id="toggle-labels" type="button" aria-pressed="true">Labels on</button>
</nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const leaders = document.querySelector("#leaders");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8efe8);
scene.fog = new THREE.Fog(0xe8efe8, 15, 28);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional open-sided compost bin. Drag to orbit and scroll to zoom."
);
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const narrowView = window.innerWidth < 760;
const orbit = {
azimuth: 0.64,
polar: 1.17,
radius: narrowView ? 16 : 11.8,
target: new THREE.Vector3(0, 1.55, 0)
};
const desired = {
azimuth: orbit.azimuth,
polar: orbit.polar,
radius: orbit.radius
};
function updateCamera() {
const sinPolar = Math.sin(orbit.polar);
camera.position.set(
orbit.target.x + orbit.radius * sinPolar * Math.sin(orbit.azimuth),
orbit.target.y + orbit.radius * Math.cos(orbit.polar),
orbit.target.z + orbit.radius * sinPolar * Math.cos(orbit.azimuth)
);
camera.lookAt(orbit.target);
}
updateCamera();
// Soft, illustrative lighting.
scene.add(new THREE.HemisphereLight(0xf8fff5, 0x61705f, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.1);
keyLight.position.set(-4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9dbe2, 1.25);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
// Deterministic randomness keeps the composition stable.
let seed = 918273;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
function choose(items) {
return items[Math.floor(random() * items.length)];
}
// Procedural wood grain.
function createWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = "#aa7048";
context.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < 90; i += 1) {
const y = range(0, canvas.height);
const lightness = Math.floor(range(75, 145));
context.strokeStyle = `rgba(${lightness + 65}, ${lightness + 25}, ${
lightness - 20
}, ${range(0.08, 0.23)})`;
context.lineWidth = range(0.5, 2.2);
context.beginPath();
context.moveTo(-20, y);
for (let x = 0; x <= canvas.width + 20; x += 24) {
context.lineTo(x, y + Math.sin(x * 0.035 + i) * range(0.3, 2.2));
}
context.stroke();
}
for (let i = 0; i < 12; i += 1) {
const x = range(20, canvas.width - 20);
const y = range(20, canvas.height - 20);
context.strokeStyle = "rgba(91, 48, 28, 0.20)";
context.lineWidth = 2;
context.beginPath();
context.ellipse(x, y, range(8, 25), range(2, 5), 0, 0, Math.PI * 2);
context.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1.6, 1);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = createWoodTexture();
const materials = {
wood: new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xf1d2b7,
roughness: 0.86
}),
post: new THREE.MeshStandardMaterial({
color: 0x805035,
roughness: 0.9
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x70452e,
roughness: 0.92
}),
metal: new THREE.MeshStandardMaterial({
color: 0x48524e,
metalness: 0.55,
roughness: 0.55
}),
ground: new THREE.MeshStandardMaterial({
color: 0xdde7dc,
roughness: 1
}),
platform: new THREE.MeshStandardMaterial({
color: 0xc9d8ca,
roughness: 0.96
})
};
// Ground and a low circular plinth.
const ground = new THREE.Mesh(new THREE.PlaneGeometry(40, 40), materials.ground);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.2;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(5.15, 5.3, 0.18, 64),
materials.platform
);
platform.position.y = -0.1;
platform.receiveShadow = true;
scene.add(platform);
const platformRing = new THREE.Mesh(
new THREE.TorusGeometry(5.02, 0.025, 6, 96),
new THREE.MeshBasicMaterial({
color: 0xa8bda9,
transparent: true,
opacity: 0.65
})
);
platformRing.rotation.x = Math.PI / 2;
platformRing.position.y = 0.002;
scene.add(platformRing);
// Open-sided slatted bin: back and sides are built; the front stays open.
const bin = new THREE.Group();
scene.add(bin);
function addBox(width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
bin.add(mesh);
return mesh;
}
const slatLevels = [0.46, 1.04, 1.62, 2.2, 2.78, 3.36];
for (const y of slatLevels) {
addBox(6.12, 0.39, 0.18, 0, y, -2.12, materials.wood);
addBox(0.18, 0.39, 4.18, -3.0, y, 0, materials.wood);
addBox(0.18, 0.39, 4.18, 3.0, y, 0, materials.wood);
}
for (const x of [-3.0, 3.0]) {
for (const z of [-2.12, 2.12]) {
addBox(0.31, 3.72, 0.31, x, 1.84, z, materials.post);
}
}
// Top rails stop before the open front.
addBox(6.28, 0.26, 0.28, 0, 3.72, -2.12, materials.post);
addBox(0.28, 0.26, 4.35, -3.0, 3.72, 0, materials.post);
addBox(0.28, 0.26, 4.35, 3.0, 3.72, 0, materials.post);
// Small bolt heads emphasize the crafted, stylized construction.
const boltGeometry = new THREE.CylinderGeometry(0.052, 0.052, 0.028, 12);
for (const x of [-3.0, 3.0]) {
for (const y of [0.48, 1.28, 2.08, 2.88, 3.5]) {
const bolt = new THREE.Mesh(boltGeometry, materials.metal);
bolt.rotation.x = Math.PI / 2;
bolt.position.set(x, y, 2.285);
bolt.castShadow = true;
bin.add(bolt);
}
}
// Twig layer.
const twigMaterials = [
new THREE.MeshStandardMaterial({ color: 0x765039, roughness: 0.96 }),
new THREE.MeshStandardMaterial({ color: 0x916444, roughness: 0.94 }),
new THREE.MeshStandardMaterial({ color: 0x624632, roughness: 0.98 })
];
const twigGeometry = new THREE.CylinderGeometry(1, 1, 1, 8, 1);
const upAxis = new THREE.Vector3(0, 1, 0);
function addTwig(a, b, radius, material) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const twig = new THREE.Mesh(twigGeometry, material);
twig.position.copy(a).add(b).multiplyScalar(0.5);
twig.scale.set(radius, length, radius);
twig.quaternion.setFromUnitVectors(upAxis, direction.normalize());
twig.castShadow = true;
twig.receiveShadow = true;
scene.add(twig);
return twig;
}
for (let i = 0; i < 42; i += 1) {
const angle = range(0, Math.PI * 2);
const length = range(1.5, 3.8);
const center = new THREE.Vector3(
range(-1.65, 1.65),
range(0.28, 0.9),
range(-1.55, 1.55)
);
const direction = new THREE.Vector3(
Math.cos(angle),
range(-0.11, 0.11),
Math.sin(angle)
).normalize();
const a = center.clone().addScaledVector(direction, -length * 0.5);
const b = center.clone().addScaledVector(direction, length * 0.5);
const radius = range(0.045, 0.105);
const material = choose(twigMaterials);
addTwig(a, b, radius, material);
if (i % 3 === 0) {
const forkStart = b.clone().addScaledVector(direction, -length * 0.18);
const forkAngle = angle + choose([-1, 1]) * range(0.45, 0.95);
const forkDirection = new THREE.Vector3(
Math.cos(forkAngle),
range(-0.08, 0.18),
Math.sin(forkAngle)
).normalize();
const forkEnd = forkStart.clone().addScaledVector(forkDirection, range(0.45, 1.05));
addTwig(forkStart, forkEnd, radius * 0.62, material);
}
}
// Leaf geometry shared by all leaves.
const leafShape = new THREE.Shape();
leafShape.moveTo(0, -0.55);
leafShape.bezierCurveTo(-0.42, -0.35, -0.42, 0.25, 0, 0.55);
leafShape.bezierCurveTo(0.42, 0.25, 0.42, -0.35, 0, -0.55);
const leafGeometry = new THREE.ExtrudeGeometry(leafShape, {
depth: 0.028,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.015,
bevelThickness: 0.012,
curveSegments: 6
});
leafGeometry.translate(0, 0, -0.014);
const veinGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -0.44, 0.035),
new THREE.Vector3(0, 0.45, 0.035)
]);
const veinMaterial = new THREE.LineBasicMaterial({
color: 0x5e5d35,
transparent: true,
opacity: 0.65
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x7b8743,
roughness: 0.92,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb28a39,
roughness: 0.9,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xa95735,
roughness: 0.92,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x5f793b,
roughness: 0.9,
side: THREE.DoubleSide,
flatShading: true
})
];
const reservedGap = new THREE.Vector2(0.18, 1.55);
function keepClearOfGap(x, z, clearance = 0.58) {
if (Math.hypot(x - reservedGap.x, z - reservedGap.y) > clearance) {
return { x, z };
}
const direction = x < reservedGap.x ? -1 : 1;
return { x: reservedGap.x + direction * clearance, z };
}
for (let i = 0; i < 47; i += 1) {
const group = new THREE.Group();
const leaf = new THREE.Mesh(leafGeometry, choose(leafMaterials));
leaf.castShadow = true;
leaf.receiveShadow = true;
group.add(leaf);
const vein = new THREE.Line(veinGeometry, veinMaterial);
group.add(vein);
let x = range(-2.35, 2.35);
let z = range(-1.65, 1.7);
const clear = keepClearOfGap(x, z);
x = clear.x;
z = clear.z;
group.position.set(x, range(1.0, 1.62), z);
group.rotation.set(
-Math.PI / 2 + range(-0.42, 0.42),
range(-0.18, 0.18),
range(0, Math.PI * 2)
);
group.scale.set(range(0.68, 1.05), range(0.72, 1.18), 1);
scene.add(group);
}
// Food-scrap materials.
const foodMaterials = {
orange: new THREE.MeshStandardMaterial({
color: 0xf28a26,
roughness: 0.72
}),
orangeLight: new THREE.MeshStandardMaterial({
color: 0xf5b64d,
roughness: 0.76
}),
red: new THREE.MeshStandardMaterial({
color: 0xd94f3d,
roughness: 0.76
}),
pale: new THREE.MeshStandardMaterial({
color: 0xf0d8a7,
roughness: 0.82
}),
yellow: new THREE.MeshStandardMaterial({
color: 0xe8c54d,
roughness: 0.75
}),
green: new THREE.MeshStandardMaterial({
color: 0x729448,
roughness: 0.85
}),
darkGreen: new THREE.MeshStandardMaterial({
color: 0x3f6e42,
roughness: 0.88
})
};
function makePeel(material, radius = 0.28) {
const group = new THREE.Group();
const peel = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.065, 6, 20, Math.PI * 1.45),
material
);
peel.rotation.x = Math.PI / 2;
peel.rotation.z = range(0, Math.PI * 2);
peel.castShadow = true;
group.add(peel);
const inner = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.7, 0.022, 5, 18, Math.PI * 1.2),
foodMaterials.pale
);
inner.rotation.x = Math.PI / 2;
inner.rotation.z = range(0, Math.PI * 2);
inner.position.y = 0.025;
group.add(inner);
return group;
}
function makeBananaPeel() {
const group = new THREE.Group();
for (let i = -1; i <= 1; i += 1) {
const offset = i * 0.13;
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(offset, 0, -0.42),
new THREE.Vector3(offset * 1.4, 0.035, -0.16),
new THREE.Vector3(-offset * 0.45, 0.075, 0.13),
new THREE.Vector3(offset * 0.65, 0.018, 0.44)
]);
const strip = new THREE.Mesh(
new THREE.TubeGeometry(curve, 14, 0.052, 6, false),
i === 0 ? foodMaterials.yellow : foodMaterials.orangeLight
);
strip.castShadow = true;
group.add(strip);
}
return group;
}
function makeTomatoSlice() {
const group = new THREE.Group();
const slice = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.22, 0.08, 16),
foodMaterials.red
);
slice.castShadow = true;
group.add(slice);
const center = new THREE.Mesh(
new THREE.CircleGeometry(0.105, 16),
foodMaterials.orangeLight
);
center.rotation.x = -Math.PI / 2;
center.position.y = 0.041;
group.add(center);
for (let i = 0; i < 5; i += 1) {
const angle = (i / 5) * Math.PI * 2;
const seed = new THREE.Mesh(
new THREE.SphereGeometry(0.022, 6, 4),
foodMaterials.pale
);
seed.scale.set(0.65, 0.25, 1.5);
seed.position.set(
Math.cos(angle) * 0.07,
0.048,
Math.sin(angle) * 0.07
);
seed.rotation.y = -angle;
group.add(seed);
}
return group;
}
function makeAvocadoPiece() {
const group = new THREE.Group();
const peel = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 12, 8),
foodMaterials.darkGreen
);
peel.scale.set(1, 0.56, 0.72);
peel.castShadow = true;
group.add(peel);
const flesh = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 12, 8),
foodMaterials.green
);
flesh.scale.set(1, 0.52, 0.72);
flesh.position.y = 0.07;
group.add(flesh);
return group;
}
function makeFoodChunk(material) {
const group = new THREE.Group();
const chunk = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.22, 0),
material
);
chunk.scale.set(range(0.8, 1.25), range(0.65, 1.05), range(0.8, 1.2));
chunk.rotation.set(range(0, 2), range(0, 2), range(0, 2));
chunk.castShadow = true;
group.add(chunk);
return group;
}
for (let i = 0; i < 28; i += 1) {
let scrap;
switch (i % 7) {
case 0:
scrap = makePeel(foodMaterials.orange);
break;
case 1:
scrap = makeBananaPeel();
break;
case 2:
scrap = makeTomatoSlice();
break;
case 3:
scrap = makeFoodChunk(foodMaterials.green);
break;
case 4:
scrap = makePeel(foodMaterials.red, 0.24);
break;
case 5:
scrap = makeAvocadoPiece();
break;
default:
scrap = makeFoodChunk(
choose([
foodMaterials.orangeLight,
foodMaterials.red,
foodMaterials.green,
foodMaterials.yellow
])
);
}
let x = range(-2.25, 2.25);
let z = range(-1.55, 1.65);
const clear = keepClearOfGap(x, z, 0.65);
x = clear.x;
z = clear.z;
scrap.position.set(x, range(1.76, 2.38), z);
scrap.rotation.y = range(0, Math.PI * 2);
scrap.scale.setScalar(range(0.78, 1.18));
scene.add(scrap);
}
// A subtle ring identifies one intentionally visible air gap.
const airRingMaterial = new THREE.MeshBasicMaterial({
color: 0x328d94,
transparent: true,
opacity: 0.72,
depthTest: false
});
const airRing = new THREE.Mesh(
new THREE.TorusGeometry(0.235, 0.013, 6, 48),
airRingMaterial
);
airRing.position.set(0.18, 1.52, 1.55);
airRing.rotation.y = -0.08;
airRing.renderOrder = 8;
scene.add(airRing);
// HTML labels with projected SVG leader lines.
const labelData = [
{
element: document.querySelector("#label-scraps"),
anchor: new THREE.Vector3(1.75, 2.25, 1.2),
color: "#db6b3e",
side: "right"
},
{
element: document.querySelector("#label-leaves"),
anchor: new THREE.Vector3(2.05, 1.42, 1.35),
color: "#778b42",
side: "right"
},
{
element: document.querySelector("#label-twigs"),
anchor: new THREE.Vector3(1.9, 0.6, 1.5),
color: "#9a5f35",
side: "right"
},
{
element: document.querySelector("#label-air"),
anchor: new THREE.Vector3(0.18, 1.52, 1.55),
color: "#328d94",
side: "left"
}
];
const svgNamespace = "http://www.w3.org/2000/svg";
for (const item of labelData) {
item.path = document.createElementNS(svgNamespace, "path");
item.path.setAttribute("class", "leader-path");
item.path.setAttribute("stroke", item.color);
item.dot = document.createElementNS(svgNamespace, "circle");
item.dot.setAttribute("class", "leader-dot");
item.dot.setAttribute("r", "4");
item.dot.setAttribute("stroke", item.color);
leaders.append(item.path, item.dot);
}
const projected = new THREE.Vector3();
function updateLeaders() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of labelData) {
projected.copy(item.anchor).project(camera);
const screenX = (projected.x * 0.5 + 0.5) * width;
const screenY = (-projected.y * 0.5 + 0.5) * height;
const visible =
projected.z > -1 &&
projected.z < 1 &&
screenX > -80 &&
screenX < width + 80 &&
screenY > -80 &&
screenY < height + 80;
const rect = item.element.getBoundingClientRect();
const endX = item.side === "right" ? rect.left - 8 : rect.right + 8;
const endY = rect.top + rect.height * 0.5;
const dx = endX - screenX;
const bend = Math.max(36, Math.min(100, Math.abs(dx) * 0.45));
const path = visible
? `M ${screenX.toFixed(1)} ${screenY.toFixed(1)}
C ${(screenX + dx * 0.38).toFixed(1)} ${screenY.toFixed(1)},
${(endX - dx * 0.32).toFixed(1)} ${endY.toFixed(1)},
${endX.toFixed(1)} ${endY.toFixed(1)}`
: "";
item.path.setAttribute("d", path);
item.path.style.opacity = visible ? "0.72" : "0";
item.dot.setAttribute("cx", screenX.toFixed(1));
item.dot.setAttribute("cy", screenY.toFixed(1));
item.dot.style.opacity = visible ? "1" : "0";
}
}
// Custom camera controls implemented without external examples or addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desired.azimuth -= deltaX * 0.006;
desired.polar = THREE.MathUtils.clamp(
desired.polar - deltaY * 0.005,
0.56,
1.43
);
});
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();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
8.2,
21
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("keydown", (event) => {
const keyStep = 0.09;
if (event.key === "ArrowLeft") desired.azimuth += keyStep;
else if (event.key === "ArrowRight") desired.azimuth -= keyStep;
else if (event.key === "ArrowUp") desired.polar -= keyStep;
else if (event.key === "ArrowDown") desired.polar += keyStep;
else if (event.key === "+" || event.key === "=") desired.radius -= 0.5;
else if (event.key === "-" || event.key === "_") desired.radius += 0.5;
else return;
desired.polar = THREE.MathUtils.clamp(desired.polar, 0.56, 1.43);
desired.radius = THREE.MathUtils.clamp(desired.radius, 8.2, 21);
event.preventDefault();
});
function resetView() {
desired.azimuth = 0.64;
desired.polar = 1.17;
desired.radius = window.innerWidth < 760 ? 16 : 11.8;
}
document.querySelector("#reset-view").addEventListener("click", resetView);
document.querySelector("#top-view").addEventListener("click", () => {
desired.azimuth = 0.58;
desired.polar = 0.67;
desired.radius = window.innerWidth < 760 ? 15 : 10.7;
});
const labelsButton = document.querySelector("#toggle-labels");
let labelsVisible = true;
labelsButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
document.body.classList.toggle("labels-off", !labelsVisible);
labelsButton.textContent = labelsVisible ? "Labels on" : "Labels off";
labelsButton.setAttribute("aria-pressed", String(labelsVisible));
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = width / height < 0.8 ? 48 : 38;
camera.updateProjectionMatrix();
leaders.setAttribute("viewBox", `0 0 ${width} ${height}`);
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const easing = reducedMotion ? 1 : 0.09;
orbit.azimuth += (desired.azimuth - orbit.azimuth) * easing;
orbit.polar += (desired.polar - orbit.polar) * easing;
orbit.radius += (desired.radius - orbit.radius) * easing;
updateCamera();
if (!reducedMotion) {
const pulse = 1 + Math.sin(elapsed * 2.1) * 0.045;
airRing.scale.setScalar(pulse);
airRingMaterial.opacity = 0.62 + Math.sin(elapsed * 2.1) * 0.12;
}
updateLeaders();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```