Prompt specification
Build or customize the Three.js 3D scene "Gyroscope vector relationship explorer" 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: Gyroscope vector relationship explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/gyroscope-vector-relationship-explorer-x1396
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a gyroscope and its torque response. Allow changing torque direction in a vector-based teaching model; show the corresponding angular-momentum change without presenting the visualization as a full rigid-body dynamics solver. Voxel style.
Working Three.js Starter (gyroscope-vector-relationship-explorer-x1396):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Gyroscope — Torque and Angular-Momentum Teaching Model</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(10, 23, 39, 0.88);
--panel-border: rgba(125, 211, 252, 0.20);
--text: #e7f3ff;
--muted: #91a9bd;
--cyan: #42d9ff;
--orange: #ff9b42;
--gold: #ffe066;
--violet: #b58cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.header {
position: fixed;
z-index: 5;
top: 22px;
left: 24px;
max-width: min(590px, calc(100vw - 410px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.75);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 3px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(66, 217, 255, 0.75);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.03;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: #b8cada;
font-size: 13px;
line-height: 1.45;
}
.model-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(255, 155, 66, 0.38);
border-radius: 7px;
background: rgba(41, 24, 14, 0.74);
color: #ffc88f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.10em;
text-transform: uppercase;
}
.model-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
right: 20px;
width: 350px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 18px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.42),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #34516a transparent;
}
.panel h2 {
margin: 0 0 4px;
font-size: 16px;
letter-spacing: -0.015em;
}
.panel-intro {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.section {
padding-top: 15px;
margin-top: 15px;
border-top: 1px solid rgba(148, 183, 210, 0.13);
}
.section-title {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 11px;
color: #d8e9f7;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-size: 10px;
font-weight: 500;
letter-spacing: 0;
text-transform: none;
}
.control {
margin-bottom: 14px;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 7px;
color: #c9dbe9;
font-size: 12px;
}
output {
color: #fff;
font-variant-numeric: tabular-nums;
font-weight: 750;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 10px;
outline: none;
appearance: none;
background: #20384d;
accent-color: var(--cyan);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #dff8ff;
border-radius: 3px;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 14px rgba(66, 217, 255, 0.48);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #dff8ff;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(66, 217, 255, 0.48);
}
#mag {
accent-color: var(--orange);
}
#mag::-webkit-slider-thumb {
background: var(--orange);
box-shadow: 0 0 14px rgba(255, 155, 66, 0.45);
}
#mag::-moz-range-thumb {
background: var(--orange);
}
#dt {
accent-color: var(--gold);
}
#dt::-webkit-slider-thumb {
background: var(--gold);
box-shadow: 0 0 14px rgba(255, 224, 102, 0.40);
}
#dt::-moz-range-thumb {
background: var(--gold);
}
.direction-row {
display: grid;
grid-template-columns: 28px 1fr 48px;
gap: 9px;
align-items: center;
margin: 10px 0;
}
.direction-row label {
color: #d8e8f5;
font-size: 12px;
font-weight: 800;
}
.direction-row output {
color: var(--muted);
font-size: 11px;
text-align: right;
}
.quick-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 5px;
margin-top: 12px;
}
button {
min-height: 31px;
border: 1px solid rgba(146, 188, 217, 0.22);
border-radius: 7px;
background: rgba(38, 61, 81, 0.7);
color: #dcecfa;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: 120ms ease;
}
button:hover {
border-color: rgba(66, 217, 255, 0.65);
background: rgba(49, 85, 111, 0.84);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.equation-card {
padding: 12px;
border: 1px solid rgba(130, 181, 214, 0.13);
border-radius: 10px;
background: rgba(4, 14, 25, 0.48);
font-variant-numeric: tabular-nums;
}
.equation-line {
display: grid;
grid-template-columns: 12px 34px 1fr;
gap: 7px;
align-items: baseline;
margin: 7px 0;
color: #b8cbd9;
font-size: 11px;
line-height: 1.35;
}
.equation-line:first-child {
margin-top: 0;
}
.equation-line:last-child {
margin-bottom: 0;
}
.equation-line strong {
color: #f4f9fd;
}
.swatch {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 2px;
transform: translateY(1px);
box-shadow: 0 0 9px currentColor;
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.swatch.orange {
color: var(--orange);
background: var(--orange);
}
.swatch.gold {
color: var(--gold);
background: var(--gold);
}
.swatch.violet {
color: var(--violet);
background: var(--violet);
}
.toggle-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px 12px;
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
color: #bdcfdd;
font-size: 11px;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
margin: 0;
accent-color: var(--cyan);
}
.info-card {
position: fixed;
z-index: 5;
left: 24px;
bottom: 22px;
width: min(355px, calc(100vw - 48px));
padding: 15px 17px;
border: 1px solid rgba(123, 174, 210, 0.16);
border-radius: 13px;
background: rgba(7, 19, 33, 0.76);
box-shadow: 0 15px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
.info-card h3 {
margin: 0 0 8px;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.info-card p {
margin: 6px 0 0;
color: #a9bfce;
font-size: 11px;
line-height: 1.52;
}
.info-card .warning {
padding-top: 8px;
border-top: 1px solid rgba(144, 181, 208, 0.12);
color: #e5bd8d;
}
.hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(119, 172, 207, 0.15);
border-radius: 8px;
background: rgba(6, 16, 28, 0.65);
color: #91a9bd;
font-size: 10px;
white-space: nowrap;
pointer-events: none;
}
#labels {
position: fixed;
z-index: 4;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.vector-label {
position: absolute;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 6px;
background: rgba(4, 13, 24, 0.84);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.36);
font-size: 11px;
font-weight: 800;
white-space: nowrap;
transform: translate(-50%, -50%);
transition: opacity 120ms ease;
}
#label-l0 {
color: var(--cyan);
}
#label-tau {
color: var(--orange);
}
#label-dl {
color: var(--gold);
}
#label-l1 {
color: var(--violet);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #07111e;
color: #9ec1d7;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 900px) {
.header {
max-width: calc(100vw - 48px);
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: 340px;
max-height: 54vh;
}
.info-card,
.hint {
display: none;
}
}
@media (max-width: 560px) {
.header {
top: 15px;
left: 15px;
}
.subtitle {
max-width: 320px;
}
.model-badge {
font-size: 8px;
}
.panel {
right: 8px;
bottom: 8px;
left: 8px;
width: auto;
max-height: 47vh;
padding: 14px;
}
.panel-intro {
margin-bottom: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel gyroscope vector visualization"></canvas>
<div id="labels" aria-hidden="true">
<div class="vector-label" id="label-l0">L₀</div>
<div class="vector-label" id="label-tau">τ</div>
<div class="vector-label" id="label-dl">ΔL</div>
<div class="vector-label" id="label-l1">L₁</div>
</div>
<header class="header">
<div class="eyebrow">Vector teaching lab</div>
<h1>Gyroscope: Torque → ΔL</h1>
<p class="subtitle">
Change the orange torque vector and inspect the corresponding
angular-momentum change over a selected interval.
</p>
<div class="model-badge">Vector snapshot · not a rigid-body solver</div>
</header>
<aside class="panel">
<h2>Torque controls</h2>
<p class="panel-intro">
Drag the orange arrowhead in the scene, or edit the direction components below.
The arrow uses the normalized direction.
</p>
<div class="control">
<div class="control-label">
<span>Torque magnitude |τ|</span>
<output id="magOut">5.0 N·m</output>
</div>
<input id="mag" type="range" min="0" max="10" step="0.1" value="5">
</div>
<div class="control">
<div class="control-label">
<span>Selected interval Δt</span>
<output id="dtOut">0.35 s</output>
</div>
<input id="dt" type="range" min="0.20" max="1.00" step="0.05" value="0.35">
</div>
<div class="section">
<div class="section-title">
Torque direction vector
<span id="dirReadout">(1.00, 0.00, 0.00)</span>
</div>
<div class="direction-row">
<label for="dirX">τx</label>
<input id="dirX" type="range" min="-1" max="1" step="0.01" value="1">
<output id="dirXOut">1.00</output>
</div>
<div class="direction-row">
<label for="dirY">τy</label>
<input id="dirY" type="range" min="-1" max="1" step="0.01" value="0">
<output id="dirYOut">0.00</output>
</div>
<div class="direction-row">
<label for="dirZ">τz</label>
<input id="dirZ" type="range" min="-1" max="1" step="0.01" value="0">
<output id="dirZOut">0.00</output>
</div>
<div class="quick-grid" aria-label="Torque direction presets">
<button type="button" data-dir="1,0,0">+X</button>
<button type="button" data-dir="-1,0,0">−X</button>
<button type="button" data-dir="0,1,0">+Y</button>
<button type="button" data-dir="0,-1,0">−Y</button>
<button type="button" data-dir="0,0,1">+Z</button>
<button type="button" data-dir="0,0,-1">−Z</button>
</div>
</div>
<div class="section">
<div class="section-title">
Vector snapshot
<span>same spatial scale</span>
</div>
<div class="equation-card" aria-live="polite">
<div class="equation-line">
<i class="swatch cyan"></i>
<strong>L₀</strong>
<span>(8.00, 0.00, 0.00) N·m·s</span>
</div>
<div class="equation-line">
<i class="swatch orange"></i>
<strong>τ</strong>
<span id="tauValue">(5.00, 0.00, 0.00) N·m</span>
</div>
<div class="equation-line">
<i class="swatch gold"></i>
<strong>ΔL</strong>
<span id="dlValue">(1.75, 0.00, 0.00) N·m·s</span>
</div>
<div class="equation-line">
<i class="swatch violet"></i>
<strong>L₁</strong>
<span id="l1Value">(9.75, 0.00, 0.00) N·m·s</span>
</div>
</div>
</div>
<div class="section">
<div class="section-title">Visible vectors</div>
<div class="toggle-grid">
<label class="toggle"><input id="showL0" type="checkbox" checked> L₀</label>
<label class="toggle"><input id="showTau" type="checkbox" checked> τ</label>
<label class="toggle"><input id="showDL" type="checkbox" checked> ΔL</label>
<label class="toggle"><input id="showL1" type="checkbox" checked> L₁</label>
</div>
<div class="button-row">
<button id="resetVectors" type="button">Reset vectors</button>
<button id="resetCamera" type="button">Reset camera</button>
</div>
</div>
</aside>
<section class="info-card">
<h3>How to read the model</h3>
<p>
The cyan vector is the reference angular momentum L₀. The orange vector is
the selected torque τ. For one constant-torque interval,
<strong>ΔL = τΔt</strong>, shown in gold from the tip of L₀. Violet shows
the geometric result L₁ = L₀ + ΔL.
</p>
<p class="warning">
This is a first-order vector teaching model. It does not solve Euler’s
equations, the inertia tensor, gyroscopic precession, nutation, or evolving
body orientation. The visible rotor spin is decorative.
</p>
</section>
<div class="hint">
Drag orange arrowhead to set τ · Drag empty space to orbit · Scroll to zoom
</div>
<div id="loading">Loading voxel gyroscope…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.Fog(0x07111e, 14, 29);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 1.85, 0);
let cameraTheta = 0.80;
let cameraPhi = 1.12;
let cameraRadius = 10.8;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0x9bdcff, 0x142033, 1.55));
const keyLight = new THREE.DirectionalLight(0xd8f2ff, 3.1);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xff9b42, 1.65);
rimLight.position.set(-7, 4, -5);
scene.add(rimLight);
const fillLight = new THREE.PointLight(0x4acfff, 18, 14, 2);
fillLight.position.set(0, 4, -3);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Voxel geometry helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.16,
...options
});
}
function addBlock(parent, size, position, material, castShadow = true) {
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 = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Instanced voxel floor.
const tileGeometry = new THREE.BoxGeometry(0.68, 0.09, 0.68);
const tileMaterial = standardMaterial(0xffffff, { roughness: 0.95 });
const tileCount = 15 * 15;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const tileDummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let gx = -7; gx <= 7; gx++) {
for (let gz = -7; gz <= 7; gz++) {
tileDummy.position.set(gx * 0.72, -0.08, gz * 0.72);
tileDummy.updateMatrix();
tiles.setMatrixAt(tileIndex, tileDummy.matrix);
const stripe = (gx + gz) % 2 === 0;
const edge = Math.max(Math.abs(gx), Math.abs(gz));
tileColor.setHex(edge >= 6 ? 0x102438 : stripe ? 0x10283a : 0x0c2132);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// Static block-built support frame.
const frameMaterial = standardMaterial(0x29465c, { metalness: 0.35 });
const frameDarkMaterial = standardMaterial(0x152b3d, { metalness: 0.42 });
const frameTrimMaterial = standardMaterial(0x47758e, { metalness: 0.55 });
const brassMaterial = standardMaterial(0xe6ad45, {
metalness: 0.65,
roughness: 0.36
});
addBlock(scene, [5.1, 0.34, 1.55], [0, 0.12, 0], frameDarkMaterial);
addBlock(scene, [4.65, 0.12, 1.20], [0, 0.34, 0], frameMaterial);
addBlock(scene, [4.20, 0.08, 0.12], [0, 0.44, 0.54], brassMaterial);
addBlock(scene, [4.20, 0.08, 0.12], [0, 0.44, -0.54], brassMaterial);
// Center pedestal.
for (let i = 0; i < 4; i++) {
addBlock(
scene,
[0.62 - i * 0.04, 0.30, 0.62 - i * 0.04],
[0, 0.57 + i * 0.31, 0],
i % 2 ? frameMaterial : frameTrimMaterial
);
}
addBlock(scene, [0.90, 0.22, 0.85], [0, 1.37, 0], frameDarkMaterial);
// Two voxel towers and bearing housings.
for (const side of [-1, 1]) {
for (let i = 0; i < 5; i++) {
addBlock(
scene,
[0.52, 0.30, 0.56],
[side * 2.03, 0.52 + i * 0.31, 0],
i % 2 ? frameMaterial : frameTrimMaterial
);
}
addBlock(
scene,
[0.76, 0.28, 0.48],
[side * 1.76, 1.88, 0],
frameDarkMaterial
);
addBlock(
scene,
[0.50, 0.58, 0.66],
[side * 1.98, 2.25, 0],
frameMaterial
);
addBlock(
scene,
[0.18, 0.31, 0.48],
[side * 1.70, 2.25, 0],
brassMaterial
);
}
// -------------------------------------------------------------------------
// Voxel flywheel
// -------------------------------------------------------------------------
const pivot = new THREE.Vector3(0, 2.25, 0);
const rotorSpin = new THREE.Group();
rotorSpin.position.copy(pivot);
scene.add(rotorSpin);
const rimMaterial = standardMaterial(0x168da3, {
metalness: 0.48,
roughness: 0.4,
emissive: 0x062b33,
emissiveIntensity: 0.45
});
const rimAltMaterial = standardMaterial(0x25b6c8, {
metalness: 0.5,
roughness: 0.38,
emissive: 0x073b45,
emissiveIntensity: 0.55
});
const spokeMaterial = standardMaterial(0x49677b, {
metalness: 0.55,
roughness: 0.44
});
const axleMaterial = standardMaterial(0xe7b14d, {
metalness: 0.72,
roughness: 0.3
});
const markerMaterial = standardMaterial(0xff8b38, {
emissive: 0xff6a00,
emissiveIntensity: 0.65,
roughness: 0.35
});
const wheelRadius = 1.18;
// Pixelated outer rim.
for (let i = 0; i < 24; i++) {
const angle = (i / 24) * Math.PI * 2;
const material = i === 0 ? markerMaterial : i % 3 === 0 ? rimAltMaterial : rimMaterial;
addBlock(
rotorSpin,
[0.42, 0.29, 0.29],
[0, Math.cos(angle) * wheelRadius, Math.sin(angle) * wheelRadius],
material
);
}
// Pixelated inner rim.
for (let i = 0; i < 16; i++) {
const angle = (i / 16) * Math.PI * 2 + Math.PI / 16;
addBlock(
rotorSpin,
[0.34, 0.20, 0.20],
[0, Math.cos(angle) * 0.78, Math.sin(angle) * 0.78],
i % 2 ? rimAltMaterial : rimMaterial
);
}
// Blocky spokes.
for (let spoke = 0; spoke < 8; spoke++) {
const angle = (spoke / 8) * Math.PI * 2;
for (let step = 1; step <= 5; step++) {
const radius = step * 0.155;
addBlock(
rotorSpin,
[0.18, 0.20, 0.20],
[0, Math.cos(angle) * radius, Math.sin(angle) * radius],
spokeMaterial
);
}
}
// Central hub.
for (let i = -2; i <= 2; i++) {
addBlock(
rotorSpin,
[0.29, 0.64, 0.64],
[i * 0.26, 0, 0],
i % 2 ? frameTrimMaterial : axleMaterial
);
}
// Long block-built axle.
for (let i = 0; i < 17; i++) {
const x = -1.92 + i * 0.24;
addBlock(
rotorSpin,
[0.22, 0.22, 0.22],
[x, 0, 0],
axleMaterial
);
}
// -------------------------------------------------------------------------
// Voxel vector arrows
// -------------------------------------------------------------------------
const AXIS_Y = new THREE.Vector3(0, 1, 0);
const VECTOR_SCALE = 0.28;
function createVoxelArrow(color) {
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.58,
roughness: 0.36,
metalness: 0.18,
transparent: true,
opacity: 0.98,
depthTest: false,
depthWrite: false
});
const group = new THREE.Group();
group.frustumCulled = false;
group.renderOrder = 20;
const shaft = new THREE.Mesh(unitBox, material);
shaft.renderOrder = 20;
group.add(shaft);
const head = new THREE.Group();
head.renderOrder = 21;
group.add(head);
const widths = [0.34, 0.27, 0.19, 0.11];
for (let i = 0; i < widths.length; i++) {
const block = new THREE.Mesh(unitBox, material);
block.scale.set(widths[i], 0.10, widths[i]);
block.position.y = 0.05 + i * 0.09;
block.renderOrder = 21;
head.add(block);
}
// Invisible oversized hit target for direct arrow dragging.
const hit = new THREE.Mesh(
unitBox,
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthTest: false,
depthWrite: false
})
);
hit.scale.set(0.56, 0.56, 0.56);
hit.renderOrder = 22;
group.add(hit);
scene.add(group);
return { group, shaft, head, hit, material };
}
const l0Arrow = createVoxelArrow(0x42d9ff);
const tauArrow = createVoxelArrow(0xff9b42);
const dlArrow = createVoxelArrow(0xffe066);
const l1Arrow = createVoxelArrow(0xb58cff);
function setArrow(arrow, origin, vector, enabled) {
const length = vector.length() * VECTOR_SCALE;
const visible = enabled && length > 0.001;
arrow.group.visible = visible;
arrow.hit.visible = visible;
if (!visible) return;
arrow.group.position.copy(origin);
arrow.group.quaternion.setFromUnitVectors(
AXIS_Y,
vector.clone().normalize()
);
const headLength = 0.38;
const shaftLength = Math.max(0.025, length - headLength);
arrow.shaft.scale.set(0.105, shaftLength, 0.105);
arrow.shaft.position.y = shaftLength * 0.5;
arrow.head.position.y = shaftLength;
arrow.hit.position.y = shaftLength + 0.18;
}
// -------------------------------------------------------------------------
// Teaching model state
// -------------------------------------------------------------------------
const state = {
direction: new THREE.Vector3(1, 0, 0),
magnitude: 5,
deltaTime: 0.35,
showL0: true,
showTau: true,
showDL: true,
showL1: true
};
const L0 = new THREE.Vector3(8, 0, 0);
const current = {
tau: new THREE.Vector3(),
dL: new THREE.Vector3(),
L1: new THREE.Vector3()
};
const labelAnchors = {
l0: new THREE.Vector3(),
tau: new THREE.Vector3(),
dl: new THREE.Vector3(),
l1: new THREE.Vector3()
};
function formatNumber(value) {
if (Math.abs(value) < 0.005) return "0.00";
return value.toFixed(2);
}
function formatVector(vector) {
return `(${formatNumber(vector.x)}, ${formatNumber(vector.y)}, ${formatNumber(vector.z)})`;
}
function updateReadouts() {
document.querySelector("#magOut").textContent =
`${state.magnitude.toFixed(1)} N·m`;
document.querySelector("#dtOut").textContent =
`${state.deltaTime.toFixed(2)} s`;
document.querySelector("#dirReadout").textContent =
`(${formatNumber(state.direction.x)}, ` +
`${formatNumber(state.direction.y)}, ` +
`${formatNumber(state.direction.z)})`;
document.querySelector("#tauValue").textContent =
`${formatVector(current.tau)} N·m`;
document.querySelector("#dlValue").textContent =
`${formatVector(current.dL)} N·m·s`;
document.querySelector("#l1Value").textContent =
`${formatVector(current.L1)} N·m·s`;
}
function updateArrows() {
current.tau.copy(state.direction).multiplyScalar(state.magnitude);
current.dL.copy(current.tau).multiplyScalar(state.deltaTime);
current.L1.copy(L0).add(current.dL);
setArrow(l0Arrow, pivot, L0, state.showL0);
setArrow(tauArrow, pivot, current.tau, state.showTau);
setArrow(dlArrow, pivot.clone().add(L0), current.dL, state.showDL);
setArrow(l1Arrow, pivot, current.L1, state.showL1);
labelAnchors.l0.copy(pivot).addScaledVector(L0, 0.69);
labelAnchors.tau.copy(pivot).addScaledVector(current.tau, 0.72);
labelAnchors.dl.copy(pivot).add(L0).addScaledVector(current.dL, 0.56);
labelAnchors.l1.copy(pivot).addScaledVector(current.L1, 0.71);
updateReadouts();
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const magInput = document.querySelector("#mag");
const dtInput = document.querySelector("#dt");
const dirXInput = document.querySelector("#dirX");
const dirYInput = document.querySelector("#dirY");
const dirZInput = document.querySelector("#dirZ");
magInput.addEventListener("input", () => {
state.magnitude = Number(magInput.value);
updateArrows();
});
dtInput.addEventListener("input", () => {
state.deltaTime = Number(dtInput.value);
updateArrows();
});
function updateDirectionSliders() {
dirXInput.value = state.direction.x.toFixed(2);
dirYInput.value = state.direction.y.toFixed(2);
dirZInput.value = state.direction.z.toFixed(2);
document.querySelector("#dirXOut").textContent =
formatNumber(state.direction.x);
document.querySelector("#dirYOut").textContent =
formatNumber(state.direction.y);
document.querySelector("#dirZOut").textContent =
formatNumber(state.direction.z);
}
function readDirectionSliders() {
const x = Number(dirXInput.value);
const y = Number(dirYInput.value);
const z = Number(dirZInput.value);
const vector = new THREE.Vector3(x, y, z);
if (vector.lengthSq() < 0.000001) {
state.direction.set(1, 0, 0);
dirXInput.value = 1;
} else {
state.direction.copy(vector.normalize());
}
document.querySelector("#dirXOut").textContent = formatNumber(x);
document.querySelector("#dirYOut").textContent = formatNumber(y);
document.querySelector("#dirZOut").textContent = formatNumber(z);
updateArrows();
}
dirXInput.addEventListener("input", readDirectionSliders);
dirYInput.addEventListener("input", readDirectionSliders);
dirZInput.addEventListener("input", readDirectionSliders);
document.querySelectorAll("[data-dir]").forEach((button) => {
button.addEventListener("click", () => {
const [x, y, z] = button.dataset.dir.split(",").map(Number);
state.direction.set(x, y, z).normalize();
updateDirectionSliders();
updateArrows();
});
});
for (const [id, key] of [
["showL0", "showL0"],
["showTau", "showTau"],
["showDL", "showDL"],
["showL1", "showL1"]
]) {
document.querySelector(`#${id}`).addEventListener("change", (event) => {
state[key] = event.target.checked;
updateArrows();
});
}
document.querySelector("#resetVectors").addEventListener("click", () => {
state.direction.set(1, 0, 0);
state.magnitude = 5;
state.deltaTime = 0.35;
magInput.value = 5;
dtInput.value = 0.35;
updateDirectionSliders();
updateArrows();
});
function resetCamera() {
cameraTheta = 0.80;
cameraPhi = 1.12;
cameraRadius = 10.8;
updateCamera();
}
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Direct manipulation: orbit, zoom, and torque-arrow dragging
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane();
const planePoint = new THREE.Vector3();
const planeNormal = new THREE.Vector3();
let pointerMode = null;
let activePointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function torqueHandleHit() {
const targets = [
tauArrow.hit,
...tauArrow.head.children
];
return raycaster.intersectObjects(targets, false).length > 0;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
activePointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updatePointer(event);
if (state.showTau && state.magnitude > 0 && torqueHandleHit()) {
pointerMode = "torque";
camera.getWorldDirection(planeNormal);
const hitPoint = raycaster.intersectObjects(
[tauArrow.hit, ...tauArrow.head.children],
false
)[0].point;
dragPlane.setFromNormalAndCoplanarPoint(planeNormal, hitPoint);
} else {
pointerMode = "orbit";
}
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = pointerMode === "torque" ? "crosshair" : "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (event.pointerId !== activePointerId) return;
if (pointerMode === "orbit") {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.28,
1.48
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else if (pointerMode === "torque") {
updatePointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planePoint)) {
const direction = planePoint.clone().sub(pivot);
if (direction.length() > 0.12) {
state.direction.copy(direction.normalize());
updateDirectionSliders();
updateArrows();
}
}
}
});
function releasePointer(event) {
if (event.pointerId !== activePointerId) return;
pointerMode = null;
activePointerId = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
6.0,
18.0
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Labels and animation
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, visible, offsetX, offsetY) {
if (!visible) {
element.style.opacity = "0";
return;
}
projected.copy(worldPosition).project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.25 ||
projected.x > 1.25 ||
projected.y < -1.25 ||
projected.y > 1.25
) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = "1";
}
function updateLabels() {
placeLabel(
document.querySelector("#label-l0"),
labelAnchors.l0,
state.showL0,
-10,
-19
);
placeLabel(
document.querySelector("#label-tau"),
labelAnchors.tau,
state.showTau && state.magnitude > 0,
7,
-18
);
placeLabel(
document.querySelector("#label-dl"),
labelAnchors.dl,
state.showDL && current.dL.length() > 0,
8,
-18
);
placeLabel(
document.querySelector("#label-l1"),
labelAnchors.l1,
state.showL1,
13,
-18
);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateDirectionSliders();
updateArrows();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
// Decorative spin only; this is not integrated gyroscopic dynamics.
rotorSpin.rotation.x += delta * 1.45;
updateLabels();
renderer.render(scene, camera);
});
document.querySelector("#loading").classList.add("hidden");
</script>
</body>
</html>
```