nx_nxa_skin_authored_t36.nx source
↩ module page · 1048 lines · 43802 B
1// nx_nxa_skin.nx -- RIG IMPORTER: fills the NXA's SKEL + SKIN sections from a binary FBX.
2// Walks the FBX id-graph (Model/LimbNode joints, Deformer Clusters with Indexes/Weights/
3// TransformLink bind matrices, the Connections table) and emits a 5-section NXA:
4// VERT/TRIS/CLUS copied from the mesh import, SKEL = joints [parent][t mm][quat q12=identity]
5// (delta-LBS convention: animation is applied RELATIVE to bind, so identity bind quats are
6// exact), SKIN = per-vertex top-4 joints + q12 weights normalized to sum 4096.
7// usage: nx_nxa_skin <in.fbx> <in.nxa> <out.nxa>
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_zlib_wrap.nx"
11import "nx_nxa.nx"
12import "nx_nxa_fk.nx"
13import "nx_fbx_core.nx"
14// G_USF: FBX GlobalSettings UnitScaleFactor in MICRO units (1000000 = factor 1.0), parsed once
15// in main via fbxc_unit_scale_micro and applied at every LENGTH site (Lcl Translation, the
16// TransformLink bind translations) -- rotations and weights are scale-free. Slot sits in the
17// spare G words past G_CLOV, same convention as the other G_ offsets.
18const G_USF: i64 = 357091
19const G_MAGIC_20000: i64 = 20000
20const G_MAGIC_2047: i64 = 2047
21const G_MAGIC_1048575: i64 = 1048575
22const G_MAGIC_4503599627370496: i64 = 4503599627370496
23const G_MAGIC_4090: i64 = 4090
24const G_MAGIC_99990: i64 = 99990
25const G_MAGIC_4096: i64 = 4096
26const G_MAGIC_7500: i64 = 7500
27const G_MAGIC_500000: i64 = 500000
28const G_MAGIC_4611686018427387903: i64 = 4611686018427387903
29
30// G layout (word offsets): 0=mcount 1=ccount 2=conncount 3=bestVertAlen 4=bestGeomId 5=skincount
31const G_MO: i64 = 16 // model ids x4096
32const G_LO: i64 = 4112 // limb flags x4096
33// CLUSTER TABLES x2048 (2026-08-23): the x512 era saturated SILENTLY on every multi-geometry
34// Fab donor -- all four measured donors reported exactly clusters=510 (the cap, not the count),
35// and every cluster past the cap dropped its authored weights without a word. 16 skins x ~110
36// joints can approach 2000 clusters; 2048 covers that and the count is now honest again.
37// CLUSTER TABLES x8192 (2026-08-23, second raise same day): x2048 ALSO saturated -- three
38// donors again read exactly clusters=2046, the cap wearing the shape of a measurement. A Fab
39// donor's cluster count is skins x joints (30 x 108 = 3240; toon 8 x 370 = 2960), so 8192
40// covers the class with headroom -- AND the walker now COUNTS overflow at G_CLOV and the
41// receipt prints it, so a future saturation announces itself instead of impersonating a count.
42const G_CID: i64 = 8208 // cluster ids x8192
43const G_CIX: i64 = 16400 // cluster Indexes meta (off,alen,enc,clen) x8192x4
44const G_CW: i64 = 49168 // cluster Weights meta x8192x4
45const G_CT: i64 = 81936 // cluster TransformLink meta x8192x4
46const G_CONN: i64 = 114704 // connection pairs (src,dst) x100000x2
47const G_SID: i64 = 314704 // Skin deformer ids x64
48const G_PR: i64 = 316000 // per-model PreRotation millideg x3
49const G_LR: i64 = 328288 // per-model Lcl Rotation default millideg x3
50const G_LT: i64 = 340576 // per-model Lcl Translation default units x3
51const G_PF: i64 = 352864 // per-model flags: 1=pre 2=lclrot 4=lclt
52const G_WORDS: i64 = 358000
53// MULTI-GEOMETRY (2026-08-23): table of every Vertices-array encounter in document order --
54// (geometry id, array len) pairs -- mirroring nx_fbx_import's merge order, so per-geometry
55// vertex bases in the merged in.nxa are prefix sums over this table. Count at G_GT, pairs after.
56// Cap 64 geometries; overflow COUNTED at G_GTOV and printed, never silent (a dropped geometry
57// would silently unskin its part). Table lives inside the G arena tail: 356960+129 < 358000.
58const G_GT: i64 = 356960
59const G_GTCAP: i64 = 64
60const G_GTOV: i64 = 357089
61// cluster-table admission cap: table is x8192, two slots of slack kept exactly as the x512 era
62// kept (512-2=510), so the walker can never write the final pair out of bounds mid-node.
63const G_CLUSTER_CAP: i64 = 8190
64// cluster overflow counter: clusters seen past the cap. Printed on the receipt -- a saturated
65// table must say so, never impersonate a count (this line exists because it happened twice today).
66const G_CLOV: i64 = 357090
67
68func skw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
69func skn(v: i64) -> i64 {
70 let t: *u8 = sys_mmap(32) as *u8
71 var m: i64 = v; var w: i64 = 0
72 if m<0 { t[w]=45 as u8; w=w+1; m=0-m }
73 if m==0 { t[w]=48 as u8; sys_write(1,t,w+1); return 0 }
74 let d: *u8 = sys_mmap(32) as *u8
75 var k: i64=0
76 while m>0 { d[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
77 var j: i64=0
78 while j<k { t[w]=d[k-1-j]; w=w+1; j=j+1 }
79 sys_write(1,t,w); return 0
80}
81func sk_u32(b: *u8, o: i64) -> i64 {
82 return ((b[o] & 0xff) as i64) | (((b[o+1] & 0xff) as i64) << 8)
83 | (((b[o+2] & 0xff) as i64) << 16) | (((b[o+3] & 0xff) as i64) << 24)
84}
85func sk_u64(b: *u8, o: i64) -> i64 { return sk_u32(b, o) | (sk_u32(b, o + 4) << 32) }
86// IEEE754 double -> value x scale (scale=1000 for mm, 4096 for q12 weights). DRY debt vs
87// nx_fbx_measure's copy -- extract to a shared nx_f64.nx when the third user appears.
88func sk_f64(b: *u8,o: i64,scale: i64) -> i64 {
89 // Shift the IEEE mantissa before decimal scaling; mantissa*4096 wraps at weight 1.0.
90 let bits:i64=fbxc_i64le(b,o)
91 let exponent:i64=(bits>>52)&2047
92 if scale<0||scale>G_MAGIC_20000||exponent>=1043{return 0}
93 return fbxc_f64_p20(bits)*scale/FBXC_P20
94}
95
96// recursive walk; ctx = current Cluster index (child arrays attach to it) or -1;
97// gctx = current Geometry node id (Vertices children identify OUR mesh's geometry) or 0
98func sk_walk(b: *u8, flen: i64, off0: i64, big: i64, G: *i64, ctx: i64, gctx: i64, mctx: i64) -> i64 {
99 let off: i64 = off0
100 var endo: i64 = 0
101 var nprops: i64 = 0
102 var plen: i64 = 0
103 var nlen: i64 = 0
104 var p: i64 = 0
105 if big == 1 {
106 endo = sk_u64(b, off)
107 nprops = sk_u64(b, off + 8)
108 plen = sk_u64(b, off + 16)
109 nlen = (b[off + 24] & 0xff) as i64
110 p = off + 25
111 }
112 if big == 0 {
113 endo = sk_u32(b, off)
114 nprops = sk_u32(b, off + 4)
115 plen = sk_u32(b, off + 8)
116 nlen = (b[off + 12] & 0xff) as i64
117 p = off + 13
118 }
119 if endo == 0 { return off }
120 if endo > flen { return flen }
121 var kind: i64 = 0
122 if nlen == 5 { if (b[p]&0xff)==77 { if (b[p+4]&0xff)==108 { kind = 1 } } } // Model
123 if nlen == 8 {
124 if (b[p]&0xff)==68 { if (b[p+7]&0xff)==114 { kind = 2 } } // Deformer
125 if (b[p]&0xff)==71 { if (b[p+7]&0xff)==121 { kind = 7 } } // Geometry
126 if (b[p]&0xff)==86 { if (b[p+7]&0xff)==115 { kind = 8 } } // Vertices
127 }
128 if nlen == 7 {
129 if (b[p]&0xff)==73 { if (b[p+6]&0xff)==115 { kind = 3 } } // Indexes
130 if (b[p]&0xff)==87 { if (b[p+6]&0xff)==115 { kind = 4 } } // Weights
131 }
132 if nlen == 13 { if (b[p]&0xff)==84 { if (b[p+12]&0xff)==107 { kind = 5 } } } // TransformLink
133 if nlen == 1 { if (b[p]&0xff)==67 { kind = 6 } } // C
134 if nlen == 1 { if (b[p]&0xff)==80 { kind = 9 } } // P (Properties70)
135 p = p + nlen
136 let pstart: i64 = p
137 var id1: i64 = 0
138 var id2: i64 = 0
139 var lseen: i64 = 0
140 var s1l: i64 = 0
141 var s1o: i64 = 0
142 var s2l: i64 = 0
143 var s2o: i64 = 0
144 var sseen: i64 = 0
145 var dseen: i64 = 0
146 var dv1: i64 = 0
147 var dv2: i64 = 0
148 var dv3: i64 = 0
149 var arro: i64 = 0
150 var arrl: i64 = 0
151 var arre: i64 = 0
152 var arrc: i64 = 0
153 var pi: i64 = 0
154 var stop: i64 = 0
155 while pi < nprops {
156 if stop == 0 {
157 let t: i64 = (b[p] & 0xff) as i64
158 p = p + 1
159 var adv: i64 = 0 - 1
160 if t == 83 {
161 let l: i64 = sk_u32(b, p)
162 sseen = sseen + 1
163 if sseen == 1 { s1o = p + 4; s1l = l }
164 if sseen == 2 { s2o = p + 4; s2l = l }
165 adv = 4 + l
166 }
167 if t == 82 { adv = 4 + sk_u32(b, p) }
168 if t == 89 { adv = 2 }
169 if t == 67 { adv = 1 }
170 if t == 73 { adv = 4 }
171 if t == 70 { adv = 4 }
172 if t == 68 {
173 if dseen == 0 { dv1 = p }
174 if dseen == 1 { dv2 = p }
175 if dseen == 2 { dv3 = p }
176 dseen = dseen + 1
177 adv = 8
178 }
179 if t == 76 {
180 lseen = lseen + 1
181 if lseen == 1 { id1 = sk_u64(b, p) }
182 if lseen == 2 { id2 = sk_u64(b, p) }
183 adv = 8
184 }
185 var isarr: i64 = 0
186 if t == 100 { isarr = 1 }
187 if t == 102 { isarr = 1 }
188 if t == 108 { isarr = 1 }
189 if t == 105 { isarr = 1 }
190 if t == 98 { isarr = 1 }
191 if isarr == 1 {
192 arrl = sk_u32(b, p)
193 arre = sk_u32(b, p + 4)
194 arrc = sk_u32(b, p + 8)
195 arro = p + 12
196 adv = 12 + arrc
197 }
198 if adv < 0 { stop = 1 }
199 if adv >= 0 { p = p + adv }
200 }
201 pi = pi + 1
202 }
203 var childctx: i64 = ctx
204 var childg: i64 = gctx
205 var childm: i64 = mctx
206 if kind == 1 { if G[0] < G_MAGIC_4090 {
207 G[G_MO + G[0]] = id1
208 var lf: i64 = 0
209 if s2l == 8 { if (b[s2o]&0xff)==76 { if (b[s2o+7]&0xff)==101 { lf = 1 } } }
210 G[G_LO + G[0]] = lf
211 childm = G[0]
212 G[0] = G[0] + 1
213 } }
214 if kind == 2 { if s2l == 7 {
215 var ctf: i64 = 0
216 if G[1] < G_CLUSTER_CAP { ctf = 1 }
217 if ctf == 1 {
218 G[G_CID + G[1]] = id1
219 childctx = G[1]
220 G[1] = G[1] + 1
221 }
222 if ctf == 0 { G[G_CLOV] = G[G_CLOV] + 1 }
223 } }
224 if kind == 2 { if s2l == 4 { if G[5] < 60 {
225 G[G_SID + G[5]] = id1
226 G[5] = G[5] + 1
227 } } }
228 if kind == 7 { childg = id1 }
229 if kind == 8 {
230 if arrl > G[3] { G[3] = arrl; G[4] = gctx }
231 var gtf: i64 = 0
232 if G[G_GT] < G_GTCAP { gtf = 1 }
233 if gtf == 1 {
234 G[G_GT + 1 + G[G_GT]*2] = gctx
235 G[G_GT + 2 + G[G_GT]*2] = arrl
236 G[G_GT] = G[G_GT] + 1
237 }
238 if gtf == 0 { G[G_GTOV] = G[G_GTOV] + 1 }
239 }
240 if kind == 3 { if ctx >= 0 {
241 G[G_CIX + ctx*4] = arro
242 G[G_CIX + ctx*4 + 1] = arrl
243 G[G_CIX + ctx*4 + 2] = arre
244 G[G_CIX + ctx*4 + 3] = arrc
245 } }
246 if kind == 4 { if ctx >= 0 {
247 G[G_CW + ctx*4] = arro
248 G[G_CW + ctx*4 + 1] = arrl
249 G[G_CW + ctx*4 + 2] = arre
250 G[G_CW + ctx*4 + 3] = arrc
251 } }
252 if kind == 5 { if ctx >= 0 {
253 G[G_CT + ctx*4] = arro
254 G[G_CT + ctx*4 + 1] = arrl
255 G[G_CT + ctx*4 + 2] = arre
256 G[G_CT + ctx*4 + 3] = arrc
257 } }
258 if kind == 6 { if G[2] < G_MAGIC_99990 {
259 G[G_CONN + G[2]*2] = id1
260 G[G_CONN + G[2]*2 + 1] = id2
261 G[2] = G[2] + 1
262 } }
263 // Properties70 P entry under a Model: name = FIRST S prop; vector value = 3 D props.
264 // Matched by (len, first, last): PreRotation(11 P..n), Lcl Rotation(12 L..n),
265 // Lcl Translation(15 L..n). Angles degrees->millideg, translation units x1000.
266 if kind == 9 { if mctx >= 0 { if dseen >= 3 {
267 var pk: i64 = 0
268 if s1l == 11 { if (b[s1o]&0xff)==80 { if (b[s1o+10]&0xff)==110 { pk = 1 } } }
269 if s1l == 12 { if (b[s1o]&0xff)==76 { if (b[s1o+11]&0xff)==110 { pk = 2 } } }
270 if s1l == 15 { if (b[s1o]&0xff)==76 { if (b[s1o+14]&0xff)==110 { pk = 3 } } }
271 if pk == 1 {
272 G[G_PR + mctx*3] = sk_f64(b, dv1, 1000)
273 G[G_PR + mctx*3 + 1] = sk_f64(b, dv2, 1000)
274 G[G_PR + mctx*3 + 2] = sk_f64(b, dv3, 1000)
275 G[G_PF + mctx] = G[G_PF + mctx] | 1
276 }
277 if pk == 2 {
278 G[G_LR + mctx*3] = sk_f64(b, dv1, 1000)
279 G[G_LR + mctx*3 + 1] = sk_f64(b, dv2, 1000)
280 G[G_LR + mctx*3 + 2] = sk_f64(b, dv3, 1000)
281 G[G_PF + mctx] = G[G_PF + mctx] | 2
282 }
283 if pk == 3 {
284 // LENGTH site: Lcl Translation carries the unit factor (identity at 1.0).
285 G[G_LT + mctx*3] = sk_f64(b, dv1, 1000) * G[G_USF] / FBXC_MICRO
286 G[G_LT + mctx*3 + 1] = sk_f64(b, dv2, 1000) * G[G_USF] / FBXC_MICRO
287 G[G_LT + mctx*3 + 2] = sk_f64(b, dv3, 1000) * G[G_USF] / FBXC_MICRO
288 G[G_PF + mctx] = G[G_PF + mctx] | 4
289 }
290 } } }
291 p = pstart + plen
292 var sent: i64 = 13
293 if big == 1 { sent = 25 }
294 while p < endo - sent {
295 p = sk_walk(b, flen, p, big, G, childctx, childg, childm)
296 if p >= flen { return flen }
297 }
298 return endo
299}
300
301// inflate-or-raw helper for FBX arrays
302func sk_arr(b: *u8, off: i64, alen: i64, enc: i64, clen: i64, esz: i64) -> *u8 {
303 if enc == 0 { return ((b as i64) + off) as *u8 }
304 let zr: *NxZlibResult = nx_zlib_inflate(((b as i64) + off) as *u8, clen, alen*esz + G_MAGIC_4096)
305 if zr.error_code != 0 { return 0 as *u8 }
306 return zr.output_data
307}
308
309func sk_authored_main(argc: i64, argv: *i64) -> i64 {
310 var authored:i64=0
311 if argc>4{if fbxc_name_is(argv[4] as *u8,0,8,"authored" as *u8)==1{authored=1}else{return 2}}
312 if argc < 4 { skw("usage: nx_nxa_skin <in.fbx> <in.nxa> <out.nxa>\n" as *u8); return 2 }
313 let lpf: *i64 = sys_mmap(16) as *i64
314 let fb: *u8 = sys_map_file(argv[1] as *u8, lpf)
315 let flen: i64 = lpf[0]
316 if flen < 64 { skw("fbx unreadable\n" as *u8); return 3 }
317 let ver: i64 = sk_u32(fb, 23)
318 var big: i64 = 0
319 if ver >= G_MAGIC_7500 { big = 1 }
320 let G: *i64 = sys_mmap(G_WORDS*8) as *i64
321 // unit scale: parse once, announce, stash for every length site (see G_USF decl). -2 refuses
322 // (forged/nonpositive factor); -1 (absent) assumes the FBX template default 1.0, announced.
323 var usf9: i64 = fbxc_unit_scale_micro(fb, flen)
324 if usf9 == 0 - 2 { skw("NXA-SKIN REFUSED: UnitScaleFactor present but not a positive double\n" as *u8); return 3 }
325 if usf9 == 0 - 1 {
326 skw("unit_scale=ABSENT assumed=1000000 micro (FBX template default)\n" as *u8)
327 usf9 = FBXC_MICRO
328 } else {
329 skw("unit_scale_micro=" as *u8); skn(usf9); skw(" source=GlobalSettings\n" as *u8)
330 }
331 G[G_USF] = usf9
332 var pos: i64 = 27
333 var guard: i64 = 0
334 while pos < flen - 200 {
335 if guard > G_MAGIC_500000 { break }
336 let e: i64 = sk_walk(fb, flen, pos, big, G, 0 - 1, 0, 0 - 1)
337 if e <= pos { break }
338 pos = e
339 guard = guard + 1
340 }
341 let mc: i64 = G[0]
342 let cc: i64 = G[1]
343 let nc: i64 = G[2]
344 let gid: i64 = G[4]
345 skw("models=" as *u8); skn(mc)
346 skw(" clusters=" as *u8); skn(cc)
347 skw(" conns=" as *u8); skn(nc)
348 skw(" skins=" as *u8); skn(G[5])
349 skw(" cluster_overflow=" as *u8); skn(G[G_CLOV]); skw("\n" as *u8)
350 // MULTI-GEOMETRY (2026-08-23): a Skin is valid iff it connects to ANY geometry in the walk
351 // table, and we RECORD WHICH -- each cluster's Indexes are local to its own geometry's
352 // vertex space, so the stamping loop offsets by that geometry's base in the merged mesh.
353 // (The old rule -- only the LARGEST geometry's skin counts -- was the single-geometry era:
354 // on a 16-part Fab donor the largest part need not be the skinned body, and it filtered
355 // every cluster out: measured valid_clusters=0 skinned=0 on dark_witch.)
356 let sval: *i64 = sys_mmap(64*8 + 64) as *i64
357 let sgeo: *i64 = sys_mmap(64*8 + 64) as *i64
358 var sv0: i64 = 0
359 while sv0 < G[5] {
360 sval[sv0] = 0
361 sgeo[sv0] = 0 - 1
362 var k9: i64 = 0
363 while k9 < nc {
364 if G[G_CONN + k9*2] == G[G_SID + sv0] {
365 let dg9: i64 = G[G_CONN + k9*2 + 1]
366 var g9: i64 = 0
367 while g9 < G[G_GT] {
368 if G[G_GT + 1 + g9*2] == dg9 {
369 sval[sv0] = 1
370 sgeo[sv0] = g9
371 g9 = G[G_GT]
372 k9 = nc
373 }
374 g9 = g9 + 1
375 }
376 }
377 k9 = k9 + 1
378 }
379 sv0 = sv0 + 1
380 }
381 // cluster validity: cluster -> (valid Skin), RECORDING the skin's geometry for base offsets
382 let cval: *i64 = sys_mmap(cc*8 + 64) as *i64
383 let cgeo: *i64 = sys_mmap(cc*8 + 64) as *i64
384 var cv0: i64 = 0
385 while cv0 < cc {
386 cval[cv0] = 0
387 cgeo[cv0] = 0 - 1
388 var k8: i64 = 0
389 while k8 < nc {
390 if G[G_CONN + k8*2] == G[G_CID + cv0] {
391 let d8: i64 = G[G_CONN + k8*2 + 1]
392 var s8: i64 = 0
393 while s8 < G[5] {
394 if G[G_SID + s8] == d8 { if sval[s8] == 1 {
395 cval[cv0] = 1
396 cgeo[cv0] = sgeo[s8]
397 s8 = 64
398 k8 = nc
399 } }
400 s8 = s8 + 1
401 }
402 }
403 k8 = k8 + 1
404 }
405 cv0 = cv0 + 1
406 }
407 // joints = limb models in encounter order
408 let jmap: *i64 = sys_mmap(mc*8 + 64) as *i64
409 var nj: i64 = 0
410 var m0: i64 = 0
411 while m0 < mc {
412 jmap[m0] = 0 - 1
413 if G[G_LO + m0] == 1 { jmap[m0] = nj; nj = nj + 1 }
414 m0 = m0 + 1
415 }
416 // parent per joint via Connections (child -> parent)
417 let jpar: *i64 = sys_mmap(nj*8 + 64) as *i64
418 var j0: i64 = 0
419 while j0 < nj { jpar[j0] = 0 - 1; j0 = j0 + 1 }
420 var m1: i64 = 0
421 while m1 < mc {
422 if jmap[m1] >= 0 {
423 let myid: i64 = G[G_MO + m1]
424 var k: i64 = 0
425 while k < nc {
426 if G[G_CONN + k*2] == myid {
427 let dst: i64 = G[G_CONN + k*2 + 1]
428 var m2: i64 = 0
429 while m2 < mc {
430 if G[G_MO + m2] == dst { if jmap[m2] >= 0 {
431 jpar[jmap[m1]] = jmap[m2]
432 m2 = mc
433 k = nc
434 } }
435 m2 = m2 + 1
436 }
437 }
438 k = k + 1
439 }
440 }
441 m1 = m1 + 1
442 }
443 // cluster -> joint (Model(limb) --conn--> Cluster) + bind translation from TransformLink
444 let cjoint: *i64 = sys_mmap(cc*8 + 64) as *i64
445 let jbind: *i64 = sys_mmap(nj*24 + 64) as *i64
446 let jreal: *i64 = sys_mmap(nj*8 + 64) as *i64
447 var c0: i64 = 0
448 while c0 < cc { cjoint[c0] = 0 - 1; c0 = c0 + 1 }
449 var c1: i64 = 0
450 while c1 < cc {
451 let cid: i64 = G[G_CID + c1]
452 var k2: i64 = 0
453 while k2 < nc {
454 if G[G_CONN + k2*2 + 1] == cid {
455 let src: i64 = G[G_CONN + k2*2]
456 var m3: i64 = 0
457 while m3 < mc {
458 if G[G_MO + m3] == src { if jmap[m3] >= 0 {
459 cjoint[c1] = jmap[m3]
460 m3 = mc
461 k2 = nc
462 } }
463 m3 = m3 + 1
464 }
465 }
466 k2 = k2 + 1
467 }
468 // bind translation: TransformLink col-major elems 12,13,14 (joint->world at bind)
469 if cjoint[c1] >= 0 { if G[G_CT + c1*4 + 1] == 16 {
470 let tl: *u8 = sk_arr(fb, G[G_CT + c1*4], 16, G[G_CT + c1*4 + 2], G[G_CT + c1*4 + 3], 8)
471 if (tl as i64) != 0 {
472 let jj: i64 = cjoint[c1]
473 // LENGTH site: TransformLink bind translation carries the unit factor (identity
474 // at 1.0) -- must scale WITH the mesh or the rig detaches from the skin.
475 jbind[jj*3] = sk_f64(tl, 12*8, 1000) * G[G_USF] / FBXC_MICRO
476 jbind[jj*3+1] = sk_f64(tl, 13*8, 1000) * G[G_USF] / FBXC_MICRO
477 jbind[jj*3+2] = sk_f64(tl, 14*8, 1000) * G[G_USF] / FBXC_MICRO
478 jreal[jj] = 1
479 }
480 } }
481 c1 = c1 + 1
482 }
483 // v2: STATIC FK -> TRUE bind positions for ALL joints, replacing the old placeholder
484 // propagation (which parked unclustered joints on an ancestor; auto-skin against those
485 // degenerate segments mis-bound flesh -- shoulder shards under real per-joint mocap).
486 // Local = PreRotation x LclRotation chained down parents; non-limb ancestor Models
487 // compose into a per-root prefix. VALIDATED against the real cluster binds before use.
488 let mpar: *i64 = sys_mmap(mc*8 + 64) as *i64
489 var mp0: i64 = 0
490 while mp0 < mc { mpar[mp0] = 0 - 1; mp0 = mp0 + 1 }
491 var mp1: i64 = 0
492 while mp1 < mc {
493 let myid2: i64 = G[G_MO + mp1]
494 var kp: i64 = 0
495 while kp < nc {
496 if G[G_CONN + kp*2] == myid2 {
497 let dstp: i64 = G[G_CONN + kp*2 + 1]
498 var mp2: i64 = 0
499 while mp2 < mc {
500 if G[G_MO + mp2] == dstp { mpar[mp1] = mp2; mp2 = mc; kp = nc }
501 mp2 = mp2 + 1
502 }
503 }
504 kp = kp + 1
505 }
506 mp1 = mp1 + 1
507 }
508 let jmi: *i64 = sys_mmap(G_MAGIC_4096*8 + 64) as *i64
509 var mj0: i64 = 0
510 while mj0 < mc {
511 if jmap[mj0] >= 0 { jmi[jmap[mj0]] = mj0 }
512 mj0 = mj0 + 1
513 }
514 let scr: *i64 = sys_mmap(256) as *i64
515 let pq: *i64 = sys_mmap(nj*32 + 64) as *i64
516 let sq: *i64 = sys_mmap(nj*32 + 64) as *i64
517 var jq0: i64 = 0
518 while jq0 < nj {
519 let mi: i64 = jmi[jq0]
520 nf_eul2q(G[G_PR + mi*3], G[G_PR + mi*3 + 1], G[G_PR + mi*3 + 2], ((pq as i64) + jq0*32) as *i64, scr)
521 nf_eul2q(G[G_LR + mi*3], G[G_LR + mi*3 + 1], G[G_LR + mi*3 + 2], ((sq as i64) + jq0*32) as *i64, scr)
522 jq0 = jq0 + 1
523 }
524 let topo: *i64 = sys_mmap(nj*8 + 64) as *i64
525 let done: *i64 = sys_mmap(nj*8 + 64) as *i64
526 var tn: i64 = 0
527 var pass2: i64 = 0
528 while pass2 < nj {
529 if tn < nj {
530 var jp0: i64 = 0
531 while jp0 < nj {
532 if done[jp0] == 0 {
533 var ok: i64 = 0
534 if jpar[jp0] < 0 { ok = 1 }
535 if jpar[jp0] >= 0 { if done[jpar[jp0]] == 1 { ok = 1 } }
536 if ok == 1 { topo[tn] = jp0; tn = tn + 1; done[jp0] = 1 }
537 }
538 jp0 = jp0 + 1
539 }
540 }
541 pass2 = pass2 + 1
542 }
543 if tn != nj { skw("PARENT-CYCLE -- refusing\n" as *u8); return 7 }
544 let gqs: *i64 = sys_mmap(nj*32 + 64) as *i64
545 let gts: *i64 = sys_mmap(nj*24 + 64) as *i64
546 let lq: *i64 = sys_mmap(64) as *i64
547 let rv: *i64 = sys_mmap(64) as *i64
548 let prefq: *i64 = sys_mmap(nj*32 + 64) as *i64
549 let preft: *i64 = sys_mmap(nj*24 + 64) as *i64
550 let anc: *i64 = sys_mmap(32*8 + 64) as *i64
551 let aqq: *i64 = sys_mmap(64) as *i64
552 let bqq: *i64 = sys_mmap(64) as *i64
553 let tq2: *i64 = sys_mmap(64) as *i64
554 var jr0: i64 = 0
555 while jr0 < nj {
556 prefq[jr0*4] = 0
557 prefq[jr0*4+1] = 0
558 prefq[jr0*4+2] = 0
559 prefq[jr0*4+3] = G_MAGIC_4096
560 if jpar[jr0] < 0 {
561 var nanc: i64 = 0
562 var cm: i64 = mpar[jmi[jr0]]
563 var hops: i64 = 0
564 while cm >= 0 {
565 if nanc < 30 { anc[nanc] = cm; nanc = nanc + 1 }
566 cm = mpar[cm]
567 hops = hops + 1
568 if hops > 30 { cm = 0 - 1 }
569 }
570 var pi2: i64 = nanc - 1
571 while pi2 >= 0 {
572 let am: i64 = anc[pi2]
573 nf_eul2q(G[G_PR + am*3], G[G_PR + am*3 + 1], G[G_PR + am*3 + 2], aqq, scr)
574 nf_eul2q(G[G_LR + am*3], G[G_LR + am*3 + 1], G[G_LR + am*3 + 2], bqq, scr)
575 nf_qmul(aqq, bqq, tq2)
576 nf_qnorm(tq2)
577 nf_qrotv(((prefq as i64) + jr0*32) as *i64, G[G_LT + am*3], G[G_LT + am*3 + 1], G[G_LT + am*3 + 2], rv, scr)
578 preft[jr0*3] = preft[jr0*3] + rv[0]
579 preft[jr0*3+1] = preft[jr0*3+1] + rv[1]
580 preft[jr0*3+2] = preft[jr0*3+2] + rv[2]
581 nf_qmul(((prefq as i64) + jr0*32) as *i64, tq2, aqq)
582 nf_qnorm(aqq)
583 prefq[jr0*4] = aqq[0]
584 prefq[jr0*4+1] = aqq[1]
585 prefq[jr0*4+2] = aqq[2]
586 prefq[jr0*4+3] = aqq[3]
587 pi2 = pi2 - 1
588 }
589 }
590 jr0 = jr0 + 1
591 }
592 var ti9: i64 = 0
593 while ti9 < nj {
594 let j9: i64 = topo[ti9]
595 let mi9: i64 = jmi[j9]
596 nf_qmul(((pq as i64) + j9*32) as *i64, ((sq as i64) + j9*32) as *i64, lq)
597 nf_qnorm(lq)
598 let pj9: i64 = jpar[j9]
599 if pj9 < 0 {
600 nf_qmul(((prefq as i64) + j9*32) as *i64, lq, ((gqs as i64) + j9*32) as *i64)
601 nf_qnorm(((gqs as i64) + j9*32) as *i64)
602 nf_qrotv(((prefq as i64) + j9*32) as *i64, G[G_LT + mi9*3], G[G_LT + mi9*3 + 1], G[G_LT + mi9*3 + 2], rv, scr)
603 gts[j9*3] = preft[j9*3] + rv[0]
604 gts[j9*3+1] = preft[j9*3+1] + rv[1]
605 gts[j9*3+2] = preft[j9*3+2] + rv[2]
606 }
607 if pj9 >= 0 {
608 nf_qmul(((gqs as i64) + pj9*32) as *i64, lq, ((gqs as i64) + j9*32) as *i64)
609 nf_qnorm(((gqs as i64) + j9*32) as *i64)
610 nf_qrotv(((gqs as i64) + pj9*32) as *i64, G[G_LT + mi9*3], G[G_LT + mi9*3 + 1], G[G_LT + mi9*3 + 2], rv, scr)
611 gts[j9*3] = gts[pj9*3] + rv[0]
612 gts[j9*3+1] = gts[pj9*3+1] + rv[1]
613 gts[j9*3+2] = gts[pj9*3+2] + rv[2]
614 }
615 ti9 = ti9 + 1
616 }
617 var fkmax: i64 = 0
618 var fkvb: i64 = 0
619 var jv0: i64 = 0
620 while jv0 < nj {
621 if jreal[jv0] == 1 {
622 fkvb = fkvb + 1
623 var a9: i64 = 0
624 while a9 < 3 {
625 var d9: i64 = gts[jv0*3+a9] - jbind[jv0*3+a9]
626 if d9 < 0 { d9 = 0 - d9 }
627 if d9 > fkmax { fkmax = d9 }
628 a9 = a9 + 1
629 }
630 }
631 jv0 = jv0 + 1
632 }
633 skw("fk_validated=" as *u8); skn(fkvb)
634 skw(" fk_maxerr_units=" as *u8); skn(fkmax); skw("\n" as *u8)
635 if fkvb < 8 { skw("TOO-FEW-REAL-BINDS -- refusing FK bind replacement\n" as *u8); return 7 }
636 // FK-vs-cluster divergence DOWNGRADED from refusal to announced-keep (2026-08-23): TransformLink
637 // cluster binds ARE the rig's own declared bind pose; the FK recomposition is a cross-check whose
638 // pre/post-rotation model does not fit every exporter (measured: a Fab donor diverged by 63% of
639 // its own height -- structural, not tolerance noise). When they disagree the DECLARED binds win
640 // and the divergence stays announced above; when they agree, FK replacement proceeds as before.
641 if fkmax > G_MAGIC_20000 { skw("FK-DIVERGES from cluster binds -- KEEPING declared cluster binds; fk replacement skipped\n" as *u8) }
642 if fkmax <= G_MAGIC_20000 {
643 var jb9: i64 = 0
644 while jb9 < nj {
645 jbind[jb9*3] = gts[jb9*3]
646 jbind[jb9*3+1] = gts[jb9*3+1]
647 jbind[jb9*3+2] = gts[jb9*3+2]
648 jb9 = jb9 + 1
649 }
650 }
651 // SKIN accumulation: top-4 (joint, weight q12) per vertex of the main mesh
652 let lpn: *i64 = sys_mmap(16) as *i64
653 let nb: *u8 = sys_map_file(argv[2] as *u8, lpn)
654 let nlen2: i64 = lpn[0]
655 let vwo: i64 = nxa_find(nb, nlen2, nxa_tag4("VERT" as *u8))
656 if vwo < 0 { skw("in.nxa bad\n" as *u8); return 5 }
657 let two: i64 = nxa_find(nb, nlen2, nxa_tag4("TRIS" as *u8))
658 if two < 0 { skw("in.nxa bad\n" as *u8); return 5 }
659 let cwo: i64 = nxa_find(nb, nlen2, nxa_tag4("CLUS" as *u8))
660 let nh: *i64 = nb as *i64
661 let nv: i64 = nh[vwo]
662 let nt: i64 = nh[two]
663 var nclw: i64 = 0
664 if cwo >= 0 { nclw = 1 + nh[cwo]*10 }
665 let j4: *i64 = sys_mmap(nv*32 + 64) as *i64
666 let w4: *i64 = sys_mmap(nv*32 + 64) as *i64
667 let japp: *i64 = sys_mmap(nj*8 + 64) as *i64
668 var cvn: i64 = 0
669 var cvi: i64 = 0
670 var dbg: i64 = 0
671 while dbg < cc {
672 if cval[dbg] == 1 { cvn = cvn + 1; cvi = cvi + G[G_CIX + dbg*4 + 1] }
673 dbg = dbg + 1
674 }
675 skw("valid_clusters=" as *u8); skn(cvn)
676 skw(" their_index_elems=" as *u8); skn(cvi)
677 skw(" gid_vert_alen=" as *u8); skn(G[3]); skw("\n" as *u8)
678 // MULTI-GEOMETRY (2026-08-23): each cluster's Indexes are LOCAL to its own geometry's
679 // vertex space; offset by that geometry's base (prefix sum over the walk table, same
680 // document order nx_fbx_import merges in). DESYNC GUARD: if the table's vertex total
681 // disagrees with the in.nxa's, the pairing is unproven and weights would land on wrong
682 // vertices -- refuse authored-weight application BY NAME (joints still emit, autoskin
683 // covers) rather than stamp a plausible-looking wrong skin.
684 var tblnv: i64 = 0
685 var gtb0: i64 = 0
686 while gtb0 < G[G_GT] { tblnv = tblnv + G[G_GT + 2 + gtb0*2]/3; gtb0 = gtb0 + 1 }
687 var geomsync: i64 = 1
688 if tblnv != nv { geomsync = 0 }
689 skw("geom_table=" as *u8); skn(G[G_GT])
690 skw(" geom_table_overflow=" as *u8); skn(G[G_GTOV])
691 skw(" tbl_nv=" as *u8); skn(tblnv)
692 skw(" mesh_nv=" as *u8); skn(nv)
693 skw(" sync=" as *u8); skn(geomsync); skw("\n" as *u8)
694 if geomsync == 0 { skw("GEOM-TABLE-DESYNC -- refusing authored-weight application (autoskin only)\n" as *u8) }
695 var c2: i64 = 0
696 var applied: i64 = 0
697 while c2 < cc {
698 if geomsync == 1 { if cval[c2] == 1 { if cjoint[c2] >= 0 { if G[G_CIX + c2*4 + 1] > 0 { if G[G_CIX + c2*4 + 1] == G[G_CW + c2*4 + 1] {
699 let ni: i64 = G[G_CIX + c2*4 + 1]
700 let cg: i64 = cgeo[c2]
701 var vb2: i64 = 0
702 var gpre: i64 = 0
703 while gpre < cg { vb2 = vb2 + G[G_GT + 2 + gpre*2]/3; gpre = gpre + 1 }
704 var gnv: i64 = 0
705 if cg >= 0 { gnv = G[G_GT + 2 + cg*2]/3 }
706 let ia: *u8 = sk_arr(fb, G[G_CIX + c2*4], ni, G[G_CIX + c2*4 + 2], G[G_CIX + c2*4 + 3], 4)
707 let wa: *u8 = sk_arr(fb, G[G_CW + c2*4], ni, G[G_CW + c2*4 + 2], G[G_CW + c2*4 + 3], 8)
708 if (ia as i64) != 0 { if (wa as i64) != 0 {
709 let jj2: i64 = cjoint[c2]
710 var k3: i64 = 0
711 while k3 < ni {
712 let vi0: i64 = sk_u32(ia, k3*4)
713 let vi: i64 = vb2 + vi0
714 if vi0 < gnv { if vi < nv {
715 let wq: i64 = sk_f64(wa, k3*8, G_MAGIC_4096)
716 if wq > 0 {
717 // replace the smallest of the vertex's 4 slots if we beat it
718 var mslot: i64 = 0
719 var s: i64 = 1
720 while s < 4 { if w4[vi*4+s] < w4[vi*4+mslot] { mslot = s } s = s + 1 }
721 if wq > w4[vi*4+mslot] {
722 w4[vi*4+mslot] = wq
723 j4[vi*4+mslot] = jj2
724 japp[jj2] = japp[jj2] + 1
725 applied = applied + 1
726 }
727 }
728 } }
729 k3 = k3 + 1
730 }
731 } }
732 } } } } }
733 c2 = c2 + 1
734 }
735 // normalize each vertex to exact sum 4096; unskinned verts -> AUTO-SKIN to the nearest
736 // two clustered joints by inverse distance (authored weights only cover skin sub-regions
737 // in CC exports, and unrigged sculpts have none at all -- auto-fill makes every mesh posable)
738 let vx5: *i64 = ((nb as i64) + vwo*8 + 8) as *i64
739 // auto-skin candidates = ONLY bones that received AUTHORED cluster weight on OUR mesh.
740 // v2 regression caught by the name instrument: FK-true binds gave EYE/TONGUE bones real
741 // positions and nearest-segment auto-skin bound ~1600 FACE verts to an eyeball rotating
742 // 260deg in the mocap -- the "scapula shards" were the face. Organ bones whose clusters
743 // live on foreign meshes (eyes/tongue/teeth) drop out of japp by construction.
744 let jok: *i64 = sys_mmap(nj*8 + 64) as *i64
745 var jo0: i64 = 0
746 while jo0 < nj {
747 jok[jo0] = 0
748 if japp[jo0] > 0 { jok[jo0] = 1 }
749 jo0 = jo0 + 1
750 }
751 var skinned: i64 = 0
752 var autos: i64 = 0
753 var v0: i64 = 0
754 while v0 < nv {
755 let s0: i64 = w4[v0*4] + w4[v0*4+1] + w4[v0*4+2] + w4[v0*4+3]
756 if s0 == 0 {
757 if authored==1{skw("AUTHORED-UNWEIGHTED vertex=" as *u8);skn(v0);skw("\n" as *u8);return 7}
758 var b1: i64 = 0 - 1
759 var b2j: i64 = 0 - 1
760 var d1: i64 = G_MAGIC_4611686018427387903
761 var d2: i64 = d1
762 var jn: i64 = 0
763 while jn < nj {
764 if jok[jn] == 1 {
765 // distance to the BONE SEGMENT (parent->joint), not the joint point --
766 // point-distance bound mid-limb verts to the wrong bone = walk clipping
767 var ax: i64 = jbind[jn*3]
768 var ay: i64 = jbind[jn*3+1]
769 var az: i64 = jbind[jn*3+2]
770 let pj: i64 = jpar[jn]
771 var bxs: i64 = ax
772 var bys: i64 = ay
773 var bzs: i64 = az
774 if pj >= 0 { if jok[pj] == 1 {
775 bxs = jbind[pj*3]
776 bys = jbind[pj*3+1]
777 bzs = jbind[pj*3+2]
778 } }
779 let abx: i64 = (bxs - ax)/16
780 let aby: i64 = (bys - ay)/16
781 let abz: i64 = (bzs - az)/16
782 let vax: i64 = (vx5[v0*3] - ax)/16
783 let vay: i64 = (vx5[v0*3+1] - ay)/16
784 let vaz: i64 = (vx5[v0*3+2] - az)/16
785 let denom: i64 = abx*abx + aby*aby + abz*abz
786 var tq: i64 = 0
787 if denom > 0 { tq = (vax*abx + vay*aby + vaz*abz)*G_MAGIC_4096/denom }
788 if tq < 0 { tq = 0 }
789 if tq > G_MAGIC_4096 { tq = G_MAGIC_4096 }
790 let dx: i64 = vax - abx*tq/G_MAGIC_4096
791 let dy: i64 = vay - aby*tq/G_MAGIC_4096
792 let dz: i64 = vaz - abz*tq/G_MAGIC_4096
793 let dd: i64 = dx*dx + dy*dy + dz*dz
794 if dd < d1 { d2 = d1; b2j = b1; d1 = dd; b1 = jn }
795 if dd >= d1 { if dd < d2 { d2 = dd; b2j = jn } }
796 }
797 jn = jn + 1
798 }
799 if b1 >= 0 {
800 if b2j >= 0 {
801 // inverse-distance split over the two nearest bones (d in squared space)
802 let wsum: i64 = d1 + d2
803 var wa: i64 = G_MAGIC_4096*d2/wsum
804 if wa > G_MAGIC_4096 { wa = G_MAGIC_4096 }
805 j4[v0*4] = b1
806 w4[v0*4] = wa
807 j4[v0*4+1] = b2j
808 w4[v0*4+1] = G_MAGIC_4096 - wa
809 }
810 if b2j < 0 {
811 j4[v0*4] = b1
812 w4[v0*4] = G_MAGIC_4096
813 }
814 autos = autos + 1
815 }
816 if b1 < 0 {
817 j4[v0*4] = 0
818 w4[v0*4] = G_MAGIC_4096
819 }
820 }
821 if s0 > 0 {
822 skinned = skinned + 1
823 var acc: i64 = 0
824 var s1: i64 = 0
825 while s1 < 3 {
826 let nw: i64 = w4[v0*4+s1]*G_MAGIC_4096/s0
827 w4[v0*4+s1] = nw
828 acc = acc + nw
829 s1 = s1 + 1
830 }
831 w4[v0*4+3] = G_MAGIC_4096 - acc
832 }
833 v0 = v0 + 1
834 }
835 // WEIGHT SMOOTHING (2 Laplacian passes over the mesh graph): the stretch instrument
836 // measured hard 100pct-to-100pct weight steps tearing under real per-joint motion --
837 // armpit Spine02<>UpperarmTwist01 128 torn edges, hip crease Pelvis<>ThighTwist01 118,
838 // shoulder top <>Clavicle 112. 60/40 own/neighbor blend, top-4 retruncate, sum=4096.
839 let tr5: *i64 = ((nb as i64) + two*8 + 8) as *i64
840 let deg: *i64 = sys_mmap(nv*8 + 64) as *i64
841 var te0: i64 = 0
842 while te0 < nt {
843 deg[tr5[te0*3]] = deg[tr5[te0*3]] + 2
844 deg[tr5[te0*3+1]] = deg[tr5[te0*3+1]] + 2
845 deg[tr5[te0*3+2]] = deg[tr5[te0*3+2]] + 2
846 te0 = te0 + 1
847 }
848 let adjo: *i64 = sys_mmap(nv*8 + 72) as *i64
849 var ao0: i64 = 0
850 var acc0: i64 = 0
851 while ao0 < nv { adjo[ao0] = acc0; acc0 = acc0 + deg[ao0]; ao0 = ao0 + 1 }
852 adjo[nv] = acc0
853 let adjl: *i64 = sys_mmap(acc0*8 + 64) as *i64
854 let cur: *i64 = sys_mmap(nv*8 + 64) as *i64
855 var te1: i64 = 0
856 while te1 < nt {
857 var ec0: i64 = 0
858 while ec0 < 3 {
859 let a5: i64 = tr5[te1*3 + ec0]
860 var b5: i64 = tr5[te1*3]
861 var c5: i64 = tr5[te1*3 + 1]
862 if ec0 == 0 { b5 = tr5[te1*3+1]; c5 = tr5[te1*3+2] }
863 if ec0 == 1 { b5 = tr5[te1*3]; c5 = tr5[te1*3+2] }
864 if ec0 == 2 { b5 = tr5[te1*3]; c5 = tr5[te1*3+1] }
865 adjl[adjo[a5] + cur[a5]] = b5
866 cur[a5] = cur[a5] + 1
867 adjl[adjo[a5] + cur[a5]] = c5
868 cur[a5] = cur[a5] + 1
869 ec0 = ec0 + 1
870 }
871 te1 = te1 + 1
872 }
873 let j4b: *i64 = sys_mmap(nv*32 + 64) as *i64
874 let w4b: *i64 = sys_mmap(nv*32 + 64) as *i64
875 let mj: *i64 = sys_mmap(64*8 + 64) as *i64
876 let mw: *i64 = sys_mmap(64*8 + 64) as *i64
877 var it0: i64 = 0
878 while it0 < 3 && authored == 0 {
879 var vs0: i64 = 0
880 while vs0 < nv {
881 let dv5: i64 = deg[vs0]
882 if dv5 == 0 {
883 var sc0: i64 = 0
884 while sc0 < 4 { j4b[vs0*4+sc0] = j4[vs0*4+sc0]; w4b[vs0*4+sc0] = w4[vs0*4+sc0]; sc0 = sc0 + 1 }
885 }
886 if dv5 > 0 {
887 var mn: i64 = 0
888 var so0: i64 = 0
889 while so0 < 4 {
890 if w4[vs0*4+so0] > 0 {
891 let jj5: i64 = j4[vs0*4+so0]
892 let ww5: i64 = w4[vs0*4+so0]*6*dv5
893 var f0: i64 = 0 - 1
894 var m0s: i64 = 0
895 while m0s < mn { if mj[m0s] == jj5 { f0 = m0s; m0s = mn } m0s = m0s + 1 }
896 if f0 >= 0 { mw[f0] = mw[f0] + ww5 }
897 if f0 < 0 { if mn < 60 { mj[mn] = jj5; mw[mn] = ww5; mn = mn + 1 } }
898 }
899 so0 = so0 + 1
900 }
901 var nb0: i64 = 0
902 while nb0 < dv5 {
903 let vn5: i64 = adjl[adjo[vs0] + nb0]
904 var sn0: i64 = 0
905 while sn0 < 4 {
906 if w4[vn5*4+sn0] > 0 {
907 let jj6: i64 = j4[vn5*4+sn0]
908 let ww6: i64 = w4[vn5*4+sn0]*4
909 var f1: i64 = 0 - 1
910 var m1s: i64 = 0
911 while m1s < mn { if mj[m1s] == jj6 { f1 = m1s; m1s = mn } m1s = m1s + 1 }
912 if f1 >= 0 { mw[f1] = mw[f1] + ww6 }
913 if f1 < 0 { if mn < 60 { mj[mn] = jj6; mw[mn] = ww6; mn = mn + 1 } }
914 }
915 sn0 = sn0 + 1
916 }
917 nb0 = nb0 + 1
918 }
919 // top-4 of the merged map -> renormalize to exact 4096
920 var sl0: i64 = 0
921 while sl0 < 4 {
922 var bi5: i64 = 0 - 1
923 var bw5: i64 = 0
924 var m2s: i64 = 0
925 while m2s < mn {
926 if mw[m2s] > bw5 { bw5 = mw[m2s]; bi5 = m2s }
927 m2s = m2s + 1
928 }
929 if bi5 >= 0 {
930 j4b[vs0*4+sl0] = mj[bi5]
931 w4b[vs0*4+sl0] = mw[bi5]
932 mw[bi5] = 0
933 }
934 if bi5 < 0 { j4b[vs0*4+sl0] = 0; w4b[vs0*4+sl0] = 0 }
935 sl0 = sl0 + 1
936 }
937 let ts5: i64 = w4b[vs0*4] + w4b[vs0*4+1] + w4b[vs0*4+2] + w4b[vs0*4+3]
938 if ts5 > 0 {
939 var an5: i64 = 0
940 var sr0: i64 = 0
941 while sr0 < 3 {
942 let nw5: i64 = w4b[vs0*4+sr0]*G_MAGIC_4096/ts5
943 w4b[vs0*4+sr0] = nw5
944 an5 = an5 + nw5
945 sr0 = sr0 + 1
946 }
947 w4b[vs0*4+3] = G_MAGIC_4096 - an5
948 }
949 if ts5 == 0 { j4b[vs0*4] = 0; w4b[vs0*4] = G_MAGIC_4096; w4b[vs0*4+1] = 0; w4b[vs0*4+2] = 0; w4b[vs0*4+3] = 0 }
950 }
951 vs0 = vs0 + 1
952 }
953 var cp0: i64 = 0
954 while cp0 < nv*4 { j4[cp0] = j4b[cp0]; w4[cp0] = w4b[cp0]; cp0 = cp0 + 1 }
955 it0 = it0 + 1
956 }
957 if authored==1{skw("weights_mode=AUTHORED top4_q12_only smoothing=0 autoskin=REFUSE\n" as *u8)}else{skw("weights_smoothed=3passes\n" as *u8)}
958 skw("joints=" as *u8); skn(nj)
959 skw(" skinned=" as *u8); skn(skinned)
960 skw(" autoskinned=" as *u8); skn(autos)
961 skw("/" as *u8); skn(nv)
962 skw(" applied=" as *u8); skn(applied); skw("\n" as *u8)
963 // build SKEL + SKIN payloads
964 let skel: *i64 = sys_mmap((1 + nj*8)*8 + 64) as *i64
965 skel[0] = nj
966 var j1: i64 = 0
967 while j1 < nj {
968 skel[1 + j1*8] = jpar[j1]
969 skel[1 + j1*8 + 1] = jbind[j1*3]
970 skel[1 + j1*8 + 2] = jbind[j1*3 + 1]
971 skel[1 + j1*8 + 3] = jbind[j1*3 + 2]
972 skel[1 + j1*8 + 4] = 0
973 skel[1 + j1*8 + 5] = 0
974 skel[1 + j1*8 + 6] = 0
975 skel[1 + j1*8 + 7] = G_MAGIC_4096
976 j1 = j1 + 1
977 }
978 let skin: *i64 = sys_mmap((1 + nv*8)*8 + 64) as *i64
979 skin[0] = nv
980 var v1: i64 = 0
981 while v1 < nv {
982 var s2: i64 = 0
983 while s2 < 4 {
984 skin[1 + v1*8 + s2] = j4[v1*4 + s2]
985 skin[1 + v1*8 + 4 + s2] = w4[v1*4 + s2]
986 s2 = s2 + 1
987 }
988 v1 = v1 + 1
989 }
990 // emit the 5-section NXA
991 let fd: i64 = sys_openat_wr(argv[3] as *u8, 0x1a4)
992 if fd < 0 { skw("open out failed\n" as *u8); return 9 }
993 let vwl: i64 = 1 + nv*3
994 let twl: i64 = 1 + nt*3
995 let swl: i64 = 1 + nj*8
996 let kwl: i64 = 1 + nv*8
997 var ns: i64 = 4
998 if nclw > 0 { ns = 5 }
999 let hdr: *i64 = sys_mmap(64) as *i64
1000 let toc: *i64 = sys_mmap(512) as *i64
1001 var o: i64 = 32 + ns*32
1002 var ti: i64 = 0
1003 toc[ti*4] = nxa_tag4("VERT" as *u8)
1004 toc[ti*4+1] = o
1005 toc[ti*4+2] = vwl
1006 toc[ti*4+3] = nxa_check2(1, ((nb as i64) + vwo*8) as *i64, vwl)
1007 o = o + vwl*8
1008 ti = ti + 1
1009 toc[ti*4] = nxa_tag4("TRIS" as *u8)
1010 toc[ti*4+1] = o
1011 toc[ti*4+2] = twl
1012 toc[ti*4+3] = nxa_check2(1, ((nb as i64) + two*8) as *i64, twl)
1013 o = o + twl*8
1014 ti = ti + 1
1015 if nclw > 0 {
1016 toc[ti*4] = nxa_tag4("CLUS" as *u8)
1017 toc[ti*4+1] = o
1018 toc[ti*4+2] = nclw
1019 toc[ti*4+3] = nxa_check2(1, ((nb as i64) + cwo*8) as *i64, nclw)
1020 o = o + nclw*8
1021 ti = ti + 1
1022 }
1023 toc[ti*4] = nxa_tag4("SKEL" as *u8)
1024 toc[ti*4+1] = o
1025 toc[ti*4+2] = swl
1026 toc[ti*4+3] = nxa_check2(1, skel, swl)
1027 o = o + swl*8
1028 ti = ti + 1
1029 toc[ti*4] = nxa_tag4("SKIN" as *u8)
1030 toc[ti*4+1] = o
1031 toc[ti*4+2] = kwl
1032 toc[ti*4+3] = nxa_check2(1, skin, kwl)
1033 hdr[0] = nxa_magic()
1034 hdr[1] = NXA_VER
1035 hdr[2] = ns
1036 hdr[3] = nxa_check2(1, toc, ns*4)
1037 sys_write(fd, hdr as *u8, 32)
1038 sys_write(fd, toc as *u8, ns*32)
1039 sys_write(fd, ((nb as i64) + vwo*8) as *u8, vwl*8)
1040 sys_write(fd, ((nb as i64) + two*8) as *u8, twl*8)
1041 if nclw > 0 { sys_write(fd, ((nb as i64) + cwo*8) as *u8, nclw*8) }
1042 sys_write(fd, skel as *u8, swl*8)
1043 sys_write(fd, skin as *u8, kwl*8)
1044 sys_close(fd)
1045 skw("NXA5 written joints=" as *u8); skn(nj)
1046 skw(" skin_verts=" as *u8); skn(nv); skw("\n" as *u8)
1047 return 0
1048}