pbrBRDFFunctions.fx 12 KB

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