gitflarePublicPartly agent-ready40
shadow-fight-web-version
No description yet.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
132be7cOct 23, 2026
Make the player visible with a procedural 3D humanoid
132be7c// Pure pose, skeleton and camera math for the 3D renderer. No WebGL, no DOM.// Engine units: x along the arena (px), y = height above the floor (px, up).// World units: 1 unit = 100 engine px. Fighters face +x (facing=1) or -x (facing=-1). export const UNIT = 0.01;export const lerp = (a, b, t) => a + (b - a) * t;export const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));export const smoothstep = (t) => { const x = clamp(t, 0, 1); return x * x * (3 - 2 * x); }; // ---------------------------------------------------------------- mat4 (column-major, WebGL layout)export function identity() { return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];}export function multiply(a, b) { const out = new Array(16); for (let c = 0; c < 4; c++) for (let r = 0; r < 4; r++) { let s = 0; for (let k = 0; k < 4; k++) s += a[k * 4 + r] * b[c * 4 + k]; out[c * 4 + r] = s; } return out;}export function translation(x, y, z) { const m = identity(); m[12] = x; m[13] = y; m[14] = z; return m;}export function scaling(x, y, z) { const m = identity(); m[0] = x; m[5] = y; m[10] = z; return m;}export function rotationX(a) { const c = Math.cos(a), s = Math.sin(a), m = identity(); m[5] = c; m[6] = s; m[9] = -s; m[10] = c; return m;}export function rotationY(a) { const c = Math.cos(a), s = Math.sin(a), m = identity(); m[0] = c; m[2] = -s; m[8] = s; m[10] = c; return m;}export function rotationZ(a) { const c = Math.cos(a), s = Math.sin(a), m = identity(); m[0] = c; m[1] = s; m[4] = -s; m[5] = c; return m;}export function transformPoint(m, [x, y, z]) { const w = m[3] * x + m[7] * y + m[11] * z + m[15] || 1; return [ (m[0] * x + m[4] * y + m[8] * z + m[12]) / w, (m[1] * x + m[5] * y + m[9] * z + m[13]) / w, (m[2] * x + m[6] * y + m[10] * z + m[14]) / w ];}export function perspective(fovY, aspect, near, far) { const f = 1 / Math.tan(fovY / 2), nf = 1 / (near - far); const m = new Array(16).fill(0); m[0] = f / aspect; m[5] = f; m[10] = (far + near) * nf; m[11] = -1; m[14] = 2 * far * near * nf; return m;}const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];export function normalize(v) { const l = Math.hypot(v[0], v[1], v[2]) || 1; return [v[0] / l, v[1] / l, v[2] / l];}export function lookAt(eye, target, up = [0, 1, 0]) { const z = normalize(sub(eye, target)); const x = normalize(cross(up, z)); const y = cross(z, x); return [x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, -dot(x, eye), -dot(y, eye), -dot(z, eye), 1];} // ---------------------------------------------------------------- camera/** * Compute a perspective camera that frames both fighters. * Returns { eye, target, fov, view, projection } with matrices ready for WebGL. */export function computeCamera(state, aspect = 16 / 9, opts = {}) { const fighters = (state && state.fighters) || []; const xs = fighters.map((f) => f.x * UNIT); const ys = fighters.map((f) => f.y * UNIT); const arenaW = ((state && state.level && state.level.arena && state.level.arena.width) || 960) * UNIT; const cx = xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : arenaW / 2; const spread = xs.length > 1 ? Math.abs(xs[0] - xs[1]) : 2; const lift = ys.length ? Math.max(...ys) * 0.3 : 0; const fov = opts.fov ?? Math.PI / 4; const minDist = opts.minDistance ?? 5.5; const dist = clamp((spread / 2 + 1.6) / Math.tan(fov / 2) / Math.max(aspect, 0.5) * 1.15, minDist, opts.maxDistance ?? 10); const target = [cx, 0.95 + lift, 0]; const eye = [cx, 1.6 + lift + dist * 0.12, dist]; return { eye, target, fov, view: lookAt(eye, target), projection: perspective(fov, aspect, 0.1, 60) };} // ---------------------------------------------------------------- poses// A pose is a set of joint angles in radians. Hips/knees: positive = forward (facing direction).// Shoulders/elbows: negative = raise forward (guard / punch), positive = swing back.export const POSE_KEYS = ['torso', 'head', 'lShoulder', 'lElbow', 'rShoulder', 'rElbow', 'lHip', 'lKnee', 'rHip', 'rKnee', 'crouch', 'lean']; export function zeroPose() { const p = {}; for (const k of POSE_KEYS) p[k] = 0; return p;}function pose(partial) { return Object.freeze({ ...zeroPose(), ...partial }); } export const POSES = Object.freeze({ idle: pose({ torso: 0.05, lShoulder: -0.9, lElbow: -1.6, rShoulder: -1.1, rElbow: -1.5, lHip: 0.25, lKnee: -0.35, rHip: -0.2, rKnee: -0.35, crouch: 0.06 }), walk: pose({ torso: 0.1, lShoulder: -0.6, lElbow: -1.3, rShoulder: -0.9, rElbow: -1.3, crouch: 0.03 }), jump: pose({ torso: -0.1, lShoulder: -2.4, lElbow: -0.8, rShoulder: -2.4, rElbow: -0.8, lHip: 0.9, lKnee: -1.4, rHip: 0.9, rKnee: -1.4 }), block: pose({ torso: 0.2, head: 0.2, lShoulder: -1.5, lElbow: -2.2, rShoulder: -1.6, rElbow: -2.0, lHip: 0.3, lKnee: -0.5, rHip: -0.2, rKnee: -0.5, crouch: 0.12 }), punchWind: pose({ torso: -0.15, lShoulder: -0.8, lElbow: -2.0, rShoulder: -1.2, rElbow: -1.7, lHip: 0.3, lKnee: -0.4, rHip: -0.2, rKnee: -0.4, crouch: 0.05, lean: -0.15 }), punchHit: pose({ torso: 0.25, lShoulder: -1.6, lElbow: 0, rShoulder: -0.9, rElbow: -1.8, lHip: 0.5, lKnee: -0.5, rHip: -0.4, rKnee: -0.3, crouch: 0.08, lean: 0.3 }), kickWind: pose({ torso: 0.05, lShoulder: -0.9, lElbow: -1.5, rShoulder: -1.2, rElbow: -1.4, lHip: 1.3, lKnee: -2.0, rHip: -0.2, rKnee: -0.3, crouch: 0.08, lean: -0.1 }), kickHit: pose({ torso: -0.35, lShoulder: -0.4, lElbow: -1.0, rShoulder: -1.8, rElbow: -1.0, lHip: 1.75, lKnee: -0.1, rHip: -0.3, rKnee: -0.2, crouch: 0.04, lean: -0.2 }), hit: pose({ torso: -0.35, head: -0.5, lShoulder: 0.6, lElbow: -0.9, rShoulder: 0.9, rElbow: -1.0, lHip: 0.4, lKnee: -0.6, rHip: -0.3, rKnee: -0.4, crouch: 0.1, lean: -0.3 }), ko: pose({ torso: -1.45, head: -0.3, lShoulder: 0.4, lElbow: -0.3, rShoulder: 0.5, rElbow: -0.2, lHip: 0.3, lKnee: -0.2, rHip: 0.1, rKnee: -0.1, crouch: 1.1, lean: -1.2 })}); /** Linear blend between two poses. */export function blendPoses(a, b, t) { const x = clamp(t, 0, 1), out = {}; for (const k of POSE_KEYS) out[k] = lerp(a[k] ?? 0, b[k] ?? 0, x); return out;} /** * Target pose for a fighter given its engine state. `time` (seconds) drives cyclic animation * for idle breathing and walk cycles. Attack phases use the engine's attack timer so the pose * matches startup / active / recovery frames when `attackSpecs` (CONFIG.attacks) is given. */export function targetPose(fighter, time = 0, attackSpecs = null) { const stance = fighter.stance || 'idle'; switch (stance) { case 'walk': { const dir = fighter.vx ? Math.sign(fighter.vx) * (fighter.facing || 1) : 1; const ph = time * 9 * dir; const swing = Math.sin(ph); const p = { ...POSES.walk }; p.lHip = swing * 0.7; p.rHip = -swing * 0.7; p.lKnee = -Math.max(0, -swing) * 1.1 - 0.15; p.rKnee = -Math.max(0, swing) * 1.1 - 0.15; p.lShoulder += -swing * 0.4; p.rShoulder += swing * 0.4; p.crouch = 0.03 + Math.abs(Math.cos(ph)) * 0.03; return p; } case 'punch': case 'kick': { const spec = attackSpecs && attackSpecs[stance]; const t = fighter.attack ? fighter.attack.t : 0; const wind = POSES[stance + 'Wind'], hit = POSES[stance + 'Hit']; if (!spec) return t < 0.1 ? wind : hit; const end = spec.startup + spec.active; if (t < spec.startup) { const u = smoothstep(t / spec.startup); const p = blendPoses(POSES.idle, wind, u); p.crouch += Math.sin(u * Math.PI) * 0.06; // anticipation dip p.lean -= Math.sin(u * Math.PI) * 0.08; return p; } if (t < end) return hit; const r = clamp((t - end) / spec.recovery, 0, 1); const p = blendPoses(hit, POSES.idle, smoothstep(r)); p.lean += Math.sin(r * Math.PI) * 0.1; // follow-through p.torso += Math.sin(r * Math.PI) * 0.08; return p; } case 'idle': { const p = { ...POSES.idle }; const b = Math.sin(time * 2.2); p.lean += Math.sin(time * 1.1) * 0.04; // weight shift p.lHip += Math.sin(time * 1.1) * 0.05; p.rHip -= Math.sin(time * 1.1) * 0.05; p.crouch += b * 0.012; p.torso += b * 0.03; p.lShoulder += b * 0.05; p.rShoulder -= b * 0.04; return p; } case 'hit': { // hit reaction: recoil peaks right after impact (fighter.hitT seconds since hit) then settles const ht = fighter.hitT ?? 0; const p = { ...POSES.hit }; const k = Math.exp(-ht * 8); p.torso *= 0.6 + 0.6 * k; p.lean *= 0.6 + 0.6 * k; p.head *= 0.6 + 0.6 * k; return p; } default: return POSES[stance] || POSES.idle; }} /** Exponential smoothing towards a target pose; rate in 1/s, dt in s. Returns a new pose. */export function smoothPose(current, target, dt, rate = 18) { if (!current) return { ...target }; return blendPoses(current, target, 1 - Math.exp(-rate * Math.max(0, dt)));} // ---------------------------------------------------------------- skeleton// Body dimensions in world units (fighter ~1.5 units = 150 px tall).export const BODY = Object.freeze({ hipHeight: 0.78, torsoLen: 0.5, headSize: 0.2, neck: 0.04, upperArm: 0.26, foreArm: 0.26, thigh: 0.4, shin: 0.38, shoulderW: 0.19, hipW: 0.1, limbR: 0.045, torsoW: 0.34, torsoD: 0.2}); function box(name, m, size, color) { return { name, matrix: m, size, color }; } /** * Build the list of world-space primitives for a posed fighter. * Each part is { name, matrix (mat4, box centre), size [sx,sy,sz], color [r,g,b] }. * A unit cube (-0.5..0.5) scaled by size and transformed by matrix renders the part. */export function buildSkeleton(fighter, p, palette) { const B = BODY, parts = []; const skin = palette.skin, suit = palette.suit, accent = palette.accent; const facing = fighter.facing || 1; const root = multiply(translation(fighter.x * UNIT, fighter.y * UNIT, 0), rotationY(facing === 1 ? 0 : Math.PI)); const crouch = p.crouch || 0; const hipY = BODY.hipHeight - crouch * 0.5; const pelvis = multiply(root, translation(0, hipY, 0)); parts.push(box('pelvis', pelvis, [B.torsoW * 0.8, 0.12, B.torsoD], suit)); const legs = [['l', p.lHip, p.lKnee, 1], ['r', p.rHip, p.rKnee, -1]]; for (const [side, hip, knee, z] of legs) { const hipM = multiply(pelvis, multiply(translation(0, 0, z * B.hipW), rotationZ(hip))); const thigh = multiply(hipM, translation(0, -B.thigh / 2, 0)); parts.push(box(side + 'Thigh', thigh, [B.limbR * 2.2, B.thigh, B.limbR * 2.2], suit)); const kneeM = multiply(hipM, multiply(translation(0, -B.thigh, 0), rotationZ(knee))); const shin = multiply(kneeM, translation(0, -B.shin / 2, 0)); parts.push(box(side + 'Shin', shin, [B.limbR * 2, B.shin, B.limbR * 2], suit)); const foot = multiply(kneeM, translation(0.05, -B.shin - 0.03, 0)); parts.push(box(side + 'Foot', foot, [0.18, 0.06, 0.09], accent)); } const torsoBase = multiply(pelvis, multiply(rotationZ(p.lean || 0), rotationY(p.torso || 0))); const chest = multiply(torsoBase, translation(0, B.torsoLen / 2 + 0.05, 0)); parts.push(box('torso', chest, [B.torsoW, B.torsoLen, B.torsoD], suit)); parts.push(box('belt', multiply(torsoBase, translation(0, 0.1, 0)), [B.torsoW * 1.02, 0.05, B.torsoD * 1.04], accent)); const neckM = multiply(torsoBase, multiply(translation(0, B.torsoLen + 0.05 + B.neck, 0), rotationZ(p.head || 0))); parts.push(box('head', multiply(neckM, translation(0, B.headSize / 2 + 0.02, 0)), [B.headSize, B.headSize, B.headSize * 0.95], skin)); parts.push(box('band', multiply(neckM, translation(0, B.headSize * 0.8, 0)), [B.headSize * 1.04, 0.035, B.headSize * 1.0], accent)); const arms = [['l', p.lShoulder, p.lElbow, 1], ['r', p.rShoulder, p.rElbow, -1]]; for (const [side, sh, el, z] of arms) { const shM = multiply(torsoBase, multiply(translation(0, B.torsoLen + 0.02, z * B.shoulderW), rotationZ(-sh))); const upper = multiply(shM, translation(0, -B.upperArm / 2, 0)); parts.push(box(side + 'UpperArm', upper, [B.limbR * 2, B.upperArm, B.limbR * 2], suit)); const elM = multiply(shM, multiply(translation(0, -B.upperArm, 0), rotationZ(-el))); const fore = multiply(elM, translation(0, -B.foreArm / 2, 0)); parts.push(box(side + 'ForeArm', fore, [B.limbR * 1.8, B.foreArm, B.limbR * 1.8], skin)); parts.push(box(side + 'Hand', multiply(elM, translation(0, -B.foreArm - 0.03, 0)), [0.08, 0.08, 0.08], skin)); } return parts;} /** Hex colour string → [r,g,b] in 0..1. */export function hexToRgb(hex, fallback = [1, 1, 1]) { if (typeof hex !== 'string') return fallback; const m = hex.replace('#', ''); if (m.length !== 6) return fallback; const n = parseInt(m, 16); if (Number.isNaN(n)) return fallback; return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];} export const PALETTES = Object.freeze([ Object.freeze({ skin: [0.93, 0.76, 0.62], suit: [0.16, 0.22, 0.55], accent: [0.96, 0.78, 0.4] }), Object.freeze({ skin: [0.65, 0.45, 0.33], suit: [0.6, 0.12, 0.16], accent: [0.9, 0.9, 0.95] })]);