pbrBlockClearcoat.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Do not edit.
  2. import { ShaderStore } from "../../Engines/shaderStore.js";
  3. const name = "pbrBlockClearcoat";
  4. const shader = `struct clearcoatOutParams
  5. {vec3 specularEnvironmentR0;float conservationFactor;vec3 clearCoatNormalW;vec2 clearCoatAARoughnessFactors;float clearCoatIntensity;float clearCoatRoughness;
  6. #ifdef REFLECTION
  7. vec3 finalClearCoatRadianceScaled;
  8. #endif
  9. #ifdef CLEARCOAT_TINT
  10. vec3 absorption;float clearCoatNdotVRefract;vec3 clearCoatColor;float clearCoatThickness;
  11. #endif
  12. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  13. vec3 energyConservationFactorClearCoat;
  14. #endif
  15. #if DEBUGMODE>0
  16. #ifdef CLEARCOAT_BUMP
  17. mat3 TBNClearCoat;
  18. #endif
  19. #ifdef CLEARCOAT_TEXTURE
  20. vec2 clearCoatMapData;
  21. #endif
  22. #if defined(CLEARCOAT_TINT) && defined(CLEARCOAT_TINT_TEXTURE)
  23. vec4 clearCoatTintMapData;
  24. #endif
  25. #ifdef REFLECTION
  26. vec4 environmentClearCoatRadiance;vec3 clearCoatEnvironmentReflectance;
  27. #endif
  28. float clearCoatNdotV;
  29. #endif
  30. };
  31. #ifdef CLEARCOAT
  32. #define pbr_inline
  33. #define inline
  34. void clearcoatBlock(
  35. in vec3 vPositionW,
  36. in vec3 geometricNormalW,
  37. in vec3 viewDirectionW,
  38. in vec2 vClearCoatParams,
  39. #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE)
  40. in vec4 clearCoatMapRoughnessData,
  41. #endif
  42. in vec3 specularEnvironmentR0,
  43. #ifdef CLEARCOAT_TEXTURE
  44. in vec2 clearCoatMapData,
  45. #endif
  46. #ifdef CLEARCOAT_TINT
  47. in vec4 vClearCoatTintParams,
  48. in float clearCoatColorAtDistance,
  49. in vec4 vClearCoatRefractionParams,
  50. #ifdef CLEARCOAT_TINT_TEXTURE
  51. in vec4 clearCoatTintMapData,
  52. #endif
  53. #endif
  54. #ifdef CLEARCOAT_BUMP
  55. in vec2 vClearCoatBumpInfos,
  56. in vec4 clearCoatBumpMapData,
  57. in vec2 vClearCoatBumpUV,
  58. #if defined(TANGENT) && defined(NORMAL)
  59. in mat3 vTBN,
  60. #else
  61. in vec2 vClearCoatTangentSpaceParams,
  62. #endif
  63. #ifdef OBJECTSPACE_NORMALMAP
  64. in mat4 normalMatrix,
  65. #endif
  66. #endif
  67. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  68. in vec3 faceNormal,
  69. #endif
  70. #ifdef REFLECTION
  71. in vec3 vReflectionMicrosurfaceInfos,
  72. in vec2 vReflectionInfos,
  73. in vec3 vReflectionColor,
  74. in vec4 vLightingIntensity,
  75. #ifdef REFLECTIONMAP_3D
  76. in samplerCube reflectionSampler,
  77. #else
  78. in sampler2D reflectionSampler,
  79. #endif
  80. #ifndef LODBASEDMICROSFURACE
  81. #ifdef REFLECTIONMAP_3D
  82. in samplerCube reflectionSamplerLow,
  83. in samplerCube reflectionSamplerHigh,
  84. #else
  85. in sampler2D reflectionSamplerLow,
  86. in sampler2D reflectionSamplerHigh,
  87. #endif
  88. #endif
  89. #ifdef REALTIME_FILTERING
  90. in vec2 vReflectionFilteringInfo,
  91. #endif
  92. #endif
  93. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  94. #ifdef RADIANCEOCCLUSION
  95. in float ambientMonochrome,
  96. #endif
  97. #endif
  98. #if defined(CLEARCOAT_BUMP) || defined(TWOSIDEDLIGHTING)
  99. in float frontFacingMultiplier,
  100. #endif
  101. out clearcoatOutParams outParams
  102. )
  103. {float clearCoatIntensity=vClearCoatParams.x;float clearCoatRoughness=vClearCoatParams.y;
  104. #ifdef CLEARCOAT_TEXTURE
  105. clearCoatIntensity*=clearCoatMapData.x;
  106. #ifdef CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE
  107. clearCoatRoughness*=clearCoatMapData.y;
  108. #endif
  109. #if DEBUGMODE>0
  110. outParams.clearCoatMapData=clearCoatMapData;
  111. #endif
  112. #endif
  113. #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE)
  114. clearCoatRoughness*=clearCoatMapRoughnessData.y;
  115. #endif
  116. outParams.clearCoatIntensity=clearCoatIntensity;outParams.clearCoatRoughness=clearCoatRoughness;
  117. #ifdef CLEARCOAT_TINT
  118. vec3 clearCoatColor=vClearCoatTintParams.rgb;float clearCoatThickness=vClearCoatTintParams.a;
  119. #ifdef CLEARCOAT_TINT_TEXTURE
  120. #ifdef CLEARCOAT_TINT_GAMMATEXTURE
  121. clearCoatColor*=toLinearSpace(clearCoatTintMapData.rgb);
  122. #else
  123. clearCoatColor*=clearCoatTintMapData.rgb;
  124. #endif
  125. clearCoatThickness*=clearCoatTintMapData.a;
  126. #if DEBUGMODE>0
  127. outParams.clearCoatTintMapData=clearCoatTintMapData;
  128. #endif
  129. #endif
  130. outParams.clearCoatColor=computeColorAtDistanceInMedia(clearCoatColor,clearCoatColorAtDistance);outParams.clearCoatThickness=clearCoatThickness;
  131. #endif
  132. #ifdef CLEARCOAT_REMAP_F0
  133. vec3 specularEnvironmentR0Updated=getR0RemappedForClearCoat(specularEnvironmentR0);
  134. #else
  135. vec3 specularEnvironmentR0Updated=specularEnvironmentR0;
  136. #endif
  137. outParams.specularEnvironmentR0=mix(specularEnvironmentR0,specularEnvironmentR0Updated,clearCoatIntensity);vec3 clearCoatNormalW=geometricNormalW;
  138. #ifdef CLEARCOAT_BUMP
  139. #ifdef NORMALXYSCALE
  140. float clearCoatNormalScale=1.0;
  141. #else
  142. float clearCoatNormalScale=vClearCoatBumpInfos.y;
  143. #endif
  144. #if defined(TANGENT) && defined(NORMAL)
  145. mat3 TBNClearCoat=vTBN;
  146. #else
  147. vec2 TBNClearCoatUV=vClearCoatBumpUV*frontFacingMultiplier;mat3 TBNClearCoat=cotangent_frame(clearCoatNormalW*clearCoatNormalScale,vPositionW,TBNClearCoatUV,vClearCoatTangentSpaceParams);
  148. #endif
  149. #if DEBUGMODE>0
  150. outParams.TBNClearCoat=TBNClearCoat;
  151. #endif
  152. #ifdef OBJECTSPACE_NORMALMAP
  153. clearCoatNormalW=normalize(clearCoatBumpMapData.xyz *2.0-1.0);clearCoatNormalW=normalize(mat3(normalMatrix)*clearCoatNormalW);
  154. #else
  155. clearCoatNormalW=perturbNormal(TBNClearCoat,clearCoatBumpMapData.xyz,vClearCoatBumpInfos.y);
  156. #endif
  157. #endif
  158. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  159. clearCoatNormalW*=sign(dot(clearCoatNormalW,faceNormal));
  160. #endif
  161. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  162. clearCoatNormalW=clearCoatNormalW*frontFacingMultiplier;
  163. #endif
  164. outParams.clearCoatNormalW=clearCoatNormalW;outParams.clearCoatAARoughnessFactors=getAARoughnessFactors(clearCoatNormalW.xyz);float clearCoatNdotVUnclamped=dot(clearCoatNormalW,viewDirectionW);float clearCoatNdotV=absEps(clearCoatNdotVUnclamped);
  165. #if DEBUGMODE>0
  166. outParams.clearCoatNdotV=clearCoatNdotV;
  167. #endif
  168. #ifdef CLEARCOAT_TINT
  169. vec3 clearCoatVRefract=refract(-viewDirectionW,clearCoatNormalW,vClearCoatRefractionParams.y);outParams.clearCoatNdotVRefract=absEps(dot(clearCoatNormalW,clearCoatVRefract));
  170. #endif
  171. #if defined(ENVIRONMENTBRDF) && (!defined(REFLECTIONMAP_SKYBOX) || defined(MS_BRDF_ENERGY_CONSERVATION))
  172. vec3 environmentClearCoatBrdf=getBRDFLookup(clearCoatNdotV,clearCoatRoughness);
  173. #endif
  174. #if defined(REFLECTION)
  175. float clearCoatAlphaG=convertRoughnessToAverageSlope(clearCoatRoughness);
  176. #ifdef SPECULARAA
  177. clearCoatAlphaG+=outParams.clearCoatAARoughnessFactors.y;
  178. #endif
  179. vec4 environmentClearCoatRadiance=vec4(0.,0.,0.,0.);vec3 clearCoatReflectionVector=computeReflectionCoords(vec4(vPositionW,1.0),clearCoatNormalW);
  180. #ifdef REFLECTIONMAP_OPPOSITEZ
  181. clearCoatReflectionVector.z*=-1.0;
  182. #endif
  183. #ifdef REFLECTIONMAP_3D
  184. vec3 clearCoatReflectionCoords=clearCoatReflectionVector;
  185. #else
  186. vec2 clearCoatReflectionCoords=clearCoatReflectionVector.xy;
  187. #ifdef REFLECTIONMAP_PROJECTION
  188. clearCoatReflectionCoords/=clearCoatReflectionVector.z;
  189. #endif
  190. clearCoatReflectionCoords.y=1.0-clearCoatReflectionCoords.y;
  191. #endif
  192. sampleReflectionTexture(
  193. clearCoatAlphaG,
  194. vReflectionMicrosurfaceInfos,
  195. vReflectionInfos,
  196. vReflectionColor,
  197. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  198. clearCoatNdotVUnclamped,
  199. #endif
  200. #ifdef LINEARSPECULARREFLECTION
  201. clearCoatRoughness,
  202. #endif
  203. reflectionSampler,
  204. clearCoatReflectionCoords,
  205. #ifndef LODBASEDMICROSFURACE
  206. reflectionSamplerLow,
  207. reflectionSamplerHigh,
  208. #endif
  209. #ifdef REALTIME_FILTERING
  210. vReflectionFilteringInfo,
  211. #endif
  212. environmentClearCoatRadiance
  213. );
  214. #if DEBUGMODE>0
  215. outParams.environmentClearCoatRadiance=environmentClearCoatRadiance;
  216. #endif
  217. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  218. vec3 clearCoatEnvironmentReflectance=getReflectanceFromBRDFLookup(vec3(vClearCoatRefractionParams.x),environmentClearCoatBrdf);
  219. #ifdef HORIZONOCCLUSION
  220. #ifdef BUMP
  221. #ifdef REFLECTIONMAP_3D
  222. float clearCoatEho=environmentHorizonOcclusion(-viewDirectionW,clearCoatNormalW,geometricNormalW);clearCoatEnvironmentReflectance*=clearCoatEho;
  223. #endif
  224. #endif
  225. #endif
  226. #else
  227. vec3 clearCoatEnvironmentReflectance=getReflectanceFromAnalyticalBRDFLookup_Jones(clearCoatNdotV,vec3(1.),vec3(1.),sqrt(1.-clearCoatRoughness));
  228. #endif
  229. clearCoatEnvironmentReflectance*=clearCoatIntensity;
  230. #if DEBUGMODE>0
  231. outParams.clearCoatEnvironmentReflectance=clearCoatEnvironmentReflectance;
  232. #endif
  233. outParams.finalClearCoatRadianceScaled=
  234. environmentClearCoatRadiance.rgb *
  235. clearCoatEnvironmentReflectance *
  236. vLightingIntensity.z;
  237. #endif
  238. #if defined(CLEARCOAT_TINT)
  239. outParams.absorption=computeClearCoatAbsorption(outParams.clearCoatNdotVRefract,outParams.clearCoatNdotVRefract,outParams.clearCoatColor,clearCoatThickness,clearCoatIntensity);
  240. #endif
  241. float fresnelIBLClearCoat=fresnelSchlickGGX(clearCoatNdotV,vClearCoatRefractionParams.x,CLEARCOATREFLECTANCE90);fresnelIBLClearCoat*=clearCoatIntensity;outParams.conservationFactor=(1.-fresnelIBLClearCoat);
  242. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  243. outParams.energyConservationFactorClearCoat=getEnergyConservationFactor(outParams.specularEnvironmentR0,environmentClearCoatBrdf);
  244. #endif
  245. }
  246. #endif
  247. `;
  248. // Sideeffect
  249. ShaderStore.IncludesShadersStore[name] = shader;
  250. /** @internal */
  251. export const pbrBlockClearcoat = { name, shader };
  252. //# sourceMappingURL=pbrBlockClearcoat.js.map