Prompt specification
Build or customize the Three.js 3D scene "Rotational toy parameter lab" 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: Rotational toy parameter lab
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotational-toy-parameter-lab-x0380
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a spinning top. Create a controlled toy display with adjustable rim radius and fictional friction; compare motion traces under an explicit simplified model without claiming accurate gyroscopic prediction. Low-poly.
Working Three.js Starter (rotational-toy-parameter-lab-x0380):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Controlled Spinning Top Laboratory</title>
<style>
:root {
color-scheme: dark;
--bg: #080d17;
--panel: #0e1624;
--panel-2: #111c2c;
--border: #26354a;
--text: #eaf2ff;
--muted: #93a6be;
--cyan: #58e6ff;
--amber: #ffb454;
--green: #73f2b0;
--red: #ff718b;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
border: 1px solid var(--border);
border-radius: 9px;
padding: 9px 12px;
color: var(--text);
background: #172438;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
background: #20314a;
border-color: #46617f;
}
button:active {
transform: translateY(1px);
}
button.primary {
color: #071016;
background: var(--cyan);
border-color: var(--cyan);
font-weight: 750;
}
button.primary:hover {
background: #8cefff;
}
.app {
height: 100vh;
height: 100dvh;
display: grid;
grid-template-rows: auto minmax(0, 1fr);
overflow: hidden;
}
header {
min-height: 68px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 13px 20px;
border-bottom: 1px solid var(--border);
background: linear-gradient(90deg, #101a29, #0b1220);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
flex: 0 0 auto;
display: grid;
place-items: center;
color: var(--cyan);
border: 1px solid #315269;
border-radius: 11px;
background: #0a1824;
box-shadow: inset 0 0 18px #58e6ff12;
}
.brand-mark svg {
width: 23px;
height: 23px;
}
h1 {
margin: 0;
font-size: clamp(16px, 2vw, 21px);
line-height: 1.15;
letter-spacing: -0.02em;
}
.subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 12px;
}
.status-row {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
}
.chip {
padding: 6px 9px;
border: 1px solid var(--border);
border-radius: 999px;
color: var(--muted);
background: #0b1421;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.chip.live {
color: var(--green);
border-color: #275a46;
}
.chip.warning {
color: var(--amber);
border-color: #654a28;
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 330px;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #090f1a;
isolation: isolate;
}
.stage canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
.stage canvas:active {
cursor: grabbing;
}
.view-label {
position: absolute;
z-index: 2;
top: 14px;
left: 14px;
min-width: 175px;
max-width: calc(50% - 28px);
padding: 10px 12px;
border: 1px solid #2b4056;
border-radius: 10px;
background: #09121dd9;
backdrop-filter: blur(9px);
box-shadow: 0 8px 25px #0004;
pointer-events: none;
}
.view-label.b {
left: calc(50% + 14px);
}
.label-title {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
.dot.amber {
background: var(--amber);
box-shadow: 0 0 10px var(--amber);
}
.view-meta {
margin-top: 7px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.5;
}
.divider {
position: absolute;
z-index: 1;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: linear-gradient(
transparent,
#40516a 15%,
#40516a 85%,
transparent
);
pointer-events: none;
}
.camera-hint {
position: absolute;
z-index: 2;
right: 12px;
bottom: 11px;
padding: 6px 9px;
color: #a8b8ca;
border: 1px solid #26384c;
border-radius: 8px;
background: #07101bcc;
font-size: 10px;
pointer-events: none;
}
.stage.stacked .view-label.b {
top: calc(50% + 14px);
left: 14px;
}
.stage.stacked .divider {
top: 50%;
bottom: auto;
left: 0;
right: 0;
width: auto;
height: 1px;
background: linear-gradient(
90deg,
transparent,
#40516a 15%,
#40516a 85%,
transparent
);
}
aside {
min-height: 0;
overflow: auto;
border-left: 1px solid var(--border);
background: linear-gradient(180deg, #0f1826, #0b1220);
}
.control-section {
padding: 18px;
border-bottom: 1px solid var(--border);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 15px;
}
.section-heading h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scenario-pill {
padding: 4px 8px;
border-radius: 999px;
color: var(--cyan);
background: #58e6ff14;
border: 1px solid #58e6ff32;
font-size: 10px;
font-weight: 800;
}
.control {
margin-bottom: 18px;
}
.control:last-child {
margin-bottom: 0;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
font-size: 12px;
}
.control-label output {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
.control small {
display: block;
margin-top: 6px;
color: #73869e;
font-size: 10px;
line-height: 1.4;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0;
appearance: none;
border-radius: 999px;
background: linear-gradient(90deg, #2a7791 var(--fill, 50%), #263449 var(--fill, 50%));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #0b1522;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px #58e6ff88, 0 0 12px #58e6ff55;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #0b1522;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px #58e6ff88, 0 0 12px #58e6ff55;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 8px;
}
.button-row button {
width: 100%;
font-size: 11px;
}
.pin-button {
width: 100%;
color: var(--amber);
border-color: #72502a;
background: #ffb4540d;
}
.pin-button:hover {
background: #ffb4541b;
}
.baseline {
margin-top: 11px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.55;
}
.chart-wrap {
padding: 12px;
border: 1px solid #26374c;
border-radius: 10px;
background: #09121d;
}
.chart-title {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: #aebed0;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
#plan-chart {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
.chart-legend {
display: flex;
gap: 15px;
margin-top: 6px;
color: var(--muted);
font-size: 10px;
}
.chart-legend span {
display: flex;
align-items: center;
gap: 5px;
}
.model-copy {
color: #a9b9cc;
font-size: 11px;
line-height: 1.55;
}
.model-copy strong {
color: var(--text);
}
.equations {
margin: 12px 0;
padding: 12px;
overflow-x: auto;
border: 1px solid #293b51;
border-radius: 9px;
color: #c9d9ea;
background: #08111c;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.75;
white-space: nowrap;
}
.warning-box {
padding: 10px 11px;
border: 1px solid #684b29;
border-radius: 9px;
color: #e8c895;
background: #ffb4540c;
font-size: 10px;
line-height: 1.5;
}
@media (max-width: 860px) {
.app {
height: auto;
min-height: 100vh;
overflow: visible;
}
.workspace {
grid-template-columns: 1fr;
}
.stage {
height: 760px;
}
aside {
border-top: 1px solid var(--border);
border-left: 0;
overflow: visible;
}
}
@media (max-width: 520px) {
header {
align-items: flex-start;
padding: 12px;
}
.subtitle {
display: none;
}
.status-row .chip:not(.warning) {
display: none;
}
.stage {
height: 740px;
}
.view-label {
min-width: 0;
max-width: calc(100% - 28px);
padding: 8px 9px;
}
.view-label.b {
max-width: calc(100% - 28px);
}
.view-meta {
font-size: 9px;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path d="M12 2v6M6 12h12M12 12l-5 9h10l-5-9Z"
stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/>
<path d="M7 9h10" stroke="currentColor" stroke-width="1.6"/>
</svg>
</div>
<div>
<h1>Spinning Top Laboratory</h1>
<div class="subtitle">Low-poly controlled toy display · side-by-side motion traces</div>
</div>
</div>
<div class="status-row">
<span class="chip live" id="run-status">Running</span>
<span class="chip">Three.js r160</span>
<span class="chip warning">Fictional model</span>
</div>
</header>
<main class="workspace">
<section class="stage" id="stage" aria-label="Two comparison views of a low-poly spinning top">
<div class="view-label">
<div class="label-title"><span class="dot"></span>A · Live input</div>
<div class="view-meta" id="meta-a">R 0.88 · F 0.16<br>Toy Ω 8.0 · ψ 0°</div>
</div>
<div class="view-label b">
<div class="label-title"><span class="dot amber"></span>B · Pinned baseline</div>
<div class="view-meta" id="meta-b">R 0.72 · F 0.30<br>Toy Ω 8.0 · ψ 0°</div>
</div>
<div class="divider"></div>
<div class="camera-hint">Drag to orbit · wheel or pinch to zoom · double-click resets view</div>
</section>
<aside>
<section class="control-section">
<div class="section-heading">
<h2>Scenario A controls</h2>
<span class="scenario-pill">Editable</span>
</div>
<div class="control">
<label class="control-label" for="radius">
<span>Rim radius R</span>
<output id="radius-value" for="radius">0.88 u</output>
</label>
<input id="radius" type="range" min="0.55" max="1.15" step="0.01" value="0.88">
<small>Changes the low-poly rim and the toy model’s radius term.</small>
</div>
<div class="control">
<label class="control-label" for="friction">
<span>Fictional friction F</span>
<output id="friction-value" for="friction">0.16</output>
</label>
<input id="friction" type="range" min="0" max="0.45" step="0.01" value="0.16">
<small>An invented, dimensionless drag parameter for controlled comparison.</small>
</div>
<div class="control">
<label class="control-label" for="speed">
<span>Initial toy spin Ω₀</span>
<output id="speed-value" for="speed">8.0</output>
</label>
<input id="speed" type="range" min="3" max="13" step="0.1" value="8">
<small>Restarting the pair after an edit keeps their elapsed time comparable.</small>
</div>
</section>
<section class="control-section">
<div class="section-heading">
<h2>Experiment actions</h2>
</div>
<div class="button-row">
<button class="primary" id="pause-button">Pause</button>
<button id="reset-button">Restart pair</button>
</div>
<button class="pin-button" id="pin-button">Pin A values into baseline B</button>
<div class="baseline" id="baseline-value">
B baseline: R 0.72 · F 0.30 · Ω₀ 8.0
</div>
</section>
<section class="control-section">
<div class="section-heading">
<h2>Top-down trace comparison</h2>
</div>
<div class="chart-wrap">
<div class="chart-title">
<span>Floor projection</span>
<span>Live</span>
</div>
<svg id="plan-chart" viewBox="0 0 240 120" role="img"
aria-label="Top-down comparison of the two rim-marker traces">
<defs>
<pattern id="chart-grid" width="20" height="20" patternUnits="userSpaceOnUse">
<path d="M 20 0 L 0 0 0 20" fill="none"
stroke="#26374b" stroke-width="0.7"/>
</pattern>
</defs>
<rect width="240" height="120" fill="url(#chart-grid)"/>
<circle cx="120" cy="60" r="42" fill="none"
stroke="#30445b" stroke-width="0.8" stroke-dasharray="3 4"/>
<path id="trace-a" fill="none" stroke="#58e6ff" stroke-width="1.8"
stroke-linecap="round" stroke-linejoin="round"/>
<path id="trace-b" fill="none" stroke="#ffb454" stroke-width="1.8"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<div class="chart-legend">
<span><i class="dot"></i> Scenario A</span>
<span><i class="dot amber"></i> Scenario B</span>
</div>
</div>
</section>
<section class="control-section">
<div class="section-heading">
<h2>Explicit toy model</h2>
<span class="chip warning">Not physics</span>
</div>
<div class="model-copy">
A bright marker is fixed to the rim. Its horizontal projection is drawn
on the floor and in the plan chart. The following arbitrary arithmetic
generates the spin and heading proxies:
</div>
<div class="equations">
Ω′ = max(0, Ω − 2.5 F Δt)<br>
ψ′ = ψ + 0.32 Ω (R / 0.80)² Δt / (1 + 3F)<br>
φ* = clamp(0.025 + 0.016 Ω (R / 0.80)² / (1 + 4F), 0.02, 0.28)<br>
φ′ = φ + (φ* − φ)(1 − exp(−2.2 Δt))
</div>
<div class="warning-box">
This is a deliberately simplified comparison model. It does not solve
rigid-body Euler equations, model real contact torques, or claim
accurate gyroscopic prediction.
</div>
</section>
</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("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.prepend(renderer.domElement);
const BASE_RADIUS = 0.8;
const REFERENCE_RADIUS = 0.8;
const MAX_TRACE_POINTS = 900;
const TRACE_SAMPLE_INTERVAL = 0.022;
const clamp = THREE.MathUtils.clamp;
class ToyPanel {
constructor(accent, initial) {
this.accent = accent;
this.model = {
radius: initial.radius,
friction: initial.friction,
initialSpin: initial.spin,
omega: initial.spin,
spin: 0,
heading: 0,
tilt: 0.06,
time: 0
};
this.history = [];
this.sampleAccumulator = 0;
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0x090f1a);
this.scene.fog = new THREE.Fog(0x090f1a, 5.5, 11);
this.camera = new THREE.PerspectiveCamera(35, 1, 0.1, 40);
this.buildEnvironment();
this.buildTop();
this.buildTrace();
this.tempPosition = new THREE.Vector3();
this.applyTransform();
this.resetTrace();
}
buildEnvironment() {
const hemisphere = new THREE.HemisphereLight(
0xb9dcff,
0x161526,
2.25
);
this.scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(3.5, 5.5, 4);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -3;
key.shadow.camera.right = 3;
key.shadow.camera.top = 3;
key.shadow.camera.bottom = -3;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 12;
key.shadow.bias = -0.0008;
this.scene.add(key);
const accentLight = new THREE.PointLight(this.accent, 13, 9, 2);
accentLight.position.set(-2.5, 2.2, -2.4);
this.scene.add(accentLight);
const ground = new THREE.Mesh(
new THREE.CircleGeometry(4.8, 64),
new THREE.MeshStandardMaterial({
color: 0x0c1522,
roughness: 0.94,
metalness: 0.04
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
this.scene.add(ground);
const grid = new THREE.GridHelper(8, 16, 0x365068, 0x213247);
grid.position.y = -0.017;
grid.material.transparent = true;
grid.material.opacity = 0.27;
this.scene.add(grid);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.75, 1.82, 0.055, 48),
new THREE.MeshStandardMaterial({
color: 0x101c2a,
roughness: 0.76,
metalness: 0.12
})
);
pedestal.position.y = -0.005;
pedestal.receiveShadow = true;
this.scene.add(pedestal);
const pedestalRing = new THREE.Mesh(
new THREE.RingGeometry(1.69, 1.73, 48),
new THREE.MeshBasicMaterial({
color: this.accent,
transparent: true,
opacity: 0.32,
side: THREE.DoubleSide
})
);
pedestalRing.rotation.x = -Math.PI / 2;
pedestalRing.position.y = 0.026;
this.scene.add(pedestalRing);
}
buildTop() {
this.root = new THREE.Group();
this.scene.add(this.root);
this.tiltPivot = new THREE.Group();
this.tiltPivot.position.y = 0.53;
this.root.add(this.tiltPivot);
this.spinner = new THREE.Group();
this.tiltPivot.add(this.spinner);
this.radialGroup = new THREE.Group();
this.spinner.add(this.radialGroup);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x29475c,
roughness: 0.5,
metalness: 0.2,
flatShading: true
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: this.accent,
emissive: this.accent,
emissiveIntensity: 0.18,
roughness: 0.36,
metalness: 0.25,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x182330,
roughness: 0.62,
metalness: 0.28,
flatShading: true
});
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.72, 0.28, 12, 1, false),
bodyMaterial
);
body.castShadow = true;
body.receiveShadow = true;
this.radialGroup.add(body);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.69, 0.062, 4, 12),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = -0.025;
rim.castShadow = true;
this.radialGroup.add(rim);
const lowerBand = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.035, 4, 10),
darkMaterial
);
lowerBand.rotation.x = Math.PI / 2;
lowerBand.position.y = -0.145;
this.radialGroup.add(lowerBand);
const tip = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.30, 10),
new THREE.MeshStandardMaterial({
color: 0x9aabb9,
roughness: 0.28,
metalness: 0.72,
flatShading: true
})
);
tip.rotation.z = Math.PI;
tip.position.y = -0.32;
tip.castShadow = true;
this.radialGroup.add(tip);
this.marker = new THREE.Mesh(
new THREE.SphereGeometry(0.068, 8, 5),
new THREE.MeshBasicMaterial({ color: this.accent })
);
this.marker.position.set(0.70, -0.02, 0);
this.radialGroup.add(this.marker);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.28, 0.14, 10),
rimMaterial
);
cap.position.y = 0.21;
cap.castShadow = true;
this.spinner.add(cap);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.105, 0.52, 8),
darkMaterial
);
stem.position.y = 0.51;
stem.castShadow = true;
this.spinner.add(stem);
const knob = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 8, 5),
rimMaterial
);
knob.position.y = 0.82;
knob.castShadow = true;
this.spinner.add(knob);
this.setRadius(this.model.radius);
}
buildTrace() {
this.tracePositions = new Float32Array(MAX_TRACE_POINTS * 3);
const geometry = new THREE.BufferGeometry();
const attribute = new THREE.BufferAttribute(
this.tracePositions,
3
);
attribute.setUsage(THREE.DynamicDrawUsage);
geometry.setAttribute("position", attribute);
geometry.setDrawRange(0, 0);
this.trace = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: this.accent,
transparent: true,
opacity: 0.88,
depthWrite: false
})
);
this.trace.frustumCulled = false;
this.scene.add(this.trace);
this.traceDot = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 8, 5),
new THREE.MeshBasicMaterial({
color: this.accent,
transparent: true,
opacity: 0.95
})
);
this.scene.add(this.traceDot);
}
setRadius(radius) {
this.model.radius = radius;
const scale = radius / BASE_RADIUS;
this.radialGroup.scale.set(scale, 1, scale);
}
applyTransform() {
this.root.rotation.y = this.model.heading;
this.tiltPivot.rotation.z = this.model.tilt;
this.tiltPivot.rotation.x = this.model.tilt * 0.35;
this.spinner.rotation.y = this.model.spin;
this.root.updateMatrixWorld(true);
}
step(dt) {
const m = this.model;
m.omega = Math.max(0, m.omega - 2.5 * m.friction * dt);
m.spin += m.omega * dt;
const radiusFactor = (m.radius / REFERENCE_RADIUS) ** 2;
m.heading +=
0.32 *
m.omega *
radiusFactor *
dt /
(1 + 3 * m.friction);
const targetTilt = clamp(
0.025 +
0.016 *
m.omega *
radiusFactor /
(1 + 4 * m.friction),
0.02,
0.28
);
m.tilt +=
(targetTilt - m.tilt) *
(1 - Math.exp(-2.2 * dt));
m.time += dt;
this.applyTransform();
this.sampleAccumulator += dt;
if (this.sampleAccumulator >= TRACE_SAMPLE_INTERVAL) {
this.sampleAccumulator %= TRACE_SAMPLE_INTERVAL;
this.sampleMarker();
}
}
sampleMarker() {
this.marker.getWorldPosition(this.tempPosition);
const x = this.tempPosition.x;
const z = this.tempPosition.z;
this.history.push({ x, z });
if (this.history.length > MAX_TRACE_POINTS) {
this.history.shift();
}
const count = this.history.length;
this.tracePositions.copyWithin(
0,
3,
count * 3
);
const offset = (count - 1) * 3;
this.tracePositions[offset] = x;
this.tracePositions[offset + 1] = 0.034;
this.tracePositions[offset + 2] = z;
this.trace.geometry.attributes.position.needsUpdate = true;
this.trace.geometry.setDrawRange(0, count);
this.traceDot.position.set(x, 0.044, z);
}
resetModel() {
const m = this.model;
m.omega = m.initialSpin;
m.spin = 0;
m.heading = 0;
m.tilt = 0.06;
m.time = 0;
this.sampleAccumulator = 0;
this.applyTransform();
this.resetTrace();
}
resetTrace() {
this.history.length = 0;
this.trace.geometry.setDrawRange(0, 0);
this.sampleMarker();
}
}
const panelA = new ToyPanel(0x58e6ff, {
radius: 0.88,
friction: 0.16,
spin: 8
});
const panelB = new ToyPanel(0xffb454, {
radius: 0.72,
friction: 0.30,
spin: 8
});
// Shared orbit state keeps the comparison camera angle identical.
const orbit = {
theta: 0.72,
phi: 1.02,
radius: 4.65,
target: new THREE.Vector3(0, 0.54, 0)
};
function updateCameras() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
const x =
orbit.radius *
sinPhi *
Math.sin(orbit.theta);
const y =
orbit.radius *
cosPhi +
orbit.target.y;
const z =
orbit.radius *
sinPhi *
Math.cos(orbit.theta);
for (const panel of [panelA, panelB]) {
panel.camera.position.set(x, y, z);
panel.camera.lookAt(orbit.target);
}
}
function resetCamera() {
orbit.theta = 0.72;
orbit.phi = 1.02;
orbit.radius = 4.65;
}
// Direct pointer controls: no examples/addons are imported.
const pointers = new Map();
let pinchDistance = 0;
function currentPinchDistance() {
if (pointers.size < 2) return 0;
const values = [...pointers.values()];
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
pinchDistance = currentPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
orbit.theta -= dx * 0.006;
orbit.phi = clamp(orbit.phi - dy * 0.005, 0.38, 1.42);
} else {
const oldDistance = currentPinchDistance();
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
const newDistance = currentPinchDistance();
if (oldDistance > 0 && newDistance > 0) {
orbit.radius = clamp(
orbit.radius * (oldDistance / newDistance),
3.1,
8.2
);
}
pinchDistance = newDistance;
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = pointers.size >= 2
? currentPinchDistance()
: 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
3.1,
8.2
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
let stageWidth = 1;
let stageHeight = 1;
let stacked = false;
function resize() {
const rect = stage.getBoundingClientRect();
stageWidth = Math.max(1, Math.floor(rect.width));
stageHeight = Math.max(1, Math.floor(rect.height));
stacked = stageWidth < 640;
renderer.setSize(stageWidth, stageHeight, false);
stage.classList.toggle("stacked", stacked);
if (stacked) {
const halfHeight = Math.floor(stageHeight / 2);
panelA.camera.aspect = stageWidth / halfHeight;
panelB.camera.aspect = stageWidth / halfHeight;
} else {
const halfWidth = Math.floor(stageWidth / 2);
panelA.camera.aspect = halfWidth / stageHeight;
panelB.camera.aspect = (stageWidth - halfWidth) / stageHeight;
}
panelA.camera.updateProjectionMatrix();
panelB.camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
function renderViews() {
renderer.setScissorTest(true);
if (stacked) {
const halfHeight = Math.floor(stageHeight / 2);
renderer.setViewport(0, halfHeight, stageWidth, stageHeight - halfHeight);
renderer.setScissor(0, halfHeight, stageWidth, stageHeight - halfHeight);
renderer.render(panelA.scene, panelA.camera);
renderer.setViewport(0, 0, stageWidth, halfHeight);
renderer.setScissor(0, 0, stageWidth, halfHeight);
renderer.render(panelB.scene, panelB.camera);
} else {
const halfWidth = Math.floor(stageWidth / 2);
renderer.setViewport(0, 0, halfWidth, stageHeight);
renderer.setScissor(0, 0, halfWidth, stageHeight);
renderer.render(panelA.scene, panelA.camera);
renderer.setViewport(halfWidth, 0, stageWidth - halfWidth, stageHeight);
renderer.setScissor(halfWidth, 0, stageWidth - halfWidth, stageHeight);
renderer.render(panelB.scene, panelB.camera);
}
renderer.setScissorTest(false);
}
// Interface elements.
const radiusInput = document.getElementById("radius");
const frictionInput = document.getElementById("friction");
const speedInput = document.getElementById("speed");
const radiusValue = document.getElementById("radius-value");
const frictionValue = document.getElementById("friction-value");
const speedValue = document.getElementById("speed-value");
const metaA = document.getElementById("meta-a");
const metaB = document.getElementById("meta-b");
const baselineValue = document.getElementById("baseline-value");
const pauseButton = document.getElementById("pause-button");
const resetButton = document.getElementById("reset-button");
const pinButton = document.getElementById("pin-button");
const runStatus = document.getElementById("run-status");
const traceA = document.getElementById("trace-a");
const traceB = document.getElementById("trace-b");
let running = true;
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateControlReadouts() {
const a = panelA.model;
const b = panelB.model;
radiusValue.textContent = `${a.radius.toFixed(2)} u`;
frictionValue.textContent = a.friction.toFixed(2);
speedValue.textContent = a.initialSpin.toFixed(1);
baselineValue.textContent =
`B baseline: R ${b.radius.toFixed(2)} · F ${b.friction.toFixed(2)} · Ω₀ ${b.initialSpin.toFixed(1)}`;
for (const input of [radiusInput, frictionInput, speedInput]) {
updateRangeFill(input);
}
}
function restartPair() {
panelA.resetModel();
panelB.resetModel();
updateControlReadouts();
}
radiusInput.addEventListener("input", () => {
panelA.setRadius(Number(radiusInput.value));
restartPair();
});
frictionInput.addEventListener("input", () => {
panelA.model.friction = Number(frictionInput.value);
restartPair();
});
speedInput.addEventListener("input", () => {
panelA.model.initialSpin = Number(speedInput.value);
restartPair();
});
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause" : "Resume";
runStatus.textContent = running ? "Running" : "Paused";
runStatus.classList.toggle("live", running);
});
resetButton.addEventListener("click", restartPair);
pinButton.addEventListener("click", () => {
const source = panelA.model;
panelB.model.radius = source.radius;
panelB.model.friction = source.friction;
panelB.model.initialSpin = source.initialSpin;
panelB.setRadius(source.radius);
restartPair();
});
function chartPath(panel) {
const points = panel.history;
if (!points.length) return "";
const scale = 76;
let path = "";
for (let i = 0; i < points.length; i++) {
const x = 120 + points[i].x * scale;
const y = 60 + points[i].z * scale;
path += `${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)} `;
}
return path.trim();
}
function updateReadouts() {
const a = panelA.model;
const b = panelB.model;
metaA.innerHTML =
`R ${a.radius.toFixed(2)} · F ${a.friction.toFixed(2)}<br>` +
`Toy Ω ${a.omega.toFixed(1)} · ψ ${THREE.MathUtils.radToDeg(a.heading).toFixed(0)}°`;
metaB.innerHTML =
`R ${b.radius.toFixed(2)} · F ${b.friction.toFixed(2)}<br>` +
`Toy Ω ${b.omega.toFixed(1)} · ψ ${THREE.MathUtils.radToDeg(b.heading).toFixed(0)}°`;
traceA.setAttribute("d", chartPath(panelA));
traceB.setAttribute("d", chartPath(panelB));
}
updateControlReadouts();
updateReadouts();
let previousTime = performance.now();
let previousUiTime = 0;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (running) {
panelA.step(dt);
panelB.step(dt);
}
updateCameras();
renderViews();
if (now - previousUiTime > 100) {
updateReadouts();
previousUiTime = now;
}
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```