code wiki / (root) / nx_normal_encoding_contract.nx

nx_normal_encoding_contract.nx source

↩ module page · 32 lines · 1396 B

1// Shared byte transfer contract used by the existing baker and renderer. 2const NTB_N_MID: i64 = 128 3const NTB_N_SCALE: i64 = 127 4const NTB_GREY_MAX: i64 = 255 5 6// Explicit normal-map transfer contracts; IDs are wire semantics, not inferred from pixels. 7// Numerators retain exact RGB8 quantization. Normalize the decoded vector after XY scale 8// and tangent-frame transformation; this decoder does not claim unit length or provenance. 9const NTB_NORMAL_ENCODING_NATIVE_MID_SCALE: i64 = 1 10const NTB_NORMAL_ENCODING_GLTF_UNORM: i64 = 2 11func ntb_normal_decode_rgb8(encoding:i64,r:i64,g:i64,b:i64,out3:*i64)->i64 { 12 if (out3 as i64)==0{return 0-1} 13 if r<0||r>NTB_GREY_MAX||g<0||g>NTB_GREY_MAX||b<0||b>NTB_GREY_MAX{return 0-2} 14 if encoding==NTB_NORMAL_ENCODING_NATIVE_MID_SCALE{ 15 out3[0]=r-NTB_N_MID;out3[1]=g-NTB_N_MID;out3[2]=b-NTB_N_MID 16 return NTB_N_SCALE 17 } 18 if encoding==NTB_NORMAL_ENCODING_GLTF_UNORM{ 19 out3[0]=2*r-NTB_GREY_MAX;out3[1]=2*g-NTB_GREY_MAX;out3[2]=2*b-NTB_GREY_MAX 20 return NTB_GREY_MAX 21 } 22 return 0-3 23} 24 25// Descriptor admission: absent preserves legacy; explicit unknown is never legacy. 26func ntb_normal_encoding_admit(declared:i64,encoding:i64)->i64{ 27 if declared==0{if encoding==0{return 0};return 0-1} 28 if declared!=1{return 0-1} 29 if encoding==NTB_NORMAL_ENCODING_NATIVE_MID_SCALE{return encoding} 30 if encoding==NTB_NORMAL_ENCODING_GLTF_UNORM{return encoding} 31 return 0-1 32}