Prompt specification
Build or customize the Three.js 3D scene "Reach a Shelf After Scale Changes" 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: Reach a Shelf After Scale Changes
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reach-a-shelf-after-scale-changes-x3740
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts character poses relative to furniture scale. Create a pose adaptation experiment changing shelf height and character proportions. Solve only reachable targets and display the remaining gap for impossible configurations. Low-poly.
Working Three.js Starter (reach-a-shelf-after-scale-changes-x3740):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Reach Lab — Furniture-Scale Pose Adaptation</title>
<style>
:root {
--ink: #26343a;
--muted: #68777d;
--paper: rgba(249, 247, 240, 0.92);
--line: rgba(38, 52, 58, 0.13);
--teal: #367f7d;
--amber: #d89532;
--red: #c85d50;
--green: #438b67;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
max-width: 390px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
content: "";
background: var(--teal);
}
.brand h1 {
margin: 0;
font-size: clamp(25px, 3vw, 40px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1;
}
.brand p {
max-width: 340px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: 326px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.68);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 18px 60px rgba(39, 48, 48, 0.13);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.02em;
}
.panel-subtitle {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
}
.reset {
flex: 0 0 auto;
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.66);
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.reset:hover {
background: white;
}
.presets {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-bottom: 18px;
}
.preset {
padding: 8px 3px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.48);
color: var(--muted);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.preset:hover,
.preset.active {
border-color: rgba(54, 127, 125, 0.45);
background: rgba(54, 127, 125, 0.1);
color: var(--teal);
}
.control {
margin: 0 0 15px;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
}
.control label {
font-size: 12px;
font-weight: 700;
}
.control output {
color: var(--teal);
font-size: 11px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--fill, 50%),
rgba(38, 52, 58, 0.14) var(--fill, 50%),
rgba(38, 52, 58, 0.14) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #f9f7f0;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--teal);
box-shadow: 0 1px 5px rgba(30, 50, 50, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #f9f7f0;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 1px 5px rgba(30, 50, 50, 0.25);
}
.divider {
height: 1px;
margin: 18px 0;
background: var(--line);
}
.telemetry {
position: fixed;
z-index: 8;
bottom: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 17px 18px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: rgba(249, 247, 240, 0.9);
box-shadow: 0 16px 48px rgba(39, 48, 48, 0.12);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.status-pill {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border-radius: 999px;
background: rgba(200, 93, 80, 0.12);
color: var(--red);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.status-pill::before {
width: 6px;
height: 6px;
border-radius: 50%;
content: "";
background: currentColor;
}
.status-pill.ok {
background: rgba(67, 139, 103, 0.12);
color: var(--green);
}
.pose-chip {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.result {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 20px;
margin: 12px 0 8px;
}
.result h3 {
margin: 0;
font-size: 19px;
letter-spacing: -0.035em;
}
.result strong {
color: var(--red);
font-size: 25px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.04em;
}
.telemetry.ok .result strong {
color: var(--green);
}
.result-note {
min-height: 28px;
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.reach-track {
height: 5px;
margin: 13px 0 15px;
overflow: hidden;
border-radius: 999px;
background: rgba(38, 52, 58, 0.11);
}
.reach-fill {
width: 50%;
height: 100%;
border-radius: inherit;
background: var(--red);
transition: width 150ms ease, background 150ms ease;
}
.telemetry.ok .reach-fill {
background: var(--green);
}
.metrics {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.metric {
min-width: 0;
padding-right: 7px;
border-right: 1px solid var(--line);
}
.metric:last-child {
border-right: 0;
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
font-size: 12px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.camera-hint {
position: fixed;
z-index: 5;
right: 24px;
bottom: 22px;
color: rgba(38, 52, 58, 0.62);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
.brand h1 {
font-size: 27px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 47vh;
padding: 15px;
overflow-y: auto;
border-radius: 17px;
}
.presets {
margin-bottom: 13px;
}
.control {
margin-bottom: 12px;
}
.telemetry {
top: 83px;
bottom: auto;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
.result {
margin: 8px 0 5px;
}
.result h3 {
font-size: 16px;
}
.result strong {
font-size: 20px;
}
.result-note,
.reach-track,
.metric:nth-child(4) {
display: none;
}
.metrics {
grid-template-columns: repeat(3, 1fr);
margin-top: 11px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive low-poly reachability experiment"></main>
<header class="brand">
<div class="eyebrow">Reach Lab / Experiment 04</div>
<h1>Furniture-scale<br />pose adaptation</h1>
<p>
Change the shelf and body proportions. Valid two-bone reaches are solved;
impossible targets retain their remaining geometric gap.
</p>
</header>
<aside class="panel" aria-label="Experiment controls">
<div class="panel-head">
<div>
<h2>Experiment controls</h2>
<p class="panel-subtitle">All dimensions are in meters.</p>
</div>
<button class="reset" id="resetView" type="button">Reset view</button>
</div>
<div class="presets" aria-label="Preset configurations">
<button class="preset" data-preset="low" type="button">Low</button>
<button class="preset active" data-preset="ideal" type="button">Ideal</button>
<button class="preset" data-preset="high" type="button">High</button>
<button class="preset" data-preset="tall" type="button">Tall</button>
</div>
<div class="control">
<div class="control-row">
<label for="shelf">Target shelf height</label>
<output id="shelfValue">1.15 m</output>
</div>
<input id="shelf" type="range" min="0.45" max="2.15" step="0.01" value="1.15" />
</div>
<div class="control">
<div class="control-row">
<label for="height">Character height</label>
<output id="heightValue">1.72 m</output>
</div>
<input id="height" type="range" min="1.15" max="2.05" step="0.01" value="1.72" />
</div>
<div class="control">
<div class="control-row">
<label for="leg">Leg proportion</label>
<output id="legValue">48%</output>
</div>
<input id="leg" type="range" min="0.44" max="0.52" step="0.01" value="0.48" />
</div>
<div class="control">
<div class="control-row">
<label for="arm">Maximum arm reach</label>
<output id="armValue">45%</output>
</div>
<input id="arm" type="range" min="0.38" max="0.50" step="0.01" value="0.45" />
</div>
<div class="divider"></div>
<div class="control">
<div class="control-row">
<label>Reach constraints</label>
<output>Two-bone IK</output>
</div>
<p class="panel-subtitle">
The target is excluded from the arm solver whenever its distance exceeds
the shoulder-to-fingertip limit.
</p>
</div>
</aside>
<section class="telemetry" id="telemetry" aria-live="polite">
<div class="status-row">
<span class="status-pill ok" id="statusPill">Target reached</span>
<span class="pose-chip" id="poseValue">Neutral reach</span>
</div>
<div class="result">
<h3 id="resultTitle">Pose solved</h3>
<strong id="resultValue">0 cm gap</strong>
</div>
<p class="result-note" id="resultNote">
The active arm reaches the target contact point exactly.
</p>
<div class="reach-track" aria-hidden="true">
<div class="reach-fill" id="reachFill"></div>
</div>
<div class="metrics">
<div class="metric">
<span>Shoulder</span>
<strong id="shoulderValue">1.33 m</strong>
</div>
<div class="metric">
<span>Target</span>
<strong id="targetValue">1.27 m</strong>
</div>
<div class="metric">
<span>Required</span>
<strong id="requiredValue">0.71 m</strong>
</div>
<div class="metric">
<span>Max reach</span>
<strong id="reachValue">0.77 m</strong>
</div>
</div>
</section>
<div class="camera-hint">Drag to orbit · scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
const V3 = THREE.Vector3;
const UP = new V3(0, 1, 0);
const state = {
shelf: 1.15,
height: 1.72,
leg: 0.48,
arm: 0.45
};
const presets = {
low: {
shelf: 0.55,
height: 1.72,
leg: 0.48,
arm: 0.45
},
ideal: {
shelf: 1.15,
height: 1.72,
leg: 0.48,
arm: 0.45
},
high: {
shelf: 1.95,
height: 1.72,
leg: 0.48,
arm: 0.45
},
tall: {
shelf: 1.95,
height: 2.05,
leg: 0.52,
arm: 0.50
}
};
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5dc);
scene.fog = new THREE.Fog(0xe9e5dc, 8, 18);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
40
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
$("stage").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff8e9, 0x819397, 2.1);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff1dc, 3.1);
key.position.set(3.5, 6.5, 4.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 5;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 15;
key.shadow.bias = -0.0003;
scene.add(key);
const fill = new THREE.DirectionalLight(0xb9d9dc, 0.85);
fill.position.set(-3, 3, -2);
scene.add(fill);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xd8d2c6,
roughness: 0.95,
flatShading: true
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(14, 14),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.012;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(12, 24, 0xa9a69f, 0xc4bfb5);
grid.position.y = -0.006;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xeee9de,
roughness: 1,
flatShading: true
});
const wall = new THREE.Mesh(new THREE.BoxGeometry(12, 4.4, 0.16), wallMaterial);
wall.position.set(0, 2.18, -1.35);
wall.receiveShadow = true;
scene.add(wall);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0x31464e,
roughness: 0.78,
flatShading: true
}),
frameLight: new THREE.MeshStandardMaterial({
color: 0x496169,
roughness: 0.8,
flatShading: true
}),
wood: new THREE.MeshStandardMaterial({
color: 0xc68e5c,
roughness: 0.85,
flatShading: true
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xd8aa78,
roughness: 0.85,
flatShading: true
}),
back: new THREE.MeshStandardMaterial({
color: 0xb9b3a6,
roughness: 1,
flatShading: true
}),
shirt: new THREE.MeshStandardMaterial({
color: 0xe36f60,
roughness: 0.82,
flatShading: true
}),
pants: new THREE.MeshStandardMaterial({
color: 0x3d7f80,
roughness: 0.85,
flatShading: true
}),
skin: new THREE.MeshStandardMaterial({
color: 0xd7a27d,
roughness: 0.9,
flatShading: true
}),
hair: new THREE.MeshStandardMaterial({
color: 0x3b3030,
roughness: 1,
flatShading: true
}),
shoe: new THREE.MeshStandardMaterial({
color: 0x2e393d,
roughness: 0.95,
flatShading: true
}),
eye: new THREE.MeshStandardMaterial({
color: 0x253238,
roughness: 0.8,
flatShading: true
}),
target: new THREE.MeshStandardMaterial({
color: 0xf0b64f,
emissive: 0x6b3a00,
emissiveIntensity: 0.12,
roughness: 0.55,
flatShading: true
}),
targetBand: new THREE.MeshStandardMaterial({
color: 0xf7f1df,
roughness: 0.72,
flatShading: true
})
};
function box(parent, width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Shelf and target
// ---------------------------------------------------------------------
const shelf = new THREE.Group();
scene.add(shelf);
const shelfCenter = 1.60;
const shelfWidth = 1.64;
const shelfDepth = 0.58;
box(shelf, 0.10, 2.34, shelfDepth, materials.frame, 0.78, 1.17, 0);
box(shelf, 0.10, 2.34, shelfDepth, materials.frame, 2.42, 1.17, 0);
box(shelf, shelfWidth, 0.10, shelfDepth, materials.frame, shelfCenter, 2.30, 0);
box(shelf, shelfWidth, 0.08, shelfDepth, materials.frame, shelfCenter, 0.12, 0);
box(
shelf,
shelfWidth - 0.12,
2.14,
0.045,
materials.back,
shelfCenter,
1.18,
-0.275
);
const activeBoard = box(
shelf,
shelfWidth - 0.13,
0.08,
shelfDepth - 0.02,
materials.wood,
shelfCenter,
1.11,
0
);
// Decorative books on the fixed bottom shelf.
const bookColors = [0x5e7f83, 0xd17b63, 0xe2b556, 0x7d7891];
for (let i = 0; i < 7; i++) {
const h = 0.18 + (i % 3) * 0.045;
box(
shelf,
0.10,
h,
0.24,
new THREE.MeshStandardMaterial({
color: bookColors[i % bookColors.length],
roughness: 0.9,
flatShading: true
}),
1.06 + i * 0.115,
0.16 + h / 2,
-0.02
);
}
// A few objects on the top shelf.
box(shelf, 0.36, 0.19, 0.30, materials.woodLight, 1.25, 2.395, 0);
box(shelf, 0.24, 0.30, 0.24, materials.frameLight, 1.95, 2.40, 0);
const targetGroup = new THREE.Group();
scene.add(targetGroup);
const targetBlock = box(
targetGroup,
0.24,
0.24,
0.24,
materials.target,
0,
0,
0
);
box(
targetGroup,
0.248,
0.055,
0.248,
materials.targetBand,
0,
0.035,
0
);
const contactRingMaterial = new THREE.MeshStandardMaterial({
color: 0x438b67,
emissive: 0x173e2d,
emissiveIntensity: 0.45,
roughness: 0.5,
flatShading: true
});
const contactRing = new THREE.Mesh(
new THREE.TorusGeometry(0.082, 0.011, 6, 18),
contactRingMaterial
);
contactRing.rotation.y = Math.PI / 2;
scene.add(contactRing);
// ---------------------------------------------------------------------
// Character
// ---------------------------------------------------------------------
const character = new THREE.Group();
scene.add(character);
let characterMeshes = [];
function clearCharacter() {
character.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
character.clear();
characterMeshes = [];
}
function characterMesh(geometry, material) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
character.add(mesh);
characterMeshes.push(mesh);
return mesh;
}
function setSegment(mesh, a, b, radius, depth = radius) {
const direction = new V3().subVectors(b, a);
const length = Math.max(direction.length(), 0.0001);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.scale.set(radius, length, depth);
}
function solveElbow(shoulder, target, upperLength, lowerLength, preference) {
const delta = new V3().subVectors(target, shoulder);
const distance = Math.max(delta.length(), 0.0001);
const direction = delta.clone().multiplyScalar(1 / distance);
const along =
(upperLength * upperLength -
lowerLength * lowerLength +
distance * distance) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, upperLength * upperLength - along * along)
);
let bend = preference.clone();
bend.addScaledVector(direction, -bend.dot(direction));
if (bend.lengthSq() < 0.0001) {
bend.set(1, 0, 0);
bend.addScaledVector(direction, -bend.dot(direction));
}
bend.normalize();
return shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
}
function solveKnee(hip, ankle, thighLength, shinLength) {
const delta = new V3().subVectors(ankle, hip);
const rawDistance = Math.max(delta.length(), 0.0001);
const distance = Math.min(rawDistance, thighLength + shinLength - 0.0001);
const direction = delta.clone().multiplyScalar(1 / rawDistance);
const along =
(thighLength * thighLength -
shinLength * shinLength +
distance * distance) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, thighLength * thighLength - along * along)
);
let bend = new V3(1, 0, 0);
bend.addScaledVector(direction, -bend.dot(direction));
if (bend.lengthSq() < 0.0001) {
bend.set(0, 0, 1);
bend.addScaledVector(direction, -bend.dot(direction));
}
bend.normalize();
return hip
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
}
function buildCharacter(pose) {
clearCharacter();
const h = state.height;
const legLength = h * state.leg;
const torsoLength = h * (1 - state.leg - 0.19);
const thighLength = h * 0.25;
const shinLength = Math.max(0.03, legLength - thighLength);
const headRadius = h * 0.095;
const shoulderHalfWidth = h * 0.09;
const hip = new V3(pose.hipX, pose.hipY, 0);
const shoulder = new V3(pose.shoulderX, pose.shoulderY, 0);
const torsoDirection = shoulder.clone().sub(hip).normalize();
const headCenter = shoulder.clone().addScaledVector(torsoDirection, headRadius * 0.85);
const torso = characterMesh(new THREE.BoxGeometry(1, 1, 1), materials.shirt);
setSegment(torso, hip, shoulder, h * 0.16, h * 0.17);
const pelvis = characterMesh(
new THREE.BoxGeometry(1, 1, 1),
materials.pants
);
pelvis.position.copy(hip);
pelvis.scale.set(h * 0.25, h * 0.14, h * 0.18);
const neck = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.skin
);
setSegment(
neck,
shoulder.clone().addScaledVector(torsoDirection, -0.01),
headCenter.clone().addScaledVector(torsoDirection, -headRadius * 0.45),
h * 0.035
);
const head = characterMesh(
new THREE.IcosahedronGeometry(headRadius, 1),
materials.skin
);
head.position.copy(headCenter);
head.scale.set(0.94, 1.06, 0.94);
const hair = characterMesh(
new THREE.IcosahedronGeometry(headRadius, 1),
materials.hair
);
hair.position.copy(headCenter);
hair.position.y += headRadius * 0.43;
hair.scale.set(1.0, 0.55, 1.0);
for (const zSign of [-1, 1]) {
const eye = characterMesh(
new THREE.SphereGeometry(h * 0.011, 6, 4),
materials.eye
);
eye.position.set(
headCenter.x + headRadius * 0.82,
headCenter.y + h * 0.008,
headCenter.z + zSign * headRadius * 0.31
);
}
const nose = characterMesh(
new THREE.ConeGeometry(h * 0.018, h * 0.045, 4),
materials.skin
);
nose.position.set(headCenter.x + headRadius * 0.97, headCenter.y, headCenter.z);
nose.quaternion.setFromUnitVectors(UP, new V3(1, 0, 0));
// Legs and shoes.
for (const zSign of [-1, 1]) {
const hipJoint = new V3(hip.x, hip.y, zSign * h * 0.07);
const ankle = new V3(hip.x, 0, zSign * h * 0.07);
const knee = solveKnee(hipJoint, ankle, thighLength, shinLength);
const thigh = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.pants
);
setSegment(thigh, hipJoint, knee, h * 0.047);
const shin = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.pants
);
setSegment(shin, knee, ankle, h * 0.037);
const shoe = characterMesh(
new THREE.BoxGeometry(1, 1, 1),
materials.shoe
);
shoe.position.set(ankle.x + h * 0.045, h * 0.035, ankle.z);
shoe.scale.set(h * 0.17, h * 0.065, h * 0.105);
shoe.rotation.z = pose.footTilt;
}
// Active arm: exact IK only when the target is within reach.
const activeShoulder = new V3(
shoulder.x,
shoulder.y,
shoulderHalfWidth
);
const upperArm = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.shirt
);
const lowerArm = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.skin
);
const hand = characterMesh(
new THREE.IcosahedronGeometry(h * 0.043, 1),
materials.skin
);
const upperLength = pose.maxReach * 0.52;
const lowerLength = pose.maxReach * 0.48;
if (pose.reachable) {
pose.activeElbow = solveElbow(
activeShoulder,
pose.contact,
upperLength,
lowerLength,
new V3(0, -1, 0)
);
pose.activeHand = pose.contact.clone();
} else {
const direction = pose.contact
.clone()
.sub(activeShoulder)
.normalize();
pose.activeElbow = activeShoulder
.clone()
.addScaledVector(direction, upperLength);
pose.activeHand = activeShoulder
.clone()
.addScaledVector(direction, pose.maxReach);
}
setSegment(upperArm, activeShoulder, pose.activeElbow, h * 0.04);
setSegment(lowerArm, pose.activeElbow, pose.activeHand, h * 0.031);
hand.position.copy(pose.activeHand);
// Free arm braces against the shelf when that contact is valid.
const freeShoulder = new V3(
shoulder.x,
shoulder.y,
-shoulderHalfWidth
);
const freeUpper = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.shirt
);
const freeLower = characterMesh(
new THREE.CylinderGeometry(1, 1, 1, 6),
materials.skin
);
const freeHand = characterMesh(
new THREE.IcosahedronGeometry(h * 0.043, 1),
materials.skin
);
const braceTarget = new V3(
0.735,
clamp(pose.contact.y - 0.1, 0.35, 1.75),
-0.19
);
if (freeShoulder.distanceTo(braceTarget) <= pose.maxReach * 0.98) {
const freeElbow = solveElbow(
freeShoulder,
braceTarget,
upperLength,
lowerLength,
new V3(0, -1, 0)
);
setSegment(freeUpper, freeShoulder, freeElbow, h * 0.04);
setSegment(freeLower, freeElbow, braceTarget, h * 0.031);
freeHand.position.copy(braceTarget);
} else {
const freeElbow = new V3(
shoulder.x - 0.06,
shoulder.y - torsoLength * 0.25,
-shoulderHalfWidth
);
const relaxedHand = new V3(
hip.x + 0.12,
hip.y + 0.04,
-shoulderHalfWidth
);
setSegment(freeUpper, freeShoulder, freeElbow, h * 0.04);
setSegment(freeLower, freeElbow, relaxedHand, h * 0.031);
freeHand.position.copy(relaxedHand);
}
}
// ---------------------------------------------------------------------
// Measurement lines and labels
// ---------------------------------------------------------------------
function createLabel(text, scale = 1) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(1.05 * scale, 0.26 * scale, 1);
sprite.renderOrder = 20;
sprite.userData = { canvas, context, texture };
drawLabel(sprite, text);
scene.add(sprite);
return sprite;
}
function drawLabel(sprite, text, color = "#f9f7f0") {
const { canvas, context, texture } = sprite.userData;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(20, 18);
context.lineTo(492, 18);
context.quadraticCurveTo(504, 18, 504, 32);
context.lineTo(504, 96);
context.quadraticCurveTo(504, 110, 490, 110);
context.lineTo(22, 110);
context.quadraticCurveTo(8, 110, 8, 96);
context.lineTo(8, 32);
context.quadraticCurveTo(8, 18, 22, 18);
context.closePath();
context.fillStyle = "rgba(38, 52, 58, 0.88)";
context.fill();
context.fillStyle = color;
context.font = "700 39px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
texture.needsUpdate = true;
}
function makeLine(color, opacity = 1, dashed = false) {
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.055,
gapSize: 0.035
})
: new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new V3(0, 0, 0),
new V3(0, 1, 0)
]),
material
);
scene.add(line);
return line;
}
function updateLine(line, a, b) {
line.geometry.dispose();
line.geometry = new THREE.BufferGeometry().setFromPoints([a, b]);
if (line.material.isLineDashedMaterial) line.computeLineDistances();
}
function updateTicks(line, x, height, z = 0.04) {
const points = [];
for (let y = 0; y <= height + 0.001; y += 0.2) {
points.push(new V3(x - 0.045, y, z));
points.push(new V3(x + 0.045, y, z));
}
points.push(new V3(x - 0.06, height, z));
points.push(new V3(x + 0.06, height, z));
line.geometry.dispose();
line.geometry = new THREE.BufferGeometry().setFromPoints(points);
}
const characterDimension = makeLine(0x65777c, 0.7);
const characterTicks = new THREE.LineSegments(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0x65777c,
transparent: true,
opacity: 0.7
})
);
scene.add(characterTicks);
const shelfDimension = makeLine(0x65777c, 0.7);
const shelfTicks = new THREE.LineSegments(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0x65777c,
transparent: true,
opacity: 0.7
})
);
scene.add(shelfTicks);
const characterLabel = createLabel("CHAR 1.72 m", 1.0);
const shelfLabel = createLabel("SHELF 1.15 m", 1.0);
const gapLabel = createLabel("24 cm REMAINING", 1.05);
const gapLine = makeLine(0xc85d50, 0.95, true);
// Standing marker on the floor.
const standingMarker = new THREE.Mesh(
new THREE.RingGeometry(0.19, 0.23, 32),
new THREE.MeshBasicMaterial({
color: 0x367f7d,
transparent: true,
opacity: 0.38,
side: THREE.DoubleSide
})
);
standingMarker.rotation.x = -Math.PI / 2;
standingMarker.position.set(0.2, 0.004, 0);
scene.add(standingMarker);
// ---------------------------------------------------------------------
// Reach solver and UI synchronization
// ---------------------------------------------------------------------
let currentPose = null;
function updateScene() {
const h = state.height;
const legLength = h * state.leg;
const torsoLength = h * (1 - state.leg - 0.19);
const nominalHipY = legLength;
const nominalShoulderY = nominalHipY + torsoLength * 0.9;
const targetY = state.shelf + 0.12;
// Low targets produce a deep crouch. High targets produce forward lean.
const lowDifference = nominalShoulderY - targetY;
const highDifference = targetY - nominalShoulderY;
const squat = clamp(
(lowDifference - 0.16) * 0.95,
0,
legLength * 0.65
);
const lean = clamp(
0.025 + Math.abs(targetY - nominalShoulderY) * 0.06,
0.025,
0.13
);
const hipX = 0.20;
const hipY = nominalHipY - squat;
const shoulderX = hipX + lean;
const shoulderY = hipY + torsoLength * 0.9;
const contact = new V3(0.955, targetY, 0);
const shoulder = new V3(
shoulderX,
shoulderY,
h * 0.09
);
const maxReach = h * state.arm;
const requiredReach = shoulder.distanceTo(contact);
const reachable = requiredReach <= maxReach;
const gap = Math.max(0, requiredReach - maxReach);
let poseName = "Neutral reach";
if (squat > 0.28) {
poseName = "Deep crouch";
} else if (squat > 0.08) {
poseName = "Low crouch";
} else if (highDifference > 0.38) {
poseName = "Forward lean";
} else if (highDifference > 0.15) {
poseName = "Raised reach";
} else if (lowDifference > 0.15) {
poseName = "Soft reach";
}
if (!reachable) poseName += " / strain";
currentPose = {
hipX,
hipY,
shoulderX,
shoulderY,
shoulder,
contact,
maxReach,
requiredReach,
reachable,
gap,
poseName,
activeElbow: null,
activeHand: null,
footTilt: highDifference > 0.3 ? -0.09 : 0
};
// Move shelf target.
activeBoard.position.y = state.shelf - 0.04;
targetGroup.position.set(1.075, targetY, 0);
contactRing.position.set(0.951, targetY, 0);
contactRingMaterial.color.set(reachable ? 0x438b67 : 0xd89532);
contactRingMaterial.emissive.set(reachable ? 0x173e2d : 0x593000);
buildCharacter(currentPose);
// Gap is explicitly visualized only for impossible configurations.
gapLine.visible = gap > 0.005;
gapLabel.visible = gap > 0.005;
if (gap > 0.005) {
updateLine(gapLine, currentPose.activeHand, contact);
const midpoint = currentPose.activeHand
.clone()
.lerp(contact, 0.5);
midpoint.y += 0.13;
gapLabel.position.copy(midpoint);
drawLabel(
gapLabel,
`${Math.round(gap * 100)} cm REMAINING`,
"#ffb3a8"
);
}
// Furniture and character dimensions.
updateLine(
characterDimension,
new V3(-0.30, 0, 0.06),
new V3(-0.30, h, 0.06)
);
updateTicks(characterTicks, -0.30, h);
characterLabel.position.set(-0.50, h * 0.58, 0.1);
drawLabel(characterLabel, `CHAR ${h.toFixed(2)} m`);
updateLine(
shelfDimension,
new V3(2.66, 0, 0.06),
new V3(2.66, state.shelf, 0.06)
);
updateTicks(shelfTicks, 2.66, state.shelf);
shelfLabel.position.set(2.87, state.shelf * 0.57, 0.1);
drawLabel(shelfLabel, `SHELF ${state.shelf.toFixed(2)} m`);
// Telemetry.
const telemetry = $("telemetry");
const statusPill = $("statusPill");
telemetry.classList.toggle("ok", reachable);
statusPill.classList.toggle("ok", reachable);
if (reachable) {
statusPill.textContent = "Target reached";
$("resultTitle").textContent = "Pose solved";
$("resultValue").textContent = "0 cm gap";
$("resultNote").textContent =
"The active arm reaches the target contact point exactly.";
} else {
statusPill.textContent = "Unreachable";
$("resultTitle").textContent = "Remaining gap";
$("resultValue").textContent = `${Math.round(gap * 100)} cm`;
$("resultNote").textContent =
"Target omitted from the solver; the arm stops at its maximum extension.";
}
$("poseValue").textContent = poseName;
$("shoulderValue").textContent = `${shoulderY.toFixed(2)} m`;
$("targetValue").textContent = `${targetY.toFixed(2)} m`;
$("requiredValue").textContent = `${requiredReach.toFixed(2)} m`;
$("reachValue").textContent = `${maxReach.toFixed(2)} m`;
$("reachFill").style.width = `${
Math.min(100, (maxReach / requiredReach) * 100)
}%`;
}
function updateControlLabels() {
$("shelfValue").textContent = `${state.shelf.toFixed(2)} m`;
$("heightValue").textContent = `${state.height.toFixed(2)} m`;
$("legValue").textContent = `${Math.round(state.leg * 100)}%`;
$("armValue").textContent = `${Math.round(state.arm * 100)}%`;
for (const key of ["shelf", "height", "leg", "arm"]) {
const input = $(key);
const min = parseFloat(input.min);
const max = parseFloat(input.max);
const value = parseFloat(input.value);
const fill = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${fill}%`);
}
}
for (const key of ["shelf", "height", "leg", "arm"]) {
$(key).addEventListener("input", (event) => {
state[key] = parseFloat(event.target.value);
updateControlLabels();
updateScene();
document.querySelectorAll(".preset").forEach((button) => {
button.classList.remove("active");
});
});
}
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
Object.assign(state, presets[button.dataset.preset]);
for (const key of ["shelf", "height", "leg", "arm"]) {
$(key).value = state[key];
}
document.querySelectorAll(".preset").forEach((item) => {
item.classList.toggle("active", item === button);
});
updateControlLabels();
updateScene();
});
});
// ---------------------------------------------------------------------
// Direct camera controls: orbit and zoom without external addons
// ---------------------------------------------------------------------
const focus = new V3(0.92, 1.05, 0);
let cameraRadius = 5.65;
let cameraTheta = 0.49;
let cameraPhi = 1.17;
function updateCamera() {
const spherical = new THREE.Spherical(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.setFromSpherical(spherical).add(focus);
camera.lookAt(focus);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = clamp(cameraPhi - dy * 0.005, 0.62, 1.43);
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();
cameraRadius = clamp(
cameraRadius + event.deltaY * 0.0035,
3.45,
9
);
updateCamera();
},
{ passive: false }
);
$("resetView").addEventListener("click", () => {
cameraRadius = 5.65;
cameraTheta = 0.49;
cameraPhi = 1.17;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
// ---------------------------------------------------------------------
// Start
// ---------------------------------------------------------------------
updateControlLabels();
updateScene();
updateCamera();
function animate(time) {
requestAnimationFrame(animate);
const pulse = 1 + Math.sin(time * 0.004) * 0.07;
contactRing.scale.setScalar(pulse);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```