Prompt specification
Build or customize the Three.js 3D scene "Two Feet and Alternating Contact Markers" 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: Two Feet and Alternating Contact Markers
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-feet-and-alternating-contact-markers-x3757
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing walking cycles, foot contact, and stride length. Model a character's lower legs above a short path with alternating footprint markers. Show stance and swing phases separately without sliding a planted foot. Stylized solid forms.
Working Three.js Starter (two-feet-and-alternating-contact-markers-x3757):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Gait Lab — Walking Cycle</title>
<style>
:root {
--ink: #172235;
--muted: #677489;
--panel: rgba(255, 255, 255, 0.88);
--line: rgba(23, 34, 53, 0.12);
--stance: #2f68d8;
--swing: #ef7b45;
--green: #28a779;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8edf2;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(
180deg,
rgba(255, 255, 255, 0.12),
transparent 28%,
transparent 72%,
rgba(28, 38, 51, 0.05)
);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.topbar {
position: absolute;
top: 24px;
left: 26px;
right: 26px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
border-radius: 14px;
background: var(--ink);
box-shadow: 0 8px 24px rgba(23, 34, 53, 0.18);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
left: 11px;
width: 20px;
height: 5px;
border-radius: 4px;
transform-origin: left center;
}
.brand-mark::before {
top: 12px;
background: var(--stance);
transform: rotate(34deg);
}
.brand-mark::after {
top: 27px;
background: var(--swing);
transform: rotate(-34deg);
}
h1 {
margin: 0;
font-size: 19px;
line-height: 1.1;
letter-spacing: -0.03em;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.toolbar {
display: flex;
gap: 8px;
pointer-events: auto;
}
.tool-button {
min-width: 42px;
height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.84);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
box-shadow: 0 7px 22px rgba(23, 34, 53, 0.08);
backdrop-filter: blur(12px);
}
.tool-button:hover {
background: white;
transform: translateY(-1px);
}
.tool-button.primary {
min-width: 84px;
color: white;
background: var(--ink);
border-color: var(--ink);
}
.panel {
position: absolute;
top: 92px;
right: 26px;
width: 316px;
padding: 19px;
border: 1px solid rgba(255, 255, 255, 0.74);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 20px 60px rgba(23, 34, 53, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.82);
backdrop-filter: blur(18px);
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.contact-badge {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--green);
font-size: 10px;
letter-spacing: 0.04em;
}
.contact-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(40, 167, 121, 0.12);
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 20px;
}
.metric {
padding: 11px 10px 10px;
border-radius: 13px;
background: rgba(233, 238, 244, 0.72);
}
.metric strong {
display: block;
font-size: 17px;
letter-spacing: -0.035em;
}
.metric span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.phase-header {
display: flex;
justify-content: space-between;
margin-bottom: 11px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.phase-row {
display: grid;
grid-template-columns: 22px 1fr 52px;
align-items: center;
gap: 9px;
margin: 10px 0;
}
.leg-letter {
font-size: 12px;
font-weight: 850;
}
.phase-track {
position: relative;
display: flex;
height: 12px;
overflow: hidden;
border-radius: 20px;
background: #e5eaf0;
}
.phase-track .stance {
width: 62%;
background: var(--stance);
}
.phase-track .swing {
flex: 1;
background: var(--swing);
}
.playhead {
position: absolute;
top: -3px;
left: 0;
width: 3px;
height: 18px;
border-radius: 3px;
background: white;
box-shadow: 0 1px 5px rgba(23, 34, 53, 0.42);
transform: translateX(-50%);
}
.phase-state {
text-align: right;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.04em;
}
.phase-state.stance {
color: var(--stance);
}
.phase-state.swing {
color: var(--swing);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 19px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.legend-item {
display: flex;
align-items: flex-start;
gap: 8px;
}
.legend-dot {
flex: 0 0 auto;
width: 9px;
height: 9px;
margin-top: 3px;
border-radius: 50%;
}
.legend-item strong {
display: block;
font-size: 11px;
}
.legend-item small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.speed-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.speed-row > span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.speed-buttons {
display: flex;
gap: 4px;
pointer-events: auto;
}
.speed-button {
padding: 6px 9px;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--muted);
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.speed-button.active {
color: white;
background: var(--ink);
}
.hint {
position: absolute;
left: 26px;
bottom: 24px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 12px;
background: rgba(255, 255, 255, 0.72);
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
backdrop-filter: blur(12px);
}
.world-label {
position: fixed;
top: 0;
left: 0;
z-index: 5;
pointer-events: none;
visibility: hidden;
white-space: nowrap;
transform: translate(-50%, -50%);
border-radius: 9px;
padding: 6px 9px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.07em;
box-shadow: 0 8px 22px rgba(23, 34, 53, 0.12);
}
.world-label.locked {
color: white;
background: var(--stance);
}
.world-label.swing {
color: white;
background: var(--swing);
}
.world-label.stride {
color: var(--ink);
background: rgba(255, 255, 255, 0.9);
border: 1px solid rgba(23, 34, 53, 0.1);
}
@media (max-width: 720px) {
.topbar {
top: 16px;
left: 16px;
right: 16px;
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 14px;
border-radius: 18px;
}
.panel-title,
.metrics,
.legend,
.speed-row {
display: none;
}
.phase-header {
margin-bottom: 7px;
}
.phase-row {
margin: 7px 0;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="hud">
<div class="topbar">
<div class="brand">
<div class="brand-mark"></div>
<div>
<h1>Gait Lab</h1>
<div class="subtitle">Lower-body locomotion study</div>
</div>
</div>
<div class="toolbar">
<button class="tool-button" id="resetView" title="Reset camera">
View
</button>
<button class="tool-button primary" id="playPause">
Pause
</button>
</div>
</div>
<section class="panel">
<div class="panel-title">
<span>Cycle analysis</span>
<span class="contact-badge" id="contactBadge">1 foot locked</span>
</div>
<div class="metrics">
<div class="metric">
<strong>1.15 m</strong>
<span>Same-foot stride</span>
</div>
<div class="metric">
<strong>1.75 s</strong>
<span>Cycle</span>
</div>
<div class="metric">
<strong>62%</strong>
<span>Stance duty</span>
</div>
</div>
<div class="phase-header">
<span>Phase timeline</span>
<span>62 / 38 split</span>
</div>
<div class="phase-row">
<span class="leg-letter">L</span>
<div class="phase-track">
<div class="stance"></div>
<div class="swing"></div>
<i class="playhead" id="playL"></i>
</div>
<span class="phase-state stance" id="phaseL">STANCE</span>
</div>
<div class="phase-row">
<span class="leg-letter">R</span>
<div class="phase-track">
<div class="stance"></div>
<div class="swing"></div>
<i class="playhead" id="playR"></i>
</div>
<span class="phase-state stance" id="phaseR">STANCE</span>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-dot" style="background: var(--stance)"></span>
<div>
<strong>Stance</strong>
<small>Foot remains fixed in world space.</small>
</div>
</div>
<div class="legend-item">
<span class="legend-dot" style="background: var(--swing)"></span>
<div>
<strong>Swing</strong>
<small>Foot clears the path along an arc.</small>
</div>
</div>
</div>
<div class="speed-row">
<span>Playback</span>
<div class="speed-buttons">
<button class="speed-button" data-speed="0.5">0.5×</button>
<button class="speed-button active" data-speed="1">1×</button>
<button class="speed-button" data-speed="1.5">1.5×</button>
</div>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
</div>
<div class="world-label locked" id="labelL">L · LOCKED</div>
<div class="world-label locked" id="labelR">R · LOCKED</div>
<div class="world-label swing" id="swingL">L · SWING</div>
<div class="world-label swing" id="swingR">R · SWING</div>
<div class="world-label stride" id="strideLabel">SAME-FOOT STRIDE · 1.15 m</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Gait configuration
// ------------------------------------------------------------
const CYCLE_SECONDS = 1.75;
const STRIDE_LENGTH = 1.15;
const DUTY_FACTOR = 0.62;
const WALK_SPEED = STRIDE_LENGTH / CYCLE_SECONDS;
const ANKLE_HEIGHT = 0.20;
const SWING_LIFT = 0.34;
const TOE_OFF_LIFT = 0.07;
const THIGH_LENGTH = 0.74;
const CALF_LENGTH = 0.72;
const STANCE_COLOR = new THREE.Color(0x2f68d8);
const SWING_COLOR = new THREE.Color(0xef7b45);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8edf2);
scene.fog = new THREE.Fog(0xe8edf2, 12, 31);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById("scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xf7fbff,
0x87909d,
2.35
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff8ed, 3.1);
keyLight.position.set(4, 8, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
scene.add(keyLight.target);
const fillLight = new THREE.DirectionalLight(0xd9e8ff, 1.1);
fillLight.position.set(-5, 4, -6);
scene.add(fillLight);
// ------------------------------------------------------------
// Environment and path
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 60),
new THREE.MeshStandardMaterial({
color: 0xdce3e9,
roughness: 0.95,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.14;
floor.receiveShadow = true;
scene.add(floor);
const path = new THREE.Mesh(
new THREE.BoxGeometry(58, 0.12, 3.2),
new THREE.MeshStandardMaterial({
color: 0xf7f4ec,
roughness: 0.88,
metalness: 0
})
);
path.position.set(21, -0.06, 0);
path.receiveShadow = true;
scene.add(path);
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0xaeb8c3,
roughness: 0.8
});
for (const z of [-1.63, 1.63]) {
const edge = new THREE.Mesh(
new THREE.BoxGeometry(58, 0.055, 0.075),
edgeMaterial
);
edge.position.set(21, 0.015, z);
edge.receiveShadow = true;
scene.add(edge);
}
// Fine ruler ticks along one side of the path.
const tickPositions = [];
for (let x = -2; x < 45; x += STRIDE_LENGTH / 2) {
tickPositions.push(x, 0.008, 1.27, x, 0.008, 1.42);
}
const tickGeometry = new THREE.BufferGeometry();
tickGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(tickPositions, 3)
);
const ticks = new THREE.LineSegments(
tickGeometry,
new THREE.LineBasicMaterial({
color: 0xb2bbc4,
transparent: true,
opacity: 0.65
})
);
scene.add(ticks);
// ------------------------------------------------------------
// Footprint shape and alternating markers
// ------------------------------------------------------------
function createFootprintShape() {
const shape = new THREE.Shape();
shape.moveTo(-0.34, -0.10);
shape.quadraticCurveTo(-0.41, 0.00, -0.29, 0.11);
shape.quadraticCurveTo(-0.15, 0.16, 0.01, 0.12);
shape.lineTo(0.24, 0.15);
shape.quadraticCurveTo(0.47, 0.17, 0.52, 0.04);
shape.quadraticCurveTo(0.56, -0.10, 0.38, -0.15);
shape.lineTo(-0.18, -0.14);
shape.quadraticCurveTo(-0.29, -0.14, -0.34, -0.10);
shape.closePath();
return shape;
}
const footprintGeometry = new THREE.ShapeGeometry(
createFootprintShape(),
12
);
const markerBlue = 0x2f68d8;
const markerOrange = 0xef7b45;
const markers = [];
function addFootprintMarker(touchTime, side) {
const material = new THREE.MeshBasicMaterial({
color: side < 0 ? markerBlue : markerOrange,
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
const group = new THREE.Group();
const mesh = new THREE.Mesh(footprintGeometry, material);
mesh.rotation.x = -Math.PI / 2;
mesh.rotation.z = side * 0.035;
group.add(mesh);
const x = WALK_SPEED * touchTime + STRIDE_LENGTH / 2;
group.position.set(x, 0.018, side * 0.22);
scene.add(group);
markers.push({
group,
material,
touchTime,
side
});
}
for (let cycleIndex = -2; cycleIndex < 42; cycleIndex++) {
addFootprintMarker(cycleIndex * CYCLE_SECONDS, -1);
addFootprintMarker(
(cycleIndex + 0.5) * CYCLE_SECONDS,
1
);
}
// ------------------------------------------------------------
// Stylized character
// ------------------------------------------------------------
const character = new THREE.Group();
scene.add(character);
const pelvisMaterial = new THREE.MeshStandardMaterial({
color: 0x263246,
roughness: 0.72,
metalness: 0.02
});
const soleMaterial = new THREE.MeshStandardMaterial({
color: 0x18202d,
roughness: 0.84
});
const pelvis = new THREE.Mesh(
new THREE.SphereGeometry(1, 28, 18),
pelvisMaterial
);
pelvis.scale.set(0.40, 0.21, 0.34);
pelvis.position.set(0, 1.48, 0);
pelvis.castShadow = true;
character.add(pelvis);
const waist = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.32, 0.30, 24),
pelvisMaterial
);
waist.position.set(0, 1.69, 0);
waist.castShadow = true;
character.add(waist);
function createFoot(phaseMaterial) {
const group = new THREE.Group();
const sole = new THREE.Mesh(
new THREE.BoxGeometry(0.66, 0.06, 0.25),
soleMaterial
);
sole.position.set(0.07, -0.16, 0);
sole.castShadow = true;
group.add(sole);
const upper = new THREE.Mesh(
new THREE.SphereGeometry(1, 24, 14),
phaseMaterial
);
upper.scale.set(0.38, 0.12, 0.225);
upper.position.set(0.07, -0.075, 0);
upper.castShadow = true;
group.add(upper);
const toe = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 12),
phaseMaterial
);
toe.scale.set(0.20, 0.10, 0.205);
toe.position.set(0.34, -0.085, 0);
toe.castShadow = true;
group.add(toe);
const heel = new THREE.Mesh(
new THREE.SphereGeometry(1, 18, 12),
phaseMaterial
);
heel.scale.set(0.17, 0.10, 0.19);
heel.position.set(-0.20, -0.075, 0);
heel.castShadow = true;
group.add(heel);
character.add(group);
return group;
}
const legs = [
{
name: "L",
side: -1,
offset: 0,
material: new THREE.MeshStandardMaterial({
color: STANCE_COLOR,
roughness: 0.62,
metalness: 0.02
})
},
{
name: "R",
side: 1,
offset: 0.5,
material: new THREE.MeshStandardMaterial({
color: STANCE_COLOR,
roughness: 0.62,
metalness: 0.02
})
}
];
for (const leg of legs) {
leg.thigh = new THREE.Mesh(
new THREE.CylinderGeometry(0.145, 0.125, 1, 18),
leg.material
);
leg.calf = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.13, 1, 18),
leg.material
);
leg.knee = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 20, 14),
leg.material
);
leg.hip = new THREE.Mesh(
new THREE.SphereGeometry(0.15, 20, 14),
pelvisMaterial
);
for (const mesh of [
leg.thigh,
leg.calf,
leg.knee,
leg.hip
]) {
mesh.castShadow = true;
character.add(mesh);
}
leg.foot = createFoot(leg.material);
// Blue ring marks the exact world-space contact point.
leg.contactRing = new THREE.Mesh(
new THREE.RingGeometry(0.31, 0.365, 48),
new THREE.MeshBasicMaterial({
color: STANCE_COLOR,
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
})
);
leg.contactRing.rotation.x = -Math.PI / 2;
scene.add(leg.contactRing);
// Orange dashed arc shows the current swing trajectory.
const arcGeometry = new THREE.BufferGeometry();
arcGeometry.setAttribute(
"position",
new THREE.BufferAttribute(
new Float32Array(33 * 3),
3
).setUsage(THREE.DynamicDrawUsage)
);
leg.swingArc = new THREE.Line(
arcGeometry,
new THREE.LineDashedMaterial({
color: SWING_COLOR,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.88,
depthWrite: false
})
);
leg.swingArc.frustumCulled = false;
scene.add(leg.swingArc);
}
// ------------------------------------------------------------
// Stride measurement bracket
// ------------------------------------------------------------
const strideGeometry = new THREE.BufferGeometry();
strideGeometry.setAttribute(
"position",
new THREE.BufferAttribute(
new Float32Array(18),
3
).setUsage(THREE.DynamicDrawUsage)
);
const strideBracket = new THREE.LineSegments(
strideGeometry,
new THREE.LineBasicMaterial({
color: 0x48576b,
transparent: true,
opacity: 0.82
})
);
strideBracket.frustumCulled = false;
scene.add(strideBracket);
// ------------------------------------------------------------
// Gait kinematics
//
// Stance:
// The foot's WORLD x coordinate is constant for the entire
// stance interval. Only the hip/body passes over it.
//
// Swing:
// The foot moves from one fixed touchdown point to the next
// while following a raised arc.
// ------------------------------------------------------------
const up = new THREE.Vector3(0, 1, 0);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(value) {
const x = clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function lerp(a, b, amount) {
return a + (b - a) * amount;
}
function getGaitState(time, offset) {
const cyclePosition = time / CYCLE_SECONDS - offset;
const cycleIndex = Math.floor(cyclePosition);
const phase = cyclePosition - cycleIndex;
const touchdownTime =
(cycleIndex + offset) * CYCLE_SECONDS;
const currentTouchX =
WALK_SPEED * touchdownTime + STRIDE_LENGTH / 2;
const previousTouchX =
currentTouchX - STRIDE_LENGTH;
const state = {
phase,
touchdownTime,
currentTouchX,
previousTouchX,
isStance: phase < DUTY_FACTOR,
footWorldX: currentTouchX,
ankleY: ANKLE_HEIGHT,
footAngle: 0
};
if (state.isStance) {
// Locked world-space contact: no x translation while planted.
state.footWorldX = currentTouchX;
const toeOffAmount = clamp(
(phase - (DUTY_FACTOR - 0.14)) / 0.14,
0,
1
);
state.ankleY =
ANKLE_HEIGHT +
TOE_OFF_LIFT * smoothstep(toeOffAmount);
if (phase < 0.12) {
state.footAngle = lerp(
0.14,
0,
phase / 0.12
);
} else if (phase > DUTY_FACTOR - 0.14) {
state.footAngle = lerp(
0,
-0.20,
(phase - (DUTY_FACTOR - 0.14)) / 0.14
);
}
} else {
const swingProgress =
(phase - DUTY_FACTOR) / (1 - DUTY_FACTOR);
const eased = smoothstep(swingProgress);
state.footWorldX = lerp(
previousTouchX,
currentTouchX,
eased
);
state.ankleY =
ANKLE_HEIGHT +
TOE_OFF_LIFT * (1 - smoothstep(swingProgress)) +
SWING_LIFT * Math.sin(Math.PI * swingProgress);
if (swingProgress < 0.45) {
state.footAngle = lerp(
-0.20,
0.16,
swingProgress / 0.45
);
} else {
state.footAngle = lerp(
0.16,
0.14,
(swingProgress - 0.45) / 0.55
);
}
}
return state;
}
function alignSegment(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(1, length, 1);
mesh.quaternion.setFromUnitVectors(
up,
direction.normalize()
);
}
function solveKnee(hip, ankle) {
const toAnkle = new THREE.Vector3().subVectors(ankle, hip);
const distance = toAnkle.length();
const direction = toAnkle.normalize();
const along =
(
THIGH_LENGTH * THIGH_LENGTH -
CALF_LENGTH * CALF_LENGTH +
distance * distance
) / (2 * distance);
const height = Math.sqrt(
Math.max(
0,
THIGH_LENGTH * THIGH_LENGTH - along * along
)
);
const midpoint = new THREE.Vector3()
.copy(hip)
.addScaledVector(direction, along);
// Forward-pointing perpendicular bends the knee toward +X.
const perpendicular = new THREE.Vector3(
-direction.y,
direction.x,
0
).normalize();
return midpoint.addScaledVector(perpendicular, height);
}
function updateSwingArc(leg, state) {
const positions =
leg.swingArc.geometry.attributes.position.array;
for (let i = 0; i <= 32; i++) {
const u = i / 32;
const eased = smoothstep(u);
const x = lerp(
state.previousTouchX,
state.currentTouchX,
eased
);
const y =
ANKLE_HEIGHT +
TOE_OFF_LIFT * (1 - smoothstep(u)) +
SWING_LIFT * Math.sin(Math.PI * u);
positions[i * 3] = x;
positions[i * 3 + 1] = y;
positions[i * 3 + 2] = leg.side * 0.22;
}
leg.swingArc.geometry.attributes.position.needsUpdate = true;
leg.swingArc.computeLineDistances();
leg.swingArc.visible = !state.isStance;
}
// ------------------------------------------------------------
// Camera controls, implemented without addons
// ------------------------------------------------------------
let cameraYaw = 0.48;
let cameraPitch = 0.34;
let cameraRadius = 7.4;
const DEFAULT_YAW = 0.48;
const DEFAULT_PITCH = 0.34;
const DEFAULT_RADIUS = 7.4;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = clamp(
cameraPitch + deltaY * 0.0045,
0.10,
0.95
);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
4.1,
13
);
},
{ passive: false }
);
document.getElementById("resetView").addEventListener("click", () => {
cameraYaw = DEFAULT_YAW;
cameraPitch = DEFAULT_PITCH;
cameraRadius = DEFAULT_RADIUS;
});
// ------------------------------------------------------------
// HUD and playback
// ------------------------------------------------------------
const phaseElements = {
L: {
state: document.getElementById("phaseL"),
playhead: document.getElementById("playL")
},
R: {
state: document.getElementById("phaseR"),
playhead: document.getElementById("playR")
}
};
const worldLabels = {
L: {
locked: document.getElementById("labelL"),
swing: document.getElementById("swingL")
},
R: {
locked: document.getElementById("labelR"),
swing: document.getElementById("swingR")
}
};
const strideLabel = document.getElementById("strideLabel");
const contactBadge = document.getElementById("contactBadge");
const playPause = document.getElementById("playPause");
let paused = false;
let playbackRate = 1;
playPause.addEventListener("click", () => {
paused = !paused;
playPause.textContent = paused ? "Play" : "Pause";
});
document.querySelectorAll(".speed-button").forEach((button) => {
button.addEventListener("click", () => {
playbackRate = Number(button.dataset.speed);
document.querySelectorAll(".speed-button").forEach((other) => {
other.classList.toggle("active", other === button);
});
});
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playPause.click();
}
});
function setLabelPosition(element, worldPosition, visible, offsetX = 0) {
const projected = worldPosition.clone().project(camera);
const onScreen =
visible &&
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
element.style.visibility = onScreen ? "visible" : "hidden";
if (!onScreen) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateStrideBracket(rightState) {
const startX = rightState.currentTouchX;
const endX = startX + STRIDE_LENGTH;
const y = 0.065;
const z = 0.86;
const positions = strideGeometry.attributes.position.array;
positions.set([
startX, y, z,
endX, y, z,
startX, y, z - 0.10,
startX, y, z + 0.10,
endX, y, z - 0.10,
endX, y, z + 0.10
]);
strideGeometry.attributes.position.needsUpdate = true;
}
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
let simulationTime = 0;
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
simulationTime += delta * playbackRate;
}
const bodyX = WALK_SPEED * simulationTime;
character.position.x = bodyX;
// Subtle vertical pelvis motion. Feet remain independently
// grounded or lifted according to their gait phase.
const bob =
Math.sin((simulationTime / CYCLE_SECONDS) * Math.PI * 4) *
0.018;
pelvis.position.y = 1.48 + bob;
waist.position.y = 1.69 + bob;
let plantedFeet = 0;
const states = {};
for (const leg of legs) {
const state = getGaitState(
simulationTime,
leg.offset
);
states[leg.name] = state;
if (state.isStance) plantedFeet++;
const hip = new THREE.Vector3(
0,
1.48 + bob,
leg.side * 0.17
);
const ankle = new THREE.Vector3(
state.footWorldX - bodyX,
state.ankleY,
leg.side * 0.22
);
const knee = solveKnee(hip, ankle);
leg.hip.position.copy(hip);
leg.knee.position.copy(knee);
alignSegment(leg.thigh, hip, knee);
alignSegment(leg.calf, knee, ankle);
leg.foot.position.copy(ankle);
leg.foot.rotation.z = state.footAngle;
if (state.isStance) {
leg.material.color.copy(STANCE_COLOR);
} else {
leg.material.color.copy(SWING_COLOR);
}
// The contact ring remains at the fixed world-space point.
leg.contactRing.position.set(
state.footWorldX,
0.025,
leg.side * 0.22
);
leg.contactRing.visible = state.isStance;
const ringPulse =
1 + Math.sin(simulationTime * 8 + leg.side) * 0.035;
leg.contactRing.scale.setScalar(ringPulse);
updateSwingArc(leg, state);
const phaseUI = phaseElements[leg.name];
phaseUI.playhead.style.left = `${state.phase * 100}%`;
if (state.isStance) {
phaseUI.state.textContent = "STANCE";
phaseUI.state.className = "phase-state stance";
} else {
phaseUI.state.textContent = "SWING";
phaseUI.state.className = "phase-state swing";
}
}
contactBadge.textContent =
plantedFeet === 2
? "2 feet locked"
: plantedFeet === 1
? "1 foot locked"
: "Swing phase";
// Pulse alternating footprint markers around touchdown.
for (const marker of markers) {
const age = simulationTime - marker.touchTime;
const pulse = age >= 0 && age < 0.75
? Math.exp(-age * 5)
: 0;
marker.material.opacity =
age < 0 ? 0.18 : 0.13 + pulse * 0.48;
const scale = 1 + pulse * 0.10;
marker.group.scale.set(scale, scale, scale);
}
updateStrideBracket(states.R);
// Keep the directional light and its shadow volume around
// the walking character.
keyLight.position.set(bodyX + 4, 8, 5);
keyLight.target.position.set(bodyX, 0.7, 0);
// Follow camera.
const target = new THREE.Vector3(bodyX, 0.88, 0);
const horizontalRadius =
cameraRadius * Math.cos(cameraPitch);
camera.position.set(
target.x + Math.sin(cameraYaw) * horizontalRadius,
target.y + Math.sin(cameraPitch) * cameraRadius,
target.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(target);
camera.updateMatrixWorld();
// World-space annotations.
for (const leg of legs) {
const state = states[leg.name];
const label = worldLabels[leg.name];
const footWorldPosition = new THREE.Vector3(
state.footWorldX,
state.ankleY + 0.54,
leg.side * 0.22
);
const screenOffset = leg.side < 0 ? -19 : 19;
setLabelPosition(
label.locked,
footWorldPosition,
state.isStance,
screenOffset
);
setLabelPosition(
label.swing,
footWorldPosition,
!state.isStance,
screenOffset
);
}
setLabelPosition(
strideLabel,
new THREE.Vector3(
states.R.currentTouchX + STRIDE_LENGTH / 2,
0.25,
0.98
),
true
);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
}
window.addEventListener("resize", resize);
requestAnimationFrame(animate);
</script>
</body>
</html>
```