pbrBlockClearcoat.fx 12 KB

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