code wiki / (root) / nx_person_mesh_candidate_t343.nx

nx_person_mesh_candidate_t343.nx source

↩ module page · 56 lines · 5207 B

1import "nx_person_field_candidate_t343.nx" 2import "nx_isosurf.nx" 3import "nx_char_asset_material_candidate_t340.nx" 4import "nx_value_parse_json.nx" 5import "nx_memfloor.nx" 6import "nx_meshcheck.nx" 7import "nx_itoa_lib.nx" 8func npm_put(s:*u8)->i64{var n:i64=0;while s[n]!=0 as u8{n=n+1};return sys_write(1,s,n)} 9func npm_req(v:*NxValue,k:*u8)->i64{var n:i64=0;while k[n]!=0 as u8{n=n+1};let o:*i64=sys_mmap(8) as *i64;if nx_value_object_get_int(v,k,n,o)!=1{npm_put("RECIPE missing integer ");npm_put(k);npm_put(" 10");sys_exit(2)};return o[0]} 11func npm_abs(v:i64)->i64{if v<0{return 0-v};return v} 12func main(argc:i64,argv:*i64)->i64{ 13 if argc!=3{npm_put("usage: recipe.json output.glb 14");return 2} 15 let len:*i64=sys_mmap(8) as *i64;let raw:*u8=sys_read_file(argv[1] as *u8,len);if (raw as i64)<=0{return 2} 16 let verdict:*i64=sys_mmap(8) as *i64;let spec:*NxValue=nx_value_parse_json(raw,len[0],verdict);if (spec as i64)==0{return 2} 17 let seed:i64=npm_req(spec,"seed");let cell:i64=npm_req(spec,"cell");let ox:i64=npm_req(spec,"min_x");let oy:i64=npm_req(spec,"min_y");let oz:i64=npm_req(spec,"min_z");let exx:i64=npm_req(spec,"max_x");let eyy:i64=npm_req(spec,"max_y");let ezz:i64=npm_req(spec,"max_z") 18 let num:i64=npm_req(spec,"scale_num");let den:i64=npm_req(spec,"scale_den");let ty:i64=npm_req(spec,"translate_y");let blend:i64=npm_req(spec,"join_blend");let maxcells:i64=npm_req(spec,"max_cells");let vcap:i64=npm_req(spec,"max_vertices");let tcap:i64=npm_req(spec,"max_triangles");let hue:i64=npm_req(spec,"hue");let region:i64=npm_req(spec,"region") 19 if cell<=0||cell>FX||num<=0||num>FX||den<=0||den>FX||blend<=0||blend>FX{return 3} 20 if ox>=exx||oy>=eyy||oz>=ezz||npm_abs(ox)>FX*FX||npm_abs(oy)>FX*FX||npm_abs(oz)>FX*FX||npm_abs(exx)>FX*FX||npm_abs(eyy)>FX*FX||npm_abs(ezz)>FX*FX||npm_abs(ty)>FX*FX{return 3} 21 let gx:i64=(exx-ox+cell-1)/cell;let gy:i64=(eyy-oy+cell-1)/cell;let gz:i64=(ezz-oz+cell-1)/cell 22 if gx>TM_GRID||gy>TM_GRID||gz>TM_GRID||gx*gy*gz>maxcells||vcap<=0||vcap>TM_VCAP||tcap<=0||tcap>TM_TCAP{return 3} 23 let qx:i64=(npm_abs(ox)+npm_abs(exx)+cell)*den/num;let qy:i64=(npm_abs(oy)+npm_abs(eyy)+2*npm_abs(ty)+cell)*den/num;let qz:i64=(npm_abs(oz)+npm_abs(ezz)+cell)*den/num 24 if qx>FX*64||qy>FX*64||qz>FX*64{return 3} 25 let points:i64=(gx+1)*(gy+1)*(gz+1);let cells:i64=gx*gy*gz 26 let need:i64=points*8+cells*8+tm_bind_bytes(vcap,tcap)+vcap*24+tcap*24+3*sdf_bytes()+vcap*96+tcap*128 27 if mf_avail_kb()<0||mf_admit("complete-person-mesh",need)!=1{return 4} 28 npm_put("ADMITTED grid=");nxi_out(gx);npm_put("x");nxi_out(gy);npm_put("x");nxi_out(gz);npm_put(" cell=");nxi_out(cell);npm_put(" estimated_peak_bytes=");nxi_out(need);npm_put(" 29") 30 let c:*NativePersonField=sys_mmap(48) as *NativePersonField;let b:i64=sys_mmap(sdf_bytes()) as i64;let h:i64=sys_mmap(sdf_bytes()) as i64 31 let skin:*i64=sys_mmap(24) as *i64;let identity:*i64=sys_mmap(64) as *i64;let genes:*i64=sys_mmap(FA_EG_N*8) as *i64;let map:*i64=sys_mmap((NPART+FA_NPARTS)*8) as *i64 32 let np:i64=npf_build(c,b,h,seed,num,den,ty,blend,skin,identity,genes,map);if np!=NPART-1+FA_NPARTS{return 5} 33 npm_put("IDENTITY seed=");nxi_out(seed);npm_put(" morphs=");var mi:i64=0;while mi<8{nxi_out(identity[mi]);npm_put(" ");mi=mi+1};npm_put(" eye_genes=");mi=0;while mi<FA_EG_N{nxi_out(genes[mi]);npm_put(" ");mi=mi+1};npm_put(" source_part_map=");mi=0;while mi<np{nxi_out(map[mi]);npm_put(" ");mi=mi+1};npm_put("\n") 34 let grid:*i64=sys_mmap(points*8) as *i64;var border:i64=0;var i:i64=0 35 while i<=gx{var j:i64=0;while j<=gy{var k:i64=0;while k<=gz{ 36 let d:i64=npf_eval(c,ox+i*cell,oy+j*cell,oz+k*cell);grid[(i*(gy+1)+j)*(gz+1)+k]=d 37 if i==0||i==gx||j==0||j==gy||k==0||k==gz{if d<=0{border=border+1}} 38 k=k+1};j=j+1};i=i+1} 39 npm_put("SAMPLED border_nonpositive=");nxi_out(border);npm_put(" parts=");nxi_out(np);npm_put(" 40");if border!=0{return 6} 41 let arena:i64=sys_mmap(tm_bind_bytes(vcap,tcap)) as i64;if tm_bind(arena,vcap,tcap)!=0{return 7} 42 surface_nets(grid,gx,gy,gz,ox,oy,oz,cell,0,0) 43 let nv:i64=tm_nv();let nt:i64=tm_nt();npm_put("MESH vertices=");nxi_out(nv);npm_put(" triangles=");nxi_out(nt);npm_put(" overflow=");nxi_out(tm_ovf());npm_put(" 44");if tm_ovf()!=0||nv<=0||nt<=0{return 8} 45 let verts:*i64=sys_mmap(nv*24) as *i64;let faces:*i64=sys_mmap(nt*24) as *i64;i=0 46 while i<nv{verts[i*3]=tm_vx(i);verts[i*3+1]=tm_vy(i);verts[i*3+2]=tm_vz(i);i=i+1};i=0 47 while i<nt{faces[i*3]=tm_ta(i);faces[i*3+1]=tm_tb(i);faces[i*3+2]=tm_tc(i);i=i+1} 48 let co:*i64=sys_mmap(MC_O_SLOTS*8) as *i64;let cl:*i64=sys_mmap(mc_list_slots()*8) as *i64;let cr:i64=mc_check(verts,nv,faces,nt,co,cl) 49 npm_put("TOPOLOGY rc=");nxi_out(cr);npm_put(" slots=");i=0;while i<MC_O_SLOTS{nxi_out(co[i]);npm_put(" ");i=i+1};npm_put(" 50") 51 let consent:*i64=sys_mmap(64) as *i64;consent_init(consent,PROV_SYNTH,1,LIC_ARTISTIC,USE_GAME|USE_DISTRIBUTE,0,seed,seed) 52 let written:i64=char_export_existing_mesh_recipe_gated(verts,faces,nv,nt,consent,sys_now_realtime_sec(),0,0,USE_GAME|USE_DISTRIBUTE,hue,region,argv[2] as *u8) 53 npm_put("DIAGNOSTIC glb_bytes=");nxi_out(written);npm_put(" material_hue=");nxi_out(hue);npm_put(" region=");nxi_out(region);npm_put(" rig=ABSENT anatomy=UNACCEPTED topology_rc=");nxi_out(cr);npm_put(" 54") 55 if written<=0{return 9};return 0 56}