Prompt specification
Build or customize the Three.js 3D scene "Spinning wheel on a gimbal" 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: Spinning wheel on a gimbal
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/spinning-wheel-on-a-gimbal-x1393
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a low-poly wheel inside two gimbal rings. Use separate colors for the three axes and expose enough of each pivot to show how the assembly connects. Let the wheel spin while the rings are held still, then allow each ring angle to be adjusted independently. This is a kinematic model, so do not automatically infer gyroscopic precession from the sliders. A stop control should bring the displayed spin to rest rather than imply an indefinitely powered device.
Working Three.js Starter (spinning-wheel-on-a-gimbal-x1393):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Low-Poly Gimbal Wheel</title>
<style>
:root {
color-scheme: light;
--x-axis: #3487e8;
--y-axis: #28b982;
--z-axis: #f39a3f;
--ink: #17212b;
--muted: #607080;
--panel: rgba(249, 252, 255, 0.9);
--line: rgba(36, 53, 70, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background:
radial-gradient(circle at 72% 18%, #ffffff 0, #edf4f8 42%, #dce7ee 100%);
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(340px, calc(100vw - 36px));
padding: 19px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(36, 55, 74, 0.16),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(16px) saturate(130%);
-webkit-backdrop-filter: blur(16px) saturate(130%);
}
h1 {
margin: 0 0 5px;
font-size: 19px;
line-height: 1.2;
letter-spacing: -0.025em;
}
.intro {
margin: 0 0 18px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.5;
}
.control {
margin-top: 15px;
}
.control-header {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
font-size: 12.5px;
font-weight: 650;
}
.control-header output {
min-width: 55px;
color: var(--muted);
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--accent, #5b7185);
cursor: pointer;
}
.axis-x {
--accent: var(--x-axis);
}
.axis-y {
--accent: var(--y-axis);
}
.axis-z {
--accent: var(--z-axis);
}
.dot {
display: inline-block;
width: 9px;
height: 9px;
margin-right: 7px;
border-radius: 50%;
background: var(--dot-color);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--dot-color) 18%, transparent);
vertical-align: 1px;
}
.button-row {
display: flex;
gap: 8px;
margin-top: 10px;
}
button {
flex: 1;
min-height: 38px;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--ink);
background: rgba(255, 255, 255, 0.86);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fff;
border-color: rgba(36, 53, 70, 0.25);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: rgba(40, 185, 130, 0.35);
color: #116b4b;
background: rgba(40, 185, 130, 0.12);
}
button.primary.active {
color: #fff;
background: var(--y-axis);
border-color: var(--y-axis);
}
button.stop {
color: #9d3c2e;
border-color: rgba(190, 73, 52, 0.25);
background: rgba(190, 73, 52, 0.08);
}
.status {
min-height: 34px;
margin-top: 12px;
padding: 9px 11px;
border-radius: 9px;
color: #465766;
background: rgba(74, 96, 116, 0.07);
font-size: 11.5px;
line-height: 1.4;
}
.note {
margin: 10px 0 0;
color: var(--muted);
font-size: 10.8px;
line-height: 1.45;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 6px;
margin-top: 16px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.legend-item {
text-align: center;
font-size: 10px;
line-height: 1.35;
color: var(--muted);
}
.legend-item strong {
display: block;
color: var(--ink);
font-size: 11.5px;
}
.camera-hint {
position: fixed;
z-index: 5;
right: 18px;
bottom: 16px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
color: #526574;
background: rgba(249, 252, 255, 0.72);
box-shadow: 0 8px 24px rgba(36, 55, 74, 0.1);
backdrop-filter: blur(12px);
font-size: 11px;
pointer-events: none;
}
@media (max-width: 620px) {
.panel {
top: 10px;
left: 10px;
padding: 15px;
}
.intro,
.legend {
display: none;
}
.camera-hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<aside class="panel" aria-label="Gimbal controls">
<h1>Low-Poly Gimbal Wheel</h1>
<p class="intro">
A kinematic wheel assembly with two independently adjustable hinge rings.
</p>
<div class="control axis-x">
<div class="control-header">
<label for="outer-angle">
<span class="dot" style="--dot-color: var(--x-axis)"></span>
Outer ring · X
</label>
<output id="outer-value" for="outer-angle">0°</output>
</div>
<input
id="outer-angle"
type="range"
min="-180"
max="180"
step="1"
value="0"
aria-label="Outer ring X angle"
>
</div>
<div class="control axis-y">
<div class="control-header">
<label for="inner-angle">
<span class="dot" style="--dot-color: var(--y-axis)"></span>
Inner ring · Y
</label>
<output id="inner-value" for="inner-angle">0°</output>
</div>
<input
id="inner-angle"
type="range"
min="-180"
max="180"
step="1"
value="0"
aria-label="Inner ring Y angle"
>
</div>
<div class="control axis-z">
<div class="control-header">
<label for="wheel-speed">
<span class="dot" style="--dot-color: var(--z-axis)"></span>
Wheel drive · Z
</label>
<output id="speed-value" for="wheel-speed">1.80 rad/s</output>
</div>
<input
id="wheel-speed"
type="range"
min="0"
max="3"
step="0.05"
value="1.8"
aria-label="Wheel angular speed"
>
</div>
<div class="button-row">
<button id="run-wheel" class="primary active" type="button">Run wheel</button>
<button id="stop-wheel" class="stop" type="button">Stop wheel</button>
</div>
<div class="button-row">
<button id="reset-rings" type="button">Reset rings</button>
<button id="reset-view" type="button">Reset view</button>
</div>
<div id="wheel-status" class="status" aria-live="polite">
Wheel running at 1.80 rad/s.
</div>
<p class="note">
The sliders set hinge angles directly. Wheel rotation does not modify the
ring angles, and no gyroscopic precession is inferred. Stop removes the
drive and lets the displayed wheel coast to rest.
</p>
<div class="legend" aria-label="Axis color legend">
<div class="legend-item">
<strong style="color: var(--x-axis)">X</strong>
Outer ring
</div>
<div class="legend-item">
<strong style="color: var(--y-axis)">Y</strong>
Inner ring
</div>
<div class="legend-item">
<strong style="color: var(--z-axis)">Z</strong>
Wheel spin
</div>
</div>
</aside>
<div class="camera-hint">Drag to orbit · mouse wheel to zoom · double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const COLORS = {
x: 0x3487e8,
y: 0x28b982,
z: 0xf39a3f,
dark: 0x263541,
metal: 0x93a4b1,
pale: 0xe8eef2,
warm: 0xffd39a
};
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe5eef4, 13, 29);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xeaf2f7, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
app.appendChild(renderer.domElement);
const canvas = renderer.domElement;
canvas.setAttribute("aria-label", "Interactive low-poly gimbal wheel scene");
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xeaf7ff, 0x71808c, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d9ff, 1.15);
fillLight.position.set(-6, 4, -5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd8ae, 0.85);
rimLight.position.set(2, 5, -8);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xdde8ee,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.82;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 60, 0xa9bac5, 0xbccad3);
grid.position.y = -2.805;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.5,
metalness: options.metalness ?? 0.12,
flatShading: options.flatShading ?? true,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
const materials = {
x: standardMaterial(COLORS.x, { roughness: 0.42, metalness: 0.2 }),
xLight: standardMaterial(0x73b4ff, { roughness: 0.4 }),
y: standardMaterial(COLORS.y, { roughness: 0.42, metalness: 0.18 }),
yLight: standardMaterial(0x76e0b8, { roughness: 0.4 }),
z: standardMaterial(COLORS.z, { roughness: 0.48, metalness: 0.14 }),
zLight: standardMaterial(0xffca82, { roughness: 0.42 }),
dark: standardMaterial(COLORS.dark, { roughness: 0.55, metalness: 0.35 }),
metal: standardMaterial(COLORS.metal, { roughness: 0.32, metalness: 0.72 }),
pale: standardMaterial(COLORS.pale, { roughness: 0.58, metalness: 0.08 }),
warm: standardMaterial(COLORS.warm, { roughness: 0.38 })
};
const up = new THREE.Vector3(0, 1, 0);
function cylinderBetween(parent, start, end, radius, material, segments = 8) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments, 1, false),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addArrowhead(parent, direction, position, material, size = 0.12) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(size, size * 2.8, 6),
material
);
arrow.position.set(...position);
arrow.quaternion.setFromUnitVectors(
up,
new THREE.Vector3(...direction).normalize()
);
arrow.castShadow = true;
parent.add(arrow);
return arrow;
}
function makeLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 384;
labelCanvas.height = 112;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
context.fillStyle = "rgba(20, 31, 41, 0.86)";
context.fillRect(0, 0, labelCanvas.width, labelCanvas.height);
context.strokeStyle = `#${color.toString(16).padStart(6, "0")}`;
context.lineWidth = 8;
context.strokeRect(4, 4, labelCanvas.width - 8, labelCanvas.height - 8);
context.fillStyle = `#${color.toString(16).padStart(6, "0")}`;
context.font = "700 48px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, labelCanvas.width / 2, labelCanvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.28, 0.375, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Mechanical hierarchy
//
// outerPivot: local X hinge
// innerPivot: local Y hinge
// wheelSpin: local Z spin
//
// Ring angles are assigned directly. There is no dynamics or precession.
// -------------------------------------------------------------------------
const outerPivot = new THREE.Group();
outerPivot.rotation.order = "XYZ";
scene.add(outerPivot);
const innerPivot = new THREE.Group();
innerPivot.rotation.order = "YXZ";
outerPivot.add(innerPivot);
const wheelSpin = new THREE.Group();
wheelSpin.rotation.order = "XYZ";
innerPivot.add(wheelSpin);
const OUTER_RADIUS = 2.25;
const INNER_RADIUS = 1.72;
const WHEEL_RADIUS = 1.05;
// Fixed pedestal and outer X-axis supports.
addBox(
scene,
[7.1, 0.32, 4.7],
[0, -2.65, 0],
materials.dark
);
addBox(
scene,
[6.55, 0.08, 4.15],
[0, -2.45, 0],
materials.metal
);
for (const side of [-1, 1]) {
addBox(
scene,
[0.48, 2.45, 0.62],
[side * 2.62, -1.225, 0],
materials.dark
);
// Fixed shaft section.
cylinderBetween(
scene,
[side * 2.30, 0, 0],
[side * 2.73, 0, 0],
0.105,
materials.metal
);
// Fixed bearing housing.
cylinderBetween(
scene,
[side * 2.49, 0, 0],
[side * 2.73, 0, 0],
0.23,
materials.dark
);
}
// Outer ring: low-poly torus in the XY plane.
const outerRing = new THREE.Mesh(
new THREE.TorusGeometry(OUTER_RADIUS, 0.145, 6, 36),
materials.x
);
outerRing.castShadow = true;
outerRing.receiveShadow = true;
outerPivot.add(outerRing);
// Exposed rotating X hinge collars.
for (const side of [-1, 1]) {
cylinderBetween(
outerPivot,
[side * 2.10, 0, 0],
[side * 2.35, 0, 0],
0.235,
materials.xLight
);
}
// Markers make rotation around the ring plane visible.
for (let i = 0; i < 4; i++) {
const angle = i * Math.PI / 2 + 0.18;
const marker = addBox(
outerPivot,
[0.34, 0.46, 0.36],
[
Math.cos(angle) * OUTER_RADIUS,
Math.sin(angle) * OUTER_RADIUS,
0
],
i === 0 ? materials.xLight : materials.pale
);
marker.rotation.z = angle;
}
addArrowhead(
outerPivot,
[1, 0, 0],
[2.92, 0, 0],
materials.x,
0.13
);
const outerLabel = makeLabel("X · OUTER", COLORS.x);
outerLabel.position.set(2.55, 0.56, 0.18);
outerPivot.add(outerLabel);
// Inner ring: low-poly torus in the YZ plane.
const innerRing = new THREE.Mesh(
new THREE.TorusGeometry(INNER_RADIUS, 0.12, 6, 32),
materials.y
);
innerRing.rotation.y = Math.PI / 2;
innerRing.castShadow = true;
innerRing.receiveShadow = true;
innerPivot.add(innerRing);
// The outer ring supports the inner Y hinge from opposite sides.
for (const side of [-1, 1]) {
cylinderBetween(
outerPivot,
[0, side * OUTER_RADIUS, 0],
[0, side * INNER_RADIUS, 0],
0.09,
materials.metal
);
cylinderBetween(
outerPivot,
[0, side * (OUTER_RADIUS - 0.13), 0],
[0, side * (OUTER_RADIUS + 0.12), 0],
0.18,
materials.xLight
);
// Rotating green inner bearing.
cylinderBetween(
innerPivot,
[0, side * (INNER_RADIUS - 0.13), 0],
[0, side * (INNER_RADIUS + 0.13), 0],
0.205,
materials.yLight
);
// Split inner shaft stops at the wheel hub instead of crossing the tire.
cylinderBetween(
innerPivot,
[0, side * 0.34, 0],
[0, side * (INNER_RADIUS - 0.06), 0],
0.075,
materials.metal
);
}
// Inner ring markers.
for (let i = 0; i < 3; i++) {
const angle = i * (Math.PI * 2 / 3) + 0.25;
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.17),
i === 0 ? materials.yLight : materials.pale
);
marker.position.set(
0,
Math.cos(angle) * INNER_RADIUS,
Math.sin(angle) * INNER_RADIUS
);
marker.rotation.set(angle * 0.3, angle, angle * 0.2);
marker.castShadow = true;
innerPivot.add(marker);
}
addArrowhead(
innerPivot,
[0, 1, 0],
[0, 2.17, 0.28],
materials.y,
0.115
);
const innerLabel = makeLabel("Y · INNER", COLORS.y);
innerLabel.position.set(0.48, 1.89, 0.25);
innerPivot.add(innerLabel);
// Supports from the inner ring to the wheel's Z axle.
for (const side of [-1, 1]) {
cylinderBetween(
innerPivot,
[0, 0, side * INNER_RADIUS],
[0, 0, side * 1.29],
0.075,
materials.metal
);
cylinderBetween(
innerPivot,
[0, 0, side * 1.20],
[0, 0, side * 1.37],
0.17,
materials.zLight
);
}
// Low-poly wheel tire in the XY plane.
const tire = new THREE.Mesh(
new THREE.TorusGeometry(WHEEL_RADIUS, 0.17, 7, 32),
materials.z
);
tire.castShadow = true;
tire.receiveShadow = true;
wheelSpin.add(tire);
const wheelRim = new THREE.Mesh(
new THREE.TorusGeometry(0.78, 0.055, 5, 28),
materials.zLight
);
wheelRim.castShadow = true;
wheelSpin.add(wheelRim);
// Central wheel hub, with its axle along Z.
cylinderBetween(
wheelSpin,
[0, 0, -1.43],
[0, 0, 1.43],
0.065,
materials.metal
);
cylinderBetween(
wheelSpin,
[0, 0, -0.24],
[0, 0, 0.24],
0.235,
materials.dark
);
cylinderBetween(
wheelSpin,
[0, 0, -0.265],
[0, 0, 0.265],
0.115,
materials.zLight
);
// Eight chunky low-poly spokes.
for (let i = 0; i < 8; i++) {
const angle = i * Math.PI / 4 + Math.PI / 8;
const innerRadius = 0.20;
const outerRadius = 0.81;
cylinderBetween(
wheelSpin,
[
Math.cos(angle) * innerRadius,
Math.sin(angle) * innerRadius,
0
],
[
Math.cos(angle) * outerRadius,
Math.sin(angle) * outerRadius,
0
],
0.052,
i === 0 ? materials.warm : materials.z,
6
);
}
// A distinct wheel marker makes its rotation easy to read.
const wheelMarker = addBox(
wheelSpin,
[0.20, 0.42, 0.37],
[0.81, 0, 0],
materials.warm
);
wheelMarker.rotation.z = Math.PI / 2;
addArrowhead(
wheelSpin,
[0, 0, 1],
[0, 0, 1.55],
materials.z,
0.105
);
const wheelLabel = makeLabel("Z · WHEEL", COLORS.z);
wheelLabel.position.set(0, 0.53, 1.28);
wheelSpin.add(wheelLabel);
// -------------------------------------------------------------------------
// UI and direct kinematic controls
// -------------------------------------------------------------------------
const outerAngleInput = document.getElementById("outer-angle");
const innerAngleInput = document.getElementById("inner-angle");
const wheelSpeedInput = document.getElementById("wheel-speed");
const outerValue = document.getElementById("outer-value");
const innerValue = document.getElementById("inner-value");
const speedValue = document.getElementById("speed-value");
const runButton = document.getElementById("run-wheel");
const stopButton = document.getElementById("stop-wheel");
const resetRingsButton = document.getElementById("reset-rings");
const resetViewButton = document.getElementById("reset-view");
const wheelStatus = document.getElementById("wheel-status");
let driveEnabled = true;
let targetWheelSpeed = Number(wheelSpeedInput.value);
let actualWheelSpeed = targetWheelSpeed;
let wheelAngle = 0;
function formatSignedDegrees(value) {
const rounded = Math.round(value);
return `${rounded > 0 ? "+" : ""}${rounded}°`;
}
outerAngleInput.addEventListener("input", () => {
const degrees = Number(outerAngleInput.value);
// Direct local hinge assignment: no dynamics or inferred motion.
outerPivot.rotation.x = THREE.MathUtils.degToRad(degrees);
outerValue.textContent = formatSignedDegrees(degrees);
});
innerAngleInput.addEventListener("input", () => {
const degrees = Number(innerAngleInput.value);
// Direct local hinge assignment, independent of the outer ring.
innerPivot.rotation.y = THREE.MathUtils.degToRad(degrees);
innerValue.textContent = formatSignedDegrees(degrees);
});
wheelSpeedInput.addEventListener("input", () => {
targetWheelSpeed = Number(wheelSpeedInput.value);
driveEnabled = targetWheelSpeed > 0;
runButton.classList.toggle("active", driveEnabled);
speedValue.textContent = `${targetWheelSpeed.toFixed(2)} rad/s`;
});
runButton.addEventListener("click", () => {
if (Number(wheelSpeedInput.value) <= 0) {
wheelSpeedInput.value = "1.8";
}
driveEnabled = true;
targetWheelSpeed = Number(wheelSpeedInput.value);
runButton.classList.add("active");
speedValue.textContent = `${targetWheelSpeed.toFixed(2)} rad/s`;
});
stopButton.addEventListener("click", () => {
driveEnabled = false;
targetWheelSpeed = 0;
wheelSpeedInput.value = "0";
runButton.classList.remove("active");
speedValue.textContent = "0.00 rad/s";
});
resetRingsButton.addEventListener("click", () => {
outerAngleInput.value = "0";
innerAngleInput.value = "0";
outerPivot.rotation.x = 0;
innerPivot.rotation.y = 0;
outerValue.textContent = "0°";
innerValue.textContent = "0°";
});
// -------------------------------------------------------------------------
// Camera controls implemented directly, without an examples/addons import
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, -0.18, 0),
radius: 10.2,
theta: 0.72,
phi: 1.03
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 10.2;
cameraState.theta = 0.72;
cameraState.phi = 1.03;
cameraState.target.set(0, -0.18, 0);
updateCamera();
}
resetCamera();
resetViewButton.addEventListener("click", resetCamera);
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.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.007;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.006,
0.18,
Math.PI - 0.18
);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.2,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (driveEnabled) {
const response = 5.0;
actualWheelSpeed +=
(targetWheelSpeed - actualWheelSpeed) *
(1 - Math.exp(-response * delta));
} else {
// Removing the drive causes the visible wheel to coast and settle.
const response = 3.0;
actualWheelSpeed +=
(targetWheelSpeed - actualWheelSpeed) *
(1 - Math.exp(-response * delta));
if (Math.abs(actualWheelSpeed) < 0.0015) {
actualWheelSpeed = 0;
}
}
wheelAngle += actualWheelSpeed * delta;
wheelSpin.rotation.z = wheelAngle;
if (driveEnabled) {
wheelStatus.textContent =
`Wheel running at ${actualWheelSpeed.toFixed(2)} rad/s. ` +
`Ring angles remain independently set.`;
} else if (actualWheelSpeed > 0) {
wheelStatus.textContent =
`Drive stopped. Coasting to rest at ${actualWheelSpeed.toFixed(2)} rad/s.`;
} else {
wheelStatus.textContent =
"Wheel stopped and at rest. The ring angles remain unchanged.";
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```