pbrBlockClearcoat.fx 13 KB

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