Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. float4 cook_torrance
  2. (
  3. in float3 normal,
  4. in float3 viewer,
  5. in float3 light,
  6. uniform int roughness_mode
  7. )
  8. {
  9. // Compute any aliases and intermediary values
  10. // -------------------------------------------
  11. float3 half_vector = normalize( light + viewer );
  12. float NdotL = saturate( dot( normal, light ) );
  13. float NdotH = saturate( dot( normal, half_vector ) );
  14. float NdotV = saturate( dot( normal, viewer ) );
  15. float VdotH = saturate( dot( viewer, half_vector ) );
  16. float r_sq = roughness_value * roughness_value;
  17.  
  18.  
  19.  
  20. // Evaluate the geometric term
  21. // --------------------------------
  22. float geo_numerator = 2.0f * NdotH;
  23. float geo_denominator = VdotH;
  24.  
  25. float geo_b = (geo_numerator * NdotV ) / geo_denominator;
  26. float geo_c = (geo_numerator * NdotL ) / geo_denominator;
  27. float geo = min( 1.0f, min( geo_b, geo_c ) );
  28.  
  29.  
  30.  
  31. // Now evaluate the roughness term
  32. // -------------------------------
  33. float roughness;
  34.  
  35. if( ROUGHNESS_LOOK_UP == roughness_mode )
  36. {
  37. // texture coordinate is:
  38. float2 tc = { NdotH, roughness_value };
  39.  
  40. // Remap the NdotH value to be 0.0-1.0
  41. // instead of -1.0..+1.0
  42. tc.x += 1.0f;
  43. tc.x /= 2.0f;
  44.  
  45. // look up the coefficient from the texture:
  46. roughness = texRoughness.Sample( sampRoughness, tc );
  47. }
  48. if( ROUGHNESS_BECKMANN == roughness_mode )
  49. {
  50. float roughness_a = 1.0f / ( 4.0f * r_sq * pow( NdotH, 4 ) );
  51. float roughness_b = NdotH * NdotH - 1.0f;
  52. float roughness_c = r_sq * NdotH * NdotH;
  53.  
  54. roughness = roughness_a * exp( roughness_b / roughness_c );
  55. }
  56. if( ROUGHNESS_GAUSSIAN == roughness_mode )
  57. {
  58. // This variable could be exposed as a variable
  59. // for the application to control:
  60. float c = 1.0f;
  61. float alpha = acos( dot( normal, half_vector ) );
  62. roughness = c * exp( -( alpha / r_sq ) );
  63. }
  64.  
  65.  
  66.  
  67. // Next evaluate the Fresnel value
  68. // -------------------------------
  69. float fresnel = pow( 1.0f - VdotH, 5.0f );
  70. fresnel *= ( 1.0f - ref_at_norm_incidence );
  71. fresnel += ref_at_norm_incidence;
  72.  
  73.  
  74.  
  75. // Put all the terms together to compute
  76. // the specular term in the equation
  77. // -------------------------------------
  78. float3 Rs_numerator = ( fresnel * geo * roughness );
  79. float Rs_denominator = NdotV * NdotL;
  80. float3 Rs = Rs_numerator/ Rs_denominator;
  81.  
  82.  
  83.  
  84. // Put all the parts together to generate
  85. // the final colour
  86. // --------------------------------------
  87. float3 final = max(0.0f, NdotL) * (cSpecular * Rs + cDiffuse);
  88.  
  89.  
  90.  
  91. // Return the result
  92. // -----------------
  93. return float4( final, 1.0f );
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement