Prompt specification
Build or customize the Three.js 3D scene "Slope material tray 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: Slope material tray comparison
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/slope-material-tray-comparison-x1158
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing landslide slopes. Place rough blocks on two adjustable ramps and show their motion under stated simplified friction assumptions; use the scene as an analogy rather than a soil stability calculation. Stylized solid forms.
Working Three.js Starter (slope-material-tray-comparison-x1158):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Landslide Slope Analogy</title>
<style>
:root {
color-scheme: light;
--ink: #253238;
--muted: #647176;
--paper: rgba(250, 248, 241, 0.94);
--line: rgba(37, 50, 56, 0.14);
--a: #167c80;
--b: #d2673f;
--shadow: 0 18px 50px rgba(30, 38, 40, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e2d5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(340px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.lede {
margin: 9px 0 14px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.warning {
margin: 12px 0;
padding: 10px 11px;
border-radius: 11px;
background: #efe2c7;
color: #604b24;
font-size: 11px;
font-weight: 750;
line-height: 1.35;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.assumptions {
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.45);
}
.assumptions strong {
display: block;
margin-bottom: 6px;
font-size: 12px;
}
.assumptions ul {
margin: 0;
padding-left: 17px;
color: #526066;
font-size: 11.5px;
line-height: 1.45;
}
.controls {
margin-top: 13px;
}
.ramp-card {
margin-top: 10px;
padding: 11px 12px;
border: 1px solid var(--line);
border-left-width: 4px;
border-radius: 12px;
background: rgba(255, 255, 255, 0.52);
}
.ramp-card.a {
border-left-color: var(--a);
}
.ramp-card.b {
border-left-color: var(--b);
}
.ramp-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.ramp-heading strong {
font-size: 13px;
}
.ramp-heading span {
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.control-row {
display: grid;
grid-template-columns: 62px 1fr 48px;
align-items: center;
gap: 8px;
min-height: 29px;
font-size: 11px;
}
.control-row label {
color: #526066;
}
.control-row output {
text-align: right;
font-variant-numeric: tabular-nums;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
accent-color: var(--a);
cursor: pointer;
}
.ramp-card.b input[type="range"] {
accent-color: var(--b);
}
.status-box {
margin-top: 9px;
padding-top: 8px;
border-top: 1px solid var(--line);
}
.relation {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.status {
display: block;
margin-top: 3px;
font-size: 12px;
font-weight: 750;
}
.status.slide {
color: #b54e2a;
}
.status.hold {
color: #5b6d24;
}
.status.fall {
color: #456d9a;
}
.status.done {
color: #46585e;
}
.buttons {
display: grid;
grid-template-columns: 1.2fr 0.8fr;
gap: 8px;
margin-top: 13px;
}
button {
min-height: 38px;
padding: 8px 10px;
border: 1px solid rgba(37, 50, 56, 0.18);
border-radius: 10px;
background: #fff;
color: var(--ink);
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #f2f0e9;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
grid-column: 1 / -1;
border-color: transparent;
background: var(--ink);
color: #fff;
}
button.primary:hover {
background: #35464c;
}
#badge {
position: fixed;
z-index: 8;
top: 18px;
right: 18px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 999px;
background: rgba(250, 248, 241, 0.9);
box-shadow: 0 8px 24px rgba(30, 38, 40, 0.1);
color: #5d4a31;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
#hint {
position: fixed;
z-index: 8;
right: 18px;
bottom: 16px;
padding: 8px 11px;
border-radius: 999px;
background: rgba(37, 50, 56, 0.78);
color: rgba(255, 255, 255, 0.9);
font-size: 10px;
pointer-events: none;
}
.footer-note {
margin: 11px 2px 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.4;
}
@media (max-width: 700px) {
#panel {
top: auto;
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
max-height: 46vh;
padding: 14px;
}
#badge {
top: 12px;
right: 12px;
max-width: 48vw;
line-height: 1.25;
}
#hint {
display: none;
}
h1 {
font-size: 19px;
}
.assumptions {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D comparison of two blocks on inclined ramps"></div>
<aside id="panel">
<h1>Landslide slope analogy</h1>
<p class="lede">
Adjust each incline and surface friction, then run the same rigid-block
experiment twice.
</p>
<div class="warning">
Visual analogy only — not a soil stability or landslide prediction
</div>
<section class="assumptions">
<strong>Simplified friction model</strong>
<ul>
<li>Each block is rigid and stays aligned with the ramp; no tipping or deformation.</li>
<li>Normal force: N = mg cos θ.</li>
<li>One coefficient μ is used for both static and kinetic friction.</li>
<li>If μ ≥ tan θ, the block remains at rest.</li>
<li>If μ < tan θ, acceleration is g(sin θ − μ cos θ).</li>
<li>After leaving a ramp, motion switches to a simple gravity-only fall.</li>
</ul>
</section>
<div class="controls">
<section class="ramp-card a">
<div class="ramp-heading">
<strong>Ramp A</strong>
<span>teal block</span>
</div>
<div class="control-row">
<label for="angleA">Angle θ</label>
<input id="angleA" type="range" min="8" max="42" step="1" value="32">
<output id="angleOutA">32°</output>
</div>
<div class="control-row">
<label for="muA">Friction μ</label>
<input id="muA" type="range" min="0.05" max="0.80" step="0.01" value="0.42">
<output id="muOutA">0.42</output>
</div>
<div class="status-box">
<span class="relation" id="relationA"></span>
<span class="status" id="statusA"></span>
</div>
</section>
<section class="ramp-card b">
<div class="ramp-heading">
<strong>Ramp B</strong>
<span>orange block</span>
</div>
<div class="control-row">
<label for="angleB">Angle θ</label>
<input id="angleB" type="range" min="8" max="42" step="1" value="32">
<output id="angleOutB">32°</output>
</div>
<div class="control-row">
<label for="muB">Friction μ</label>
<input id="muB" type="range" min="0.05" max="0.80" step="0.01" value="0.25">
<output id="muOutB">0.25</output>
</div>
<div class="status-box">
<span class="relation" id="relationB"></span>
<span class="status" id="statusB"></span>
</div>
</section>
</div>
<div class="buttons">
<button class="primary" id="run">Run comparison</button>
<button id="pause">Pause</button>
<button id="reset">Reset blocks</button>
</div>
<p class="footer-note">
The model contains no pore-water pressure, soil strength, deformation,
runout geometry, vegetation, or scale effects. Block mass cancels from
the simplified acceleration formula.
</p>
</aside>
<div id="badge">Analogy only · not geotechnical analysis</div>
<div id="hint">Drag to orbit · wheel to zoom · double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const G = 9.81;
const GROUND_Y = -0.85;
const RAMP_LENGTH = 5.6;
const RAMP_DEPTH = 1.8;
const RAMP_THICKNESS = 0.3;
const SURFACE_HEIGHT = 0.05;
const BLOCK_WIDTH = 1.35;
const BLOCK_HEIGHT = 0.92;
const BLOCK_DEPTH = 1.15;
const START_DISTANCE = 0.65;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e2d5);
scene.fog = new THREE.Fog(0xe8e2d5, 19, 42);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional scene with two adjustable ramps and moving blocks"
);
$("scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff8e8, 0x6f746d, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d5, 3.1);
sun.position.set(-5, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d7df, 0.75);
fill.position.set(8, 4, -8);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xcac4b6,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = GROUND_Y;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x8d938f, 0x9ca19b);
grid.position.y = GROUND_Y + 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
function createLabel(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(64, 64, 49, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.88)";
ctx.lineWidth = 5;
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.font = "800 64px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 64, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.78, 0.78, 0.78);
sprite.renderOrder = 20;
return sprite;
}
function createRoughBlock(color) {
const w = BLOCK_WIDTH;
const h = BLOCK_HEIGHT;
const d = BLOCK_DEPTH;
// A deliberately irregular, flat-bottomed faceted block.
const vertices = [
[-w / 2, -h / 2, -d / 2],
[ w / 2, -h / 2, -d / 2],
[ w / 2, -h / 2, d / 2],
[-w / 2, -h / 2, d / 2],
[-w * 0.44, h / 2, -d * 0.46],
[ w * 0.48, h / 2 + 0.06, -d * 0.43],
[ w * 0.46, h / 2 - 0.04, d * 0.47],
[-w * 0.47, h / 2 + 0.03, d * 0.44]
];
const triangles = [
0, 2, 1, 0, 3, 2,
4, 6, 5, 4, 7, 6,
0, 4, 5, 0, 5, 1,
3, 2, 6, 3, 6, 7,
0, 3, 7, 0, 7, 4,
1, 6, 5, 1, 2, 6
];
const positions = [];
for (const index of triangles) {
positions.push(...vertices[index]);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
metalness: 0.02,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 24),
new THREE.LineBasicMaterial({
color: 0x26383c,
transparent: true,
opacity: 0.35
})
);
mesh.add(edges);
return mesh;
}
function makeRamp({
pivot,
angleDegrees,
mu,
color,
cssColor,
label,
ids
}) {
const group = new THREE.Group();
group.position.copy(pivot);
scene.add(group);
const body = new THREE.Mesh(
new THREE.BoxGeometry(
RAMP_LENGTH,
RAMP_THICKNESS,
RAMP_DEPTH
),
new THREE.MeshStandardMaterial({
color: 0x59666b,
roughness: 0.82,
metalness: 0.06
})
);
body.position.set(RAMP_LENGTH / 2, -RAMP_THICKNESS / 2, 0);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(
RAMP_LENGTH,
SURFACE_HEIGHT,
RAMP_DEPTH - 0.08
),
new THREE.MeshStandardMaterial({
color: 0x889396,
roughness: 0.9
})
);
cap.position.set(
RAMP_LENGTH / 2,
SURFACE_HEIGHT / 2,
0
);
cap.castShadow = true;
cap.receiveShadow = true;
group.add(cap);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(RAMP_LENGTH, 0.075, 0.055),
new THREE.MeshStandardMaterial({
color,
roughness: 0.75
})
);
accent.position.set(
RAMP_LENGTH / 2,
SURFACE_HEIGHT + 0.025,
RAMP_DEPTH / 2 - 0.015
);
group.add(accent);
const grooveGeometry = new THREE.BoxGeometry(
0.035,
0.018,
RAMP_DEPTH - 0.18
);
const grooveMaterial = new THREE.MeshStandardMaterial({
color: 0x687477,
roughness: 1
});
for (let i = 1; i <= 9; i++) {
const groove = new THREE.Mesh(grooveGeometry, grooveMaterial);
groove.position.set(
(i / 10) * RAMP_LENGTH,
SURFACE_HEIGHT + 0.006,
0
);
group.add(groove);
}
const topColumn = new THREE.Mesh(
new THREE.BoxGeometry(0.3, 1, 0.34),
new THREE.MeshStandardMaterial({
color: 0x465258,
roughness: 0.8
})
);
const topColumnHeight = pivot.y - GROUND_Y;
topColumn.scale.y = topColumnHeight;
topColumn.position.set(
pivot.x,
GROUND_Y + topColumnHeight / 2,
pivot.z
);
topColumn.castShadow = true;
scene.add(topColumn);
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.24, 0.62, 20),
new THREE.MeshStandardMaterial({
color,
roughness: 0.58,
metalness: 0.2
})
);
hinge.position.copy(pivot);
hinge.castShadow = true;
scene.add(hinge);
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 1, 0.24),
new THREE.MeshStandardMaterial({
color: 0x4b585d,
roughness: 0.84
})
);
support.castShadow = true;
scene.add(support);
const supportBase = new THREE.Mesh(
new THREE.BoxGeometry(0.7, 0.1, 0.62),
new THREE.MeshStandardMaterial({
color: 0x566165,
roughness: 0.92
})
);
supportBase.castShadow = true;
supportBase.receiveShadow = true;
scene.add(supportBase);
// Angle guide around the fixed hinge.
const arcPoints = [];
const arcRadius = 1.05;
const arcZ = pivot.z + RAMP_DEPTH / 2 + 0.12;
for (let degrees = 0; degrees <= 42; degrees++) {
const a = THREE.MathUtils.degToRad(degrees);
arcPoints.push(
new THREE.Vector3(
pivot.x + Math.cos(a) * arcRadius,
pivot.y - Math.sin(a) * arcRadius,
arcZ
)
);
}
const arc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(arcPoints),
new THREE.LineBasicMaterial({
color: 0x6c7779,
transparent: true,
opacity: 0.34
})
);
scene.add(arc);
const currentLine = new THREE.Line(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(6), 3)
),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.8
})
);
scene.add(currentLine);
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 16, 10),
new THREE.MeshStandardMaterial({
color,
roughness: 0.55
})
);
scene.add(marker);
const sceneLabel = createLabel(label, cssColor);
sceneLabel.position.set(
pivot.x,
pivot.y + 0.72,
pivot.z + RAMP_DEPTH / 2 + 0.15
);
scene.add(sceneLabel);
const block = createRoughBlock(color);
scene.add(block);
return {
pivot,
angle: THREE.MathUtils.degToRad(angleDegrees),
mu,
color,
group,
block,
support,
supportBase,
marker,
currentLine,
arcZ,
s: START_DISTANCE,
v: 0,
phase: "ready",
released: false,
travelTime: 0,
flightTime: 0,
fallVelocity: new THREE.Vector3(),
ids
};
}
const rigs = [
makeRamp({
pivot: new THREE.Vector3(-5.6, 3.75, -0.9),
angleDegrees: 32,
mu: 0.42,
color: 0x167c80,
cssColor: "#167c80",
label: "A",
ids: {
angle: "angleA",
mu: "muA",
angleOut: "angleOutA",
muOut: "muOutA",
relation: "relationA",
status: "statusA"
}
}),
makeRamp({
pivot: new THREE.Vector3(0.3, 3.75, 0.9),
angleDegrees: 32,
mu: 0.25,
color: 0xd2673f,
cssColor: "#d2673f",
label: "B",
ids: {
angle: "angleB",
mu: "muB",
angleOut: "angleOutB",
muOut: "muOutB",
relation: "relationB",
status: "statusB"
}
})
];
function rampPoint(rig, distance, target = new THREE.Vector3()) {
const c = Math.cos(rig.angle);
const s = Math.sin(rig.angle);
return target.set(
rig.pivot.x + distance * c,
rig.pivot.y - distance * s,
rig.pivot.z
);
}
function rampNormal(rig, target = new THREE.Vector3()) {
return target.set(
Math.sin(rig.angle),
Math.cos(rig.angle),
0
);
}
function rampTangent(rig, target = new THREE.Vector3()) {
return target.set(
Math.cos(rig.angle),
-Math.sin(rig.angle),
0
);
}
function placeBlockOnRamp(rig) {
const point = rampPoint(rig, rig.s);
const normal = rampNormal(rig);
rig.block.position.copy(point).addScaledVector(
normal,
BLOCK_HEIGHT / 2 + SURFACE_HEIGHT + 0.012
);
rig.block.rotation.set(0, 0, -rig.angle);
}
function updateRampVisual(rig) {
rig.group.rotation.z = -rig.angle;
const supportDistance = 3.85;
const surface = rampPoint(rig, supportDistance);
const undersideY =
surface.y -
Math.cos(rig.angle) * RAMP_THICKNESS;
const supportHeight = Math.max(0.1, undersideY - GROUND_Y);
rig.support.scale.y = supportHeight;
rig.support.position.set(
surface.x,
GROUND_Y + supportHeight / 2,
rig.pivot.z
);
rig.supportBase.position.set(
surface.x,
GROUND_Y + 0.05,
rig.pivot.z
);
const a = rig.angle;
const markerX =
rig.pivot.x + Math.cos(a) * 1.05;
const markerY =
rig.pivot.y - Math.sin(a) * 1.05;
rig.marker.position.set(markerX, markerY, rig.arcZ);
const positions = rig.currentLine.geometry.attributes.position;
positions.setXYZ(
0,
rig.pivot.x,
rig.pivot.y,
rig.arcZ
);
positions.setXYZ(1, markerX, markerY, rig.arcZ);
positions.needsUpdate = true;
rig.currentLine.geometry.computeBoundingSphere();
}
function accelerationFor(rig) {
const alongGravity = G * Math.sin(rig.angle);
const friction = rig.mu * G * Math.cos(rig.angle);
return Math.max(0, alongGravity - friction);
}
function wouldSlide(rig) {
return Math.tan(rig.angle) > rig.mu;
}
function refreshStatus(rig) {
const relation = `tan θ ${Math.tan(rig.angle).toFixed(3)} · μ ${rig.mu.toFixed(2)}`;
$(rig.ids.relation).textContent = relation;
let text;
let mode;
if (rig.phase === "ready") {
text = wouldSlide(rig)
? `Ready · would slide at ${accelerationFor(rig).toFixed(2)} m/s²`
: "Ready · friction would hold the block";
mode = wouldSlide(rig) ? "slide" : "hold";
} else if (rig.phase === "static") {
text = "Held at the start · μ ≥ tan θ";
mode = "hold";
} else if (rig.phase === "sliding") {
text = `Sliding · ${accelerationFor(rig).toFixed(2)} m/s²`;
mode = "slide";
} else if (rig.phase === "falling") {
text = "Airborne · gravity-only fall";
mode = "fall";
} else {
const total = rig.travelTime + rig.flightTime;
text = `Landed · total time ${total.toFixed(2)} s`;
mode = "done";
}
$(rig.ids.status).textContent = text;
$(rig.ids.status).className = `status ${mode}`;
}
function resetRig(rig) {
rig.s = START_DISTANCE;
rig.v = 0;
rig.phase = "ready";
rig.released = false;
rig.travelTime = 0;
rig.flightTime = 0;
rig.fallVelocity.set(0, 0, 0);
placeBlockOnRamp(rig);
refreshStatus(rig);
}
let paused = false;
function runComparison() {
paused = false;
$("pause").textContent = "Pause";
for (const rig of rigs) {
resetRig(rig);
rig.released = true;
rig.phase = wouldSlide(rig) ? "sliding" : "static";
refreshStatus(rig);
}
}
function resetPositions() {
paused = false;
$("pause").textContent = "Pause";
for (const rig of rigs) resetRig(rig);
}
function applySettings(rig) {
rig.angle = THREE.MathUtils.degToRad(
Number($(rig.ids.angle).value)
);
rig.mu = Number($(rig.ids.mu).value);
$(rig.ids.angleOut).textContent =
`${$(rig.ids.angle).value}°`;
$(rig.ids.muOut).textContent =
Number($(rig.ids.mu).value).toFixed(2);
if (rig.released) {
if (rig.phase === "static") {
rig.phase = wouldSlide(rig) ? "sliding" : "static";
} else if (
rig.phase === "sliding" &&
!wouldSlide(rig)
) {
rig.phase = "static";
rig.v = 0;
}
}
updateRampVisual(rig);
placeBlockOnRamp(rig);
refreshStatus(rig);
}
for (const rig of rigs) {
updateRampVisual(rig);
placeBlockOnRamp(rig);
refreshStatus(rig);
$(rig.ids.angle).addEventListener("input", () => {
clearTimeout(autoRunTimer);
applySettings(rig);
});
$(rig.ids.mu).addEventListener("input", () => {
clearTimeout(autoRunTimer);
applySettings(rig);
});
}
function transitionToFalling(rig) {
rig.phase = "falling";
rig.s = RAMP_LENGTH - BLOCK_WIDTH / 2;
rig.flightTime = 0;
const tangent = rampTangent(rig);
rig.fallVelocity.copy(tangent).multiplyScalar(rig.v);
placeBlockOnRamp(rig);
refreshStatus(rig);
}
function approximateBottomHeight(rotation) {
const zTerm =
Math.abs(Math.sin(rotation.z)) * BLOCK_WIDTH / 2;
const yTerm =
Math.abs(Math.cos(rotation.z)) * BLOCK_HEIGHT / 2;
const xTerm =
Math.abs(Math.sin(rotation.x)) * BLOCK_DEPTH / 2;
return zTerm + yTerm + xTerm;
}
function stepSimulation(dt) {
for (const rig of rigs) {
if (rig.phase === "sliding") {
if (!wouldSlide(rig)) {
rig.phase = "static";
rig.v = 0;
refreshStatus(rig);
continue;
}
rig.v += accelerationFor(rig) * dt;
rig.s += rig.v * dt;
rig.travelTime += dt;
placeBlockOnRamp(rig);
if (rig.s >= RAMP_LENGTH - BLOCK_WIDTH / 2) {
transitionToFalling(rig);
}
} else if (rig.phase === "falling") {
rig.flightTime += dt;
rig.fallVelocity.y -= G * dt;
rig.block.position.addScaledVector(
rig.fallVelocity,
dt
);
rig.block.rotation.x += dt * 0.55;
rig.block.rotation.z += dt * 0.48;
const bottomHeight = approximateBottomHeight(
rig.block.rotation
);
if (
rig.block.position.y <=
GROUND_Y + bottomHeight + 0.025
) {
rig.block.position.y =
GROUND_Y + bottomHeight;
rig.block.rotation.x *= 0.35;
rig.block.rotation.z *= 0.35;
rig.phase = "landed";
refreshStatus(rig);
}
}
}
}
$("run").addEventListener("click", () => {
clearTimeout(autoRunTimer);
runComparison();
});
$("reset").addEventListener("click", () => {
clearTimeout(autoRunTimer);
resetPositions();
});
$("pause").addEventListener("click", () => {
paused = !paused;
$("pause").textContent = paused ? "Resume" : "Pause";
});
// A short delayed demonstration starts automatically.
const autoRunTimer = setTimeout(() => {
runComparison();
}, 1200);
// ---------------------------------------------------------------------
// Custom camera controls: pointer orbit, wheel zoom, double-click reset.
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.35, 1.25, 0);
let cameraYaw = -0.08;
let cameraPitch = 1.14;
let cameraRadius = 15.5;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const horizontal = Math.sin(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.cos(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = -0.08;
cameraPitch = 1.14;
cameraRadius = innerWidth < 700 ? 18 : 15.5;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.48,
1.43
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
event.pointerId !== undefined &&
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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
8,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.fov = innerWidth < 700 ? 54 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
cameraTarget.x = innerWidth < 700 ? 0.8 : 0.35;
}
addEventListener("resize", resize);
resize();
resetCamera();
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.04);
previousTime = now;
if (!paused) {
stepSimulation(dt);
}
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```