Advertisement
Guest User

Pack Normal and Specular into RGB10A2

a guest
Dec 3rd, 2010
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2. float PackIEto10bit(in float I, in float E) {
  3.  
  4.     const float OneOver1023 = 1.0 / 1023.0;
  5.    
  6.     return (floor(I * 31) * 32 + E * 31) * OneOver1023;
  7.    
  8. }//function
  9. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  10. void UnpackIEfrom10bit(in float V, out float I, out float E) {
  11.  
  12.     const float OneOver32 = 1.0 / 32.0;
  13.     const float OneOver31 = 1.0 / 31.0;
  14.    
  15.     float t1;
  16.     float t2;
  17.    
  18.     t1  = V * 1023;
  19.     t2  = floor(t1 * OneOver32);
  20.     I   = t2 * OneOver31;
  21.     E   = (t1 - t2 * 32) * OneOver31;
  22.    
  23. }//function
  24. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25. float4 PackNIEtoRGB10A2(in float3 N, in float I, in float E) {
  26.  
  27.     float4  color;
  28.     float   phi;
  29.  
  30.     // Map E
  31.     E           = saturate(log10(E) - 1);
  32.  
  33.     phi         = atan2(N.x, N.z); // Note! This gives NaN when x = z = 0
  34.    
  35.     color.r     = abs(phi) * OneOverPi;
  36.     color.g     = abs(N.y);
  37.     color.b     = PackIEto10bit(I, E);
  38.    
  39.     float a1    = saturate(sign(phi)) * 0.3333;
  40.     float a2    = saturate(sign(N.y)) * 0.6666;
  41.    
  42.     color.a     = a1 + a2;
  43.    
  44.     return color;
  45.    
  46. }//function
  47. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  48. // R = abs(phi)
  49. // G = abs(Y)
  50. // B = IE
  51. // A = sign(phi) + sign(Y)
  52. float3 UnpackNIEfromRGB10A2(in float4 color, out float I, out float E) {
  53.  
  54.     UnpackIEfrom10bit(color.b, I, E);
  55.    
  56.     // Map E
  57.     E = pow(10, E + 1);
  58.    
  59.     float signPhi   = sign(frac(color.a * 1.9) - 0.5);
  60.     float signY     = sign(color.a - 0.5);
  61.     float phi       = color.r * Pi * signPhi;
  62.     float Y         = color.g * signY;
  63.    
  64.     float2 N;
  65.     sincos(phi, N.x, N.y);
  66.    
  67.     const float threshold = 1021.0 / 1023.0;
  68.    
  69.     if (color.g > threshold) N = 0.0; // bodge up for singularity
  70.  
  71.     float L = sqrt(1.0 - Y * Y);
  72.    
  73.     return float3(N * L, Y);
  74.    
  75. }//function
  76. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement