Prompt specification
Build or customize the Three.js 3D scene "Posture and load-distance teaching rig" 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: Posture and load-distance teaching rig
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/posture-and-load-distance-teaching-rig-x1028
Scene Description Prompt:
Use Three.js from cdnjs in one HTML file for a low-poly torso-and-load lever showing external torque, explicitly excluding injury predictions.
Working Three.js Starter (posture-and-load-distance-teaching-rig-x1028):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Torso + Load Lever — External Torque</title>
<style>
:root {
color-scheme: dark;
--bg: #07131c;
--panel: rgba(10, 25, 37, 0.82);
--panel-border: rgba(166, 220, 235, 0.16);
--text: #eaf7fb;
--muted: #91aab7;
--teal: #41d6c4;
--force: #ff6078;
--torque: #aa91ff;
--moment: #f3cf72;
--load: #ff9f43;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport,
#labels {
position: fixed;
inset: 0;
}
#viewport {
z-index: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.label {
position: absolute;
padding: 0.42rem 0.62rem;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 0.55rem;
background: rgba(5, 16, 24, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
color: var(--text);
font-size: 0.74rem;
font-weight: 650;
letter-spacing: 0.015em;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.label::before {
display: inline-block;
width: 0.48rem;
height: 0.48rem;
margin-right: 0.42rem;
border-radius: 50%;
background: currentColor;
content: "";
vertical-align: 0.02rem;
}
.label.force {
color: var(--force);
}
.label.torque {
color: var(--torque);
}
.label.moment {
color: var(--moment);
}
.label.load {
color: var(--load);
}
.label.pivot {
color: var(--teal);
}
.hud {
position: fixed;
z-index: 4;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--panel-border);
border-radius: 1rem;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(16px) saturate(125%);
}
.header {
top: 1.4rem;
left: 1.5rem;
width: min(31rem, calc(100vw - 3rem));
}
.eyebrow {
margin-bottom: 0.45rem;
color: var(--teal);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.55rem, 3vw, 2.65rem);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.04;
}
.subtitle {
max-width: 31rem;
margin: 0.7rem 0 0;
color: var(--muted);
font-size: 0.88rem;
line-height: 1.55;
}
.controls {
top: 1.4rem;
right: 1.4rem;
width: 19.5rem;
padding: 1rem;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
margin-bottom: 0.85rem;
}
.panel-title h2 {
margin: 0;
font-size: 0.78rem;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.badge {
padding: 0.24rem 0.48rem;
border: 1px solid rgba(65, 214, 196, 0.25);
border-radius: 999px;
background: rgba(65, 214, 196, 0.08);
color: var(--teal);
font-size: 0.62rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control {
margin-top: 0.9rem;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 1rem;
margin-bottom: 0.4rem;
}
.control label {
color: #c7d9e1;
font-size: 0.78rem;
font-weight: 650;
}
output {
color: var(--text);
font-variant-numeric: tabular-nums;
font-size: 0.78rem;
font-weight: 760;
}
input[type="range"] {
width: 100%;
height: 0.3rem;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(90deg, #244b58, #41d6c4);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 1rem;
height: 1rem;
border: 2px solid #d9fffa;
border-radius: 50%;
appearance: none;
background: #41d6c4;
box-shadow: 0 0 0 4px rgba(65, 214, 196, 0.12);
}
input[type="range"]::-moz-range-thumb {
width: 1rem;
height: 1rem;
border: 2px solid #d9fffa;
border-radius: 50%;
background: #41d6c4;
box-shadow: 0 0 0 4px rgba(65, 214, 196, 0.12);
}
.toggle-row {
display: flex;
flex-wrap: wrap;
gap: 0.65rem 1rem;
margin-top: 1rem;
padding-top: 0.85rem;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.toggle {
display: flex;
align-items: center;
gap: 0.45rem;
color: #bcd0d8;
font-size: 0.72rem;
cursor: pointer;
}
.toggle input {
accent-color: var(--teal);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 0.45rem 0.8rem;
margin-top: 0.9rem;
color: var(--muted);
font-size: 0.66rem;
}
.legend span {
display: flex;
align-items: center;
gap: 0.35rem;
}
.swatch {
width: 1rem;
height: 0.18rem;
border-radius: 1rem;
background: currentColor;
}
.button-row {
display: flex;
gap: 0.55rem;
margin-top: 0.95rem;
}
button {
flex: 1;
padding: 0.62rem 0.75rem;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 0.65rem;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font-size: 0.72rem;
font-weight: 700;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease;
}
button:hover {
border-color: rgba(65, 214, 196, 0.4);
background: rgba(65, 214, 196, 0.1);
}
.metrics {
right: 1.4rem;
bottom: 1.4rem;
width: 19.5rem;
padding: 1rem;
}
.equation {
margin: 0 0 0.85rem;
padding: 0.65rem 0.7rem;
border: 1px solid rgba(170, 145, 255, 0.17);
border-radius: 0.7rem;
background: rgba(170, 145, 255, 0.06);
color: #d9d0ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.78rem;
text-align: center;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.75rem;
}
.metric {
padding: 0.65rem;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 0.7rem;
background: rgba(255, 255, 255, 0.035);
}
.metric .name {
display: block;
margin-bottom: 0.25rem;
color: var(--muted);
font-size: 0.63rem;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.metric .value {
color: var(--text);
font-size: 1rem;
font-variant-numeric: tabular-nums;
font-weight: 780;
}
.direction {
margin-top: 0.75rem;
color: var(--muted);
font-size: 0.68rem;
text-align: center;
}
.scope {
bottom: 1.4rem;
left: 1.5rem;
width: min(28rem, calc(100vw - 3rem));
padding: 0.9rem 1rem;
border-color: rgba(255, 159, 67, 0.22);
}
.scope strong {
display: block;
margin-bottom: 0.25rem;
color: #ffc27e;
font-size: 0.76rem;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.scope p {
margin: 0;
color: #bacbd2;
font-size: 0.73rem;
line-height: 1.48;
}
.help {
bottom: 1.5rem;
left: 50%;
color: rgba(210, 230, 238, 0.58);
font-size: 0.65rem;
transform: translateX(-50%);
white-space: nowrap;
}
@media (max-width: 900px) {
.help {
display: none;
}
.header {
top: 0.8rem;
left: 0.8rem;
width: calc(100vw - 1.6rem);
}
.subtitle {
max-width: 24rem;
font-size: 0.78rem;
}
.controls {
top: auto;
right: 0.8rem;
bottom: 0.8rem;
width: min(19rem, calc(100vw - 1.6rem));
max-height: 48vh;
overflow-y: auto;
}
.metrics {
display: none;
}
.scope {
top: 9.5rem;
bottom: auto;
left: 0.8rem;
width: min(22rem, calc(100vw - 1.6rem));
}
}
@media (max-width: 520px) {
.header h1 {
font-size: 1.45rem;
}
.subtitle {
display: none;
}
.scope {
top: 5.3rem;
}
.scope p {
font-size: 0.66rem;
}
.label {
font-size: 0.64rem;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="labels">
<div id="labelForce" class="label force">F_g</div>
<div id="labelTorque" class="label torque">τ external</div>
<div id="labelMoment" class="label moment">r⊥</div>
<div id="labelLoad" class="label load">LOAD</div>
<div id="labelPivot" class="label pivot">HIP PIVOT</div>
</div>
<header class="hud header">
<div class="eyebrow">Mechanical Lever Demonstration</div>
<h1>Torso + Load Lever</h1>
<p class="subtitle">
A low-poly rigid torso rotates about a fixed hip pivot. The downward
gravitational force from the suspended load creates an external
moment about the hip.
</p>
</header>
<section class="hud panel controls">
<div class="panel-title">
<h2>Model Parameters</h2>
<span class="badge">Ideal rigid lever</span>
</div>
<div class="control">
<div class="control-row">
<label for="mass">Suspended mass</label>
<output id="massValue" for="mass">18 kg</output>
</div>
<input id="mass" type="range" min="5" max="50" step="1" value="18">
</div>
<div class="control">
<div class="control-row">
<label for="lean">Torso lean</label>
<output id="leanValue" for="lean">28°</output>
</div>
<input id="lean" type="range" min="0" max="40" step="1" value="28">
</div>
<div class="toggle-row">
<label class="toggle">
<input id="sweep" type="checkbox">
Sweep lean
</label>
<label class="toggle">
<input id="vectors" type="checkbox" checked>
Show vectors
</label>
</div>
<div class="legend" aria-label="Vector legend">
<span style="color: var(--force)">
<i class="swatch"></i>Weight force
</span>
<span style="color: var(--torque)">
<i class="swatch"></i>External torque
</span>
<span style="color: var(--moment)">
<i class="swatch"></i>Moment arm
</span>
</div>
<div class="button-row">
<button id="resetCamera" type="button">Reset camera</button>
<button id="resetModel" type="button">Reset model</button>
</div>
</section>
<section class="hud panel metrics">
<div class="panel-title">
<h2>External Load Calculation</h2>
<span class="badge">g = 9.81 m/s²</span>
</div>
<div class="equation">τ<sub>ext</sub> = |r × F| = F<sub>g</sub> · r⊥</div>
<div class="metric-grid">
<div class="metric">
<span class="name">Weight force</span>
<span id="metricForce" class="value">177 N</span>
</div>
<div class="metric">
<span class="name">Moment arm</span>
<span id="metricMoment" class="value">1.97 m</span>
</div>
<div class="metric">
<span class="name">External torque</span>
<span id="metricTorque" class="value">348 N·m</span>
</div>
<div class="metric">
<span class="name">Position vector</span>
<span id="metricRadius" class="value">2.02 m</span>
</div>
</div>
<div class="direction">
Rotation sense: clockwise about hip (−Z). Vector lengths are symbolic.
</div>
</section>
<aside class="hud panel scope">
<strong>Mechanical visualization only</strong>
<p>
This model explicitly excludes tissue deformation, internal joint loads,
material tolerances, injury thresholds, diagnosis, and all forms of
injury prediction. It does not estimate injury risk or bodily harm.
</p>
</aside>
<div class="hud help">
Drag: orbit · Wheel: zoom · Shift-drag: pan · Double-click: reset view
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131c);
scene.fog = new THREE.FogExp2(0x07131c, 0.038);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly torso and suspended-load lever"
);
viewport.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
50
);
const ambient = new THREE.HemisphereLight(0xbfe8ff, 0x14252d, 2.15);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x41d6c4, 1.5);
rimLight.position.set(-4, 3, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f43, 14, 8, 2);
warmLight.position.set(2.2, 2.4, 2.3);
scene.add(warmLight);
// ------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
metalness: 0.03,
flatShading: true,
...extra
});
const materials = {
skin: material(0xeeb183),
shirt: material(0x35b9ad),
shirtLight: material(0x55d4c7),
shorts: material(0x245b69),
joint: material(0x192c38, { metalness: 0.42, roughness: 0.45 }),
support: material(0x233945, { metalness: 0.35, roughness: 0.55 }),
hair: material(0x26333d),
load: material(0xf39a3d),
strap: material(0x754321),
rope: material(0xc7b58b),
torque: material(0x9e84ff, {
emissive: 0x241c48,
emissiveIntensity: 0.35
})
};
function addMesh(geometry, mat, parent = scene) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addEdges(mesh, color = 0x07131c, opacity = 0.48) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 24);
const line = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(line);
return line;
}
function makeRod(a, b, radius, mat, parent = scene, segments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, length, segments, 1, false),
mat,
parent
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function makeIco(radius, detail, mat, parent = scene) {
return addMesh(new THREE.IcosahedronGeometry(radius, detail), mat, parent);
}
function placeRod(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.scale.set(1, length, 1);
}
// ------------------------------------------------------------
// Ground, support, and fixed hip pivot
// ------------------------------------------------------------
const floor = addMesh(
new THREE.PlaneGeometry(30, 30),
material(0x091822, { roughness: 0.95 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.castShadow = false;
const grid = new THREE.GridHelper(16, 32, 0x315361, 0x193541);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const hip = new THREE.Vector3(0, 1.55, 0);
const support = new THREE.Group();
scene.add(support);
const column = addMesh(
new THREE.BoxGeometry(0.2, 1.48, 0.2),
materials.support,
support
);
column.position.set(0, 0.75, -0.42);
const foot = addMesh(
new THREE.BoxGeometry(0.95, 0.12, 0.72),
materials.support,
support
);
foot.position.set(0, 0.07, -0.42);
makeRod(
new THREE.Vector3(-0.38, 0.13, -0.42),
new THREE.Vector3(0, 1.42, -0.42),
0.07,
materials.support,
support
);
const pivotHousing = makeIco(
0.24,
1,
materials.joint,
scene
);
pivotHousing.position.copy(hip);
pivotHousing.scale.set(1, 1, 0.72);
const pivotRing = addMesh(
new THREE.TorusGeometry(0.285, 0.035, 5, 24),
materials.torque,
scene
);
pivotRing.position.copy(hip);
pivotRing.position.z = 0.12;
// ------------------------------------------------------------
// Low-poly torso and articulated-looking arm assembly
// ------------------------------------------------------------
const torso = new THREE.Group();
torso.position.copy(hip);
scene.add(torso);
const pelvis = makeIco(0.42, 1, materials.shorts, torso);
pelvis.position.y = 0.08;
pelvis.scale.set(1.05, 0.72, 0.82);
addEdges(pelvis);
const trunk = addMesh(
new THREE.CylinderGeometry(0.49, 0.34, 1.3, 5, 1, false),
materials.shirt,
torso
);
trunk.position.y = 0.83;
addEdges(trunk);
const chest = addMesh(
new THREE.BoxGeometry(1.02, 0.36, 0.48),
materials.shirtLight,
torso
);
chest.position.y = 1.46;
addEdges(chest);
const neck = addMesh(
new THREE.CylinderGeometry(0.12, 0.14, 0.28, 6),
materials.skin,
torso
);
neck.position.y = 1.76;
addEdges(neck);
const head = makeIco(0.285, 1, materials.skin, torso);
head.position.set(0, 2.04, 0);
head.scale.set(0.9, 1.08, 0.9);
addEdges(head);
const hair = makeIco(0.27, 1, materials.hair, torso);
hair.position.set(0, 2.16, -0.015);
hair.scale.set(0.92, 0.45, 0.92);
addEdges(hair, 0x061017, 0.3);
function buildArm(z) {
const shoulder = new THREE.Vector3(0.4, 1.43, z);
const elbow = new THREE.Vector3(0.96, 1.27, z);
const wrist = new THREE.Vector3(1.64, 1.11, z);
const upper = makeRod(
shoulder,
elbow,
0.125,
materials.shirtLight,
torso
);
addEdges(upper);
const fore = makeRod(
elbow,
wrist,
0.095,
materials.skin,
torso
);
addEdges(fore);
const elbowJoint = makeIco(0.12, 1, materials.skin, torso);
elbowJoint.position.copy(elbow);
const hand = makeIco(0.12, 1, materials.skin, torso);
hand.position.copy(wrist);
hand.scale.set(1.25, 0.85, 0.85);
const shoulderJoint = makeIco(0.14, 1, materials.shirtLight, torso);
shoulderJoint.position.copy(shoulder);
}
buildArm(0.17);
buildArm(-0.17);
makeRod(
new THREE.Vector3(1.64, 1.11, -0.17),
new THREE.Vector3(1.64, 1.11, 0.17),
0.035,
materials.joint,
torso
);
const gripLocal = new THREE.Vector3(1.64, 1.11, 0);
const gripWorld = new THREE.Vector3();
// ------------------------------------------------------------
// Suspended load
// ------------------------------------------------------------
const loadGroup = new THREE.Group();
scene.add(loadGroup);
const crate = addMesh(
new THREE.BoxGeometry(0.72, 0.68, 0.62),
materials.load,
loadGroup
);
addEdges(crate, 0x5f3216, 0.72);
for (const x of [-0.19, 0.19]) {
const strap = addMesh(
new THREE.BoxGeometry(0.065, 0.71, 0.025),
materials.strap,
loadGroup
);
strap.position.set(x, 0, 0.318);
}
const handle = addMesh(
new THREE.TorusGeometry(0.13, 0.032, 4, 10, Math.PI),
materials.joint,
loadGroup
);
handle.position.y = 0.35;
handle.rotation.z = Math.PI;
const rope = addMesh(
new THREE.CylinderGeometry(0.022, 0.022, 1, 6),
materials.rope,
scene
);
// ------------------------------------------------------------
// Force, moment arm, and external-torque visualization
// ------------------------------------------------------------
const vectorsGroup = new THREE.Group();
scene.add(vectorsGroup);
const forceArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(),
0.9,
0xff6078,
0.17,
0.1
);
vectorsGroup.add(forceArrow);
const dashedMaterial = new THREE.LineDashedMaterial({
color: 0xf3cf72,
dashSize: 0.11,
gapSize: 0.07,
transparent: true,
opacity: 0.9
});
const momentLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
hip,
hip.clone()
]),
dashedMaterial
);
vectorsGroup.add(momentLine);
const actionLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
hip,
hip.clone()
]),
new THREE.LineDashedMaterial({
color: 0xff6078,
dashSize: 0.1,
gapSize: 0.07,
transparent: true,
opacity: 0.72
})
);
vectorsGroup.add(actionLine);
// Clockwise torque arc in the XY plane.
const arcPoints = [];
const arcStart = 2.35;
const arcEnd = -0.52;
const arcRadius = 0.74;
for (let i = 0; i <= 50; i++) {
const t = i / 50;
const angle = THREE.MathUtils.lerp(arcStart, arcEnd, t);
arcPoints.push(
new THREE.Vector3(
hip.x + Math.cos(angle) * arcRadius,
hip.y + Math.sin(angle) * arcRadius,
hip.z + 0.43
)
);
}
const torqueArc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(arcPoints),
new THREE.LineBasicMaterial({
color: 0xaa91ff,
transparent: true,
opacity: 0.95
})
);
vectorsGroup.add(torqueArc);
const tangent = new THREE.Vector3(
Math.sin(arcEnd),
-Math.cos(arcEnd),
0
).normalize();
const torqueHead = addMesh(
new THREE.ConeGeometry(0.105, 0.25, 5),
materials.torque,
vectorsGroup
);
torqueHead.position
.copy(arcPoints[arcPoints.length - 1])
.addScaledVector(tangent, 0.07);
torqueHead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
// ------------------------------------------------------------
// UI state
// ------------------------------------------------------------
const massInput = document.getElementById("mass");
const leanInput = document.getElementById("lean");
const sweepInput = document.getElementById("sweep");
const vectorsInput = document.getElementById("vectors");
const massValue = document.getElementById("massValue");
const leanValue = document.getElementById("leanValue");
const metricForce = document.getElementById("metricForce");
const metricMoment = document.getElementById("metricMoment");
const metricTorque = document.getElementById("metricTorque");
const metricRadius = document.getElementById("metricRadius");
const labelForce = document.getElementById("labelForce");
const labelTorque = document.getElementById("labelTorque");
const labelMoment = document.getElementById("labelMoment");
const labelLoad = document.getElementById("labelLoad");
const labelPivot = document.getElementById("labelPivot");
let mass = Number(massInput.value);
let lean = Number(leanInput.value);
let sweep = false;
let force = mass * 9.81;
let momentArm = 0;
let torque = 0;
let positionRadius = 0;
massInput.addEventListener("input", () => {
mass = Number(massInput.value);
massValue.textContent = `${mass} kg`;
});
leanInput.addEventListener("input", () => {
lean = Number(leanInput.value);
sweep = false;
sweepInput.checked = false;
leanValue.textContent = `${lean}°`;
});
sweepInput.addEventListener("change", () => {
sweep = sweepInput.checked;
});
vectorsInput.addEventListener("change", () => {
vectorsGroup.visible = vectorsInput.checked;
});
// ------------------------------------------------------------
// Custom camera controls: orbit, zoom, and pan
// ------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0.65, 1.48, 0),
radius: 6.35,
theta: 0.38,
phi: 1.22
};
const defaultCamera = {
target: cameraState.target.clone(),
radius: cameraState.radius,
theta: cameraState.theta,
phi: cameraState.phi
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.target.copy(defaultCamera.target);
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
updateCamera();
}
let dragging = false;
let panning = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.shiftKey || event.button === 2;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
event.preventDefault();
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const panScale = cameraState.radius * 0.00135;
cameraState.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.42,
1.49
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
3.1,
11
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("resetCamera").addEventListener("click", resetCamera);
document.getElementById("resetModel").addEventListener("click", () => {
mass = 18;
lean = 28;
sweep = false;
massInput.value = "18";
leanInput.value = "28";
sweepInput.checked = false;
massValue.textContent = "18 kg";
leanValue.textContent = "28°";
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
if (event.key === "ArrowLeft") {
cameraState.theta -= 0.08;
updateCamera();
}
if (event.key === "ArrowRight") {
cameraState.theta += 0.08;
updateCamera();
}
if (event.key === "ArrowUp") {
cameraState.phi = Math.max(0.42, cameraState.phi - 0.06);
updateCamera();
}
if (event.key === "ArrowDown") {
cameraState.phi = Math.min(1.49, cameraState.phi + 0.06);
updateCamera();
}
});
// ------------------------------------------------------------
// Model update
// ------------------------------------------------------------
function updateModel() {
torso.rotation.z = -THREE.MathUtils.degToRad(lean);
torso.updateMatrixWorld(true);
gripWorld.copy(gripLocal);
torso.localToWorld(gripWorld);
force = mass * 9.81;
momentArm = Math.abs(gripWorld.x - hip.x);
torque = force * momentArm;
const relative = gripWorld.clone().sub(hip);
positionRadius = relative.length();
const crateTop = new THREE.Vector3(
gripWorld.x,
gripWorld.y - 0.38,
0
);
loadGroup.position.set(
gripWorld.x,
crateTop.y - 0.34,
0
);
placeRod(rope, gripWorld, crateTop);
forceArrow.position.set(
gripWorld.x,
gripWorld.y + 0.48,
0.43
);
forceArrow.setDirection(new THREE.Vector3(0, -1, 0));
forceArrow.setLength(0.9, 0.17, 0.1);
const momentEnd = new THREE.Vector3(
gripWorld.x,
hip.y,
0.35
);
momentLine.geometry.setFromPoints([hip, momentEnd]);
momentLine.computeLineDistances();
const actionEnd = new THREE.Vector3(
gripWorld.x,
gripWorld.y - 1.22,
0.35
);
actionLine.geometry.setFromPoints([hip, actionEnd]);
actionLine.computeLineDistances();
metricForce.textContent = `${force.toFixed(0)} N`;
metricMoment.textContent = `${momentArm.toFixed(2)} m`;
metricTorque.textContent = `${torque.toFixed(0)} N·m`;
metricRadius.textContent = `${positionRadius.toFixed(2)} m`;
}
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition, visible = true) {
if (!visible) {
element.style.display = "none";
return;
}
projected.copy(worldPosition).project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
Math.abs(projected.x) > 1.25 ||
Math.abs(projected.y) > 1.25
) {
element.style.display = "none";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.display = "block";
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
const showVectors = vectorsInput.checked;
positionLabel(
labelForce,
new THREE.Vector3(
gripWorld.x + 0.5,
gripWorld.y - 0.22,
0.43
),
showVectors
);
positionLabel(
labelTorque,
new THREE.Vector3(hip.x - 0.28, hip.y + 0.98, 0.43),
showVectors
);
positionLabel(
labelMoment,
new THREE.Vector3(
(hip.x + gripWorld.x) * 0.5,
hip.y + 0.16,
0.35
),
showVectors
);
positionLabel(
labelLoad,
new THREE.Vector3(
loadGroup.position.x,
loadGroup.position.y,
0.42
)
);
positionLabel(
labelPivot,
new THREE.Vector3(
hip.x - 0.55,
hip.y - 0.08,
0.34
)
);
}
// ------------------------------------------------------------
// 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();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (sweep) {
lean = 28 + 16 * Math.sin(elapsed * 0.75);
leanInput.value = String(Math.round(lean));
leanValue.textContent = `${Math.round(lean)}°`;
}
updateModel();
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
updateCamera();
updateModel();
</script>
</body>
</html>
```