Prompt specification
Build or customize the Three.js 3D scene "Ball and Rebound Height Markers" 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: Ball and Rebound Height Markers
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ball-and-rebound-height-markers-x3601
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Place a ball above a floor beside a vertical scale and expected rebound marker. Show initial height separately from the selected idealized restitution parameter.
Working Three.js Starter (ball-and-rebound-height-markers-x3601):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Rebound Studio</title>
<style>
:root {
color-scheme: dark;
--bg: #081321;
--panel: rgba(12, 25, 41, 0.88);
--line: rgba(174, 210, 232, 0.16);
--text: #edf6fb;
--muted: #90a8ba;
--initial: #ffb45e;
--rebound: #53dfd1;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.brand {
position: fixed;
top: 24px;
left: 28px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.5);
}
.brand small {
display: block;
color: var(--rebound);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.brand h1 {
margin: 7px 0 0;
font-size: clamp(24px, 3vw, 40px);
font-weight: 650;
letter-spacing: -0.04em;
}
.status {
position: fixed;
top: 29px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
padding: 8px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 19, 33, 0.64);
backdrop-filter: blur(12px);
color: #c6d7e4;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
pointer-events: none;
}
.status::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--rebound);
box-shadow: 0 0 12px var(--rebound);
}
.panel {
position: fixed;
top: 24px;
right: 24px;
width: min(340px, calc(100vw - 48px));
max-height: calc(100vh - 48px);
overflow: auto;
padding: 20px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(20px) saturate(1.25);
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 18px;
}
.panel-heading h2 {
margin: 0 0 5px;
font-size: 18px;
font-weight: 650;
letter-spacing: -0.02em;
}
.panel-heading p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.tag {
flex: none;
padding: 6px 8px;
border: 1px solid rgba(83, 223, 209, 0.28);
border-radius: 8px;
color: var(--rebound);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
}
.control {
padding: 14px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.025);
}
.control + .control { margin-top: 10px; }
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.control-head label {
color: #d9e7f0;
font-size: 13px;
font-weight: 600;
}
.value-box {
display: flex;
align-items: baseline;
gap: 5px;
color: var(--muted);
font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.value-box input {
width: 66px;
padding: 5px 6px;
border: 1px solid var(--line);
border-radius: 7px;
outline: none;
background: rgba(5, 13, 24, 0.65);
color: var(--text);
text-align: right;
font: 700 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.value-box input:focus {
border-color: rgba(83, 223, 209, 0.7);
}
.value-box span { min-width: 12px; }
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--initial) 0%,
var(--initial) var(--fill, 50%),
rgba(255, 255, 255, 0.12) var(--fill, 50%),
rgba(255, 255, 255, 0.12) 100%
);
cursor: pointer;
}
.control.restitution input[type="range"] {
background: linear-gradient(
90deg,
var(--rebound) 0%,
var(--rebound) var(--fill, 50%),
rgba(255, 255, 255, 0.12) var(--fill, 50%),
rgba(255, 255, 255, 0.12) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #0d1b2b;
border-radius: 50%;
appearance: none;
background: #edf6fb;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #0d1b2b;
border-radius: 50%;
background: #edf6fb;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #718a9d;
font: 500 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.derived {
margin-top: 14px;
padding: 17px;
border: 1px solid rgba(83, 223, 209, 0.25);
border-radius: 15px;
background: linear-gradient(135deg, rgba(83, 223, 209, 0.1), rgba(83, 223, 209, 0.025));
}
.derived-label {
display: flex;
align-items: center;
justify-content: space-between;
color: #a9c0ce;
font-size: 12px;
}
.derived-label span:last-child {
color: var(--rebound);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.derived-value {
margin-top: 8px;
color: var(--rebound);
font: 700 34px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.06em;
text-shadow: 0 0 24px rgba(83, 223, 209, 0.2);
}
.formula {
margin-top: 10px;
color: #9fb5c4;
font: 500 11px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.formula b { color: #e7f2f7; }
.legend {
display: grid;
gap: 8px;
margin: 15px 0;
color: var(--muted);
font-size: 11px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 24px;
height: 3px;
border-radius: 9px;
background: var(--initial);
box-shadow: 0 0 8px rgba(255, 180, 94, 0.4);
}
.swatch.rebound {
background: var(--rebound);
box-shadow: 0 0 8px rgba(83, 223, 209, 0.4);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
button {
min-height: 42px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
cursor: pointer;
transition: background 150ms, border-color 150ms, transform 150ms;
}
button:hover {
border-color: rgba(255, 255, 255, 0.28);
background: rgba(255, 255, 255, 0.1);
}
button:active { transform: translateY(1px); }
button.primary {
border-color: rgba(83, 223, 209, 0.35);
background: rgba(83, 223, 209, 0.14);
color: #a8fff5;
}
.note {
margin: 13px 0 0;
color: #718a9d;
font-size: 10px;
line-height: 1.55;
}
.camera-help {
position: fixed;
right: 25px;
bottom: 22px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(8, 19, 33, 0.62);
color: #8299aa;
font: 500 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
@media (max-width: 720px) {
.brand { top: 16px; left: 18px; }
.brand h1 { font-size: 24px; }
.status { top: 75px; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 48vh;
padding: 15px;
}
.camera-help { display: none; }
.legend, .note { display: none; }
}
</style>
</head>
<body>
<header class="brand">
<small>Idealized motion study</small>
<h1>Rebound Studio</h1>
</header>
<div class="status" id="status">HELD AT INITIAL HEIGHT</div>
<aside class="panel">
<div class="panel-heading">
<div>
<h2>Model controls</h2>
<p>Initial drop height and idealized restitution are selected independently.</p>
</div>
<span class="tag">v1.0</span>
</div>
<section class="control">
<div class="control-head">
<label for="heightRange">Initial height h₀</label>
<div class="value-box">
<input id="heightNumber" type="number" min="1" max="5.5" step="0.1" value="4.0" />
<span>m</span>
</div>
</div>
<input id="heightRange" type="range" min="1" max="5.5" step="0.05" value="4" />
<div class="range-ends"><span>1.0 m</span><span>5.5 m</span></div>
</section>
<section class="control restitution">
<div class="control-head">
<label for="restitutionRange">Selected restitution e</label>
<div class="value-box">
<input id="restitutionNumber" type="number" min="0.2" max="0.95" step="0.01" value="0.68" />
<span>—</span>
</div>
</div>
<input id="restitutionRange" type="range" min="0.2" max="0.95" step="0.01" value="0.68" />
<div class="range-ends"><span>0.20</span><span>0.95</span></div>
</section>
<section class="derived">
<div class="derived-label">
<span>Expected first rebound</span>
<span>IDEALIZED</span>
</div>
<div class="derived-value" id="expectedValue">1.85 m</div>
<div class="formula">
h₁ = e² · h₀ = <b id="formulaValue">0.4624 × 4.00 = 1.85 m</b>
</div>
</section>
<div class="legend">
<div class="legend-row"><span class="swatch"></span><span>Initial height and fall guide</span></div>
<div class="legend-row"><span class="swatch rebound"></span><span>Predicted first-rebound apex</span></div>
</div>
<div class="actions">
<button id="replayButton" class="primary" type="button">REPLAY DROP</button>
<button id="pauseButton" type="button">PAUSE</button>
</div>
<p class="note">
The model uses gravity, a rigid floor, and the selected coefficient of restitution.
Air drag, deformation, and spin are omitted.
</p>
</aside>
<div class="camera-help">DRAG TO ORBIT · SCROLL TO ZOOM</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081321);
scene.fog = new THREE.Fog(0x081321, 13, 29);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xc6eaff, 0x182235, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff0dc, 3.5);
keyLight.position.set(5.5, 10, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x49d9d0, 22, 14, 2);
rimLight.position.set(-3.5, 5.2, 4.5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f50, 12, 9, 2);
warmLight.position.set(4, 2.5, 1.5);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0x122237,
roughness: 0.86,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.012;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x426176, 0x24394d);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.46;
scene.add(grid);
const scaleGroup = new THREE.Group();
scene.add(scaleGroup);
const scalePost = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 6.15, 0.2),
new THREE.MeshStandardMaterial({
color: 0xd8e5e8,
roughness: 0.48,
metalness: 0.18
})
);
scalePost.position.set(-3.22, 3.075, 0);
scalePost.castShadow = true;
scalePost.receiveShadow = true;
scaleGroup.add(scalePost);
const scaleBase = new THREE.Mesh(
new THREE.BoxGeometry(0.82, 0.14, 0.72),
new THREE.MeshStandardMaterial({
color: 0x283d50,
roughness: 0.58,
metalness: 0.25
})
);
scaleBase.position.set(-3.22, 0.07, 0);
scaleBase.castShadow = true;
scaleBase.receiveShadow = true;
scaleGroup.add(scaleBase);
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextSprite(text, color = "#eef8fc", background = null, worldWidth = 2.15) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 160;
const ctx = canvas.getContext("2d");
if (background) {
roundedRect(ctx, 5, 8, 630, 144, 25);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.13)";
ctx.lineWidth = 3;
ctx.stroke();
}
ctx.font = `700 ${text.length > 25 ? 30 : text.length > 18 ? 34 : 40}px ui-monospace, SFMono-Regular, Menlo, monospace`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(text, 320, 82);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(worldWidth, worldWidth * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
for (let meter = 0; meter <= 6; meter++) {
const major = new THREE.Mesh(
new THREE.BoxGeometry(0.37, 0.025, 0.075),
new THREE.MeshStandardMaterial({ color: 0x385166, roughness: 0.5 })
);
major.position.set(-2.95, meter, 0.115);
scaleGroup.add(major);
const label = makeTextSprite(String(meter), "#dbeaf0", null, 0.48);
label.position.set(-3.47, meter, 0.15);
scaleGroup.add(label);
if (meter < 6) {
const half = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.018, 0.065),
new THREE.MeshStandardMaterial({ color: 0x516b7e, roughness: 0.5 })
);
half.position.set(-3.045, meter + 0.5, 0.11);
scaleGroup.add(half);
}
}
const metersLabel = makeTextSprite("METERS", "#77dcd4", "rgba(8,19,33,0.86)", 1.15);
metersLabel.position.set(-3.22, 6.33, 0.1);
scaleGroup.add(metersLabel);
const BALL_X = 0.72;
const BALL_RADIUS = 0.34;
const ball = new THREE.Group();
scene.add(ball);
const ballBody = new THREE.Mesh(
new THREE.IcosahedronGeometry(BALL_RADIUS, 4),
new THREE.MeshStandardMaterial({
color: 0xff873f,
emissive: 0x351105,
emissiveIntensity: 0.18,
roughness: 0.48,
metalness: 0.04,
flatShading: true
})
);
ballBody.castShadow = true;
ballBody.receiveShadow = true;
ball.add(ballBody);
const seamMaterial = new THREE.MeshStandardMaterial({
color: 0x522415,
roughness: 0.55,
metalness: 0.05
});
const seamA = new THREE.Mesh(
new THREE.TorusGeometry(BALL_RADIUS + 0.002, 0.012, 8, 72),
seamMaterial
);
ball.add(seamA);
const seamB = new THREE.Mesh(
new THREE.TorusGeometry(BALL_RADIUS + 0.002, 0.012, 8, 72),
seamMaterial
);
seamB.rotation.y = Math.PI / 2;
ball.add(seamB);
const seamC = new THREE.Mesh(
new THREE.TorusGeometry(BALL_RADIUS + 0.002, 0.01, 8, 72),
seamMaterial
);
seamC.rotation.x = Math.PI / 2;
ball.add(seamC);
const groundGlow = new THREE.Mesh(
new THREE.RingGeometry(0.42, 0.7, 64),
new THREE.MeshBasicMaterial({
color: 0x53dfd1,
transparent: true,
opacity: 0.11,
side: THREE.DoubleSide,
depthWrite: false
})
);
groundGlow.rotation.x = -Math.PI / 2;
groundGlow.position.set(BALL_X, 0.012, 0);
scene.add(groundGlow);
const shadowCanvas = document.createElement("canvas");
shadowCanvas.width = shadowCanvas.height = 128;
const shadowContext = shadowCanvas.getContext("2d");
const gradient = shadowContext.createRadialGradient(64, 64, 3, 64, 64, 62);
gradient.addColorStop(0, "rgba(0,0,0,0.55)");
gradient.addColorStop(0.5, "rgba(0,0,0,0.23)");
gradient.addColorStop(1, "rgba(0,0,0,0)");
shadowContext.fillStyle = gradient;
shadowContext.fillRect(0, 0, 128, 128);
const shadowTexture = new THREE.CanvasTexture(shadowCanvas);
const blobShadow = new THREE.Mesh(
new THREE.PlaneGeometry(1.35, 1.35),
new THREE.MeshBasicMaterial({
map: shadowTexture,
transparent: true,
depthWrite: false,
opacity: 0.58
})
);
blobShadow.rotation.x = -Math.PI / 2;
blobShadow.position.set(BALL_X, 0.015, 0);
scene.add(blobShadow);
const INITIAL_COLOR = 0xffb45e;
const REBOUND_COLOR = 0x53dfd1;
const initialLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: INITIAL_COLOR,
dashSize: 0.14,
gapSize: 0.09,
transparent: true,
opacity: 0.9
})
);
scene.add(initialLine);
const expectedLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: REBOUND_COLOR,
dashSize: 0.14,
gapSize: 0.09,
transparent: true,
opacity: 0.95
})
);
scene.add(expectedLine);
const fallGuide = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: INITIAL_COLOR,
dashSize: 0.09,
gapSize: 0.08,
transparent: true,
opacity: 0.36
})
);
scene.add(fallGuide);
const reboundArc = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: REBOUND_COLOR,
dashSize: 0.11,
gapSize: 0.07,
transparent: true,
opacity: 0.7
})
);
scene.add(reboundArc);
let expectedRing = new THREE.Mesh(
new THREE.TorusGeometry(0.47, 0.023, 10, 72),
new THREE.MeshBasicMaterial({
color: REBOUND_COLOR,
transparent: true,
opacity: 0.9
})
);
expectedRing.rotation.x = Math.PI / 2;
scene.add(expectedRing);
let initialLabel = null;
let expectedLabel = null;
const params = {
height: 4,
restitution: 0.68
};
const gravity = 9.81;
const simulation = {
y: params.height,
velocity: 0,
hold: 1.1,
settled: false,
settledTime: 0,
impactPulse: 0
};
let running = true;
function replaceLine(line, points) {
line.geometry.dispose();
line.geometry = new THREE.BufferGeometry().setFromPoints(points);
line.computeLineDistances();
}
function removeLabel(label) {
if (!label) return;
scene.remove(label);
label.material.map?.dispose();
label.material.dispose();
}
function expectedReboundHeight() {
return params.height * params.restitution * params.restitution;
}
function updateModel() {
const rebound = expectedReboundHeight();
document.getElementById("expectedValue").textContent = `${rebound.toFixed(2)} m`;
document.getElementById("formulaValue").textContent =
`${(params.restitution * params.restitution).toFixed(4)} × ${params.height.toFixed(2)} = ${rebound.toFixed(2)} m`;
replaceLine(initialLine, [
new THREE.Vector3(-2.89, params.height, 0.12),
new THREE.Vector3(2.18, params.height, 0.12)
]);
replaceLine(expectedLine, [
new THREE.Vector3(-2.89, rebound, 0.14),
new THREE.Vector3(2.18, rebound, 0.14)
]);
replaceLine(fallGuide, [
new THREE.Vector3(BALL_X, 0, -0.3),
new THREE.Vector3(BALL_X, params.height, -0.3)
]);
const arcPoints = [];
for (let i = 0; i <= 60; i++) {
const u = i / 60;
arcPoints.push(
new THREE.Vector3(
BALL_X,
4 * rebound * u * (1 - u),
0.29
)
);
}
replaceLine(reboundArc, arcPoints);
expectedRing.position.set(BALL_X, rebound, 0);
removeLabel(initialLabel);
removeLabel(expectedLabel);
initialLabel = makeTextSprite(
`INITIAL h₀ ${params.height.toFixed(2)} m`,
"#ffd29d",
"rgba(42,27,17,0.88)",
2.15
);
initialLabel.position.set(-1.62, params.height + 0.21, 0.18);
scene.add(initialLabel);
expectedLabel = makeTextSprite(
`REBOUND h₁ ${rebound.toFixed(2)} m`,
"#a8fff5",
"rgba(8,38,40,0.9)",
2.25
);
expectedLabel.position.set(-1.55, rebound + 0.21, 0.2);
scene.add(expectedLabel);
}
function resetSimulation(delay = 0.85) {
simulation.y = params.height;
simulation.velocity = 0;
simulation.hold = delay;
simulation.settled = false;
simulation.settledTime = 0;
simulation.impactPulse = 0;
}
function advanceBall(dt) {
let remaining = dt;
while (remaining > 0.000001 && !simulation.settled) {
if (simulation.velocity <= 0) {
const y = Math.max(0, simulation.y);
const v = simulation.velocity;
const discriminant = v * v + 2 * gravity * y;
const timeToFloor = (v + Math.sqrt(discriminant)) / gravity;
if (timeToFloor <= remaining + 0.000001) {
simulation.y = Math.max(
0,
y + v * timeToFloor - 0.5 * gravity * timeToFloor * timeToFloor
);
remaining -= timeToFloor;
const impactSpeed = Math.max(0, -(v - gravity * timeToFloor));
simulation.y = 0;
if (impactSpeed < 0.38) {
simulation.velocity = 0;
simulation.settled = true;
simulation.settledTime = 0;
} else {
simulation.velocity = params.restitution * impactSpeed;
simulation.impactPulse = 1;
}
continue;
}
}
simulation.y +=
simulation.velocity * remaining -
0.5 * gravity * remaining * remaining;
simulation.velocity -= gravity * remaining;
remaining = 0;
}
}
function updateSimulation(dt) {
if (running) {
if (simulation.hold > 0) {
simulation.hold = Math.max(0, simulation.hold - dt);
} else if (simulation.settled) {
simulation.settledTime += dt;
if (simulation.settledTime > 1.35) {
resetSimulation(0.35);
}
} else {
advanceBall(dt);
}
}
simulation.impactPulse = Math.max(
0,
simulation.impactPulse - dt * 4.5
);
const squash = simulation.impactPulse * 0.13;
ball.position.set(BALL_X, simulation.y + BALL_RADIUS, 0);
ball.scale.set(1 + squash * 0.48, 1 - squash, 1 + squash * 0.48);
const heightFactor = THREE.MathUtils.clamp(
1 - simulation.y / (params.height + 0.001),
0,
1
);
blobShadow.material.opacity = 0.58 * (1 - heightFactor * 0.64);
blobShadow.scale.setScalar(1 - heightFactor * 0.3);
groundGlow.material.opacity = 0.09 + simulation.impactPulse * 0.28;
const status = document.getElementById("status");
if (!running) {
status.textContent = "PAUSED";
} else if (simulation.hold > 0) {
status.textContent = "HELD AT INITIAL HEIGHT";
} else if (simulation.settled) {
status.textContent = "AT REST";
} else if (simulation.velocity < 0) {
status.textContent = "FALLING";
} else {
status.textContent = "REBOUNDING";
}
}
const heightRange = document.getElementById("heightRange");
const heightNumber = document.getElementById("heightNumber");
const restitutionRange = document.getElementById("restitutionRange");
const restitutionNumber = document.getElementById("restitutionNumber");
function updateRangeFill(input) {
const percent =
100 * (Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min));
input.style.setProperty("--fill", `${percent}%`);
}
function setHeight(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) return;
params.height = THREE.MathUtils.clamp(parsed, 1, 5.5);
heightRange.value = params.height;
heightNumber.value = params.height.toFixed(1);
updateRangeFill(heightRange);
updateModel();
resetSimulation();
}
function setRestitution(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) return;
params.restitution = THREE.MathUtils.clamp(parsed, 0.2, 0.95);
restitutionRange.value = params.restitution;
restitutionNumber.value = params.restitution.toFixed(2);
updateRangeFill(restitutionRange);
updateModel();
resetSimulation();
}
heightRange.addEventListener("input", () => setHeight(heightRange.value));
heightNumber.addEventListener("change", () => setHeight(heightNumber.value));
restitutionRange.addEventListener("input", () => setRestitution(restitutionRange.value));
restitutionNumber.addEventListener("change", () => setRestitution(restitutionNumber.value));
document.getElementById("replayButton").addEventListener("click", () => {
running = true;
document.getElementById("pauseButton").textContent = "PAUSE";
resetSimulation(0.45);
});
document.getElementById("pauseButton").addEventListener("click", (event) => {
running = !running;
event.currentTarget.textContent = running ? "PAUSE" : "RESUME";
});
// Camera orbit controls implemented directly, without external addons.
const target = new THREE.Vector3(-0.05, 2.55, 0);
let cameraTheta = 0.48;
let cameraPhi = 1.13;
let cameraRadius = 11.4;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.48,
1.47
);
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
7.2,
18
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateRangeFill(heightRange);
updateRangeFill(restitutionRange);
updateModel();
resetSimulation();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateSimulation(dt);
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```