File size: 25,352 Bytes
7207c65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// RigLM brain: runs the two trained networks (RigNet v2 + MotionGPT v2) in plain JS,
// plus the scene runtime (paths, ground contact, forward kinematics). No three.js here.
(function (root) {
"use strict";
const S = root.RigSculpt;
const FAMS = ["biped", "quadruped", "bird", "fish", "snake", "insect"];
const NJ = { biped: 17, quadruped: 19, bird: 17, fish: 8, snake: 10, insect: 21 };
const OFF = {}; { let o = 0; for (const f of FAMS) { OFF[f] = [o, o + NJ[f]]; o += NJ[f]; } }
const TOT = 92, K = 256, CLIP = 64, FPS = 15;
const ACTIONS = { biped: ["idle", "walk", "run", "wave", "jump", "dance"], quadruped: ["idle", "walk", "run", "jump", "sit", "eat"], bird: ["idle", "hop", "peck", "fly", "glide"], fish: ["idle", "swim", "dart"], snake: ["idle", "slither"], insect: ["idle", "crawl", "run"] };
const PROMPTS = []; for (const f of FAMS) for (const a of ACTIONS[f]) PROMPTS.push(f + ":" + a);
const VOCAB = K * FAMS.length + PROMPTS.length;

// ------------------------------------------------------------------ weights
function f16(h) { const s = (h & 0x8000) >> 15, e = (h & 0x7C00) >> 10, f = h & 0x03FF; if (e === 0) return (s ? -1 : 1) * Math.pow(2, -14) * (f / 1024); if (e === 31) return f ? NaN : (s ? -1 : 1) * Infinity; return (s ? -1 : 1) * Math.pow(2, e - 15) * (1 + f / 1024); }
function decodeArr(o) {
  const bin = typeof atob === "function" ? atob(o.b64) : Buffer.from(o.b64, "base64").toString("binary");
  const n = bin.length / 2, out = new Float32Array(n);
  for (let i = 0; i < n; i++) out[i] = f16(bin.charCodeAt(2 * i) | (bin.charCodeAt(2 * i + 1) << 8));
  out.shape = o.shape; return out;
}
let W = null;
function loadWeights(json) {
  W = { rig: {}, gpt: {}, tok: {} };
  for (const g of ["rig", "gpt", "tok"]) for (const k in json[g]) W[g][k] = decodeArr(json[g][k]);
  W.cfg = json.cfg; return W;
}

// ------------------------------------------------------------------ tiny linear algebra
function linear(X, n, i, Wm, b, o, act) { // X: n x i, Wm: i x o
  const Y = new Float32Array(n * o);
  for (let r = 0; r < n; r++) {
    const yo = r * o, xo = r * i;
    for (let c = 0; c < o; c++) Y[yo + c] = b ? b[c] : 0;
    for (let k = 0; k < i; k++) { const x = X[xo + k]; if (x === 0) continue; const wo = k * o; for (let c = 0; c < o; c++) Y[yo + c] += x * Wm[wo + c]; }
    if (act === "relu") for (let c = 0; c < o; c++) if (Y[yo + c] < 0) Y[yo + c] = 0;
  }
  return Y;
}
function linearCols(X, n, i, Wm, b, o, c0, c1) { // only output columns [c0,c1)
  const w = c1 - c0, Y = new Float32Array(n * w);
  for (let r = 0; r < n; r++) {
    for (let c = 0; c < w; c++) Y[r * w + c] = b[c0 + c];
    for (let k = 0; k < i; k++) { const x = X[r * i + k]; if (x === 0) continue; const wo = k * o + c0; for (let c = 0; c < w; c++) Y[r * w + c] += x * Wm[wo + c]; }
  }
  return Y;
}

// ------------------------------------------------------------------ RigNet v2
function rigTrunk(P, n, g) {
  const R = W.rig;
  const f1 = linear(P, n, 3, R.l1w, R.l1b, 64, "relu");
  const f2 = linear(f1, n, 64, R.l2w, R.l2b, 128, "relu");
  let gl = g;
  if (!gl) {
    const f3 = linear(f2, n, 128, R.l3w, R.l3b, 256, "relu");
    gl = new Float32Array(256).fill(-1e9);
    for (let r = 0; r < n; r++) for (let c = 0; c < 256; c++) if (f3[r * 256 + c] > gl[c]) gl[c] = f3[r * 256 + c];
  }
  const gb = linear(gl, 1, 256, R.gbw, R.gbb, 256);
  const ga = linear(f2, n, 128, R.gaw, R.gab, 256);
  for (let r = 0; r < n; r++) for (let c = 0; c < 256; c++) { const v = ga[r * 256 + c] + gb[c]; ga[r * 256 + c] = v > 0 ? v : 0; }
  const h = linear(ga, n, 256, R.l4w, R.l4b, 128, "relu");
  return { g: gl, h };
}
function softmaxRows(A, n, m) { for (let r = 0; r < n; r++) { let mx = -1e9; for (let c = 0; c < m; c++) mx = Math.max(mx, A[r * m + c]); let s = 0; for (let c = 0; c < m; c++) { A[r * m + c] = Math.exp(A[r * m + c] - mx); s += A[r * m + c]; } for (let c = 0; c < m; c++) A[r * m + c] /= s; } return A; }

// Learned rig for a mesh: returns family guess, joints (world), per-vertex weights (sparse top-4)
function learnedRig(verts, faces, seed) {
  const rng = S.RNG(seed || 1);
  const pts = S.samplePoints({ verts, faces }, 512, rng);
  const { c, s } = S.normalizeFrame(pts);
  const P = new Float32Array(512 * 3);
  pts.forEach((p, i) => { for (let d = 0; d < 3; d++) P[i * 3 + d] = (p[d] - c[d]) / s; });
  const R = W.rig;
  const { g, h } = rigTrunk(P, 512, null);
  const logits = linear(g, 1, 256, R.clsw, R.clsb, 6);
  const probs = softmaxRows(Float32Array.from(logits), 1, 6);
  let fi = 0; for (let i = 1; i < 6; i++) if (probs[i] > probs[fi]) fi = i;
  const fam = FAMS[fi], [a, b] = OFF[fam], nj = b - a;
  const att = linearCols(h, 512, 128, R.haw, R.hab, TOT, a, b);
  const off = linearCols(h, 512, 128, R.how, R.hob, TOT * 3, a * 3, b * 3);
  // softmax over points for each joint
  const J = [];
  for (let j = 0; j < nj; j++) {
    let mx = -1e9; for (let r = 0; r < 512; r++) mx = Math.max(mx, att[r * nj + j]);
    let sw = 0; const acc = [0, 0, 0];
    for (let r = 0; r < 512; r++) { const w = Math.exp(att[r * nj + j] - mx); sw += w; for (let d = 0; d < 3; d++) acc[d] += w * (P[r * 3 + d] + off[r * nj * 3 + j * 3 + d]); }
    J.push(acc.map((v, d) => v / sw * s + c[d]));
  }
  // skin weights on (a subset of) vertices, then nearest-neighbour fill
  const nv = verts.length, step = Math.max(1, Math.floor(nv / 1200));
  const sub = []; for (let i = 0; i < nv; i += step) sub.push(i);
  const Pv = new Float32Array(sub.length * 3);
  sub.forEach((vi, k) => { for (let d = 0; d < 3; d++) Pv[k * 3 + d] = (verts[vi][d] - c[d]) / s; });
  const tv = rigTrunk(Pv, sub.length, g);
  const sk = softmaxRows(linearCols(tv.h, sub.length, 128, R.hsw, R.hsb, TOT, a, b), sub.length, nj);
  const subW = sub.map((_, k) => Array.from(sk.subarray(k * nj, k * nj + nj)));
  const Wv = new Array(nv);
  if (step === 1) sub.forEach((vi, k) => Wv[vi] = subW[k]);
  else {
    const cell = s / 40, grid = new Map(), key = (p) => Math.floor(p[0] / cell) + "," + Math.floor(p[1] / cell) + "," + Math.floor(p[2] / cell);
    sub.forEach((vi, k) => { const kk = key(verts[vi]); if (!grid.has(kk)) grid.set(kk, []); grid.get(kk).push(k); });
    for (let v = 0; v < nv; v++) {
      const p = verts[v], cx = Math.floor(p[0] / cell), cy = Math.floor(p[1] / cell), cz = Math.floor(p[2] / cell);
      let best = -1, bd = 1e9;
      for (let rad = 1; rad < 6 && best < 0; rad++) for (let x = -rad; x <= rad; x++) for (let y = -rad; y <= rad; y++) for (let z = -rad; z <= rad; z++) {
        const L = grid.get((cx + x) + "," + (cy + y) + "," + (cz + z)); if (!L) continue;
        for (const k of L) { const q = verts[sub[k]], d = (q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2 + (q[2] - p[2]) ** 2; if (d < bd) { bd = d; best = k; } }
      }
      Wv[v] = subW[Math.max(0, best)];
    }
  }
  return { family: fam, probs: Array.from(probs), joints: J, weights: Wv.map(w => sparsify(w)) };
}
function sparsify(w, k = 4) {
  const idx = w.map((v, i) => [v, i]).sort((x, y) => y[0] - x[0]).slice(0, k).filter(e => e[0] > 0.02);
  const s = idx.reduce((a, e) => a + e[0], 0) || 1;
  return idx.map(e => [e[1], e[0] / s]);
}
function generatorRig(cr) {
  const { s } = S.normalizeFrame(cr.verts);
  const Wd = S.gtWeights(cr.family, cr.joints, cr.ext, cr.verts, s);
  return { family: cr.family, joints: cr.joints, weights: Wd.map(w => sparsify(w)) };
}
function compareRigs(learned, gen, verts) {
  if (learned.family !== gen.family) return { familyOk: false, jointErr: null, skinAgree: null };
  const { s } = S.normalizeFrame(verts);
  let e = 0; gen.joints.forEach((j, i) => e += S.V.len(S.V.sub(j, learned.joints[i])));
  let agree = 0; for (let v = 0; v < verts.length; v++) if (learned.weights[v][0] && gen.weights[v][0] && learned.weights[v][0][0] === gen.weights[v][0][0]) agree++;
  return { familyOk: true, jointErr: e / gen.joints.length / s, skinAgree: agree / verts.length };
}

// ------------------------------------------------------------------ MotionGPT v2 (with KV cache)
function gelu(x) { return 0.5 * x * (1 + Math.tanh(0.7978845608 * (x + 0.044715 * x * x * x))); }
function layernorm(x, w, b) { const n = x.length; let m = 0; for (let i = 0; i < n; i++) m += x[i]; m /= n; let v = 0; for (let i = 0; i < n; i++) v += (x[i] - m) ** 2; v /= n; const inv = 1 / Math.sqrt(v + 1e-5); const y = new Float32Array(n); for (let i = 0; i < n; i++) y[i] = (x[i] - m) * inv * w[i] + b[i]; return y; }
class GPTRun {
  constructor() { const c = W.cfg.gpt; this.d = c.d; this.h = c.h; this.L = c.layers; this.cache = []; for (let l = 0; l < this.L; l++) this.cache.push({ k: [], v: [] }); this.t = 0; }
  step(tok) { // feed one token, return logits for next
    const G = W.gpt, d = this.d, H = this.h, dh = d / H, pos = this.t++;
    let x = new Float32Array(d); for (let i = 0; i < d; i++) x[i] = G.tok[tok * d + i] + G.pos[pos * d + i];
    for (let l = 0; l < this.L; l++) {
      const p = "b" + l + "_";
      const a = layernorm(x, G[p + "l1w"], G[p + "l1b"]);
      const q = linear(a, 1, d, G[p + "qw"], G[p + "qb"], d), k = linear(a, 1, d, G[p + "kw"], G[p + "kb"], d), v = linear(a, 1, d, G[p + "vw"], G[p + "vb"], d);
      const C = this.cache[l]; C.k.push(k); C.v.push(v);
      const y = new Float32Array(d), T = C.k.length, sc = 1 / Math.sqrt(dh);
      for (let hh = 0; hh < H; hh++) {
        const att = new Float32Array(T); let mx = -1e9;
        for (let t = 0; t < T; t++) { let s = 0; for (let i = 0; i < dh; i++) s += q[hh * dh + i] * C.k[t][hh * dh + i]; att[t] = s * sc; mx = Math.max(mx, att[t]); }
        let ss = 0; for (let t = 0; t < T; t++) { att[t] = Math.exp(att[t] - mx); ss += att[t]; }
        for (let t = 0; t < T; t++) { const w = att[t] / ss; for (let i = 0; i < dh; i++) y[hh * dh + i] += w * C.v[t][hh * dh + i]; }
      }
      const o = linear(y, 1, d, G[p + "ow"], G[p + "ob"], d);
      for (let i = 0; i < d; i++) x[i] += o[i];
      const m = layernorm(x, G[p + "l2w"], G[p + "l2b"]);
      const f1 = linear(m, 1, d, G[p + "f1w"], G[p + "f1b"], 4 * d); for (let i = 0; i < 4 * d; i++) f1[i] = gelu(f1[i]);
      const f2 = linear(f1, 1, 4 * d, G[p + "f2w"], G[p + "f2b"], d);
      for (let i = 0; i < d; i++) x[i] += f2[i];
    }
    return linear(layernorm(x, G.lnw, G.lnb), 1, d, G.headw, G.headb, VOCAB);
  }
}
function sampleTok(logits, lo, hi, rng, temp, topk) {
  const c = []; for (let i = lo; i < hi; i++) c.push([logits[i] / temp, i]);
  c.sort((a, b) => b[0] - a[0]); const top = c.slice(0, topk); const mx = top[0][0];
  let s = 0; const p = top.map(e => { const w = Math.exp(e[0] - mx); s += w; return w; });
  let r = rng() * s; for (let i = 0; i < top.length; i++) { r -= p[i]; if (r <= 0) return top[i][1]; } return top[top.length - 1][1];
}
// Write n pose tokens for "family:action" (continues across 64-token windows)
function writeMotion(fam, action, n, rng, opts = {}) {
  const pid = K * FAMS.length + PROMPTS.indexOf(fam + ":" + action);
  if (pid < K * FAMS.length) throw new Error("unknown prompt " + fam + ":" + action);
  const fi = FAMS.indexOf(fam), lo = fi * K, hi = lo + K;
  const out = [];
  while (out.length < n) {
    const run = new GPTRun();
    const ctx = out.slice(-31);
    let logits = run.step(pid);
    for (const t of ctx) logits = run.step(t);
    const room = CLIP - 1 - ctx.length;
    for (let i = 0; i < room && out.length < n; i++) { const t = sampleTok(logits, lo, hi, rng, opts.temp || 0.8, opts.topk || 8); out.push(t); logits = run.step(t); }
  }
  return out;
}
function decodeTokens(fam, toks) {
  const C = W.tok[fam + "_C"], mu = W.tok[fam + "_mu"], sd = W.tok[fam + "_sd"], D = mu.length;
  return toks.map(t => { const f = new Float32Array(D); for (let i = 0; i < D; i++) f[i] = C[(t - FAMS.indexOf(fam) * K) * D + i] * sd[i] + mu[i]; return f; });
}
// 15 fps tokens -> 30 fps smooth frames
function upsample(frames, nj) {
  const D = frames[0].length, n = frames.length, m = (n - 1) * 2 + 1, Y = [];
  for (let i = 0; i < m; i++) { const t = i / 2, a = Math.floor(t), b = Math.min(n - 1, a + 1), u = t - a; const f = new Float32Array(D); for (let d = 0; d < D; d++) f[d] = frames[a][d] * (1 - u) + frames[b][d] * u; Y.push(f); }
  const k = [-4, -3, -2, -1, 0, 1, 2, 3, 4].map(x => Math.exp(-0.5 * (x / 1.6) ** 2)); const ks = k.reduce((a, b) => a + b, 0);
  const Z = Y.map((_, i) => { const f = new Float32Array(D); for (let j = -4; j <= 4; j++) { const q = Y[Math.min(m - 1, Math.max(0, i + j))]; for (let d = 0; d < D; d++) f[d] += q[d] * k[j + 4] / ks; } return f; });
  for (const f of Z) { f[nj * 3 + 2] *= 0.5; f[nj * 3 + 3] *= 0.5; }
  return Z;
}

// ------------------------------------------------------------------ kinematics
function euler(e0, e1, e2) { return S.mm(S.rotY(e1), S.mm(S.rotX(e0), S.rotZ(e2))); }
function dirRot(a, b) {
  a = S.V.norm(a); b = S.V.norm(b); const v = [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]], c = S.V.dot(a, b);
  if (S.V.len(v) < 1e-8) return [1, 0, 0, 0, 1, 0, 0, 0, 1];
  const vx = [0, -v[2], v[1], v[2], 0, -v[0], -v[1], v[0], 0], vx2 = S.mm(vx, vx), f = 1 / (1 + c);
  return [1, 0, 0, 0, 1, 0, 0, 0, 1].map((x, i) => x + vx[i] + vx2[i] * f);
}
function corrections(fam, J) {
  const par = S.FAMILIES[fam].parent, canon = S.FAMILIES[fam].canon, out = J.map(() => [1, 0, 0, 0, 1, 0, 0, 0, 1]);
  for (const k in canon) { const kid = par.indexOf(+k); if (kid >= 0) out[k] = dirRot(S.V.sub(J[kid], J[k]), canon[k]); }
  return out;
}
// local rotations (model space, root yaw excluded) + world joint transforms
function pose(fam, J, corr, f) {
  const par = S.FAMILIES[fam].parent, n = J.length, L = [], Rw = [], pw = [];
  for (let i = 0; i < n; i++) {
    L[i] = S.mm(euler(f[i * 3], f[i * 3 + 1], f[i * 3 + 2]), corr[i]);
    if (par[i] < 0) { Rw[i] = L[i]; pw[i] = J[i].slice(); }
    else { Rw[i] = S.mm(Rw[par[i]], L[i]); pw[i] = S.V.add(pw[par[i]], S.mv(Rw[par[i]], S.V.sub(J[i], J[par[i]]))); }
  }
  return { L, Rw, pw };
}

// ------------------------------------------------------------------ scene runtime
const SECONDS_DEFAULT = 6;
const MOTION_CACHE = new Map(); // same body plan + action share up to 3 written takes
class Actor {
  constructor(spec, cr, rig, idx, rng, world) {
    this.spec = spec; this.cr = cr; this.rig = rig; this.idx = idx; this.rng = rng; this.world = world;
    this.fam = rig.family; this.J = rig.joints; this.corr = corrections(this.fam, this.J);
    this.unit = cr.unit; this.feet = S.FAMILIES[this.fam].feet;
    this.ground = this.feet.length ? Math.min(...this.feet.map(i => this.J[i][1])) : 0;
    this.pos = spec._start.slice(); this.yaw = spec._yaw; this.name = spec._name;
    const plan = (spec.script && spec.script.length ? spec.script : [{ action: ACTIONS[this.fam].includes("walk") ? "walk" : ACTIONS[this.fam][1], seconds: SECONDS_DEFAULT, path: "wander" }]);
    this.plan = plan.map(p => Object.assign({}, p, { action: fixAction(this.fam, p.action), seconds: Math.max(1, Math.min(30, p.seconds || SECONDS_DEFAULT)) }));
    this.frames = []; this.segOf = [];
    const nj = this.J.length;
    for (let si = 0; si < this.plan.length; si++) {
      const p = this.plan[si];
      const nt = Math.ceil(p.seconds * FPS) + 2;
      const ck = this.fam + ":" + p.action + ":" + nt + ":" + (spec._variant || 0);
      if (!MOTION_CACHE.has(ck)) MOTION_CACHE.set(ck, writeMotion(this.fam, p.action, nt, rng));
      const toks = MOTION_CACHE.get(ck);
      const fr = upsample(decodeTokens(this.fam, toks), nj).slice(0, Math.round(p.seconds * 30));
      // crossfade 12 frames from previous segment
      const prev = this.frames.length ? this.frames[this.frames.length - 1] : null;
      fr.forEach((f, i) => { if (prev && i < 12) { const u = (i + 1) / 13; for (let d = 0; d < f.length; d++) f[d] = prev[d] * (1 - u) + f[d] * u; } this.frames.push(f); this.segOf.push(si); });
      if (si === 0) this.tokens = toks;
    }
    this.n = this.frames.length;
    this.alt = spec.altitude !== undefined ? spec.altitude : (this.fam === "fish" ? world.waterDepth * 0.5 : 0);
    this.state = { pos: this.pos.slice(), yaw: this.yaw, wp: null };
  }
  reset() { this.state = { pos: this.pos.slice(), yaw: this.yaw, wp: null }; }
  // advance one 30fps frame at global frame index t; returns transforms
  tick(t, actors) {
    const nj = this.J.length, fi = t % this.n, f = this.frames[fi], seg = this.plan[this.segOf[fi]], st = this.state;
    if (fi === 0 && t > 0 && this.spec.loop === false) { /* hold */ }
    let speed = f[nj * 3 + 2] * this.unit * 30; // world units / second
    const path = seg.path || "wander", dt = 1 / 30, maxTurn = (this.fam === "bird" ? 1.2 : 2.0) * dt;
    let desired = null;
    if (path === "stay") speed = 0;
    else if (path === "circle") { const c = seg.center || this.spec._center, R = seg.radius || 4; const d = [st.pos[0] - c[0], st.pos[1] - c[1]]; const a = Math.atan2(d[1], d[0]); const tgt = [c[0] + Math.cos(a + 0.35) * R, c[1] + Math.sin(a + 0.35) * R]; desired = Math.atan2(tgt[0] - st.pos[0], tgt[1] - st.pos[1]); }
    else if (path === "toward" && seg.target) { const dx = seg.target[0] - st.pos[0], dz = seg.target[1] - st.pos[1]; if (Math.hypot(dx, dz) < 0.6) speed = 0; else desired = Math.atan2(dx, dz); }
    else if (path === "follow" || path === "flee") {
      const o = actors.find(a => a.name === seg.follow) || actors.find(a => a !== this);
      if (o) { const dx = o.state.pos[0] - st.pos[0], dz = o.state.pos[1] - st.pos[1]; desired = Math.atan2(dx, dz) + (path === "flee" ? Math.PI : 0); if (path === "follow" && Math.hypot(dx, dz) < (seg.gap || 1.5)) speed *= 0.2; }
    } else { // wander inside the area
      const c = this.spec._center, R = this.spec._radius;
      if (!st.wp || Math.hypot(st.wp[0] - st.pos[0], st.wp[1] - st.pos[1]) < 1) { const a = this.rng() * 6.283, r = Math.sqrt(this.rng()) * R; st.wp = [c[0] + Math.cos(a) * r, c[1] + Math.sin(a) * r]; }
      desired = Math.atan2(st.wp[0] - st.pos[0], st.wp[1] - st.pos[1]);
    }
    if (desired !== null && speed > 0.01) { let d = desired - st.yaw; while (d > Math.PI) d -= 6.283; while (d < -Math.PI) d += 6.283; st.yaw += Math.max(-maxTurn, Math.min(maxTurn, d)); }
    st.yaw += f[nj * 3 + 3];
    // keep inside world
    const lim = this.world.size * 0.48; st.pos[0] = Math.max(-lim, Math.min(lim, st.pos[0])); st.pos[1] = Math.max(-lim, Math.min(lim, st.pos[1]));
    st.pos[0] += Math.sin(st.yaw) * speed * dt; st.pos[1] += Math.cos(st.yaw) * speed * dt;
    // soft separation from others
    for (const o of actors) if (o !== this && o.fam !== "bird") { const dx = st.pos[0] - o.state.pos[0], dz = st.pos[1] - o.state.pos[1], d = Math.hypot(dx, dz), m = (this.cr.radius + o.cr.radius) * 0.8; if (d > 1e-3 && d < m) { st.pos[0] += dx / d * (m - d) * 0.3; st.pos[1] += dz / d * (m - d) * 0.3; } }
    const P = pose(this.fam, this.J, this.corr, f);
    let dy = f[nj * 3 + 1] * this.unit;
    if (this.feet.length) dy = Math.max(dy, this.ground - Math.min(...this.feet.map(i => P.pw[i][1])));
    dy += this.alt;
    const sway = f[nj * 3] * this.unit;
    return { L: P.L, Rw: P.Rw, pw: P.pw, root: [st.pos[0] + Math.cos(st.yaw) * sway, dy, st.pos[1] - Math.sin(st.yaw) * sway], yaw: st.yaw, action: seg.action };
  }
}
function fixAction(fam, a) {
  a = (a || "").toLowerCase();
  const A = ACTIONS[fam]; if (A.includes(a)) return a;
  const alias = { walk: { bird: "hop", fish: "swim", snake: "slither", insect: "crawl" }, run: { bird: "fly", fish: "dart", snake: "slither" }, fly: { biped: "jump", quadruped: "run", fish: "swim", snake: "slither", insect: "run" }, swim: { biped: "walk", quadruped: "walk", bird: "glide", snake: "slither", insect: "crawl" }, crawl: { biped: "walk", quadruped: "walk", bird: "hop", fish: "swim", snake: "slither" }, slither: { biped: "walk", quadruped: "walk", bird: "hop", fish: "swim", insect: "crawl" }, eat: { bird: "peck", biped: "idle", fish: "idle", snake: "idle", insect: "idle" }, sit: { biped: "idle", bird: "idle" }, dance: { quadruped: "jump", bird: "hop", fish: "swim", snake: "slither", insect: "run" }, wave: { quadruped: "idle", bird: "idle" }, jump: { bird: "hop", fish: "dart", snake: "slither", insect: "run" } };
  if (alias[a] && alias[a][fam]) return alias[a][fam];
  if (["chase", "hunt", "flee", "gallop", "sprint"].includes(a)) return A.includes("run") ? "run" : A[A.length - 1];
  if (["stroll", "roam", "wander", "patrol", "graze"].includes(a)) return fam === "quadruped" && a === "graze" ? "eat" : (A.includes("walk") ? "walk" : A[1]);
  if (["sleep", "rest", "stand", "look", "watch"].includes(a)) return fam === "quadruped" ? "sit" : "idle";
  if (["play", "celebrate", "party"].includes(a)) return A.includes("dance") ? "dance" : A.includes("jump") ? "jump" : A[1];
  return A.includes("walk") ? "walk" : A[1];
}

// Lay out a scene script into concrete specs (positions, names) — deterministic per seed
const hashS = (x) => S.hashStr(String(x)) % 100000;
function layout(script, seed) {
  const rng = S.RNG(seed || 7), size = Math.max(20, Math.min(120, (script.environment && script.environment.size) || 40));
  const objs = [], crs = [];
  const area = (a) => ({ c: a && a.x !== undefined ? [a.x, a.z] : [0, 0], r: a && a.r ? a.r : size * 0.4 });
  for (const o of script.objects || []) {
    const n = Math.min(60, o.count || 1), A = area(o.area);
    for (let i = 0; i < n; i++) { const a = rng() * 6.283, r = n === 1 && o.area && !o.area.r ? 0 : Math.sqrt(rng()) * A.r; objs.push({ type: o.type, size: o.size || 1, scale: 0.85 + 0.3 * rng(), color: o.color, colors: o.colors, pos: [A.c[0] + Math.cos(a) * r, A.c[1] + Math.sin(a) * r], yaw: o.yaw !== undefined ? o.yaw : rng() * 6.283, seed: (seed * 31 + (i % 2) * 7 + hashS(o.type)) | 0 }); }
  }
  let k = 0;
  for (const c of script.creatures || []) {
    const n = Math.min(30, c.count || 1), A = area(c.area || { r: size * 0.25 });
    for (let i = 0; i < n; i++) {
      const a = rng() * 6.283, r = Math.sqrt(rng()) * Math.min(A.r, 2 + n);
      crs.push(Object.assign({}, c, { _start: [A.c[0] + Math.cos(a) * r, A.c[1] + Math.sin(a) * r], _yaw: c.facing !== undefined ? c.facing : rng() * 6.283, _center: A.c, _radius: A.r, _name: (c.name || c.species || c.family) + (n > 1 ? "#" + (i + 1) : ""), _variant: i % 2, _group: k - i, _seed: (seed * 131 + k * 17) | 0 }));
      k++;
    }
  }
  return { size, objs, crs };
}

// Build everything that doesn't need three.js. progress(msg) is called between steps (may be async).
async function buildScene(script, opts = {}) {
  const seed = opts.seed || 7, L = layout(script, seed), env = script.environment || {};
  MOTION_CACHE.clear();
  const world = { size: L.size, waterDepth: env.ground === "water" ? 3 : 0 };
  const objects = [], actors = [], stats = [], ocache = new Map(), ccache = new Map();
  const yieldUI = opts.yield || (async () => {});
  for (const o of L.objs) {
    if (!root.RigObjects.OBJECTS[o.type]) continue;
    const key = JSON.stringify([o.type, o.size, o.color, o.colors, o.seed]);
    if (!ocache.has(key)) { ocache.set(key, root.RigObjects.makeObject({ type: o.type, size: o.size, color: o.color, colors: o.colors, seed: o.seed })); await yieldUI(`sculpting ${o.type}`); }
    objects.push(Object.assign({}, ocache.get(key), { pos: o.pos, yaw: o.yaw, scale: o.scale, key }));
  }
  for (const c of L.crs) {
    const sp = c.species && root.RigSpecies.SPECIES[c.species];
    const fam = (sp && sp.family) || c.family;
    if (!fam || !S.FAMILIES[fam]) continue;
    // members of one group share up to 3 sculpted variants (and their rigs)
    const key = JSON.stringify([fam, c.species, c.genome, c.colors, c.pattern, c.size, c._variant, c._group]);
    if (!ccache.has(key)) {
      const cr = S.makeCreature({ family: fam, species: sp ? c.species : undefined, genome: c.genome, colors: c.colors, pattern: c.pattern, size: c.size, seed: (seed * 7 + hashS(key)) | 0 });
      let lo = [1e9, 1e9], hi = [-1e9, -1e9]; for (const v of cr.verts) { lo[0] = Math.min(lo[0], v[0]); hi[0] = Math.max(hi[0], v[0]); lo[1] = Math.min(lo[1], v[2]); hi[1] = Math.max(hi[1], v[2]); }
      cr.radius = Math.max(hi[0] - lo[0], hi[1] - lo[1]) * 0.5;
      await yieldUI(`sculpting ${c._name}`);
      const gen = generatorRig(cr);
      const learned = learnedRig(cr.verts, cr.faces, c._seed);
      ccache.set(key, { cr, gen, learned, cmp: compareRigs(learned, gen, cr.verts) });
      await yieldUI(`rigging ${c._name}`);
    }
    const { cr, gen, learned, cmp } = ccache.get(key);
    stats.push(Object.assign({ name: c._name, family: fam, guessed: learned.family, probs: learned.probs }, cmp));
    const useLearned = opts.rig !== "generator" && cmp.familyOk;
    const rng = S.RNG(c._seed + 5);
    const actor = new Actor(c, cr, useLearned ? learned : gen, actors.length, rng, world);
    actor.learned = learned; actor.gen = gen; actor.rigSource = useLearned ? "learned" : "generator";
    actors.push(actor); await yieldUI(`animating ${c._name}`);
  }
  return { env, world, objects, actors, stats, length: Math.max(1, ...actors.map(a => a.n)) };
}

root.RigBrain = { GPTRun, FAMS, ACTIONS, PROMPTS, loadWeights, learnedRig, generatorRig, compareRigs, writeMotion, decodeTokens, upsample, pose, corrections, buildScene, layout, fixAction, Actor, get W() { return W; } };
})(typeof globalThis !== "undefined" ? globalThis : this);