gizgame/figure
A rigged figure in play: the clip it shows (Animator) and the characters of one kind a frame draws (Cast).
import * as Figure from "gizgame/figure";Types
Played
type Played = { posture: Gltf.Posture; placed: Matrix4; }A character's pose in a frame (Animator.play) and where its model stands.
posture:gizlang_gltf.Postureplaced:gizlang_geometry.Matrix4
Anchor
type Anchor = { joint: string; to: Point3; }A joint of a character held at a place of the world: a rider's hips on its seat. The model moves so the joint, as the last pose had it, is there.
joint:stringto:gizlang_geometry.Point3
Staging
type Staging = { over?: Gltf.Over; anchor?: Anchor; }How a character is drawn beyond its clip (Cast.stand): OVER, a clip laid over its upper body (Gltf.Over), and its ANCHOR.
over:_gizlang_sdk_core.Option<gizlang_gltf.Over>anchor:_gizlang_sdk_core.Option<M200.Anchor>
Animator
class Animator { rig: Gltf.Rig; fade: f64; private clip: string; private leaving: string; private weight: f64 = 0; private stride: f64 = 0; private began: f64 = 0; private cycled: f64 = 0; private wasCycled: f64 = 0; private last: f64 = -1; private held: Gltf.Posture = { palettes: [], morphs: new Map<string, f64>() }; private looking: f64 = 0; private look: Gltf.Plant | undefined = undefined; private reaching = new Map<string, Gltf.Plant>(); private body: Gltf.Body; private yaw: f64 = 0.0; private at: Point3 | undefined = undefined; private placed: Matrix4 = identity(); private feet: Vec<Point3> = []; private off: f64 = 0.0; private turn: Turn | undefined = undefined; private layer: Gltf.Over | undefined = undefined; private left: Gltf.Over | undefined = undefined; private fading: f64 = 0.0; private entering: f64 = 1.0; constructor(rig: Gltf.Rig, clip: string, fade: f64 = 0.2, phase: f64 = 0.0) { this.rig = rig; this.clip = clip; this.leaving = clip; this.fade = fade; this.body = new Gltf.Body(phase); } play(clip: string, rate: f64, time: f64, once: bool, planted: Vec<Gltf.Plant>, hold: bool, place: (yaw: f64) => Matrix4 throws string, at: Point3, size: f64, yaw: f64, stands: bool, staging: Staging = {}): Played throws string { if (this.last <0) { this.yaw = yaw; this.off = time; } const step = this.last <0 ? 0.0: Math.max(0, Math.min(0.1, time-this.last)); this.last = time; const moved = this.at === undefined ? point3(0, 0, 0): sub(at, this.at!); this.at = at; const speed = step> 0 ? Math.sqrt(moved.x*moved.x+moved.z*moved.z)/step: 0.0; this.turned(yaw, time, step, stands && speed <MOVES); let placed = place(this.yaw); if (staging.anchor !== undefined) { const a = staging.anchor!; const was = this.held.palettes.length> 0 ? this.held: undefined; const off = sub(a.to, transformed(placed, transformed(this.rig.placeOf(a.joint, was), point3(0, 0, 0)))); placed = multiply(transform(off, { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }, point3(1, 1, 1)), placed); } this.body.at(placed, step, speed); this.stride+ = step*rate; const stays = clip === this.clip; if (!stays) { const back = clip === this.leaving; const cycle = this.rig.length(this.clip); const next = this.rig.length(clip); const turns = cycle> 0 ? (this.stride-this.cycled)/cycle: 0.0; const share = turns-Math.floor(turns); const cycled = back ? this.wasCycled: this.stride-share*next; this.wasCycled = this.cycled; this.cycled = cycled; this.weight = back ? 1-this.weight: 1; this.leaving = this.clip; this.clip = clip; this.began = this.stride; } this.weight = Math.max(0, this.weight-step/Math.max(0.001, this.fade)); const moment = once ? Math.min(this.stride-this.began, this.rig.length(clip)*0.999): this.stride-this.cycled; const from: Gltf.Fade | undefined = this.weight> 0 ? { clip: this.leaving, time: this.stride-this.wasCycled, weight: this.weight }: undefined; const stepping = this.stepping(time, place, size); const feet = this.rig.feet; let plants = stepping.length> 0 ? planted.filter(p => !feet.includes(p.end)): planted; for (const p of stepping) { plants.push(p); } const inside = inverse(placed); plants = plants.map((p: Gltf.Plant): Gltf.Plant => p.look !== undefined ? { ...p, look: transformed(inside, p.look!), glance: p.glance === undefined ? undefined: transformed(inside, p.glance!) }: p.to === undefined ? p: { ...p, to: transformed(inside, p.to!) }); let reaching = new Map<string, Gltf.Plant>(); let held: Vec<Gltf.Plant> = []; const most = step/Math.max(0.001, this.fade); for (const p of plants) { if (p.to === undefined) { held.push(p); continue; } const was = this.reaching.get(p.end)?.weight ?? 0.0; const want = p.weight ?? 1.0; const on = this.rig.feet.includes(p.end) ? want-was: Math.min(most, want-was); const e = { ...p, weight: was+Math.max(-most, on) }; reaching.set(p.end, e); held.push(e); } for (const [end, p] of this.reaching.entries()) { if (reaching.has(end)) { continue; } const w = (p.weight ?? 0.0)-most; if (w> 0.0) { const e = { ...p, weight: w }; reaching.set(end, e); held.push(e); } } this.reaching = reaching; plants = held; const look = plants.find(p => p.look !== undefined); if (look !== undefined) { this.look = look; } this.looking = Math.max(0, Math.min(1, this.looking+(look !== undefined ? 1: -1)*step/LOOKING)); plants = plants.filter(p => p.look === undefined); if (this.look !== undefined && this.looking> 0) { const eased = Math.smoothstep(0, 1, this.looking); plants.push({ ...this.look!, weight: eased }); } this.placed = placed; const now = staging.over; const was = this.layer; this.layer = now; if (was !== undefined && (now === undefined || now!.clip !== was!.clip || now!.time <was!.time)) { if (now !== undefined || was!.weight> 0.5) { this.left = was; this.fading = 1.0; } } if (was === undefined && now !== undefined && this.fading <= 0.0) { this.entering = 0.0; } this.fading = Math.max(0.0, this.fading-step/Math.max(0.001, this.fade)); this.entering = Math.min(1.0, this.entering+step/Math.max(0.001, this.fade)); let entered: Gltf.Over | undefined = undefined; if (now !== undefined) { const n = now!; entered = { ...n, weight: n.weight*Math.smoothstep(0.0, 1.0, this.entering) }; } let over = entered; if (this.left !== undefined && this.fading> 0.0) { const l = this.left!; const f = Math.smoothstep(0.0, 1.0, this.fading); if (entered === undefined) { over = { ...l, weight: l.weight*f }; } else { const n = entered!; over = { ...n, weight: n.weight+(l.weight-n.weight)*f, leaving: { clip: l.clip, time: l.time, weight: f } }; } } if (hold && stays && over === undefined && this.held.palettes.length> 0) { return { posture: this.held, placed }; } this.held = this.rig.posture(clip, moment, from, plants, this.body, over, !once); if (this.turn === undefined) { this.feet = this.held.feet ?? []; } return { posture: this.held, placed }; } done(): bool throws string { return this.stride-this.began> = this.rig.length(this.clip)*0.999; } private turned(yaw: f64, time: f64, step: f64, still: bool): void { if (!still || this.rig.feet.length !== 2 || this.feet.length !== 2) { this.turn = undefined; this.off = time; this.yaw+ = angleTo(this.yaw, yaw)*(step> 0 ? Math.eased(step, FOLLOWS): 1.0); } else if (this.turn !== undefined) { this.turn = { ...this.turn!, to: yaw }; const t = this.turn!; const s = Math.min(1.0, (time-t.began)/t.takes); this.yaw = t.from+angleTo(t.from, t.to)*Math.smoothstep(0.0, 1.0, s); if (s> = 1.0) { this.yaw = t.to; this.turn = undefined; this.off = time; } } else { const off = angleTo(this.yaw, yaw); if (Math.abs(off) <= SETTLES) { this.off = time; } if (Math.abs(off)> STEPS || time-this.off> WAITS) { const stood = this.feet.map((p: Point3): Point3 => transformed(this.placed, p)); this.turn = { from: this.yaw, to: yaw, began: time, takes: TAKES+TAKING*Math.abs(off), lead: off> 0 ? 0: 1, stood, feet: this.feet }; } } this.body.twist = Math.max(-TWISTS, Math.min(TWISTS, angleTo(this.yaw, yaw))); } private stepping(time: f64, place: (yaw: f64) => Matrix4 throws string, size: f64): Vec<Gltf.Plant> throws string { if (this.turn === undefined) { return []; } const t = this.turn!; const s = Math.min(1.0, (time-t.began)/t.takes); const end = place(t.to); let out: Vec<Gltf.Plant> = []; for (const i of range(0, 2)) { const u = Math.max(0.0, Math.min(1.0, (s-(i === t.lead ? 0.0: 0.5))*2.0)); const e = Math.smoothstep(0.0, 1.0, u); const there = transformed(end, t.feet[i]); out.push({ end: this.rig.feet[i], to: add(add(t.stood[i], scale(sub(there, t.stood[i]), e)), point3(0, LIFTS*size*Math.sin(Math.PI*u), 0)) }); } return out; } }A rig's clips as a character shows them: the clip it plays, blended from the one before over FADE seconds, its time running at the rate the game gives (so feet do not slide). A clip played ONCE (an attack, a fall) starts from its beginning and holds its last pose. Over its clips its body moves of itself (Gltf.Body: it breathes, its loose parts swing) and turns in place on its feet.
rig:gizlang_gltf.Rigfade:f64clip:stringleaving:stringweight:f64stride:f64began:f64cycled:f64— Where on the stride its looped clip, and the clip it leaves, started their cycles (Animator play).wasCycled:f64last:f64held:gizlang_gltf.Posturelooking:f64— How far the head turns to what it looks at (0 to 1, eased over LOOKING seconds), and the last look.look:_gizlang_sdk_core.Option<gizlang_gltf.Plant>reaching:_gizlang_sdk_collections.Map<string, gizlang_gltf.Plant>— Each limb's place as last eased (the model's space), by its joint.body:gizlang_gltf.Body— Its body's own motion, its own yaw (it lags the one it is given while it stands), where it stood and was placed in the last frame, where its clip had its feet then, since when its yaw has been off by more than SETTLES, and its turn in place under way.yaw:f64at:_gizlang_sdk_core.Option<gizlang_geometry.Point3>placed:gizlang_geometry.Matrix4feet:Vec<gizlang_geometry.Point3>off:f64turn:_gizlang_sdk_core.Option<M200.Turn>layer:_gizlang_sdk_core.Option<gizlang_gltf.Over>— The upper layer (Staging over) of the last frame, and the one it left, still shown by LEFT's weight times FADING.left:_gizlang_sdk_core.Option<gizlang_gltf.Over>fading:f64entering:f64— How far a layer that came has faded in (0 to 1, over FADE).
Cast
class Cast { stage: Stage; name: string; rig: Gltf.Rig; private most: i64; private clip: string; private animators = new Map<i64, Animator>(); private renewed = new Map<i64, f64>(); private used: i64 = 0; private eyes: bool; private looks: bool; private garments: Vec<string>; constructor(stage: Stage, name: string, rig: Gltf.Rig, most: i64, clip: string, far: Vec<f64> = []) throws string { this.stage = stage; this.name = name; this.rig = rig; this.most = most; this.clip = clip; this.eyes = rig.parts.some(p => (p.rigging?.names ?? []).includes("eyesClosed_L")); this.looks = rig.parts.some(p => (p.rigging?.names ?? []).includes("eyesLookLeft_L")); this.garments = garments(rig.parts); stage.model(name, rig.parts, most, true, far); rig.lean(); } private wearing(worn: Vec<string> | undefined): i64 { let mask = worn === undefined ? WEARS_ALL: 0; for (const name of worn ?? []) { const bit = this.garments.indexOf(name); if (bit> = 0) { mask = mask | (1 <<bit); } } return mask; } passing(at: Point3, size: f64, yaw: f64, clip: string, stride: f64, worn: Vec<string> | undefined): bool throws string { if (this.used> = this.most) { return false; } this.used+ = 1; const frames = Math.maxI64(1, i64(Math.ceil(this.rig.length(clip)*SHARED))); const frame = ((i64(Math.floor(stride*SHARED))%frames)+frames)%frames; const key = `${clip } ${ frame }`; let posture = this.shared.get(key); if (posture === undefined) { posture = this.rig.posture(clip, f64(frame)/SHARED, undefined, [], this.still, undefined); this.shared.set(key, posture!); } const mask = this.wearing(worn); this.stage.pose(this.name, this.stage.fit(this.name, at, size, yaw, false, 0, 0), posture!.palettes, [], mask); return true; } private shared = new Map<string, Gltf.Posture>(); private still = new Gltf.Body(0.0); begin(): void { this.used = 0; } rested(joint: string, at: Point3, size: f64, yaw: f64): Point3 throws string { return transformed(this.stage.fit(this.name, at, size, yaw, false, 0, 0), transformed(this.rig.placeOf(joint), point3(0, 0, 0))); } stand(slot: i64, at: Point3, size: f64, yaw: f64, clip: string, rate: f64, time: f64, once: bool = false, lying: bool = false, pitch: f64 = 0.0, roll: f64 = 0.0, planted: Vec<Gltf.Plant> = [], far: f64 = 0.0, face: Map<string, f64> = new Map<string, f64>(), worn: Vec<string> | undefined = undefined, staging: Staging = {}): Played | undefined throws string { if (this.used> = this.most) { return undefined; } this.used+ = 1; let animator = this.animators.get(slot) ?? new Animator(this.rig, this.clip, 0.2, f64(slot)*2.39996); this.animators.set(slot, animator); const every = far <8 ? 0.0: 1.0/30.0; const made = this.renewed.get(slot) ?? -1.0e9; const hold = time-made <every-1.0e-6 && time> = made; const phase = every*((f64(slot)*0.618034)%1.0); if (!hold) { this.renewed.set(slot, made> -1.0e8 && time-made <2.0*every ? made+every: time-phase); } const place = (turned: f64): Matrix4 throws string => this.stage.fit(this.name, at, size, turned, lying, pitch, roll); const played = animator.play(clip, rate, time, once, planted, hold, place, at, size, yaw, !lying && !once && staging.anchor === undefined, staging); const posture = played.posture; const placed = played.placed; let weights = face; const shut = this.eyes ? lids(time, f64(slot)): 0.0; const gaze = this.looks ? posture.gaze: undefined; if (shut> 0 || posture.morphs.size> 0 || gaze !== undefined) { let held = new Map<string, f64>(); if (shut> 0) { for (const side of ["L", "R"]) { held.set(`eyesClosed_${side }`, shut); } } if (gaze !== undefined) { const turned = (way: string, angle: f64): void => { for (const side of ["L", "R"]) { held.set(`eyesLook${way }_${ side }`, Math.min(1, Math.max(0, angle/EYE_TURN))); } }; turned("Left", gaze![0]); turned("Right", -gaze![0]); turned("Up", gaze![1]); turned("Down", -gaze![1]); } weights = Gltf.layered(Gltf.layered(face, posture.morphs, true), held, false); } const mask = this.wearing(worn); this.stage.pose(this.name, placed, posture.palettes, weights.size> 0 ? this.rig.faces(weights): [], mask); return played; } }Up to MOST characters of one rig on a stage at once, each frame: a crowd of one kind, such as the soldiers of a match or a village's folk. A game gives each character a slot (a player's number, an entity's index), whose Animator keeps its clip from frame to frame, and draws it with stand: every character is an instance of the one model, bent by its own joints.
stage:gizlang_meshdraw.Stagename:stringrig:gizlang_gltf.Rigmost:i64clip:stringanimators:_gizlang_sdk_collections.Map<i64, Ref<M200.Animator>>renewed:_gizlang_sdk_collections.Map<i64, f64>— When each character's pose was last made, by its slot.used:i64— The characters this one drew this frame (begin starts a frame).eyes:bool— Whether the rig's face can close its eyes (morph targets eyesClosed_L and _R): its characters then blink; and whether it can turn them (eyesLookLeft_L and the rest): its characters then look at what they look at.looks:boolgarments:Vec<string>— Its rig's garments, in the order of their bits (gizlang/meshdraw garments).shared:_gizlang_sdk_collections.Map<string, gizlang_gltf.Posture>— The poses passers-by share, by clip and frame (passing), and the still body they are made for.still:gizlang_gltf.Body
Functions
constructor
constructor(rig: Gltf.Rig, clip: string, fade: f64 = 0.2, phase: f64 = 0.0)RIG, starting with CLIP; its body breathes from PHASE (radians).
Effects: fail, write, alloc.
constructor
constructor(stage: Stage, name: string, rig: Gltf.Rig, most: i64, clip: string, far: Vec<f64> = []) throws stringDeclares RIG's model as NAME on STAGE, for MOST characters a frame, drawn coarser from each distance of FAR (meters, rising: Stage.model); a character starts with CLIP.
Effects: fail, read, write, console, io, alloc.
dressingHands
function dressingHands(at: Point3, yaw: f64, size: f64, t: f64, hands: Vec<string>): Vec<Gltf.Plant>Where HANDS (left, right) of one SIZE tall standing at AT facing YAW reach as it changes its top, T of the way through (Game.change): crossed to grip the hem at the hips, up over the head (the tops change there), the new one pulled down to the hips, and let go. Each place is a share of its height up, ahead and to its right.
Effects: fail, alloc.
Methods
Animator.play
play(clip: string, rate: f64, time: f64, once: bool, planted: Vec<Gltf.Plant>, hold: bool, place: (yaw: f64) => Matrix4 throws string, at: Point3, size: f64, yaw: f64, stands: bool, staging: Staging = {}): Played throws stringThe posture (Gltf.Posture) at TIME of CLIP, which runs at RATE times its own speed, looping unless ONCE, and the model's placement: PLACE places it standing at AT (SIZE meters tall) at a yaw, YAW the one it is to face; its body's own yaw lags that one while it STANDS (turn in place). PLANTED limbs reach their places in the world (Gltf.Plant). With HOLD, the last posture stands while the clip stays: time runs on, and the pose is not made again. STAGING lays a clip over its upper body and holds a joint in place (Staging).
Effects: fail, read, write, console, io, alloc.
Animator.done
done(): bool throws stringWhether a clip played once has reached its end.
Effects: fail, read, alloc.
Cast.passing
passing(at: Point3, size: f64, yaw: f64, clip: string, stride: f64, worn: Vec<string> | undefined): bool throws stringDraws a passer-by standing on the ground at AT, SIZE meters tall, facing YAW, STRIDE seconds into CLIP (which loops), wearing WORN (none: all): whether it was drawn (none past MOST a frame). It has no turn, blend, reach or look of its own: it wears the pose its clip has at the nearest 30th of a second, which every passer-by on that frame shares (Unreal's animation sharing), so a crowd costs a clip's frames, not a pose each.
Effects: fail, read, write, console, io, alloc.
Cast.begin
begin(): voidA new frame: no character is drawn yet.
Effects: fail, read, write.
Cast.rested
rested(joint: string, at: Point3, size: f64, yaw: f64): Point3 throws stringWhere JOINT is at rest on a character standing at AT, SIZE meters tall, facing YAW: the rig's own measures in the world (an arm's length, where the shoulder is above the hips).
Effects: fail, read, write, alloc.
Cast.stand
stand(slot: i64, at: Point3, size: f64, yaw: f64, clip: string, rate: f64, time: f64, once: bool = false, lying: bool = false, pitch: f64 = 0.0, roll: f64 = 0.0, planted: Vec<Gltf.Plant> = [], far: f64 = 0.0, face: Map<string, f64> = new Map<string, f64>(), worn: Vec<string> | undefined = undefined, staging: Staging = {}): Played | undefined throws stringDraws character SLOT standing on the ground at AT, SIZE meters tall, facing YAW (tipped PITCH and ROLL), its feet PLANTED, playing CLIP at RATE (once when ONCE) at TIME; a LYING one is SIZE meters long. Past MOST characters in a frame, the rest are not drawn: a larger crowd shows the first MOST. A character FAR body heights from the eye renews its pose 30 times a second past 8 body heights, each slot at its own phase (so a crowd costs each frame alike): a pose is most of what a character costs the CPU. FACE weighs its face's expressions by name (a talking character's mouth: Gltf.Rig.faces); a face that can close its eyes blinks as well (lids). PLANTED limbs reach their places in the world (Gltf.Plant). It WORN names the garments it wears (none: all; gizlang/meshdraw dressed). STAGING lays a clip over its upper body and holds a joint in place (Staging). Its pose and place as drawn, none past MOST.
Effects: fail, read, write, console, io, alloc.