Prompt specification
Build or customize the Three.js 3D scene "Pose Transfer Across Different Bodies" 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: Pose Transfer Across Different Bodies
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pose-transfer-across-different-bodies-x3700
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts character body proportions and recognizable silhouettes. Create a character rig experiment transferring a reach pose across different proportions. Adapt joint angles to target positions and show when a target falls outside reach. Low-poly.
Working Three.js Starter (pose-transfer-across-different-bodies-x3700):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reach Transfer — Three.js Rig Experiment</title>
<style>
:root {
color-scheme: dark;
--bg: #090d14;
--panel: rgba(16, 22, 33, 0.88);
--panel-line: rgba(255, 255, 255, 0.11);
--text: #edf3fb;
--muted: #8996aa;
--accent: #ffb35c;
--ok: #67e8b0;
--bad: #ff647c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
button,
input,
select {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
header {
position: fixed;
top: 26px;
left: 30px;
z-index: 5;
pointer-events: none;
max-width: 430px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 6px 10px;
border: 1px solid rgba(255, 179, 92, 0.3);
border-radius: 999px;
color: #ffd39e;
background: rgba(255, 179, 92, 0.07);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 13px var(--accent);
}
h1 {
margin: 12px 0 6px;
font-size: clamp(25px, 3vw, 43px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 760;
}
header p {
max-width: 390px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#panel {
position: fixed;
top: 22px;
right: 22px;
z-index: 10;
width: 306px;
max-height: calc(100vh - 44px);
overflow-y: auto;
padding: 18px;
border: 1px solid var(--panel-line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #384459 transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
}
.panel-title strong {
font-size: 13px;
letter-spacing: 0.02em;
}
.panel-title span {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section {
padding: 15px 0;
border-top: 1px solid var(--panel-line);
}
.section:first-of-type {
padding-top: 0;
border-top: 0;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: #cbd5e4;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-label small {
color: var(--muted);
font-size: 9px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: none;
}
select {
width: 100%;
height: 38px;
padding: 0 11px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
outline: none;
color: var(--text);
background: #121a27;
font-size: 12px;
}
.control {
margin-top: 12px;
}
.control label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
color: #aeb9ca;
font-size: 11px;
}
output {
min-width: 44px;
color: #e6edf7;
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0;
border-radius: 10px;
accent-color: var(--accent);
cursor: pointer;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
button {
min-height: 38px;
padding: 0 11px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
color: #dbe5f2;
background: #182231;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 150ms ease;
}
button:hover {
border-color: rgba(255, 179, 92, 0.45);
background: #202c3d;
transform: translateY(-1px);
}
button.primary {
color: #20170d;
border-color: transparent;
background: var(--accent);
}
button.primary:hover {
background: #ffc57e;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 0;
color: #aeb9ca;
font-size: 11px;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--accent);
cursor: pointer;
}
.note {
margin: 13px 0 0;
color: #718096;
font-size: 10px;
line-height: 1.55;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.rig-label {
position: absolute;
width: 218px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 11px;
background: rgba(10, 15, 23, 0.82);
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
transform: translate(-50%, 0);
transition: border-color 150ms ease, background 150ms ease;
}
.rig-label.selected {
background: rgba(18, 25, 37, 0.94);
}
.label-top {
display: flex;
align-items: center;
gap: 7px;
font-size: 11px;
}
.label-top strong {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.rig-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--rig-color);
box-shadow: 0 0 8px var(--rig-color);
}
.status {
padding: 3px 5px;
border-radius: 5px;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.06em;
}
.status.ok {
color: var(--ok);
background: rgba(103, 232, 176, 0.1);
}
.status.bad {
color: var(--bad);
background: rgba(255, 100, 124, 0.11);
}
.metrics {
display: flex;
gap: 9px;
margin-top: 7px;
color: #8290a5;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
#hint {
position: fixed;
left: 50%;
bottom: 20px;
z-index: 6;
padding: 8px 13px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 999px;
color: #8491a5;
background: rgba(10, 15, 23, 0.76);
font-size: 10px;
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
}
#toast {
position: fixed;
left: 50%;
top: 26px;
z-index: 20;
padding: 10px 15px;
border: 1px solid rgba(255, 179, 92, 0.25);
border-radius: 999px;
color: #ffe0b9;
background: rgba(27, 23, 19, 0.92);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.3);
font-size: 11px;
opacity: 0;
transform: translate(-50%, -12px);
transition: 200ms ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 800px) {
header {
top: 18px;
left: 18px;
max-width: 55vw;
}
header p {
display: none;
}
h1 {
font-size: 25px;
}
#panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(292px, calc(100vw - 24px));
max-height: 47vh;
padding: 14px;
}
#hint {
display: none;
}
.rig-label {
width: 166px;
padding: 8px;
}
.metrics {
gap: 6px;
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<header>
<div class="eyebrow">Procedural Rig Study</div>
<h1>Reach Transfer</h1>
<p>
One authored hand target, three silhouettes. Each rig independently solves
its shoulder and elbow angles from its own proportions.
</p>
</header>
<aside id="panel">
<div class="panel-title">
<strong>Rig Controls</strong>
<span>Two-bone IK</span>
</div>
<div class="section">
<div class="section-label">
Selected Character
<small>edit its local target</small>
</div>
<select id="rigSelect" aria-label="Selected character">
<option value="0">Compact</option>
<option value="1" selected>Balanced</option>
<option value="2">Lanky</option>
</select>
<div class="control">
<label for="targetX">Horizontal X <output id="targetXOut"></output></label>
<input id="targetX" type="range" min="-1.8" max="1.8" step="0.01">
</div>
<div class="control">
<label for="targetY">Height above pelvis <output id="targetYOut"></output></label>
<input id="targetY" type="range" min="-1.05" max="1.7" step="0.01">
</div>
<div class="control">
<label for="targetZ">Forward depth <output id="targetZOut"></output></label>
<input id="targetZ" type="range" min="-0.8" max="0.8" step="0.01">
</div>
<div class="button-row">
<button id="transferBtn" class="primary">Transfer to all</button>
<button id="resetBtn">Reset all</button>
</div>
<p class="note">
Transfer copies the selected rig's local target to the other rigs.
Each character then solves and validates that pose independently.
</p>
</div>
<div class="section">
<div class="section-label">
Selected Proportions
<small>live rebuild</small>
</div>
<div class="control">
<label for="stature">Stature <output id="statureOut"></output></label>
<input id="stature" type="range" min="0.75" max="1.25" step="0.01">
</div>
<div class="control">
<label for="shoulder">Shoulder span <output id="shoulderOut"></output></label>
<input id="shoulder" type="range" min="0.65" max="1.35" step="0.01">
</div>
<div class="control">
<label for="arm">Arm length <output id="armOut"></output></label>
<input id="arm" type="range" min="0.7" max="1.5" step="0.01">
</div>
<div class="control">
<label for="head">Head scale <output id="headOut"></output></label>
<input id="head" type="range" min="0.85" max="1.25" step="0.01">
</div>
</div>
<div class="section">
<div class="section-label">Display</div>
<label class="toggle">
Reach envelopes
<input id="showReach" type="checkbox" checked>
</label>
<label class="toggle">
Joint markers
<input id="showJoints" type="checkbox" checked>
</label>
<label class="toggle">
Character readouts
<input id="showLabels" type="checkbox" checked>
</label>
</div>
</aside>
<div id="labels"></div>
<div id="hint">Drag a target to edit · drag the scene to orbit · wheel to zoom</div>
<div id="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x090d14);
scene.fog = new THREE.FogExp2(0x090d14, 0.036);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.35, 0);
let cameraYaw = 0;
let cameraPitch = 0.1;
let cameraDistance = 11.7;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9d7ff, 0x111827, 1.75));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6e9fff, 2.2);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb45e, 28, 13, 2);
warmLight.position.set(4, 3.2, 3.5);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0d131d,
roughness: 0.94,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.065;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x314057, 0x1d2838);
grid.position.y = -0.055;
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// ------------------------------------------------------------
// Rig definitions and shared constants
// ------------------------------------------------------------
const DEFAULT_TARGET = { x: 1.35, y: 0.95, z: 0.15 };
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const clamp = THREE.MathUtils.clamp;
const definitions = [
{
name: "Compact",
baseX: -3.25,
color: 0x61d7c1,
params: {
stature: 0.82,
shoulder: 0.78,
arm: 0.76,
head: 1.12
}
},
{
name: "Balanced",
baseX: 0,
color: 0xffb35c,
params: {
stature: 1,
shoulder: 1,
arm: 1,
head: 1
}
},
{
name: "Lanky",
baseX: 3.25,
color: 0xa78bfa,
params: {
stature: 1.12,
shoulder: 1.08,
arm: 1.32,
head: 0.88
}
}
];
const rigs = definitions.map((definition, index) => ({
...definition,
index,
initial: structuredClone(definition.params),
params: { ...definition.params },
target: { ...DEFAULT_TARGET },
root: new THREE.Group(),
materials: createMaterials(definition.color),
metrics: {},
transferPulse: 0
}));
const state = {
selected: 1,
showReach: true,
showJoints: true,
showLabels: true
};
function createMaterials(accentColor) {
return {
body: new THREE.MeshStandardMaterial({
color: 0x263448,
roughness: 0.82,
metalness: 0.06,
flatShading: true
}),
bodyDark: new THREE.MeshStandardMaterial({
color: 0x182332,
roughness: 0.88,
metalness: 0.04,
flatShading: true
}),
skin: new THREE.MeshStandardMaterial({
color: 0xd4b39b,
roughness: 0.78,
flatShading: true
}),
hair: new THREE.MeshStandardMaterial({
color: 0x141b25,
roughness: 0.95,
flatShading: true
}),
eye: new THREE.MeshBasicMaterial({
color: 0x101722,
toneMapped: false
}),
accent: new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.12,
roughness: 0.48,
metalness: 0.12,
flatShading: true
}),
target: new THREE.MeshBasicMaterial({
color: 0x67e8b0,
transparent: true,
opacity: 0.96,
depthTest: false,
toneMapped: false
}),
targetLine: new THREE.LineBasicMaterial({
color: 0x67e8b0,
transparent: true,
opacity: 0.8,
depthTest: false,
toneMapped: false
}),
reachSphere: new THREE.MeshBasicMaterial({
color: accentColor,
wireframe: true,
transparent: true,
opacity: 0.035,
depthWrite: false,
toneMapped: false
}),
reachRing: new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.3,
depthWrite: false,
toneMapped: false
}),
outside: new THREE.LineDashedMaterial({
color: 0xff647c,
dashSize: 0.08,
gapSize: 0.045,
transparent: true,
opacity: 0.95,
toneMapped: false
})
};
}
function disposeGeometry(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
group.clear();
}
function setSegment(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = Math.max(direction.length(), 0.0001);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
mesh.scale.set(1, length, 1);
}
function createSegment(parent, material, radiusTop, radiusBottom) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
1,
6,
1,
false
),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addJoint(parent, material, position, radius, joints) {
const joint = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 1),
material
);
joint.position.copy(position);
joint.castShadow = true;
parent.add(joint);
joints.push(joint);
return joint;
}
function deriveDimensions(rig) {
const p = rig.params;
const stature = p.stature;
return {
stature,
pelvisY: 1.28 * stature,
shoulderY: 2.05 * stature,
halfShoulder: 0.39 * stature * p.shoulder,
upperArm: 0.62 * stature * p.arm,
lowerArm: 0.58 * stature * p.arm,
headRadius: 0.25 * stature * p.head,
torsoLength: 0.77 * stature,
legLength: 1.18 * stature
};
}
// ------------------------------------------------------------
// Build each low-poly character
// ------------------------------------------------------------
function buildRig(rig) {
disposeGeometry(rig.root);
rig.root.position.x = rig.baseX;
rig.jointMeshes = [];
const d = deriveDimensions(rig);
rig.dimensions = d;
const m = rig.materials;
// Pelvis
const pelvis = new THREE.Mesh(
new THREE.BoxGeometry(
0.58 * d.stature * rig.params.shoulder,
0.3 * d.stature,
0.38 * d.stature
),
m.bodyDark
);
pelvis.position.y = d.pelvisY;
pelvis.castShadow = true;
pelvis.receiveShadow = true;
rig.root.add(pelvis);
// Faceted torso
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(
0.34 * d.stature * rig.params.shoulder,
0.27 * d.stature,
d.torsoLength,
6,
1,
false
),
m.body
);
torso.position.y = d.pelvisY + d.torsoLength / 2;
torso.scale.z = 0.78;
torso.castShadow = true;
torso.receiveShadow = true;
rig.root.add(torso);
// Chest accent plate
const chest = new THREE.Mesh(
new THREE.BoxGeometry(
0.27 * d.stature,
0.3 * d.stature,
0.045
),
m.accent
);
chest.position.set(
0,
d.pelvisY + d.torsoLength * 0.63,
0.285 * d.stature
);
chest.rotation.z = -0.08;
chest.castShadow = true;
rig.root.add(chest);
// Neck
const neck = createSegment(
rig.root,
m.skin,
0.075 * d.stature,
0.09 * d.stature
);
setSegment(
neck,
new THREE.Vector3(0, d.shoulderY - 0.01, 0),
new THREE.Vector3(0, d.shoulderY + 0.2 * d.stature, 0)
);
// Head
const headCenter = d.shoulderY + 0.43 * d.stature;
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(d.headRadius, 1),
m.skin
);
head.position.set(0, headCenter, 0);
head.scale.set(0.9, 1.08, 0.9);
head.castShadow = true;
rig.root.add(head);
// Hair cap
const hair = new THREE.Mesh(
new THREE.SphereGeometry(
d.headRadius * 1.035,
8,
5,
0,
Math.PI * 2,
0,
Math.PI * 0.5
),
m.hair
);
hair.position.set(0, headCenter + 0.025 * d.stature, -0.008);
hair.scale.set(0.91, 0.92, 0.91);
hair.castShadow = true;
rig.root.add(hair);
// Nose gives the otherwise abstract mannequin a clear facing direction.
const nose = new THREE.Mesh(
new THREE.ConeGeometry(0.045, 0.11, 4),
m.skin
);
nose.rotation.x = Math.PI / 2;
nose.position.set(
0,
headCenter - 0.015,
d.headRadius * 0.86
);
nose.castShadow = true;
rig.root.add(nose);
// Eyes
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(
new THREE.SphereGeometry(0.025, 6, 4),
m.eye
);
eye.position.set(
side * 0.075 * d.stature,
headCenter + 0.035 * d.stature,
d.headRadius * 0.76
);
rig.root.add(eye);
}
// Legs
for (const side of [-1, 1]) {
const hip = new THREE.Vector3(
side * 0.16 * d.stature,
d.pelvisY - 0.02,
0
);
const knee = new THREE.Vector3(
side * 0.2 * d.stature,
0.62 * d.stature,
0.025
);
const ankle = new THREE.Vector3(
side * 0.235 * d.stature,
0.13,
0
);
const thigh = createSegment(
rig.root,
m.bodyDark,
0.105 * d.stature,
0.125 * d.stature
);
setSegment(thigh, hip, knee);
const shin = createSegment(
rig.root,
m.body,
0.075 * d.stature,
0.095 * d.stature
);
setSegment(shin, knee, ankle);
addJoint(
rig.root,
m.accent,
hip,
0.075 * d.stature,
rig.jointMeshes
);
addJoint(
rig.root,
m.accent,
knee,
0.07 * d.stature,
rig.jointMeshes
);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(
0.18 * d.stature,
0.13 * d.stature,
0.34 * d.stature
),
m.bodyDark
);
foot.position.set(
ankle.x,
0.075,
0.075
);
foot.castShadow = true;
foot.receiveShadow = true;
rig.root.add(foot);
}
// Shoulder caps
for (const side of [-1, 1]) {
addJoint(
rig.root,
m.accent,
new THREE.Vector3(
side * d.halfShoulder,
d.shoulderY,
0
),
0.095 * d.stature,
rig.jointMeshes
);
}
// Dynamic arm
rig.upperArm = createSegment(
rig.root,
m.body,
0.083 * d.stature,
0.11 * d.stature
);
rig.lowerArm = createSegment(
rig.root,
m.skin,
0.068 * d.stature,
0.084 * d.stature
);
rig.elbow = addJoint(
rig.root,
m.accent,
new THREE.Vector3(),
0.08 * d.stature,
rig.jointMeshes
);
rig.hand = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105 * d.stature, 0),
m.skin
);
rig.hand.scale.set(0.9, 1.08, 0.78);
rig.hand.castShadow = true;
rig.root.add(rig.hand);
// Reach envelope
rig.reachGroup = new THREE.Group();
rig.root.add(rig.reachGroup);
const reachSphere = new THREE.Mesh(
new THREE.SphereGeometry(
d.upperArm + d.lowerArm,
16,
10
),
m.reachSphere
);
reachSphere.position.set(0, d.shoulderY, 0);
rig.reachGroup.add(reachSphere);
const reachRing = new THREE.Mesh(
new THREE.TorusGeometry(
d.upperArm + d.lowerArm,
0.009,
4,
72
),
m.reachRing
);
reachRing.position.set(0, d.shoulderY, 0);
rig.reachGroup.add(reachRing);
// Target marker
rig.targetGroup = new THREE.Group();
rig.root.add(rig.targetGroup);
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.019, 6, 24),
m.target
);
rig.targetGroup.add(targetRing);
const outerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.205, 0.007, 4, 24),
m.target
);
rig.targetGroup.add(outerRing);
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 8, 6),
m.target
);
rig.targetGroup.add(dot);
const crossGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.075, 0, 0),
new THREE.Vector3(0.075, 0, 0),
new THREE.Vector3(0, -0.075, 0),
new THREE.Vector3(0, 0.075, 0)
]);
rig.targetGroup.add(new THREE.LineSegments(crossGeometry, m.targetLine));
// Larger invisible hit area for easy dragging.
rig.hit = new THREE.Mesh(
new THREE.SphereGeometry(0.24, 8, 6),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
rig.hit.userData.rigIndex = rig.index;
rig.targetGroup.add(rig.hit);
// Dashed line shown when the requested target cannot be reached.
rig.outsideLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
m.outside
);
rig.outsideLine.visible = false;
rig.root.add(rig.outsideLine);
rig.root.updateMatrixWorld(true);
}
// Character platforms
for (const rig of rigs) {
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(1.08, 1.16, 0.09, 32),
new THREE.MeshStandardMaterial({
color: 0x111925,
roughness: 0.86,
metalness: 0.08
})
);
platform.position.set(rig.baseX, 0, 0);
platform.receiveShadow = true;
scene.add(platform);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.08, 0.012, 5, 64),
new THREE.MeshBasicMaterial({
color: rig.color,
transparent: true,
opacity: 0.38,
toneMapped: false
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(rig.baseX, 0.052, 0);
scene.add(ring);
rig.root.position.x = rig.baseX;
scene.add(rig.root);
buildRig(rig);
}
// ------------------------------------------------------------
// Two-bone IK and reach validation
// ------------------------------------------------------------
const tempDirection = new THREE.Vector3();
const tempBend = new THREE.Vector3();
function solveRig(rig, time) {
const d = rig.dimensions;
const target = rig.target;
const side = target.x >= 0 ? 1 : -1;
const shoulder = new THREE.Vector3(
side * d.halfShoulder,
d.shoulderY,
0
);
const desiredTarget = new THREE.Vector3(
target.x,
d.pelvisY + target.y,
target.z
);
const toTarget = new THREE.Vector3().subVectors(
desiredTarget,
shoulder
);
const distance = toTarget.length();
const maximumReach = d.upperArm + d.lowerArm;
const minimumReach = Math.abs(d.upperArm - d.lowerArm);
const outside =
distance > maximumReach + 0.0001 ||
distance < minimumReach - 0.0001;
let effectiveDistance = distance;
if (effectiveDistance < 0.00001) {
tempDirection.set(1, 0, 0);
effectiveDistance = 0.00001;
} else {
tempDirection.copy(toTarget).normalize();
}
const solvedDistance = clamp(
effectiveDistance,
minimumReach + 0.00001,
maximumReach - 0.00001
);
// Law of cosines: angle between upper arm and shoulder-to-target line.
const cosShoulderAngle = clamp(
(
d.upperArm * d.upperArm -
d.lowerArm * d.lowerArm +
solvedDistance * solvedDistance
) /
(2 * d.upperArm * solvedDistance),
-1,
1
);
const shoulderOffset = d.upperArm * Math.acos(cosShoulderAngle);
const bendHeight = Math.sqrt(Math.max(
0,
d.upperArm * d.upperArm - shoulderOffset * shoulderOffset
));
// Prefer an outward and downward elbow plane.
tempBend.set(side * 0.82, -1, 0.12);
tempBend.addScaledVector(
tempDirection,
-tempBend.dot(tempDirection)
);
if (tempBend.lengthSq() < 0.00001) {
tempBend.set(side, 0, 0);
}
tempBend.normalize();
const elbow = shoulder.clone()
.addScaledVector(tempDirection, shoulderOffset)
.addScaledVector(tempBend, bendHeight);
let handPosition;
if (distance > maximumReach) {
handPosition = shoulder.clone()
.addScaledVector(tempDirection, maximumReach);
} else if (distance < minimumReach) {
handPosition = shoulder.clone()
.addScaledVector(tempDirection, minimumReach);
} else {
handPosition = desiredTarget.clone();
}
setSegment(rig.upperArm, shoulder, elbow);
setSegment(rig.lowerArm, elbow, handPosition);
rig.elbow.position.copy(elbow);
rig.hand.position.copy(handPosition);
// Readout angles
const upperDirection = elbow.clone().sub(shoulder).normalize();
const shoulderAngle = Math.acos(clamp(
-upperDirection.y,
-1,
1
)) * THREE.MathUtils.RAD2DEG;
const towardShoulder = shoulder.clone().sub(elbow).normalize();
const towardHand = handPosition.clone().sub(elbow).normalize();
const elbowAngle = Math.acos(clamp(
towardShoulder.dot(towardHand),
-1,
1
)) * THREE.MathUtils.RAD2DEG;
const reachPercent = distance / maximumReach * 100;
rig.metrics = {
distance,
maximumReach,
shoulderAngle,
elbowAngle,
reachPercent,
outside,
arm: side > 0 ? "R" : "L"
};
// Target color communicates whether the requested point is valid.
const statusColor = outside ? 0xff647c : 0x67e8b0;
rig.materials.target.color.setHex(statusColor);
rig.materials.targetLine.color.setHex(statusColor);
rig.materials.outside.color.setHex(statusColor);
rig.targetGroup.position.copy(desiredTarget);
rig.targetGroup.rotation.z = time * 0.28;
rig.transferPulse = Math.max(0, rig.transferPulse - 0.016);
const pulse =
Math.sin((1 - rig.transferPulse) * Math.PI) *
rig.transferPulse;
const selectedScale = rig.index === state.selected ? 1.15 : 1;
rig.targetGroup.scale.setScalar(selectedScale * (1 + pulse * 0.24));
rig.reachGroup.visible = state.showReach;
rig.materials.reachSphere.opacity =
rig.index === state.selected ? 0.075 : 0.035;
rig.materials.reachRing.opacity =
rig.index === state.selected ? 0.55 : 0.24;
for (const joint of rig.jointMeshes) {
joint.visible = state.showJoints;
}
// The solid arm ends at the closest reachable point. The dashed line
// continues to the requested target when it lies outside the envelope.
rig.outsideLine.visible = outside;
if (outside) {
const positions = rig.outsideLine.geometry.attributes.position;
positions.setXYZ(0, handPosition.x, handPosition.y, handPosition.z);
positions.setXYZ(
1,
desiredTarget.x,
desiredTarget.y,
desiredTarget.z
);
positions.needsUpdate = true;
rig.outsideLine.geometry.computeBoundingSphere();
rig.outsideLine.computeLineDistances();
}
updateReadout(rig);
}
// ------------------------------------------------------------
// DOM readouts
// ------------------------------------------------------------
const labelContainer = document.querySelector("#labels");
for (const rig of rigs) {
const label = document.createElement("div");
label.className = "rig-label";
label.style.setProperty(
"--rig-color",
`#${rig.color.toString(16).padStart(6, "0")}`
);
label.innerHTML = `
<div class="label-top">
<span class="rig-dot"></span>
<strong></strong>
<span class="status ok">REACHABLE</span>
</div>
<div class="metrics">
<span class="reach"></span>
<span class="angles"></span>
</div>
`;
labelContainer.appendChild(label);
rig.label = label;
}
function updateReadout(rig) {
const custom =
Object.keys(rig.params).some(
key => Math.abs(rig.params[key] - rig.initial[key]) > 0.001
);
rig.label.querySelector("strong").textContent =
`${rig.name}${custom ? " · custom" : ""}`;
const status = rig.label.querySelector(".status");
status.textContent = rig.metrics.outside ? "OUTSIDE" : "REACHABLE";
status.className = `status ${rig.metrics.outside ? "bad" : "ok"}`;
rig.label.querySelector(".reach").textContent =
`${Math.round(rig.metrics.reachPercent)}% reach`;
rig.label.querySelector(".angles").textContent =
`${rig.metrics.shoulderAngle.toFixed(0)}° shoulder · ` +
`${rig.metrics.elbowAngle.toFixed(0)}° elbow`;
rig.label.style.borderColor = rig.index === state.selected
? `#${rig.color.toString(16).padStart(6, "0")}`
: "rgba(255,255,255,0.12)";
}
function projectLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const rig of rigs) {
const point = new THREE.Vector3(
rig.baseX,
0.025,
1.02
).project(camera);
const x = (point.x * 0.5 + 0.5) * width;
const y = (-point.y * 0.5 + 0.5) * height;
rig.label.style.left = `${x}px`;
rig.label.style.top = `${y}px`;
rig.label.style.display =
state.showLabels && point.z < 1 ? "block" : "none";
}
}
// ------------------------------------------------------------
// Control synchronization
// ------------------------------------------------------------
const targetX = document.querySelector("#targetX");
const targetY = document.querySelector("#targetY");
const targetZ = document.querySelector("#targetZ");
const stature = document.querySelector("#stature");
const shoulder = document.querySelector("#shoulder");
const arm = document.querySelector("#arm");
const head = document.querySelector("#head");
function syncControls() {
const rig = rigs[state.selected];
document.querySelector("#rigSelect").value = state.selected;
targetX.value = rig.target.x;
targetY.value = rig.target.y;
targetZ.value = rig.target.z;
stature.value = rig.params.stature;
shoulder.value = rig.params.shoulder;
arm.value = rig.params.arm;
head.value = rig.params.head;
document.querySelector("#targetXOut").value = `${rig.target.x.toFixed(2)} m`;
document.querySelector("#targetYOut").value = `${rig.target.y.toFixed(2)} m`;
document.querySelector("#targetZOut").value = `${rig.target.z.toFixed(2)} m`;
document.querySelector("#statureOut").value = `${rig.params.stature.toFixed(2)}×`;
document.querySelector("#shoulderOut").value = `${rig.params.shoulder.toFixed(2)}×`;
document.querySelector("#armOut").value = `${rig.params.arm.toFixed(2)}×`;
document.querySelector("#headOut").value = `${rig.params.head.toFixed(2)}×`;
}
function bindTargetInput(input, key) {
input.addEventListener("input", () => {
rigs[state.selected].target[key] = Number(input.value);
syncControls();
});
}
bindTargetInput(targetX, "x");
bindTargetInput(targetY, "y");
bindTargetInput(targetZ, "z");
function bindProportionInput(input, key) {
input.addEventListener("input", () => {
const rig = rigs[state.selected];
rig.params[key] = Number(input.value);
buildRig(rig);
syncControls();
});
}
bindProportionInput(stature, "stature");
bindProportionInput(shoulder, "shoulder");
bindProportionInput(arm, "arm");
bindProportionInput(head, "head");
document.querySelector("#rigSelect").addEventListener("change", event => {
state.selected = Number(event.target.value);
syncControls();
});
document.querySelector("#showReach").addEventListener("change", event => {
state.showReach = event.target.checked;
});
document.querySelector("#showJoints").addEventListener("change", event => {
state.showJoints = event.target.checked;
});
document.querySelector("#showLabels").addEventListener("change", event => {
state.showLabels = event.target.checked;
});
// ------------------------------------------------------------
// Pose transfer feedback
// ------------------------------------------------------------
const toast = document.querySelector("#toast");
let toastTimer = 0;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 1900);
}
let transferBeam = null;
let transferBeamLife = 0;
document.querySelector("#transferBtn").addEventListener("click", () => {
const source = rigs[state.selected];
for (const rig of rigs) {
if (rig !== source) {
rig.target = { ...source.target };
rig.transferPulse = 1;
}
}
if (transferBeam) {
scene.remove(transferBeam);
transferBeam.geometry.dispose();
transferBeam.material.dispose();
}
const points = [];
for (const rig of rigs) {
if (rig === source) continue;
points.push(
new THREE.Vector3(
source.baseX + source.target.x,
source.dimensions.pelvisY + source.target.y,
source.target.z
),
new THREE.Vector3(
rig.baseX + rig.target.x,
rig.dimensions.pelvisY + rig.target.y,
rig.target.z
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: source.color,
transparent: true,
opacity: 0.8,
toneMapped: false
});
transferBeam = new THREE.LineSegments(geometry, material);
scene.add(transferBeam);
transferBeamLife = 1;
showToast(`Pose transferred from ${source.name}`);
});
document.querySelector("#resetBtn").addEventListener("click", () => {
for (const rig of rigs) {
rig.params = { ...rig.initial };
rig.target = { ...DEFAULT_TARGET };
rig.transferPulse = 0;
buildRig(rig);
}
state.selected = 1;
syncControls();
showToast("Experiment reset");
});
// ------------------------------------------------------------
// Direct camera controls and target dragging
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane();
let pointerMode = null;
let activePointer = null;
let previousX = 0;
let previousY = 0;
let dragOffset = new THREE.Vector2();
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function hitTestTarget() {
const targets = rigs
.map(rig => rig.hit)
.filter(Boolean);
const hits = raycaster.intersectObjects(targets, false);
return hits.length ? hits[0] : null;
}
canvas.addEventListener("pointerdown", event => {
if (activePointer !== null) return;
activePointer = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
setPointer(event);
const hit = hitTestTarget();
if (hit && event.button === 0) {
const rig = rigs[hit.object.userData.rigIndex];
state.selected = rig.index;
syncControls();
const point = new THREE.Vector3();
const plane = new THREE.Plane(
new THREE.Vector3(0, 0, 1),
-rig.target.z
);
if (raycaster.ray.intersectPlane(plane, point)) {
dragOffset.set(
point.x - (rig.baseX + rig.target.x),
point.y - (rig.dimensions.pelvisY + rig.target.y)
);
} else {
dragOffset.set(0, 0);
}
dragPlane.copy(plane);
pointerMode = "target";
canvas.style.cursor = "grabbing";
} else {
pointerMode = event.shiftKey ? "pan" : "orbit";
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", event => {
if (activePointer !== event.pointerId) {
setPointer(event);
const hover = hitTestTarget();
canvas.style.cursor = hover ? "pointer" : "grab";
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (pointerMode === "orbit") {
cameraYaw -= dx * 0.0055;
cameraPitch = clamp(
cameraPitch + dy * 0.004,
-0.08,
0.58
);
updateCamera();
} else if (pointerMode === "pan") {
cameraTarget.x -= dx * cameraDistance * 0.0009;
cameraTarget.y += dy * cameraDistance * 0.0009;
cameraTarget.y = clamp(cameraTarget.y, 0.4, 3.2);
updateCamera();
} else if (pointerMode === "target") {
setPointer(event);
const point = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragPlane, point)) {
const rig = rigs[state.selected];
rig.target.x = clamp(
point.x - rig.baseX - dragOffset.x,
-1.8,
1.8
);
rig.target.y = clamp(
point.y - rig.dimensions.pelvisY - dragOffset.y,
-1.05,
1.7
);
syncControls();
}
}
});
function endPointer(event) {
if (event.pointerId !== activePointer) return;
activePointer = null;
pointerMode = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraDistance = clamp(
cameraDistance + event.deltaY * 0.009,
7.5,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", event => {
event.preventDefault();
});
canvas.addEventListener("dblclick", () => {
cameraYaw = 0;
cameraPitch = 0.1;
cameraDistance =
window.innerWidth / window.innerHeight < 0.8 ? 17 : 11.7;
cameraTarget.set(0, 1.35, 0);
updateCamera();
});
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (width / height < 0.8) {
cameraDistance = Math.max(cameraDistance, 16);
}
updateCamera();
}
window.addEventListener("resize", resize);
syncControls();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
for (const rig of rigs) {
solveRig(rig, time);
}
if (transferBeam) {
transferBeamLife -= delta * 0.9;
transferBeam.material.opacity = Math.max(0, transferBeamLife);
if (transferBeamLife <= 0) {
scene.remove(transferBeam);
transferBeam.geometry.dispose();
transferBeam.material.dispose();
transferBeam = null;
}
}
projectLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```