code wiki / _hdl_build / nx_char_custom_gate.nx

nx_char_custom_gate.nx source

↩ module page · 70 lines · 3888 B

1// nx_char_custom_gate.nx -- the extended customization layers (operator: body paint / piercings / jewelry, "really 2// layer and change things"). A base + [body-paint + navel piercing + belt] composes; the body paint recolors a 3// body region (green arms) and the piercing+belt add worn geometry. Proves the new LT_BODYPAINT / LT_PIERCING types 4// apply and render. Emits knowledge/synth_custom.png. expect_exit:0 license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_sdfrender.nx" 7import "nx_assetvariant.nx" 8import "nx_body_figure.nx" 9import "nx_char_layers.nx" 10import "nx_png.nx" 11 12func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func 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 } 14 15const CW: i64 = 144 16const CH: i64 = 132 17const CFOCAL: i64 = 150 18 19func c_render(base: i64, fb: *i64) -> i64 { 20 let cy4: i64 = it_cos4096(470); let sy4: i64 = it_sin4096(470) 21 let R: i64 = 5 * 1024 22 let rox: i64 = 0 - sy4 * R / 4096; let roz: i64 = 0 - cy4 * R / 4096 23 var y: i64 = 0 24 while y < CH { 25 let misscol: i64 = (24 + y*30/CH) + (26 + y*28/CH)*256 + (40 + y*26/CH)*65536 26 var x: i64 = 0 27 while x < CW { 28 let ndcx: i64 = (2*x+1-CW)*1024/(2*CFOCAL); let ndcy: i64 = (CH-(2*y+1))*1024/(2*CFOCAL) 29 let rl: i64 = sdf_isqrt(ndcx*ndcx + ndcy*ndcy + 1024*1024) 30 let vdx: i64 = ndcx*1024/rl; let rdy: i64 = ndcy*1024/rl; let vdz: i64 = 1024*1024/rl 31 let rdx: i64 = (cy4*vdx + sy4*vdz)/4096; let rdz: i64 = (0-sy4*vdx + cy4*vdz)/4096 32 fb[y*CW+x] = sdf_shade_ray(base, rox, 0, roz, rdx, rdy, rdz, 200, 176, 152, misscol) 33 x = x + 1 34 } 35 y = y + 1 36 } 37 return 0 38} 39func count_green(fb: *i64, n: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { let col: i64=fb[i]; let r: i64=col&255; let g: i64=(col>>8)&255; let b: i64=(col>>16)&255; if g>r+20 { if g>b+15 { if g>80 { c=c+1 } } } i=i+1 } return c } 40 41func main() -> i64 { 42 hw("=== nx_char_custom_gate -- body paint + piercing + jewelry as layers ===\n" as *u8) 43 let base: i64 = sys_mmap(sdf_bytes()) as i64 44 let fb: *i64 = sys_mmap(CW * CH * 8) as *i64 45 var fails: i64 = 0 46 47 let stack: *i64 = sys_mmap(4 * 2 * 8) as *i64 48 stack[0]=LT_BODYPAINT; stack[1]=1 // green paint on the arms 49 stack[2]=LT_PIERCING; stack[3]=PIERCE_NAVEL // navel stud 50 stack[4]=LT_ACCESSORY; stack[5]=ACC_BELT // metal belt 51 let nl: i64 = 3 52 53 av_build(base, 0, 0, 0, 0, 100) 54 figure_apply(base, FIG_FEM) 55 let np0: *i64 = (base + O_NPART) as *i64 56 let nbase: i64 = np0[0] 57 let nfin: i64 = char_layers_apply(base, nbase, stack, nl) 58 hw(" base="); pn(nbase); hw(" -> "); pn(nfin); hw(" parts (piercing 1 + belt 1 added; body-paint recolors, no new part)\n" as *u8) 59 if nfin == nbase + 2 { hw("T1 PASS piercing + belt geometry added; body-paint applied as a recolor\n" as *u8) } else { fails=fails+1; hw("T1 FAIL part count\n" as *u8) } 60 61 c_render(base, fb) 62 write_png(fb, CW, CH, "knowledge/synth_custom.png" as *u8) 63 let gr: i64 = count_green(fb, CW*CH) 64 hw(" body-paint (green arms) pixels="); pn(gr); hw(" -> knowledge/synth_custom.png\n" as *u8) 65 if gr > 150 { hw("T2 PASS body paint renders on the body region (a real recolor layer)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL body paint not visible\n" as *u8) } 66 67 if fails == 0 { hw("CHAR-CUSTOM-GATE 2/2 GREEN -- body-paint + piercing + jewelry compose as layers; the stack really layers & changes the character\n" as *u8); sys_exit(0); return 0 } 68 hw("CHAR-CUSTOM-GATE RED fails="); pn(fails); hw("\n" as *u8) 69 sys_exit(1); return 1 70}