Prompt specification
Build or customize the Three.js 3D scene "Refill quantity accounting" 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: Refill quantity accounting
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/refill-quantity-accounting-x0526
Scene Description Prompt:
Fill a low-poly jar with countable grain units, increasing contents while container tare stays fixed. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (refill-quantity-accounting-x0526):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fixed-Tare Grain Jar</title>
<style>
:root {
--ink: #263a32;
--muted: #6e7e76;
--paper: #f3efe5;
--green: #315f4d;
--lime: #c9df72;
--gold: #d4a64c;
--line: rgba(38, 58, 50, 0.15);
}
* {
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,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
cursor: grab;
}
#stage:active {
cursor: grabbing;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.brand {
position: fixed;
z-index: 2;
top: 28px;
left: 32px;
pointer-events: none;
}
.brand__index {
margin-bottom: 9px;
color: var(--green);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
max-width: 300px;
font-size: clamp(25px, 3vw, 43px);
font-weight: 650;
letter-spacing: -0.055em;
line-height: 0.98;
}
.brand p {
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
}
.panel {
border: 1px solid rgba(49, 95, 77, 0.18);
background: rgba(250, 248, 240, 0.84);
box-shadow:
0 20px 60px rgba(37, 56, 47, 0.09),
inset 0 1px 0 rgba(255, 255, 255, 0.7);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.readout {
position: fixed;
z-index: 3;
top: 28px;
right: 28px;
width: 292px;
padding: 18px;
border-radius: 18px;
}
.readout__top {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.lock {
display: inline-flex;
align-items: center;
gap: 5px;
color: var(--green);
}
.lock::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--lime);
box-shadow: 0 0 0 3px rgba(201, 223, 114, 0.2);
}
.gross {
display: flex;
align-items: baseline;
gap: 7px;
margin: 18px 0 16px;
font-variant-numeric: tabular-nums;
}
.gross strong {
font-size: 46px;
font-weight: 520;
letter-spacing: -0.06em;
line-height: 1;
}
.gross span {
color: var(--muted);
font-size: 15px;
}
.readout__rows {
border-top: 1px solid var(--line);
}
.readout__row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 11px 0;
border-bottom: 1px solid var(--line);
font-size: 12px;
}
.readout__row > span {
color: var(--muted);
}
.readout__row strong {
font: 650 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
font-variant-numeric: tabular-nums;
}
.readout__row em {
margin-left: 5px;
color: var(--green);
font-size: 8px;
font-style: normal;
letter-spacing: 0.1em;
}
.meter {
height: 5px;
margin-top: 16px;
overflow: hidden;
border-radius: 99px;
background: rgba(49, 95, 77, 0.11);
}
.meter__fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--green), #91b65a);
transition: width 160ms ease;
}
.controls {
position: fixed;
z-index: 3;
bottom: 28px;
left: 50%;
width: min(620px, calc(100vw - 32px));
padding: 17px 18px 16px;
border-radius: 18px;
transform: translateX(-50%);
}
.control__label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.control__label output {
color: var(--muted);
font-variant-numeric: tabular-nums;
letter-spacing: 0.04em;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
-webkit-appearance: none;
border-radius: 99px;
outline: none;
background:
linear-gradient(
90deg,
var(--green) 0%,
var(--green) var(--fill, 0%),
rgba(49, 95, 77, 0.14) var(--fill, 0%),
rgba(49, 95, 77, 0.14) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
appearance: none;
-webkit-appearance: none;
border: 4px solid #f9f7ef;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 8px rgba(38, 58, 50, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 4px solid #f9f7ef;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 8px rgba(38, 58, 50, 0.28);
}
.buttons {
display: flex;
gap: 8px;
margin-top: 15px;
}
button {
min-height: 37px;
padding: 0 13px;
border: 1px solid rgba(49, 95, 77, 0.19);
border-radius: 10px;
background: transparent;
color: var(--ink);
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
border-color: rgba(49, 95, 77, 0.4);
background: rgba(49, 95, 77, 0.06);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
flex: 1;
border-color: var(--green);
background: var(--green);
color: #f8f6ed;
}
button.primary:hover {
background: #264f3f;
}
.hint {
position: fixed;
z-index: 2;
right: 30px;
bottom: 30px;
color: rgba(38, 58, 50, 0.55);
font: 600 10px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.03em;
text-align: right;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.brand h1 {
font-size: 27px;
}
.brand p {
display: none;
}
.readout {
top: 16px;
right: 16px;
width: 205px;
padding: 13px;
border-radius: 14px;
}
.readout__top {
font-size: 8px;
}
.gross {
margin: 13px 0 10px;
}
.gross strong {
font-size: 34px;
}
.readout__row {
padding: 8px 0;
font-size: 10px;
}
.readout__row strong {
font-size: 10px;
}
.controls {
bottom: 14px;
width: calc(100vw - 28px);
padding: 14px;
border-radius: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive low-poly jar on a scale"></main>
<header class="brand">
<div class="brand__index">Bulk study / 01</div>
<h1>Grain fill<br />laboratory</h1>
<p>Fixed container weight. Countable contents.</p>
</header>
<section class="readout panel" aria-label="Live scale readout">
<div class="readout__top">
<span>Scale / live</span>
<span class="lock">Tare locked</span>
</div>
<div class="gross">
<strong id="grossValue">126.0</strong>
<span>g gross</span>
</div>
<div class="readout__rows">
<div class="readout__row">
<span>Container tare</span>
<strong>126.0 g <em>FIXED</em></strong>
</div>
<div class="readout__row">
<span>Grain units</span>
<strong id="unitValue">000</strong>
</div>
<div class="readout__row">
<span>Contents</span>
<strong id="contentValue">0.0 g</strong>
</div>
</div>
<div class="meter" aria-hidden="true">
<div class="meter__fill" id="meterFill"></div>
</div>
</section>
<section class="controls panel" aria-label="Jar fill controls">
<label class="control__label" for="fillRange">
<span>Fill level</span>
<output id="countOutput" for="fillRange">000 / 420</output>
</label>
<input
id="fillRange"
type="range"
min="0"
max="420"
step="1"
value="0"
aria-label="Number of grain units"
/>
<div class="buttons">
<button class="primary" id="addButton" type="button">+24 units</button>
<button id="autoButton" type="button">Auto fill</button>
<button id="resetButton" type="button">Empty</button>
</div>
</section>
<div class="hint">
Drag to orbit · Wheel to zoom<br />
Click the jar to add 12 · Double-click to reset view
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Configuration
// ------------------------------------------------------------
const MAX_GRAINS = 420;
const GRAINS_PER_LAYER = 24;
const UNIT_MASS = 0.5;
const FIXED_TARE = 126.0;
const stage = document.getElementById("stage");
const fillRange = document.getElementById("fillRange");
const countOutput = document.getElementById("countOutput");
const unitValue = document.getElementById("unitValue");
const contentValue = document.getElementById("contentValue");
const grossValue = document.getElementById("grossValue");
const meterFill = document.getElementById("meterFill");
const addButton = document.getElementById("addButton");
const autoButton = document.getElementById("autoButton");
const resetButton = document.getElementById("resetButton");
let targetCount = 0;
let visualCount = 0;
let autoFilling = false;
let autoTimer = 0;
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee9dd);
scene.fog = new THREE.Fog(0xeee9dd, 10, 22);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const hemisphere = new THREE.HemisphereLight(
0xfff9e9,
0x789183,
2.2
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4da, 4.1);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb7d5cf, 2.3);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
// Ground and low-poly scale platform.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xe9e3d5,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.115;
ground.receiveShadow = true;
scene.add(ground);
const floorMark = new THREE.Mesh(
new THREE.CircleGeometry(3.2, 12),
new THREE.MeshStandardMaterial({
color: 0xe3ddcf,
roughness: 1
})
);
floorMark.rotation.x = -Math.PI / 2;
floorMark.position.y = -0.108;
floorMark.receiveShadow = true;
scene.add(floorMark);
const scaleBase = new THREE.Mesh(
new THREE.CylinderGeometry(1.38, 1.47, 0.18, 12),
new THREE.MeshStandardMaterial({
color: 0x315f4d,
roughness: 0.72,
flatShading: true
})
);
scaleBase.position.y = 0;
scaleBase.castShadow = true;
scaleBase.receiveShadow = true;
scene.add(scaleBase);
const scaleTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.31, 1.31, 0.035, 12),
new THREE.MeshStandardMaterial({
color: 0xdfe3cf,
roughness: 0.65,
flatShading: true
})
);
scaleTop.position.y = 0.105;
scaleTop.receiveShadow = true;
scene.add(scaleTop);
const scaleAccent = new THREE.Mesh(
new THREE.TorusGeometry(0.23, 0.018, 4, 18),
new THREE.MeshStandardMaterial({
color: 0xc9df72,
roughness: 0.5,
flatShading: true
})
);
scaleAccent.rotation.x = Math.PI / 2;
scaleAccent.position.y = 0.13;
scene.add(scaleAccent);
// ------------------------------------------------------------
// Low-poly jar
// ------------------------------------------------------------
const jarGroup = new THREE.Group();
jarGroup.position.y = 0.125;
scene.add(jarGroup);
const outerProfile = [
new THREE.Vector2(0.0, 0.0),
new THREE.Vector2(0.7, 0.0),
new THREE.Vector2(0.9, 0.1),
new THREE.Vector2(1.02, 0.28),
new THREE.Vector2(1.08, 0.64),
new THREE.Vector2(1.08, 1.38),
new THREE.Vector2(1.04, 1.95),
new THREE.Vector2(0.96, 2.4),
new THREE.Vector2(0.82, 2.75),
new THREE.Vector2(0.66, 2.92)
];
const jarGeometry = new THREE.LatheGeometry(
outerProfile,
14
);
const jarMaterial = new THREE.MeshStandardMaterial({
color: 0xa8cfc1,
roughness: 0.22,
metalness: 0.05,
transparent: true,
opacity: 0.27,
depthWrite: false,
side: THREE.DoubleSide,
flatShading: true
});
const jarBody = new THREE.Mesh(jarGeometry, jarMaterial);
jarBody.renderOrder = 3;
jarGroup.add(jarBody);
const jarEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(jarGeometry, 12),
new THREE.LineBasicMaterial({
color: 0x527b6c,
transparent: true,
opacity: 0.62
})
);
jarEdges.renderOrder = 4;
jarGroup.add(jarEdges);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x6f9987,
roughness: 0.36,
metalness: 0.06,
flatShading: true
});
const jarRim = new THREE.Mesh(
new THREE.TorusGeometry(0.665, 0.055, 4, 14),
rimMaterial
);
jarRim.rotation.x = Math.PI / 2;
jarRim.position.y = 2.91;
jarRim.castShadow = true;
jarGroup.add(jarRim);
const jarFoot = new THREE.Mesh(
new THREE.TorusGeometry(0.74, 0.035, 4, 14),
new THREE.MeshStandardMaterial({
color: 0x729d8b,
roughness: 0.45,
flatShading: true
})
);
jarFoot.rotation.x = Math.PI / 2;
jarFoot.position.y = 0.065;
jarGroup.add(jarFoot);
// ------------------------------------------------------------
// Individually countable grain units
// ------------------------------------------------------------
const grainGeometry = new THREE.IcosahedronGeometry(0.07, 0);
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
flatShading: true
});
const grainMesh = new THREE.InstancedMesh(
grainGeometry,
grainMaterial,
MAX_GRAINS
);
grainMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
grainMesh.count = 0;
grainMesh.castShadow = true;
grainMesh.receiveShadow = true;
grainMesh.frustumCulled = false;
jarGroup.add(grainMesh);
const dummy = new THREE.Object3D();
const grainTransforms = [];
const grainColor = new THREE.Color();
// Deterministic, evenly distributed pseudo-random arrangement.
let seed = 73129;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function availableRadius(y) {
if (y < 0.9) {
return 0.82 + y * 0.075;
}
return 0.89 - (y - 0.9) * 0.22;
}
for (let i = 0; i < MAX_GRAINS; i++) {
const layer = Math.floor(i / GRAINS_PER_LAYER);
const index = i % GRAINS_PER_LAYER;
const y = 0.255 + layer * 0.135;
const radiusLimit = availableRadius(y);
// Golden-angle distribution keeps each layer separated and countable.
const radius =
Math.sqrt((index + 0.5) / GRAINS_PER_LAYER) *
radiusLimit *
0.92;
const angle =
index * 2.399963229728653 +
layer * 1.173 +
(random() - 0.5) * 0.18;
const x =
Math.cos(angle) * radius +
(random() - 0.5) * 0.025;
const z =
Math.sin(angle) * radius +
(random() - 0.5) * 0.025;
const quaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(
(random() - 0.5) * 0.8,
random() * Math.PI * 2,
(random() - 0.5) * 0.8
)
);
grainTransforms.push({
position: new THREE.Vector3(x, y, z),
quaternion,
scale: new THREE.Vector3(
1.5 + random() * 0.18,
0.58 + random() * 0.12,
0.76 + random() * 0.12
)
});
grainColor.setHSL(
0.105 + random() * 0.025,
0.48 + random() * 0.18,
0.48 + random() * 0.12
);
grainMesh.setColorAt(i, grainColor);
}
grainMesh.instanceColor.needsUpdate = true;
function updateGrainInstances(value) {
const whole = Math.floor(value + 0.00001);
const fraction = value - whole;
const visible = Math.ceil(value);
grainMesh.count = visible;
for (let i = 0; i < visible; i++) {
const transform = grainTransforms[i];
dummy.position.copy(transform.position);
dummy.quaternion.copy(transform.quaternion);
const grow = i === whole ? Math.max(0.04, fraction) : 1;
dummy.scale.set(
transform.scale.x * grow,
transform.scale.y * grow,
transform.scale.z * grow
);
dummy.updateMatrix();
grainMesh.setMatrixAt(i, dummy.matrix);
}
grainMesh.instanceMatrix.needsUpdate = true;
}
// ------------------------------------------------------------
// Custom camera controls — no external controls addon
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.45, 0);
let azimuth = -0.58;
let polar = 1.17;
let cameraDistance = 6.7;
const defaultView = {
azimuth: -0.58,
polar: 1.17,
cameraDistance: 6.7
};
function updateCamera() {
const sinPolar = Math.sin(polar);
camera.position.set(
cameraTarget.x +
cameraDistance * sinPolar * Math.sin(azimuth),
cameraTarget.y +
cameraDistance * Math.cos(polar),
cameraTarget.z +
cameraDistance * sinPolar * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const canvas = renderer.domElement;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let activePointer = null;
let pointerStartX = 0;
let pointerStartY = 0;
let lastPointerX = 0;
let lastPointerY = 0;
let pointerMoved = false;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
activePointer = event.pointerId;
pointerStartX = lastPointerX = event.clientX;
pointerStartY = lastPointerY = event.clientY;
pointerMoved = false;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
const totalDistance = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalDistance > 4) {
pointerMoved = true;
}
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(
polar - dy * 0.005,
0.38,
1.48
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
if (event.pointerId !== activePointer) return;
const wasClick = !pointerMoved && event.detail < 2;
dragging = false;
activePointer = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (wasClick) {
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);
if (raycaster.intersectObject(jarBody, false).length > 0) {
setTargetCount(targetCount + 12);
stopAutoFill();
}
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
activePointer = null;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance * Math.exp(event.deltaY * 0.001),
4.4,
10
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
azimuth = defaultView.azimuth;
polar = defaultView.polar;
cameraDistance = defaultView.cameraDistance;
updateCamera();
});
// ------------------------------------------------------------
// Controls and fixed-tare readout
// ------------------------------------------------------------
function updateUI() {
const contentsMass = targetCount * UNIT_MASS;
const grossMass = FIXED_TARE + contentsMass;
const percentage = (targetCount / MAX_GRAINS) * 100;
unitValue.textContent = String(targetCount).padStart(3, "0");
countOutput.textContent =
`${String(targetCount).padStart(3, "0")} / ${MAX_GRAINS}`;
contentValue.textContent = `${contentsMass.toFixed(1)} g`;
grossValue.textContent = grossMass.toFixed(1);
meterFill.style.width = `${percentage}%`;
fillRange.style.setProperty("--fill", `${percentage}%`);
fillRange.setAttribute(
"aria-valuetext",
`${targetCount} of ${MAX_GRAINS} grain units`
);
}
function setTargetCount(value) {
targetCount = THREE.MathUtils.clamp(
Math.round(value),
0,
MAX_GRAINS
);
fillRange.value = String(targetCount);
updateUI();
if (targetCount >= MAX_GRAINS) {
stopAutoFill();
}
}
function stopAutoFill() {
autoFilling = false;
autoTimer = 0;
autoButton.textContent = "Auto fill";
autoButton.setAttribute("aria-pressed", "false");
}
fillRange.addEventListener("input", () => {
stopAutoFill();
setTargetCount(Number(fillRange.value));
});
addButton.addEventListener("click", () => {
stopAutoFill();
setTargetCount(targetCount + 24);
});
resetButton.addEventListener("click", () => {
stopAutoFill();
setTargetCount(0);
});
autoButton.addEventListener("click", () => {
if (autoFilling) {
stopAutoFill();
return;
}
if (targetCount >= MAX_GRAINS) {
setTargetCount(0);
}
autoFilling = true;
autoButton.textContent = "Pause fill";
autoButton.setAttribute("aria-pressed", "true");
});
updateUI();
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (autoFilling) {
autoTimer += delta;
if (autoTimer >= 0.15) {
autoTimer = 0;
setTargetCount(targetCount + 4);
}
}
// Smoothly approach the requested exact unit count.
visualCount +=
(targetCount - visualCount) *
(1 - Math.exp(-delta * 10));
if (Math.abs(targetCount - visualCount) < 0.01) {
visualCount = targetCount;
}
updateGrainInstances(visualCount);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```