Prompt specification
Build or customize the Three.js 3D scene "Produce scale versus counting tray" 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: Produce scale versus counting tray
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/produce-scale-versus-counting-tray-x0557
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing produce weighing. Place three differently sized apples on a tray beside a scale and numbered counters; show that equal item counts do not necessarily imply equal modeled mass. Stylized solid forms.
Working Three.js Starter (produce-scale-versus-counting-tray-x0557):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Produce Mass Lab — Count Is Not Mass</title>
<style>
:root {
--ink: #20333b;
--muted: #69777b;
--paper: #f5f0e7;
--card: rgba(255, 255, 255, 0.91);
--small: #e3b84f;
--medium: #61a69f;
--large: #577fb9;
--red: #c94336;
}
* {
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;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 42%, transparent 45%, rgba(76, 61, 43, 0.07) 100%),
linear-gradient(to bottom, rgba(255, 255, 255, 0.12), transparent 35%);
}
.topbar {
position: fixed;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
z-index: 5;
}
.brand {
max-width: 440px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--red);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.2vw, 48px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.brand p {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.concept {
position: absolute;
top: 6px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
padding: 10px 16px;
border: 1px solid rgba(32, 51, 59, 0.11);
border-radius: 999px;
background: rgba(255, 255, 255, 0.68);
box-shadow: 0 10px 30px rgba(72, 56, 37, 0.07);
backdrop-filter: blur(12px);
white-space: nowrap;
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
.concept strong {
color: var(--ink);
font-size: 13px;
}
.concept .not-equal {
color: var(--red);
font-size: 20px;
line-height: 1;
}
.view-controls {
display: flex;
gap: 7px;
pointer-events: auto;
}
.view-controls button {
display: grid;
width: 39px;
height: 39px;
place-items: center;
padding: 0;
border: 1px solid rgba(32, 51, 59, 0.12);
border-radius: 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 8px 20px rgba(72, 56, 37, 0.06);
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
.view-controls button:hover {
transform: translateY(-2px);
background: #fff;
}
.view-controls .reset {
width: auto;
padding: 0 14px;
font-size: 12px;
font-weight: 800;
}
.bottom-panel {
position: fixed;
left: 24px;
right: 24px;
bottom: 20px;
width: min(1060px, calc(100% - 48px));
margin: auto;
padding: 15px;
border: 1px solid rgba(32, 51, 59, 0.11);
border-radius: 22px;
background: var(--card);
box-shadow: 0 18px 60px rgba(75, 58, 39, 0.13);
backdrop-filter: blur(16px);
z-index: 5;
pointer-events: none;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin: 0 3px 12px;
}
.panel-heading strong {
font-size: 13px;
letter-spacing: -0.01em;
}
.panel-heading span {
color: var(--muted);
font-size: 11px;
}
.samples {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.sample {
position: relative;
display: grid;
grid-template-columns: 42px 1fr auto;
align-items: center;
gap: 12px;
min-width: 0;
padding: 12px 14px;
overflow: hidden;
border: 1px solid rgba(32, 51, 59, 0.11);
border-radius: 15px;
color: var(--ink);
text-align: left;
background: rgba(249, 247, 242, 0.86);
cursor: pointer;
pointer-events: auto;
transition:
transform 170ms ease,
border-color 170ms ease,
background 170ms ease,
box-shadow 170ms ease;
}
.sample:hover {
transform: translateY(-2px);
background: #fff;
}
.sample.active {
border-color: var(--accent);
background: #fff;
box-shadow: 0 8px 24px rgba(32, 51, 59, 0.09);
}
.sample::after {
position: absolute;
right: -25px;
bottom: -38px;
width: 90px;
height: 90px;
border-radius: 50%;
background: var(--accent);
content: "";
opacity: 0.08;
pointer-events: none;
}
.sample-number {
display: grid;
width: 42px;
height: 42px;
place-items: center;
border-radius: 13px;
color: #20333b;
background: var(--accent);
font-size: 18px;
font-weight: 900;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.07);
}
.sample-copy {
min-width: 0;
}
.sample-name {
display: block;
margin-bottom: 4px;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.1em;
}
.sample-detail {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.count-dots {
display: inline-flex;
gap: 3px;
}
.count-dots i {
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--red);
}
.sample-mass {
position: relative;
z-index: 1;
text-align: right;
white-space: nowrap;
}
.sample-mass strong {
display: block;
font-size: 21px;
line-height: 1;
letter-spacing: -0.04em;
}
.sample-mass span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 20;
background: var(--paper);
color: var(--muted);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.topbar {
top: 16px;
left: 16px;
right: 16px;
}
.concept {
display: none;
}
.brand p {
max-width: 260px;
}
.bottom-panel {
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
padding: 11px;
}
.panel-heading span {
display: none;
}
.sample {
grid-template-columns: 34px 1fr;
gap: 9px;
padding: 10px;
}
.sample-number {
width: 34px;
height: 34px;
border-radius: 10px;
font-size: 15px;
}
.sample-mass {
grid-column: 1 / -1;
padding-top: 8px;
border-top: 1px solid rgba(32, 51, 59, 0.08);
text-align: left;
}
.sample-mass strong {
display: inline;
margin-right: 7px;
font-size: 17px;
}
.sample-mass span {
display: inline;
margin: 0;
}
}
@media (max-width: 500px) {
.brand p {
display: none;
}
h1 {
font-size: 27px;
}
.view-controls .reset {
display: none;
}
.panel-heading {
margin-bottom: 8px;
}
.panel-heading strong {
font-size: 11px;
}
.samples {
gap: 6px;
}
.sample {
display: block;
padding: 8px;
}
.sample-number {
width: 28px;
height: 28px;
margin-bottom: 8px;
border-radius: 8px;
font-size: 13px;
}
.sample-name {
font-size: 9px;
}
.sample-detail {
display: none;
}
.sample-mass {
margin-top: 7px;
padding-top: 7px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D produce weighing demonstration"></canvas>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Produce Mass Lab · 03</div>
<h1>Same count.<br>Different mass.</h1>
<p>
Select a numbered apple to weigh a three-apple sample.
Item count stays constant while modeled mass changes with size.
</p>
</div>
<div class="concept" aria-label="Three items does not equal the same mass">
<strong>COUNT 3</strong>
<span class="not-equal">≠</span>
<strong>SAME MASS</strong>
</div>
<div class="view-controls" aria-label="Camera controls">
<button id="zoom-out" type="button" aria-label="Zoom out">−</button>
<button id="zoom-in" type="button" aria-label="Zoom in">+</button>
<button id="reset-view" class="reset" type="button">Reset view</button>
</div>
</header>
<section class="bottom-panel" aria-label="Produce samples">
<div class="panel-heading">
<strong>Select a size class to place it on the digital scale</strong>
<span>Every sample contains exactly three apples</span>
</div>
<div class="samples">
<button
class="sample"
type="button"
data-sample="0"
style="--accent: var(--small)"
aria-pressed="false"
>
<span class="sample-number">1</span>
<span class="sample-copy">
<span class="sample-name">SMALL</span>
<span class="sample-detail">
<span class="count-dots"><i></i><i></i><i></i></span>
COUNT 3 · 0.18 KG EACH
</span>
</span>
<span class="sample-mass">
<strong>0.54</strong>
<span>kg modeled mass</span>
</span>
</button>
<button
class="sample active"
type="button"
data-sample="1"
style="--accent: var(--medium)"
aria-pressed="true"
>
<span class="sample-number">2</span>
<span class="sample-copy">
<span class="sample-name">MEDIUM</span>
<span class="sample-detail">
<span class="count-dots"><i></i><i></i><i></i></span>
COUNT 3 · 0.32 KG EACH
</span>
</span>
<span class="sample-mass">
<strong>0.96</strong>
<span>kg modeled mass</span>
</span>
</button>
<button
class="sample"
type="button"
data-sample="2"
style="--accent: var(--large)"
aria-pressed="false"
>
<span class="sample-number">3</span>
<span class="sample-copy">
<span class="sample-name">LARGE</span>
<span class="sample-detail">
<span class="count-dots"><i></i><i></i><i></i></span>
COUNT 3 · 0.55 KG EACH
</span>
</span>
<span class="sample-mass">
<strong>1.65</strong>
<span>kg modeled mass</span>
</span>
</button>
</div>
</section>
<div class="loading" id="loading">Loading produce lab…</div>
<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);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#f4efe7");
scene.fog = new THREE.Fog("#f4efe7", 19, 34);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemi = new THREE.HemisphereLight("#fff8e9", "#9c8870", 2.25);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight("#fff3dc", 4.1);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight("#c8e2ef", 1.25);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...options
});
const materials = {
ground: material("#e5ddd0"),
table: material("#e8d7bd"),
tray: material("#b65d42"),
trayInside: material("#f0d3ad"),
dark: material("#233946"),
scale: material("#294655"),
pan: material("#f3eee4"),
screen: new THREE.MeshBasicMaterial({ color: "#ffffff" }),
stem: material("#67432f"),
leaf: material("#4d7d4e"),
marker: material("#cb4437")
};
function roundedRectShape(width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function roundedBox(width, height, depth, radius = 0.12) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, radius),
{
depth,
bevelEnabled: false,
curveSegments: 10,
steps: 1
}
);
geometry.center();
return geometry;
}
function mesh(geometry, mat, position, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
if (position) object.position.set(...position);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
// Ground and a broad, solid display table.
const ground = mesh(
new THREE.PlaneGeometry(100, 100),
materials.ground,
[0, -0.32, 0]
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
mesh(
roundedBox(10.5, 0.3, 4.3, 0.34),
materials.table,
[0, -0.15, 0]
);
// Left-side specimen tray.
const trayX = -2.45;
const trayZ = 0.35;
const trayTop = 0.295;
mesh(
roundedBox(4.8, 0.24, 2.35, 0.24),
materials.tray,
[trayX, 0.12, trayZ]
);
mesh(
roundedBox(4.22, 0.08, 1.84, 0.18),
materials.trayInside,
[trayX, 0.26, trayZ]
);
mesh(
roundedBox(4.72, 0.21, 0.13, 0.06),
materials.tray,
[trayX, 0.31, trayZ + 1.07]
);
mesh(
roundedBox(4.72, 0.21, 0.13, 0.06),
materials.tray,
[trayX, 0.31, trayZ - 1.07]
);
mesh(
roundedBox(0.13, 0.21, 2.02, 0.06),
materials.tray,
[trayX - 2.33, 0.31, trayZ]
);
mesh(
roundedBox(0.13, 0.21, 2.02, 0.06),
materials.tray,
[trayX + 2.33, 0.31, trayZ]
);
// A stylized lathed apple with a dipped top, stem, and solid leaf.
const appleProfile = [
new THREE.Vector2(0.00, -0.92),
new THREE.Vector2(0.29, -0.88),
new THREE.Vector2(0.55, -0.72),
new THREE.Vector2(0.77, -0.45),
new THREE.Vector2(0.90, -0.12),
new THREE.Vector2(0.92, 0.18),
new THREE.Vector2(0.84, 0.45),
new THREE.Vector2(0.67, 0.68),
new THREE.Vector2(0.45, 0.82),
new THREE.Vector2(0.25, 0.87),
new THREE.Vector2(0.18, 0.76),
new THREE.Vector2(0.00, 0.73)
];
const appleGeometry = new THREE.LatheGeometry(appleProfile, 48);
const appleSpecs = [
{
index: 0,
name: "SMALL",
radius: 0.52,
mass: 0.54,
eachMass: 0.18,
color: "#d84a3e",
accent: "#e3b84f",
x: -3.83,
z: 0.38
},
{
index: 1,
name: "MEDIUM",
radius: 0.69,
mass: 0.96,
eachMass: 0.32,
color: "#c93c34",
accent: "#61a69f",
x: -2.52,
z: 0.22
},
{
index: 2,
name: "LARGE",
radius: 0.87,
mass: 1.65,
eachMass: 0.55,
color: "#b9322d",
accent: "#577fb9",
x: -1.15,
z: 0.43
}
];
const apples = [];
const tokens = [];
const pickMeshes = [];
function numberTexture(number, textColor = "#20333b") {
const c = document.createElement("canvas");
c.width = 256;
c.height = 256;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, 256, 256);
ctx.fillStyle = textColor;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 150px Arial";
ctx.fillText(String(number), 128, 137);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
appleSpecs.forEach((spec) => {
const group = new THREE.Group();
group.position.set(spec.x, trayTop + spec.radius * 0.92, spec.z);
group.userData.baseY = group.position.y;
group.userData.sampleIndex = spec.index;
scene.add(group);
const appleMaterial = material(spec.color, {
roughness: 0.67,
metalness: 0.01
});
const body = mesh(appleGeometry, appleMaterial, [0, 0, 0], group);
body.scale.set(spec.radius, spec.radius, spec.radius);
body.userData.sampleIndex = spec.index;
pickMeshes.push(body);
const stem = mesh(
new THREE.CylinderGeometry(
spec.radius * 0.052,
spec.radius * 0.067,
spec.radius * 0.42,
10
),
materials.stem,
[spec.radius * 0.055, spec.radius * 0.88, 0],
group
);
stem.rotation.z = -0.13;
const leaf = mesh(
new THREE.SphereGeometry(1, 24, 12),
materials.leaf,
[spec.radius * 0.17, spec.radius * 0.86, spec.radius * 0.035],
group
);
leaf.scale.set(
spec.radius * 0.16,
spec.radius * 0.035,
spec.radius * 0.29
);
leaf.rotation.set(0.1, 0.35, 0.28);
group.userData.leaf = leaf;
group.userData.pop = 0;
apples.push(group);
// Numbered physical counter beside each apple.
const token = new THREE.Group();
token.position.set(spec.x, 0.33, 1.03);
token.userData.baseY = token.position.y;
scene.add(token);
const disk = mesh(
new THREE.CylinderGeometry(0.285, 0.285, 0.075, 40),
material(spec.accent),
[0, 0, 0],
token
);
disk.userData.sampleIndex = spec.index;
pickMeshes.push(disk);
const number = mesh(
new THREE.CircleGeometry(0.225, 48),
new THREE.MeshBasicMaterial({
map: numberTexture(spec.index + 1),
transparent: true,
depthWrite: false
}),
[0, 0.039, 0],
token
);
number.rotation.x = -Math.PI / 2;
number.renderOrder = 3;
number.userData.sampleIndex = spec.index;
pickMeshes.push(number);
tokens.push(token);
});
// Right-side digital scale.
const scaleX = 2.65;
mesh(
roundedBox(2.65, 0.36, 2.35, 0.22),
materials.scale,
[scaleX, 0.18, 0.35]
);
mesh(
roundedBox(2.32, 2.13, 0.44, 0.22),
materials.scale,
[scaleX, 1.39, -0.78]
);
mesh(
roundedBox(2.16, 0.16, 1.72, 0.2),
materials.pan,
[scaleX, 0.44, 0.39]
);
// Three solid sample markers on the weighing pan emphasize equal count.
const panMarkers = [];
for (let i = 0; i < 3; i++) {
const marker = mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.045, 32),
material("#cb4437"),
[scaleX + (i - 1) * 0.37, 0.545, 0.58]
);
panMarkers.push(marker);
}
// Screen frame and LCD.
mesh(
roundedBox(1.84, 1.18, 0.14, 0.14),
material("#172a33"),
[scaleX, 1.72, -0.51]
);
const screenCanvas = document.createElement("canvas");
screenCanvas.width = 640;
screenCanvas.height = 360;
const screenContext = screenCanvas.getContext("2d");
const screenTexture = new THREE.CanvasTexture(screenCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
screenTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const screen = mesh(
new THREE.PlaneGeometry(1.62, 0.96),
new THREE.MeshBasicMaterial({ map: screenTexture }),
[scaleX, 1.72, -0.432]
);
screen.castShadow = false;
const knob = mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.12, 36),
material("#ddb75a"),
[scaleX, 0.77, -0.49]
);
knob.rotation.x = Math.PI / 2;
const brandPlate = mesh(
roundedBox(0.96, 0.18, 0.05, 0.05),
material("#ecdfc8"),
[scaleX, 0.43, -0.49]
);
brandPlate.castShadow = false;
// Selected-apple ring.
const ringMaterial = new THREE.MeshBasicMaterial({
color: "#61a69f",
transparent: true,
opacity: 0.8,
depthWrite: false
});
const selectionRing = mesh(
new THREE.TorusGeometry(1, 0.034, 12, 72),
ringMaterial
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.castShadow = false;
let selectedIndex = 1;
let targetMass = appleSpecs[selectedIndex].mass;
let displayedMass = targetMass;
function drawScreen(value, index) {
const spec = appleSpecs[index];
const ctx = screenContext;
const gradient = ctx.createLinearGradient(0, 0, 0, 360);
gradient.addColorStop(0, "#183b35");
gradient.addColorStop(1, "#102a27");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 640, 360);
ctx.fillStyle = spec.accent;
ctx.fillRect(0, 0, 12, 360);
ctx.textAlign = "left";
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#a7c7b8";
ctx.font = "700 25px Arial";
ctx.fillText("MODELED MASS · 3 APPLES", 37, 48);
ctx.fillStyle = "#e3f3d9";
ctx.font = "800 104px Arial";
ctx.fillText(value.toFixed(2), 34, 158);
ctx.font = "700 31px Arial";
ctx.fillText("kg", 334, 156);
ctx.fillStyle = "#a7c7b8";
ctx.font = "700 24px Arial";
ctx.fillText(spec.name, 39, 220);
for (let i = 0; i < 3; i++) {
ctx.beginPath();
ctx.arc(48 + i * 31, 276, 10, 0, Math.PI * 2);
ctx.fillStyle = spec.accent;
ctx.fill();
}
ctx.fillStyle = "#d8ead0";
ctx.font = "700 22px Arial";
ctx.fillText("COUNT = 3", 155, 284);
screenTexture.needsUpdate = true;
}
drawScreen(displayedMass, selectedIndex);
const sampleButtons = [...document.querySelectorAll(".sample")];
function selectSample(index) {
selectedIndex = index;
targetMass = appleSpecs[index].mass;
sampleButtons.forEach((button, buttonIndex) => {
const active = buttonIndex === index;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
apples.forEach((apple) => {
if (apple.userData.sampleIndex === index) {
apple.userData.pop = 1;
}
});
panMarkers.forEach((marker) => {
marker.material.color.set(appleSpecs[index].accent);
});
ringMaterial.color.set(appleSpecs[index].accent);
}
sampleButtons.forEach((button) => {
button.addEventListener("click", () => {
selectSample(Number(button.dataset.sample));
});
});
// Custom orbit controls implemented directly with pointer events.
const cameraTarget = new THREE.Vector3(-0.15, 1.0, 0.18);
const defaultCamera = {
theta: 0.37,
phi: 1.02,
radius: 12.4
};
const orbit = { ...defaultCamera };
let pointerDown = false;
let pointerMoved = 0;
let lastX = 0;
let lastY = 0;
function applyCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
Object.assign(orbit, defaultCamera);
applyCamera();
}
function zoomBy(amount) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + amount,
8.5,
18
);
applyCamera();
}
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = 0;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
pointerMoved += Math.abs(dx) + Math.abs(dy);
lastX = event.clientX;
lastY = event.clientY;
orbit.theta -= dx * 0.0055;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.0045,
0.67,
1.34
);
applyCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (pointerMoved < 7) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (hits.length) {
const index = hits[0].object.userData.sampleIndex;
if (Number.isInteger(index)) selectSample(index);
}
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.5,
18
);
applyCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#zoom-in").addEventListener("click", () => zoomBy(-0.8));
document.querySelector("#zoom-out").addEventListener("click", () => zoomBy(0.8));
document.querySelector("#reset-view").addEventListener("click", resetCamera);
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = camera.aspect < 0.85 ? 51 : 40;
camera.updateProjectionMatrix();
applyCamera();
}
window.addEventListener("resize", resize);
resize();
resetCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
apples.forEach((apple, index) => {
const baseY = apple.userData.baseY;
const bob = Math.sin(time * 1.35 + index * 1.9) * 0.009;
apple.position.y = baseY + bob;
apple.userData.pop = Math.max(0, apple.userData.pop - dt * 2.7);
const popScale = 1 + Math.sin(apple.userData.pop * Math.PI) * 0.045;
apple.scale.setScalar(popScale);
const leaf = apple.userData.leaf;
leaf.rotation.z = 0.28 + Math.sin(time * 1.8 + index) * 0.045;
});
tokens.forEach((token, index) => {
const selected = index === selectedIndex;
token.position.y =
token.userData.baseY +
(selected ? 0.055 : 0) +
Math.sin(time * 2 + index * 1.4) * 0.004;
});
const selectedApple = apples[selectedIndex];
const selectedSpec = appleSpecs[selectedIndex];
selectionRing.position.set(
selectedApple.position.x,
trayTop + 0.022,
selectedApple.position.z
);
selectionRing.scale.setScalar(
selectedSpec.radius * 1.04 * (1 + Math.sin(time * 2.4) * 0.018)
);
ringMaterial.opacity = 0.68 + Math.sin(time * 2.4) * 0.12;
const massDifference = targetMass - displayedMass;
if (Math.abs(massDifference) > 0.0005) {
displayedMass += massDifference * (1 - Math.exp(-dt * 5.5));
drawScreen(displayedMass, selectedIndex);
} else if (displayedMass !== targetMass) {
displayedMass = targetMass;
drawScreen(displayedMass, selectedIndex);
}
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```