Prompt specification
Build or customize the Three.js 3D scene "Caster wheel brake mechanism" 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: Caster wheel brake mechanism
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/caster-wheel-brake-mechanism-x0197
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a swiveling caster with fork, wheel axle, and brake lever; expose the lever's contact with the wheel so locked and released positions are distinguishable. Voxel style.
Working Three.js Starter (caster-wheel-brake-mechanism-x0197):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Swivel Caster</title>
<style>
:root {
--ink: #17252f;
--muted: #60717d;
--panel: rgba(247, 250, 249, 0.9);
--border: rgba(23, 37, 47, 0.13);
--blue: #587b9b;
--orange: #f47b3c;
--green: #178b62;
--amber: #c87816;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce5e5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 14px 36px rgba(29, 48, 59, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.intro {
top: 22px;
left: 22px;
width: min(355px, calc(100vw - 44px));
padding: 18px 20px;
}
.eyebrow {
margin: 0 0 6px;
color: var(--blue);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.controls {
top: 22px;
right: 22px;
width: 275px;
padding: 16px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.status-label {
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-badge {
display: inline-flex;
align-items: center;
gap: 7px;
min-width: 94px;
justify-content: center;
padding: 6px 9px;
border-radius: 999px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.06em;
transition: background 160ms ease, color 160ms ease;
}
.status-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
content: "";
background: currentColor;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.55);
}
.status-badge.locked {
color: var(--green);
background: rgba(23, 139, 98, 0.12);
}
.status-badge.released {
color: var(--amber);
background: rgba(200, 120, 22, 0.13);
}
.state-detail {
min-height: 36px;
margin: 0 0 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
button {
width: 100%;
min-height: 42px;
border: 0;
border-radius: 10px;
padding: 10px 13px;
color: #fff;
background: var(--ink);
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
background: #263d4b;
box-shadow: 0 6px 14px rgba(23, 37, 47, 0.16);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 3px solid rgba(88, 123, 155, 0.35);
outline-offset: 2px;
}
#swivelButton {
margin-top: 8px;
color: var(--ink);
background: rgba(88, 123, 155, 0.13);
}
#swivelButton:hover {
background: rgba(88, 123, 155, 0.2);
}
.legend {
left: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 14px;
padding: 11px 14px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.help {
right: 22px;
bottom: 22px;
padding: 10px 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.contact-label {
position: fixed;
z-index: 8;
left: 50%;
top: 50%;
min-width: 154px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 9px;
color: #fff;
background: rgba(23, 139, 98, 0.92);
box-shadow: 0 8px 22px rgba(23, 37, 47, 0.18);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.075em;
line-height: 1.25;
text-align: center;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, calc(-100% - 17px));
transition: background 160ms ease;
}
.contact-label::after {
position: absolute;
left: 50%;
top: 100%;
width: 2px;
height: 15px;
content: "";
background: currentColor;
opacity: 0.75;
transform: translateX(-50%);
}
.contact-label.released {
background: rgba(200, 120, 22, 0.94);
}
@media (max-width: 700px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 58px;
width: 220px;
padding: 12px;
}
.state-detail {
display: none;
}
.legend {
left: 12px;
bottom: 12px;
gap: 9px;
}
.help {
display: none;
}
.contact-label {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="panel intro">
<p class="eyebrow">Mechanism Study 01</p>
<h1>Voxel Swivel Caster</h1>
<p class="subtitle">
Drag to orbit, scroll or pinch to zoom, and click the orange brake
to inspect its contact with the wheel.
</p>
</header>
<section class="panel controls" aria-label="Caster controls">
<div class="status-row">
<span class="status-label">Brake state</span>
<span id="brakeState" class="status-badge locked" aria-live="polite">
Locked
</span>
</div>
<p id="stateDetail" class="state-detail">
The brake shoe is touching the tire crown.
</p>
<button id="brakeButton" type="button" aria-pressed="true">
Release brake
</button>
<button id="swivelButton" type="button" aria-pressed="true">
Pause swivel
</button>
</section>
<div class="panel legend" aria-label="Component colors">
<span><i class="swatch" style="background:#25313b"></i>Tire</span>
<span><i class="swatch" style="background:#587b9b"></i>Fork</span>
<span><i class="swatch" style="background:#f47b3c"></i>Brake</span>
<span><i class="swatch" style="background:#a8b5ba"></i>Axle</span>
</div>
<div class="panel help">
Drag: orbit · Wheel/pinch: zoom<br>
Click brake: toggle · B: brake · S: swivel
</div>
<div id="contactLabel" class="contact-label">
Locked · Shoe contact
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce5e5);
scene.fog = new THREE.Fog(0xdce5e5, 11, 22);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf5fbff, 0x718074, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.6);
keyLight.position.set(5.5, 8.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xbfdfff, 1.8);
rimLight.position.set(-5, 4.5, -4);
scene.add(rimLight);
const fillLight = new THREE.DirectionalLight(0xffffff, 0.75);
fillLight.position.set(0, 2, 6);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Shared geometry and materials
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materials = {
fork: new THREE.MeshStandardMaterial({
color: 0x587b9b,
roughness: 0.72,
metalness: 0.12
}),
forkLight: new THREE.MeshStandardMaterial({
color: 0x7192ad,
roughness: 0.68,
metalness: 0.12
}),
forkDark: new THREE.MeshStandardMaterial({
color: 0x3f607e,
roughness: 0.7,
metalness: 0.2
}),
orange: new THREE.MeshStandardMaterial({
color: 0xf47b3c,
roughness: 0.52,
metalness: 0.14
}),
orangeDark: new THREE.MeshStandardMaterial({
color: 0xb94b24,
roughness: 0.55,
metalness: 0.2
}),
steel: new THREE.MeshStandardMaterial({
color: 0xa8b5ba,
roughness: 0.32,
metalness: 0.78
}),
steelDark: new THREE.MeshStandardMaterial({
color: 0x64747d,
roughness: 0.38,
metalness: 0.72
}),
white: new THREE.MeshStandardMaterial({
color: 0xe8efee,
roughness: 0.66,
metalness: 0.04
}),
tire: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.93,
metalness: 0.02
})
};
const ledMaterial = new THREE.MeshStandardMaterial({
color: 0x44d79a,
emissive: 0x178b62,
emissiveIntensity: 1.8,
roughness: 0.35
});
function box(parent, size, position, material, name = "") {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.name = name;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel tile floor
// -------------------------------------------------------------------------
const floor = new THREE.Group();
scene.add(floor);
const tileCount = 17;
const tileSpacing = 0.62;
const tileGeometry = new THREE.BoxGeometry(0.585, 0.075, 0.585);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0
});
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCount * tileCount
);
tiles.receiveShadow = true;
tiles.castShadow = false;
const tileDummy = new THREE.Object3D();
const floorColors = [
new THREE.Color(0xc8d3d1),
new THREE.Color(0xd2dbd9),
new THREE.Color(0xbfcdcb)
];
let tileIndex = 0;
for (let x = 0; x < tileCount; x++) {
for (let z = 0; z < tileCount; z++) {
const px = (x - (tileCount - 1) / 2) * tileSpacing;
const pz = (z - (tileCount - 1) / 2) * tileSpacing;
tileDummy.position.set(px, -0.047, pz);
tileDummy.rotation.set(0, 0, 0);
tileDummy.updateMatrix();
tiles.setMatrixAt(tileIndex, tileDummy.matrix);
const shade = (x * 3 + z * 5) % 11;
const color = floorColors[shade === 0 ? 2 : shade === 1 ? 1 : 0];
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
floor.add(tiles);
// A low voxel platform makes the mechanism feel like a product study.
box(
floor,
[3.3, 0.11, 3.3],
[0, -0.115, 0],
new THREE.MeshStandardMaterial({
color: 0xaebfbe,
roughness: 0.9
}),
"Display platform"
);
// -------------------------------------------------------------------------
// Caster assembly
// -------------------------------------------------------------------------
const caster = new THREE.Group();
scene.add(caster);
// Fixed mounting plate stays horizontal while the lower assembly swivels.
box(caster, [0.98, 0.16, 0.98], [0, 2.77, 0], materials.fork, "Fixed top plate");
const boltPositions = [
[-0.34, -0.34],
[0.34, -0.34],
[-0.34, 0.34],
[0.34, 0.34]
];
for (const [x, z] of boltPositions) {
box(
caster,
[0.13, 0.09, 0.13],
[x, 2.89, z],
materials.steel,
"Mounting bolt"
);
}
const swivel = new THREE.Group();
swivel.name = "Swiveling fork and wheel";
caster.add(swivel);
// Rotating bearing and square stem.
box(swivel, [0.36, 0.33, 0.38], [0, 2.42, 0], materials.steelDark, "Swivel stem");
box(swivel, [0.64, 0.22, 0.64], [0, 2.58, 0], materials.forkDark, "Bearing housing");
// Visible index marker makes the swivel motion easy to read.
box(swivel, [0.13, 0.17, 0.1], [0, 2.59, 0.37], materials.orange, "Swivel index");
// Fork bridge.
box(swivel, [0.74, 0.3, 1.18], [0, 2.19, 0], materials.fork, "Fork bridge");
// Stepped voxel fork legs, with the wheel trailing slightly behind the axis.
const legLevels = [
[-0.18, 0.76],
[-0.12, 1.08],
[-0.06, 1.4],
[0.0, 1.72],
[0.0, 2.04]
];
for (const side of [-1, 1]) {
const z = side * 0.46;
legLevels.forEach(([x, y], index) => {
box(
swivel,
[0.34, 0.3, 0.24],
[x, y, z],
index === 0 ? materials.forkDark : materials.fork,
side < 0 ? "Rear fork leg" : "Front fork leg"
);
});
// Square axle bearing and cap.
box(
swivel,
[0.39, 0.39, 0.28],
[-0.18, 0.74, z],
materials.forkDark,
"Axle bearing block"
);
box(
swivel,
[0.23, 0.23, 0.12],
[-0.18, 0.74, side * 0.64],
materials.steel,
"Axle cap"
);
}
// -------------------------------------------------------------------------
// Voxel wheel
// -------------------------------------------------------------------------
const wheelX = -0.18;
const wheelY = 0.74;
const wheelZ = 0;
// Annular tire assembled from individual-looking voxel blocks.
const tireBlocks = [];
const spacing = 0.12;
for (let ix = -6; ix <= 6; ix++) {
for (let iy = -6; iy <= 6; iy++) {
const x = ix * spacing;
const y = iy * spacing;
const radius = Math.hypot(x, y);
if (radius >= 0.36 && radius <= 0.66) {
tireBlocks.push({ ix, iy, x, y });
}
}
}
const tire = new THREE.InstancedMesh(
unitBox,
materials.tire,
tireBlocks.length * 3
);
tire.castShadow = true;
tire.receiveShadow = true;
const tireDummy = new THREE.Object3D();
const tireColorA = new THREE.Color(0x202b34);
const tireColorB = new THREE.Color(0x2b3842);
const tireColorC = new THREE.Color(0x182129);
let tireIndex = 0;
for (const block of tireBlocks) {
for (let layer = 0; layer < 3; layer++) {
const z = (layer - 1) * 0.15;
tireDummy.position.set(
wheelX + block.x,
wheelY + block.y,
wheelZ + z
);
tireDummy.rotation.set(0, 0, 0);
tireDummy.scale.set(0.112, 0.112, 0.142);
tireDummy.updateMatrix();
tire.setMatrixAt(tireIndex, tireDummy.matrix);
const shade = (block.ix + block.iy + layer * 2) % 7;
tire.setColorAt(
tireIndex,
shade === 0 ? tireColorC : shade === 1 ? tireColorB : tireColorA
);
tireIndex++;
}
}
tire.instanceMatrix.needsUpdate = true;
tire.instanceColor.needsUpdate = true;
swivel.add(tire);
// Orange hub and cardinal spokes.
box(swivel, [0.38, 0.38, 0.42], [wheelX, wheelY, 0], materials.orange, "Hub");
box(swivel, [0.68, 0.14, 0.34], [wheelX, wheelY, 0], materials.orange, "Horizontal spoke");
box(swivel, [0.14, 0.68, 0.34], [wheelX, wheelY, 0], materials.orange, "Vertical spoke");
// Square metal axle projects beyond both fork legs.
box(
swivel,
[0.14, 0.14, 1.66],
[wheelX, wheelY, 0],
materials.steel,
"Wheel axle"
);
box(swivel, [0.22, 0.22, 0.14], [wheelX, wheelY, 0.86], materials.steelDark, "Front axle end");
box(swivel, [0.22, 0.22, 0.14], [wheelX, wheelY, -0.86], materials.steelDark, "Rear axle end");
// -------------------------------------------------------------------------
// Exposed brake lever
// -------------------------------------------------------------------------
const LOCKED_ANGLE = 0.32;
const RELEASED_ANGLE = -0.30;
// Bracket connects the brake pivot to the front fork leg.
box(
swivel,
[0.55, 0.18, 0.22],
[0.22, 1.73, 0.47],
materials.forkDark,
"Brake pivot bracket"
);
const lever = new THREE.Group();
lever.name = "Brake lever";
lever.position.set(0.45, 1.73, 0.47);
lever.rotation.z = LOCKED_ANGLE;
swivel.add(lever);
box(lever, [0.27, 0.27, 0.25], [0, 0, 0], materials.steelDark, "Brake pivot");
// Voxel arm: the left end lifts when the brake is released.
[-0.18, -0.38, -0.58].forEach((x, index) => {
box(
lever,
[0.25, 0.13, 0.16],
[x, 0, 0],
index === 2 ? materials.orangeDark : materials.orange,
"Brake arm block"
);
});
// The shoe extends inward in Z so its contact with the tire is visible.
const brakeShoe = box(
lever,
[0.25, 0.12, 0.46],
[-0.79, -0.025, -0.16],
materials.orange,
"Brake contact shoe"
);
// Small raised grip gives the lever a readable silhouette.
box(
lever,
[0.24, 0.09, 0.19],
[-0.43, 0.105, 0],
materials.orangeDark,
"Brake grip"
);
lever.traverse((object) => {
object.userData.isBrakePart = true;
});
// Indicator LED mounted near the pivot.
const led = box(
swivel,
[0.12, 0.12, 0.1],
[0.49, 1.98, 0.5],
ledMaterial,
"Brake state indicator"
);
// Anchor used by the HTML callout. It follows the moving brake shoe.
const contactAnchor = new THREE.Object3D();
contactAnchor.position.set(-0.79, -0.09, -0.15);
lever.add(contactAnchor);
// -------------------------------------------------------------------------
// Interface state
// -------------------------------------------------------------------------
const brakeButton = document.querySelector("#brakeButton");
const swivelButton = document.querySelector("#swivelButton");
const brakeState = document.querySelector("#brakeState");
const stateDetail = document.querySelector("#stateDetail");
const contactLabel = document.querySelector("#contactLabel");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let brakeLocked = true;
let autoSwivel = !reducedMotion;
let swivelAngle = -0.18;
function setBrakeUI() {
if (brakeLocked) {
brakeState.textContent = "Locked";
brakeState.className = "status-badge locked";
stateDetail.textContent = "The brake shoe is touching the tire crown.";
contactLabel.textContent = "Locked · Shoe contact";
contactLabel.className = "contact-label";
ledMaterial.color.setHex(0x44d79a);
ledMaterial.emissive.setHex(0x178b62);
ledMaterial.emissiveIntensity = 1.8;
brakeButton.textContent = "Release brake";
brakeButton.setAttribute("aria-pressed", "true");
} else {
brakeState.textContent = "Released";
brakeState.className = "status-badge released";
stateDetail.textContent = "The shoe is lifted, leaving a visible gap above the tire.";
contactLabel.textContent = "Released · Clear gap";
contactLabel.className = "contact-label released";
ledMaterial.color.setHex(0xffb34d);
ledMaterial.emissive.setHex(0xc87816);
ledMaterial.emissiveIntensity = 1.45;
brakeButton.textContent = "Lock brake";
brakeButton.setAttribute("aria-pressed", "false");
}
}
function toggleBrake() {
brakeLocked = !brakeLocked;
setBrakeUI();
}
function setSwivelUI() {
swivelButton.textContent = autoSwivel ? "Pause swivel" : "Start swivel";
swivelButton.setAttribute("aria-pressed", String(autoSwivel));
}
function toggleSwivel() {
autoSwivel = !autoSwivel;
setSwivelUI();
}
brakeButton.addEventListener("click", toggleBrake);
swivelButton.addEventListener("click", toggleSwivel);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "b") {
toggleBrake();
}
if (event.key.toLowerCase() === "s") {
toggleSwivel();
}
});
setBrakeUI();
setSwivelUI();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and pointer picking
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.43, 0);
let cameraTheta = 0.62;
let cameraPhi = 1.16;
let cameraRadius = 6.35;
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function updateCamera() {
const horizontalRadius = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontalRadius * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function pickBrake(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObject(lever, true);
if (hits.length > 0) {
toggleBrake();
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
pointerMoved = true;
}
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.5,
1.43
);
previousX = event.clientX;
previousY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
pickBrake(event);
}
pointerDown = false;
renderer.domElement.releasePointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.0045,
4.15,
10
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
const projectedContact = new THREE.Vector3();
const worldContact = new THREE.Vector3();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (autoSwivel) {
swivelAngle += delta * 0.58;
}
swivel.rotation.y = swivelAngle;
const targetBrakeAngle = brakeLocked ? LOCKED_ANGLE : RELEASED_ANGLE;
lever.rotation.z = THREE.MathUtils.damp(
lever.rotation.z,
targetBrakeAngle,
10,
delta
);
updateCamera();
// Project the moving brake shoe into screen space for the callout.
contactAnchor.getWorldPosition(worldContact);
projectedContact.copy(worldContact).project(camera);
const screenX =
(projectedContact.x * 0.5 + 0.5) * window.innerWidth;
const screenY =
(-projectedContact.y * 0.5 + 0.5) * window.innerHeight;
contactLabel.style.left = `${screenX}px`;
contactLabel.style.top = `${screenY}px`;
contactLabel.style.opacity =
projectedContact.z > -1 && projectedContact.z < 1 ? "1" : "0";
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```