Prompt specification
Build or customize the Three.js 3D scene "Playground Bounce Surface Comparison" 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: Playground Bounce Surface Comparison
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/playground-bounce-surface-comparison-x3603
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows ball rebounds and energy loss. Build a small playground with three labeled toy surface models. Drop the same ball from equal heights and compare rebounds without claiming calibrated real material properties. Voxel style.
Working Three.js Starter (playground-bounce-surface-comparison-x3603):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta
name="description"
content="A voxel-style Three.js playground comparing illustrative ball rebounds and energy loss on three toy surface models."
>
<title>Rebound Lab — Voxel Playground</title>
<style>
:root {
--ink: #202536;
--muted: #687084;
--paper: #f4efe5;
--panel: #fffdf8;
--line: #ddd7cc;
--cushion: #56b86b;
--spring: #ef6478;
--rigid: #4e8fd8;
--accent: #f5b942;
--shadow: 0 12px 30px rgba(27, 31, 43, 0.12);
}
* {
box-sizing: border-box;
}
html {
color-scheme: light;
}
body {
margin: 0;
min-width: 320px;
background:
linear-gradient(90deg, rgba(32, 37, 54, 0.025) 1px, transparent 1px),
linear-gradient(rgba(32, 37, 54, 0.025) 1px, transparent 1px),
var(--paper);
background-size: 24px 24px;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 22px clamp(18px, 4vw, 56px);
color: #fff;
background: var(--ink);
border-bottom: 5px solid var(--accent);
}
.brand {
display: flex;
align-items: center;
gap: 15px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 auto;
background: var(--accent);
border: 3px solid #171b28;
box-shadow:
6px 6px 0 #ef6478,
9px 9px 0 #56b86b;
transform: rotate(-5deg);
}
.brand-mark::after {
content: "";
position: absolute;
inset: 8px;
background: #fff7e7;
}
h1 {
margin: 0;
font-size: clamp(1.35rem, 3vw, 2rem);
line-height: 1;
letter-spacing: -0.035em;
}
.brand p {
margin: 6px 0 0;
color: #b9bfce;
font-size: 0.86rem;
}
.model-note-badge {
flex: 0 0 auto;
padding: 9px 12px;
color: #ffe8ad;
background: rgba(245, 185, 66, 0.12);
border: 1px solid rgba(245, 185, 66, 0.55);
border-radius: 4px;
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.09em;
text-align: center;
}
main {
width: min(1480px, 100%);
margin: 0 auto;
padding: clamp(16px, 3vw, 34px);
}
.stage {
position: relative;
height: clamp(500px, 68vh, 720px);
min-height: 500px;
overflow: hidden;
background: #eee9df;
border: 2px solid #d7d0c4;
border-radius: 12px;
box-shadow: var(--shadow);
isolation: isolate;
}
.stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
.stage canvas:active {
cursor: grabbing;
}
.hud {
position: absolute;
z-index: 5;
pointer-events: none;
}
.hud > * {
pointer-events: auto;
}
.toolbar {
top: 16px;
left: 16px;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.control {
min-height: 42px;
padding: 9px 14px;
color: var(--ink);
background: rgba(255, 253, 248, 0.94);
border: 2px solid var(--ink);
border-radius: 5px;
box-shadow: 4px 4px 0 rgba(32, 37, 54, 0.18);
font-weight: 800;
cursor: pointer;
transition:
transform 120ms ease,
box-shadow 120ms ease,
background 120ms ease;
}
.control:hover:not(:disabled) {
transform: translate(-1px, -1px);
box-shadow: 6px 6px 0 rgba(32, 37, 54, 0.2);
}
.control:active:not(:disabled) {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 rgba(32, 37, 54, 0.2);
}
.control:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 3px;
}
.control.primary {
color: #fff;
background: var(--ink);
}
.control:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.scene-key {
top: 16px;
right: 16px;
display: grid;
gap: 7px;
max-width: 240px;
padding: 12px 14px;
background: rgba(255, 253, 248, 0.91);
border: 1px solid rgba(32, 37, 54, 0.18);
border-radius: 6px;
color: var(--muted);
font-size: 0.76rem;
line-height: 1.35;
box-shadow: 0 6px 18px rgba(32, 37, 54, 0.08);
}
.scene-key strong {
color: var(--ink);
}
.status {
position: absolute;
z-index: 5;
left: 50%;
bottom: 18px;
width: max-content;
max-width: calc(100% - 28px);
padding: 10px 15px;
transform: translateX(-50%);
color: #fff;
background: rgba(32, 37, 54, 0.91);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 5px;
box-shadow: 0 7px 20px rgba(32, 37, 54, 0.2);
font-size: 0.82rem;
font-weight: 700;
text-align: center;
pointer-events: none;
}
.camera-hint {
position: absolute;
z-index: 4;
bottom: 17px;
left: 16px;
color: rgba(32, 37, 54, 0.64);
font-size: 0.72rem;
font-weight: 700;
pointer-events: none;
}
.compare {
margin-top: 24px;
}
.compare-heading {
display: flex;
align-items: end;
justify-content: space-between;
gap: 18px;
margin-bottom: 13px;
}
.compare-heading h2 {
margin: 0;
font-size: 1.15rem;
letter-spacing: -0.02em;
}
.compare-heading p {
max-width: 650px;
margin: 0;
color: var(--muted);
font-size: 0.82rem;
line-height: 1.45;
text-align: right;
}
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 13px;
}
.model-card {
position: relative;
padding: 16px;
overflow: hidden;
background: var(--panel);
border: 1px solid var(--line);
border-top: 5px solid var(--model-color);
border-radius: 8px;
box-shadow: 0 7px 18px rgba(32, 37, 54, 0.06);
}
.model-card::before {
content: "";
position: absolute;
top: 16px;
right: 15px;
width: 13px;
height: 13px;
background: var(--model-color);
border: 2px solid var(--ink);
box-shadow:
4px 4px 0 color-mix(in srgb, var(--model-color), white 45%);
transform: rotate(8deg);
}
.card-title {
margin: 0 30px 3px 0;
font-size: 1rem;
font-weight: 900;
}
.card-subtitle {
margin: 0;
color: var(--muted);
font-size: 0.72rem;
}
.meter-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-top: 17px;
font-size: 0.75rem;
font-weight: 800;
}
.meter-head .numbers {
white-space: nowrap;
}
.meter-head em {
margin-left: 5px;
color: var(--muted);
font-size: 0.66rem;
font-style: normal;
font-weight: 600;
}
.meter {
height: 12px;
margin-top: 7px;
overflow: hidden;
background: #e8e2d8;
border: 1px solid #d2cabd;
border-radius: 2px;
}
.meter-fill {
width: 100%;
height: 100%;
background: var(--model-color);
transition: width 160ms ease-out;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 14px;
}
.stat {
padding: 9px 10px;
background: #f2ede4;
border-radius: 4px;
}
.stat span {
display: block;
color: var(--muted);
font-size: 0.65rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.stat strong {
display: block;
margin-top: 3px;
font-size: 1.05rem;
}
.disclaimer {
margin: 18px 0 8px;
padding: 13px 16px;
color: #5b6171;
background: rgba(255, 253, 248, 0.72);
border: 1px dashed #cfc7ba;
border-radius: 6px;
font-size: 0.76rem;
line-height: 1.55;
}
.disclaimer strong {
color: var(--ink);
}
footer {
padding: 0 20px 24px;
color: #777d8c;
font-size: 0.72rem;
text-align: center;
}
@media (max-width: 840px) {
.topbar {
align-items: flex-start;
}
.model-note-badge {
max-width: 145px;
}
.scene-key {
top: auto;
right: 12px;
bottom: 62px;
max-width: 185px;
}
.camera-hint {
display: none;
}
.cards {
grid-template-columns: 1fr;
}
.compare-heading {
display: block;
}
.compare-heading p {
margin-top: 7px;
text-align: left;
}
}
@media (max-width: 560px) {
.topbar {
display: block;
}
.model-note-badge {
display: inline-block;
max-width: none;
margin-top: 18px;
}
main {
padding: 12px;
}
.stage {
height: 560px;
min-height: 560px;
}
.toolbar {
top: 10px;
left: 10px;
}
.control {
min-height: 38px;
padding: 8px 10px;
font-size: 0.78rem;
}
.scene-key {
display: none;
}
.status {
bottom: 12px;
font-size: 0.72rem;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Rebound Lab</h1>
<p>One voxel ball. Three toy landings. Different energy outcomes.</p>
</div>
</div>
<div class="model-note-badge">
ILLUSTRATIVE SCENE RULES<br>
NOT MATERIAL CALIBRATION
</div>
</header>
<main>
<section class="stage" id="stage" aria-label="Interactive three-dimensional rebound playground">
<div class="hud toolbar">
<button class="control primary" id="dropButton" type="button">▶ Drop all</button>
<button class="control" id="pauseButton" type="button" disabled>Pause</button>
<button class="control" id="resetButton" type="button">Reset</button>
<button class="control" id="cameraButton" type="button">Reset camera</button>
</div>
<div class="hud scene-key">
<strong>Controlled comparison</strong>
<span>Same ball geometry</span>
<span>Same absolute release height</span>
<span>Different scene-only energy rules</span>
</div>
<div class="status" id="status" role="status" aria-live="polite">
Ready — three identical voxel balls wait at equal height.
</div>
<div class="camera-hint">Drag to orbit · Scroll or pinch to zoom</div>
</section>
<section class="compare" aria-labelledby="compareTitle">
<div class="compare-heading">
<h2 id="compareTitle">Compare the rebounds</h2>
<p>
Each meter shows mechanical energy remaining relative to release.
The percentages are illustrative scene parameters, not measurements.
</p>
</div>
<div class="cards">
<article class="model-card" style="--model-color: var(--cushion)">
<h3 class="card-title">Cushion Cubes</h3>
<p class="card-subtitle">Green · scene rule retains 12%</p>
<div class="meter-head">
<span>Energy left</span>
<span class="numbers">
<b id="energy0">100%</b>
<em id="loss0">0% lost</em>
</span>
</div>
<div class="meter"><div class="meter-fill" id="meter0"></div></div>
<div class="stats">
<div class="stat">
<span>First rebound</span>
<strong id="first0">—</strong>
</div>
<div class="stat">
<span>Best peak</span>
<strong id="peak0">—</strong>
</div>
</div>
</article>
<article class="model-card" style="--model-color: var(--spring)">
<h3 class="card-title">Spring Tiles</h3>
<p class="card-subtitle">Pink · scene rule retains 42%</p>
<div class="meter-head">
<span>Energy left</span>
<span class="numbers">
<b id="energy1">100%</b>
<em id="loss1">0% lost</em>
</span>
</div>
<div class="meter"><div class="meter-fill" id="meter1"></div></div>
<div class="stats">
<div class="stat">
<span>First rebound</span>
<strong id="first1">—</strong>
</div>
<div class="stat">
<span>Best peak</span>
<strong id="peak1">—</strong>
</div>
</div>
</article>
<article class="model-card" style="--model-color: var(--rigid)">
<h3 class="card-title">Solid Blocks</h3>
<p class="card-subtitle">Blue · scene rule retains 8%</p>
<div class="meter-head">
<span>Energy left</span>
<span class="numbers">
<b id="energy2">100%</b>
<em id="loss2">0% lost</em>
</span>
</div>
<div class="meter"><div class="meter-fill" id="meter2"></div></div>
<div class="stats">
<div class="stat">
<span>First rebound</span>
<strong id="first2">—</strong>
</div>
<div class="stat">
<span>Best peak</span>
<strong id="peak2">—</strong>
</div>
</div>
</article>
</div>
<div class="disclaimer">
<strong>Model note:</strong>
Surface names, colors, and retention values are visual scene presets.
This playground is not calibrated to foam, springs, wood, rubber, or any
other real material and should not be used for engineering or product
performance predictions.
</div>
</section>
</main>
<footer>
Keyboard: Space = drop all · R = reset · drag the scene to orbit
</footer>
<noscript>This playground requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and scene configuration
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const statusElement = document.getElementById("status");
const dropButton = document.getElementById("dropButton");
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const cameraButton = document.getElementById("cameraButton");
const SURFACE_Y = 0.46;
const BALL_RADIUS = 0.22;
const IMPACT_Y = SURFACE_Y + BALL_RADIUS;
const INITIAL_Y = 3.75;
const DROP_HEIGHT = INITIAL_Y - IMPACT_Y;
const GRAVITY = 9.0;
const modelDefinitions = [
{
title: "Cushion Cubes",
type: "cushion",
retain: 0.12,
color: 0x56b86b,
css: "#56b86b",
x: -4.35
},
{
title: "Spring Tiles",
type: "spring",
retain: 0.42,
color: 0xef6478,
css: "#ef6478",
x: 0
},
{
title: "Solid Blocks",
type: "rigid",
retain: 0.08,
color: 0x4e8fd8,
css: "#4e8fd8",
x: 4.35
}
];
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0xeee9df, 1);
renderer.domElement.setAttribute(
"aria-label",
"Three voxel balls released above three labeled toy surfaces"
);
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee9df);
scene.fog = new THREE.Fog(0xeee9df, 18, 31);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraState = {
radius: 15,
theta: 0.63,
phi: 1.03,
target: new THREE.Vector3(0, 1.65, 0)
};
const defaultCamera = {
radius: 15,
theta: 0.63,
phi: 1.03
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x7a8397, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4db, 3.25);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d7ff, 0.85);
fill.position.set(7, 4, -5);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared materials and helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0.02,
...options
});
}
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRectPath(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeLabelSprite(title, subtitle, color) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 180;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.shadowColor = "rgba(32, 37, 54, 0.2)";
context.shadowBlur = 18;
context.shadowOffsetY = 8;
roundedRectPath(context, 10, 10, 620, 158, 20);
context.fillStyle = "rgba(255, 253, 248, 0.97)";
context.fill();
context.shadowColor = "transparent";
context.lineWidth = 5;
context.strokeStyle = color;
context.stroke();
context.fillStyle = color;
context.fillRect(30, 35, 23, 23);
context.fillStyle = "#202536";
context.font = "900 36px system-ui, sans-serif";
context.textBaseline = "top";
context.fillText(title, 70, 29);
context.fillStyle = "#687084";
context.font = "700 22px system-ui, sans-serif";
context.fillText(subtitle, 32, 91);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
renderer.capabilities.getMaxAnisotropy(),
8
);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.45, 0.97, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Playground base
// -------------------------------------------------------------------------
const groundMaterial = standardMaterial(0xd8d0c2);
addBox(
scene,
new THREE.Vector3(17.3, 0.35, 6.2),
new THREE.Vector3(0, -0.26, 0),
groundMaterial,
false
);
const groundTop = new THREE.Mesh(
new THREE.PlaneGeometry(17.25, 6.15),
standardMaterial(0xe5ded2, { roughness: 1 })
);
groundTop.rotation.x = -Math.PI / 2;
groundTop.position.y = -0.081;
groundTop.receiveShadow = true;
scene.add(groundTop);
// Subtle voxel floor markings.
const markingMaterial = standardMaterial(0xcfc6b7);
for (let x = -8; x <= 8; x += 1) {
addBox(
scene,
new THREE.Vector3(0.025, 0.012, 5.85),
new THREE.Vector3(x, -0.067, 0),
markingMaterial,
false
);
}
for (let z = -2.5; z <= 2.5; z += 1) {
addBox(
scene,
new THREE.Vector3(16.9, 0.012, 0.025),
new THREE.Vector3(0, -0.066, z),
markingMaterial,
false
);
}
// -------------------------------------------------------------------------
// Toy surface models
// -------------------------------------------------------------------------
const models = [];
const platformMaterial = standardMaterial(0xfaf7f0);
const platformEdgeMaterial = standardMaterial(0xc9c0b3);
function createCushionSurface(model) {
const deform = new THREE.Group();
deform.position.y = 0.18;
model.root.add(deform);
const colors = [0x56b86b, 0x65c779, 0x48a65d, 0x72cf83];
for (let ix = 0; ix < 6; ix++) {
for (let iz = 0; iz < 4; iz++) {
const material = standardMaterial(
colors[(ix + iz * 2) % colors.length]
);
addBox(
deform,
new THREE.Vector3(0.48, 0.28, 0.48),
new THREE.Vector3(
(ix - 2.5) * 0.52,
0.14,
(iz - 1.5) * 0.52
),
material
);
}
}
}
function createSpringSurface(model) {
const deform = new THREE.Group();
deform.position.y = 0.18;
model.root.add(deform);
const stemMaterial = standardMaterial(0xbd4058);
const capMaterial = standardMaterial(0xef6478);
const capAccentMaterial = standardMaterial(0xf7b942);
for (let ix = 0; ix < 5; ix++) {
for (let iz = 0; iz < 3; iz++) {
const x = (ix - 2) * 0.58;
const z = (iz - 1) * 0.58;
addBox(
deform,
new THREE.Vector3(0.25, 0.17, 0.25),
new THREE.Vector3(x, 0.085, z),
stemMaterial
);
addBox(
deform,
new THREE.Vector3(0.52, 0.11, 0.52),
new THREE.Vector3(x, 0.225, z),
(ix + iz) % 4 === 0 ? capAccentMaterial : capMaterial
);
}
}
}
function createRigidSurface(model) {
const deform = new THREE.Group();
deform.position.y = 0.18;
model.root.add(deform);
const colors = [0x4e8fd8, 0x3f7fc8, 0x66a2e2];
for (let ix = 0; ix < 4; ix++) {
for (let iz = 0; iz < 3; iz++) {
addBox(
deform,
new THREE.Vector3(0.76, 0.28, 0.76),
new THREE.Vector3(
(ix - 1.5) * 0.8,
0.14,
(iz - 1) * 0.8
),
standardMaterial(colors[(ix + iz) % colors.length])
);
}
}
}
modelDefinitions.forEach((definition, index) => {
const root = new THREE.Group();
root.position.x = definition.x;
scene.add(root);
addBox(
root,
new THREE.Vector3(3.62, 0.18, 2.7),
new THREE.Vector3(0, 0.09, 0),
platformMaterial
);
addBox(
root,
new THREE.Vector3(3.7, 0.08, 2.78),
new THREE.Vector3(0, 0.015, 0),
platformEdgeMaterial
);
if (definition.type === "cushion") {
createCushionSurface({ root });
} else if (definition.type === "spring") {
createSpringSurface({ root });
} else {
createRigidSurface({ root });
}
const label = makeLabelSprite(
definition.title,
`TOY MODEL 0${index + 1} · SCENE RULE ${Math.round(
definition.retain * 100
)}%`,
definition.css
);
label.position.set(0, 0.5, 1.83);
root.add(label);
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(definition.x + 1.48, SURFACE_Y, 0.88),
new THREE.Vector3(definition.x + 1.48, INITIAL_Y, 0.88)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0x9d9589,
dashSize: 0.11,
gapSize: 0.09,
transparent: true,
opacity: 0.45
})
);
guide.computeLineDistances();
scene.add(guide);
const peakGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(definition.x + 1.48, SURFACE_Y, 0.88),
new THREE.Vector3(definition.x + 1.48, SURFACE_Y, 0.88)
]);
const peakLine = new THREE.Line(
peakGeometry,
new THREE.LineDashedMaterial({
color: definition.color,
dashSize: 0.11,
gapSize: 0.075,
transparent: true,
opacity: 0.95
})
);
peakLine.visible = false;
scene.add(peakLine);
const peakCap = addBox(
scene,
new THREE.Vector3(0.46, 0.065, 0.1),
new THREE.Vector3(definition.x + 1.48, SURFACE_Y, 0.88),
standardMaterial(definition.color),
false
);
peakCap.visible = false;
models.push({
...definition,
root,
deform: root.children.find((child) => child.isGroup),
peakLine,
peakCap,
impactTime: -10,
impactAmplitude:
definition.type === "spring"
? 0.38
: definition.type === "cushion"
? 0.22
: 0.045
});
});
// -------------------------------------------------------------------------
// Equal-height reference
// -------------------------------------------------------------------------
const releaseLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-6.8, INITIAL_Y, -1.18),
new THREE.Vector3(6.8, INITIAL_Y, -1.18)
]),
new THREE.LineDashedMaterial({
color: 0x8b8497,
dashSize: 0.16,
gapSize: 0.12,
transparent: true,
opacity: 0.55
})
);
releaseLine.computeLineDistances();
scene.add(releaseLine);
const releaseLabel = makeLabelSprite(
"EQUAL RELEASE",
"IDENTICAL BALLS · IDENTICAL HEIGHT",
"#8b8497"
);
releaseLabel.scale.set(2.65, 0.76, 1);
releaseLabel.position.set(0, INITIAL_Y + 0.55, -1.2);
scene.add(releaseLabel);
// -------------------------------------------------------------------------
// Voxel balls
// -------------------------------------------------------------------------
function createVoxelBall() {
const group = new THREE.Group();
const cubeSize = 0.13;
const spacing = 0.13;
const orange = standardMaterial(0xf39a3f);
const pale = standardMaterial(0xffd27b);
const dark = standardMaterial(0xe77b2c);
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const distance = x * x + y * y + z * z;
if (distance > 2) continue;
let material = orange;
if ((x + y + z) % 3 === 0) material = pale;
if (distance === 2) material = dark;
const cube = new THREE.Mesh(
new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize),
material
);
cube.position.set(
x * spacing,
y * spacing,
z * spacing
);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
}
}
}
return group;
}
const states = models.map((model) => {
const ball = createVoxelBall();
ball.position.set(model.x, INITIAL_Y, 0);
scene.add(ball);
return {
ball,
y: INITIAL_Y,
vy: 0,
energy: 1,
settled: false,
impactCount: 0,
firstRebound: null,
bestPeak: 0,
state: "ready"
};
});
// -------------------------------------------------------------------------
// Impact particles
// -------------------------------------------------------------------------
const particleGeometry = new THREE.BoxGeometry(0.09, 0.09, 0.09);
const particles = [];
for (let i = 0; i < 60; i++) {
const material = standardMaterial(0xffffff, {
transparent: true,
opacity: 0
});
const mesh = new THREE.Mesh(particleGeometry, material);
mesh.visible = false;
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(),
life: 0,
maxLife: 1
});
}
let particleCursor = 0;
function spawnImpactParticles(x, color) {
for (let i = 0; i < 10; i++) {
const particle = particles[particleCursor];
particleCursor = (particleCursor + 1) % particles.length;
particle.mesh.visible = true;
particle.mesh.position.set(
x + (Math.random() - 0.5) * 0.18,
IMPACT_Y,
(Math.random() - 0.5) * 0.18
);
particle.mesh.scale.setScalar(0.7 + Math.random() * 0.8);
particle.mesh.material.color.setHex(color);
particle.mesh.material.opacity = 1;
particle.velocity.set(
(Math.random() - 0.5) * 3.2,
1.6 + Math.random() * 2.4,
(Math.random() - 0.5) * 2.2
);
particle.maxLife = 0.45 + Math.random() * 0.35;
particle.life = particle.maxLife;
}
}
function updateParticles(delta) {
particles.forEach((particle) => {
if (!particle.mesh.visible || particle.life <= 0) return;
particle.life -= delta;
particle.velocity.y -= 7 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += delta * 7;
particle.mesh.rotation.y += delta * 9;
const lifeRatio = Math.max(0, particle.life / particle.maxLife);
particle.mesh.material.opacity = lifeRatio;
particle.mesh.scale.setScalar(Math.max(0.05, lifeRatio));
if (particle.life <= 0) {
particle.mesh.visible = false;
}
});
}
// -------------------------------------------------------------------------
// Simulation and readouts
// -------------------------------------------------------------------------
let running = false;
let paused = false;
let simulationTime = 0;
const energyElements = modelDefinitions.map((_, index) =>
document.getElementById(`energy${index}`)
);
const lossElements = modelDefinitions.map((_, index) =>
document.getElementById(`loss${index}`)
);
const meterElements = modelDefinitions.map((_, index) =>
document.getElementById(`meter${index}`)
);
const firstElements = modelDefinitions.map((_, index) =>
document.getElementById(`first${index}`)
);
const peakElements = modelDefinitions.map((_, index) =>
document.getElementById(`peak${index}`)
);
function setStatus(message) {
statusElement.textContent = message;
}
function updateReadout(index) {
const state = states[index];
const energyPercent = Math.max(0, Math.round(state.energy * 100));
const lostPercent = 100 - energyPercent;
energyElements[index].textContent = `${energyPercent}%`;
lossElements[index].textContent = `${lostPercent}% lost`;
meterElements[index].style.width = `${energyPercent}%`;
firstElements[index].textContent =
state.firstRebound === null
? "—"
: `${Math.round(state.firstRebound * 100)}%`;
peakElements[index].textContent =
state.bestPeak <= 0
? "—"
: `${Math.round((state.bestPeak / DROP_HEIGHT) * 100)}%`;
}
function updatePeakMarker(model, state) {
if (state.bestPeak <= 0) {
model.peakLine.visible = false;
model.peakCap.visible = false;
return;
}
const peakY = SURFACE_Y + state.bestPeak;
const positions = model.peakLine.geometry.attributes.position;
positions.setXYZ(0, model.x + 1.48, SURFACE_Y, 0.88);
positions.setXYZ(1, model.x + 1.48, peakY, 0.88);
positions.needsUpdate = true;
model.peakLine.geometry.computeBoundingSphere();
model.peakLine.computeLineDistances();
model.peakLine.visible = true;
model.peakCap.position.y = peakY;
model.peakCap.visible = true;
}
function resetSimulation(showReady = true) {
running = false;
paused = false;
simulationTime = 0;
states.forEach((state, index) => {
state.y = INITIAL_Y;
state.vy = 0;
state.energy = 1;
state.settled = false;
state.impactCount = 0;
state.firstRebound = null;
state.bestPeak = 0;
state.state = "ready";
state.ball.position.set(models[index].x, INITIAL_Y, 0);
state.ball.rotation.set(0, 0, 0);
models[index].impactTime = -10;
models[index].deform.scale.set(1, 1, 1);
models[index].peakLine.visible = false;
models[index].peakCap.visible = false;
updateReadout(index);
});
particles.forEach((particle) => {
particle.mesh.visible = false;
particle.life = 0;
});
pauseButton.disabled = true;
pauseButton.textContent = "Pause";
dropButton.textContent = "▶ Drop all";
if (showReady) {
setStatus("Ready — three identical voxel balls wait at equal height.");
}
}
function startDrop() {
resetSimulation(false);
running = true;
pauseButton.disabled = false;
dropButton.textContent = "↻ Restart drops";
setStatus("Released together — watch the energy meters at each impact.");
}
function handleImpact(index) {
const model = models[index];
const state = states[index];
const retained = state.energy * model.retain;
state.energy = retained;
state.impactCount += 1;
if (state.firstRebound === null) {
state.firstRebound = state.energy;
}
const reboundHeight = state.energy * DROP_HEIGHT;
state.bestPeak = Math.max(state.bestPeak, reboundHeight);
state.state = "rebounding";
model.impactTime = simulationTime;
spawnImpactParticles(model.x, model.color);
const loss = Math.round((1 - model.retain) * 100);
setStatus(
`${model.title} impact — ${loss}% of the toy energy rule was lost.`
);
updatePeakMarker(model, state);
updateReadout(index);
if (reboundHeight < 0.025) {
state.settled = true;
state.vy = 0;
state.y = IMPACT_Y;
state.energy = 0;
state.state = "settled";
updateReadout(index);
} else {
state.vy = Math.sqrt(2 * GRAVITY * reboundHeight);
}
}
function updateBall(index, delta) {
const state = states[index];
if (state.settled) return;
state.vy -= GRAVITY * delta;
state.y += state.vy * delta;
if (state.y <= IMPACT_Y && state.vy < 0) {
state.y = IMPACT_Y;
handleImpact(index);
}
state.ball.position.y = state.y;
state.ball.rotation.x += delta * 2.1;
state.ball.rotation.z += delta * 1.45;
}
function updateSurfaceDeformations() {
models.forEach((model) => {
const age = simulationTime - model.impactTime;
if (age >= 0 && age < 0.65) {
const pulse =
Math.exp(-age * 8) * Math.sin(age * 19) *
model.impactAmplitude;
model.deform.scale.y = Math.max(0.46, 1 - pulse);
} else {
model.deform.scale.y = 1;
}
});
}
resetSimulation(true);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.58,
1.43
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
9,
23
);
updateCamera();
},
{ passive: false }
);
cameraButton.addEventListener("click", () => {
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
updateCamera();
});
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
dropButton.addEventListener("click", startDrop);
resetButton.addEventListener("click", () => {
resetSimulation(true);
});
pauseButton.addEventListener("click", () => {
if (!running) return;
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
setStatus(
paused
? "Paused — inspect the positions and energy meters."
: "Resumed."
);
});
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "BUTTON" || tag === "INPUT" || tag === "TEXTAREA") return;
if (event.code === "Space") {
event.preventDefault();
startDrop();
} else if (event.key.toLowerCase() === "r") {
resetSimulation(true);
}
});
// -------------------------------------------------------------------------
// Responsive renderer
// -------------------------------------------------------------------------
function resizeRenderer() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / Math.max(height, 1);
const viewHeight = 8.7;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(stage);
resizeRenderer();
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.035);
if (running && !paused) {
simulationTime += delta;
let everyBallSettled = true;
states.forEach((state, index) => {
if (!state.settled) {
everyBallSettled = false;
updateBall(index, delta);
}
});
if (everyBallSettled) {
running = false;
pauseButton.disabled = true;
pauseButton.textContent = "Pause";
setStatus("All three balls have settled. Reset or drop again to compare.");
}
}
updateSurfaceDeformations();
updateParticles(running && !paused ? delta : 0);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```