code wiki / _hdl_build / nx_gltf_scene_gate.nx

nx_gltf_scene_gate.nx source

↩ module page · 95 lines · 5246 B

1// nx_gltf_scene_gate.nx -- ★export the FULL holistic being (all 8 anatomical systems) as ONE peelable multi-mesh .glb. 2// Each system polygonized separately + per-part coloured -> a named node/mesh. Self-verifies the container + writes 3// knowledge/nishi_being_full.glb. license_tier: ORIGINAL expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_trimesh.nx" 6import "nx_isosurf.nx" 7import "nx_bodyatlas.nx" 8import "nx_gltf_scene.nx" 9 10func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 12func ru32(buf: *u8, at: i64) -> i64 { return (buf[at] as i64) + ((buf[at+1] as i64)<<8) + ((buf[at+2] as i64)<<16) + ((buf[at+3] as i64)<<24) } 13func hasstr(buf: *u8, n: i64, needle: *u8) -> i64 { var m: i64=0; while needle[m]!=(0 as u8){m=m+1} var i: i64=0; while i+m<=n { var k: i64=0; while k<m { if buf[i+k]!=needle[k] {k=m+9} else {k=k+1} } if k==m {return 1} i=i+1 } return 0 } 14 15const GX: i64 = 32 16const GY: i64 = 110 17const GZ: i64 = 20 18const GC: i64 = 18 19const OX: i64 = 0-288 20const OY: i64 = 0-990 21const OZ: i64 = 0-180 22static GRID: i64 23 24func fill_grid(mask: i64) -> i64 { 25 let g: *i64 = GRID as *i64 26 var i: i64 = 0 27 while i <= GX { var jj: i64=0; while jj<=GY { var k: i64=0; while k<=GZ { g[(i*(GY+1)+jj)*(GZ+1)+k] = ba_sdf(OX+i*GC, OY+jj*GC, OZ+k*GC, mask); k=k+1 } jj=jj+1 } i=i+1 } 28 return 0 29} 30func color_by_part(mask: i64) -> i64 { 31 let out: *i64 = sys_mmap(24) as *i64 32 var i: i64 = 0 33 let nv: i64 = tm_nv() 34 while i < nv { tm_vpos(i, out); let k: i64 = ba_nearest(out[0], out[1], out[2], mask); if k>=0 { tm_vcol(i, ba_part_color(k)) } else { tm_vcol(i, 200+200*256+200*65536) } i=i+1 } 35 return 0 36} 37func build_system(mask: i64, name: *u8, rough: i64, metal: i64) -> i64 { 38 fill_grid(mask) 39 tm_reset() 40 surface_nets(GRID as *i64, GX, GY, GZ, OX, OY, OZ, GC, 0, 200+200*256+200*65536) 41 color_by_part(mask) 42 let v: i64 = tm_nv(); let t: i64 = tm_nt() 43 sc_add_mesh(name, rough, metal) 44 hw(" + "); hw(name); hw(": verts="); pn(v); hw(" tris="); pn(t); hw(" rough="); pn(rough); hw(" ovf="); pn(tm_ovf()); hw("\n" as *u8) 45 return 0 46} 47 48func main() -> i64 { 49 hw("=== nx_gltf_scene_gate -- full holistic being (8 systems) -> one peelable .glb ===\n" as *u8) 50 var fails: i64 = 0 51 atlas_build(0) 52 GRID = sys_mmap((GX+1)*(GY+1)*(GZ+1)*8) as i64 53 sc_reset() 54 build_system(1, "skin" as *u8, 720, 0) // ★PBR: skin soft dielectric 55 build_system(2, "muscle" as *u8, 550, 0) // muscle 56 build_system(4, "skeleton" as *u8, 620, 0) // bone semi-matte 57 build_system(8, "digestive" as *u8, 350, 0) // viscera wet/glossy 58 build_system(16, "circulatory" as *u8, 260, 0) // vessels/heart glossy wet 59 build_system(32, "respiratory" as *u8, 480, 0) // lungs 60 build_system(64, "urinary" as *u8, 400, 0) 61 build_system(128, "nervous" as *u8, 450, 0) 62 let M: i64 = sc_mesh_count() 63 hw(" meshes in scene="); pn(M); hw("\n" as *u8) 64 var t0: i64 = 0 65 if M == 8 { t0 = 1 } 66 if t0 == 1 { hw("T0 PASS all 8 systems meshed into the scene\n" as *u8) } else { fails=fails+1; hw("T0 FAIL meshes="); pn(M); hw("\n" as *u8) } 67 68 let out: *u8 = sys_mmap(16777216) 69 let total: i64 = sc_finalize(out) 70 hw(" glb bytes="); pn(total); hw("\n" as *u8) 71 // self-verify container 72 var t1: i64 = 0 73 if (out[0] as i64)==103 { if (out[1] as i64)==108 { if (out[2] as i64)==84 { if (out[3] as i64)==70 { if ru32(out,8)==total { t1=1 } } } } } 74 if t1 == 1 { hw("T1 PASS glTF header + length ok\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 75 let jsonLen: i64 = ru32(out, 12) 76 let binLenPos: i64 = 20 + jsonLen 77 let binLen: i64 = ru32(out, binLenPos) 78 var t2: i64 = 0 79 if (out[16] as i64)==74 { if (out[binLenPos+4] as i64)==66 { if (12+8+jsonLen+8+binLen)==total { t2=1 } } } 80 if t2 == 1 { hw("T2 PASS chunk types + arithmetic close\n" as *u8) } else { fails=fails+1; hw("T2 FAIL\n" as *u8) } 81 // json names all systems + has 8 meshes' worth of accessors 82 var t3: i64 = 0 83 if hasstr(out, 20+jsonLen, "skeleton" as *u8)==1 { if hasstr(out, 20+jsonLen, "nervous" as *u8)==1 { if hasstr(out, 20+jsonLen, "circulatory" as *u8)==1 { t3=1 } } } 84 if t3 == 1 { hw("T3 PASS scene JSON names the systems (skeleton, circulatory, nervous, ...)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL\n" as *u8) } 85 86 let fd: i64 = sys_openat_wr("knowledge/nishi_being_full.glb\x00" as *u8, 0x1a4) 87 sys_write(fd, out, total) 88 sys_close(fd) 89 hw("wrote knowledge/nishi_being_full.glb ("); pn(total); hw(" bytes)\n" as *u8) 90 91 if fails == 0 { hw("GLTF-SCENE-GATE GREEN -- the full holistic being (8 named anatomical systems) exports as ONE peelable multi-mesh .glb, container self-verified. Toggle systems in any glTF viewer -> a real, downloadable ZygoteBody.\n" as *u8); sys_exit(0); return 0 } 92 hw("GLTF-SCENE-GATE RED fails="); pn(fails); hw("\n" as *u8) 93 sys_exit(1) 94 return 1 95}