code wiki / _hdl_build / nx_texture_gate.nx

nx_texture_gate.nx source

↩ module page · 95 lines · 5229 B

1// nx_texture_gate.nx -- ★F3 procedural TEXTURE: 3D solid-noise surface detail. Flat uniform colour vs mottled/detailed 2// skin. T1 the textured surface has far more HIGH-FREQUENCY detail (adjacent-pixel variation) than flat. T2 determinism. 3// T3 gallery (skin flat | skin textured | textured close-up). license_tier: ORIGINAL expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_png.nx" 6import "nx_trimesh.nx" 7import "nx_isosurf.nx" 8import "nx_bodyatlas.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 clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 } 13// HIGH-FREQUENCY detail via 2nd difference |2*c - left - right| -- cancels smooth shading gradients, isolates texture 14func detail(fb: *i64, W: i64, H: i64, bg: i64) -> i64 { 15 var s: i64=0; var y: i64=0 16 while y<H { var x: i64=1; while x<W-1 { 17 let l: i64=fb[y*W+x-1]; let c: i64=fb[y*W+x]; let r: i64=fb[y*W+x+1] 18 if l!=bg { if c!=bg { if r!=bg { var d: i64=2*(c&255)-(l&255)-(r&255); if d<0{d=0-d} s=s+d } } } 19 x=x+1 } y=y+1 } 20 return s 21} 22 23const BG: i64 = 24 + 26*256 + 34*65536 24const W: i64 = 340 25const H: i64 = 470 26const GX: i64 = 32 27const GY: i64 = 110 28const GZ: i64 = 20 29const GC: i64 = 18 30 31func mesh_skin() -> i64 { 32 let grid: *i64 = sys_mmap((GX+1)*(GY+1)*(GZ+1)*8) as *i64 33 var i: i64=0 34 while i<=GX { var jj: i64=0; while jj<=GY { var k: i64=0; while k<=GZ { grid[(i*(GY+1)+jj)*(GZ+1)+k]=ba_sdf(0-288+i*GC, 0-990+jj*GC, 0-180+k*GC, 1); k=k+1 } jj=jj+1 } i=i+1 } 35 tm_reset() 36 surface_nets(grid, GX,GY,GZ, 0-288,0-990,0-180, GC, 0, 216+172*256+152*65536) 37 let vp: *i64=sys_mmap(24) as *i64 38 i=0; let nv: i64=tm_nv() 39 while i<nv { tm_vpos(i,vp); let kk: i64=ba_nearest(vp[0],vp[1],vp[2],1); if kk>=0 { tm_vcol(i,ba_part_color(kk)) } else { tm_vcol(i,216+172*256+152*65536) } i=i+1 } 40 return 0 41} 42 43func main() -> i64 { 44 hw("=== nx_texture_gate -- F3 procedural 3D solid-noise texture ===\n" as *u8) 45 var fails: i64 = 0 46 let npx: i64 = W*H 47 let fb: *i64 = sys_mmap(npx*8) as *i64 48 let zb: *i64 = sys_mmap(npx*8) as *i64 49 atlas_build(0) 50 mesh_skin() 51 52 tm_set_spec(0); tm_set_tex(0) 53 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-240, 2500, 520, 2) 54 let flatD: i64 = detail(fb, W, H, BG) 55 tm_set_tex(13) // fine pore-scale mottle 56 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-240, 2500, 520, 2) 57 let texD: i64 = detail(fb, W, H, BG) 58 hw(" surface detail (adjacent-px variation): flat="); pn(flatD); hw(" textured="); pn(texD); hw("\n" as *u8) 59 var t1: i64 = 0 60 if texD*2 > flatD*3 { t1 = 1 } // texture adds >=50% more high-freq detail over the mesh's baseline faceting 61 if t1 == 1 { hw("T1 PASS TEXTURE: procedural noise adds real surface detail ("); pn(texD); hw(" vs flat "); pn(flatD); hw(")\n" as *u8) } 62 else { fails=fails+1; hw("T1 FAIL flat="); pn(flatD); hw(" tex="); pn(texD); hw("\n" as *u8) } 63 64 // T2 determinism 65 let f2: *i64 = sys_mmap(npx*8) as *i64 66 clearfb(f2, npx, BG); trimesh_zclear(zb, npx); trimesh_render(f2, zb, W, H, 2400, 0-240, 2500, 520, 2) 67 var diff: i64=0; var i: i64=0 68 while i<npx { if f2[i]!=fb[i] { diff=diff+1 } i=i+1 } 69 var t2: i64=0 70 if diff==0 { t2=1 } 71 if t2==1 { hw("T2 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T2 FAIL diff="); pn(diff); hw("\n" as *u8) } 72 73 // T3 gallery: flat | textured | textured close-up 74 let GW: i64 = W*3 75 let gal: *i64 = sys_mmap(GW*H*8) as *i64 76 clearfb(gal, GW*H, BG) 77 let cell: *i64 = sys_mmap(npx*8) as *i64 78 tm_set_tex(0); clearfb(cell,npx,BG); trimesh_zclear(zb,npx); trimesh_render(cell,zb,W,H,2400,0-240,2500,520,2) 79 var y: i64=0 80 while y<H { var x: i64=0; while x<W { gal[y*GW+x]=cell[y*W+x]; x=x+1 } y=y+1 } 81 tm_set_tex(13); tm_set_spec(60); clearfb(cell,npx,BG); trimesh_zclear(zb,npx); trimesh_render(cell,zb,W,H,2400,0-240,2500,520,2) 82 y=0 83 while y<H { var x: i64=0; while x<W { gal[y*GW+W+x]=cell[y*W+x]; x=x+1 } y=y+1 } 84 // close-up (smaller camz zooms in on the surface detail) 85 clearfb(cell,npx,BG); trimesh_zclear(zb,npx); trimesh_render(cell,zb,W,H,3200,0-120,1500,520,2) 86 y=0 87 while y<H { var x: i64=0; while x<W { gal[y*GW+2*W+x]=cell[y*W+x]; x=x+1 } y=y+1 } 88 write_png(gal, GW, H, "knowledge/nx_texture.png" as *u8) 89 hw("T3 gallery -> knowledge/nx_texture.png (skin flat | skin textured+sheen | close-up)\n" as *u8) 90 91 if fails == 0 { hw("TEXTURE-GATE GREEN -- F3 procedural 3D solid-noise surface texture (opt-in tm_set_tex, zero-cost off): mottled/detailed skin, not flat clay. Census texture-mapping GAP->PARTIAL. Residual: image textures + UV unwrap (the UV axis stays open).\n" as *u8); sys_exit(0); return 0 } 92 hw("TEXTURE-GATE RED fails="); pn(fails); hw("\n" as *u8) 93 sys_exit(1) 94 return 1 95}