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 helpers for the procedural 3D humanoid and its on-screen visibility (no DOM / WebGL).import { multiply, translation, scaling, transformPoint, buildSkeleton, clamp } from './pose.mjs'; /** Unit sphere (radius 0.5) with smooth normals, interleaved [px,py,pz,nx,ny,nz]. */export function sphereMesh(rings = 14, segs = 20) { const v = [], idx = []; for (let r = 0; r <= rings; r++) { const th = (r / rings) * Math.PI, y = Math.cos(th), s = Math.sin(th); for (let g = 0; g <= segs; g++) { const ph = (g / segs) * Math.PI * 2, x = s * Math.cos(ph), z = s * Math.sin(ph); v.push(x * 0.5, y * 0.5, z * 0.5, x, y, z); } } for (let r = 0; r < rings; r++) for (let g = 0; g < segs; g++) { const a = r * (segs + 1) + g, b = a + segs + 1; idx.push(a, a + 1, b, b, a + 1, b + 1); } return { vertices: new Float32Array(v), indices: new Uint16Array(idx) };} /** * The renderer may be called as render(state, dt) or render(state, { time, ... }) (main.mjs does * the latter). A non-number dt made the clock NaN, every pose NaN and the fighters invisible. */export function resolveDt(arg, lastTime) { if (typeof arg === 'number' && Number.isFinite(arg)) return { dt: clamp(arg, 0, 0.1), time: lastTime }; const t = arg && typeof arg.time === 'number' && Number.isFinite(arg.time) ? arg.time : null; if (t === null) return { dt: 1 / 60, time: lastTime }; const dt = lastTime == null ? 1 / 60 : clamp(t - lastTime, 0, 0.1); return { dt, time: t };} /** Direction *towards* the key light, normalized and always from above. */export function keyLightDir(dir) { let [x, y, z] = Array.isArray(dir) && dir.length === 3 ? dir : [0.35, 0.8, 0.5]; if (y < 0) { x = -x; y = -y; z = -z; } const l = Math.hypot(x, y, z) || 1; return [x / l, y / l, z / l];} const ROUND = new Set(['head', 'lHand', 'rHand']);const darker = (c, k) => c.map((v) => v * k); /** * Smooth humanoid built from shaded ellipsoids, driven by the pose skeleton. * Returns [{ name, matrix, size, color, shininess }] where size scales a unit sphere. */export function humanoidParts(fighter, pose, palette) { const out = []; const hair = [0.12, 0.08, 0.06]; for (const p of buildSkeleton(fighter, pose, palette)) { let size = p.size.slice(), color = p.color, shin = 0.2; if (/Thigh|Shin|UpperArm|ForeArm/.test(p.name)) { size = [size[0] * 1.25, size[1] * 1.22, size[2] * 1.25]; shin = 0.35; } else if (p.name === 'torso') { size = [size[0] * 1.05, size[1] * 1.12, size[2] * 1.05]; shin = 0.3; } else if (p.name === 'pelvis') size = [size[0] * 1.1, size[1] * 1.6, size[2] * 1.1]; else if (/Foot/.test(p.name)) { size = [size[0] * 1.15, size[1] * 1.4, size[2] * 1.2]; color = darker(color, 0.5); } else if (ROUND.has(p.name)) { size = size.map((v) => v * 1.12); shin = 0.15; } else if (p.name === 'band') size = [size[0] * 1.12, size[1], size[2] * 1.12]; out.push({ name: p.name, matrix: p.matrix, size, color, shininess: shin }); if (p.name === 'torso') { // shoulders (deltoids) and neck for (const z of [1, -1]) out.push({ name: 'deltoid', matrix: multiply(p.matrix, translation(0, 0.2, z * 0.17)), size: [0.14, 0.13, 0.14], color, shininess: 0.3 }); out.push({ name: 'neck', matrix: multiply(p.matrix, translation(0, 0.3, 0)), size: [0.09, 0.12, 0.09], color: palette.skin, shininess: 0.15 }); } if (p.name === 'head') { out.push({ name: 'hair', matrix: multiply(p.matrix, translation(-0.015, 0.035, 0)), size: [0.23, 0.18, 0.22], color: hair, shininess: 0.5 }); out.push({ name: 'nose', matrix: multiply(p.matrix, translation(0.11, -0.01, 0)), size: [0.04, 0.04, 0.03], color: palette.skin, shininess: 0.1 }); for (const z of [1, -1]) out.push({ name: 'eye', matrix: multiply(p.matrix, translation(0.1, 0.02, z * 0.045)), size: [0.02, 0.025, 0.025], color: [0.05, 0.05, 0.06], shininess: 1 }); } } return out;} /** Soft blob shadow: stacked discs, wide+faint to narrow+dark. */export function shadowDiscs(x, height, steps = 4) { const k = clamp(1 - height * 0.35, 0.3, 1), out = []; for (let i = 0; i < steps; i++) { const s = (1 - i / steps) * 0.9 * k; out.push({ position: [x, 0.002 + i * 0.001, 0], size: [s, 0.003, s * 0.6], darkness: (i + 1) / steps }); } return out;} /** Project world-space parts with (projection*view) and return NDC bounds. */export function screenBounds(parts, viewProj) { let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; for (const p of parts) { const m = multiply(viewProj, multiply(p.matrix, scaling(p.size[0], p.size[1], p.size[2]))); for (const c of [[-.5, -.5, -.5], [.5, .5, .5], [-.5, .5, .5], [.5, -.5, -.5]]) { const [x, y] = transformPoint(m, c); minX = Math.min(minX, x); maxX = Math.max(maxX, x); minY = Math.min(minY, y); maxY = Math.max(maxY, y); } } return { minX, minY, maxX, maxY };}