pbrBlockClearcoat.fx 12 KB

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