Prompt specification
Build or customize the Three.js 3D scene "Bus passenger balance vignette" 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: Bus passenger balance vignette
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bus-passenger-balance-vignette-x0999
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts human stance and hip balance. Compose a standing passenger, handrail, and floor footprint markers inside a small bus; illustrate shifts of support when holding the rail without simulating injury. Voxel style.
Working Three.js Starter (bus-passenger-balance-vignette-x0999):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Transit — Stance & Hip Balance</title>
<style>
:root {
--ink: #17313a;
--muted: #597078;
--glass: rgba(248, 252, 250, 0.86);
--line: rgba(23, 49, 58, 0.13);
--green: #25a77b;
--blue: #477fc0;
--amber: #efa92f;
--orange: #ee7951;
}
* {
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 28% 16%, #f8fbf7 0, #dce9e5 42%, #b9ceca 100%);
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#viewport canvas.dragging {
cursor: grabbing;
}
.card {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--glass);
box-shadow:
0 18px 50px rgba(31, 63, 66, 0.13),
inset 0 1px 0 rgba(255, 255, 255, 0.85);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px 17px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.045em;
}
#intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#controlPanel {
left: 22px;
bottom: 22px;
width: min(350px, calc(100vw - 44px));
padding: 16px;
border-radius: 18px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#phaseBadge {
padding: 5px 8px;
border-radius: 999px;
background: #e5f2ec;
color: #23795f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
white-space: nowrap;
}
.mode-buttons {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
}
.mode-buttons button {
min-height: 39px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition:
background 160ms ease,
transform 160ms ease,
border-color 160ms ease;
}
.mode-buttons button:hover {
transform: translateY(-1px);
border-color: rgba(23, 49, 58, 0.3);
}
.mode-buttons button.active {
border-color: var(--green);
background: var(--green);
color: white;
}
#statusTitle {
margin-top: 15px;
font-size: 16px;
font-weight: 800;
letter-spacing: -0.02em;
}
#statusText {
min-height: 43px;
margin-top: 5px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.meter-label {
display: flex;
justify-content: space-between;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.meter-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 6px;
}
.meter {
position: relative;
height: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 49, 58, 0.08);
}
.meter span {
position: absolute;
inset: 0 auto 0 0;
width: 50%;
border-radius: inherit;
transition: width 120ms linear;
}
#leftMeter {
background: var(--blue);
}
#rightMeter {
background: var(--green);
}
.meter-values {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
}
.safe-note {
display: flex;
gap: 8px;
align-items: flex-start;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #5b706f;
font-size: 10px;
line-height: 1.4;
}
.safe-dot {
flex: 0 0 auto;
width: 8px;
height: 8px;
margin-top: 2px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(37, 167, 123, 0.12);
}
#legend {
top: 22px;
right: 22px;
padding: 13px 15px;
border-radius: 14px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin: 7px 0;
color: var(--muted);
font-size: 11px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.swatch.green {
background: var(--green);
}
.swatch.blue {
background: var(--blue);
}
.swatch.amber {
background: var(--amber);
}
#cameraPanel {
top: 150px;
right: 22px;
padding: 11px 13px;
border-radius: 14px;
text-align: right;
pointer-events: none;
}
#cameraPanel span {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
#resetCamera {
margin-top: 8px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 700;
}
.world-label {
position: fixed;
z-index: 5;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 7px;
background: rgba(250, 253, 251, 0.78);
box-shadow: 0 5px 18px rgba(34, 63, 67, 0.1);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
line-height: 1;
pointer-events: none;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
}
#massLabel {
color: #9a6610;
}
#supportLabel {
color: #187252;
}
#hipLabel {
color: #315f91;
}
@media (max-width: 720px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#intro p {
display: none;
}
#controlPanel {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
#legend {
top: 96px;
right: 12px;
}
#cameraPanel {
top: 96px;
left: 12px;
right: auto;
}
#statusText {
min-height: 0;
}
.world-label {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<section id="intro" class="card">
<div class="eyebrow">Voxel Transit Lab</div>
<h1>Stance & hip balance</h1>
<p>
A compact bus interior showing a small, comfortable support shift while
holding a handrail. No injury or pain response is simulated.
</p>
</section>
<section id="controlPanel" class="card">
<div class="panel-heading">
<strong>Support posture</strong>
<span id="phaseBadge">AUTO</span>
</div>
<div class="mode-buttons" role="group" aria-label="Passenger posture">
<button type="button" data-mode="neutral">Neutral</button>
<button type="button" data-mode="hold">Hold rail</button>
<button type="button" data-mode="auto" class="active">Auto</button>
</div>
<div id="statusTitle">Even two-foot support</div>
<div id="statusText">
The pelvis remains centered while both feet share the load.
</div>
<div class="meter-label">
<span>Illustrative support</span>
<span>Not a biomechanical model</span>
</div>
<div class="meter-row">
<div class="meter"><span id="leftMeter"></span></div>
<div class="meter"><span id="rightMeter"></span></div>
</div>
<div class="meter-values">
<span id="leftValue">Left 50%</span>
<span id="rightValue">Right 50%</span>
</div>
<div class="safe-note">
<span class="safe-dot"></span>
<span>
Voluntary, low-range movement only. The scene does not depict strain,
injury, pain, or loss of balance.
</span>
</div>
</section>
<aside id="legend" class="card">
<div class="legend-row">
<span class="swatch green"></span><span>Primary support</span>
</div>
<div class="legend-row">
<span class="swatch blue"></span><span>Secondary support</span>
</div>
<div class="legend-row">
<span class="swatch amber"></span><span>Center-of-mass guide</span>
</div>
</aside>
<aside id="cameraPanel" class="card">
<span>Drag to orbit · Wheel to zoom</span>
<span>Arrow keys rotate · R resets</span>
<button id="resetCamera" type="button">Reset camera</button>
</aside>
<div id="massLabel" class="world-label">Center of mass</div>
<div id="hipLabel" class="world-label">Pelvis / hip axis</div>
<div id="supportLabel" class="world-label">Rail-side support</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const modeButtons = [...document.querySelectorAll("[data-mode]")];
const phaseBadge = document.getElementById("phaseBadge");
const statusTitle = document.getElementById("statusTitle");
const statusText = document.getElementById("statusText");
const leftMeter = document.getElementById("leftMeter");
const rightMeter = document.getElementById("rightMeter");
const leftValue = document.getElementById("leftValue");
const rightValue = document.getElementById("rightValue");
const worldLabels = {
mass: document.getElementById("massLabel"),
hip: document.getElementById("hipLabel"),
support: document.getElementById("supportLabel")
};
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xcbdcd8, 15, 29);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf8fff8, 0x647674, 2.2);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.2);
sun.position.set(-5, 9, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbadde4, 1.15);
fill.position.set(6, 4, -4);
scene.add(fill);
const materialCache = new Map();
function getMaterial(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function addBox(
width,
height,
depth,
x,
y,
z,
color,
parent = scene
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const mesh = new THREE.Mesh(geometry, getMaterial(color));
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Small cutaway bus interior
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0xb8ccc7,
roughness: 1,
transparent: true,
opacity: 0.68
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.15;
ground.receiveShadow = true;
scene.add(ground);
const bus = new THREE.Group();
scene.add(bus);
// Raised floor.
addBox(8.7, 0.24, 4.35, 0, 0, 0, 0x46575a, bus);
addBox(8.42, 0.025, 4.08, 0, 0.135, 0, 0x607174, bus);
// Floor seams and edge strips.
for (let x = -3.8; x <= 3.8; x += 0.8) {
addBox(0.018, 0.009, 3.95, x, 0.151, 0, 0x708083, bus);
}
addBox(8.45, 0.018, 0.055, 0, 0.158, 1.83, 0xd6ad45, bus);
addBox(8.45, 0.018, 0.055, 0, 0.158, -1.83, 0xd6ad45, bus);
// Rear wall and window band.
addBox(8.7, 3.15, 0.18, 0, 1.7, -2.13, 0xd9ded9, bus);
addBox(8.62, 0.24, 0.2, 0, 3.18, -2.13, 0xeef0e9, bus);
addBox(8.62, 0.28, 0.2, 0, 0.35, -2.13, 0x657477, bus);
const windowPositions = [-3.25, -1.65, -0.05, 1.55, 3.15];
for (const x of windowPositions) {
addBox(1.42, 1.37, 0.055, x, 1.91, -2.018, 0x9fc9cf, bus);
addBox(1.48, 0.07, 0.075, x, 2.63, -2.005, 0x68787a, bus);
addBox(0.055, 1.42, 0.075, x - 0.74, 1.91, -2.005, 0x68787a, bus);
addBox(0.055, 1.42, 0.075, x + 0.74, 1.91, -2.005, 0x68787a, bus);
}
// Left end wall and low cutaway wall on the camera-facing side.
addBox(0.17, 3.05, 4.15, -4.28, 1.64, 0, 0xd9ded9, bus);
addBox(0.18, 0.52, 4.15, 4.28, 0.38, 0, 0xd9ded9, bus);
addBox(0.19, 0.1, 4.15, 4.28, 0.7, 0, 0x607174, bus);
// Open roof frame.
addBox(8.55, 0.13, 0.13, 0, 3.15, -1.99, 0x738184, bus);
addBox(0.13, 0.13, 4.08, -4.22, 3.15, 0, 0x738184, bus);
addBox(0.13, 0.13, 4.08, 4.22, 3.15, 0, 0x738184, bus);
// Bench seats along the rear wall.
function addBench(x, width) {
addBox(width, 0.24, 0.7, x, 0.69, -1.52, 0xe47f5c, bus);
addBox(width, 0.76, 0.18, x, 1.16, -1.82, 0xe47f5c, bus);
const divisions = Math.max(2, Math.round(width / 0.8));
for (let i = 0; i <= divisions; i++) {
const px = x - width / 2 + (width * i) / divisions;
addBox(0.035, 0.7, 0.19, px, 1.16, -1.805, 0xbd6046, bus);
}
addBox(0.1, 0.58, 0.1, x - width / 2 + 0.16, 0.38, -1.48, 0x58676a, bus);
addBox(0.1, 0.58, 0.1, x + width / 2 - 0.16, 0.38, -1.48, 0x58676a, bus);
}
addBench(-2.65, 2.45);
addBench(2.55, 2.35);
// Ceiling and vertical grab rails.
addBox(8.05, 0.115, 0.115, 0, 3.03, -0.15, 0xe87952, bus);
addBox(0.115, 2.9, 0.115, -3.82, 1.58, -0.15, 0xe87952, bus);
addBox(0.115, 2.9, 0.115, 3.82, 1.58, -0.15, 0xe87952, bus);
// Main rail used by the passenger.
const railX = 0.75;
const railZ = -0.15;
addBox(0.13, 2.9, 0.13, railX, 1.58, railZ, 0xef855b, bus);
addBox(0.17, 0.62, 0.17, railX, 1.8, railZ, 0x8e4b3b, bus);
addBox(0.42, 0.11, 0.42, railX, 0.2, railZ, 0x48575a, bus);
// Small emergency marker and route panel.
addBox(0.62, 0.23, 0.08, 3.45, 2.73, -1.99, 0x2b9c72, bus);
addBox(0.68, 0.12, 0.08, -0.05, 2.76, -1.98, 0x283f46, bus);
// -------------------------------------------------------------------------
// Voxel passenger
// -------------------------------------------------------------------------
const passenger = new THREE.Group();
passenger.position.set(-0.15, 0, 0.28);
scene.add(passenger);
const upper = new THREE.Group();
passenger.add(upper);
const torso = addBox(0.72, 0.82, 0.48, 0, 0, 0, 0x277f84, upper);
addBox(0.18, 0.64, 0.025, 0, -0.02, 0.252, 0xe8eee5, torso);
addBox(0.16, 0.16, 0.18, 0, 0.49, 0, 0xd49a76, upper);
const head = new THREE.Group();
head.position.set(0, 0.77, 0);
upper.add(head);
addBox(0.48, 0.48, 0.46, 0, 0, 0, 0xdca47f, head);
addBox(0.5, 0.13, 0.48, 0, 0.19, 0, 0x30383a, head);
addBox(0.07, 0.07, 0.055, -0.105, 0.025, 0.248, 0x263638, head);
addBox(0.07, 0.07, 0.055, 0.105, 0.025, 0.248, 0x263638, head);
addBox(0.07, 0.09, 0.07, 0, -0.035, 0.26, 0xc68c69, head);
const pelvis = addBox(0.68, 0.34, 0.44, 0, 1.53, 0, 0x254d59, passenger);
const hipAxis = addBox(0.56, 0.045, 0.065, 0, 1.57, 0.235, 0xefb13f, passenger);
const leftHipJoint = addBox(0.11, 0.11, 0.12, -0.2, 1.53, 0.23, 0xefa92f, passenger);
const rightHipJoint = addBox(0.11, 0.11, 0.12, 0.2, 1.53, 0.23, 0xefa92f, passenger);
const leftShoulder = addBox(0.19, 0.19, 0.19, -0.43, 0.27, 0, 0x277f84, upper);
const rightShoulder = addBox(0.19, 0.19, 0.19, 0.43, 0.27, 0, 0x277f84, upper);
function createSegment(thickness, color) {
return addBox(thickness, 1, thickness, 0, 0, 0, color, passenger);
}
const leftUpperArm = createSegment(0.19, 0x277f84);
const leftForearm = createSegment(0.16, 0xdca47f);
const rightUpperArm = createSegment(0.19, 0x277f84);
const rightForearm = createSegment(0.16, 0xdca47f);
const leftThigh = createSegment(0.24, 0x263d48);
const leftShin = createSegment(0.21, 0x263d48);
const rightThigh = createSegment(0.24, 0x263d48);
const rightShin = createSegment(0.21, 0x263d48);
const leftElbow = addBox(0.19, 0.19, 0.19, 0, 0, 0, 0xdca47f, passenger);
const rightElbow = addBox(0.19, 0.19, 0.19, 0, 0, 0, 0xdca47f, passenger);
const leftHand = addBox(0.18, 0.18, 0.18, 0, 0, 0, 0xdca47f, passenger);
const rightHand = addBox(0.18, 0.18, 0.18, 0, 0, 0, 0xdca47f, passenger);
function createFoot() {
const group = new THREE.Group();
passenger.add(group);
addBox(0.32, 0.06, 0.48, 0, 0.17, 0.05, 0xe9e7dc, group);
addBox(0.3, 0.18, 0.43, 0, 0.26, 0.035, 0x36474d, group);
addBox(0.31, 0.08, 0.14, 0, 0.29, 0.24, 0x26383f, group);
return group;
}
const leftFoot = createFoot();
const rightFoot = createFoot();
// -------------------------------------------------------------------------
// Floor footprint markers and balance guides
// -------------------------------------------------------------------------
const leftFootMaterial = new THREE.MeshStandardMaterial({
color: 0x35ad84,
roughness: 0.85,
transparent: true,
opacity: 0.9
});
const rightFootMaterial = new THREE.MeshStandardMaterial({
color: 0x35ad84,
roughness: 0.85,
transparent: true,
opacity: 0.9
});
function createFootprint(material) {
const group = new THREE.Group();
scene.add(group);
// Heel and forefoot.
addBox(0.22, 0.025, 0.18, 0, 0, -0.12, material, group);
addBox(0.27, 0.025, 0.25, 0, 0, 0.1, material, group);
// Voxel toes.
addBox(0.065, 0.026, 0.08, -0.09, 0, 0.27, material, group);
addBox(0.065, 0.026, 0.085, -0.03, 0, 0.285, material, group);
addBox(0.065, 0.026, 0.08, 0.035, 0, 0.275, material, group);
addBox(0.055, 0.026, 0.065, 0.095, 0, 0.25, material, group);
return group;
}
const leftFootprint = createFootprint(leftFootMaterial);
const rightFootprint = createFootprint(rightFootMaterial);
// Base-of-support bracket on the floor.
const baseMaterial = new THREE.MeshBasicMaterial({
color: 0x6e9dc7,
transparent: true,
opacity: 0.42
});
addBox(0.025, 0.014, 0.95, -0.53, 0.163, 0.4, baseMaterial);
addBox(0.025, 0.014, 0.95, 0.23, 0.163, 0.4, baseMaterial);
addBox(0.78, 0.014, 0.025, -0.15, 0.163, 0.85, baseMaterial);
// A low-opacity transfer arrow appears as support shifts.
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x35ad84,
transparent: true,
opacity: 0.12
});
const shiftArrow = new THREE.Group();
scene.add(shiftArrow);
addBox(0.34, 0.014, 0.055, 0.12, 0, 0, arrowMaterial, shiftArrow);
const arrowHeadA = addBox(
0.18, 0.014, 0.055, 0.31, 0, 0, arrowMaterial, shiftArrow
);
arrowHeadA.rotation.y = Math.PI / 4;
const arrowHeadB = addBox(
0.18, 0.014, 0.055, 0.31, 0, 0, arrowMaterial, shiftArrow
);
arrowHeadB.rotation.y = -Math.PI / 4;
// Center-of-mass cube, plumb guide, and floor projection.
const massMaterial = new THREE.MeshStandardMaterial({
color: 0xefa92f,
emissive: 0x7a4700,
emissiveIntensity: 0.18,
roughness: 0.55
});
const massGuideMaterial = new THREE.MeshBasicMaterial({
color: 0xefa92f,
transparent: true,
opacity: 0.58
});
const massMarker = addBox(
0.14, 0.14, 0.14, 0, 2.2, 0.3, massMaterial
);
const massGuide = addBox(
0.018, 1, 0.018, 0, 1.2, 0.3, massGuideMaterial
);
const massProjection = addBox(
0.12, 0.026, 0.12, 0, 0.175, 0.3, massMaterial
);
// -------------------------------------------------------------------------
// Articulated pose helpers
// -------------------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
function setSegment(mesh, from, to) {
const direction = new THREE.Vector3().subVectors(to, from);
const length = Math.max(direction.length(), 0.0001);
mesh.position.copy(from).add(to).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.scale.set(1, length, 1);
}
function solveJoint(root, end, lengthA, lengthB, bendHint) {
const delta = new THREE.Vector3().subVectors(end, root);
const rawDistance = Math.max(delta.length(), 0.0001);
const distance = THREE.MathUtils.clamp(rawDistance, 0.001, lengthA + lengthB - 0.001);
const direction = delta.clone().normalize();
const along =
(lengthA * lengthA - lengthB * lengthB + distance * distance) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, lengthA * lengthA - along * along)
);
const bend = bendHint.clone().projectOnPlane(direction);
if (bend.lengthSq() < 0.0001) {
bend.set(0, 0, 1).projectOnPlane(direction);
}
bend.normalize();
return root.clone().addScaledVector(direction, along).addScaledVector(bend, height);
}
function transformedUpperPoint(x, y, z) {
return new THREE.Vector3(x, y, z)
.applyQuaternion(upper.quaternion)
.add(upper.position);
}
const green = new THREE.Color(0x35ad84);
const blue = new THREE.Color(0x477fc0);
function updatePassenger(shift) {
// Small, smooth pelvis and trunk adjustment toward the rail-side foot.
pelvis.position.x = shift * 0.1;
hipAxis.position.x = pelvis.position.x;
leftHipJoint.position.x = pelvis.position.x - 0.2;
rightHipJoint.position.x = pelvis.position.x + 0.2;
upper.position.set(shift * 0.17, 2.03, 0);
upper.rotation.z = -shift * 0.085;
// Feet remain planted.
const leftFootPosition = new THREE.Vector3(-0.38, 0, 0.12);
const rightFootPosition = new THREE.Vector3(0.38, 0, 0.12);
leftFoot.position.copy(leftFootPosition);
rightFoot.position.copy(rightFootPosition);
leftFoot.rotation.y = 0.075;
rightFoot.rotation.y = -0.065;
const leftHip = new THREE.Vector3(
pelvis.position.x - 0.2,
1.49,
0
);
const rightHip = new THREE.Vector3(
pelvis.position.x + 0.2,
1.49,
0
);
const leftAnkle = new THREE.Vector3(-0.38, 0.35, 0.12);
const rightAnkle = new THREE.Vector3(0.38, 0.35, 0.12);
const kneeBend = new THREE.Vector3(0, 0, 1);
const leftKnee = solveJoint(leftHip, leftAnkle, 0.72, 0.72, kneeBend);
const rightKnee = solveJoint(rightHip, rightAnkle, 0.72, 0.72, kneeBend);
setSegment(leftThigh, leftHip, leftKnee);
setSegment(leftShin, leftKnee, leftAnkle);
setSegment(rightThigh, rightHip, rightKnee);
setSegment(rightShin, rightKnee, rightAnkle);
// Arms.
const leftShoulderPoint = transformedUpperPoint(-0.43, 0.27, 0);
const rightShoulderPoint = transformedUpperPoint(0.43, 0.27, 0);
const neutralRightHand = new THREE.Vector3(0.43, 1.7, 0.02);
const railRightHand = new THREE.Vector3(0.9, 1.8, -0.43);
const rightHandPosition = neutralRightHand.lerp(railRightHand, shift);
const leftHandPosition = new THREE.Vector3(-0.43, 1.7, 0.02);
leftHandPosition.x -= shift * 0.035;
const leftElbowPosition = solveJoint(
leftShoulderPoint,
leftHandPosition,
0.5,
0.5,
new THREE.Vector3(-1, 0.1, 0.12)
);
const rightElbowPosition = solveJoint(
rightShoulderPoint,
rightHandPosition,
0.5,
0.5,
new THREE.Vector3(1, -0.12, -0.18)
);
setSegment(leftUpperArm, leftShoulderPoint, leftElbowPosition);
setSegment(leftForearm, leftElbowPosition, leftHandPosition);
setSegment(rightUpperArm, rightShoulderPoint, rightElbowPosition);
setSegment(rightForearm, rightElbowPosition, rightHandPosition);
leftElbow.position.copy(leftElbowPosition);
rightElbow.position.copy(rightElbowPosition);
leftHand.position.copy(leftHandPosition);
rightHand.position.copy(rightHandPosition);
// Footprint colors show the support transition.
leftFootMaterial.color.copy(green).lerp(blue, shift);
rightFootMaterial.color.copy(green);
const leftWorld = passenger.localToWorld(leftFootPosition.clone());
const rightWorld = passenger.localToWorld(rightFootPosition.clone());
leftFootprint.position.set(leftWorld.x, 0.164, leftWorld.z);
rightFootprint.position.set(rightWorld.x, 0.164, rightWorld.z);
// Center-of-mass guide.
const massLocal = transformedUpperPoint(0, 0.22, 0.02);
const massWorld = passenger.localToWorld(massLocal.clone());
massMarker.position.copy(massWorld);
massProjection.position.set(massWorld.x, 0.174, massWorld.z);
const guideHeight = Math.max(0.1, massWorld.y - 0.174);
massGuide.position.set(massWorld.x, 0.174 + guideHeight / 2, massWorld.z);
massGuide.scale.set(1, guideHeight, 1);
// Floor transfer arrow.
shiftArrow.position.set(passenger.position.x + shift * 0.13, 0.17, 0.72);
arrowMaterial.opacity = 0.12 + shift * 0.52;
return {
massWorld,
hipWorld: passenger.localToWorld(
new THREE.Vector3(pelvis.position.x, 1.65, 0.2)
),
supportWorld: rightWorld.clone().add(new THREE.Vector3(0, 0.16, 0))
};
}
// -------------------------------------------------------------------------
// Direct camera controls: drag orbit, wheel zoom, keyboard
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.68,
pitch: 0.36,
radius: 11.4,
target: new THREE.Vector3(-0.1, 1.35, 0)
};
const defaultCamera = {
yaw: 0.68,
pitch: 0.36,
radius: 11.4
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(yaw) * Math.cos(pitch),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.radius = defaultCamera.radius;
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.13,
1.05
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
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(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
6.5,
17
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.yaw += 0.1;
if (event.key === "ArrowRight") cameraState.yaw -= 0.1;
if (event.key === "ArrowUp") cameraState.pitch = Math.max(0.13, cameraState.pitch - 0.08);
if (event.key === "ArrowDown") cameraState.pitch = Math.min(1.05, cameraState.pitch + 0.08);
if (event.key === "+" || event.key === "=") cameraState.radius = Math.max(6.5, cameraState.radius - 0.6);
if (event.key === "-") cameraState.radius = Math.min(17, cameraState.radius + 0.6);
if (event.key.toLowerCase() === "r") resetCamera();
});
document.getElementById("resetCamera").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Posture modes and UI
// -------------------------------------------------------------------------
let selectedMode = "auto";
let shift = 0;
let lastStatusKey = "";
modeButtons.forEach((button) => {
button.addEventListener("click", () => {
selectedMode = button.dataset.mode;
modeButtons.forEach((item) => {
item.classList.toggle("active", item === button);
});
});
});
function updateStatus(targetShift) {
const key = `${selectedMode}-${targetShift}`;
if (key === lastStatusKey) return;
lastStatusKey = key;
if (selectedMode === "auto") {
phaseBadge.textContent = targetShift > 0.5 ? "AUTO · RAIL HOLD" : "AUTO · NEUTRAL";
if (targetShift > 0.5) {
statusTitle.textContent = "Comfortable rail-side support";
statusText.textContent =
"The rail-side hand assists balance while the pelvis moves slightly toward the rail-side foot.";
} else {
statusTitle.textContent = "Even two-foot support";
statusText.textContent =
"The pelvis returns to center while both feet share the illustrative load.";
}
} else if (selectedMode === "hold") {
phaseBadge.textContent = "RAIL HOLD";
statusTitle.textContent = "Comfortable rail-side support";
statusText.textContent =
"A small trunk and hip adjustment transfers more support to the rail-side foot; both feet remain planted.";
} else {
phaseBadge.textContent = "NEUTRAL";
statusTitle.textContent = "Even two-foot support";
statusText.textContent =
"The pelvis remains centered over the footprint while both feet share the illustrative load.";
}
}
function updateMeter(shiftAmount) {
const left = Math.round(50 - shiftAmount * 18);
const right = Math.round(50 + shiftAmount * 18);
leftMeter.style.width = `${left}%`;
rightMeter.style.width = `${right}%`;
leftValue.textContent = `Left ${left}%`;
rightValue.textContent = `Right ${right}%`;
}
function placeWorldLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
if (projected.z > 1 || projected.z < -1) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = width < 700 ? 48 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", onResize);
onResize();
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let autoPhase = 0;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = now * 0.001;
let targetShift = 0;
if (selectedMode === "hold") {
targetShift = 1;
} else if (selectedMode === "neutral") {
targetShift = 0;
} else {
autoPhase = elapsed % 9;
targetShift = autoPhase < 5 ? 1 : 0;
}
// Smooth, low-range transition with no abrupt jolt.
shift += (targetShift - shift) * (1 - Math.exp(-dt * 4.2));
const anchors = updatePassenger(shift);
updateStatus(targetShift);
updateMeter(shift);
updateCamera();
camera.updateMatrixWorld();
placeWorldLabel(worldLabels.mass, anchors.massWorld.clone().add(new THREE.Vector3(0, 0.22, 0)));
placeWorldLabel(worldLabels.hip, anchors.hipWorld.clone().add(new THREE.Vector3(-0.12, 0.18, 0)));
placeWorldLabel(worldLabels.support, anchors.supportWorld.clone().add(new THREE.Vector3(0.1, 0.16, 0)));
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```