pbrFunctions.fx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Constants
  2. #define RECIPROCAL_PI2 0.15915494
  3. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  4. // AlphaG epsilon to avoid numerical issues
  5. #define MINIMUMVARIANCE 0.0005
  6. #define CLEARCOATREFLECTANCE90 1.0
  7. float convertRoughnessToAverageSlope(float roughness)
  8. {
  9. // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
  10. return square(roughness) + MINIMUMVARIANCE;
  11. }
  12. vec2 getAARoughnessFactors(vec3 normalVector) {
  13. #ifdef SPECULARAA
  14. vec3 nDfdx = dFdx(normalVector.xyz);
  15. vec3 nDfdy = dFdy(normalVector.xyz);
  16. float slopeSquare = max(dot(nDfdx, nDfdx), dot(nDfdy, nDfdy));
  17. // Vive analytical lights roughness factor.
  18. float geometricRoughnessFactor = pow(clamp(slopeSquare , 0., 1.), 0.333);
  19. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  20. float geometricAlphaGFactor = sqrt(slopeSquare);
  21. // BJS factor.
  22. geometricAlphaGFactor *= 0.75;
  23. return vec2(geometricRoughnessFactor, geometricAlphaGFactor);
  24. #else
  25. return vec2(0.);
  26. #endif
  27. }
  28. // Schlick's approximation for R0 (Fresnel Reflectance Values)
  29. // Keep for references
  30. // vec3 getR0fromAirToSurfaceIOR(vec3 ior1) {
  31. // return getR0fromIOR(ior1, vec3(1.0));
  32. // }
  33. // vec3 getR0fromIOR(vec3 ior1, vec3 ior2) {
  34. // vec3 t = (ior1 - ior2) / (ior1 + ior2);
  35. // return t * t;
  36. // }
  37. // vec3 getIORfromAirToSurfaceR0(vec3 f0) {
  38. // vec3 s = sqrt(f0);
  39. // return (1.0 + s) / (1.0 - s);
  40. // }
  41. // f0 Remapping due to layers
  42. // vec3 getR0RemappedForClearCoat(vec3 f0, vec3 clearCoatF0) {
  43. // vec3 iorBase = getIORfromAirToSurfaceR0(f0);
  44. // vec3 clearCoatIor = getIORfromAirToSurfaceR0(clearCoatF0);
  45. // return getR0fromIOR(iorBase, clearCoatIor);
  46. // }
  47. #ifdef CLEARCOAT
  48. // Knowing ior clear coat is fix for the material
  49. // Solving iorbase = 1 + sqrt(fo) / (1 - sqrt(fo)) and f0base = square((iorbase - iorclearcoat) / (iorbase - iorclearcoat))
  50. // provide f0base = square(A + B * sqrt(fo)) / (B + A * sqrt(fo))
  51. // where A = 1 - iorclearcoat
  52. // and B = 1 + iorclearcoat
  53. vec3 getR0RemappedForClearCoat(vec3 f0) {
  54. #ifdef CLEARCOAT_DEFAULTIOR
  55. #ifdef MOBILE
  56. return clamp(f0 * (f0 * 0.526868 + 0.529324) - 0.0482256, 0., 1.);
  57. #else
  58. return clamp(f0 * (f0 * (0.941892 - 0.263008 * f0) + 0.346479) - 0.0285998, 0., 1.);
  59. #endif
  60. #else
  61. vec3 s = sqrt(f0);
  62. vec3 t = (vClearCoatRefractionParams.z + vClearCoatRefractionParams.w * s) / (vClearCoatRefractionParams.w + vClearCoatRefractionParams.z * s);
  63. return t * t;
  64. #endif
  65. }
  66. #endif
  67. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  68. // Keep for references
  69. // float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  70. // {
  71. // float tanSquared = (1.0 - dot * dot) / (dot * dot);
  72. // return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  73. // }
  74. // float smithVisibility_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  75. // {
  76. // float visibility = smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  77. // visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integrated in visibility to avoid issues when visibility function changes.
  78. // return visibility;
  79. // }
  80. // From smithVisibilityG1_TrowbridgeReitzGGX * dot / dot to cancel the cook
  81. // torrance denominator :-)
  82. float smithVisibilityG1_TrowbridgeReitzGGXFast(float dot, float alphaG)
  83. {
  84. #ifdef MOBILE
  85. // Appply simplification as all squared root terms are below 1 and squared
  86. return 1.0 / (dot + alphaG + (1.0 - alphaG) * dot ));
  87. #else
  88. float alphaSquared = alphaG * alphaG;
  89. return 1.0 / (dot + sqrt(alphaSquared + (1.0 - alphaSquared) * dot * dot));
  90. #endif
  91. }
  92. float smithVisibility_TrowbridgeReitzGGXFast(float NdotL, float NdotV, float alphaG)
  93. {
  94. float visibility = smithVisibilityG1_TrowbridgeReitzGGXFast(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGXFast(NdotV, alphaG);
  95. // No Cook Torance Denominator as it is canceled out in the previous form
  96. return visibility;
  97. }
  98. float kelemenVisibility(float VdotH) {
  99. // Simplified form integration the cook torrance denminator.
  100. // Expanded is nl * nv / vh2 which factor with 1 / (4 * nl * nv)
  101. // giving 1 / (4 * vh2))
  102. return 0.25 / (VdotH * VdotH);
  103. }
  104. // Trowbridge-Reitz (GGX)
  105. // Generalised Trowbridge-Reitz with gamma power=2.0
  106. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  107. {
  108. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  109. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  110. // a tangent (gradient) closer to the macrosurface than this slope.
  111. float a2 = square(alphaG);
  112. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  113. return a2 / (PI * d * d);
  114. }
  115. // Aniso parameter remapping
  116. // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24
  117. vec2 getAnisotropicRoughness(float alphaG, float anisotropy) {
  118. float alphaT = max(alphaG * (1.0 + anisotropy), MINIMUMVARIANCE);
  119. float alphaB = max(alphaG * (1.0 - anisotropy), MINIMUMVARIANCE);
  120. return vec2(alphaT, alphaB);
  121. }
  122. // GGX Distribution Anisotropic
  123. // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf Addenda
  124. float normalDistributionFunction_BurleyGGX_Anisotropic(float NdotH, float TdotH, float BdotH, const vec2 alphaTB) {
  125. float a2 = alphaTB.x * alphaTB.y;
  126. vec3 v = vec3(alphaTB.y * TdotH, alphaTB.x * BdotH, a2 * NdotH);
  127. float v2 = dot(v, v);
  128. float w2 = a2 / v2;
  129. return a2 * w2 * w2 * (1.0 / PI);
  130. }
  131. // GGX Mask/Shadowing Anisotropic
  132. // Heitz http://jcgt.org/published/0003/02/03/paper.pdf
  133. float smithVisibility_GGXCorrelated_Anisotropic(float NdotV, float NdotL, float TdotV, float BdotV, float TdotL, float BdotL, const vec2 alphaTB) {
  134. float lambdaV = NdotL * length(vec3(alphaTB.x * TdotV, alphaTB.y * BdotV, NdotV));
  135. float lambdaL = NdotV * length(vec3(alphaTB.x * TdotL, alphaTB.y * BdotL, NdotL));
  136. float v = 0.5 / (lambdaV + lambdaL);
  137. return v;
  138. }
  139. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  140. {
  141. return reflectance0 + (reflectance90 - reflectance0) * pow(1.0 - VdotH, 5.0);
  142. }
  143. float fresnelSchlickGGX(float VdotH, float reflectance0, float reflectance90)
  144. {
  145. return reflectance0 + (reflectance90 - reflectance0) * pow(1.0 - VdotH, 5.0);
  146. }
  147. vec3 fresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  148. {
  149. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  150. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  151. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  152. }
  153. // From beer lambert law I1/I0 = e −α′lc
  154. // c is considered included in alpha
  155. // https://blog.selfshadow.com/publications/s2017-shading-course/drobot/s2017_pbs_multilayered.pdf page 47
  156. // where L on a thin constant size layer can be (d * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))
  157. vec3 cocaLambert(float NdotVRefract, float NdotLRefract, vec3 alpha, float thickness) {
  158. return exp(alpha * -(thickness * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))));
  159. }
  160. // From beerLambert Solves what alpha should be for a given resutlt at a known distance.
  161. vec3 computeColorAtDistanceInMedia(vec3 color, float distance) {
  162. return -log(color) / distance;
  163. }
  164. // Disney diffuse term
  165. // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
  166. // Page 14
  167. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness) {
  168. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  169. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  170. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  171. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  172. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  173. float fresnel =
  174. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  175. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  176. return fresnel / PI;
  177. }
  178. // Cook Torance Specular computation.
  179. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor) {
  180. roughness = max(roughness, geometricRoughnessFactor);
  181. float alphaG = convertRoughnessToAverageSlope(roughness);
  182. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  183. float visibility = smithVisibility_TrowbridgeReitzGGXFast(NdotL, NdotV, alphaG);
  184. float specTerm = max(0., visibility * distribution);
  185. vec3 fresnel = fresnelSchlickGGX(VdotH, reflectance0, reflectance90);
  186. return fresnel * specTerm;
  187. }
  188. vec3 computeAnisotropicSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float TdotH, float BdotH, float TdotV, float BdotV, float TdotL, float BdotL, float roughness, float anisotropy, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor) {
  189. float alphaG = convertRoughnessToAverageSlope(roughness);
  190. vec2 alphaTB = getAnisotropicRoughness(alphaG, anisotropy);
  191. alphaTB = max(alphaTB, geometricRoughnessFactor * geometricRoughnessFactor);
  192. float distribution = normalDistributionFunction_BurleyGGX_Anisotropic(NdotH, TdotH, BdotH, alphaTB);
  193. float visibility = smithVisibility_GGXCorrelated_Anisotropic(NdotV, NdotL, TdotV, BdotV, TdotL, BdotL, alphaTB);
  194. float specTerm = max(0., visibility * distribution);
  195. vec3 fresnel = fresnelSchlickGGX(VdotH, reflectance0, reflectance90);
  196. return fresnel * specTerm;
  197. }
  198. vec2 computeClearCoatTerm(float NdotH, float VdotH, float clearCoatRoughness, float geometricRoughnessFactor, float clearCoatIntensity) {
  199. clearCoatRoughness = max(clearCoatRoughness, geometricRoughnessFactor);
  200. float alphaG = convertRoughnessToAverageSlope(clearCoatRoughness);
  201. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  202. float visibility = kelemenVisibility(VdotH);
  203. float clearCoatTerm = max(0., visibility * distribution);
  204. float fresnel = fresnelSchlickGGX(VdotH, vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90);
  205. fresnel *= clearCoatIntensity;
  206. return vec2(fresnel * clearCoatTerm, 1.0 - fresnel);
  207. }
  208. vec3 computeClearCoatAbsorption(float NdotVRefract, float NdotLRefract, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
  209. vec3 clearCoatAbsorption = mix(vec3(1.0),
  210. cocaLambert(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness),
  211. clearCoatIntensity);
  212. return clearCoatAbsorption;
  213. }
  214. float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)
  215. {
  216. #if defined(USEPHYSICALLIGHTFALLOFF) || defined(USEGLTFLIGHTFALLOFF)
  217. // At small angle this approximation works.
  218. float lightRoughness = lightRadius / lightDistance;
  219. // Distribution can sum.
  220. float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);
  221. return totalRoughness;
  222. #else
  223. return roughness;
  224. #endif
  225. }
  226. float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  227. {
  228. const float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  229. float reflectivityLuminance = getLuminance(reflectivityColor);
  230. float reflectivityLuma = sqrt(reflectivityLuminance);
  231. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  232. return microSurface;
  233. }
  234. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  235. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
  236. float fresnelGrazingReflectance(float reflectance0) {
  237. float reflectance90 = clamp(reflectance0 * 25.0, 0.0, 1.0);
  238. return reflectance90;
  239. }
  240. // To enable 8 bit textures to be used we need to pack and unpack the LOD
  241. //inverse alpha is used to work around low-alpha bugs in Edge and Firefox
  242. #define UNPACK_LOD(x) (1.0 - x) * 255.0
  243. float getLodFromAlphaG(float cubeMapDimensionPixels, float alphaG, float NdotV) {
  244. float microsurfaceAverageSlope = alphaG;
  245. // Compensate for solid angle change between half-vector measure (Blinn-Phong) and reflected-vector measure (Phong):
  246. // dWr = 4*cos(theta)*dWh,
  247. // where dWr = solid angle (delta omega) in environment incident radiance (reflection-vector) measure;
  248. // where dWh = solid angle (delta omega) in microfacet normal (half-vector) measure;
  249. // so the relationship is proportional to cosine theta = NdotV.
  250. // The constant factor of four is handled elsewhere as part of the scale/offset filter parameters.
  251. microsurfaceAverageSlope *= sqrt(abs(NdotV));
  252. float microsurfaceAverageSlopeTexels = microsurfaceAverageSlope * cubeMapDimensionPixels;
  253. float lod = log2(microsurfaceAverageSlopeTexels);
  254. return lod;
  255. }
  256. float environmentRadianceOcclusion(float ambientOcclusion, float NdotVUnclamped) {
  257. // Best balanced (implementation time vs result vs perf) analytical environment specular occlusion found.
  258. // http://research.tri-ace.com/Data/cedec2011_RealtimePBR_Implementation_e.pptx
  259. float temp = NdotVUnclamped + ambientOcclusion;
  260. return clamp(square(temp) - 1.0 + ambientOcclusion, 0.0, 1.0);
  261. }
  262. float environmentHorizonOcclusion(vec3 view, vec3 normal) {
  263. // http://marmosetco.tumblr.com/post/81245981087
  264. vec3 reflection = reflect(view, normal);
  265. float temp = clamp( 1.0 + 1.1 * dot(reflection, normal), 0.0, 1.0);
  266. return square(temp);
  267. }