Prompt specification
Build or customize the Three.js 3D scene "Accessory Attachment Failure Explorer" 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: Accessory Attachment Failure Explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/accessory-attachment-failure-explorer-x3708
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring clothing layers and attachments on moving characters. Create a character rig with selectable attachment points for a bag and hat. Compare valid and intentionally wrong parenting so motion reveals the structural difference. Voxel style.
Working Three.js Starter (accessory-attachment-failure-explorer-x3708):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Attachment Lab</title>
<style>
:root {
--ink: #17202a;
--panel: rgba(247, 249, 252, 0.92);
--line: rgba(23, 32, 42, 0.14);
--green: #079c83;
--green-light: #d9f6ee;
--red: #d74662;
--red-light: #ffe2e8;
--purple: #7157d9;
--yellow: #f3b82f;
--shadow: 0 16px 45px rgba(29, 39, 54, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8ef;
color: var(--ink);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene,
#scene canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#scene canvas {
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.75);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px 17px;
border-radius: 4px 18px 18px 18px;
}
.eyebrow {
color: var(--purple);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: clamp(23px, 3vw, 38px);
line-height: 0.98;
letter-spacing: -0.055em;
}
#title p {
margin: 0;
max-width: 340px;
color: #536171;
font-size: 12px;
line-height: 1.55;
}
#comparison {
position: absolute;
top: 22px;
right: 22px;
width: 370px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.comparison-card {
min-width: 0;
padding: 14px;
border-radius: 4px 16px 16px 16px;
}
.comparison-card.valid {
border-top: 4px solid var(--green);
}
.comparison-card.wrong {
border-top: 4px solid var(--red);
}
.comparison-card h2 {
margin: 0 0 12px;
font-size: 11px;
letter-spacing: 0.07em;
}
.comparison-line {
margin-top: 9px;
}
.comparison-line > span {
display: block;
margin-bottom: 4px;
color: #657181;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
}
.comparison-line b {
display: block;
margin-bottom: 4px;
font-size: 11px;
}
.comparison-line b.good {
color: var(--green);
}
.comparison-line b.bad {
color: var(--red);
}
.comparison-line code {
display: block;
overflow-wrap: anywhere;
color: #6a7583;
font-size: 9px;
line-height: 1.45;
}
#controls {
position: absolute;
right: 22px;
bottom: 22px;
width: 370px;
padding: 16px;
border-radius: 18px 18px 4px 18px;
pointer-events: auto;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.control-heading strong {
font-size: 12px;
letter-spacing: 0.08em;
}
.control-heading span {
color: #7b8694;
font-size: 10px;
}
.control-label {
display: block;
margin: 12px 0 7px;
color: #697585;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
}
.segmented.three {
grid-template-columns: repeat(4, 1fr);
}
.segmented button,
.socket-button,
.small-button {
min-height: 35px;
padding: 8px 7px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.75);
cursor: pointer;
font-size: 10px;
font-weight: 800;
transition:
background 120ms ease,
border-color 120ms ease,
transform 120ms ease;
}
.segmented button:hover,
.socket-button:hover,
.small-button:hover {
transform: translateY(-1px);
border-color: rgba(113, 87, 217, 0.45);
}
.segmented button.active,
.socket-button.active {
color: white;
border-color: var(--purple);
background: var(--purple);
}
.socket-button {
position: relative;
}
.socket-button::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
background: var(--yellow);
box-shadow: 0 0 0 2px rgba(243, 184, 47, 0.2);
}
.options {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.options label {
display: flex;
align-items: center;
gap: 7px;
color: #586575;
font-size: 10px;
cursor: pointer;
}
.options input {
accent-color: var(--purple);
}
.small-button {
min-height: 30px;
padding: 6px 10px;
}
.timeline {
display: grid;
grid-template-columns: 72px 1fr 35px;
align-items: center;
gap: 8px;
margin-top: 12px;
}
.timeline button {
min-height: 30px;
border: 1px solid var(--line);
border-radius: 8px;
background: white;
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.timeline input {
width: 100%;
accent-color: var(--purple);
}
.timeline output {
color: #6b7685;
font-size: 10px;
text-align: right;
}
#instructions {
position: absolute;
left: 22px;
bottom: 22px;
width: min(390px, calc(100vw - 44px));
padding: 14px 16px;
border-radius: 4px 14px 14px 14px;
color: #536171;
font-size: 11px;
line-height: 1.65;
}
#instructions strong {
color: var(--ink);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 7px;
color: #6d7988;
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.07em;
}
.legend span::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
background: var(--green);
}
.legend span:nth-child(2)::before {
background: var(--red);
}
.legend span:nth-child(3)::before {
background: var(--yellow);
}
#tooltip {
position: fixed;
z-index: 20;
display: none;
max-width: 230px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 7px;
background: rgba(23, 32, 42, 0.9);
color: white;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.18);
pointer-events: none;
font-size: 10px;
line-height: 1.45;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #dce8ef;
color: #435064;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 820px) {
#title {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 15px;
}
#title p {
display: none;
}
#comparison {
top: 88px;
left: 10px;
right: 10px;
width: auto;
}
.comparison-card {
padding: 10px;
}
.comparison-line code {
display: none;
}
#controls {
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 43vh;
overflow-y: auto;
padding: 12px;
}
#instructions {
display: none;
}
}
@media (max-height: 690px) and (min-width: 821px) {
#title {
padding: 12px 15px;
}
#title p {
display: none;
}
#comparison {
width: 330px;
}
.comparison-card {
padding: 10px;
}
.comparison-line code {
display: none;
}
#controls {
width: 330px;
padding: 12px;
}
#instructions {
width: 330px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="loading">Building voxel rigs…</div>
<main class="hud">
<header id="title" class="panel">
<div class="eyebrow">Hierarchy Test / 02</div>
<h1>ATTACHMENT<br>LAB</h1>
<p>
Clothing is layered onto a moving voxel skeleton. Attachments reveal
the difference between inheriting a correct transform and merely
appearing nearby.
</p>
</header>
<section id="comparison" aria-label="Parenting comparison">
<article class="comparison-card valid panel">
<h2>VALID HIERARCHY</h2>
<div class="comparison-line">
<span>BAG</span>
<b id="referenceBagState" class="good">HAND SOCKET</b>
<code id="referenceBagPath"></code>
</div>
<div class="comparison-line">
<span>HAT</span>
<b id="referenceHatState" class="good">HEAD SOCKET</b>
<code id="referenceHatPath"></code>
</div>
</article>
<article class="comparison-card wrong panel">
<h2>INTENTIONALLY WRONG</h2>
<div class="comparison-line">
<span>BAG</span>
<b id="brokenBagState" class="bad">TORSO SOCKET</b>
<code id="brokenBagPath"></code>
</div>
<div class="comparison-line">
<span>HAT</span>
<b id="brokenHatState" class="bad">TORSO SOCKET</b>
<code id="brokenHatPath"></code>
</div>
</article>
</section>
<section id="controls" class="panel">
<div class="control-heading">
<strong>RIG EDITOR</strong>
<span>Click a 3D socket or use the buttons</span>
</div>
<span class="control-label">SELECT ACCESSORY</span>
<div class="segmented" id="accessoryButtons">
<button type="button" data-accessory="bag" class="active">BAG</button>
<button type="button" data-accessory="hat">HAT</button>
</div>
<span class="control-label">EDIT RIG</span>
<div class="segmented" id="targetButtons">
<button type="button" data-target="reference">VALID RIG</button>
<button type="button" data-target="broken" class="active">WRONG RIG</button>
</div>
<span class="control-label">ATTACH TO</span>
<div class="segmented three" id="socketButtons">
<button type="button" data-socket="head">HEAD</button>
<button type="button" data-socket="hand">HAND</button>
<button type="button" data-socket="torso">TORSO</button>
<button type="button" data-socket="root">ROOT</button>
</div>
<div class="options">
<label>
<input type="checkbox" id="showLinks" checked>
Hierarchy links
</label>
<button type="button" class="small-button" id="resetRig">
RESET DEMO
</button>
</div>
<div class="timeline">
<button type="button" id="playPause">PAUSE</button>
<input
id="speed"
type="range"
min="0.25"
max="2"
value="1"
step="0.05"
aria-label="Animation speed"
>
<output id="speedValue">1.0×</output>
</div>
</section>
<aside id="instructions" class="panel">
<strong>Motion test:</strong> the valid bag follows the swinging hand,
and the valid hat follows the turning head. The wrong attachments remain
on the torso.
<div class="legend">
<span>Valid socket</span>
<span>Wrong rig</span>
<span>Selected</span>
</div>
<div style="margin-top:8px">
Drag to orbit · wheel to zoom · click sockets to reparent
</div>
</aside>
</main>
<div id="tooltip"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and application state
// -------------------------------------------------------------------------
const dom = {
scene: document.querySelector("#scene"),
loading: document.querySelector("#loading"),
tooltip: document.querySelector("#tooltip"),
accessoryButtons: document.querySelector("#accessoryButtons"),
targetButtons: document.querySelector("#targetButtons"),
socketButtons: document.querySelector("#socketButtons"),
showLinks: document.querySelector("#showLinks"),
resetRig: document.querySelector("#resetRig"),
playPause: document.querySelector("#playPause"),
speed: document.querySelector("#speed"),
speedValue: document.querySelector("#speedValue")
};
const state = {
accessory: "bag",
target: "broken",
socket: "hand",
paused: false,
speed: 1,
showLinks: true
};
// -------------------------------------------------------------------------
// Renderer, scene, camera and lights
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8ef);
scene.fog = new THREE.Fog(0xdce8ef, 13, 28);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.5));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel scene comparing valid and invalid attachment parenting"
);
dom.scene.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 1.45, 0);
const cameraState = {
radius: 12.2,
theta: 0.18,
phi: 1.02
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x71806d, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d4, 3.2);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x9ecbff, 1.15);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.0,
flatShading: true,
...options
});
}
function makeBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, background, foreground = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(255,255,255,0.24)";
ctx.lineWidth = 8;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
ctx.fillStyle = foreground;
ctx.font = "bold 43px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.8, 0.45, 1);
sprite.renderOrder = 30;
return sprite;
}
// -------------------------------------------------------------------------
// Voxel environment
// -------------------------------------------------------------------------
const groundMaterial = standardMaterial(0xcbd9d7);
makeBox(scene, [24, 0.3, 18], [0, -0.17, 0], groundMaterial);
const laneMaterialA = standardMaterial(0xb5d7d1);
const laneMaterialB = standardMaterial(0xa6cbc7);
makeBox(
scene,
[2.35, 0.035, 10.5],
[-2.45, 0.015, 0],
standardMaterial(0xd2e3df)
);
makeBox(
scene,
[2.35, 0.035, 10.5],
[2.45, 0.015, 0],
standardMaterial(0xe1dce1)
);
const grid = new THREE.GridHelper(24, 24, 0x91aaa8, 0xaec0bd);
grid.position.y = 0.035;
grid.material.transparent = true;
grid.material.opacity = 0.3;
scene.add(grid);
const movingTiles = [];
for (const laneX of [-2.45, 2.45]) {
for (let i = 0; i < 10; i += 1) {
const tile = makeBox(
scene,
[1.95, 0.038, 0.25],
[laneX, 0.055, -4.95 + i * 1.1],
i % 2 === 0 ? laneMaterialA : laneMaterialB
);
movingTiles.push({ mesh: tile, index: i });
}
}
const buildingMaterials = [
standardMaterial(0x9eb9c5),
standardMaterial(0xb4c3c9),
standardMaterial(0x8faab5),
standardMaterial(0xc3c0c7)
];
const windowMaterial = standardMaterial(0xffd98a, {
emissive: 0x8a5a18,
emissiveIntensity: 0.35
});
for (let i = 0; i < 9; i += 1) {
const width = 1.4 + (i % 3) * 0.28;
const height = 2.1 + ((i * 7) % 5) * 0.55;
const x = -10 + i * 2.5;
const z = -6.8 - (i % 2) * 0.4;
makeBox(
scene,
[width, height, 1.45],
[x, height / 2, z],
buildingMaterials[i % buildingMaterials.length]
);
makeBox(
scene,
[width + 0.12, 0.18, 1.55],
[x, height + 0.06, z],
standardMaterial(0x647d88)
);
for (let row = 0; row < Math.floor(height / 0.85); row += 1) {
for (let column = 0; column < 2; column += 1) {
makeBox(
scene,
[0.22, 0.25, 0.035],
[
x + (column - 0.5) * width * 0.36,
0.55 + row * 0.75,
z + 0.75
],
windowMaterial
);
}
}
}
const trunkMaterial = standardMaterial(0x795941);
const leafMaterial = standardMaterial(0x668d68);
for (const tree of [
[-6.8, 0, 3.7],
[-7.8, 0, -2.5],
[6.8, 0, 3.7],
[7.8, 0, -2.5]
]) {
makeBox(scene, [0.35, 1.2, 0.35], [tree[0], 0.6, tree[2]], trunkMaterial);
makeBox(scene, [1.25, 1.05, 1.15], [tree[0], 1.65, tree[2]], leafMaterial);
makeBox(scene, [0.85, 0.75, 0.85], [tree[0] + 0.15, 2.25, tree[2]], leafMaterial);
}
const floatingCubes = [];
const floatingMaterials = [
standardMaterial(0xf2c86b),
standardMaterial(0x83b8b0),
standardMaterial(0xd993a5)
];
for (let i = 0; i < 9; i += 1) {
const cube = makeBox(
scene,
[0.22 + (i % 3) * 0.05, 0.22, 0.22],
[-8 + i * 2, 3.8 + (i % 4) * 0.5, -3.5 - (i % 2)],
floatingMaterials[i % floatingMaterials.length]
);
floatingCubes.push({
mesh: cube,
baseY: cube.position.y,
phase: i * 0.8
});
}
// -------------------------------------------------------------------------
// Character rig
//
// Root
// └─ Hips
// ├─ Torso
// │ ├─ Head
// │ │ └─ HeadSocket
// │ ├─ RightArm
// │ │ └─ HandSocket
// │ ├─ LeftArm
// │ └─ TorsoSocket
// ├─ LeftLeg
// └─ RightLeg
// -------------------------------------------------------------------------
const socketPaths = {
root: "ROOT",
torso: "ROOT → HIPS → TORSO",
head: "ROOT → HIPS → TORSO → HEAD",
hand: "ROOT → HIPS → TORSO → RIGHT ARM → HAND"
};
const placements = {
hat: {
root: [0.56, 1.81, 0.02],
torso: [-0.56, 1.40, 0.02],
head: [0, 0, 0],
hand: [0, -0.02, 0.03]
},
bag: {
root: [0, -0.48, 0.03],
torso: [0, -0.12, 0.03],
head: [0.27, -0.06, 0.14],
hand: [0.27, -0.24, 0.04]
}
};
const allMarkers = [];
const pickMeshes = [];
const characters = {};
function addSocketMarker(character, socket, socketName, offset) {
const marker = new THREE.Group();
marker.position.set(offset[0], offset[1], offset[2]);
const coreMaterial = new THREE.MeshBasicMaterial({
color: 0xf3b82f,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
});
const core = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.13, 0.13),
coreMaterial
);
core.renderOrder = 40;
core.userData = {
characterId: character.id,
socketName
};
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0xf3b82f,
wireframe: true,
transparent: true,
opacity: 0.8,
depthTest: false,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.25, 0.25),
haloMaterial
);
halo.rotation.set(0.35, 0.55, 0.2);
halo.renderOrder = 39;
marker.add(core, halo);
socket.add(marker);
const record = {
characterId: character.id,
socketName,
marker,
core,
halo
};
marker.userData.record = record;
allMarkers.push(record);
pickMeshes.push(core);
}
function createCharacter(id, config) {
const root = new THREE.Group();
root.position.set(config.x, 0.03, config.z);
scene.add(root);
const rigRoot = new THREE.Group();
root.add(rigRoot);
const hips = new THREE.Group();
hips.position.y = 1.04;
rigRoot.add(hips);
const skinMaterial = standardMaterial(config.skin);
const hairMaterial = standardMaterial(config.hair);
const jacketMaterial = standardMaterial(config.jacket);
const shirtMaterial = standardMaterial(config.shirt);
const pantsMaterial = standardMaterial(config.pants);
const shoeMaterial = standardMaterial(config.shoes);
const eyeMaterial = standardMaterial(0x18202a);
const beltMaterial = standardMaterial(0x283541);
// Layered pelvis: skin base, trousers, belt.
makeBox(hips, [0.58, 0.27, 0.34], [0, 0, 0], skinMaterial);
makeBox(hips, [0.72, 0.30, 0.43], [0, 0.015, 0], pantsMaterial);
makeBox(hips, [0.75, 0.09, 0.45], [0, 0.12, 0], beltMaterial);
const torso = new THREE.Group();
torso.position.y = 0.18;
hips.add(torso);
// Layered torso: shirt beneath a slightly larger jacket shell.
makeBox(torso, [0.72, 0.69, 0.39], [0, 0.40, 0], shirtMaterial);
makeBox(torso, [0.84, 0.75, 0.47], [0, 0.40, 0], jacketMaterial);
makeBox(torso, [0.39, 0.60, 0.035], [0, 0.39, 0.25], shirtMaterial);
makeBox(torso, [0.39, 0.11, 0.045], [0, 0.72, 0.25], shirtMaterial);
// Collar and pocket details.
makeBox(torso, [0.37, 0.13, 0.36], [0, 0.82, 0], shirtMaterial);
makeBox(
torso,
[0.18, 0.16, 0.035],
[0.25, 0.26, 0.25],
standardMaterial(config.jacketDark)
);
const headPivot = new THREE.Group();
headPivot.position.set(0, 0.98, 0);
torso.add(headPivot);
makeBox(headPivot, [0.66, 0.64, 0.64], [0, 0.32, 0], skinMaterial);
makeBox(headPivot, [0.74, 0.15, 0.70], [0, 0.62, -0.015], hairMaterial);
makeBox(headPivot, [0.11, 0.07, 0.035], [-0.14, 0.36, 0.338], eyeMaterial);
makeBox(headPivot, [0.11, 0.07, 0.035], [0.14, 0.36, 0.338], eyeMaterial);
makeBox(headPivot, [0.10, 0.11, 0.08], [0, 0.22, 0.36], skinMaterial);
const rightArm = new THREE.Group();
rightArm.position.set(0.58, 0.75, 0);
torso.add(rightArm);
makeBox(rightArm, [0.30, 0.46, 0.33], [0, -0.18, 0], jacketMaterial);
makeBox(rightArm, [0.26, 0.34, 0.28], [0, -0.55, 0], skinMaterial);
makeBox(rightArm, [0.30, 0.22, 0.31], [0, -0.80, 0], skinMaterial);
const leftArm = new THREE.Group();
leftArm.position.set(-0.58, 0.75, 0);
torso.add(leftArm);
makeBox(leftArm, [0.30, 0.46, 0.33], [0, -0.18, 0], jacketMaterial);
makeBox(leftArm, [0.26, 0.34, 0.28], [0, -0.55, 0], skinMaterial);
makeBox(leftArm, [0.30, 0.22, 0.31], [0, -0.80, 0], skinMaterial);
const rightLeg = new THREE.Group();
rightLeg.position.set(0.22, -0.03, 0);
hips.add(rightLeg);
makeBox(rightLeg, [0.27, 0.65, 0.30], [0, -0.35, 0], skinMaterial);
makeBox(rightLeg, [0.33, 0.72, 0.38], [0, -0.38, 0], pantsMaterial);
makeBox(rightLeg, [0.39, 0.20, 0.53], [0, -0.83, 0.10], shoeMaterial);
const leftLeg = new THREE.Group();
leftLeg.position.set(-0.22, -0.03, 0);
hips.add(leftLeg);
makeBox(leftLeg, [0.27, 0.65, 0.30], [0, -0.35, 0], skinMaterial);
makeBox(leftLeg, [0.33, 0.72, 0.38], [0, -0.38, 0], pantsMaterial);
makeBox(leftLeg, [0.39, 0.20, 0.53], [0, -0.83, 0.10], shoeMaterial);
// Actual attachment transforms.
const rootSocket = new THREE.Group();
rootSocket.position.set(-0.56, 1.05, 0.04);
rigRoot.add(rootSocket);
const torsoSocket = new THREE.Group();
torsoSocket.position.set(0.56, 0.26, 0.04);
torso.add(torsoSocket);
const headSocket = new THREE.Group();
headSocket.position.set(0, 0.68, 0);
headPivot.add(headSocket);
const handSocket = new THREE.Group();
handSocket.position.set(0, -0.78, 0);
rightArm.add(handSocket);
const hat = new THREE.Group();
hat.name = `${id}-hat`;
makeBox(
hat,
[0.79, 0.10, 0.70],
[0, 0, 0],
standardMaterial(config.hat)
);
makeBox(
hat,
[0.57, 0.35, 0.53],
[0, 0.21, 0],
standardMaterial(config.hat)
);
makeBox(
hat,
[0.59, 0.10, 0.55],
[0, 0.08, 0],
standardMaterial(config.hatDark)
);
const bag = new THREE.Group();
bag.name = `${id}-bag`;
makeBox(
bag,
[0.57, 0.48, 0.31],
[0, -0.27, 0],
standardMaterial(config.bag)
);
makeBox(
bag,
[0.59, 0.16, 0.33],
[0, -0.11, 0],
standardMaterial(config.bagDark)
);
makeBox(
bag,
[0.07, 0.20, 0.07],
[-0.17, 0.05, 0],
standardMaterial(config.bagDark)
);
makeBox(
bag,
[0.07, 0.20, 0.07],
[0.17, 0.05, 0],
standardMaterial(config.bagDark)
);
makeBox(
bag,
[0.41, 0.07, 0.07],
[0, 0.13, 0],
standardMaterial(config.bagDark)
);
const label = makeTextSprite(
id === "reference" ? "VALID" : "WRONG",
id === "reference" ? "#079c83" : "#d74662"
);
label.position.set(0, 3.34, 0);
root.add(label);
const character = {
id,
root,
rigRoot,
hips,
torso,
headPivot,
rightArm,
leftArm,
rightLeg,
leftLeg,
sockets: {
root: rootSocket,
torso: torsoSocket,
head: headSocket,
hand: handSocket
},
accessories: { hat, bag },
current: { hat: null, bag: null },
links: {},
baseX: config.x,
baseZ: config.z,
phase: config.phase
};
addSocketMarker(character, rootSocket, "root", [0, 0.04, 0.35]);
addSocketMarker(character, torsoSocket, "torso", [0.12, 0.02, 0.34]);
addSocketMarker(character, headSocket, "head", [0.43, 0.04, 0.30]);
addSocketMarker(character, handSocket, "hand", [0.28, 0.02, 0.34]);
for (const type of ["hat", "bag"]) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([0, 0, 0, 0, 0, 0], 3)
);
const material = new THREE.LineBasicMaterial({
color: id === "reference" ? 0x079c83 : 0xd74662,
transparent: true,
opacity: id === "reference" ? 0.58 : 0.95,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
line.renderOrder = 20;
scene.add(line);
character.links[type] = line;
}
return character;
}
characters.reference = createCharacter("reference", {
x: -2.45,
z: 0,
phase: 0,
skin: 0xe7a77d,
hair: 0x4b3029,
jacket: 0x168f89,
jacketDark: 0x0c6665,
shirt: 0xe8f4db,
pants: 0x32445b,
shoes: 0x24313c,
hat: 0xf3bd43,
hatDark: 0xd78e28,
bag: 0xe29558,
bagDark: 0x8a4f37
});
characters.broken = createCharacter("broken", {
x: 2.45,
z: 0,
phase: 0.04,
skin: 0xb9785d,
hair: 0x302b37,
jacket: 0xc04f70,
jacketDark: 0x88364e,
shirt: 0xf1e2d1,
pants: 0x454052,
shoes: 0x302a35,
hat: 0xef668a,
hatDark: 0xb93e64,
bag: 0x7961cf,
bagDark: 0x493795
});
function setAccessoryParent(character, type, socketName) {
const accessory = character.accessories[type];
const socket = character.sockets[socketName];
socket.add(accessory);
accessory.position.fromArray(placements[type][socketName]);
accessory.rotation.set(0, 0, 0);
character.current[type] = socketName;
}
// Initial comparison: valid attachments versus intentionally wrong ones.
setAccessoryParent(characters.reference, "bag", "hand");
setAccessoryParent(characters.reference, "hat", "head");
setAccessoryParent(characters.broken, "bag", "torso");
setAccessoryParent(characters.broken, "hat", "torso");
// -------------------------------------------------------------------------
// UI state and hierarchy reporting
// -------------------------------------------------------------------------
const canonicalParent = {
reference: { bag: "hand", hat: "head" },
broken: { bag: "hand", hat: "head" }
};
function chainFor(characterId, type) {
const socketName = characters[characterId].current[type];
return `${socketPaths[socketName]} → ${type.toUpperCase()}`;
}
function updateMarkerStyles() {
for (const marker of allMarkers) {
const isSelected =
marker.characterId === state.target &&
marker.socketName === state.socket;
const isTarget = marker.characterId === state.target;
let color;
if (isSelected) {
color = 0xf3b82f;
} else if (isTarget) {
color = marker.characterId === "reference" ? 0x22cdb0 : 0xf08a3c;
} else {
color = marker.characterId === "reference" ? 0x43a99a : 0xa85a82;
}
marker.core.material.color.setHex(color);
marker.halo.material.color.setHex(color);
marker.core.material.opacity = isSelected || isTarget ? 1 : 0.55;
marker.halo.material.opacity = isSelected || isTarget ? 0.9 : 0.4;
}
}
function updateAccessoryScale() {
for (const character of Object.values(characters)) {
for (const type of ["bag", "hat"]) {
const selected = type === state.accessory;
character.accessories[type].scale.setScalar(selected ? 1.055 : 1);
}
}
}
function updateUI() {
const referenceBagCorrect =
characters.reference.current.bag === canonicalParent.reference.bag;
const referenceHatCorrect =
characters.reference.current.hat === canonicalParent.reference.hat;
const brokenBagCorrect =
characters.broken.current.bag === canonicalParent.broken.bag;
const brokenHatCorrect =
characters.broken.current.hat === canonicalParent.broken.hat;
const referenceBagState = document.querySelector("#referenceBagState");
const referenceHatState = document.querySelector("#referenceHatState");
const brokenBagState = document.querySelector("#brokenBagState");
const brokenHatState = document.querySelector("#brokenHatState");
referenceBagState.textContent = referenceBagCorrect
? "VALID: HAND"
: `EDITED: ${characters.reference.current.bag.toUpperCase()}`;
referenceHatState.textContent = referenceHatCorrect
? "VALID: HEAD"
: `EDITED: ${characters.reference.current.hat.toUpperCase()}`;
brokenBagState.textContent = brokenBagCorrect
? "REPAIRED: HAND"
: `WRONG: ${characters.broken.current.bag.toUpperCase()}`;
brokenHatState.textContent = brokenHatCorrect
? "REPAIRED: HEAD"
: `WRONG: ${characters.broken.current.hat.toUpperCase()}`;
referenceBagState.className = referenceBagCorrect ? "good" : "bad";
referenceHatState.className = referenceHatCorrect ? "good" : "bad";
brokenBagState.className = brokenBagCorrect ? "good" : "bad";
brokenHatState.className = brokenHatCorrect ? "good" : "bad";
document.querySelector("#referenceBagPath").textContent =
chainFor("reference", "bag");
document.querySelector("#referenceHatPath").textContent =
chainFor("reference", "hat");
document.querySelector("#brokenBagPath").textContent =
chainFor("broken", "bag");
document.querySelector("#brokenHatPath").textContent =
chainFor("broken", "hat");
document.querySelectorAll("[data-accessory]").forEach(button => {
const active = button.dataset.accessory === state.accessory;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
document.querySelectorAll("[data-target]").forEach(button => {
const active = button.dataset.target === state.target;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
document.querySelectorAll("[data-socket]").forEach(button => {
const active = button.dataset.socket === state.socket;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
updateMarkerStyles();
updateAccessoryScale();
}
function attachSelected(socketName, characterId = state.target) {
state.target = characterId;
state.socket = socketName;
setAccessoryParent(
characters[characterId],
state.accessory,
socketName
);
updateUI();
}
dom.accessoryButtons.addEventListener("click", event => {
const button = event.target.closest("[data-accessory]");
if (!button) return;
state.accessory = button.dataset.accessory;
updateUI();
});
dom.targetButtons.addEventListener("click", event => {
const button = event.target.closest("[data-target]");
if (!button) return;
state.target = button.dataset.target;
updateUI();
});
dom.socketButtons.addEventListener("click", event => {
const button = event.target.closest("[data-socket]");
if (!button) return;
attachSelected(button.dataset.socket);
});
dom.showLinks.addEventListener("change", () => {
state.showLinks = dom.showLinks.checked;
});
dom.resetRig.addEventListener("click", () => {
setAccessoryParent(characters.reference, "bag", "hand");
setAccessoryParent(characters.reference, "hat", "head");
setAccessoryParent(characters.broken, "bag", "torso");
setAccessoryParent(characters.broken, "hat", "torso");
state.accessory = "bag";
state.target = "broken";
state.socket = "hand";
updateUI();
});
dom.playPause.addEventListener("click", () => {
state.paused = !state.paused;
dom.playPause.textContent = state.paused ? "PLAY" : "PAUSE";
});
dom.speed.addEventListener("input", () => {
state.speed = Number(dom.speed.value);
dom.speedValue.textContent = `${state.speed.toFixed(2)}×`;
});
updateUI();
// -------------------------------------------------------------------------
// Custom camera controls and marker picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const normalizedPointer = new THREE.Vector2();
const pointers = new Map();
let activePointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
let previousPinchDistance = 0;
let gestureMoved = false;
let hoveredMarker = null;
function pickMarker(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
normalizedPointer.x =
((clientX - rect.left) / rect.width) * 2 - 1;
normalizedPointer.y =
-((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(normalizedPointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
return hits.length ? hits[0].object.userData : null;
}
function updateHover(clientX, clientY) {
const hit = pickMarker(clientX, clientY);
hoveredMarker = hit;
if (!hit) {
dom.tooltip.style.display = "none";
renderer.domElement.style.cursor = "grab";
return;
}
const rigName =
hit.characterId === "reference" ? "VALID RIG" : "WRONG RIG";
dom.tooltip.textContent =
`${rigName} · ${hit.socketName.toUpperCase()} SOCKET\n` +
`Click to attach the ${state.accessory.toUpperCase()}`;
dom.tooltip.style.display = "block";
dom.tooltip.style.left = `${clientX + 14}px`;
dom.tooltip.style.top = `${clientY + 14}px`;
renderer.domElement.style.cursor = "pointer";
}
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
activePointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
gestureMoved = false;
renderer.domElement.style.cursor = "grabbing";
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
gestureMoved = true;
}
dom.tooltip.style.display = "none";
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) {
if (event.pointerType === "mouse") {
updateHover(event.clientX, event.clientY);
}
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && event.pointerId === activePointerId) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.hypot(dx, dy) > 3) {
gestureMoved = true;
}
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.48,
1.35
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius *
(previousPinchDistance / distance),
7,
20
);
updateCamera();
}
previousPinchDistance = distance;
gestureMoved = true;
}
});
renderer.domElement.addEventListener("pointerup", event => {
const wasSinglePointer = pointers.size === 1;
pointers.delete(event.pointerId);
if (wasSinglePointer && !gestureMoved) {
const hit = pickMarker(event.clientX, event.clientY);
if (hit) {
attachSelected(hit.socketName, hit.characterId);
}
}
if (pointers.size === 0) {
activePointerId = null;
previousPinchDistance = 0;
renderer.domElement.style.cursor = "grab";
}
});
renderer.domElement.addEventListener("pointercancel", event => {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
activePointerId = null;
renderer.domElement.style.cursor = "grab";
}
});
renderer.domElement.addEventListener("pointerleave", () => {
if (pointers.size === 0) {
hoveredMarker = null;
dom.tooltip.style.display = "none";
}
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
20
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let simulationTime = 0;
const socketWorld = new THREE.Vector3();
const accessoryWorld = new THREE.Vector3();
function animateCharacter(character, time) {
const walk = time * 5.2 + character.phase;
const step = Math.sin(walk);
character.hips.position.y = 1.04 + Math.abs(step) * 0.045;
character.torso.rotation.z = step * 0.035;
character.torso.rotation.y = Math.sin(time * 0.72) * 0.045;
character.leftLeg.rotation.x = step * 0.58;
character.rightLeg.rotation.x = -step * 0.58;
character.leftArm.rotation.x = -step * 0.48;
character.rightArm.rotation.x = step * 0.63;
character.leftArm.rotation.z = -0.06;
character.rightArm.rotation.z = 0.06;
character.headPivot.rotation.y =
Math.sin(time * 1.08 + character.phase) * 0.31;
character.headPivot.rotation.x =
Math.sin(time * 0.84 + character.phase) * 0.085;
character.headPivot.rotation.z =
Math.sin(time * 0.63 + character.phase) * 0.055;
// A slight treadmill movement keeps the comparison readable.
character.root.position.x =
character.baseX + Math.sin(time * 0.42 + character.phase) * 0.065;
character.root.position.z =
character.baseZ + Math.sin(time * 0.68 + character.phase) * 0.055;
}
function updateHierarchyLinks() {
scene.updateMatrixWorld(true);
for (const character of Object.values(characters)) {
for (const type of ["bag", "hat"]) {
const link = character.links[type];
link.visible = state.showLinks;
character.sockets[character.current[type]].getWorldPosition(
socketWorld
);
character.accessories[type].getWorldPosition(accessoryWorld);
const positionAttribute = link.geometry.attributes.position;
positionAttribute.setXYZ(0, socketWorld.x, socketWorld.y, socketWorld.z);
positionAttribute.setXYZ(
1,
accessoryWorld.x,
accessoryWorld.y,
accessoryWorld.z
);
positionAttribute.needsUpdate = true;
}
}
}
function updateMarkerMotion(time) {
for (const marker of allMarkers) {
const selected =
marker.characterId === state.target &&
marker.socketName === state.socket;
const hovered = hoveredMarker &&
hoveredMarker.characterId === marker.characterId &&
hoveredMarker.socketName === marker.socketName;
const pulse = selected ? 1 + Math.sin(time * 4.5) * 0.10 : 1;
const scale = hovered ? 1.38 : pulse;
marker.core.scale.setScalar(scale);
marker.halo.scale.setScalar(hovered ? 1.5 : scale * 1.05);
marker.halo.rotation.y += 0.012;
marker.halo.rotation.x += 0.006;
}
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!state.paused) {
simulationTime += delta * state.speed;
}
animateCharacter(characters.reference, simulationTime);
animateCharacter(characters.broken, simulationTime);
for (const tile of movingTiles) {
const z =
((tile.index * 1.1 - simulationTime * 1.15) % 11 + 11) % 11 - 5.5;
tile.mesh.position.z = z;
}
for (const cube of floatingCubes) {
cube.mesh.position.y =
cube.baseY + Math.sin(simulationTime * 0.8 + cube.phase) * 0.12;
cube.mesh.rotation.y = simulationTime * 0.25 + cube.phase;
}
updateMarkerMotion(simulationTime);
updateHierarchyLinks();
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize and start
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.5));
renderer.setSize(window.innerWidth, window.innerHeight);
});
renderer.domElement.style.cursor = "grab";
animate();
dom.loading.style.display = "none";
</script>
</body>
</html>
```