pbrBlockClearcoat.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. struct clearcoatOutParams
  2. {
  3. vec3 specularEnvironmentR0;
  4. float conservationFactor;
  5. vec3 clearCoatNormalW;
  6. vec2 clearCoatAARoughnessFactors;
  7. float clearCoatIntensity;
  8. float clearCoatRoughness;
  9. #ifdef REFLECTION
  10. vec3 finalClearCoatRadianceScaled;
  11. #endif
  12. #ifdef CLEARCOAT_TINT
  13. vec3 absorption;
  14. float clearCoatNdotVRefract;
  15. vec3 clearCoatColor;
  16. float clearCoatThickness;
  17. #endif
  18. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  19. vec3 energyConservationFactorClearCoat;
  20. #endif
  21. #if DEBUGMODE > 0
  22. mat3 TBNClearCoat;
  23. vec2 clearCoatMapData;
  24. vec4 clearCoatTintMapData;
  25. vec4 environmentClearCoatRadiance;
  26. float clearCoatNdotV;
  27. vec3 clearCoatEnvironmentReflectance;
  28. #endif
  29. };
  30. #ifdef CLEARCOAT
  31. #define inline
  32. void clearcoatBlock(
  33. const in vec3 vPositionW,
  34. const in vec3 geometricNormalW,
  35. const in vec3 viewDirectionW,
  36. const in vec2 vClearCoatParams,
  37. const in vec3 specularEnvironmentR0,
  38. #ifdef CLEARCOAT_TEXTURE
  39. const in vec2 clearCoatMapData,
  40. #endif
  41. #ifdef CLEARCOAT_TINT
  42. const in vec4 vClearCoatTintParams,
  43. const in float clearCoatColorAtDistance,
  44. const in vec4 vClearCoatRefractionParams,
  45. #ifdef CLEARCOAT_TINT_TEXTURE
  46. const in vec4 clearCoatTintMapData,
  47. #endif
  48. #endif
  49. #ifdef CLEARCOAT_BUMP
  50. const in vec2 vClearCoatBumpInfos,
  51. const in vec4 clearCoatBumpMapData,
  52. const in vec2 vClearCoatBumpUV,
  53. #if defined(TANGENT) && defined(NORMAL)
  54. const in mat3 vTBN,
  55. #else
  56. const in vec2 vClearCoatTangentSpaceParams,
  57. #endif
  58. #ifdef OBJECTSPACE_NORMALMAP
  59. const in mat4 normalMatrix,
  60. #endif
  61. #endif
  62. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  63. const in vec3 faceNormal,
  64. #endif
  65. #ifdef REFLECTION
  66. const in vec3 vReflectionMicrosurfaceInfos,
  67. const in vec2 vReflectionInfos,
  68. const in vec3 vReflectionColor,
  69. const in vec4 vLightingIntensity,
  70. #ifdef REFLECTIONMAP_3D
  71. const in samplerCube reflectionSampler,
  72. #else
  73. const in sampler2D reflectionSampler,
  74. #endif
  75. #ifndef LODBASEDMICROSFURACE
  76. #ifdef REFLECTIONMAP_3D
  77. const in samplerCube reflectionSamplerLow,
  78. const in samplerCube reflectionSamplerHigh,
  79. #else
  80. const in sampler2D reflectionSamplerLow,
  81. const in sampler2D reflectionSamplerHigh,
  82. #endif
  83. #endif
  84. #endif
  85. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  86. #ifdef RADIANCEOCCLUSION
  87. const in float ambientMonochrome,
  88. #endif
  89. #endif
  90. out clearcoatOutParams outParams
  91. )
  92. {
  93. // Clear COAT parameters.
  94. float clearCoatIntensity = vClearCoatParams.x;
  95. float clearCoatRoughness = vClearCoatParams.y;
  96. #ifdef CLEARCOAT_TEXTURE
  97. clearCoatIntensity *= clearCoatMapData.x;
  98. clearCoatRoughness *= clearCoatMapData.y;
  99. #if DEBUGMODE > 0
  100. outParams.clearCoatMapData = clearCoatMapData;
  101. #endif
  102. #endif
  103. outParams.clearCoatIntensity = clearCoatIntensity;
  104. outParams.clearCoatRoughness = clearCoatRoughness;
  105. #ifdef CLEARCOAT_TINT
  106. vec3 clearCoatColor = vClearCoatTintParams.rgb;
  107. float clearCoatThickness = vClearCoatTintParams.a;
  108. #ifdef CLEARCOAT_TINT_TEXTURE
  109. clearCoatColor *= clearCoatTintMapData.rgb;
  110. clearCoatThickness *= clearCoatTintMapData.a;
  111. #if DEBUGMODE > 0
  112. outParams.clearCoatTintMapData = clearCoatTintMapData;
  113. #endif
  114. #endif
  115. outParams.clearCoatColor = computeColorAtDistanceInMedia(clearCoatColor, clearCoatColorAtDistance);
  116. outParams.clearCoatThickness = clearCoatThickness;
  117. #endif
  118. // remapping and linearization of clear coat roughness
  119. // Let s see how it ends up in gltf
  120. // clearCoatRoughness = mix(0.089, 0.6, clearCoatRoughness);
  121. // Remap F0 to account for the change of interface within the material.
  122. vec3 specularEnvironmentR0Updated = getR0RemappedForClearCoat(specularEnvironmentR0);
  123. outParams.specularEnvironmentR0 = mix(specularEnvironmentR0, specularEnvironmentR0Updated, clearCoatIntensity);
  124. // Needs to use the geometric normal before bump for this.
  125. vec3 clearCoatNormalW = geometricNormalW;
  126. #ifdef CLEARCOAT_BUMP
  127. #ifdef NORMALXYSCALE
  128. float clearCoatNormalScale = 1.0;
  129. #else
  130. float clearCoatNormalScale = vClearCoatBumpInfos.y;
  131. #endif
  132. #if defined(TANGENT) && defined(NORMAL)
  133. mat3 TBNClearCoat = vTBN;
  134. #else
  135. mat3 TBNClearCoat = cotangent_frame(clearCoatNormalW * clearCoatNormalScale, vPositionW, vClearCoatBumpUV, vClearCoatTangentSpaceParams);
  136. #endif
  137. #if DEBUGMODE > 0
  138. outParams.TBNClearCoat = TBNClearCoat;
  139. #endif
  140. #ifdef OBJECTSPACE_NORMALMAP
  141. clearCoatNormalW = normalize(clearCoatBumpMapData.xyz * 2.0 - 1.0);
  142. clearCoatNormalW = normalize(mat3(normalMatrix) * clearCoatNormalW);
  143. #else
  144. clearCoatNormalW = perturbNormal(TBNClearCoat, clearCoatBumpMapData.xyz, vClearCoatBumpInfos.y);
  145. #endif
  146. #endif
  147. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  148. clearCoatNormalW *= sign(dot(clearCoatNormalW, faceNormal));
  149. #endif
  150. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  151. clearCoatNormalW = gl_FrontFacing ? clearCoatNormalW : -clearCoatNormalW;
  152. #endif
  153. outParams.clearCoatNormalW = clearCoatNormalW;
  154. // Clear Coat AA
  155. outParams.clearCoatAARoughnessFactors = getAARoughnessFactors(clearCoatNormalW.xyz);
  156. // Compute N dot V.
  157. float clearCoatNdotVUnclamped = dot(clearCoatNormalW, viewDirectionW);
  158. // The order 1886 page 3.
  159. float clearCoatNdotV = absEps(clearCoatNdotVUnclamped);
  160. #if DEBUGMODE > 0
  161. outParams.clearCoatNdotV = clearCoatNdotV;
  162. #endif
  163. #ifdef CLEARCOAT_TINT
  164. // Used later on in the light fragment and ibl.
  165. vec3 clearCoatVRefract = -refract(vPositionW, clearCoatNormalW, vClearCoatRefractionParams.y);
  166. // The order 1886 page 3.
  167. outParams.clearCoatNdotVRefract = absEps(dot(clearCoatNormalW, clearCoatVRefract));
  168. #endif
  169. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  170. // BRDF Lookup
  171. vec3 environmentClearCoatBrdf = getBRDFLookup(clearCoatNdotV, clearCoatRoughness);
  172. #endif
  173. // Clear Coat Reflection
  174. #if defined(REFLECTION)
  175. float clearCoatAlphaG = convertRoughnessToAverageSlope(clearCoatRoughness);
  176. #ifdef SPECULARAA
  177. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  178. clearCoatAlphaG += outParams.clearCoatAARoughnessFactors.y;
  179. #endif
  180. vec4 environmentClearCoatRadiance = vec4(0., 0., 0., 0.);
  181. vec3 clearCoatReflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), clearCoatNormalW);
  182. #ifdef REFLECTIONMAP_OPPOSITEZ
  183. clearCoatReflectionVector.z *= -1.0;
  184. #endif
  185. // _____________________________ 2D vs 3D Maps ________________________________
  186. #ifdef REFLECTIONMAP_3D
  187. vec3 clearCoatReflectionCoords = clearCoatReflectionVector;
  188. #else
  189. vec2 clearCoatReflectionCoords = clearCoatReflectionVector.xy;
  190. #ifdef REFLECTIONMAP_PROJECTION
  191. clearCoatReflectionCoords /= clearCoatReflectionVector.z;
  192. #endif
  193. clearCoatReflectionCoords.y = 1.0 - clearCoatReflectionCoords.y;
  194. #endif
  195. sampleReflectionTexture(
  196. clearCoatAlphaG,
  197. vReflectionMicrosurfaceInfos,
  198. vReflectionInfos,
  199. vReflectionColor,
  200. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  201. clearCoatNdotVUnclamped,
  202. #endif
  203. #ifdef LINEARSPECULARREFLECTION
  204. clearCoatRoughness,
  205. #endif
  206. reflectionSampler,
  207. clearCoatReflectionCoords,
  208. #ifndef LODBASEDMICROSFURACE
  209. reflectionSamplerLow,
  210. reflectionSamplerHigh,
  211. #endif
  212. environmentClearCoatRadiance
  213. );
  214. #if DEBUGMODE > 0
  215. outParams.environmentClearCoatRadiance = environmentClearCoatRadiance;
  216. #endif
  217. // _________________________ Clear Coat Environment Oclusion __________________________
  218. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  219. vec3 clearCoatEnvironmentReflectance = getReflectanceFromBRDFLookup(vec3(vClearCoatRefractionParams.x), environmentClearCoatBrdf);
  220. #ifdef RADIANCEOCCLUSION
  221. float clearCoatSeo = environmentRadianceOcclusion(ambientMonochrome, clearCoatNdotVUnclamped);
  222. clearCoatEnvironmentReflectance *= clearCoatSeo;
  223. #endif
  224. #ifdef HORIZONOCCLUSION
  225. #ifdef BUMP
  226. #ifdef REFLECTIONMAP_3D
  227. float clearCoatEho = environmentHorizonOcclusion(-viewDirectionW, clearCoatNormalW, geometricNormalW);
  228. clearCoatEnvironmentReflectance *= clearCoatEho;
  229. #endif
  230. #endif
  231. #endif
  232. #else
  233. // Jones implementation of a well balanced fast analytical solution.
  234. vec3 clearCoatEnvironmentReflectance = getReflectanceFromAnalyticalBRDFLookup_Jones(clearCoatNdotV, vec3(1.), vec3(1.), sqrt(1. - clearCoatRoughness));
  235. #endif
  236. clearCoatEnvironmentReflectance *= clearCoatIntensity;
  237. #if DEBUGMODE > 0
  238. outParams.clearCoatEnvironmentReflectance = clearCoatEnvironmentReflectance;
  239. #endif
  240. outParams.finalClearCoatRadianceScaled =
  241. environmentClearCoatRadiance.rgb *
  242. clearCoatEnvironmentReflectance *
  243. vLightingIntensity.z;
  244. #endif
  245. #if defined(CLEARCOAT_TINT)
  246. // NdotL = NdotV in IBL
  247. outParams.absorption = computeClearCoatAbsorption(outParams.clearCoatNdotVRefract, outParams.clearCoatNdotVRefract, outParams.clearCoatColor, clearCoatThickness, clearCoatIntensity);
  248. #endif
  249. // clear coat energy conservation
  250. float fresnelIBLClearCoat = fresnelSchlickGGX(clearCoatNdotV, vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90);
  251. fresnelIBLClearCoat *= clearCoatIntensity;
  252. outParams.conservationFactor = (1. - fresnelIBLClearCoat);
  253. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  254. outParams.energyConservationFactorClearCoat = getEnergyConservationFactor(outParams.specularEnvironmentR0, environmentClearCoatBrdf);
  255. #endif
  256. }
  257. #endif