pbrBRDFFunctions.fx 13 KB

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