Prompt specification
Build or customize the Three.js 3D scene "Tripod Gait for an Invented Creature" 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: Tripod Gait for an Invented Creature
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tripod-gait-for-an-invented-creature-x3722
Scene Description Prompt:
Tripod gait for an invented creature. Animate an original six-legged creature using alternating support tripods. Keep planted feet stable and show the support pattern without asserting zoological realism for the fictional animal. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (tripod-gait-for-an-invented-creature-x3722):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TRIPOD // Invented Six-Legged Creature</title>
<style>
:root {
color-scheme: dark;
--ink: #dff8f2;
--muted: #8ba8ae;
--panel: rgba(7, 20, 27, 0.78);
--line: rgba(142, 213, 215, 0.2);
--a: #55e6c1;
--b: #ff70ae;
--swing: #ffb454;
--plant: #6ef0b5;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07131b;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
#intro {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 4px;
}
.eyebrow {
color: var(--a);
font-size: 10px;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 5px;
font-size: clamp(24px, 4vw, 38px);
line-height: 0.95;
letter-spacing: -0.06em;
font-weight: 800;
}
.subtitle {
color: var(--muted);
font-size: 11px;
line-height: 1.6;
}
.rule {
height: 1px;
margin: 14px 0 12px;
background: linear-gradient(90deg, var(--a), transparent);
}
.facts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.fact {
border-left: 2px solid rgba(85, 230, 193, 0.45);
padding-left: 9px;
}
.fact b {
display: block;
margin-bottom: 3px;
font-size: 10px;
letter-spacing: 0.08em;
}
.fact span {
color: var(--muted);
font-size: 10px;
}
#support {
left: 22px;
bottom: 22px;
width: 278px;
padding: 15px 16px;
border-radius: 4px;
}
.support-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.support-head strong {
font-size: 11px;
letter-spacing: 0.12em;
}
#support-state {
color: var(--a);
font-size: 10px;
}
.support-body {
display: grid;
grid-template-columns: 116px 1fr;
align-items: center;
gap: 8px;
}
#gait-map {
width: 116px;
height: 116px;
overflow: visible;
}
.legend {
display: grid;
gap: 8px;
font-size: 9px;
color: var(--muted);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--plant);
box-shadow: 0 0 9px rgba(110, 240, 181, 0.55);
}
.dot.swing {
background: var(--swing);
box-shadow: 0 0 9px rgba(255, 180, 84, 0.55);
}
.note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: #78949b;
font-size: 9px;
line-height: 1.5;
}
#controls {
top: 22px;
right: 22px;
padding: 12px;
border-radius: 4px;
display: flex;
gap: 7px;
}
button {
border: 1px solid rgba(142, 213, 215, 0.28);
border-radius: 3px;
padding: 9px 11px;
color: var(--ink);
background: rgba(20, 47, 57, 0.78);
font: inherit;
font-size: 10px;
letter-spacing: 0.07em;
cursor: pointer;
}
button:hover {
border-color: var(--a);
color: var(--a);
}
#hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
color: rgba(202, 233, 231, 0.66);
font-size: 9px;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
text-shadow: 0 2px 8px #000;
}
@media (max-width: 760px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.facts, .rule { display: none; }
#controls {
top: 150px;
right: 12px;
flex-direction: column;
}
#support {
left: 12px;
bottom: 12px;
width: 238px;
padding: 11px 12px;
}
.support-body {
grid-template-columns: 94px 1fr;
}
#gait-map {
width: 94px;
height: 94px;
}
#hint { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated invented six-legged creature"></canvas>
<section id="intro" class="panel">
<div class="eyebrow">Procedural locomotion study / fictional morphology</div>
<h1>TRIPOD<br>HEX-03</h1>
<div class="subtitle">
An invented low-poly walker using alternating three-foot support.
Stance contacts remain fixed in world space while the body advances.
</div>
<div class="rule"></div>
<div class="facts">
<div class="fact">
<b>SUPPORT A</b>
<span>LF · RM · RR</span>
</div>
<div class="fact">
<b>SUPPORT B</b>
<span>RF · LM · LR</span>
</div>
<div class="fact">
<b>SWING</b>
<span>1.09 s / cycle</span>
</div>
<div class="fact">
<b>STANCE</b>
<span>1.51 s / cycle</span>
</div>
</div>
</section>
<section id="support" class="panel" aria-live="polite">
<div class="support-head">
<strong>CONTACT MAP</strong>
<span id="support-state">SUPPORT A</span>
</div>
<div class="support-body">
<svg id="gait-map" viewBox="0 0 120 120" aria-label="Top view of the six-foot support pattern">
<path
d="M53 26 Q60 19 67 26 L69 88 Q60 98 51 88 Z"
fill="#173d45"
stroke="#3f747a"
stroke-width="2"
/>
<path d="M60 24 L60 94" stroke="#6a9da0" stroke-width="1" opacity=".45"/>
<polygon id="poly-a" points="42,28 85,58 78,88"
fill="rgba(85,230,193,.12)" stroke="#55e6c1" stroke-width="2"/>
<polygon id="poly-b" points="78,28 35,58 42,88"
fill="rgba(255,112,174,.10)" stroke="#ff70ae" stroke-width="2"/>
<g stroke-width="2">
<circle id="dot-LF" cx="42" cy="28" r="6" fill="#6ef0b5" stroke="#55e6c1"/>
<circle id="dot-RF" cx="78" cy="28" r="6" fill="#ffb454" stroke="#ff70ae"/>
<circle id="dot-LM" cx="35" cy="58" r="6" fill="#ffb454" stroke="#ff70ae"/>
<circle id="dot-RM" cx="85" cy="58" r="6" fill="#6ef0b5" stroke="#55e6c1"/>
<circle id="dot-LR" cx="42" cy="88" r="6" fill="#ffb454" stroke="#ff70ae"/>
<circle id="dot-RR" cx="78" cy="88" r="6" fill="#6ef0b5" stroke="#55e6c1"/>
</g>
<text x="60" y="61" text-anchor="middle" fill="#bce6e1" font-size="8">FRONT</text>
</svg>
<div class="legend">
<div class="legend-item"><i class="dot"></i> planted / fixed</div>
<div class="legend-item"><i class="dot swing"></i> swing / lifted</div>
<div class="legend-item"><i class="dot" style="background:#55e6c1"></i> tripod A</div>
<div class="legend-item"><i class="dot" style="background:#ff70ae"></i> tripod B</div>
</div>
</div>
<div class="note">
Fictional gait visualization only. The geometry is not presented as a model
of zoological locomotion.
</div>
</section>
<section id="controls" class="panel">
<button id="pause" type="button">PAUSE</button>
<button id="speed" type="button">1.0×</button>
<button id="reset" type="button">RESET VIEW</button>
</section>
<div id="hint">DRAG TO ORBIT · WHEEL TO ZOOM · SPACE TO PAUSE · R TO RESET</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Constants and state
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const CYCLE = 2.6;
const SWING_FRACTION = 0.42;
const STRIDE = 1.2;
const LEAD = 0.25;
const SWING_HEIGHT = 0.34;
const GROUP_A_OFFSET = 0.45;
const GROUP_B_OFFSET = 0.0;
const COLORS = {
a: 0x55e6c1,
b: 0xff70ae,
swing: 0xffb454,
planted: 0x6ef0b5
};
let simulationTime = 0;
let paused = false;
let playbackRate = 1;
let activeGroup = "A";
// ---------------------------------------------------------------------
// Renderer, scene, camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
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.18;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131b);
scene.fog = new THREE.Fog(0x07131b, 13, 43);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
160
);
const cameraState = {
target: new THREE.Vector3(0, 1.1, 0),
theta: 0.82,
phi: 1.08,
radius: 7.1
};
const cameraDesired = new THREE.Vector3();
function resetView() {
cameraState.theta = 0.82;
cameraState.phi = 1.08;
cameraState.radius = 7.1;
}
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ddce2, 0x17303a, 1.7));
const key = new THREE.DirectionalLight(0xffefd5, 3.2);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -9;
key.shadow.camera.right = 9;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -9;
key.shadow.camera.near = 1;
key.shadow.camera.far = 35;
key.shadow.bias = -0.00025;
scene.add(key);
scene.add(key.target);
const rim = new THREE.DirectionalLight(0x4ed5d2, 1.45);
scene.add(rim);
// ---------------------------------------------------------------------
// Ground, grid, and low-poly scenery
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(260, 260),
new THREE.MeshStandardMaterial({
color: 0x0b2027,
roughness: 0.96,
metalness: 0.04
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.set(0, -0.015, 90);
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(220, 110, 0x245c64, 0x153740);
grid.position.y = 0.004;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
let seed = 9137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
const rockGeometry = new THREE.DodecahedronGeometry(1, 0);
const rockMaterials = [
new THREE.MeshStandardMaterial({ color: 0x17353b, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x21434a, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x102a31, roughness: 1, flatShading: true })
];
for (let i = 0; i < 58; i++) {
const side = i % 2 === 0 ? -1 : 1;
const rock = new THREE.Mesh(
rockGeometry,
rockMaterials[i % rockMaterials.length]
);
rock.position.set(
side * (4.3 + random() * 7),
0.04,
-25 + i * 4.2 + random() * 2.2
);
const s = 0.12 + random() * 0.36;
rock.scale.set(s * (0.8 + random()), s * 0.55, s);
rock.rotation.set(random() * 2, random() * 2, random() * 2);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0x28515a,
emissive: 0x0b3034,
emissiveIntensity: 0.35,
roughness: 0.8,
flatShading: true
});
for (let i = 0; i < 32; i++) {
for (const side of [-1, 1]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.055, 0.42, 5),
markerMaterial
);
post.position.set(side * 3.45, 0.21, -15 + i * 5);
post.castShadow = true;
scene.add(post);
const cap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
new THREE.MeshStandardMaterial({
color: side < 0 ? 0x55e6c1 : 0xff70ae,
emissive: side < 0 ? 0x164f43 : 0x4f1d34,
emissiveIntensity: 0.6,
flatShading: true
})
);
cap.position.set(side * 3.45, 0.45, -15 + i * 5);
scene.add(cap);
}
}
// ---------------------------------------------------------------------
// Materials and body
// ---------------------------------------------------------------------
const bodyRoot = new THREE.Group();
scene.add(bodyRoot);
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0x246d70,
roughness: 0.72,
metalness: 0.08,
flatShading: true
});
const darkShellMaterial = new THREE.MeshStandardMaterial({
color: 0x17474e,
roughness: 0.82,
metalness: 0.06,
flatShading: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: 0xf28b61,
emissive: 0x501c11,
emissiveIntensity: 0.2,
roughness: 0.6,
flatShading: true
});
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0xffe6a0,
emissive: 0xff9e3d,
emissiveIntensity: 1.6,
roughness: 0.35,
flatShading: true
});
const shellGeometry = new THREE.IcosahedronGeometry(1, 1);
function bodyMesh(geometry, material, position, scale) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.scale.copy(scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
bodyRoot.add(mesh);
return mesh;
}
bodyMesh(
shellGeometry,
darkShellMaterial,
new THREE.Vector3(0, 1.25, -0.72),
new THREE.Vector3(0.78, 0.61, 1.03)
);
bodyMesh(
shellGeometry,
shellMaterial,
new THREE.Vector3(0, 1.31, 0.10),
new THREE.Vector3(0.88, 0.67, 0.83)
);
bodyMesh(
shellGeometry,
shellMaterial,
new THREE.Vector3(0, 1.29, 0.91),
new THREE.Vector3(0.64, 0.54, 0.61)
);
// Dorsal plates give the invented silhouette a mechanical/insect-like read.
for (let i = 0; i < 3; i++) {
const plate = new THREE.Mesh(
new THREE.ConeGeometry(0.22 - i * 0.025, 0.38 - i * 0.035, 5),
i === 1 ? accentMaterial : darkShellMaterial
);
plate.position.set(0, 1.91 - i * 0.025, 0.45 - i * 0.48);
plate.rotation.y = Math.PI / 4;
plate.castShadow = true;
bodyRoot.add(plate);
}
// Low-poly eyes.
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.14, 1),
eyeMaterial
);
eye.position.set(side * 0.31, 1.48, 1.31);
eye.scale.set(1, 0.82, 0.72);
eye.castShadow = true;
bodyRoot.add(eye);
const socket = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.18, 1),
darkShellMaterial
);
socket.position.set(side * 0.34, 1.46, 1.27);
socket.scale.set(1, 0.9, 0.5);
bodyRoot.add(socket);
}
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const X_AXIS = new THREE.Vector3(1, 0, 0);
function alignCylinder(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
mesh.scale.set(1, length, 1);
}
function localSegment(parent, a, b, radius, material) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.7, radius, 1, 5),
material
);
alignCylinder(mesh, a, b);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
// Antenna-like sensory projections.
for (const side of [-1, 1]) {
const a = new THREE.Vector3(side * 0.22, 1.66, 1.16);
const b = new THREE.Vector3(side * 0.40, 2.04, 1.66);
localSegment(bodyRoot, a, b, 0.035, accentMaterial);
const tip = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
eyeMaterial
);
tip.position.copy(b);
bodyRoot.add(tip);
}
// Short paired forward processes.
for (const side of [-1, 1]) {
const a = new THREE.Vector3(side * 0.20, 1.18, 1.27);
const b = new THREE.Vector3(side * 0.31, 1.03, 1.67);
localSegment(bodyRoot, a, b, 0.08, accentMaterial);
}
// ---------------------------------------------------------------------
// Six legs
//
// Group A: LF, RM, RR
// Group B: RF, LM, LR
//
// Foot Z is computed from an absolute cycle formula. During stance the
// result is constant, so the foot does not slide with the moving body.
// ---------------------------------------------------------------------
const legDefinitions = [
{ name: "LF", side: -1, neutralZ: 0.72, group: "A" },
{ name: "LM", side: -1, neutralZ: 0.00, group: "B" },
{ name: "LR", side: -1, neutralZ: -0.72, group: "B" },
{ name: "RF", side: 1, neutralZ: 0.72, group: "B" },
{ name: "RM", side: 1, neutralZ: 0.00, group: "A" },
{ name: "RR", side: 1, neutralZ: -0.72, group: "A" }
];
const upperMaterial = new THREE.MeshStandardMaterial({
color: 0x2b7d7c,
roughness: 0.72,
flatShading: true
});
const lowerMaterial = new THREE.MeshStandardMaterial({
color: 0x143f49,
roughness: 0.78,
flatShading: true
});
const jointMaterial = new THREE.MeshStandardMaterial({
color: 0xf09b65,
roughness: 0.55,
flatShading: true
});
const footMaterial = new THREE.MeshStandardMaterial({
color: 0x17343b,
roughness: 0.86,
flatShading: true
});
const upperGeometry = new THREE.CylinderGeometry(0.12, 0.16, 1, 6);
const lowerGeometry = new THREE.CylinderGeometry(0.085, 0.12, 1, 6);
const legs = legDefinitions.map((definition) => {
const upper = new THREE.Mesh(upperGeometry, upperMaterial);
const lower = new THREE.Mesh(lowerGeometry, lowerMaterial);
upper.castShadow = true;
lower.castShadow = true;
scene.add(upper, lower);
const knee = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.13, 0),
jointMaterial
);
knee.castShadow = true;
scene.add(knee);
const footGroup = new THREE.Group();
const pad = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.13, 0.32),
footMaterial
);
pad.castShadow = true;
footGroup.add(pad);
const toe = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.31, 4),
footMaterial
);
toe.rotation.x = Math.PI / 2;
toe.position.z = 0.29;
toe.castShadow = true;
footGroup.add(toe);
scene.add(footGroup);
const ringMaterial = new THREE.MeshBasicMaterial({
color: COLORS.planted,
transparent: true,
opacity: 0.9,
depthWrite: false,
side: THREE.DoubleSide
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.17, 0.235, 12),
ringMaterial
);
ring.rotation.x = -Math.PI / 2;
scene.add(ring);
const offset = definition.group === "A"
? GROUP_A_OFFSET
: GROUP_B_OFFSET;
// This adjustment makes both tripods land with the same forward lead.
const extra = LEAD - offset * STRIDE;
return {
...definition,
offset,
extra,
upper,
lower,
knee,
footGroup,
ring,
footX: definition.side * 1.38,
state: {
x: definition.side * 1.38,
y: 0.09,
z: 0,
swing: false,
ratio: 0
}
};
});
function smoothStep(t) {
return t * t * (3 - 2 * t);
}
function updateLegState(leg, time) {
const absoluteCycle = time / CYCLE + leg.offset;
const cycleIndex = Math.floor(absoluteCycle);
const phase = absoluteCycle - cycleIndex;
// Position of the landing contact for this absolute cycle.
const landingZ =
leg.neutralZ +
leg.extra +
SWING_FRACTION * STRIDE +
cycleIndex * STRIDE;
const state = leg.state;
if (phase < SWING_FRACTION) {
const ratio = phase / SWING_FRACTION;
const eased = smoothStep(ratio);
state.swing = true;
state.ratio = ratio;
state.x = leg.footX;
state.z = THREE.MathUtils.lerp(
landingZ - STRIDE,
landingZ,
eased
);
state.y = 0.09 + Math.sin(Math.PI * ratio) * SWING_HEIGHT;
} else {
// Exact world-space stance contact: no body-relative drift.
state.swing = false;
state.ratio = 0;
state.x = leg.footX;
state.z = landingZ;
state.y = 0.09;
}
return state;
}
const hipLocal = new THREE.Vector3();
const hipWorld = new THREE.Vector3();
const footWorld = new THREE.Vector3();
const delta = new THREE.Vector3();
const axis = new THREE.Vector3();
const pole = new THREE.Vector3();
const bend = new THREE.Vector3();
const kneeWorld = new THREE.Vector3();
const UPPER_LENGTH = 0.78;
const LOWER_LENGTH = 0.88;
function updateLegIK(leg) {
hipLocal.set(leg.side * 0.56, 1.06, leg.neutralZ);
hipWorld.copy(hipLocal);
bodyRoot.localToWorld(hipWorld);
footWorld.set(leg.state.x, leg.state.y, leg.state.z);
delta.subVectors(footWorld, hipWorld);
const rawDistance = Math.max(delta.length(), 0.0001);
const distance = THREE.MathUtils.clamp(
rawDistance,
Math.abs(UPPER_LENGTH - LOWER_LENGTH) + 0.001,
UPPER_LENGTH + LOWER_LENGTH - 0.001
);
axis.copy(delta).normalize();
const along =
(
UPPER_LENGTH * UPPER_LENGTH -
LOWER_LENGTH * LOWER_LENGTH +
distance * distance
) / (2 * distance);
const height = Math.sqrt(
Math.max(0, UPPER_LENGTH * UPPER_LENGTH - along * along)
);
// Prefer an outward and longitudinal knee bend.
pole.set(
leg.side * 0.42,
0.04,
Math.sign(leg.neutralZ || 1) * 0.72
);
bend.copy(pole)
.addScaledVector(axis, -pole.dot(axis));
if (bend.lengthSq() < 0.0001) {
bend.set(leg.side, 0, 0);
bend.addScaledVector(axis, -bend.dot(axis));
}
bend.normalize();
kneeWorld.copy(hipWorld)
.addScaledVector(axis, along)
.addScaledVector(bend, height);
alignCylinder(leg.upper, hipWorld, kneeWorld);
alignCylinder(leg.lower, kneeWorld, footWorld);
leg.knee.position.copy(kneeWorld);
leg.footGroup.position.copy(footWorld);
leg.footGroup.rotation.x = leg.state.swing
? Math.sin(Math.PI * leg.state.ratio) * 0.18
: 0;
leg.ring.position.set(leg.state.x, 0.025, leg.state.z);
leg.ring.material.color.setHex(
leg.state.swing ? COLORS.swing : COLORS.planted
);
leg.ring.material.opacity = leg.state.swing ? 0.42 : 0.92;
}
// ---------------------------------------------------------------------
// Ground support triangles and labels
// ---------------------------------------------------------------------
function makeLabelSprite(letter, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.beginPath();
context.arc(64, 64, 43, 0, Math.PI * 2);
context.fillStyle = "rgba(5, 17, 23, 0.86)";
context.fill();
context.lineWidth = 7;
context.strokeStyle = color;
context.stroke();
context.fillStyle = color;
context.font = "bold 62px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(letter, 64, 68);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.38, 0.38, 0.38);
sprite.renderOrder = 10;
return sprite;
}
function makeSupportTriangle(color, letter) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const segments = [];
for (let i = 0; i < 3; i++) {
const segment = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material
);
segment.renderOrder = 2;
group.add(segment);
segments.push(segment);
}
const label = makeLabelSprite(
letter,
letter === "A" ? "#55e6c1" : "#ff70ae"
);
group.add(label);
scene.add(group);
return { group, segments, label };
}
const supportA = makeSupportTriangle(COLORS.a, "A");
const supportB = makeSupportTriangle(COLORS.b, "B");
function setSegment(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(X_AXIS, direction.normalize());
mesh.scale.set(length, 0.022, 0.035);
}
function updateSupportTriangle(triangle, memberLegs) {
const points = memberLegs.map(leg =>
new THREE.Vector3(
leg.state.x,
0.038,
leg.state.z
)
);
for (let i = 0; i < 3; i++) {
setSegment(
triangle.segments[i],
points[i],
points[(i + 1) % 3]
);
}
triangle.label.position.copy(points[0])
.add(points[1])
.add(points[2])
.multiplyScalar(1 / 3);
triangle.label.position.y = 0.48;
}
const byName = Object.fromEntries(legs.map(leg => [leg.name, leg]));
const groupALegs = [byName.LF, byName.RM, byName.RR];
const groupBLegs = [byName.RF, byName.LM, byName.LR];
// ---------------------------------------------------------------------
// UI support map
// ---------------------------------------------------------------------
const supportState = document.querySelector("#support-state");
const polyA = document.querySelector("#poly-a");
const polyB = document.querySelector("#poly-b");
function updateSupportMap() {
for (const leg of legs) {
const dot = document.querySelector(`#dot-${leg.name}`);
dot.setAttribute(
"fill",
leg.state.swing ? "#ffb454" : "#6ef0b5"
);
dot.setAttribute("r", leg.state.swing ? "5" : "6");
}
polyA.setAttribute(
"opacity",
activeGroup === "A" ? "1" : "0.17"
);
polyB.setAttribute(
"opacity",
activeGroup === "B" ? "1" : "0.17"
);
supportState.textContent = `SUPPORT ${activeGroup}`;
supportState.style.color =
activeGroup === "A" ? "#55e6c1" : "#ff70ae";
}
// ---------------------------------------------------------------------
// Direct pointer camera controls
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.32,
1.43
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
3.5,
14
);
}, { passive: false });
// ---------------------------------------------------------------------
// UI controls
// ---------------------------------------------------------------------
const pauseButton = document.querySelector("#pause");
const speedButton = document.querySelector("#speed");
const resetButton = document.querySelector("#reset");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "RESUME" : "PAUSE";
});
speedButton.addEventListener("click", () => {
const rates = [0.65, 1, 1.35];
const index = (rates.indexOf(playbackRate) + 1) % rates.length;
playbackRate = rates[index];
speedButton.textContent = `${playbackRate.toFixed(2).replace(/0$/, "")}×`;
});
resetButton.addEventListener("click", resetView);
addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetView();
}
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function isSwinging(group, time) {
const offset = group === "A" ? GROUP_A_OFFSET : GROUP_B_OFFSET;
const phase = (time / CYCLE + offset) % 1;
return phase < SWING_FRACTION;
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta * playbackRate;
}
const bodyZ = (simulationTime / CYCLE) * STRIDE;
// Small body motion; feet are independently solved and remain fixed
// during stance.
const gaitWave = simulationTime / CYCLE * Math.PI * 2;
const bob = Math.sin(gaitWave * 2) * 0.035;
const pitch = Math.sin(gaitWave) * 0.012;
const roll = Math.sin(gaitWave * 2 + 0.6) * 0.022;
bodyRoot.position.set(0, bob, bodyZ);
bodyRoot.rotation.set(pitch, 0, roll);
bodyRoot.updateMatrixWorld(true);
for (const leg of legs) {
updateLegState(leg, simulationTime);
updateLegIK(leg);
}
const aSwinging = isSwinging("A", simulationTime);
const bSwinging = isSwinging("B", simulationTime);
if (bSwinging) {
activeGroup = "A";
} else if (aSwinging) {
activeGroup = "B";
}
updateSupportTriangle(supportA, groupALegs);
updateSupportTriangle(supportB, groupBLegs);
supportA.group.visible = activeGroup === "A";
supportB.group.visible = activeGroup === "B";
updateSupportMap();
// Track the moving creature while retaining user orbit and zoom.
cameraState.target.set(0, 1.18, bodyZ + 0.12);
const sinPhi = Math.sin(cameraState.phi);
cameraDesired.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.position.lerp(cameraDesired, 1 - Math.exp(-delta * 8));
camera.lookAt(cameraState.target);
key.position.set(bodyZ + 5.5, 10.5, bodyZ + 4.0);
key.target.position.set(0, 0.8, bodyZ);
key.target.updateMatrixWorld();
rim.position.set(-5, 5, bodyZ + 6);
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
animate();
</script>
</body>
</html>
```