pbrHelperFunctions.fx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Constants
  2. #define RECIPROCAL_PI2 0.15915494
  3. #define RECIPROCAL_PI 0.31830988618
  4. // AlphaG epsilon to avoid numerical issues
  5. #define MINIMUMVARIANCE 0.0005
  6. float convertRoughnessToAverageSlope(float roughness)
  7. {
  8. // Calculate AlphaG as square of roughness (add epsilon to avoid numerical issues)
  9. return square(roughness) + MINIMUMVARIANCE;
  10. }
  11. float fresnelGrazingReflectance(float reflectance0) {
  12. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  13. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
  14. float reflectance90 = saturate(reflectance0 * 25.0);
  15. return reflectance90;
  16. }
  17. vec2 getAARoughnessFactors(vec3 normalVector) {
  18. #ifdef SPECULARAA
  19. vec3 nDfdx = dFdx(normalVector.xyz);
  20. vec3 nDfdy = dFdy(normalVector.xyz);
  21. float slopeSquare = max(dot(nDfdx, nDfdx), dot(nDfdy, nDfdy));
  22. // Vive analytical lights roughness factor.
  23. float geometricRoughnessFactor = pow(saturate(slopeSquare), 0.333);
  24. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  25. float geometricAlphaGFactor = sqrt(slopeSquare);
  26. // BJS factor.
  27. geometricAlphaGFactor *= 0.75;
  28. return vec2(geometricRoughnessFactor, geometricAlphaGFactor);
  29. #else
  30. return vec2(0.);
  31. #endif
  32. }
  33. #ifdef ANISOTROPIC
  34. // Aniso parameter remapping
  35. // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24
  36. vec2 getAnisotropicRoughness(float alphaG, float anisotropy) {
  37. float alphaT = max(alphaG * (1.0 + anisotropy), MINIMUMVARIANCE);
  38. float alphaB = max(alphaG * (1.0 - anisotropy), MINIMUMVARIANCE);
  39. return vec2(alphaT, alphaB);
  40. }
  41. // Aniso Bent Normals
  42. // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far
  43. vec3 getAnisotropicBentNormals(const vec3 T, const vec3 B, const vec3 N, const vec3 V, float anisotropy) {
  44. vec3 anisotropicFrameDirection = anisotropy >= 0.0 ? B : T;
  45. vec3 anisotropicFrameTangent = cross(normalize(anisotropicFrameDirection), V);
  46. vec3 anisotropicFrameNormal = cross(anisotropicFrameTangent, anisotropicFrameDirection);
  47. vec3 anisotropicNormal = normalize(mix(N, anisotropicFrameNormal, abs(anisotropy)));
  48. return anisotropicNormal;
  49. // should we also do http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf page 80 ?
  50. }
  51. #endif
  52. #if defined(CLEARCOAT) || defined(SS_REFRACTION)
  53. // From beer lambert law I1/I0 = e −α′lc
  54. // c is considered included in alpha
  55. // https://blog.selfshadow.com/publications/s2017-shading-course/drobot/s2017_pbs_multilayered.pdf page 47
  56. vec3 cocaLambert(vec3 alpha, float distance) {
  57. return exp(-alpha * distance);
  58. }
  59. // where L on a thin constant size layer can be (d * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))
  60. vec3 cocaLambert(float NdotVRefract, float NdotLRefract, vec3 alpha, float thickness) {
  61. return cocaLambert(alpha, (thickness * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))));
  62. }
  63. // From beerLambert Solves what alpha should be for a given result at a known distance.
  64. vec3 computeColorAtDistanceInMedia(vec3 color, float distance) {
  65. return -log(color) / distance;
  66. }
  67. vec3 computeClearCoatAbsorption(float NdotVRefract, float NdotLRefract, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
  68. vec3 clearCoatAbsorption = mix(vec3(1.0),
  69. cocaLambert(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness),
  70. clearCoatIntensity);
  71. return clearCoatAbsorption;
  72. }
  73. #endif
  74. // ___________________________________________________________________________________
  75. //
  76. // LEGACY
  77. // ___________________________________________________________________________________
  78. #ifdef MICROSURFACEAUTOMATIC
  79. float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  80. {
  81. const float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  82. float reflectivityLuminance = getLuminance(reflectivityColor);
  83. float reflectivityLuma = sqrt(reflectivityLuminance);
  84. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  85. return microSurface;
  86. }
  87. #endif