Prompt specification
Build or customize the Three.js 3D scene "Spilled bag object grouping" 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: Spilled bag object grouping
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/spilled-bag-object-grouping-x3909
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: spilled bag object grouping. Display an open grocery bag beside scattered apples, a carton, and an intact loaf; distinguish rolling objects from stationary ones using shape and position. Return one self-contained HTML file.
Working Three.js Starter (spilled-bag-object-grouping-x3909):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#eee4d1">
<title>The Spill — Three.js Still Life</title>
<style>
:root {
--paper: #eee4d1;
--ink: #263b37;
--muted: #66736d;
--rolling: #c9664d;
--rolling-soft: rgba(201, 102, 77, 0.14);
--stationary: #5b9287;
--stationary-soft: rgba(91, 146, 135, 0.16);
--line: rgba(38, 59, 55, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
user-select: none;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
.intro {
position: fixed;
top: 34px;
left: 38px;
z-index: 3;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 12px;
color: #66736d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--rolling);
box-shadow: 0 0 0 4px rgba(201, 102, 77, 0.12);
content: "";
}
h1 {
margin: 0;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(48px, 6.5vw, 88px);
font-weight: 500;
letter-spacing: -0.055em;
line-height: 0.9;
}
h1 span {
color: var(--rolling);
}
.intro p {
max-width: 310px;
margin: 17px 0 0;
color: #68736e;
font-size: 13px;
line-height: 1.55;
}
.legend {
position: fixed;
top: 34px;
right: 36px;
z-index: 4;
width: 225px;
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.52);
border-radius: 16px;
background: rgba(250, 246, 237, 0.62);
box-shadow: 0 15px 45px rgba(62, 57, 45, 0.08);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-title {
margin: 0 0 13px;
color: #7a837e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 31px 1fr;
align-items: center;
gap: 10px;
padding: 10px 0;
}
.legend-row + .legend-row {
border-top: 1px solid var(--line);
}
.swatch {
position: relative;
display: grid;
width: 31px;
height: 31px;
place-items: center;
border-radius: 50%;
}
.swatch.rolling {
background: var(--rolling-soft);
}
.swatch.stationary {
background: var(--stationary-soft);
}
.swatch.rolling::after {
width: 13px;
height: 13px;
border-radius: 50%;
background: var(--rolling);
box-shadow: 3px -3px 0 -2px #6b8c4d;
content: "";
}
.swatch.stationary::after {
width: 14px;
height: 14px;
border: 2px solid var(--stationary);
border-radius: 3px;
content: "";
}
.legend strong {
display: block;
margin-bottom: 3px;
color: var(--ink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
}
.legend small {
display: block;
color: #74807a;
font-size: 10px;
line-height: 1.4;
}
.object-tag {
position: fixed;
top: 0;
left: 0;
z-index: 2;
padding: 6px 9px;
border: 1px solid rgba(38, 59, 55, 0.12);
border-radius: 999px;
background: rgba(249, 244, 233, 0.8);
box-shadow: 0 6px 20px rgba(55, 51, 41, 0.08);
color: var(--ink);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.13em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 150ms ease;
}
.object-tag::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 13px;
background: rgba(38, 59, 55, 0.28);
content: "";
}
.object-tag.rolling {
color: #9f4935;
}
.object-tag.stationary {
color: #3e746b;
}
.hint {
position: fixed;
bottom: 25px;
left: 50%;
z-index: 3;
color: rgba(38, 59, 55, 0.56);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translateX(-50%);
}
.scene-index {
position: fixed;
bottom: 23px;
left: 38px;
z-index: 3;
color: rgba(38, 59, 55, 0.55);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.controls {
position: fixed;
right: 36px;
bottom: 21px;
z-index: 5;
display: flex;
gap: 6px;
}
.controls button {
display: grid;
width: 34px;
height: 34px;
padding: 0;
border: 1px solid rgba(38, 59, 55, 0.14);
border-radius: 50%;
background: rgba(250, 246, 237, 0.76);
color: var(--ink);
cursor: pointer;
font: 600 17px/1 system-ui, sans-serif;
place-items: center;
transition:
background 150ms ease,
transform 150ms ease;
}
.controls button:hover {
background: #fffaf0;
transform: translateY(-2px);
}
.controls button:active {
transform: translateY(0) scale(0.96);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
align-content: center;
justify-items: center;
gap: 14px;
background: var(--paper);
color: #68736e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 550ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
width: 28px;
height: 28px;
border: 2px solid rgba(38, 59, 55, 0.12);
border-top-color: var(--rolling);
border-radius: 50%;
animation: spin 900ms linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 760px) {
.intro {
top: 22px;
left: 22px;
}
.intro p {
display: none;
}
.legend {
top: auto;
right: auto;
bottom: 65px;
left: 16px;
width: 190px;
padding: 10px 13px;
}
.legend-title {
margin-bottom: 5px;
}
.legend-row {
padding: 7px 0;
}
.hint,
.scene-index {
display: none;
}
.controls {
top: 19px;
right: 18px;
bottom: auto;
}
.optional-tag {
display: none;
}
}
@media (max-width: 460px) {
h1 {
font-size: 46px;
}
.eyebrow {
max-width: 150px;
font-size: 8px;
line-height: 1.5;
}
.legend {
width: 175px;
}
.object-tag {
font-size: 7px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive stylized still life of an open grocery bag, scattered apples, a carton, and an intact loaf. Drag to orbit and scroll to zoom."
></canvas>
<header class="intro">
<div class="eyebrow">Still life / group study</div>
<h1>The spill<span>.</span></h1>
<p>
A paper bag, a rolling scatter, and two things that stay put.
Shape and placement reveal the group.
</p>
</header>
<aside class="legend" aria-label="Object grouping legend">
<div class="legend-title">Object behavior</div>
<div class="legend-row">
<span class="swatch rolling" aria-hidden="true"></span>
<div>
<strong>Rolling</strong>
<small>Round forms · foreground scatter</small>
</div>
</div>
<div class="legend-row">
<span class="swatch stationary" aria-hidden="true"></span>
<div>
<strong>Stationary</strong>
<small>Grounded forms · aligned rear group</small>
</div>
</div>
</aside>
<div id="tag-bag" class="object-tag">Open grocery bag</div>
<div id="tag-rolling" class="object-tag rolling">Rolling · apples</div>
<div id="tag-carton" class="object-tag stationary">Stationary · carton</div>
<div id="tag-loaf" class="object-tag stationary optional-tag">Intact loaf</div>
<div class="scene-index">Scene 03 / Paper & produce</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div class="controls" aria-label="Camera controls">
<button id="reset" type="button" aria-label="Reset camera" title="Reset camera">↺</button>
<button id="zoom-out" type="button" aria-label="Zoom out" title="Zoom out">−</button>
<button id="zoom-in" type="button" aria-label="Zoom in" title="Zoom in">+</button>
</div>
<div id="loading">
<div class="loader" aria-hidden="true"></div>
<span>Setting the scene</span>
</div>
<noscript>This scene requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee4d1);
scene.fog = new THREE.Fog(0xeee4d1, 16, 31);
const camera = new THREE.OrthographicCamera(-7, 7, 4, -4, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
const desiredCamera = {
theta: 0.58,
polar: 1.04,
zoom: 1
};
const currentCamera = {
theta: desiredCamera.theta,
polar: desiredCamera.polar,
zoom: desiredCamera.zoom
};
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x9a8a73, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.25);
keyLight.position.set(-4.5, 8.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.normalBias = 0.025;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9d9d1, 1.1);
fillLight.position.set(6, 4, -5);
scene.add(fillLight);
const warmLight = new THREE.PointLight(0xffb26f, 0.35, 12);
warmLight.position.set(-4, 3, 2);
scene.add(warmLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.85,
metalness: 0,
...options
});
}
function mesh(geometry, material, position, parent = scene) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.copy(position);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function makePanelGeometry(a, b, c, d) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[...a, ...b, ...c, ...d],
3
)
);
geometry.setIndex([0, 1, 2, 0, 2, 3]);
geometry.computeVertexNormals();
return geometry;
}
function rodBetween(a, b, radius, material, parent) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
8
);
const object = mesh(
geometry,
material,
new THREE.Vector3().addVectors(a, b).multiplyScalar(0.5),
parent
);
object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return object;
}
function createTexture(width, height, draw) {
const textureCanvas = document.createElement("canvas");
textureCanvas.width = width;
textureCanvas.height = height;
const context = textureCanvas.getContext("2d");
draw(context, width, height);
const texture = new THREE.CanvasTexture(textureCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function roundedRectShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
// Ground and quiet color-coded grouping zones.
const groundMaterial = standardMaterial(0xeee4d1);
const ground = mesh(
new THREE.PlaneGeometry(60, 60),
groundMaterial,
new THREE.Vector3(0, -0.2, 0)
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
const plinth = mesh(
new THREE.CylinderGeometry(5.65, 5.78, 0.2, 96),
standardMaterial(0xe1d4bc),
new THREE.Vector3(0, -0.1, 0)
);
plinth.castShadow = false;
const rollingZone = mesh(
new THREE.CircleGeometry(1, 72),
new THREE.MeshBasicMaterial({
color: 0xc9664d,
transparent: true,
opacity: 0.065,
depthWrite: false
}),
new THREE.Vector3(-0.25, 0.014, 1.45)
);
rollingZone.rotation.x = -Math.PI / 2;
rollingZone.scale.set(1.65, 1.12, 1);
rollingZone.castShadow = false;
rollingZone.receiveShadow = false;
const stationaryZone = mesh(
new THREE.ShapeGeometry(roundedRectShape(2.55, 2.65, 0.32)),
new THREE.MeshBasicMaterial({
color: 0x5b9287,
transparent: true,
opacity: 0.12,
depthWrite: false
}),
new THREE.Vector3(1.9, 0.017, -0.2)
);
stationaryZone.rotation.x = -Math.PI / 2;
stationaryZone.castShadow = false;
stationaryZone.receiveShadow = false;
// Open kraft-paper grocery bag.
const bag = new THREE.Group();
bag.position.set(-2.15, 0, -0.38);
bag.rotation.y = 0.08;
bag.rotation.z = -0.025;
scene.add(bag);
const bagFrontMaterial = standardMaterial(0xc78a53, {
side: THREE.DoubleSide
});
const bagSideMaterial = standardMaterial(0xb77743, {
side: THREE.DoubleSide
});
const bagBackMaterial = standardMaterial(0x99613b, {
side: THREE.DoubleSide
});
const bagInsideMaterial = standardMaterial(0x74472e, {
side: THREE.DoubleSide
});
const bagRimMaterial = standardMaterial(0xe0ad72);
const frontBottomLeft = [-0.75, 0.08, 0.44];
const frontBottomRight = [0.75, 0.08, 0.44];
const frontTopRight = [1.0, 2.5, 0.63];
const frontTopLeft = [-1.0, 2.5, 0.63];
const backBottomLeft = [-0.73, 0.08, -0.44];
const backBottomRight = [0.73, 0.08, -0.44];
const backTopRight = [0.94, 2.5, -0.58];
const backTopLeft = [-0.94, 2.5, -0.58];
mesh(
makePanelGeometry(
frontBottomLeft,
frontBottomRight,
frontTopRight,
frontTopLeft
),
bagFrontMaterial,
null,
bag
);
mesh(
makePanelGeometry(
backTopLeft,
backTopRight,
backBottomRight,
backBottomLeft
),
bagBackMaterial,
null,
bag
);
mesh(
makePanelGeometry(
frontBottomLeft,
frontTopLeft,
backTopLeft,
backBottomLeft
),
bagSideMaterial,
null,
bag
);
mesh(
makePanelGeometry(
frontBottomRight,
backBottomRight,
backTopRight,
frontTopRight
),
bagSideMaterial,
null,
bag
);
const insideBottom = mesh(
new THREE.PlaneGeometry(1.47, 0.88),
bagInsideMaterial,
new THREE.Vector3(0, 0.1, 0),
bag
);
insideBottom.rotation.x = -Math.PI / 2;
const rimPoints = [
[frontTopLeft, frontTopRight],
[frontTopRight, backTopRight],
[backTopRight, backTopLeft],
[backTopLeft, frontTopLeft]
];
rimPoints.forEach(([a, b]) => {
rodBetween(
new THREE.Vector3(...a),
new THREE.Vector3(...b),
0.035,
bagRimMaterial,
bag
);
});
const handleMaterial = standardMaterial(0xdca66b);
const handleZ = [0.23, -0.39];
handleZ.forEach((z) => {
const handleCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.31, 2.48, z),
new THREE.Vector3(-0.3, 2.86, z - 0.01),
new THREE.Vector3(-0.17, 3.13, z - 0.02),
new THREE.Vector3(0, 3.22, z - 0.02),
new THREE.Vector3(0.17, 3.13, z - 0.02),
new THREE.Vector3(0.3, 2.86, z - 0.01),
new THREE.Vector3(0.31, 2.48, z)
]);
mesh(
new THREE.TubeGeometry(handleCurve, 32, 0.034, 8, false),
handleMaterial,
null,
bag
);
});
const bagLabelTexture = createTexture(512, 320, (ctx, width, height) => {
ctx.fillStyle = "#ead7b1";
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "#9c6841";
ctx.lineWidth = 8;
ctx.strokeRect(18, 18, width - 36, height - 36);
ctx.fillStyle = "#5a4935";
ctx.textAlign = "center";
ctx.font = "800 49px Arial, sans-serif";
ctx.fillText("MARKET", width / 2, 134);
ctx.font = "600 27px Arial, sans-serif";
ctx.fillText("GOOD THINGS, LOOSE.", width / 2, 189);
ctx.fillStyle = "#b46f46";
ctx.beginPath();
ctx.arc(width / 2, 244, 18, 0, Math.PI * 2);
ctx.fill();
});
const bagLabel = mesh(
new THREE.PlaneGeometry(0.83, 0.52),
standardMaterial(0xffffff, { map: bagLabelTexture }),
new THREE.Vector3(-0.04, 1.29, 0.546),
bag
);
bagLabel.rotation.x = 0.078;
const creaseMaterial = new THREE.LineBasicMaterial({
color: 0x83502f,
transparent: true,
opacity: 0.42
});
function addBagCrease(points) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(([x, y]) => {
const z = 0.44 + (y - 0.08) * 0.0785 + 0.008;
return new THREE.Vector3(x, y, z);
})
);
bag.add(new THREE.Line(geometry, creaseMaterial));
}
addBagCrease([
[-0.68, 0.44],
[-0.42, 0.72],
[-0.58, 1.0]
]);
addBagCrease([
[0.68, 0.36],
[0.43, 0.63],
[0.57, 0.91]
]);
addBagCrease([
[-0.21, 1.74],
[0.03, 1.94],
[-0.12, 2.18]
]);
addBagCrease([
[0.74, 1.78],
[0.57, 2.0],
[0.69, 2.26]
]);
// Gable-top carton in the stationary group.
const carton = new THREE.Group();
carton.position.set(2.13, 0, -1.12);
scene.add(carton);
const cartonBodyMaterial = standardMaterial(0x659b91);
const cartonRoofMaterial = standardMaterial(0xf1dfbd);
const cartonRoofSideMaterial = standardMaterial(0xd6bd91);
mesh(
new THREE.BoxGeometry(1.08, 1.42, 0.82),
cartonBodyMaterial,
new THREE.Vector3(0, 0.71, 0),
carton
);
const roofShape = new THREE.Shape();
roofShape.moveTo(-0.54, 0);
roofShape.lineTo(0.54, 0);
roofShape.lineTo(0.54, 0.43);
roofShape.lineTo(0, 0.94);
roofShape.lineTo(-0.54, 0.43);
roofShape.closePath();
const roofGeometry = new THREE.ExtrudeGeometry(roofShape, {
depth: 0.82,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.018,
bevelThickness: 0.018,
steps: 1
});
roofGeometry.translate(0, 0, -0.41);
mesh(
roofGeometry,
[cartonRoofMaterial, cartonRoofSideMaterial],
new THREE.Vector3(0, 1.42, 0),
carton
);
const cartonLabelTexture = createTexture(512, 640, (ctx, width, height) => {
ctx.fillStyle = "#f6ecd8";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#5f978c";
ctx.fillRect(0, 0, width, 145);
ctx.fillStyle = "#f6ecd8";
ctx.textAlign = "center";
ctx.font = "800 31px Arial, sans-serif";
ctx.fillText("DAILY", width / 2, 66);
ctx.font = "700 20px Arial, sans-serif";
ctx.fillText("FARM SERIES", width / 2, 106);
ctx.fillStyle = "#d87355";
ctx.beginPath();
ctx.arc(width / 2, 290, 93, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#f6ecd8";
ctx.font = "800 43px Arial, sans-serif";
ctx.fillText("OAT", width / 2, 286);
ctx.font = "700 23px Arial, sans-serif";
ctx.fillText("& HONEY", width / 2, 326);
ctx.fillStyle = "#36564f";
ctx.font = "700 22px Arial, sans-serif";
ctx.fillText("SMOOTH / LIGHTLY SWEET", width / 2, 448);
ctx.fillStyle = "#b6a990";
ctx.fillRect(82, 492, width - 164, 5);
ctx.font = "600 18px Arial, sans-serif";
ctx.fillText("1 LITER", width / 2, 548);
});
const cartonLabel = mesh(
new THREE.PlaneGeometry(0.99, 1.25),
new THREE.MeshBasicMaterial({
map: cartonLabelTexture,
toneMapped: false
}),
new THREE.Vector3(0, 0.72, 0.416),
carton
);
cartonLabel.castShadow = false;
const cap = mesh(
new THREE.CylinderGeometry(0.085, 0.085, 0.07, 20),
standardMaterial(0xf4e8d1),
new THREE.Vector3(0.24, 1.84, 0.46),
carton
);
cap.rotation.x = Math.PI / 2;
const capRing = mesh(
new THREE.TorusGeometry(0.088, 0.012, 8, 24),
standardMaterial(0xb66c4c),
new THREE.Vector3(0.24, 1.84, 0.501),
carton
);
capRing.castShadow = false;
// Intact rustic loaf, resting on its broad side.
const loaf = new THREE.Group();
loaf.position.set(1.55, 0, 0.64);
loaf.rotation.y = -0.06;
scene.add(loaf);
const loafGeometry = new THREE.SphereGeometry(0.7, 28, 18);
const loafPositions = loafGeometry.attributes.position;
for (let i = 0; i < loafPositions.count; i++) {
const x = loafPositions.getX(i);
const y = loafPositions.getY(i);
const z = loafPositions.getZ(i);
const noise =
Math.sin(x * 15.1 + z * 8.3) *
Math.cos(y * 11.7 - x * 5.4) *
0.012;
loafPositions.setXYZ(
i,
x * (1 + noise),
y * (1 + noise),
z * (1 + noise)
);
}
loafGeometry.computeVertexNormals();
const loafBody = mesh(
loafGeometry,
standardMaterial(0xc98843, {
roughness: 0.96,
flatShading: false
}),
new THREE.Vector3(0, 0.455, 0),
loaf
);
loafBody.scale.set(1.38, 0.61, 0.83);
const scorePositions = [-0.52, 0, 0.52];
scorePositions.forEach((x, index) => {
const top = 0.455 + 0.7 * 0.61 * Math.sqrt(1 - (x / 0.966) ** 2);
const darkScore = mesh(
new THREE.SphereGeometry(1, 18, 10),
standardMaterial(0x7d4829),
new THREE.Vector3(x, top + 0.004, 0),
loaf
);
darkScore.scale.set(0.065, 0.014, 0.29);
darkScore.rotation.y = -0.48 + index * 0.03;
const lightScore = mesh(
new THREE.SphereGeometry(1, 18, 10),
standardMaterial(0xf0d39e),
new THREE.Vector3(x, top + 0.014, 0),
loaf
);
lightScore.scale.set(0.043, 0.017, 0.245);
lightScore.rotation.y = -0.48 + index * 0.03;
});
const flourMaterial = standardMaterial(0xf1dcb2);
const flourSpots = [
[-0.72, 0.68, 0.16],
[-0.31, 0.84, -0.18],
[0.23, 0.84, 0.21],
[0.66, 0.71, -0.12],
[-0.56, 0.72, -0.26],
[0.39, 0.78, -0.24]
];
flourSpots.forEach(([x, y, z]) => {
const flour = mesh(
new THREE.SphereGeometry(0.018, 8, 6),
flourMaterial,
new THREE.Vector3(x, y, z),
loaf
);
flour.scale.y = 0.45;
});
// Apples: round silhouettes make them read as rolling objects.
function createApple(color, accent, leafColor) {
const root = new THREE.Group();
const pivot = new THREE.Group();
pivot.position.y = 0.355;
root.add(pivot);
const geometry = new THREE.SphereGeometry(0.355, 24, 16);
const positions = geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const z = positions.getZ(i);
const topAmount = Math.max(0, (y / 0.355 - 0.58) / 0.42);
const bottomAmount = Math.max(0, (-y / 0.355 - 0.72) / 0.28);
const dimple = 1 - topAmount * topAmount * 0.13 - bottomAmount * 0.045;
positions.setXYZ(i, x * dimple, y, z * dimple);
}
geometry.computeVertexNormals();
const body = mesh(
geometry,
standardMaterial(color, {
roughness: 0.72,
flatShading: true
}),
null,
pivot
);
body.scale.y = 0.95;
const dimpleMaterial = standardMaterial(accent);
const dimple = mesh(
new THREE.CylinderGeometry(0.062, 0.078, 0.012, 16),
dimpleMaterial,
new THREE.Vector3(0, 0.337, 0),
pivot
);
const stem = mesh(
new THREE.CylinderGeometry(0.017, 0.024, 0.14, 7),
standardMaterial(0x6f482d),
new THREE.Vector3(0.012, 0.398, 0),
pivot
);
stem.rotation.z = -0.14;
const leafShape = new THREE.Shape();
leafShape.moveTo(0, 0);
leafShape.bezierCurveTo(0.07, 0.1, 0.19, 0.11, 0.2, 0.04);
leafShape.bezierCurveTo(0.14, -0.025, 0.04, -0.025, 0, 0);
const leaf = mesh(
new THREE.ShapeGeometry(leafShape),
standardMaterial(leafColor, { side: THREE.DoubleSide }),
new THREE.Vector3(0.035, 0.397, 0.005),
pivot
);
leaf.rotation.x = -Math.PI / 2;
leaf.rotation.z = 0.45;
root.userData.pivot = pivot;
return root;
}
const appleSpecs = [
{
position: [-0.72, 0.018, 1.12],
color: 0xc84f3d,
accent: 0x7f2e27,
leaf: 0x688d46,
rolling: true,
phase: 0.2,
speed: 0.55,
radius: 0.13
},
{
position: [0.12, 0.018, 1.72],
color: 0xa9b957,
accent: 0x66782f,
leaf: 0x4f793e,
rolling: true,
phase: 2.1,
speed: 0.42,
radius: 0.1
},
{
position: [0.83, 0.018, 1.22],
color: 0xd55b42,
accent: 0x8f3027,
leaf: 0x6b9147,
rolling: false
},
{
position: [-0.22, 0.018, 2.32],
color: 0xd8a63f,
accent: 0x8e6927,
leaf: 0x5d813d,
rolling: false
},
{
position: [1.24, 0.018, 1.92],
color: 0xb2bd52,
accent: 0x6d7d31,
leaf: 0x51793b,
rolling: false
}
];
const rollingApples = [];
appleSpecs.forEach((spec) => {
const apple = createApple(spec.color, spec.accent, spec.leaf);
apple.position.set(...spec.position);
apple.rotation.y = Math.random() * Math.PI * 2;
scene.add(apple);
if (spec.rolling) {
const baseQuaternion = apple.userData.pivot.quaternion.clone();
apple.userData.roll = {
center: apple.position.clone(),
phase: spec.phase,
speed: spec.speed,
radius: spec.radius,
baseQuaternion
};
rollingApples.push(apple);
}
});
// A few decorative floor marks reinforce the direction of the scatter.
const trailMaterial = new THREE.LineDashedMaterial({
color: 0xc9664d,
dashSize: 0.11,
gapSize: 0.09,
transparent: true,
opacity: 0.4
});
const trailCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-1.23, 0.027, 2.13),
new THREE.Vector3(-0.76, 0.027, 2.5),
new THREE.Vector3(-0.05, 0.027, 2.62),
new THREE.Vector3(0.61, 0.027, 2.48),
new THREE.Vector3(1.16, 0.027, 2.09)
]);
const trail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(trailCurve.getPoints(80)),
trailMaterial
);
trail.computeLineDistances();
scene.add(trail);
// HTML labels are projected directly from 3D anchors.
const tagAnchors = [
{
element: document.querySelector("#tag-bag"),
position: new THREE.Vector3(-2.15, 3.25, -0.38)
},
{
element: document.querySelector("#tag-rolling"),
position: new THREE.Vector3(-0.22, 0.1, 2.63)
},
{
element: document.querySelector("#tag-carton"),
position: new THREE.Vector3(2.13, 2.42, -1.12)
},
{
element: document.querySelector("#tag-loaf"),
position: new THREE.Vector3(1.55, 1.02, 0.64)
}
];
const projected = new THREE.Vector3();
function updateTags() {
tagAnchors.forEach(({ element, position }) => {
projected.copy(position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
});
}
function updateCamera() {
currentCamera.theta +=
(desiredCamera.theta - currentCamera.theta) * 0.09;
currentCamera.polar +=
(desiredCamera.polar - currentCamera.polar) * 0.09;
currentCamera.zoom +=
(desiredCamera.zoom - currentCamera.zoom) * 0.09;
const radius = 12;
const sinPolar = Math.sin(currentCamera.polar);
camera.position.set(
cameraTarget.x + radius * sinPolar * Math.sin(currentCamera.theta),
cameraTarget.y + radius * Math.cos(currentCamera.polar),
cameraTarget.z + radius * sinPolar * Math.cos(currentCamera.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = currentCamera.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(7.55, 10.2 / aspect);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
// Camera interaction is implemented directly without external controls.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.theta -= deltaX * 0.006;
desiredCamera.polar = THREE.MathUtils.clamp(
desiredCamera.polar - deltaY * 0.005,
0.48,
1.38
);
});
function stopDragging(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.zoom = THREE.MathUtils.clamp(
desiredCamera.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.65
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") desiredCamera.theta += 0.12;
if (key === "arrowright") desiredCamera.theta -= 0.12;
if (key === "arrowup") {
desiredCamera.polar = THREE.MathUtils.clamp(
desiredCamera.polar - 0.08,
0.48,
1.38
);
}
if (key === "arrowdown") {
desiredCamera.polar = THREE.MathUtils.clamp(
desiredCamera.polar + 0.08,
0.48,
1.38
);
}
if (key === "+" || key === "=") {
desiredCamera.zoom = THREE.MathUtils.clamp(
desiredCamera.zoom * 1.1,
0.72,
1.65
);
}
if (key === "-") {
desiredCamera.zoom = THREE.MathUtils.clamp(
desiredCamera.zoom / 1.1,
0.72,
1.65
);
}
if (key === "r") resetCamera();
if (key.startsWith("arrow") || key === "+" || key === "=" || key === "-") {
event.preventDefault();
}
});
function resetCamera() {
desiredCamera.theta = 0.58;
desiredCamera.polar = 1.04;
desiredCamera.zoom = 1;
}
document.querySelector("#reset").addEventListener("click", resetCamera);
document.querySelector("#zoom-in").addEventListener("click", () => {
desiredCamera.zoom = THREE.MathUtils.clamp(
desiredCamera.zoom * 1.13,
0.72,
1.65
);
});
document.querySelector("#zoom-out").addEventListener("click", () => {
desiredCamera.zoom = THREE.MathUtils.clamp(
desiredCamera.zoom / 1.13,
0.72,
1.65
);
});
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
const rollAxis = new THREE.Vector3();
const rollQuaternion = new THREE.Quaternion();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
updateCamera();
if (!reducedMotion) {
rollingApples.forEach((apple) => {
const roll = apple.userData.roll;
const angle = elapsed * roll.speed + roll.phase;
apple.position.x =
roll.center.x + Math.cos(angle) * roll.radius;
apple.position.z =
roll.center.z + Math.sin(angle) * roll.radius;
rollAxis.set(Math.cos(angle), 0, Math.sin(angle)).normalize();
rollQuaternion.setFromAxisAngle(
rollAxis,
-angle / roll.radius
);
apple.userData.pivot.quaternion
.copy(rollQuaternion)
.multiply(roll.baseQuaternion);
});
}
renderer.render(scene, camera);
updateTags();
});
renderer.render(scene, camera);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 650);
});
</script>
</body>
</html>
```