Prompt specification
Build or customize the Three.js 3D scene "Adjustable work surface comparison" 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: Adjustable work surface comparison
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/adjustable-work-surface-comparison-x0186
Scene Description Prompt:
Raise and lower a voxel desk beside a seated figure, showing changing elbow and forearm alignment. One HTML file using Three.js from cdnjs.
Working Three.js Starter (adjustable-work-surface-comparison-x0186):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Desk / Elbow Alignment</title>
<style>
:root {
--ink: #172b32;
--muted: #64757a;
--paper: rgba(250, 248, 242, 0.88);
--line: rgba(23, 43, 50, 0.13);
--teal: #167f7b;
--coral: #e96e50;
--amber: #e8a83e;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f1eee6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
top: 26px;
left: 28px;
right: 28px;
display: flex;
justify-content: space-between;
align-items: flex-start;
pointer-events: none;
z-index: 10;
}
.brand {
display: flex;
gap: 14px;
align-items: center;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 4px;
transform: rotate(-5deg);
}
.brand-mark span {
display: block;
background: var(--ink);
border-radius: 3px;
}
.brand-mark span:nth-child(2) {
background: var(--coral);
}
.brand-mark span:nth-child(3) {
background: var(--teal);
}
.brand-mark span:nth-child(4) {
background: var(--amber);
}
.eyebrow {
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.5vw, 34px);
line-height: 1;
letter-spacing: -0.055em;
font-weight: 850;
}
.readout {
width: 244px;
padding: 17px 18px 16px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 14px 45px rgba(42, 54, 52, 0.11);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.readout-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
}
.readout-head span:first-child {
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live-dot {
display: flex;
align-items: center;
gap: 6px;
color: var(--teal);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.live-dot::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(22, 127, 123, 0.12);
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
align-items: baseline;
gap: 12px;
padding: 9px 0;
border-top: 1px solid var(--line);
}
.metric-label {
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.metric strong {
font-size: 19px;
line-height: 1;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.status {
margin-top: 10px;
padding: 8px 10px;
border-radius: 8px;
background: rgba(22, 127, 123, 0.1);
color: var(--teal);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-align: center;
text-transform: uppercase;
}
.status.up {
color: #a35a18;
background: rgba(232, 168, 62, 0.15);
}
.status.down {
color: #b4513e;
background: rgba(233, 110, 80, 0.13);
}
.controls {
position: fixed;
left: 50%;
bottom: 24px;
z-index: 20;
display: flex;
align-items: center;
gap: 9px;
width: max-content;
max-width: calc(100vw - 30px);
padding: 10px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 16px;
background: rgba(250, 248, 242, 0.9);
box-shadow: 0 16px 50px rgba(42, 54, 52, 0.14);
backdrop-filter: blur(16px);
}
.control-button {
min-width: 40px;
height: 40px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.6);
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 800;
transition:
transform 150ms ease,
background 150ms ease,
border-color 150ms ease;
}
.control-button:hover {
transform: translateY(-1px);
border-color: rgba(23, 43, 50, 0.28);
background: #fff;
}
.control-button:active {
transform: translateY(1px);
}
.control-button.active {
border-color: rgba(22, 127, 123, 0.28);
background: rgba(22, 127, 123, 0.12);
color: var(--teal);
}
.arrow-button {
padding: 0;
font-size: 19px;
font-weight: 500;
}
.range-wrap {
position: relative;
display: flex;
align-items: center;
width: clamp(150px, 23vw, 270px);
height: 40px;
padding: 0 7px;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
to right,
var(--teal) 0%,
var(--teal) var(--fill, 40%),
rgba(23, 43, 50, 0.13) var(--fill, 40%),
rgba(23, 43, 50, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
appearance: none;
border: 3px solid #fff;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 43, 50, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 43, 50, 0.28);
}
.divider {
width: 1px;
height: 26px;
margin: 0 2px;
background: var(--line);
}
.legend {
position: fixed;
left: 28px;
bottom: 29px;
z-index: 10;
display: grid;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 19px;
height: 7px;
border-radius: 2px;
}
.swatch.arm {
background: var(--coral);
}
.swatch.forearm {
background: #dfae8b;
}
.swatch.guide {
height: 2px;
background: repeating-linear-gradient(
90deg,
var(--teal) 0 5px,
transparent 5px 9px
);
}
.hint {
position: fixed;
right: 28px;
bottom: 30px;
z-index: 10;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
top: 16px;
left: 16px;
right: 16px;
}
.brand-mark {
width: 34px;
height: 34px;
}
.eyebrow {
font-size: 8px;
}
.readout {
width: 178px;
padding: 12px;
}
.metric {
padding: 7px 0;
}
.metric strong {
font-size: 16px;
}
.controls {
bottom: 14px;
gap: 5px;
padding: 7px;
}
.control-button {
min-width: 35px;
height: 36px;
padding: 0 9px;
font-size: 10px;
}
.range-wrap {
width: 115px;
height: 36px;
}
.divider,
.legend,
.hint {
display: none;
}
}
@media (max-width: 520px) {
.readout {
top: 68px;
right: 12px;
width: 160px;
}
.brand h1 {
font-size: 21px;
}
.brand-mark {
display: none;
}
#guidesButton,
#resetButton {
display: none;
}
.range-wrap {
width: 100px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span><span></span><span></span><span></span>
</div>
<div>
<div class="eyebrow">Kinematic study / 01</div>
<h1>DESK / ELBOW</h1>
</div>
</div>
<aside class="readout" aria-label="Live alignment measurements">
<div class="readout-head">
<span>Live metrics</span>
<span class="live-dot">Active</span>
</div>
<div class="metric">
<span class="metric-label">Desk height</span>
<strong id="heightValue">90 cm</strong>
</div>
<div class="metric">
<span class="metric-label">Elbow angle</span>
<strong id="angleValue">—</strong>
</div>
<div class="metric">
<span class="metric-label">Forearm tilt</span>
<strong id="tiltValue">—</strong>
</div>
<div id="status" class="status">Measuring alignment</div>
</aside>
</header>
<div class="legend" aria-hidden="true">
<div class="legend-row">
<span class="swatch arm"></span>
Upper arm / sleeve
</div>
<div class="legend-row">
<span class="swatch forearm"></span>
Forearm / skin
</div>
<div class="legend-row">
<span class="swatch guide"></span>
Alignment axis
</div>
</div>
<section class="controls" aria-label="Desk and view controls">
<button
id="lowerButton"
class="control-button arrow-button"
type="button"
aria-label="Lower desk"
title="Lower desk"
>↓</button>
<label class="range-wrap">
<input
id="heightSlider"
type="range"
min="0"
max="100"
value="36"
aria-label="Desk height"
/>
</label>
<button
id="raiseButton"
class="control-button arrow-button"
type="button"
aria-label="Raise desk"
title="Raise desk"
>↑</button>
<div class="divider"></div>
<button id="guidesButton" class="control-button active" type="button">
Guides
</button>
<button id="autoButton" class="control-button active" type="button">
Pause cycle
</button>
<button id="resetButton" class="control-button" type="button">
Reset view
</button>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Arrow keys adjust height</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const heightSlider = document.getElementById("heightSlider");
const heightValue = document.getElementById("heightValue");
const angleValue = document.getElementById("angleValue");
const tiltValue = document.getElementById("tiltValue");
const statusValue = document.getElementById("status");
const lowerButton = document.getElementById("lowerButton");
const raiseButton = document.getElementById("raiseButton");
const guidesButton = document.getElementById("guidesButton");
const autoButton = document.getElementById("autoButton");
const resetButton = document.getElementById("resetButton");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
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.08;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel figure seated beside a height-adjustable desk"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf1eee6);
scene.fog = new THREE.Fog(0xf1eee6, 10, 22);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(-0.05, 1.18, 0.14);
const cameraOrbit = {
theta: 0.72,
phi: 1.10,
radius: 7.9
};
function updateCamera() {
const sinPhi = Math.sin(cameraOrbit.phi);
camera.position.set(
cameraTarget.x + cameraOrbit.radius * sinPhi * Math.sin(cameraOrbit.theta),
cameraTarget.y + cameraOrbit.radius * Math.cos(cameraOrbit.phi),
cameraTarget.z + cameraOrbit.radius * sinPhi * Math.cos(cameraOrbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const ambient = new THREE.HemisphereLight(0xfffaf0, 0x8d9996, 2.15);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff4dc, 4.2);
sun.position.set(5.5, 8.5, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -5;
sun.shadow.camera.right = 5;
sun.shadow.camera.top = 5;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 20;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcde8e6, 1.25);
fill.position.set(-5, 3.5, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
const up = new THREE.Vector3(0, 1, 0);
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(parent, color, size, position, customMaterial = null) {
const mesh = new THREE.Mesh(
unitBox,
customMaterial || material(color)
);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function setSegment(mesh, start, end, width, depth) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
if (length < 0.00001) {
mesh.visible = false;
return;
}
mesh.visible = true;
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.scale.set(width, length, depth);
}
// -------------------------------------------------------------------------
// Ground and room accents
// -------------------------------------------------------------------------
const ground = box(
scene,
0xe7e3da,
[24, 0.16, 24],
[0, -0.19, 0]
);
ground.castShadow = false;
const platform = box(
scene,
0xf4f0e7,
[5.9, 0.12, 3.9],
[0, -0.065, 0.12]
);
platform.castShadow = false;
const rug = box(
scene,
0xd9ddd3,
[3.4, 0.025, 2.45],
[-0.12, 0.012, 0.15]
);
rug.castShadow = false;
// Small voxel plant on the far side of the desk.
const plant = new THREE.Group();
plant.position.set(2.05, 0, -0.35);
scene.add(plant);
box(plant, 0xb76f50, [0.36, 0.36, 0.36], [0, 0.18, 0]);
box(plant, 0x2f6755, [0.12, 0.58, 0.12], [0, 0.59, 0]);
box(plant, 0x397e61, [0.28, 0.25, 0.25], [-0.15, 0.78, 0]);
box(plant, 0x4a956e, [0.25, 0.22, 0.24], [0.16, 0.67, 0.02]);
box(plant, 0x2f735a, [0.23, 0.2, 0.23], [0.03, 0.95, -0.04]);
// A small stack of books balances the composition.
const books = new THREE.Group();
books.position.set(-2.05, 0, -0.83);
books.rotation.y = 0.12;
scene.add(books);
box(books, 0x386d70, [0.7, 0.14, 0.5], [0, 0.07, 0]);
box(books, 0xd47a56, [0.63, 0.13, 0.46], [0.05, 0.205, 0.02]);
box(books, 0xe0b358, [0.56, 0.11, 0.42], [-0.03, 0.325, 0.01]);
// -------------------------------------------------------------------------
// Chair
// -------------------------------------------------------------------------
const chair = new THREE.Group();
scene.add(chair);
const chairWood = 0xa97952;
const chairPad = 0x4f6870;
box(chair, chairPad, [0.64, 0.15, 0.86], [-1.5, 0.82, 0.1]);
box(chair, chairWood, [0.13, 1.04, 0.88], [-1.82, 1.37, 0.1]);
box(chair, chairPad, [0.08, 0.73, 0.68], [-1.73, 1.44, 0.1]);
for (const x of [-1.72, -1.3]) {
for (const z of [-0.22, 0.42]) {
box(chair, chairWood, [0.11, 0.75, 0.11], [x, 0.39, z]);
}
}
// -------------------------------------------------------------------------
// Seated voxel figure
// -------------------------------------------------------------------------
const figure = new THREE.Group();
scene.add(figure);
const skin = 0xe0ad88;
const skinLight = 0xf0c3a1;
const shirt = 0xe96e50;
const shirtDark = 0xce583f;
const trousers = 0x344a61;
const hair = 0x383235;
// Hips and torso.
box(figure, trousers, [0.55, 0.34, 0.69], [-1.13, 1.08, 0.1]);
box(figure, shirt, [0.52, 0.73, 0.66], [-1.16, 1.59, 0.1]);
box(figure, shirtDark, [0.09, 0.59, 0.54], [-0.89, 1.58, 0.1]);
box(figure, skin, [0.22, 0.17, 0.23], [-1.11, 2.02, 0.1]);
// Head and face.
box(figure, skinLight, [0.48, 0.49, 0.47], [-1.08, 2.31, 0.1]);
box(figure, hair, [0.51, 0.12, 0.49], [-1.13, 2.56, 0.1]);
box(figure, hair, [0.15, 0.37, 0.49], [-1.31, 2.37, 0.1]);
for (const z of [-0.015, 0.205]) {
box(figure, hair, [0.025, 0.055, 0.052], [-0.826, 2.36, z]);
}
box(figure, skin, [0.08, 0.08, 0.09], [-0.805, 2.28, 0.1]);
// Seated thighs and lower legs.
for (const z of [-0.11, 0.31]) {
box(figure, trousers, [0.77, 0.26, 0.25], [-0.72, 0.94, z]);
box(figure, trousers, [0.25, 0.7, 0.23], [-0.32, 0.53, z]);
box(figure, 0x253748, [0.48, 0.17, 0.27], [-0.08, 0.19, z]);
box(figure, 0xf2eee3, [0.49, 0.05, 0.28], [-0.07, 0.085, z]);
}
// Relaxed arm on the far side.
const relaxedShoulder = new THREE.Vector3(-0.91, 1.9, -0.08);
const relaxedElbow = new THREE.Vector3(-0.67, 1.43, -0.11);
const relaxedWrist = new THREE.Vector3(-0.38, 1.19, -0.12);
const relaxedUpper = box(
figure,
shirt,
[1, 1, 1],
[0, 0, 0]
);
const relaxedFore = box(
figure,
skin,
[1, 1, 1],
[0, 0, 0]
);
const relaxedHand = box(
figure,
skinLight,
[0.2, 0.13, 0.19],
relaxedWrist.toArray()
);
setSegment(relaxedUpper, relaxedShoulder, relaxedElbow, 0.23, 0.23);
setSegment(relaxedFore, relaxedElbow, relaxedWrist, 0.16, 0.16);
// -------------------------------------------------------------------------
// Height-adjustable voxel desk
// -------------------------------------------------------------------------
const desk = new THREE.Group();
scene.add(desk);
const deskTop = new THREE.Group();
desk.add(deskTop);
// Instanced top tiles keep the desk visually voxelized.
const columns = 11;
const rows = 6;
const tileStep = 0.2;
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.01,
vertexColors: true
});
const tiles = new THREE.InstancedMesh(
unitBox,
tileMaterial,
columns * rows
);
tiles.castShadow = true;
tiles.receiveShadow = true;
const dummy = new THREE.Object3D();
const tileColors = [0xd99b61, 0xe1a76f, 0xd39259, 0xe5ad78];
let instance = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
dummy.position.set(
-0.45 + column * tileStep,
0,
-0.35 + row * tileStep
);
dummy.scale.set(0.192, 0.14, 0.192);
dummy.updateMatrix();
tiles.setMatrixAt(instance, dummy.matrix);
tiles.setColorAt(
instance,
new THREE.Color(tileColors[(row + column * 2) % tileColors.length])
);
instance++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
deskTop.add(tiles);
// Underside and moving crossbars.
box(deskTop, 0x81583c, [2.12, 0.14, 1.12], [0.55, -0.115, 0.15]);
box(deskTop, 0x376b6b, [2.02, 0.1, 0.08], [0.55, -0.22, -0.2]);
box(deskTop, 0x376b6b, [2.02, 0.1, 0.08], [0.55, -0.22, 0.5]);
// Desk accessories that move with the top.
const keyboard = new THREE.Group();
keyboard.position.set(0.13, 0.11, 0.28);
deskTop.add(keyboard);
box(keyboard, 0x30484b, [0.55, 0.07, 0.38], [0, 0, 0]);
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 7; column++) {
box(
keyboard,
row === 2 && column > 4 ? 0xe6b967 : 0x8fa09b,
[0.045, 0.012, 0.045],
[-0.21 + column * 0.07, 0.042, -0.11 + row * 0.09]
);
}
}
const notebook = new THREE.Group();
notebook.position.set(0.91, 0.13, -0.19);
notebook.rotation.y = -0.09;
deskTop.add(notebook);
box(notebook, 0x426f73, [0.53, 0.08, 0.47], [0, 0, 0]);
box(notebook, 0xf3ead6, [0.47, 0.055, 0.42], [0.015, 0.015, 0.005]);
box(notebook, 0xe7b85d, [0.06, 0.025, 0.46], [-0.22, 0.025, 0]);
// Pencil cup.
box(deskTop, 0xcf7455, [0.24, 0.28, 0.24], [1.26, 0.21, 0.38]);
for (let index = 0; index < 4; index++) {
box(
deskTop,
[0xe6bd5e, 0x36756e, 0xe98161, 0x4e6072][index],
[0.035, 0.4, 0.035],
[1.2 + index * 0.035, 0.42, 0.36 + (index % 2) * 0.035]
);
}
// Telescopic legs: fixed lower sections and moving upper sections.
const frameColor = 0x28575a;
const legPositions = [
[-0.2, -0.22],
[-0.2, 0.52],
[1.3, -0.22],
[1.3, 0.52]
];
const upperLegs = [];
for (const [x, z] of legPositions) {
box(desk, frameColor, [0.14, 0.58, 0.14], [x, 0.37, z]);
box(desk, 0x1d4246, [0.25, 0.09, 0.24], [x, 0.08, z]);
const upper = box(desk, 0x4b8582, [1, 1, 1], [x, 0.6, z]);
upperLegs.push(upper);
}
// -------------------------------------------------------------------------
// Active arm and analytical guides
// -------------------------------------------------------------------------
const activeShoulder = new THREE.Vector3(-0.91, 1.9, 0.3);
const activeElbow = new THREE.Vector3();
const activeWrist = new THREE.Vector3();
const upperArm = box(figure, shirt, [1, 1, 1], [0, 0, 0]);
const forearm = box(figure, skin, [1, 1, 1], [0, 0, 0]);
const elbowMarker = box(
figure,
0xe7ad42,
[0.18, 0.18, 0.18],
[0, 0, 0]
);
const hand = box(
figure,
skinLight,
[0.22, 0.12, 0.2],
[0, 0, 0]
);
const forearmHighlightMaterial = new THREE.MeshBasicMaterial({
color: 0x33aaa2,
transparent: true,
opacity: 0.19,
depthWrite: false
});
const forearmHighlight = new THREE.Mesh(
unitBox,
forearmHighlightMaterial
);
forearmHighlight.renderOrder = 3;
figure.add(forearmHighlight);
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
activeShoulder,
activeElbow,
activeWrist
]);
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x176d6a,
dashSize: 0.075,
gapSize: 0.045,
transparent: true,
opacity: 0.88,
depthTest: false
});
const guideLine = new THREE.Line(guideGeometry, guideMaterial);
guideLine.renderOrder = 10;
figure.add(guideLine);
const arcGeometry = new THREE.BufferGeometry();
const arcMaterial = new THREE.LineBasicMaterial({
color: 0xe5a23a,
transparent: true,
opacity: 0.95,
depthTest: false
});
const angleArc = new THREE.Line(arcGeometry, arcMaterial);
angleArc.renderOrder = 11;
figure.add(angleArc);
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.012, 8, 36),
new THREE.MeshBasicMaterial({
color: 0x2b9991,
transparent: true,
opacity: 0.8
})
);
targetRing.rotation.x = Math.PI / 2;
scene.add(targetRing);
// -------------------------------------------------------------------------
// Two-bone inverse kinematics
// -------------------------------------------------------------------------
function solveElbow(shoulder, target, upperLength, lowerLength, hint) {
const axis = new THREE.Vector3().subVectors(target, shoulder);
const rawDistance = axis.length();
const distance = THREE.MathUtils.clamp(
rawDistance,
Math.abs(upperLength - lowerLength) + 0.0001,
upperLength + lowerLength - 0.0001
);
axis.normalize();
const along =
(
upperLength * upperLength -
lowerLength * lowerLength +
distance * distance
) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, upperLength * upperLength - along * along)
);
const bend = hint.clone();
bend.addScaledVector(axis, -bend.dot(axis)).normalize();
return shoulder
.clone()
.addScaledVector(axis, along)
.addScaledVector(bend, height);
}
function updateAngleArc(elbow, shoulder, wrist) {
const first = shoulder.clone().sub(elbow).normalize();
const second = wrist.clone().sub(elbow).normalize();
const axis = new THREE.Vector3().crossVectors(first, second);
if (axis.lengthSq() < 0.00001) {
angleArc.visible = false;
return;
}
axis.normalize();
const angle = Math.acos(
THREE.MathUtils.clamp(first.dot(second), -1, 1)
);
const points = [];
const radius = 0.22;
for (let index = 0; index <= 28; index++) {
const fraction = index / 28;
const theta = fraction * angle;
const rotated = first
.clone()
.multiplyScalar(Math.cos(theta))
.add(
axis
.clone()
.cross(first)
.multiplyScalar(Math.sin(theta))
);
points.push(
elbow
.clone()
.add(rotated.multiplyScalar(radius))
.add(new THREE.Vector3(0, 0, 0.075))
);
}
arcGeometry.setFromPoints(points);
}
// -------------------------------------------------------------------------
// Desk state and controls
// -------------------------------------------------------------------------
const MIN_DESK_Y = 0.88;
const MAX_DESK_Y = 1.72;
const MIN_DESK_CM = 68;
const MAX_DESK_CM = 132;
let deskY = 1.05;
let deskTarget = deskY;
let guidesVisible = true;
let autoCycle = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let autoPhase = -0.9;
function normalizedHeight() {
return (deskTarget - MIN_DESK_Y) / (MAX_DESK_Y - MIN_DESK_Y);
}
function setNormalizedHeight(value, stopAuto = false) {
deskTarget = THREE.MathUtils.lerp(
MIN_DESK_Y,
MAX_DESK_Y,
THREE.MathUtils.clamp(value, 0, 1)
);
if (stopAuto) {
autoCycle = false;
updateAutoButton();
}
}
function updateAutoButton() {
autoButton.classList.toggle("active", autoCycle);
autoButton.textContent = autoCycle ? "Pause cycle" : "Auto cycle";
autoButton.setAttribute("aria-pressed", String(autoCycle));
}
function updateSliderFill() {
const fill = ((heightSlider.value - heightSlider.min) / 100) * 100;
heightSlider.style.setProperty("--fill", `${fill}%`);
}
heightSlider.addEventListener("input", () => {
setNormalizedHeight(Number(heightSlider.value) / 100, true);
updateSliderFill();
});
lowerButton.addEventListener("click", () => {
setNormalizedHeight(normalizedHeight() - 0.055, true);
heightSlider.value = String(normalizedHeight() * 100);
updateSliderFill();
});
raiseButton.addEventListener("click", () => {
setNormalizedHeight(normalizedHeight() + 0.055, true);
heightSlider.value = String(normalizedHeight() * 100);
updateSliderFill();
});
guidesButton.addEventListener("click", () => {
guidesVisible = !guidesVisible;
guidesButton.classList.toggle("active", guidesVisible);
guidesButton.setAttribute("aria-pressed", String(guidesVisible));
});
autoButton.addEventListener("click", () => {
autoCycle = !autoCycle;
if (autoCycle) {
const normalized = THREE.MathUtils.clamp(
normalizedHeight(),
0,
1
);
autoPhase = Math.asin(normalized * 2 - 1);
}
updateAutoButton();
});
resetButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target === heightSlider) return;
if (event.key === "ArrowUp") {
event.preventDefault();
setNormalizedHeight(normalizedHeight() + 0.04, true);
}
if (event.key === "ArrowDown") {
event.preventDefault();
setNormalizedHeight(normalizedHeight() - 0.04, true);
}
});
updateAutoButton();
updateSliderFill();
// -------------------------------------------------------------------------
// Direct camera controls: pointer orbit, wheel zoom, double-click reset
// -------------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function resetCamera() {
cameraOrbit.theta = 0.72;
cameraOrbit.phi = 1.1;
cameraOrbit.radius = 7.9;
cameraTarget.set(-0.05, 1.18, 0.14);
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraOrbit.theta -= deltaX * 0.006;
cameraOrbit.phi = THREE.MathUtils.clamp(
cameraOrbit.phi - deltaY * 0.005,
0.48,
1.42
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraOrbit.radius = THREE.MathUtils.clamp(
cameraOrbit.radius * Math.exp(event.deltaY * 0.001),
4.5,
12
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Animation and live measurements
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function updateDesk(delta) {
if (autoCycle) {
autoPhase += delta * 0.75;
const wave = (Math.sin(autoPhase) + 1) * 0.5;
deskTarget = THREE.MathUtils.lerp(
MIN_DESK_Y,
MAX_DESK_Y,
wave
);
heightSlider.value = String(wave * 100);
updateSliderFill();
}
const smoothing = 1 - Math.exp(-delta * 7.5);
deskY = THREE.MathUtils.lerp(deskY, deskTarget, smoothing);
deskTop.position.y = deskY;
for (const upper of upperLegs) {
const start = new THREE.Vector3(
upper.position.x,
0.61,
upper.position.z
);
const end = new THREE.Vector3(
upper.position.x,
deskY - 0.19,
upper.position.z
);
setSegment(upper, start, end, 0.095, 0.095);
}
targetRing.position.set(-0.3, deskY + 0.078, 0.3);
}
function updateArm() {
activeWrist.set(-0.3, deskY + 0.13, 0.3);
activeElbow.copy(
solveElbow(
activeShoulder,
activeWrist,
0.67,
0.68,
new THREE.Vector3(0, -1, 0.2)
)
);
setSegment(upperArm, activeShoulder, activeElbow, 0.23, 0.23);
setSegment(forearm, activeElbow, activeWrist, 0.16, 0.16);
setSegment(
forearmHighlight,
activeElbow,
activeWrist,
0.25,
0.25
);
elbowMarker.position.copy(activeElbow);
hand.position.copy(activeWrist);
const shoulderToElbow = activeShoulder
.clone()
.sub(activeElbow)
.normalize();
const elbowToWrist = activeWrist
.clone()
.sub(activeElbow)
.normalize();
const elbowAngle =
Math.acos(
THREE.MathUtils.clamp(
shoulderToElbow.dot(elbowToWrist),
-1,
1
)
) *
(180 / Math.PI);
const forearmRise = activeWrist.y - activeElbow.y;
const forearmRun = Math.hypot(
activeWrist.x - activeElbow.x,
activeWrist.z - activeElbow.z
);
const forearmTilt =
Math.atan2(forearmRise, Math.max(forearmRun, 0.0001)) *
(180 / Math.PI);
const deskCm = Math.round(
THREE.MathUtils.lerp(
MIN_DESK_CM,
MAX_DESK_CM,
(deskY - MIN_DESK_Y) / (MAX_DESK_Y - MIN_DESK_Y)
)
);
heightValue.textContent = `${deskCm} cm`;
angleValue.textContent = `${Math.round(elbowAngle)}°`;
tiltValue.textContent = `${forearmTilt >= 0 ? "+" : "−"}${Math.abs(
Math.round(forearmTilt)
)}°`;
statusValue.classList.remove("up", "down");
if (Math.abs(forearmTilt) < 8) {
statusValue.textContent = "Neutral forearm alignment";
} else if (forearmTilt > 0) {
statusValue.textContent = "Forearm inclined upward";
statusValue.classList.add("up");
} else {
statusValue.textContent = "Forearm inclined downward";
statusValue.classList.add("down");
}
const guideOffset = new THREE.Vector3(0, 0, 0.075);
guideGeometry.setFromPoints([
activeShoulder.clone().add(guideOffset),
activeElbow.clone().add(guideOffset),
activeWrist.clone().add(guideOffset)
]);
guideLine.computeLineDistances();
guideLine.visible = guidesVisible;
angleArc.visible = guidesVisible;
forearmHighlight.visible = guidesVisible;
targetRing.visible = guidesVisible;
updateAngleArc(activeElbow, activeShoulder, activeWrist);
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
updateDesk(delta);
updateArm();
renderer.render(scene, camera);
}
animate();
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```