code wiki / _hdl_build / nx_engine_scene_gate.nx

nx_engine_scene_gate.nx source

↩ module page · 249 lines · 12661 B

1// nx_engine_scene_gate.nx -- ★ENGINE CONVERGENCE RUNG 1: worlds + beings + assets in ONE scene, ONE renderer (the 2// Unreal lesson: Nanite geometry + MetaHumans + PCG coexist as ENGINE CITIZENS, not island demos). This scene: 3// TERRAIN layered-noise heightfield (the banked Minecraft-pipeline lesson: octaves + biome-ish surface colours) 4// BEING the holistic human, WALK-POSED, polygonized IN PLACE onto the terrain (SDF -> mesh at world coords) 5// ASSET the Stanford bunny (corpus citizen), placed beside the being 6// -- all in ONE trimesh buffer through ONE z-buffered render (SSAA hero shot). Visibility proofs are CAUSAL: remove 7// a citizen -> the image must change. T0 composition, T1 renders, T2 causal visibility, T3 determinism, T4 evidence. 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_png.nx" 11import "nx_trimesh.nx" 12import "nx_isosurf.nx" 13import "nx_bodyatlas.nx" 14import "nx_objload.nx" 15import "nx_natstat.nx" 16 17func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func 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 } 19func clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 } 20func filled(fb: *i64, n: i64, bg: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { if fb[i]!=bg { c=c+1 } i=i+1 } return c } 21func imdiff(a: *i64, b: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { if a[i]!=b[i] { s=s+1 } i=i+1 } return s } 22 23const W: i64 = 520 24const H: i64 = 380 25const BG: i64 = 30 + 40*256 + 58*65536 // dusk-sky backdrop 26 27// layered-noise terrain height (2 octaves -- the banked pipeline lesson in miniature) 28func terr_h(x: i64, z: i64) -> i64 { 29 let n1: i64 = tm_noise3(x, 7777, z, 1500) 30 let n2: i64 = tm_noise3(x, 12345, z, 420) 31 return n1*430/1024 + n2*150/1024 - 300 32} 33func paint(from: i64, to: i64, col: i64) -> i64 { var i: i64=from; while i<to { tm_vcol(i,col); i=i+1 } return 0 } 34 35const GXb: i64 = 32 36const GYb: i64 = 110 37const GZb: i64 = 20 38const GCb: i64 = 18 39 40func main() -> i64 { 41 hw("=== nx_engine_scene_gate -- worlds + beings + corpus assets: ONE scene, ONE renderer ===\n" as *u8) 42 var fails: i64 = 0 43 tm_set_spec(0) 44 tm_set_tex(0) 45 tm_set_image(0 as *u8, 0, 0) 46 47 // ---- citizen 1: the Stanford bunny (obj_load resets the buffer, so it goes FIRST) ---- 48 let okB: i64 = obj_load("knowledge/stdassets/bunny.obj" as *u8, 1300, 214+208*256+198*65536) 49 let bunnyTo: i64 = tm_nv() 50 // place: scale 0.45, on the terrain at (1250, -420) 51 let bh: i64 = terr_h(1250, 0-420) 52 tm_place(0, bunnyTo, 1250, bh + 292, 0-420, 450) // normalized bunny centre ~0 -> half-height*0.45 ~ 292 lifts feet to ground 53 hw(" bunny: ok="); pn(okB); hw(" verts="); pn(bunnyTo); hw(" ground="); pn(bh); hw("\n" as *u8) 54 55 // ---- citizen 2: TERRAIN heightfield (41x41 verts, biome-ish colours) ---- 56 let terrFrom: i64 = tm_nv() 57 let TN: i64 = 41 58 let SP: i64 = 185 // spacing -> 7400 wide (a place, not a backyard) 59 var gz: i64 = 0 60 while gz < TN { 61 var gx: i64 = 0 62 while gx < TN { 63 let wx: i64 = (gx - 20)*SP 64 let wz: i64 = (gz - 20)*SP 65 let h: i64 = terr_h(wx, wz) 66 let vi: i64 = tm_vert(wx, h, wz) 67 var col: i64 = 70 + 124*256 + 56*65536 // grass 68 if h > 40 { col = 122 + 112*256 + 86*65536 } // rock 69 if h > 170 { col = 224 + 227*256 + 233*65536 } // snow 70 let jit: i64 = tm_hash3(gx, gz, 5) % 22 71 col = col + jit + jit*256 + jit*65536 72 tm_vcol(vi, col) 73 gx = gx + 1 74 } 75 gz = gz + 1 76 } 77 var qz: i64 = 0 78 while qz < TN-1 { 79 var qx: i64 = 0 80 while qx < TN-1 { 81 let v0: i64 = terrFrom + qz*TN + qx 82 tm_quad(v0, v0+1, v0+TN+1, v0+TN, 0) 83 qx = qx + 1 84 } 85 qz = qz + 1 86 } 87 let terrTo: i64 = tm_nv() 88 hw(" terrain: verts="); pn(terrTo-terrFrom); hw("\n" as *u8) 89 90 // ---- citizen 4 (placed EARLY so unified normals exclude the being): three simple trees ---- 91 let treesFrom: i64 = tm_nv() 92 let tx: *i64 = sys_mmap(3*8) as *i64 93 let tz: *i64 = sys_mmap(3*8) as *i64 94 tx[0]=0-2500; tz[0]=0-1200 95 tx[1]=2450; tz[1]=1500 96 tx[2]=650; tz[2]=0-2400 97 var t: i64 = 0 98 while t < 3 { 99 let th: i64 = terr_h(tx[t], tz[t]) 100 let trFrom: i64 = tm_nv() 101 tm_cube(tx[t], th+170, tz[t], 55, 96+72*256+46*65536) 102 tm_pyramid(tx[t], th+560, tz[t], 300, 42+96*256+40*65536) 103 paint(trFrom, trFrom+8, 96+72*256+46*65536) 104 paint(trFrom+8, tm_nv(), 42+96*256+40*65536) 105 t = t + 1 106 } 107 hw(" trees: verts="); pn(tm_nv()-treesFrom); hw("\n" as *u8) 108 // unified face-normals for bunny + terrain + trees NOW (the being keeps its SDF-GRADIENT normals, added after) 109 tm_compute_normals() 110 111 // ---- citizen 3: the holistic BEING, walk-posed, polygonized IN PLACE at world coords ---- 112 atlas_build(0) 113 atlas_pose(1) // mid-stride 114 let bx: i64 = 0-900 115 let bz: i64 = 620 116 let ground: i64 = terr_h(bx, bz) 117 let grid: *i64 = sys_mmap((GXb+1)*(GYb+1)*(GZb+1)*8) as *i64 118 var i: i64 = 0 119 while i <= GXb { var jj: i64=0; while jj<=GYb { var k: i64=0; while k<=GZb { grid[(i*(GYb+1)+jj)*(GZb+1)+k]=ba_sdf(0-288+i*GCb, 0-990+jj*GCb, 0-180+k*GCb, 1); k=k+1 } jj=jj+1 } i=i+1 } 120 let beingFrom: i64 = tm_nv() 121 surface_nets(grid, GXb, GYb, GZb, bx-288, ground+900-990, bz-180, GCb, 0, 216+172*256+152*65536) 122 let beingTo: i64 = tm_nv() 123 // per-part colours (query at BEING-LOCAL coords: local = world - (bx, ground+900, bz)) 124 let vp: *i64 = sys_mmap(24) as *i64 125 i = beingFrom 126 while i < beingTo { 127 tm_vpos(i, vp) 128 let kk: i64 = ba_nearest(vp[0]-bx, vp[1]-(ground+900), vp[2]-bz, 1) 129 if kk >= 0 { tm_vcol(i, ba_part_color(kk)) } else { tm_vcol(i, 216+172*256+152*65536) } 130 i = i + 1 131 } 132 hw(" being: verts="); pn(beingTo-beingFrom); hw(" at ground="); pn(ground); hw(" TOTAL verts="); pn(tm_nv()); hw(" tris="); pn(tm_nt()); hw(" ovf="); pn(tm_ovf()); hw("\n" as *u8) 133 134 // frame the world: shift the whole scene down so the camera (looking +z about the origin) centres it 135 tm_place(0, tm_nv(), 0, 0-650, 0, 1000) 136 // ★Q1+Q2 (quality ladder 2026-07-12): multi-octave albedo texture + a world-space sun with a baked shadow map 137 tm_set_sun(340, 780, 0-300) 138 tm_shadow_bake() 139 tm_set_tex(70) 140 tm_set_shadow(1) 141 142 // ---- T0 composition ---- 143 var t0: i64 = 0 144 if okB == 1 { if bunnyTo == 35947 { if beingTo-beingFrom > 3000 { if tm_ovf() == 0 { t0 = 1 } } } } 145 if t0 == 1 { hw("T0 PASS one buffer holds all citizens (bunny 35947 exact + being + terrain + trees, no overflow)\n" as *u8) } 146 else { fails=fails+1; hw("T0 FAIL\n" as *u8) } 147 148 // ---- T1 the unified render ---- 149 let npx: i64 = W*H 150 let hero: *i64 = sys_mmap(npx*8) as *i64 151 clearfb(hero, npx, BG) 152 trimesh_render_aa(hero, W, H, 620, 0-330, 6600, 520, 2) 153 let fil: i64 = filled(hero, npx, BG) 154 hw(" hero filled="); pn(fil); hw("\n" as *u8) 155 var t1: i64 = 0 156 if fil > npx/3 { t1 = 1 } 157 if t1 == 1 { hw("T1 PASS ONE renderer draws the whole world (SSAA hero frame)\n" as *u8) } 158 else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 159 160 // ---- T2 CAUSAL visibility: removing a citizen changes the image ---- 161 let zb: *i64 = sys_mmap(npx*8) as *i64 162 let base: *i64 = sys_mmap(npx*8) as *i64 163 clearfb(base, npx, BG); trimesh_zclear(zb, npx); trimesh_render(base, zb, W, H, 620, 0-330, 6600, 520, 2) 164 tm_place(beingFrom, beingTo, 0, 0-100000, 0, 1000) // banish the being 165 let nob: *i64 = sys_mmap(npx*8) as *i64 166 clearfb(nob, npx, BG); trimesh_zclear(zb, npx); trimesh_render(nob, zb, W, H, 620, 0-330, 6600, 520, 2) 167 let dBeing: i64 = imdiff(base, nob, npx) 168 tm_place(beingFrom, beingTo, 0, 100000, 0, 1000) // restore 169 tm_place(0, bunnyTo, 0, 0-100000, 0, 1000) // banish the bunny 170 clearfb(nob, npx, BG); trimesh_zclear(zb, npx); trimesh_render(nob, zb, W, H, 620, 0-330, 6600, 520, 2) 171 let dBunny: i64 = imdiff(base, nob, npx) 172 tm_place(0, bunnyTo, 0, 100000, 0, 1000) // restore 173 hw(" causal visibility: being-removal diff="); pn(dBeing); hw(" bunny-removal diff="); pn(dBunny); hw("\n" as *u8) 174 var t2: i64 = 0 175 if dBeing > 800 { if dBunny > 800 { t2 = 1 } } 176 if t2 == 1 { hw("T2 PASS every citizen VISIBLY contributes (removal changes the frame) -- a real shared scene\n" as *u8) } 177 else { fails=fails+1; hw("T2 FAIL\n" as *u8) } 178 179 // ---- T3 determinism ---- 180 let re: *i64 = sys_mmap(npx*8) as *i64 181 clearfb(re, npx, BG); trimesh_zclear(zb, npx); trimesh_render(re, zb, W, H, 620, 0-330, 6600, 520, 2) 182 var dd: i64 = 0; i = 0 183 while i < npx { if re[i]!=base[i] { dd=dd+1 } i=i+1 } 184 var t3: i64 = 0 185 if dd == 0 { t3 = 1 } 186 if t3 == 1 { hw("T3 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T3 FAIL diff="); pn(dd); hw("\n" as *u8) } 187 188 // ---- T3b Q1+Q2 QUALITY, organ-checked: shadows darken (causal) and the photoreal judge must score HIGHER ---- 189 let fbOn: *i64 = sys_mmap(npx*8) as *i64 190 let fbOff: *i64 = sys_mmap(npx*8) as *i64 191 clearfb(fbOn, npx, BG) 192 trimesh_render_aa(fbOn, W, H, 620, 0-330, 6600, 520, 2) 193 tm_set_shadow(0) 194 clearfb(fbOff, npx, BG) 195 trimesh_render_aa(fbOff, W, H, 620, 0-330, 6600, 520, 2) 196 tm_set_shadow(1) 197 var darkC: i64 = 0 198 var brightC: i64 = 0 199 var qi: i64 = 0 200 while qi < npx { 201 let lOn: i64 = (fbOn[qi]&255) + ((fbOn[qi]>>8)&255) + ((fbOn[qi]>>16)&255) 202 let lOff: i64 = (fbOff[qi]&255) + ((fbOff[qi]>>8)&255) + ((fbOff[qi]>>16)&255) 203 if lOn + 26 < lOff { darkC = darkC + 1 } 204 if lOff + 26 < lOn { brightC = brightC + 1 } 205 qi = qi + 1 206 } 207 hw(" shadows: darkened px="); pn(darkC); hw(" brightened px="); pn(brightC); hw("\n" as *u8) 208 var t3b: i64 = 0 209 if darkC > 900 { if brightC*6 < darkC { t3b = 1 } } 210 if t3b == 1 { hw("T3b PASS Q2 shadows are CAUSAL geometry darkening (sun-blocked fragments darken; nothing brightens)\n" as *u8) } 211 else { fails=fails+1; hw("T3b FAIL\n" as *u8) } 212 // the ORGAN must move: ns_assess on the clay render vs the Q1+Q2 render 213 tm_set_tex(0) 214 tm_set_shadow(0) 215 let fbClay: *i64 = sys_mmap(npx*8) as *i64 216 clearfb(fbClay, npx, BG) 217 trimesh_render_aa(fbClay, W, H, 620, 0-330, 6600, 520, 2) 218 tm_set_tex(70) 219 tm_set_shadow(1) 220 let nbuf1: *i64 = sys_mmap(npx*8) as *i64 221 let nbuf2: *i64 = sys_mmap(npx*8) as *i64 222 let no5: *i64 = sys_mmap(64) as *i64 223 let lvClay: i64 = ns_assess(fbClay, W, H, nbuf1, nbuf2, no5) 224 let lvQ: i64 = ns_assess(fbOn, W, H, nbuf1, nbuf2, no5) 225 hw(" ORGAN (ns_assess): clay LEVEL="); pn(lvClay); hw(" -> Q1+Q2 LEVEL="); pn(lvQ); hw("\n" as *u8) 226 var t3c: i64 = 0 227 if lvQ > lvClay { t3c = 1 } 228 if t3c == 1 { hw("T3c PASS the photoreal ORGAN scores the quality render HIGHER (the QA-loop contract: organs move, not prose)\n" as *u8) } 229 else { fails=fails+1; hw("T3c FAIL organ did not move\n" as *u8) } 230 231 // ---- T4 evidence: hero (AA) + a second angle ---- 232 let GW: i64 = W*2 233 let gal: *i64 = sys_mmap(GW*H*8) as *i64 234 clearfb(gal, GW*H, BG) 235 var y: i64 = 0 236 while y < H { var x: i64=0; while x<W { gal[y*GW+x]=hero[y*W+x]; x=x+1 } y=y+1 } 237 let v2: *i64 = sys_mmap(npx*8) as *i64 238 clearfb(v2, npx, BG) 239 trimesh_render_aa(v2, W, H, 0-700, 0-300, 5400, 520, 2) 240 y = 0 241 while y < H { var x: i64=0; while x<W { gal[y*GW+W+x]=v2[y*W+x]; x=x+1 } y=y+1 } 242 write_png(gal, GW, H, "knowledge/nx_engine_scene.png" as *u8) 243 hw("T4 evidence -> knowledge/nx_engine_scene.png (hero + second angle)\n" as *u8) 244 245 if fails == 0 { hw("ENGINE-SCENE-GATE GREEN -- convergence rung 1: procedural world + walk-posed holistic being + Stanford bunny + prop citizens in ONE buffer through ONE z-buffered renderer (SSAA), causally verified. The organs are no longer islands. Residual: cluster LOD/streaming (Nanite direction), dynamic GI on the scene (Lumen direction), full-rig humans (MetaHuman direction).\n" as *u8); sys_exit(0); return 0 } 246 hw("ENGINE-SCENE-GATE RED fails="); pn(fails); hw("\n" as *u8) 247 sys_exit(1) 248 return 1 249}