Prompt specification
Build or customize the Three.js 3D scene "Cooperation versus solo cleanup" 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: Cooperation versus solo cleanup
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cooperation-versus-solo-cleanup-x3912
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing spilled groceries and help from a neighbor. Create the same spill under one-helper and two-helper conditions with explicit action durations; compare completion timelines while every object and route remains consistent between scenarios. Low-poly.
Working Three.js Starter (cooperation-versus-solo-cleanup-x3912):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Spilled Groceries — Controlled Helper Comparison</title>
<style>
:root {
--ink: #173d3a;
--ink-soft: #55706d;
--paper: #f4f0e7;
--panel: #fffdf8;
--line: #d8ddd5;
--teal: #287a78;
--teal-light: #dcece7;
--orange: #dd754e;
--orange-light: #f7e1d7;
--gold: #e7b85c;
--shadow: 0 18px 50px rgba(28, 55, 50, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 12% 0%, rgba(255, 255, 255, 0.9), transparent 31rem),
var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button,
select {
color: var(--ink);
}
.app {
width: min(1580px, calc(100% - 32px));
margin: 0 auto;
padding: 30px 0 38px;
}
.masthead {
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 32px;
margin-bottom: 22px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: var(--teal);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
background: currentColor;
content: "";
}
h1 {
max-width: 800px;
margin: 0;
font-size: clamp(2rem, 4vw, 3.65rem);
line-height: 0.98;
letter-spacing: -0.055em;
}
.intro {
max-width: 530px;
margin: 0 0 4px;
color: var(--ink-soft);
font-size: 0.98rem;
line-height: 1.55;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 340px;
gap: 18px;
align-items: stretch;
}
.stage {
position: relative;
min-width: 0;
height: 680px;
overflow: hidden;
border: 1px solid rgba(33, 67, 62, 0.12);
border-radius: 24px;
background: #e8e5db;
box-shadow: var(--shadow);
isolation: isolate;
}
.three-canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
.three-canvas:active {
cursor: grabbing;
}
.divider {
position: absolute;
z-index: 2;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: rgba(27, 62, 58, 0.19);
pointer-events: none;
}
.view-heads {
position: absolute;
z-index: 3;
inset: 14px 14px auto;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
pointer-events: none;
}
.view-head {
justify-self: start;
max-width: calc(100% - 8px);
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 13px;
background: rgba(255, 253, 248, 0.86);
box-shadow: 0 8px 24px rgba(32, 56, 52, 0.09);
backdrop-filter: blur(8px);
}
.view-head .condition {
display: block;
margin-bottom: 2px;
color: var(--ink-soft);
font-size: 0.62rem;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.view-head strong {
display: block;
font-size: 0.95rem;
letter-spacing: -0.02em;
}
.view-head small {
display: block;
max-width: 340px;
margin-top: 3px;
overflow: hidden;
color: var(--ink-soft);
font-size: 0.69rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.view-head.complete {
border-color: rgba(40, 122, 120, 0.4);
background: rgba(226, 243, 237, 0.92);
}
.camera-hint {
position: absolute;
z-index: 3;
left: 50%;
bottom: 12px;
padding: 6px 10px;
transform: translateX(-50%);
border-radius: 999px;
background: rgba(255, 253, 248, 0.78);
color: var(--ink-soft);
font-size: 0.65rem;
pointer-events: none;
backdrop-filter: blur(6px);
white-space: nowrap;
}
.panel {
display: flex;
flex-direction: column;
gap: 14px;
min-width: 0;
padding: 18px;
border: 1px solid rgba(33, 67, 62, 0.11);
border-radius: 24px;
background: var(--panel);
box-shadow: var(--shadow);
}
.panel h2,
.timeline-panel h2 {
margin: 0;
font-size: 0.95rem;
letter-spacing: -0.025em;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-width: 0;
padding: 12px;
border-radius: 14px;
background: #eef2ec;
}
.metric.teal {
background: var(--teal-light);
}
.metric.orange {
background: var(--orange-light);
}
.metric span {
display: block;
color: var(--ink-soft);
font-size: 0.64rem;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 5px;
font-size: 1.65rem;
line-height: 1;
letter-spacing: -0.055em;
}
.metric small {
display: block;
margin-top: 5px;
color: var(--ink-soft);
font-size: 0.65rem;
}
.saving-note {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 12px;
border: 1px solid #ead5c7;
border-radius: 13px;
background: #fff5ed;
color: #754431;
font-size: 0.75rem;
}
.saving-note strong {
font-size: 1.05rem;
white-space: nowrap;
}
.control-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
button,
select {
min-height: 36px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
padding: 0 11px;
font-size: 0.74rem;
font-weight: 750;
cursor: pointer;
}
button:hover,
select:hover {
border-color: #9eb7b1;
}
button.primary {
border-color: var(--teal);
background: var(--teal);
color: white;
}
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 3px solid rgba(40, 122, 120, 0.24);
outline-offset: 2px;
}
.scrub-label {
display: flex;
justify-content: space-between;
gap: 12px;
color: var(--ink-soft);
font-size: 0.7rem;
font-weight: 750;
}
.scrub-label output {
color: var(--ink);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
cursor: pointer;
}
.setting-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: var(--ink-soft);
font-size: 0.72rem;
}
.setting-row label {
display: inline-flex;
align-items: center;
gap: 7px;
cursor: pointer;
}
.setting-row input {
accent-color: var(--teal);
}
.ledger {
padding-top: 2px;
border-top: 1px solid var(--line);
}
.ledger-title {
margin: 0 0 8px;
font-size: 0.75rem;
font-weight: 800;
}
.ledger-row {
display: grid;
grid-template-columns: 12px 1fr auto;
align-items: center;
gap: 8px;
padding: 5px 0;
color: var(--ink-soft);
font-size: 0.69rem;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
}
.ledger-row strong {
color: var(--ink);
font-variant-numeric: tabular-nums;
}
.cycle-total {
display: flex;
justify-content: space-between;
margin-top: 8px;
padding-top: 9px;
border-top: 1px dashed var(--line);
font-size: 0.73rem;
font-weight: 800;
}
.integrity {
margin-top: auto;
padding: 11px 12px;
border-radius: 13px;
background: var(--ink);
color: #eaf4ef;
font-size: 0.68rem;
line-height: 1.45;
}
.integrity strong {
display: block;
margin-bottom: 3px;
color: white;
font-size: 0.7rem;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.timeline-panel {
margin-top: 18px;
padding: 19px 21px 20px;
border: 1px solid rgba(33, 67, 62, 0.11);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(28, 55, 50, 0.08);
}
.timeline-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 20px;
margin-bottom: 15px;
}
.timeline-top p {
margin: 0;
color: var(--ink-soft);
font-size: 0.73rem;
}
.timeline-row {
display: grid;
grid-template-columns: 145px minmax(0, 1fr);
align-items: center;
gap: 12px;
margin: 8px 0;
}
.timeline-label {
color: var(--ink-soft);
font-size: 0.69rem;
line-height: 1.3;
}
.timeline-label strong {
display: block;
color: var(--ink);
font-size: 0.74rem;
}
.track {
position: relative;
display: flex;
height: 32px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 9px;
background:
repeating-linear-gradient(
90deg,
transparent 0,
transparent calc(20% - 1px),
rgba(23, 61, 58, 0.1) calc(20% - 1px),
rgba(23, 61, 58, 0.1) 20%
),
#edf0eb;
}
.segment {
position: relative;
flex: 0 0 auto;
height: 100%;
overflow: hidden;
border-right: 1px solid rgba(23, 61, 58, 0.12);
background:
linear-gradient(
90deg,
#a8c2b8 0 23.333%,
#ecc36f 23.333% 40%,
#65a1a4 40% 66.667%,
#e69172 66.667% 76.667%,
#c6ceca 76.667% 100%
);
}
.segment-progress {
position: absolute;
inset: 0 auto 0 0;
width: 0;
background: rgba(23, 61, 58, 0.32);
border-right: 2px solid rgba(23, 61, 58, 0.65);
}
.segment span {
position: relative;
z-index: 1;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: #173d3a;
font-size: 0.62rem;
font-weight: 850;
text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.completion-mark {
position: absolute;
z-index: 2;
top: -2px;
bottom: -2px;
left: 60%;
width: 2px;
background: var(--orange);
pointer-events: none;
}
.completion-mark::after {
position: absolute;
top: -15px;
left: 5px;
color: var(--orange);
content: "18s";
font-size: 0.58rem;
font-weight: 850;
white-space: nowrap;
}
.axis {
display: grid;
grid-template-columns: repeat(6, 1fr);
margin-left: 157px;
color: var(--ink-soft);
font-size: 0.59rem;
font-variant-numeric: tabular-nums;
}
.axis span {
text-align: left;
}
.axis span:last-child {
text-align: right;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 1fr;
}
.stage {
height: 680px;
}
.panel {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
align-items: start;
}
.integrity {
grid-column: 1 / -1;
}
}
@media (max-width: 680px) {
.app {
width: min(100% - 18px, 1580px);
padding-top: 20px;
}
.masthead {
display: block;
}
.intro {
margin-top: 14px;
}
.stage {
height: 760px;
}
.divider {
top: 50%;
right: 0;
bottom: auto;
left: 0;
width: auto;
height: 1px;
}
.view-heads {
grid-template-columns: 1fr;
}
.view-head:nth-child(2) {
position: absolute;
top: calc(50% - 2px);
}
.panel {
display: flex;
}
.timeline-panel {
padding: 16px 12px;
}
.timeline-top {
display: block;
}
.timeline-top p {
margin-top: 5px;
}
.timeline-row {
grid-template-columns: 94px minmax(0, 1fr);
gap: 7px;
}
.axis {
margin-left: 101px;
}
.segment span {
font-size: 0.53rem;
}
}
</style>
</head>
<body>
<main class="app">
<header class="masthead">
<div>
<div class="eyebrow">Household tasks / controlled comparison</div>
<h1>A little help.<br />A cleaner floor.</h1>
</div>
<p class="intro">
The same five groceries have fallen beside the cart. Compare a neighbor
working alone with a resident and neighbor working together.
</p>
</header>
<section class="workspace">
<div class="stage" id="stage" aria-label="Interactive 3D comparison of two grocery cleanup scenarios">
<div class="divider"></div>
<div class="view-heads">
<div class="view-head" id="headA">
<span class="condition">Condition A</span>
<strong>1 helper · Neighbor only</strong>
<small id="statusA">Preparing the route…</small>
</div>
<div class="view-head" id="headB">
<span class="condition">Condition B</span>
<strong>2 helpers · Resident + neighbor</strong>
<small id="statusB">Preparing the route…</small>
</div>
</div>
<div class="camera-hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset view</div>
</div>
<aside class="panel">
<div>
<h2>Completion comparison</h2>
</div>
<div class="metric-grid">
<div class="metric">
<span>One helper</span>
<strong>30.0s</strong>
<small>5 sequential item cycles</small>
</div>
<div class="metric teal">
<span>Two helpers</span>
<strong>18.0s</strong>
<small>3 + 2 parallel cycles</small>
</div>
</div>
<div class="saving-note">
<span>12 seconds saved<br /><strong>40% less elapsed time</strong></span>
<span style="font-size: 1.5rem">↘</span>
</div>
<div>
<div class="control-row">
<button class="primary" id="playButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset</button>
<select id="speedSelect" aria-label="Playback speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
</div>
</div>
<label class="scrub-label" for="timelineSlider">
<span>Global timeline</span>
<output id="clock">0.0 / 30.0s</output>
</label>
<input
id="timelineSlider"
type="range"
min="0"
max="30"
step="0.05"
value="0"
aria-label="Scrub comparison timeline"
/>
<div class="setting-row">
<span>Display</span>
<span style="display: flex; gap: 12px">
<label><input id="routesToggle" type="checkbox" checked /> Routes</label>
<label><input id="labelsToggle" type="checkbox" checked /> Labels</label>
</span>
</div>
<div class="ledger">
<p class="ledger-title">Locked action durations</p>
<div class="ledger-row">
<span class="swatch" style="background: #a8c2b8"></span>
<span>Travel to assigned item</span>
<strong>1.4s</strong>
</div>
<div class="ledger-row">
<span class="swatch" style="background: #ecc36f"></span>
<span>Lift and inspect</span>
<strong>1.0s</strong>
</div>
<div class="ledger-row">
<span class="swatch" style="background: #65a1a4"></span>
<span>Carry to cart</span>
<strong>1.6s</strong>
</div>
<div class="ledger-row">
<span class="swatch" style="background: #e69172"></span>
<span>Place in basket</span>
<strong>0.6s</strong>
</div>
<div class="ledger-row">
<span class="swatch" style="background: #c6ceca"></span>
<span>Return to route start</span>
<strong>1.4s</strong>
</div>
<div class="cycle-total">
<span>One item cycle</span>
<span>6.0 seconds</span>
</div>
</div>
<div class="integrity">
<strong>Controlled setup</strong>
Identical grocery set, spill positions, cart position, floor, lighting,
route network, and action durations. Only the number of helpers and
their item assignments change.
</div>
</aside>
</section>
<section class="timeline-panel">
<div class="timeline-top">
<h2>Completion timelines</h2>
<p>Each colored block is one 6.0-second item cycle. Completion includes the final route return.</p>
</div>
<div class="timeline-row">
<div class="timeline-label">
<strong>1 helper</strong>
Neighbor only
</div>
<div class="track" id="trackA"></div>
</div>
<div class="timeline-row">
<div class="timeline-label">
<strong>2 helpers</strong>
Neighbor
</div>
<div class="track" id="trackNeighbor"></div>
</div>
<div class="timeline-row">
<div class="timeline-label">
<strong>2 helpers</strong>
Resident
</div>
<div class="track" id="trackResident">
<div class="completion-mark"></div>
</div>
</div>
<div class="axis" aria-hidden="true">
<span>0s</span>
<span>6s</span>
<span>12s</span>
<span>18s</span>
<span>24s</span>
<span>30s</span>
</div>
</section>
</main>
<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.getElementById("stage");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const speedSelect = document.getElementById("speedSelect");
const timelineSlider = document.getElementById("timelineSlider");
const routesToggle = document.getElementById("routesToggle");
const labelsToggle = document.getElementById("labelsToggle");
const clock = document.getElementById("clock");
const statusA = document.getElementById("statusA");
const statusB = document.getElementById("statusB");
const headA = document.getElementById("headA");
const headB = document.getElementById("headB");
const DURATION = Object.freeze({
travel: 1.4,
lift: 1.0,
carry: 1.6,
place: 0.6,
return: 1.4
});
const CYCLE = Object.values(DURATION).reduce((sum, value) => sum + value, 0);
const MAX_TIME = 30;
const CART = new THREE.Vector3(3.65, 0, -2.2);
const STAGING = new THREE.Vector3(2.8, 0, -1.15);
const ITEM_DEFS = [
{
home: new THREE.Vector3(-2.75, 0, 1.25),
color: 0xf08a32,
label: "1",
crumbs: [[-0.38, 0.18], [0.31, 0.27], [0.12, -0.32]]
},
{
home: new THREE.Vector3(-1.15, 0, 2.35),
color: 0xc64d43,
label: "2",
crumbs: [[-0.32, -0.2], [0.36, 0.14], [-0.08, 0.38]]
},
{
home: new THREE.Vector3(0.72, 0, 1.45),
color: 0x4f86ba,
label: "3",
crumbs: [[-0.35, 0.22], [0.33, -0.24], [0.15, 0.36]]
},
{
home: new THREE.Vector3(-0.45, 0, -0.35),
color: 0x6b9c57,
label: "4",
crumbs: [[-0.31, -0.3], [0.38, 0.2], [0.17, 0.35]]
},
{
home: new THREE.Vector3(1.95, 0, 0.15),
color: 0xe8c84d,
label: "5",
crumbs: [[-0.37, 0.12], [0.34, 0.24], [-0.14, -0.3]]
}
];
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.domElement.className = "three-canvas";
renderer.domElement.setAttribute("aria-label", "Low-poly grocery spill comparison scene");
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
stage.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(37, 1, 0.1, 100);
const orbit = {
target: new THREE.Vector3(0.15, 0.35, -0.25),
radius: 14.8,
theta: 0.69,
phi: 0.86
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
const raycastPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const tempVector = new THREE.Vector3();
const tempVector2 = new THREE.Vector3();
const tempQuaternion = new THREE.Quaternion();
const yAxis = new THREE.Vector3(0, 1, 0);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0,
flatShading: options.flatShading ?? true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1
});
}
function makeTextSprite(text, background = "#fffdf8", foreground = "#173d3a", width = 256) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = 96;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = background;
const x = 8;
const y = 8;
const w = canvas.width - 16;
const h = canvas.height - 16;
const radius = 22;
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + w - radius, y);
context.quadraticCurveTo(x + w, y, x + w, y + radius);
context.lineTo(x + w, y + h - radius);
context.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
context.lineTo(x + radius, y + h);
context.quadraticCurveTo(x, y + h, x, y + h - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
context.fill();
context.fillStyle = foreground;
context.font = "800 38px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.renderOrder = 20;
return sprite;
}
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), mat);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, mat) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 6),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.receiveShadow = true;
return mesh;
}
function buildRouteData(def) {
const home = def.home.clone();
const away = home.clone().sub(STAGING).setY(0).normalize();
const stand = home.clone().addScaledVector(away, 0.43);
const bend = new THREE.Vector3(
THREE.MathUtils.clamp(home.x + 0.78, -2.15, 2.72),
0,
home.z
);
return {
home,
stand,
bend,
out: [STAGING.clone(), bend.clone(), stand.clone()],
carry: [stand.clone(), bend.clone(), STAGING.clone(), CART.clone()],
pickup: [stand.clone(), home.clone()],
cartSpur: [STAGING.clone(), CART.clone()]
};
}
const ROUTES = ITEM_DEFS.map(buildRouteData);
function makeItem(index) {
const group = new THREE.Group();
const def = ITEM_DEFS[index];
if (index === 0) {
const orange = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.28, 0),
material(def.color)
);
orange.position.y = 0.29;
orange.castShadow = true;
group.add(orange);
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.08, 0.19, 5),
material(0x5d8544)
);
leaf.position.set(0.08, 0.59, 0);
leaf.rotation.z = -0.55;
leaf.castShadow = true;
group.add(leaf);
} else if (index === 1) {
const can = new THREE.Mesh(
new THREE.CylinderGeometry(0.23, 0.23, 0.52, 10),
material(def.color, { metalness: 0.22, roughness: 0.55 })
);
can.position.y = 0.27;
can.castShadow = true;
group.add(can);
const lid = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.035, 10),
material(0xc8c8bd, { metalness: 0.58, roughness: 0.38 })
);
lid.position.y = 0.55;
group.add(lid);
} else if (index === 2) {
const box = addBox(
group,
[0.55, 0.72, 0.33],
[0, 0.37, 0],
material(def.color)
);
box.rotation.y = -0.13;
addBox(
group,
[0.43, 0.18, 0.345],
[0, 0.44, 0],
material(0xf2dfad),
false
);
} else if (index === 3) {
const bag = addBox(
group,
[0.54, 0.62, 0.29],
[0, 0.32, 0],
material(def.color)
);
bag.rotation.y = 0.18;
const fold = addBox(
group,
[0.5, 0.13, 0.26],
[0, 0.68, -0.02],
material(0x88aa69)
);
fold.rotation.z = 0.12;
} else {
const banana = new THREE.Group();
const yellow = material(def.color);
for (let piece = 0; piece < 3; piece++) {
const segment = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.25, 0),
yellow
);
segment.scale.set(1.22, 0.52, 0.7);
segment.position.set((piece - 1) * 0.24, 0.18 + Math.abs(piece - 1) * 0.05, 0);
segment.rotation.y = (piece - 1) * 0.28;
segment.castShadow = true;
banana.add(segment);
}
banana.rotation.y = -0.4;
group.add(banana);
}
group.position.copy(def.home);
group.rotation.y = index * 0.31 - 0.42;
return group;
}
function makeCart() {
const group = new THREE.Group();
const frameMat = material(0xc85f42, { roughness: 0.68 });
const basketMat = material(0xe1845f, { roughness: 0.72 });
const darkMat = material(0x344d49);
const metalMat = material(0x9fa9a1, { metalness: 0.42, roughness: 0.5 });
addBox(group, [1.25, 0.12, 0.92], [0, 0.55, 0], basketMat);
addBox(group, [1.25, 0.58, 0.08], [0, 0.86, 0.42], basketMat);
addBox(group, [1.25, 0.58, 0.08], [0, 0.86, -0.42], basketMat);
addBox(group, [0.08, 0.58, 0.82], [-0.59, 0.86, 0], basketMat);
addBox(group, [0.08, 0.58, 0.82], [0.59, 0.86, 0], basketMat);
addBox(group, [0.08, 0.85, 0.08], [-0.48, 1.02, -0.52], metalMat);
addBox(group, [0.08, 0.85, 0.08], [0.48, 1.02, -0.52], metalMat);
addBox(group, [1.04, 0.08, 0.08], [0, 1.42, -0.52], darkMat);
for (const x of [-0.48, 0.48]) {
for (const z of [-0.31, 0.31]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.09, 10),
darkMat
);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.17, z);
wheel.castShadow = true;
group.add(wheel);
}
}
group.position.copy(CART);
group.rotation.y = -0.08;
return group;
}
function makeHuman(name, jacketColor, pantsColor) {
const root = new THREE.Group();
const jacket = material(jacketColor);
const pants = material(pantsColor);
const skin = material(0xd89f79);
const hair = material(0x37312d);
const shoes = material(0x343c3a);
const leftLeg = addBox(root, [0.17, 0.64, 0.2], [-0.13, 0.35, 0], pants);
const rightLeg = addBox(root, [0.17, 0.64, 0.2], [0.13, 0.35, 0], pants);
addBox(root, [0.23, 0.11, 0.31], [-0.13, 0.065, 0.05], shoes);
addBox(root, [0.23, 0.11, 0.31], [0.13, 0.065, 0.05], shoes);
addBox(root, [0.53, 0.66, 0.34], [0, 1.01, 0], jacket);
addBox(root, [0.08, 0.57, 0.36], [0, 1.02, 0.01], material(0xf0eee5));
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.245, 1),
skin
);
head.position.y = 1.57;
head.castShadow = true;
root.add(head);
const hairCap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.25, 1),
hair
);
hairCap.position.set(0, 1.67, -0.025);
hairCap.scale.set(1, 0.55, 1);
hairCap.castShadow = true;
root.add(hairCap);
const nose = new THREE.Mesh(
new THREE.ConeGeometry(0.045, 0.12, 5),
skin
);
nose.rotation.x = Math.PI / 2;
nose.position.set(0, 1.56, 0.245);
root.add(nose);
const leftArm = addBox(root, [0.14, 0.58, 0.17], [-0.35, 1.01, 0], jacket);
const rightArm = addBox(root, [0.14, 0.58, 0.17], [0.35, 1.01, 0], jacket);
const hand = new THREE.Object3D();
hand.position.set(0.29, 0.88, 0.39);
root.add(hand);
const label = makeTextSprite(name, "#fffdf8", "#173d3a", 256);
label.position.set(0, 2.13, 0);
label.scale.set(0.9, 0.24, 1);
root.add(label);
return {
root,
leftLeg,
rightLeg,
leftArm,
rightArm,
hand,
label,
moving: false
};
}
function createScene(kind) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(kind === "one" ? 0xe8e4d8 : 0xe5ebe5);
const hemisphere = new THREE.HemisphereLight(0xfff7e7, 0x637b70, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5de, 3.0);
sun.position.set(-4.5, 10, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(1536, 1536);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.0004;
scene.add(sun);
const floorMat = material(0xd9d4c6);
const floor = addBox(scene, [12.2, 0.2, 9.1], [0, -0.12, 0], floorMat, false);
floor.receiveShadow = true;
const wallMat = material(0xf1eee3);
addBox(scene, [12.2, 3.45, 0.18], [0, 1.6, -4.55], wallMat, false);
const baseboard = addBox(
scene,
[12.1, 0.16, 0.11],
[0, 0.09, -4.42],
material(0x849b8d),
false
);
baseboard.receiveShadow = true;
addBox(scene, [5.3, 0.86, 0.72], [-2.4, 0.42, -4.0], material(0xb2c2b3), false);
addBox(scene, [5.45, 0.12, 0.82], [-2.4, 0.91, -4.0], material(0xf5f0e4), false);
for (const x of [-4.1, -2.4, -0.7]) {
addBox(scene, [0.08, 0.5, 0.08], [x, 0.5, -3.59], material(0x6d8378), false);
}
const routeGroup = new THREE.Group();
const labelGroup = new THREE.Group();
scene.add(routeGroup, labelGroup);
const routeMat = new THREE.MeshBasicMaterial({
color: kind === "one" ? 0x6d968d : 0x5f8b83,
transparent: true,
opacity: 0.46,
depthWrite: false
});
const waypointMat = new THREE.MeshBasicMaterial({
color: 0x5f8b83,
transparent: true,
opacity: 0.56,
depthWrite: false
});
ROUTES.forEach((route) => {
const visiblePaths = [...route.out, ...route.pickup, ...route.cartSpur];
for (let i = 0; i < visiblePaths.length - 1; i++) {
const a = visiblePaths[i].clone();
const b = visiblePaths[i + 1].clone();
a.y = 0.027;
b.y = 0.027;
routeGroup.add(cylinderBetween(a, b, 0.025, routeMat));
}
for (const point of [route.bend, route.stand, route.home]) {
const dot = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.07, 0.022, 10),
waypointMat
);
dot.position.set(point.x, 0.025, point.z);
routeGroup.add(dot);
}
});
const items = ITEM_DEFS.map((def, index) => {
const group = makeItem(index);
scene.add(group);
const label = makeTextSprite(def.label, "#fffdf8", "#173d3a", 128);
label.position.copy(def.home);
label.position.y = 0.98;
label.scale.set(0.38, 0.19, 1);
labelGroup.add(label);
def.crumbs.forEach(([x, z]) => {
const crumb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.055, 0),
material(index === 2 ? 0x75a2c4 : 0xc6a36d)
);
crumb.position.set(def.home.x + x, 0.055, def.home.z + z);
crumb.scale.y = 0.45;
crumb.castShadow = true;
scene.add(crumb);
});
return {
root: group,
home: def.home.clone(),
label
};
});
const cart = makeCart();
scene.add(cart);
const cartLabel = makeTextSprite("CART", "#173d3a", "#fffdf8", 256);
cartLabel.position.set(CART.x, 1.82, CART.z);
cartLabel.scale.set(0.72, 0.24, 1);
labelGroup.add(cartLabel);
const basketMarkers = ITEM_DEFS.map((def, index) => {
const marker = addBox(
cart,
[0.23, 0.2, 0.2],
[
-0.25 + (index % 3) * 0.25,
0.79,
-0.17 + Math.floor(index / 3) * 0.31
],
material(def.color)
);
marker.rotation.y = index * 0.37;
marker.visible = false;
return marker;
});
const helperRecords = [];
if (kind === "one") {
const helper = makeHuman("NEIGHBOR", 0x287a78, 0x29495d);
scene.add(helper.root);
helperRecords.push({ agent: helper, jobs: [0, 1, 2, 3, 4], role: "Neighbor" });
} else {
const neighbor = makeHuman("NEIGHBOR", 0x287a78, 0x29495d);
const resident = makeHuman("YOU", 0xdd754e, 0x36586b);
scene.add(neighbor.root, resident.root);
helperRecords.push({ agent: neighbor, jobs: [0, 2, 4], role: "Neighbor" });
helperRecords.push({ agent: resident, jobs: [1, 3], role: "Resident" });
}
return {
kind,
scene,
routeGroup,
labelGroup,
items,
routes: ROUTES,
cart,
cartLabel,
basketMarkers,
helperRecords,
duration: kind === "one" ? 30 : 18
};
}
const scenarioA = createScene("one");
const scenarioB = createScene("two");
function pathSample(points, u, position, tangent) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = points[i].distanceTo(points[i + 1]);
lengths.push(length);
total += length;
}
let distance = THREE.MathUtils.clamp(u, 0, 1) * total;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i] || i === lengths.length - 1) {
const segmentU = lengths[i] === 0 ? 0 : distance / lengths[i];
position.lerpVectors(points[i], points[i + 1], segmentU);
tangent.subVectors(points[i + 1], points[i]).normalize();
return;
}
distance -= lengths[i];
}
}
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function setAgentPosition(agent, position, tangent = null) {
agent.root.position.copy(position);
if (tangent && tangent.lengthSq() > 0.001) {
agent.root.rotation.y = Math.atan2(tangent.x, tangent.z);
}
agent.root.updateMatrixWorld(true);
}
function faceAgent(agent, direction) {
if (direction.lengthSq() > 0.001) {
agent.root.rotation.y = Math.atan2(direction.x, direction.z);
agent.root.updateMatrixWorld(true);
}
}
function updateScenario(data, time) {
const placed = new Set();
data.items.forEach((item) => {
item.root.visible = true;
item.root.position.copy(item.home);
});
data.helperRecords.forEach((record) => {
const agent = record.agent;
agent.root.position.copy(STAGING);
agent.root.rotation.y = 0;
agent.moving = false;
record.jobs.forEach((itemIndex, jobIndex) => {
const local = time - jobIndex * CYCLE;
if (local < 0) return;
const item = data.items[itemIndex];
const route = data.routes[itemIndex];
if (local >= DURATION.travel + DURATION.lift + DURATION.carry + DURATION.place) {
placed.add(itemIndex);
item.root.visible = false;
}
if (local >= CYCLE) {
setAgentPosition(agent, STAGING);
return;
}
const handPosition = new THREE.Vector3();
agent.root.updateMatrixWorld(true);
agent.hand.getWorldPosition(handPosition);
if (local < DURATION.travel) {
const u = local / DURATION.travel;
pathSample(route.out, u, tempVector, tempVector2);
setAgentPosition(agent, tempVector, tempVector2);
agent.moving = true;
} else if (local < DURATION.travel + DURATION.lift) {
const u = (local - DURATION.travel) / DURATION.lift;
setAgentPosition(agent, route.stand);
faceAgent(agent, tempVector.copy(route.home).sub(route.stand));
agent.root.updateMatrixWorld(true);
agent.hand.getWorldPosition(handPosition);
item.root.position.lerpVectors(item.home, handPosition, smoothstep(u));
} else if (
local <
DURATION.travel + DURATION.lift + DURATION.carry
) {
const u =
(local - DURATION.travel - DURATION.lift) / DURATION.carry;
pathSample(route.carry, u, tempVector, tempVector2);
setAgentPosition(agent, tempVector, tempVector2);
agent.moving = true;
agent.root.updateMatrixWorld(true);
agent.hand.getWorldPosition(handPosition);
item.root.position.copy(handPosition);
} else if (
local <
DURATION.travel + DURATION.lift + DURATION.carry + DURATION.place
) {
const u =
(local -
DURATION.travel -
DURATION.lift -
DURATION.carry) /
DURATION.place;
setAgentPosition(agent, CART);
faceAgent(agent, tempVector.set(0, 0, -1));
agent.root.updateMatrixWorld(true);
agent.hand.getWorldPosition(handPosition);
const target = new THREE.Vector3(
CART.x - 0.22 + (itemIndex % 3) * 0.22,
0.48,
CART.z - 0.15 + Math.floor(itemIndex / 3) * 0.28
);
item.root.position.lerpVectors(handPosition, target, smoothstep(u));
} else {
const u =
(local -
DURATION.travel -
DURATION.lift -
DURATION.carry -
DURATION.place) /
DURATION.return;
pathSample([CART, STAGING], u, tempVector, tempVector2);
setAgentPosition(agent, tempVector, tempVector2);
agent.moving = true;
item.root.visible = false;
}
});
});
data.basketMarkers.forEach((marker, index) => {
marker.visible = placed.has(index);
});
}
function phaseName(local) {
if (local < DURATION.travel) return "walking to";
if (local < DURATION.travel + DURATION.lift) return "lifting";
if (local < DURATION.travel + DURATION.lift + DURATION.carry) return "carrying";
if (
local <
DURATION.travel + DURATION.lift + DURATION.carry + DURATION.place
) return "placing";
return "returning";
}
function scenarioStatus(data, time) {
if (time >= data.duration) {
return "Complete · 5 of 5 groceries in the cart";
}
const parts = [];
data.helperRecords.forEach((record) => {
let description = "finished";
record.jobs.forEach((itemIndex, jobIndex) => {
const start = jobIndex * CYCLE;
const local = time - start;
if (local >= 0 && local < CYCLE) {
description = `${phaseName(local)} item ${itemIndex + 1}`;
}
});
parts.push(`${record.role}: ${description}`);
});
return parts.join(" · ");
}
function updateStatus(data, element, head, time) {
element.textContent = scenarioStatus(data, time);
head.classList.toggle("complete", time >= data.duration);
}
const trackRecords = [];
function buildTrack(elementId, jobIndices) {
const track = document.getElementById(elementId);
jobIndices.forEach((itemIndex, jobIndex) => {
const segment = document.createElement("div");
segment.className = "segment";
segment.style.width = `${(CYCLE / MAX_TIME) * 100}%`;
const progress = document.createElement("div");
progress.className = "segment-progress";
const label = document.createElement("span");
label.textContent = `Item ${itemIndex + 1}`;
segment.append(progress, label);
track.append(segment);
trackRecords.push({
segment,
progress,
jobIndex,
itemIndex
});
});
}
buildTrack("trackA", [0, 1, 2, 3, 4]);
buildTrack("trackNeighbor", [0, 2, 4]);
buildTrack("trackResident", [1, 3]);
function updateTimeline(time) {
trackRecords.forEach((record) => {
const start = record.jobIndex * CYCLE;
const progress = THREE.MathUtils.clamp((time - start) / CYCLE, 0, 1);
record.progress.style.width = `${progress * 100}%`;
record.segment.classList.toggle(
"active",
time >= start && time < start + CYCLE
);
});
clock.textContent = `${time.toFixed(1)} / ${MAX_TIME.toFixed(1)}s`;
timelineSlider.value = time.toFixed(2);
}
let simTime = 0;
let speed = 1;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function updatePlayButton() {
playButton.textContent = playing
? "Pause"
: simTime >= MAX_TIME
? "Replay"
: "Play";
}
playButton.addEventListener("click", () => {
if (!playing && simTime >= MAX_TIME) {
simTime = 0;
}
playing = !playing;
updatePlayButton();
});
resetButton.addEventListener("click", () => {
simTime = 0;
playing = true;
updatePlayButton();
});
speedSelect.addEventListener("change", () => {
speed = Number(speedSelect.value);
});
timelineSlider.addEventListener("input", () => {
simTime = Number(timelineSlider.value);
playing = false;
updatePlayButton();
});
routesToggle.addEventListener("change", () => {
scenarioA.routeGroup.visible = routesToggle.checked;
scenarioB.routeGroup.visible = routesToggle.checked;
});
labelsToggle.addEventListener("change", () => {
scenarioA.labelGroup.visible = labelsToggle.checked;
scenarioB.labelGroup.visible = labelsToggle.checked;
scenarioA.helperRecords.forEach((record) => {
record.agent.label.visible = labelsToggle.checked;
});
scenarioB.helperRecords.forEach((record) => {
record.agent.label.visible = labelsToggle.checked;
});
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLSelectElement
) return;
if (event.code === "Space") {
event.preventDefault();
if (!playing && simTime >= MAX_TIME) simTime = 0;
playing = !playing;
updatePlayButton();
}
if (event.key.toLowerCase() === "r") {
simTime = 0;
playing = true;
updatePlayButton();
}
});
function resetCamera() {
orbit.target.set(0.15, 0.35, -0.25);
orbit.radius = 14.8;
orbit.theta = 0.69;
orbit.phi = 0.86;
updateCamera();
}
const pointers = new Map();
let dragPointer = null;
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function currentPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
dragPointer = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = currentPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1 && dragPointer === event.pointerId) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.35, 1.25);
updateCamera();
} else if (pointers.size === 2) {
const nextDistance = currentPinchDistance();
if (pinchDistance > 0 && nextDistance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (pinchDistance / nextDistance),
9,
24
);
updateCamera();
}
pinchDistance = nextDistance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (dragPointer === event.pointerId) {
dragPointer = null;
}
if (pointers.size < 2) {
pinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
9,
24
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
function isStacked() {
return window.innerWidth <= 680;
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
if (isStacked()) {
camera.aspect = width / (height / 2);
} else {
camera.aspect = (width / 2) / height;
}
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
resize();
function renderScenes() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setScissorTest(true);
if (isStacked()) {
renderer.setViewport(0, height / 2, width, height / 2);
renderer.setScissor(0, height / 2, width, height / 2);
renderer.render(scenarioA.scene, camera);
renderer.setViewport(0, 0, width, height / 2);
renderer.setScissor(0, 0, width, height / 2);
renderer.render(scenarioB.scene, camera);
} else {
renderer.setViewport(0, 0, width / 2, height);
renderer.setScissor(0, 0, width / 2, height);
renderer.render(scenarioA.scene, camera);
renderer.setViewport(width / 2, 0, width / 2, height);
renderer.setScissor(width / 2, 0, width / 2, height);
renderer.render(scenarioB.scene, camera);
}
renderer.setScissorTest(false);
}
updatePlayButton();
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
simTime = Math.min(MAX_TIME, simTime + dt * speed);
if (simTime >= MAX_TIME) {
playing = false;
updatePlayButton();
}
}
updateScenario(scenarioA, simTime);
updateScenario(scenarioB, simTime);
[scenarioA, scenarioB].forEach((scenario) => {
scenario.helperRecords.forEach((record) => {
const agent = record.agent;
const stride = agent.moving ? Math.sin(now * 0.012) * 0.5 : 0;
agent.leftLeg.rotation.x = stride;
agent.rightLeg.rotation.x = -stride;
agent.leftArm.rotation.x = -stride * 0.62;
agent.rightArm.rotation.x = stride * 0.62;
});
});
updateStatus(scenarioA, statusA, headA, simTime);
updateStatus(scenarioB, statusB, headB, simTime);
updateTimeline(simTime);
renderScenes();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```