pbrBRDFFunctions.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Constants
  2. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  3. // ______________________________________________________________________
  4. //
  5. // BRDF LOOKUP
  6. // ______________________________________________________________________
  7. #ifdef MS_BRDF_ENERGY_CONSERVATION
  8. // http://www.jcgt.org/published/0008/01/03/
  9. // http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf
  10. vec3 getEnergyConservationFactor(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
  11. return 1.0 + specularEnvironmentR0 * (1.0 / environmentBrdf.y - 1.0);
  12. }
  13. #endif
  14. #ifdef ENVIRONMENTBRDF
  15. vec2 getBRDFLookup(float NdotV, float perceptualRoughness, sampler2D brdfSampler) {
  16. // Indexed on cos(theta) and roughness
  17. vec2 UV = vec2(NdotV, perceptualRoughness);
  18. // We can find the scale and offset to apply to the specular value.
  19. vec2 brdfLookup = texture2D(brdfSampler, UV).xy;
  20. return brdfLookup;
  21. }
  22. vec3 getReflectanceFromBRDFLookup(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
  23. #ifdef BRDF_V_HEIGHT_CORRELATED
  24. vec3 reflectance = mix(environmentBrdf.xxx, environmentBrdf.yyy, specularEnvironmentR0);
  25. #else
  26. vec3 reflectance = specularEnvironmentR0 * environmentBrdf.x + environmentBrdf.y;
  27. #endif
  28. return reflectance;
  29. }
  30. #endif
  31. #if !defined(ENVIRONMENTBRDF) || defined(REFLECTIONMAP_SKYBOX) || defined(ALPHAFRESNEL)
  32. vec3 getReflectanceFromAnalyticalBRDFLookup_Jones(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  33. {
  34. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  35. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  36. return reflectance0 + weight * (reflectance90 - reflectance0) * pow5(saturate(1.0 - VdotN));
  37. }
  38. #endif
  39. #if defined(SHEEN) && defined(REFLECTION)
  40. /**
  41. * Special thanks to @romainguy for all the support :-)
  42. * Analytical approximation of the pre-filtered DFG terms for the cloth shading
  43. * model. This approximation is based on the Estevez & Kulla distribution term
  44. * ("Charlie" sheen) and the Neubelt visibility term. See brdf.fs for more
  45. * details.
  46. */
  47. vec2 getCharlieSheenAnalyticalBRDFLookup_RomainGuy(float NoV, float roughness) {
  48. const vec3 c0 = vec3(0.95, 1250.0, 0.0095);
  49. const vec4 c1 = vec4(0.04, 0.2, 0.3, 0.2);
  50. float a = 1.0 - NoV;
  51. float b = 1.0 - roughness;
  52. float n = pow(c1.x + a, 64.0);
  53. float e = b - c0.x;
  54. float g = exp2(-(e * e) * c0.y);
  55. float f = b + c1.y;
  56. float a2 = a * a;
  57. float a3 = a2 * a;
  58. float c = n * g + c1.z * (a + c1.w) * roughness + f * f * a3 * a3 * a2;
  59. float r = min(c, 18.0);
  60. return vec2(r, r * c0.z);
  61. }
  62. vec3 getSheenReflectanceFromBRDFLookup(const vec3 reflectance0, float NdotV, float sheenAlphaG) {
  63. vec2 environmentSheenBrdf = getCharlieSheenAnalyticalBRDFLookup_RomainGuy(NdotV, sheenAlphaG);
  64. vec3 reflectance = reflectance0 * environmentSheenBrdf.x + environmentSheenBrdf.y;
  65. return reflectance;
  66. }
  67. #endif
  68. // ______________________________________________________________________
  69. //
  70. // Schlick/Fresnel
  71. // ______________________________________________________________________
  72. // Schlick's approximation for R0 (Fresnel Reflectance Values)
  73. // Keep for references
  74. // vec3 getR0fromAirToSurfaceIOR(vec3 ior1) {
  75. // return getR0fromIOR(ior1, vec3(1.0));
  76. // }
  77. // vec3 getR0fromIOR(vec3 ior1, vec3 ior2) {
  78. // vec3 t = (ior1 - ior2) / (ior1 + ior2);
  79. // return t * t;
  80. // }
  81. // vec3 getIORfromAirToSurfaceR0(vec3 f0) {
  82. // vec3 s = sqrt(f0);
  83. // return (1.0 + s) / (1.0 - s);
  84. // }
  85. // f0 Remapping due to layers
  86. // vec3 getR0RemappedForClearCoat(vec3 f0, vec3 clearCoatF0) {
  87. // vec3 iorBase = getIORfromAirToSurfaceR0(f0);
  88. // vec3 clearCoatIor = getIORfromAirToSurfaceR0(clearCoatF0);
  89. // return getR0fromIOR(iorBase, clearCoatIor);
  90. // }
  91. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  92. {
  93. return reflectance0 + (reflectance90 - reflectance0) * pow5(1.0 - VdotH);
  94. }
  95. float fresnelSchlickGGX(float VdotH, float reflectance0, float reflectance90)
  96. {
  97. return reflectance0 + (reflectance90 - reflectance0) * pow5(1.0 - VdotH);
  98. }
  99. #ifdef CLEARCOAT
  100. // Knowing ior clear coat is fix for the material
  101. // Solving iorbase = 1 + sqrt(fo) / (1 - sqrt(fo)) and f0base = square((iorbase - iorclearcoat) / (iorbase - iorclearcoat))
  102. // provide f0base = square(A + B * sqrt(fo)) / (B + A * sqrt(fo))
  103. // where A = 1 - iorclearcoat
  104. // and B = 1 + iorclearcoat
  105. vec3 getR0RemappedForClearCoat(vec3 f0) {
  106. #ifdef CLEARCOAT_DEFAULTIOR
  107. #ifdef MOBILE
  108. return saturate(f0 * (f0 * 0.526868 + 0.529324) - 0.0482256);
  109. #else
  110. return saturate(f0 * (f0 * (0.941892 - 0.263008 * f0) + 0.346479) - 0.0285998);
  111. #endif
  112. #else
  113. vec3 s = sqrt(f0);
  114. vec3 t = (vClearCoatRefractionParams.z + vClearCoatRefractionParams.w * s) / (vClearCoatRefractionParams.w + vClearCoatRefractionParams.z * s);
  115. return t * t;
  116. #endif
  117. }
  118. #endif
  119. // ______________________________________________________________________
  120. //
  121. // Distribution
  122. // ______________________________________________________________________
  123. // Trowbridge-Reitz (GGX)
  124. // Generalised Trowbridge-Reitz with gamma power=2.0
  125. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  126. {
  127. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  128. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  129. // a tangent (gradient) closer to the macrosurface than this slope.
  130. float a2 = square(alphaG);
  131. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  132. return a2 / (PI * d * d);
  133. }
  134. #ifdef SHEEN
  135. // https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
  136. float normalDistributionFunction_CharlieSheen(float NdotH, float alphaG)
  137. {
  138. float invR = 1. / alphaG;
  139. float cos2h = NdotH * NdotH;
  140. float sin2h = 1. - cos2h;
  141. return (2. + invR) * pow(sin2h, invR * .5) / (2. * PI);
  142. }
  143. #endif
  144. #ifdef ANISOTROPIC
  145. // GGX Distribution Anisotropic
  146. // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf Addenda
  147. float normalDistributionFunction_BurleyGGX_Anisotropic(float NdotH, float TdotH, float BdotH, const vec2 alphaTB) {
  148. float a2 = alphaTB.x * alphaTB.y;
  149. vec3 v = vec3(alphaTB.y * TdotH, alphaTB.x * BdotH, a2 * NdotH);
  150. float v2 = dot(v, v);
  151. float w2 = a2 / v2;
  152. return a2 * w2 * w2 * RECIPROCAL_PI;
  153. }
  154. #endif
  155. // ______________________________________________________________________
  156. //
  157. // Visibility/Geometry
  158. // ______________________________________________________________________
  159. #ifdef BRDF_V_HEIGHT_CORRELATED
  160. // GGX Mask/Shadowing Isotropic
  161. // Heitz http://jcgt.org/published/0003/02/03/paper.pdf
  162. // https://twvideo01.ubm-us.net/o1/vault/gdc2017/Presentations/Hammon_Earl_PBR_Diffuse_Lighting.pdf
  163. float smithVisibility_GGXCorrelated(float NdotL, float NdotV, float alphaG) {
  164. #ifdef MOBILE
  165. // Appply simplification as all squared root terms are below 1 and squared
  166. float GGXV = NdotL * (NdotV * (1.0 - alphaG) + alphaG);
  167. float GGXL = NdotV * (NdotL * (1.0 - alphaG) + alphaG);
  168. return 0.5 / (GGXV + GGXL);
  169. #else
  170. float a2 = alphaG * alphaG;
  171. float GGXV = NdotL * sqrt(NdotV * (NdotV - a2 * NdotV) + a2);
  172. float GGXL = NdotV * sqrt(NdotL * (NdotL - a2 * NdotL) + a2);
  173. return 0.5 / (GGXV + GGXL);
  174. #endif
  175. }
  176. #else
  177. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  178. // Keep for references
  179. // float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  180. // {
  181. // float tanSquared = (1.0 - dot * dot) / (dot * dot);
  182. // return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  183. // }
  184. // float smithVisibility_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  185. // {
  186. // float visibility = smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  187. // visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integrated in visibility to avoid issues when visibility function changes.
  188. // return visibility;
  189. // }
  190. // From smithVisibilityG1_TrowbridgeReitzGGX * dot / dot to cancel the cook
  191. // torrance denominator :-)
  192. float smithVisibilityG1_TrowbridgeReitzGGXFast(float dot, float alphaG)
  193. {
  194. #ifdef MOBILE
  195. // Appply simplification as all squared root terms are below 1 and squared
  196. return 1.0 / (dot + alphaG + (1.0 - alphaG) * dot ));
  197. #else
  198. float alphaSquared = alphaG * alphaG;
  199. return 1.0 / (dot + sqrt(alphaSquared + (1.0 - alphaSquared) * dot * dot));
  200. #endif
  201. }
  202. float smithVisibility_TrowbridgeReitzGGXFast(float NdotL, float NdotV, float alphaG)
  203. {
  204. float visibility = smithVisibilityG1_TrowbridgeReitzGGXFast(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGXFast(NdotV, alphaG);
  205. // No Cook Torance Denominator as it is canceled out in the previous form
  206. return visibility;
  207. }
  208. #endif
  209. #ifdef ANISOTROPIC
  210. // GGX Mask/Shadowing Anisotropic
  211. // Heitz http://jcgt.org/published/0003/02/03/paper.pdf
  212. float smithVisibility_GGXCorrelated_Anisotropic(float NdotL, float NdotV, float TdotV, float BdotV, float TdotL, float BdotL, const vec2 alphaTB) {
  213. float lambdaV = NdotL * length(vec3(alphaTB.x * TdotV, alphaTB.y * BdotV, NdotV));
  214. float lambdaL = NdotV * length(vec3(alphaTB.x * TdotL, alphaTB.y * BdotL, NdotL));
  215. float v = 0.5 / (lambdaV + lambdaL);
  216. return v;
  217. }
  218. #endif
  219. #ifdef CLEARCOAT
  220. float visibility_Kelemen(float VdotH) {
  221. // Simplified form integration the cook torrance denminator.
  222. // Expanded is nl * nv / vh2 which factor with 1 / (4 * nl * nv)
  223. // giving 1 / (4 * vh2))
  224. return 0.25 / (VdotH * VdotH);
  225. }
  226. #endif
  227. #ifdef SHEEN
  228. // https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
  229. // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_sheen.pdf
  230. // http://www.cs.utah.edu/~premoze/dbrdf/dBRDF.pdf
  231. float visibility_Ashikhmin(float NdotL, float NdotV)
  232. {
  233. return 1. / (4. * (NdotL + NdotV - NdotL * NdotV));
  234. }
  235. #endif
  236. // ______________________________________________________________________
  237. //
  238. // DiffuseBRDF
  239. // ______________________________________________________________________
  240. // Disney diffuse term
  241. // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
  242. // Page 14
  243. float diffuseBRDF_Burley(float NdotL, float NdotV, float VdotH, float roughness) {
  244. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  245. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  246. float diffuseFresnelNV = pow5(saturateEps(1.0 - NdotL));
  247. float diffuseFresnelNL = pow5(saturateEps(1.0 - NdotV));
  248. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  249. float fresnel =
  250. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  251. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  252. return fresnel / PI;
  253. }