Prompt specification
Build or customize the Three.js 3D scene "Package cushioning demonstration bench" 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: Package cushioning demonstration bench
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/package-cushioning-demonstration-bench-x1399
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts impulse during a controlled stop. Compose a falling parcel above two illustrative cushioning pads and synchronized force plots; state the toy model and avoid claiming tested packaging protection performance. Voxel style.
Working Three.js Starter (package-cushioning-demonstration-bench-x1399):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Impulse During a Controlled Stop — Voxel Toy Model</title>
<style>
:root {
color-scheme: dark;
--bg: #080e19;
--panel: #101a2a;
--panel-2: #142235;
--line: #26384f;
--text: #e7f0fb;
--muted: #91a4bb;
--cyan: #42d9e6;
--amber: #ffb34d;
--red: #ff6f77;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button,
select {
color: var(--text);
background: #17263a;
border: 1px solid #31465f;
border-radius: 7px;
}
button {
min-height: 36px;
padding: 0 13px;
cursor: pointer;
font-weight: 700;
}
button:hover,
select:hover {
background: #20334c;
border-color: #4c6683;
}
button:focus-visible,
select:focus-visible,
input:focus-visible,
canvas:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.app {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
header {
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 72px;
padding: 12px 18px;
background: #0c1421;
border-bottom: 1px solid var(--line);
gap: 18px;
}
.brand {
min-width: 0;
}
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font: 700 0.68rem/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1rem, 2vw, 1.45rem);
line-height: 1.15;
letter-spacing: -0.025em;
}
.header-actions {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.status-badge {
padding: 7px 10px;
color: #ffd6d9;
background: rgba(255, 111, 119, 0.11);
border: 1px solid rgba(255, 111, 119, 0.36);
border-radius: 6px;
font: 800 0.68rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
}
#speed {
min-height: 36px;
padding: 0 8px;
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 410px;
min-height: 0;
flex: 1;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #0a1220;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.stage-top,
.stage-bottom {
position: absolute;
z-index: 2;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-top {
top: 15px;
}
.stage-bottom {
bottom: 14px;
align-items: flex-end;
}
.scene-chip {
padding: 8px 10px;
color: #d9e7f7;
background: rgba(8, 15, 26, 0.76);
border: 1px solid rgba(116, 145, 177, 0.28);
border-radius: 7px;
backdrop-filter: blur(8px);
font: 700 0.72rem/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.025em;
}
.scene-chip small {
display: block;
margin-top: 3px;
color: var(--muted);
font-weight: 500;
}
.legend {
display: flex;
gap: 6px;
flex-wrap: wrap;
justify-content: flex-end;
}
.legend span {
padding: 7px 9px;
background: rgba(8, 15, 26, 0.76);
border: 1px solid rgba(116, 145, 177, 0.28);
border-radius: 6px;
font: 700 0.69rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.dot {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
border-radius: 1px;
vertical-align: 0;
}
.dot.cyan {
background: var(--cyan);
box-shadow: 0 0 8px rgba(66, 217, 230, 0.45);
}
.dot.amber {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 179, 77, 0.45);
}
.scene-tag {
position: absolute;
z-index: 3;
transform: translate(-50%, -50%);
padding: 5px 7px;
border: 1px solid;
border-radius: 5px;
background: rgba(8, 15, 26, 0.85);
font: 800 0.66rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
white-space: nowrap;
}
#tagA {
color: var(--cyan);
border-color: rgba(66, 217, 230, 0.48);
}
#tagB {
color: var(--amber);
border-color: rgba(255, 179, 77, 0.48);
}
aside {
min-height: 0;
overflow-y: auto;
padding: 16px;
background: var(--panel);
border-left: 1px solid var(--line);
}
.aside-heading {
margin-bottom: 13px;
}
.aside-heading h2 {
margin: 0 0 5px;
font-size: 1rem;
}
.aside-heading p {
margin: 0;
color: var(--muted);
font-size: 0.77rem;
line-height: 1.45;
}
.chart-block {
margin-bottom: 10px;
padding: 11px;
background: #0c1523;
border: 1px solid #25364b;
border-radius: 9px;
}
.chart-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 5px;
font: 800 0.74rem/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.025em;
}
.chart-title .reading {
font-size: 0.8rem;
}
.chart-a .reading {
color: var(--cyan);
}
.chart-b .reading {
color: var(--amber);
}
.plot {
display: block;
width: 100%;
height: 142px;
}
.timeline-panel {
margin: 13px 0 16px;
padding: 11px;
background: #0c1523;
border: 1px solid #25364b;
border-radius: 9px;
}
.timeline-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font: 700 0.7rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
letter-spacing: 0.055em;
}
#clock {
color: var(--text);
}
.timeline-row {
display: grid;
grid-template-columns: 34px 1fr 34px;
align-items: center;
gap: 7px;
color: var(--muted);
font: 600 0.66rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 16px;
}
.metric {
padding: 9px 8px;
background: var(--panel-2);
border: 1px solid var(--line);
border-radius: 7px;
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font: 700 0.61rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.metric strong {
font: 800 0.91rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.model-card {
padding: 13px;
background: #0d1725;
border: 1px solid #2a3c53;
border-radius: 9px;
}
.model-card h3 {
margin: 0 0 8px;
font-size: 0.88rem;
}
.model-card p,
.model-card li {
color: #b4c2d3;
font-size: 0.75rem;
line-height: 1.5;
}
.model-card p {
margin: 0 0 9px;
}
.model-card ul {
margin: 0 0 11px;
padding-left: 18px;
}
.model-card code {
color: #e3edf9;
font: 700 0.7rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.disclaimer {
margin-top: 11px !important;
padding: 10px;
color: #ffd6d9 !important;
background: rgba(255, 111, 119, 0.08);
border: 1px solid rgba(255, 111, 119, 0.26);
border-radius: 6px;
}
@media (max-width: 920px) {
html,
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100%;
}
header {
align-items: flex-start;
}
main {
display: block;
}
#stage {
height: 58vh;
min-height: 430px;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
}
@media (max-width: 600px) {
header {
flex-direction: column;
}
.header-actions {
width: 100%;
justify-content: flex-start;
}
.status-badge {
order: 3;
width: 100%;
}
.stage-top,
.stage-bottom {
flex-direction: column;
}
.legend {
justify-content: flex-start;
}
.plot {
height: 155px;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<p class="eyebrow">Voxel mechanics laboratory</p>
<h1>Impulse During a Controlled Stop</h1>
</div>
<div class="header-actions">
<span class="status-badge">Toy model · not validated</span>
<button id="playButton" type="button" aria-label="Pause simulation">Pause</button>
<button id="resetButton" type="button">Reset</button>
<select id="speed" aria-label="Playback speed">
<option value="0.25">0.25×</option>
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
</select>
</div>
</header>
<main>
<section id="stage" aria-label="Interactive voxel parcel and cushioning-pad scene">
<div id="scene"></div>
<div class="stage-top">
<div class="scene-chip">
RIGID PARCEL / TWO PADS
<small>Voxel response, not a physical drop test</small>
</div>
<div class="legend" aria-label="Pad color legend">
<span><i class="dot cyan"></i>PAD A</span>
<span><i class="dot amber"></i>PAD B</span>
<span>BLOCK HEIGHT = FORCE</span>
</div>
</div>
<div id="tagA" class="scene-tag">A · 70 N/m</div>
<div id="tagB" class="scene-tag">B · 85 N/m</div>
<div class="stage-bottom">
<div class="scene-chip">
DRAG TO ORBIT · WHEEL TO ZOOM · DOUBLE-CLICK TO RESET VIEW
</div>
<div class="scene-chip">
NO PACKAGING PROTECTION PERFORMANCE IS CLAIMED
</div>
</div>
</section>
<aside aria-label="Simulation data and model notes">
<div class="aside-heading">
<h2>Synchronized force traces</h2>
<p>
Both plots share the same time cursor. Bright trace sections are
elapsed; dim sections show the remaining toy-model response.
</p>
</div>
<div class="chart-block chart-a">
<div class="chart-title">
<span><i class="dot cyan"></i>PAD A FORCE</span>
<span class="reading" id="forceAValue">0.0 N</span>
</div>
<canvas id="plotA" class="plot" aria-label="Pad A force versus time"></canvas>
</div>
<div class="chart-block chart-b">
<div class="chart-title">
<span><i class="dot amber"></i>PAD B FORCE</span>
<span class="reading" id="forceBValue">0.0 N</span>
</div>
<canvas id="plotB" class="plot" aria-label="Pad B force versus time"></canvas>
</div>
<div class="timeline-panel">
<div class="timeline-head">
<span>Shared simulation time</span>
<span id="clock">0.00 s</span>
</div>
<div class="timeline-row">
<span>0 s</span>
<input
id="timeline"
type="range"
min="0"
max="6"
step="0.001"
value="0"
aria-label="Scrub synchronized simulation time"
/>
<span>6 s</span>
</div>
</div>
<div class="metrics">
<div class="metric">
<span>Vertical speed</span>
<strong id="speedValue">0.00 m/s</strong>
</div>
<div class="metric">
<span>Compression</span>
<strong id="compressionValue">0 mm</strong>
</div>
<div class="metric">
<span>Parcel height</span>
<strong id="heightValue">5.70 m</strong>
</div>
</div>
<div class="model-card">
<h3>Toy model</h3>
<p>
The parcel is represented as one rigid point mass. Each pad is a
unilateral linear spring–damper contact. Both pads share the same
geometric overlap, while their illustrative constants differ.
</p>
<ul>
<li>Parcel mass: <code>1.20 kg</code></li>
<li>Gravity: <code>9.81 m/s²</code></li>
<li>
Pad A: <code>k = 70 N/m</code>,
<code>c = 6.5 N·s/m</code>
</li>
<li>
Pad B: <code>k = 85 N/m</code>,
<code>c = 7.5 N·s/m</code>
</li>
<li>Fixed integration step: <code>1/240 s</code></li>
</ul>
<p class="disclaimer">
Illustrative visualization only. The constants are not calibrated
to a real cushioning material, and this scene is not a prediction
of damage or a packaging protection result. Representative
packages require instrumented testing before performance claims
can be made.
</p>
</div>
</aside>
</main>
</div>
<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("scene");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const speedSelect = document.getElementById("speed");
const timeline = document.getElementById("timeline");
const clock = document.getElementById("clock");
const speedValue = document.getElementById("speedValue");
const compressionValue = document.getElementById("compressionValue");
const heightValue = document.getElementById("heightValue");
const forceAValue = document.getElementById("forceAValue");
const forceBValue = document.getElementById("forceBValue");
const plotA = document.getElementById("plotA");
const plotB = document.getElementById("plotB");
const COLORS = {
cyan: 0x42d9e6,
amber: 0xffb34d,
darkCyan: 0x17606d,
darkAmber: 0x7a4c22,
background: 0x0a1220,
platform: 0x152235,
parcel: 0xbc8150,
parcelLight: 0xd59b65,
tape: 0xf0d79a,
outline: 0x273a50
};
const PARAMS = {
mass: 1.2,
gravity: 9.81,
kA: 70,
kB: 85,
cA: 6.5,
cB: 7.5
};
const DURATION = 6;
const FIXED_DT = 1 / 240;
const SAMPLE_EVERY = 4;
const SAMPLE_DT = FIXED_DT * SAMPLE_EVERY;
const PAD_BASE_TOP = 0.24;
const PAD_HEIGHT = 1.44;
const PARCEL_HEIGHT = 1.5;
const PARCEL_HALF_HEIGHT = PARCEL_HEIGHT / 2;
const REST_CENTER_Y = PAD_BASE_TOP + PAD_HEIGHT + PARCEL_HALF_HEIGHT;
const INITIAL_Y = 5.7;
// ---------------------------------------------------------------------
// Renderer, camera, and direct camera controls
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
const canvas = renderer.domElement;
canvas.tabIndex = 0;
canvas.setAttribute("aria-label", "Voxel simulation. Drag to orbit and use the wheel to zoom.");
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 12, 24);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 60);
const orbit = {
target: new THREE.Vector3(0, 2.45, 0),
radius: 11.2,
theta: 0.68,
phi: 1.03
};
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);
}
function resetCamera() {
orbit.target.set(0, 2.45, 0);
orbit.radius = 11.2;
orbit.theta = 0.68;
orbit.phi = 1.03;
updateCamera();
}
let dragging = false;
let dragX = 0;
let dragY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.focus();
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
orbit.theta -= dx * 0.008;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.006, 0.42, 1.43);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
6.5,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta -= 0.08;
break;
case "ArrowRight":
orbit.theta += 0.08;
break;
case "ArrowUp":
orbit.phi = Math.max(0.42, orbit.phi - 0.06);
break;
case "ArrowDown":
orbit.phi = Math.min(1.43, orbit.phi + 0.06);
break;
case "+":
case "=":
orbit.radius = Math.max(6.5, orbit.radius - 0.5);
break;
case "-":
orbit.radius = Math.min(18, orbit.radius + 0.5);
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
// ---------------------------------------------------------------------
// Lights
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc9e3ff, 0x172033, 2.2));
const keyLight = new THREE.DirectionalLight(0xfff1d8, 3.1);
keyLight.position.set(4.5, 9, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5edfff, 1.25);
rimLight.position.set(-5, 4, -5);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Voxel scene helpers
// ---------------------------------------------------------------------
function makeBox(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
parent = scene,
options = {}
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
return mesh;
}
function addEdges(mesh, color = COLORS.outline, opacity = 0.65) {
const edges = new THREE.EdgesGeometry(mesh.geometry);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const outline = new THREE.LineSegments(edges, material);
mesh.add(outline);
return outline;
}
// Platform and voxel tile field.
makeBox(7.2, 0.42, 5.4, COLORS.platform, 0, -0.22, 0);
const tileGeometry = new THREE.BoxGeometry(0.47, 0.06, 0.47);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
flatShading: true
});
const tileCountX = 14;
const tileCountZ = 10;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < tileCountX; ix++) {
for (let iz = 0; iz < tileCountZ; iz++) {
dummy.position.set(
-3.25 + ix * 0.5,
0.025,
-2.25 + iz * 0.5
);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const shade = (ix + iz) % 3;
tileColor.setHex(
shade === 0 ? 0x1b2b40 : shade === 1 ? 0x203249 : 0x18273a
);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.receiveShadow = true;
tiles.castShadow = false;
scene.add(tiles);
// Rear voxel drop guide.
const guideMaterial = new THREE.MeshBasicMaterial({
color: 0x7894b2,
transparent: true,
opacity: 0.32
});
for (let y = 2.75; y <= 6.35; y += 0.34) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.055, 0.055),
guideMaterial
);
marker.position.set(0, y, -1.25);
scene.add(marker);
}
// ---------------------------------------------------------------------
// Cushioning pads and force gauges
// ---------------------------------------------------------------------
function createPad(x, color, darkColor, label) {
const root = new THREE.Group();
root.position.x = x;
scene.add(root);
const base = makeBox(
1.72,
PAD_BASE_TOP,
1.82,
0x202e41,
0,
PAD_BASE_TOP / 2,
0,
root
);
addEdges(base, 0x3c5068, 0.45);
const compressible = new THREE.Group();
compressible.position.y = PAD_BASE_TOP;
root.add(compressible);
const layerMaterials = [];
for (let i = 0; i < 4; i++) {
const layer = makeBox(
1.28,
0.28,
1.38,
i % 2 === 0 ? color : darkColor,
0,
0.16 + i * 0.295,
0,
compressible,
{
emissive: color,
emissiveIntensity: 0
}
);
layerMaterials.push(layer.material);
}
const cap = makeBox(
1.42,
0.16,
1.5,
color,
0,
1.35,
0,
compressible,
{
emissive: color,
emissiveIntensity: 0
}
);
addEdges(cap, 0x0c1c28, 0.5);
// Pixelated force gauge in front of each pad.
const gauge = new THREE.Group();
gauge.position.set(x, 0, 1.62);
scene.add(gauge);
makeBox(
0.62,
0.12,
0.4,
0x1a2a3d,
0,
0.08,
0,
gauge
);
const gaugeBlocks = [];
for (let i = 0; i < 9; i++) {
const block = makeBox(
0.17,
0.15,
0.17,
0x26354a,
0,
0.24 + i * 0.2,
0,
gauge,
{
emissive: 0x000000,
emissiveIntensity: 0
}
);
gaugeBlocks.push(block);
}
return {
x,
compressible,
layerMaterials,
cap,
gaugeBlocks,
label
};
}
const padA = createPad(-1.15, COLORS.cyan, COLORS.darkCyan, "A");
const padB = createPad(1.15, COLORS.amber, COLORS.darkAmber, "B");
// ---------------------------------------------------------------------
// Voxel parcel
// ---------------------------------------------------------------------
const parcel = new THREE.Group();
parcel.rotation.y = -0.055;
scene.add(parcel);
const parcelBody = makeBox(
2.9,
PARCEL_HEIGHT,
2.1,
COLORS.parcel,
0,
0,
0,
parcel,
{ roughness: 0.9 }
);
addEdges(parcelBody, 0x4e3426, 0.68);
// Top tape.
makeBox(
0.34,
0.045,
2.15,
COLORS.tape,
0,
PARCEL_HALF_HEIGHT + 0.024,
0,
parcel
);
// Front tape.
makeBox(
0.34,
1.53,
0.045,
COLORS.tape,
0,
0,
1.075,
parcel
);
// Side tape.
makeBox(
0.045,
1.53,
0.34,
COLORS.tape,
1.475,
0,
0,
parcel
);
// Shipping label and tiny voxel barcode.
makeBox(
0.64,
0.48,
0.025,
0xe9e2cf,
0.77,
0.08,
1.09,
parcel,
{ castShadow: false }
);
const barcodeWidths = [0.035, 0.065, 0.03, 0.08, 0.045, 0.065, 0.035];
let barcodeX = 0.51;
for (const width of barcodeWidths) {
makeBox(
width,
0.21,
0.012,
0x27313a,
barcodeX + width / 2,
0.02,
1.11,
parcel,
{ castShadow: false }
);
barcodeX += width + 0.025;
}
// Corner protectors.
const cornerPositions = [
[-1.34, -0.65, 0.94],
[1.34, -0.65, 0.94],
[-1.34, 0.65, 0.94],
[1.34, 0.65, 0.94]
];
for (const [x, y, z] of cornerPositions) {
makeBox(0.2, 0.2, 0.12, COLORS.parcelLight, x, y, z, parcel);
}
// ---------------------------------------------------------------------
// Toy-model simulation
// ---------------------------------------------------------------------
function simulateToyModel() {
const samples = [];
let y = INITIAL_Y;
let velocity = 0;
const stepCount = Math.round(DURATION / FIXED_DT);
for (let step = 0; step <= stepCount; step++) {
const overlap = Math.max(0, REST_CENTER_Y - y);
let forceA = 0;
let forceB = 0;
if (overlap > 0) {
// Unilateral Kelvin-Voigt contacts.
forceA = Math.max(
0,
PARAMS.kA * overlap - PARAMS.cA * velocity
);
forceB = Math.max(
0,
PARAMS.kB * overlap - PARAMS.cB * velocity
);
}
if (step % SAMPLE_EVERY === 0) {
samples.push({
time: step * FIXED_DT,
y,
velocity,
compression: overlap,
forceA,
forceB
});
}
const acceleration =
-PARAMS.gravity + (forceA + forceB) / PARAMS.mass;
velocity += acceleration * FIXED_DT;
y += velocity * FIXED_DT;
}
return samples;
}
const samples = simulateToyModel();
const maxForce = Math.max(
...samples.flatMap((sample) => [sample.forceA, sample.forceB])
);
const plotMaxForce = Math.max(20, Math.ceil(maxForce / 20) * 20);
function sampleAt(time) {
const index = THREE.MathUtils.clamp(
time / SAMPLE_DT,
0,
samples.length - 1
);
const i = Math.floor(index);
const j = Math.min(i + 1, samples.length - 1);
const fraction = index - i;
const a = samples[i];
const b = samples[j];
return {
time,
y: THREE.MathUtils.lerp(a.y, b.y, fraction),
velocity: THREE.MathUtils.lerp(a.velocity, b.velocity, fraction),
compression: THREE.MathUtils.lerp(
a.compression,
b.compression,
fraction
),
forceA: THREE.MathUtils.lerp(a.forceA, b.forceA, fraction),
forceB: THREE.MathUtils.lerp(a.forceB, b.forceB, fraction)
};
}
// ---------------------------------------------------------------------
// Plot rendering, using the same time axis and cursor for both charts
// ---------------------------------------------------------------------
function hexToRgba(hex, alpha) {
const color = new THREE.Color(hex);
return `rgba(${Math.round(color.r * 255)}, ${
Math.round(color.g * 255)
}, ${Math.round(color.b * 255)}, ${alpha})`;
}
function preparePlot(plotCanvas) {
const rect = plotCanvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(rect.width * dpr));
const height = Math.max(1, Math.round(rect.height * dpr));
if (plotCanvas.width !== width || plotCanvas.height !== height) {
plotCanvas.width = width;
plotCanvas.height = height;
}
const context = plotCanvas.getContext("2d");
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.clearRect(0, 0, rect.width, rect.height);
return { context, width: rect.width, height: rect.height };
}
function drawPlot(plotCanvas, forceKey, color, currentTime) {
const { context, width, height } = preparePlot(plotCanvas);
const left = 37;
const right = 10;
const top = 14;
const bottom = 25;
const chartWidth = width - left - right;
const chartHeight = height - top - bottom;
const xForTime = (time) => left + (time / DURATION) * chartWidth;
const yForForce = (force) =>
top + chartHeight - (force / plotMaxForce) * chartHeight;
context.font =
'10px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace';
context.textBaseline = "middle";
// Grid.
context.strokeStyle = "rgba(112, 139, 167, 0.14)";
context.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const y = top + (chartHeight * i) / 4;
const value = plotMaxForce * (1 - i / 4);
context.beginPath();
context.moveTo(left, y + 0.5);
context.lineTo(width - right, y + 0.5);
context.stroke();
context.fillStyle = "#71849a";
context.textAlign = "right";
context.fillText(value.toFixed(0), left - 7, y);
}
context.textAlign = "left";
context.fillStyle = "#8fa3b8";
context.fillText("N", 7, 7);
for (const tick of [0, 2, 4, 6]) {
const x = xForTime(tick);
context.strokeStyle = "rgba(112, 139, 167, 0.08)";
context.beginPath();
context.moveTo(x + 0.5, top);
context.lineTo(x + 0.5, top + chartHeight);
context.stroke();
context.fillStyle = "#71849a";
context.textAlign = tick === 0 ? "left" : tick === 6 ? "right" : "center";
context.fillText(`${tick}s`, x, height - 10);
}
// Full trace, dim.
context.strokeStyle = hexToRgba(color, 0.2);
context.lineWidth = 1.6;
context.beginPath();
samples.forEach((sample, index) => {
const x = xForTime(sample.time);
const y = yForForce(sample[forceKey]);
if (index === 0) context.moveTo(x, y);
else context.lineTo(x, y);
});
context.stroke();
// Elapsed trace, bright.
context.save();
context.beginPath();
context.rect(left, top - 2, chartWidth + 2, chartHeight + 4);
context.clip();
context.strokeStyle = hexToRgba(color, 0.98);
context.lineWidth = 2.25;
context.lineJoin = "miter";
context.lineCap = "square";
context.beginPath();
let started = false;
for (const sample of samples) {
if (sample.time > currentTime) break;
const x = xForTime(sample.time);
const y = yForForce(sample[forceKey]);
if (!started) {
context.moveTo(x, y);
started = true;
} else {
context.lineTo(x, y);
}
}
context.stroke();
context.restore();
// Shared time cursor.
const cursorX = xForTime(currentTime);
context.save();
context.setLineDash([3, 4]);
context.strokeStyle = "rgba(231, 240, 251, 0.62)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(cursorX + 0.5, top);
context.lineTo(cursorX + 0.5, top + chartHeight);
context.stroke();
context.restore();
// Current sample marker.
const current = sampleAt(currentTime);
const markerY = yForForce(current[forceKey]);
context.fillStyle = hexToRgba(color, 1);
context.fillRect(cursorX - 3, markerY - 3, 6, 6);
}
// ---------------------------------------------------------------------
// Playback
// ---------------------------------------------------------------------
let currentTime = 0;
let playing = true;
let playbackSpeed = 1;
let lastFrame = performance.now();
function updatePlayButton() {
playButton.textContent = playing ? "Pause" : "Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause simulation" : "Play simulation"
);
}
playButton.addEventListener("click", () => {
if (!playing && currentTime >= DURATION) currentTime = 0;
playing = !playing;
updatePlayButton();
});
resetButton.addEventListener("click", () => {
currentTime = 0;
playing = true;
updatePlayButton();
});
speedSelect.addEventListener("change", () => {
playbackSpeed = Number(speedSelect.value);
});
timeline.addEventListener("input", () => {
currentTime = Number(timeline.value);
playing = false;
updatePlayButton();
});
// ---------------------------------------------------------------------
// Scene updates
// ---------------------------------------------------------------------
const tagA = document.getElementById("tagA");
const tagB = document.getElementById("tagB");
const projected = new THREE.Vector3();
function placeSceneTag(element, x) {
projected.set(x, 0.2, 1.95).project(camera);
const rect = stage.getBoundingClientRect();
element.style.left = `${(projected.x * 0.5 + 0.5) * rect.width}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * rect.height}px`;
element.style.display = projected.z > 1 ? "none" : "block";
}
function updatePad(pad, force, currentColor) {
const visualCompression = Math.min(
pad.compression ?? 0,
PAD_HEIGHT * 0.52
);
const scale = Math.max(
0.48,
(PAD_HEIGHT - visualCompression) / PAD_HEIGHT
);
pad.compressible.scale.y = scale;
const normalizedForce = THREE.MathUtils.clamp(
force / Math.max(plotMaxForce, 1),
0,
1
);
for (const material of pad.layerMaterials) {
material.emissive.setHex(currentColor);
material.emissiveIntensity = 0.08 + normalizedForce * 0.48;
}
pad.cap.material.emissive.setHex(currentColor);
pad.cap.material.emissiveIntensity = 0.16 + normalizedForce * 0.72;
const activeBlocks = Math.ceil(normalizedForce * pad.gaugeBlocks.length);
pad.gaugeBlocks.forEach((block, index) => {
const active = index < activeBlocks;
block.material.color.setHex(
active ? currentColor : 0x26354a
);
block.material.emissive.setHex(active ? currentColor : 0x000000);
block.material.emissiveIntensity = active ? 0.48 : 0;
});
}
function updateScene(state) {
parcel.position.y = state.y;
// The two contacts share the same geometric overlap in this toy model.
padA.compression = state.compression;
padB.compression = state.compression;
updatePad(padA, state.forceA, COLORS.cyan);
updatePad(padB, state.forceB, COLORS.amber);
placeSceneTag(tagA, padA.x);
placeSceneTag(tagB, padB.x);
clock.textContent = `${state.time.toFixed(2)} s`;
timeline.value = state.time.toFixed(3);
speedValue.textContent = `${Math.abs(state.velocity).toFixed(2)} m/s`;
compressionValue.textContent = `${Math.round(state.compression * 1000)} mm`;
heightValue.textContent = `${state.y.toFixed(2)} m`;
forceAValue.textContent = `${state.forceA.toFixed(1)} N`;
forceBValue.textContent = `${state.forceB.toFixed(1)} N`;
drawPlot(plotA, "forceA", COLORS.cyan, state.time);
drawPlot(plotB, "forceB", COLORS.amber, state.time);
}
function resizeRenderer() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", () => {
resizeRenderer();
updateCamera();
});
resizeRenderer();
updateCamera();
function animate(now) {
const delta = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (playing) {
currentTime = Math.min(DURATION, currentTime + delta * playbackSpeed);
if (currentTime >= DURATION) {
playing = false;
updatePlayButton();
}
}
const state = sampleAt(currentTime);
updateScene(state);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```