pbrFunctions.fx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Constants
  2. #define RECIPROCAL_PI2 0.15915494
  3. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  4. // PBR CUSTOM CONSTANTS
  5. const float kPi = 3.1415926535897932384626433832795;
  6. const float kRougnhessToAlphaScale = 0.1;
  7. const float kRougnhessToAlphaOffset = 0.29248125;
  8. float Square(float value)
  9. {
  10. return value * value;
  11. }
  12. float getLuminance(vec3 color)
  13. {
  14. return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);
  15. }
  16. float convertRoughnessToAverageSlope(float roughness)
  17. {
  18. // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
  19. const float kMinimumVariance = 0.0005;
  20. float alphaG = Square(roughness) + kMinimumVariance;
  21. return alphaG;
  22. }
  23. // Based on Beckamm roughness to Blinn exponent + http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  24. float getMipMapIndexFromAverageSlope(float maxMipLevel, float alpha)
  25. {
  26. // do not take in account lower mips hence -1... and wait from proper preprocess.
  27. // formula comes from approximation of the mathematical solution.
  28. //float mip = maxMipLevel + kRougnhessToAlphaOffset + 0.5 * log2(alpha);
  29. // In the mean time
  30. // Always [0..1] goes from max mip to min mip in a log2 way.
  31. // Change 5 to nummip below.
  32. // http://www.wolframalpha.com/input/?i=x+in+0..1+plot+(+5+%2B+0.3+%2B+0.1+*+5+*+log2(+(1+-+x)+*+(1+-+x)+%2B+0.0005))
  33. float mip = kRougnhessToAlphaOffset + maxMipLevel + (maxMipLevel * kRougnhessToAlphaScale * log2(alpha));
  34. return clamp(mip, 0., maxMipLevel);
  35. }
  36. float getMipMapIndexFromAverageSlopeWithPMREM(float maxMipLevel, float alphaG)
  37. {
  38. float specularPower = clamp(2. / alphaG - 2., 0.000001, 2048.);
  39. // Based on CubeMapGen for cosine power with 2048 spec default and 0.25 dropoff
  40. return clamp(- 0.5 * log2(specularPower) + 5.5, 0., maxMipLevel);
  41. }
  42. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  43. float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  44. {
  45. float tanSquared = (1.0 - dot * dot) / (dot * dot);
  46. return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  47. }
  48. float smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  49. {
  50. return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  51. }
  52. // Trowbridge-Reitz (GGX)
  53. // Generalised Trowbridge-Reitz with gamma power=2.0
  54. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  55. {
  56. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  57. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  58. // a tangent (gradient) closer to the macrosurface than this slope.
  59. float a2 = Square(alphaG);
  60. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  61. return a2 / (kPi * d * d);
  62. }
  63. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  64. {
  65. return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);
  66. }
  67. vec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  68. {
  69. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  70. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  71. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  72. }
  73. // Cook Torance Specular computation.
  74. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)
  75. {
  76. float alphaG = convertRoughnessToAverageSlope(roughness);
  77. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  78. float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);
  79. visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.
  80. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));
  81. float specTerm = max(0., visibility * distribution) * NdotL;
  82. return fresnel * specTerm * kPi; // TODO: audit pi constants
  83. }
  84. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)
  85. {
  86. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  87. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  88. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  89. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  90. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  91. float diffuseFresnelTerm =
  92. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  93. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  94. return diffuseFresnelTerm * NdotL;
  95. // PI Test
  96. // diffuseFresnelTerm /= kPi;
  97. }
  98. float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)
  99. {
  100. // At small angle this approximation works.
  101. float lightRoughness = lightRadius / lightDistance;
  102. // Distribution can sum.
  103. float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);
  104. return totalRoughness;
  105. }
  106. float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  107. {
  108. float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  109. float reflectivityLuminance = getLuminance(reflectivityColor);
  110. float reflectivityLuma = sqrt(reflectivityLuminance);
  111. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  112. return microSurface;
  113. }
  114. vec3 toLinearSpace(vec3 color)
  115. {
  116. return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));
  117. }
  118. vec3 toGammaSpace(vec3 color)
  119. {
  120. return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
  121. }
  122. float computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range)
  123. {
  124. #ifdef USEPHYSICALLIGHTFALLOFF
  125. float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001));
  126. return lightDistanceFalloff;
  127. #else
  128. float lightFalloff = max(0., 1.0 - length(lightOffset) / range);
  129. return lightFalloff;
  130. #endif
  131. }
  132. #ifdef CAMERATONEMAP
  133. vec3 toneMaps(vec3 color)
  134. {
  135. color = max(color, 0.0);
  136. // TONE MAPPING / EXPOSURE
  137. color.rgb = color.rgb * vCameraInfos.x;
  138. float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately
  139. // PI Test
  140. // tuning *= kPi;
  141. vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper
  142. color.rgb = mix(color.rgb, tonemapped, 1.0);
  143. return color;
  144. }
  145. #endif
  146. #ifdef CAMERACONTRAST
  147. vec4 contrasts(vec4 color)
  148. {
  149. color = clamp(color, 0.0, 1.0);
  150. vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);
  151. float contrast = vCameraInfos.y;
  152. if (contrast < 1.0)
  153. {
  154. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  155. color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);
  156. }
  157. else
  158. {
  159. // Increase contrast: apply simple shoulder-toe high contrast curve
  160. color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);
  161. }
  162. return color;
  163. }
  164. #endif