pbr.fragment.fx 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. #if defined(BUMP) || !defined(NORMAL) || defined(FORCENORMALFORWARD) || defined(SPECULARAA) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. #ifdef LODBASEDMICROSFURACE
  5. #extension GL_EXT_shader_texture_lod : enable
  6. #endif
  7. #define CUSTOM_FRAGMENT_BEGIN
  8. #ifdef LOGARITHMICDEPTH
  9. #extension GL_EXT_frag_depth : enable
  10. #endif
  11. precision highp float;
  12. // Forces linear space for image processing
  13. #ifndef FROMLINEARSPACE
  14. #define FROMLINEARSPACE
  15. #endif
  16. // Declaration
  17. #include<__decl__pbrFragment>
  18. #include<pbrFragmentExtraDeclaration>
  19. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  20. #include<pbrFragmentSamplersDeclaration>
  21. #include<imageProcessingDeclaration>
  22. #include<clipPlaneFragmentDeclaration>
  23. #include<logDepthDeclaration>
  24. #include<fogFragmentDeclaration>
  25. // Helper Functions
  26. #include<helperFunctions>
  27. #include<pbrHelperFunctions>
  28. #include<imageProcessingFunctions>
  29. #include<shadowsFragmentFunctions>
  30. #include<harmonicsFunctions>
  31. #include<pbrDirectLightingSetupFunctions>
  32. #include<pbrDirectLightingFalloffFunctions>
  33. #include<pbrBRDFFunctions>
  34. #include<pbrDirectLightingFunctions>
  35. #include<pbrIBLFunctions>
  36. #include<bumpFragmentFunctions>
  37. #ifdef REFLECTION
  38. #include<reflectionFunction>
  39. #endif
  40. #define CUSTOM_FRAGMENT_DEFINITIONS
  41. // _____________________________ MAIN FUNCTION ____________________________
  42. void main(void) {
  43. #define CUSTOM_FRAGMENT_MAIN_BEGIN
  44. #include<clipPlaneFragment>
  45. // _____________________________ Geometry Information ____________________________
  46. vec3 viewDirectionW = normalize(vEyePosition.xyz - vPositionW);
  47. #ifdef NORMAL
  48. vec3 normalW = normalize(vNormalW);
  49. #else
  50. vec3 normalW = normalize(cross(dFdx(vPositionW), dFdy(vPositionW))) * vEyePosition.w;
  51. #endif
  52. vec3 geometricNormalW = normalW;
  53. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  54. geometricNormalW = gl_FrontFacing ? geometricNormalW : -geometricNormalW;
  55. #endif
  56. #ifdef CLEARCOAT
  57. // Needs to use the geometric normal before bump for this.
  58. vec3 clearCoatNormalW = geometricNormalW;
  59. #endif
  60. #include<bumpFragment>
  61. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  62. vec3 faceNormal = normalize(cross(dFdx(vPositionW), dFdy(vPositionW))) * vEyePosition.w;
  63. #if defined(TWOSIDEDLIGHTING)
  64. faceNormal = gl_FrontFacing ? faceNormal : -faceNormal;
  65. #endif
  66. normalW *= sign(dot(normalW, faceNormal));
  67. #endif
  68. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  69. normalW = gl_FrontFacing ? normalW : -normalW;
  70. #endif
  71. // _____________________________ Albedo Information ______________________________
  72. // Albedo
  73. vec3 surfaceAlbedo = vAlbedoColor.rgb;
  74. // Alpha
  75. float alpha = vAlbedoColor.a;
  76. #ifdef ALBEDO
  77. vec4 albedoTexture = texture2D(albedoSampler, vAlbedoUV + uvOffset);
  78. #if defined(ALPHAFROMALBEDO) || defined(ALPHATEST)
  79. alpha *= albedoTexture.a;
  80. #endif
  81. #ifdef GAMMAALBEDO
  82. surfaceAlbedo *= toLinearSpace(albedoTexture.rgb);
  83. #else
  84. surfaceAlbedo *= albedoTexture.rgb;
  85. #endif
  86. surfaceAlbedo *= vAlbedoInfos.y;
  87. #endif
  88. #ifdef VERTEXCOLOR
  89. surfaceAlbedo *= vColor.rgb;
  90. #endif
  91. #define CUSTOM_FRAGMENT_UPDATE_ALBEDO
  92. // _____________________________ Alpha Information _______________________________
  93. #ifdef OPACITY
  94. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  95. #ifdef OPACITYRGB
  96. alpha = getLuminance(opacityMap.rgb);
  97. #else
  98. alpha *= opacityMap.a;
  99. #endif
  100. alpha *= vOpacityInfos.y;
  101. #endif
  102. #ifdef VERTEXALPHA
  103. alpha *= vColor.a;
  104. #endif
  105. #if !defined(SS_LINKREFRACTIONTOTRANSPARENCY) && !defined(ALPHAFRESNEL)
  106. #ifdef ALPHATEST
  107. if (alpha < ALPHATESTVALUE)
  108. discard;
  109. #ifndef ALPHABLEND
  110. // Prevent to blend with the canvas.
  111. alpha = 1.0;
  112. #endif
  113. #endif
  114. #endif
  115. #define CUSTOM_FRAGMENT_UPDATE_ALPHA
  116. #include<depthPrePass>
  117. #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
  118. // _____________________________ AO Information _______________________________
  119. vec3 ambientOcclusionColor = vec3(1., 1., 1.);
  120. #ifdef AMBIENT
  121. vec3 ambientOcclusionColorMap = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
  122. #ifdef AMBIENTINGRAYSCALE
  123. ambientOcclusionColorMap = vec3(ambientOcclusionColorMap.r, ambientOcclusionColorMap.r, ambientOcclusionColorMap.r);
  124. #endif
  125. ambientOcclusionColor = mix(ambientOcclusionColor, ambientOcclusionColorMap, vAmbientInfos.z);
  126. #endif
  127. #ifdef UNLIT
  128. vec3 diffuseBase = vec3(1., 1., 1.);
  129. #else
  130. // _____________________________ Reflectivity Info _______________________________
  131. float microSurface = vReflectivityColor.a;
  132. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  133. vec3 baseColor = surfaceAlbedo;
  134. #ifdef METALLICWORKFLOW
  135. vec2 metallicRoughness = surfaceReflectivityColor.rg;
  136. #ifdef REFLECTIVITY
  137. vec4 surfaceMetallicColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  138. #ifdef AOSTOREINMETALMAPRED
  139. vec3 aoStoreInMetalMap = vec3(surfaceMetallicColorMap.r, surfaceMetallicColorMap.r, surfaceMetallicColorMap.r);
  140. ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, vReflectivityInfos.z);
  141. #endif
  142. #ifdef METALLNESSSTOREINMETALMAPBLUE
  143. metallicRoughness.r *= surfaceMetallicColorMap.b;
  144. #else
  145. metallicRoughness.r *= surfaceMetallicColorMap.r;
  146. #endif
  147. #ifdef ROUGHNESSSTOREINMETALMAPALPHA
  148. metallicRoughness.g *= surfaceMetallicColorMap.a;
  149. #else
  150. #ifdef ROUGHNESSSTOREINMETALMAPGREEN
  151. metallicRoughness.g *= surfaceMetallicColorMap.g;
  152. #endif
  153. #endif
  154. #endif
  155. #ifdef MICROSURFACEMAP
  156. vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
  157. metallicRoughness.g *= microSurfaceTexel.r;
  158. #endif
  159. #define CUSTOM_FRAGMENT_UPDATE_METALLICROUGHNESS
  160. // Compute microsurface from roughness.
  161. microSurface = 1.0 - metallicRoughness.g;
  162. // Diffuse is used as the base of the reflectivity.
  163. #ifdef REFLECTANCE
  164. // *** NOT USED ANYMORE ***
  165. // Following Frostbite Remapping,
  166. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf page 115
  167. // vec3 f0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + baseColor * metallic;
  168. // where 0.16 * reflectance * reflectance remaps the reflectance to allow storage in 8 bit texture
  169. // Compute the converted diffuse.
  170. surfaceAlbedo = baseColor.rgb * (1.0 - metallicRoughness.r);
  171. // Compute the converted reflectivity.
  172. surfaceReflectivityColor = mix(0.16 * reflectance * reflectance, baseColor, metallicRoughness.r);
  173. #else
  174. vec3 metallicF0 = vec3(vReflectivityColor.a, vReflectivityColor.a, vReflectivityColor.a);
  175. #ifdef METALLICF0FACTORFROMMETALLICMAP
  176. #ifdef REFLECTIVITY
  177. metallicF0 *= surfaceMetallicColorMap.a;
  178. #endif
  179. #endif
  180. // Compute the converted diffuse.
  181. surfaceAlbedo = mix(baseColor.rgb * (1.0 - metallicF0.r), vec3(0., 0., 0.), metallicRoughness.r);
  182. // Compute the converted reflectivity.
  183. surfaceReflectivityColor = mix(metallicF0, baseColor, metallicRoughness.r);
  184. #endif
  185. #else
  186. #ifdef REFLECTIVITY
  187. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  188. surfaceReflectivityColor *= toLinearSpace(surfaceReflectivityColorMap.rgb);
  189. surfaceReflectivityColor *= vReflectivityInfos.y;
  190. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  191. microSurface *= surfaceReflectivityColorMap.a;
  192. microSurface *= vReflectivityInfos.z;
  193. #else
  194. #ifdef MICROSURFACEAUTOMATIC
  195. microSurface *= computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  196. #endif
  197. #ifdef MICROSURFACEMAP
  198. vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
  199. microSurface *= microSurfaceTexel.r;
  200. #endif
  201. #define CUSTOM_FRAGMENT_UPDATE_MICROSURFACE
  202. #endif
  203. #endif
  204. #endif
  205. // Adapt microSurface.
  206. microSurface = saturate(microSurface);
  207. // Compute roughness.
  208. float roughness = 1. - microSurface;
  209. // _____________________________ Alpha Fresnel ___________________________________
  210. #ifdef ALPHAFRESNEL
  211. #if defined(ALPHATEST) || defined(ALPHABLEND)
  212. // Convert approximate perceptual opacity (gamma-encoded opacity) to linear opacity (absorptance, or inverse transmission)
  213. // for use with the linear HDR render target. The final composition will be converted back to gamma encoded values for eventual display.
  214. // Uses power 2.0 rather than 2.2 for simplicity/efficiency, and because the mapping does not need to map the gamma applied to RGB.
  215. float opacityPerceptual = alpha;
  216. #ifdef LINEARALPHAFRESNEL
  217. float opacity0 = opacityPerceptual;
  218. #else
  219. float opacity0 = opacityPerceptual * opacityPerceptual;
  220. #endif
  221. float opacity90 = fresnelGrazingReflectance(opacity0);
  222. vec3 normalForward = faceforward(normalW, -viewDirectionW, normalW);
  223. // Calculate the appropriate linear opacity for the current viewing angle (formally, this quantity is the "directional absorptance").
  224. alpha = getReflectanceFromAnalyticalBRDFLookup_Jones(saturate(dot(viewDirectionW, normalForward)), vec3(opacity0), vec3(opacity90), sqrt(microSurface)).x;
  225. #ifdef ALPHATEST
  226. if (alpha < ALPHATESTVALUE)
  227. discard;
  228. #ifndef ALPHABLEND
  229. // Prevent to blend with the canvas.
  230. alpha = 1.0;
  231. #endif
  232. #endif
  233. #endif
  234. #endif
  235. // _____________________________ Compute Geometry info _________________________________
  236. float NdotVUnclamped = dot(normalW, viewDirectionW);
  237. // The order 1886 page 3.
  238. float NdotV = absEps(NdotVUnclamped);
  239. float alphaG = convertRoughnessToAverageSlope(roughness);
  240. vec2 AARoughnessFactors = getAARoughnessFactors(normalW.xyz);
  241. #ifdef SPECULARAA
  242. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  243. alphaG += AARoughnessFactors.y;
  244. #endif
  245. #ifdef ANISOTROPIC
  246. float anisotropy = vAnisotropy.b;
  247. vec3 anisotropyDirection = vec3(vAnisotropy.xy, 0.);
  248. #ifdef ANISOTROPIC_TEXTURE
  249. vec3 anisotropyMapData = texture2D(anisotropySampler, vAnisotropyUV + uvOffset).rgb * vAnisotropyInfos.y;
  250. anisotropy *= anisotropyMapData.b;
  251. anisotropyDirection.rg *= anisotropyMapData.rg * 2.0 - 1.0;
  252. #endif
  253. mat3 anisoTBN = mat3(normalize(TBN[0]), normalize(TBN[1]), normalize(TBN[2]));
  254. vec3 anisotropicTangent = normalize(anisoTBN * anisotropyDirection);
  255. vec3 anisotropicBitangent = normalize(cross(anisoTBN[2], anisotropicTangent));
  256. vec3 anisotropicNormal = getAnisotropicBentNormals(anisotropicTangent, anisotropicBitangent, normalW, viewDirectionW, anisotropy);
  257. #endif
  258. // _____________________________ Refraction Info _______________________________________
  259. #ifdef SS_REFRACTION
  260. vec4 environmentRefraction = vec4(0., 0., 0., 0.);
  261. #ifdef ANISOTROPIC
  262. vec3 refractionVector = refract(-viewDirectionW, anisotropicNormal, vRefractionInfos.y);
  263. #else
  264. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  265. #endif
  266. #ifdef SS_REFRACTIONMAP_OPPOSITEZ
  267. refractionVector.z *= -1.0;
  268. #endif
  269. // _____________________________ 2D vs 3D Maps ________________________________
  270. #ifdef SS_REFRACTIONMAP_3D
  271. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  272. vec3 refractionCoords = refractionVector;
  273. refractionCoords = vec3(refractionMatrix * vec4(refractionCoords, 0));
  274. #else
  275. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  276. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  277. refractionCoords.y = 1.0 - refractionCoords.y;
  278. #endif
  279. #ifdef SS_LODINREFRACTIONALPHA
  280. float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
  281. #elif defined(SS_LINEARSPECULARREFRACTION)
  282. float refractionLOD = getLinearLodFromRoughness(vRefractionMicrosurfaceInfos.x, roughness);
  283. #else
  284. float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG);
  285. #endif
  286. #ifdef LODBASEDMICROSFURACE
  287. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  288. refractionLOD = refractionLOD * vRefractionMicrosurfaceInfos.y + vRefractionMicrosurfaceInfos.z;
  289. #ifdef SS_LODINREFRACTIONALPHA
  290. // Automatic LOD adjustment to ensure that the smoothness-based environment LOD selection
  291. // is constrained to appropriate LOD levels in order to prevent aliasing.
  292. // The environment map is first sampled without custom LOD selection to determine
  293. // the hardware-selected LOD, and this is then used to constrain the final LOD selection
  294. // so that excessive surface smoothness does not cause aliasing (e.g. on curved geometry
  295. // where the normal is varying rapidly).
  296. // Note: Shader Model 4.1 or higher can provide this directly via CalculateLevelOfDetail(), and
  297. // manual calculation via derivatives is also possible, but for simplicity we use the
  298. // hardware LOD calculation with the alpha channel containing the LOD for each mipmap.
  299. float automaticRefractionLOD = UNPACK_LOD(sampleRefraction(refractionSampler, refractionCoords).a);
  300. float requestedRefractionLOD = max(automaticRefractionLOD, refractionLOD);
  301. #else
  302. float requestedRefractionLOD = refractionLOD;
  303. #endif
  304. environmentRefraction = sampleRefractionLod(refractionSampler, refractionCoords, requestedRefractionLOD);
  305. #else
  306. float lodRefractionNormalized = saturate(refractionLOD / log2(vRefractionMicrosurfaceInfos.x));
  307. float lodRefractionNormalizedDoubled = lodRefractionNormalized * 2.0;
  308. vec4 environmentRefractionMid = sampleRefraction(refractionSampler, refractionCoords);
  309. if(lodRefractionNormalizedDoubled < 1.0){
  310. environmentRefraction = mix(
  311. sampleRefraction(refractionSamplerHigh, refractionCoords),
  312. environmentRefractionMid,
  313. lodRefractionNormalizedDoubled
  314. );
  315. }else{
  316. environmentRefraction = mix(
  317. environmentRefractionMid,
  318. sampleRefraction(refractionSamplerLow, refractionCoords),
  319. lodRefractionNormalizedDoubled - 1.0
  320. );
  321. }
  322. #endif
  323. #ifdef SS_RGBDREFRACTION
  324. environmentRefraction.rgb = fromRGBD(environmentRefraction);
  325. #endif
  326. #ifdef SS_GAMMAREFRACTION
  327. environmentRefraction.rgb = toLinearSpace(environmentRefraction.rgb);
  328. #endif
  329. // _____________________________ Levels _____________________________________
  330. environmentRefraction.rgb *= vRefractionInfos.x;
  331. #endif
  332. // _____________________________ Reflection Info _______________________________________
  333. #ifdef REFLECTION
  334. vec4 environmentRadiance = vec4(0., 0., 0., 0.);
  335. vec3 environmentIrradiance = vec3(0., 0., 0.);
  336. #ifdef ANISOTROPIC
  337. vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), anisotropicNormal);
  338. #else
  339. vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  340. #endif
  341. #ifdef REFLECTIONMAP_OPPOSITEZ
  342. reflectionVector.z *= -1.0;
  343. #endif
  344. // _____________________________ 2D vs 3D Maps ________________________________
  345. #ifdef REFLECTIONMAP_3D
  346. vec3 reflectionCoords = reflectionVector;
  347. #else
  348. vec2 reflectionCoords = reflectionVector.xy;
  349. #ifdef REFLECTIONMAP_PROJECTION
  350. reflectionCoords /= reflectionVector.z;
  351. #endif
  352. reflectionCoords.y = 1.0 - reflectionCoords.y;
  353. #endif
  354. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  355. float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
  356. #elif defined(LINEARSPECULARREFLECTION)
  357. float reflectionLOD = getLinearLodFromRoughness(vReflectionMicrosurfaceInfos.x, roughness);
  358. #else
  359. float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG);
  360. #endif
  361. #ifdef LODBASEDMICROSFURACE
  362. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  363. reflectionLOD = reflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  364. #ifdef LODINREFLECTIONALPHA
  365. // Automatic LOD adjustment to ensure that the smoothness-based environment LOD selection
  366. // is constrained to appropriate LOD levels in order to prevent aliasing.
  367. // The environment map is first sampled without custom LOD selection to determine
  368. // the hardware-selected LOD, and this is then used to constrain the final LOD selection
  369. // so that excessive surface smoothness does not cause aliasing (e.g. on curved geometry
  370. // where the normal is varying rapidly).
  371. // Note: Shader Model 4.1 or higher can provide this directly via CalculateLevelOfDetail(), and
  372. // manual calculation via derivatives is also possible, but for simplicity we use the
  373. // hardware LOD calculation with the alpha channel containing the LOD for each mipmap.
  374. float automaticReflectionLOD = UNPACK_LOD(sampleReflection(reflectionSampler, reflectionCoords).a);
  375. float requestedReflectionLOD = max(automaticReflectionLOD, reflectionLOD);
  376. #else
  377. float requestedReflectionLOD = reflectionLOD;
  378. #endif
  379. environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, requestedReflectionLOD);
  380. #else
  381. float lodReflectionNormalized = saturate(reflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
  382. float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
  383. vec4 environmentSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
  384. if(lodReflectionNormalizedDoubled < 1.0){
  385. environmentRadiance = mix(
  386. sampleReflection(reflectionSamplerHigh, reflectionCoords),
  387. environmentSpecularMid,
  388. lodReflectionNormalizedDoubled
  389. );
  390. }else{
  391. environmentRadiance = mix(
  392. environmentSpecularMid,
  393. sampleReflection(reflectionSamplerLow, reflectionCoords),
  394. lodReflectionNormalizedDoubled - 1.0
  395. );
  396. }
  397. #endif
  398. #ifdef RGBDREFLECTION
  399. environmentRadiance.rgb = fromRGBD(environmentRadiance);
  400. #endif
  401. #ifdef GAMMAREFLECTION
  402. environmentRadiance.rgb = toLinearSpace(environmentRadiance.rgb);
  403. #endif
  404. // _____________________________ Irradiance ________________________________
  405. #ifdef USESPHERICALFROMREFLECTIONMAP
  406. #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
  407. environmentIrradiance = vEnvironmentIrradiance;
  408. #else
  409. #ifdef ANISOTROPIC
  410. vec3 irradianceVector = vec3(reflectionMatrix * vec4(anisotropicNormal, 0)).xyz;
  411. #else
  412. vec3 irradianceVector = vec3(reflectionMatrix * vec4(normalW, 0)).xyz;
  413. #endif
  414. #ifdef REFLECTIONMAP_OPPOSITEZ
  415. irradianceVector.z *= -1.0;
  416. #endif
  417. environmentIrradiance = computeEnvironmentIrradiance(irradianceVector);
  418. #endif
  419. #elif defined(USEIRRADIANCEMAP)
  420. vec4 environmentIrradiance4 = sampleReflection(irradianceSampler, reflectionCoords);
  421. environmentIrradiance = environmentIrradiance4.rgb;
  422. #ifdef RGBDREFLECTION
  423. environmentIrradiance.rgb = fromRGBD(environmentIrradiance4);
  424. #endif
  425. #ifdef GAMMAREFLECTION
  426. environmentIrradiance.rgb = toLinearSpace(environmentIrradiance.rgb);
  427. #endif
  428. #endif
  429. // _____________________________ Levels _____________________________________
  430. environmentRadiance.rgb *= vReflectionInfos.x;
  431. environmentRadiance.rgb *= vReflectionColor.rgb;
  432. environmentIrradiance *= vReflectionColor.rgb;
  433. #endif
  434. // ___________________ Compute Reflectance aka R0 F0 info _________________________
  435. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  436. float reflectance90 = fresnelGrazingReflectance(reflectance);
  437. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  438. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  439. // ________________________________ Sheen Information ______________________________
  440. #ifdef SHEEN
  441. float sheenIntensity = vSheenColor.a;
  442. #ifdef SHEEN_TEXTURE
  443. vec4 sheenMapData = texture2D(sheenSampler, vSheenUV + uvOffset) * vSheenInfos.y;
  444. sheenIntensity *= sheenMapData.a;
  445. #endif
  446. #ifdef SHEEN_LINKWITHALBEDO
  447. float sheenFactor = pow5(1.0-sheenIntensity);
  448. vec3 sheenColor = baseColor.rgb*(1.0-sheenFactor);
  449. float sheenRoughness = sheenIntensity;
  450. // remap albedo.
  451. surfaceAlbedo.rgb *= sheenFactor;
  452. #else
  453. vec3 sheenColor = vSheenColor.rgb;
  454. #ifdef SHEEN_TEXTURE
  455. sheenColor.rgb *= toLinearSpace(sheenMapData.rgb);
  456. #endif
  457. #ifdef SHEEN_ROUGHNESS
  458. float sheenRoughness = vSheenRoughness;
  459. #else
  460. float sheenRoughness = roughness;
  461. #endif
  462. // Sheen Lobe Layering.
  463. #if !defined(SHEEN_ALBEDOSCALING)
  464. sheenIntensity *= (1. - reflectance);
  465. #endif
  466. // Remap F0 and sheen.
  467. sheenColor *= sheenIntensity;
  468. #endif
  469. // Sheen Reflection
  470. #if defined(REFLECTION)
  471. float sheenAlphaG = convertRoughnessToAverageSlope(sheenRoughness);
  472. #ifdef SPECULARAA
  473. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  474. sheenAlphaG += AARoughnessFactors.y;
  475. #endif
  476. vec4 environmentSheenRadiance = vec4(0., 0., 0., 0.);
  477. // _____________________________ 2D vs 3D Maps ________________________________
  478. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  479. float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG, NdotVUnclamped);
  480. #elif defined(LINEARSPECULARREFLECTION)
  481. float sheenReflectionLOD = getLinearLodFromRoughness(vReflectionMicrosurfaceInfos.x, sheenRoughness);
  482. #else
  483. float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG);
  484. #endif
  485. #ifdef LODBASEDMICROSFURACE
  486. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  487. sheenReflectionLOD = sheenReflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  488. environmentSheenRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, sheenReflectionLOD);
  489. #else
  490. float lodSheenReflectionNormalized = saturate(sheenReflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
  491. float lodSheenReflectionNormalizedDoubled = lodSheenReflectionNormalized * 2.0;
  492. vec4 environmentSheenMid = sampleReflection(reflectionSampler, reflectionCoords);
  493. if(lodSheenReflectionNormalizedDoubled < 1.0){
  494. environmentSheenRadiance = mix(
  495. sampleReflection(reflectionSamplerHigh, reflectionCoords),
  496. environmentSheenMid,
  497. lodSheenReflectionNormalizedDoubled
  498. );
  499. }else{
  500. environmentSheenRadiance = mix(
  501. environmentSheenMid,
  502. sampleReflection(reflectionSamplerLow, reflectionCoords),
  503. lodSheenReflectionNormalizedDoubled - 1.0
  504. );
  505. }
  506. #endif
  507. #ifdef RGBDREFLECTION
  508. environmentSheenRadiance.rgb = fromRGBD(environmentSheenRadiance);
  509. #endif
  510. #ifdef GAMMAREFLECTION
  511. environmentSheenRadiance.rgb = toLinearSpace(environmentSheenRadiance.rgb);
  512. #endif
  513. // _____________________________ Levels _____________________________________
  514. environmentSheenRadiance.rgb *= vReflectionInfos.x;
  515. environmentSheenRadiance.rgb *= vReflectionColor.rgb;
  516. #endif
  517. #endif
  518. // _____________________________ Clear Coat Information ____________________________
  519. #ifdef CLEARCOAT
  520. // Clear COAT parameters.
  521. float clearCoatIntensity = vClearCoatParams.x;
  522. float clearCoatRoughness = vClearCoatParams.y;
  523. #ifdef CLEARCOAT_TEXTURE
  524. vec2 clearCoatMapData = texture2D(clearCoatSampler, vClearCoatUV + uvOffset).rg * vClearCoatInfos.y;
  525. clearCoatIntensity *= clearCoatMapData.x;
  526. clearCoatRoughness *= clearCoatMapData.y;
  527. #endif
  528. #ifdef CLEARCOAT_TINT
  529. vec3 clearCoatColor = vClearCoatTintParams.rgb;
  530. float clearCoatThickness = vClearCoatTintParams.a;
  531. #ifdef CLEARCOAT_TINT_TEXTURE
  532. vec4 clearCoatTintMapData = texture2D(clearCoatTintSampler, vClearCoatTintUV + uvOffset);
  533. clearCoatColor *= toLinearSpace(clearCoatTintMapData.rgb);
  534. clearCoatThickness *= clearCoatTintMapData.a;
  535. #endif
  536. clearCoatColor = computeColorAtDistanceInMedia(clearCoatColor, clearCoatColorAtDistance);
  537. #endif
  538. // remapping and linearization of clear coat roughness
  539. // Let s see how it ends up in gltf
  540. // clearCoatRoughness = mix(0.089, 0.6, clearCoatRoughness);
  541. // Remap F0 to account for the change of interface within the material.
  542. vec3 specularEnvironmentR0Updated = getR0RemappedForClearCoat(specularEnvironmentR0);
  543. specularEnvironmentR0 = mix(specularEnvironmentR0, specularEnvironmentR0Updated, clearCoatIntensity);
  544. #ifdef CLEARCOAT_BUMP
  545. #ifdef NORMALXYSCALE
  546. float clearCoatNormalScale = 1.0;
  547. #else
  548. float clearCoatNormalScale = vClearCoatBumpInfos.y;
  549. #endif
  550. #if defined(TANGENT) && defined(NORMAL)
  551. mat3 TBNClearCoat = vTBN;
  552. #else
  553. mat3 TBNClearCoat = cotangent_frame(clearCoatNormalW * clearCoatNormalScale, vPositionW, vClearCoatBumpUV, vClearCoatTangentSpaceParams);
  554. #endif
  555. #ifdef OBJECTSPACE_NORMALMAP
  556. clearCoatNormalW = normalize(texture2D(clearCoatBumpSampler, vClearCoatBumpUV + uvOffset).xyz * 2.0 - 1.0);
  557. clearCoatNormalW = normalize(mat3(normalMatrix) * clearCoatNormalW);
  558. #else
  559. clearCoatNormalW = perturbNormal(TBNClearCoat, texture2D(clearCoatBumpSampler, vClearCoatBumpUV + uvOffset).xyz, vClearCoatBumpInfos.y);
  560. #endif
  561. #endif
  562. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  563. clearCoatNormalW *= sign(dot(clearCoatNormalW, faceNormal));
  564. #endif
  565. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  566. clearCoatNormalW = gl_FrontFacing ? clearCoatNormalW : -clearCoatNormalW;
  567. #endif
  568. // Clear Coat AA
  569. vec2 clearCoatAARoughnessFactors = getAARoughnessFactors(clearCoatNormalW.xyz);
  570. // Compute N dot V.
  571. float clearCoatNdotVUnclamped = dot(clearCoatNormalW, viewDirectionW);
  572. // The order 1886 page 3.
  573. float clearCoatNdotV = absEps(clearCoatNdotVUnclamped);
  574. #ifdef CLEARCOAT_TINT
  575. // Used later on in the light fragment and ibl.
  576. vec3 clearCoatVRefract = -refract(vPositionW, clearCoatNormalW, vClearCoatRefractionParams.y);
  577. // The order 1886 page 3.
  578. float clearCoatNdotVRefract = absEps(dot(clearCoatNormalW, clearCoatVRefract));
  579. vec3 absorption = vec3(0.);
  580. #endif
  581. // Clear Coat Reflection
  582. #if defined(REFLECTION)
  583. float clearCoatAlphaG = convertRoughnessToAverageSlope(clearCoatRoughness);
  584. #ifdef SPECULARAA
  585. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  586. clearCoatAlphaG += clearCoatAARoughnessFactors.y;
  587. #endif
  588. vec4 environmentClearCoatRadiance = vec4(0., 0., 0., 0.);
  589. vec3 clearCoatReflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), clearCoatNormalW);
  590. #ifdef REFLECTIONMAP_OPPOSITEZ
  591. clearCoatReflectionVector.z *= -1.0;
  592. #endif
  593. // _____________________________ 2D vs 3D Maps ________________________________
  594. #ifdef REFLECTIONMAP_3D
  595. vec3 clearCoatReflectionCoords = clearCoatReflectionVector;
  596. #else
  597. vec2 clearCoatReflectionCoords = clearCoatReflectionVector.xy;
  598. #ifdef REFLECTIONMAP_PROJECTION
  599. clearCoatReflectionCoords /= clearCoatReflectionVector.z;
  600. #endif
  601. clearCoatReflectionCoords.y = 1.0 - clearCoatReflectionCoords.y;
  602. #endif
  603. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  604. float clearCoatReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, clearCoatAlphaG, clearCoatNdotVUnclamped);
  605. #elif defined(LINEARSPECULARREFLECTION)
  606. float clearCoatReflectionLOD = getLinearLodFromRoughness(vReflectionMicrosurfaceInfos.x, clearCoatRoughness);
  607. #else
  608. float clearCoatReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, clearCoatAlphaG);
  609. #endif
  610. #ifdef LODBASEDMICROSFURACE
  611. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  612. clearCoatReflectionLOD = clearCoatReflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  613. float requestedClearCoatReflectionLOD = clearCoatReflectionLOD;
  614. environmentClearCoatRadiance = sampleReflectionLod(reflectionSampler, clearCoatReflectionCoords, requestedClearCoatReflectionLOD);
  615. #else
  616. float lodClearCoatReflectionNormalized = saturate(clearCoatReflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
  617. float lodClearCoatReflectionNormalizedDoubled = lodClearCoatReflectionNormalized * 2.0;
  618. vec4 environmentClearCoatMid = sampleReflection(reflectionSampler, reflectionCoords);
  619. if(lodClearCoatReflectionNormalizedDoubled < 1.0){
  620. environmentClearCoatRadiance = mix(
  621. sampleReflection(reflectionSamplerHigh, clearCoatReflectionCoords),
  622. environmentClearCoatMid,
  623. lodClearCoatReflectionNormalizedDoubled
  624. );
  625. }else{
  626. environmentClearCoatRadiance = mix(
  627. environmentClearCoatMid,
  628. sampleReflection(reflectionSamplerLow, clearCoatReflectionCoords),
  629. lodClearCoatReflectionNormalizedDoubled - 1.0
  630. );
  631. }
  632. #endif
  633. #ifdef RGBDREFLECTION
  634. environmentClearCoatRadiance.rgb = fromRGBD(environmentClearCoatRadiance);
  635. #endif
  636. #ifdef GAMMAREFLECTION
  637. environmentClearCoatRadiance.rgb = toLinearSpace(environmentClearCoatRadiance.rgb);
  638. #endif
  639. // _____________________________ Levels _____________________________________
  640. environmentClearCoatRadiance.rgb *= vReflectionInfos.x;
  641. environmentClearCoatRadiance.rgb *= vReflectionColor.rgb;
  642. #endif
  643. #endif
  644. // _____________________________ IBL BRDF + Energy Cons ________________________________
  645. #if defined(ENVIRONMENTBRDF)
  646. // BRDF Lookup
  647. vec3 environmentBrdf = getBRDFLookup(NdotV, roughness);
  648. #ifdef MS_BRDF_ENERGY_CONSERVATION
  649. vec3 energyConservationFactor = getEnergyConservationFactor(specularEnvironmentR0, environmentBrdf);
  650. #endif
  651. #endif
  652. // ___________________________________ SubSurface ______________________________________
  653. #ifdef SUBSURFACE
  654. #ifdef SS_REFRACTION
  655. float refractionIntensity = vSubSurfaceIntensity.x;
  656. #ifdef SS_LINKREFRACTIONTOTRANSPARENCY
  657. refractionIntensity *= (1.0 - alpha);
  658. // Put alpha back to 1;
  659. alpha = 1.0;
  660. #endif
  661. #endif
  662. #ifdef SS_TRANSLUCENCY
  663. float translucencyIntensity = vSubSurfaceIntensity.y;
  664. #endif
  665. #ifdef SS_SCATTERING
  666. float scatteringIntensity = vSubSurfaceIntensity.z;
  667. #endif
  668. #ifdef SS_THICKNESSANDMASK_TEXTURE
  669. vec4 thicknessMap = texture2D(thicknessSampler, vThicknessUV + uvOffset);
  670. float thickness = thicknessMap.r * vThicknessParam.y + vThicknessParam.x;
  671. #ifdef SS_MASK_FROM_THICKNESS_TEXTURE
  672. #ifdef SS_REFRACTION
  673. refractionIntensity *= thicknessMap.g;
  674. #endif
  675. #ifdef SS_TRANSLUCENCY
  676. translucencyIntensity *= thicknessMap.b;
  677. #endif
  678. #ifdef SS_SCATTERING
  679. scatteringIntensity *= thicknessMap.a;
  680. #endif
  681. #endif
  682. #else
  683. float thickness = vThicknessParam.y;
  684. #endif
  685. #ifdef SS_TRANSLUCENCY
  686. thickness = maxEps(thickness);
  687. vec3 transmittance = transmittanceBRDF_Burley(vTintColor.rgb, vDiffusionDistance, thickness);
  688. transmittance *= translucencyIntensity;
  689. #endif
  690. #endif
  691. // ____________________________________________________________________________________
  692. // _____________________________ Direct Lighting Info __________________________________
  693. vec3 diffuseBase = vec3(0., 0., 0.);
  694. #ifdef SPECULARTERM
  695. vec3 specularBase = vec3(0., 0., 0.);
  696. #endif
  697. #ifdef CLEARCOAT
  698. vec3 clearCoatBase = vec3(0., 0., 0.);
  699. #endif
  700. #ifdef SHEEN
  701. vec3 sheenBase = vec3(0., 0., 0.);
  702. #endif
  703. #ifdef LIGHTMAP
  704. vec4 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset);
  705. #ifdef RGBDLIGHTMAP
  706. lightmapColor.rgb = fromRGBD(lightmapColor);
  707. #endif
  708. #ifdef GAMMALIGHTMAP
  709. lightmapColor.rgb = toLinearSpace(lightmapColor.rgb);
  710. #endif
  711. lightmapColor.rgb *= vLightmapInfos.y;
  712. #endif
  713. // Direct Lighting Variables
  714. preLightingInfo preInfo;
  715. lightingInfo info;
  716. float shadow = 1.; // 1 - shadowLevel
  717. #include<lightFragment>[0..maxSimultaneousLights]
  718. // _________________________ Specular Environment Oclusion __________________________
  719. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  720. vec3 specularEnvironmentReflectance = getReflectanceFromBRDFLookup(specularEnvironmentR0, environmentBrdf);
  721. #ifdef RADIANCEOCCLUSION
  722. #ifdef AMBIENTINGRAYSCALE
  723. float ambientMonochrome = ambientOcclusionColor.r;
  724. #else
  725. float ambientMonochrome = getLuminance(ambientOcclusionColor);
  726. #endif
  727. float seo = environmentRadianceOcclusion(ambientMonochrome, NdotVUnclamped);
  728. specularEnvironmentReflectance *= seo;
  729. #endif
  730. #ifdef HORIZONOCCLUSION
  731. #ifdef BUMP
  732. #ifdef REFLECTIONMAP_3D
  733. float eho = environmentHorizonOcclusion(-viewDirectionW, normalW, geometricNormalW);
  734. specularEnvironmentReflectance *= eho;
  735. #endif
  736. #endif
  737. #endif
  738. #else
  739. // Jones implementation of a well balanced fast analytical solution.
  740. vec3 specularEnvironmentReflectance = getReflectanceFromAnalyticalBRDFLookup_Jones(NdotV, specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  741. #endif
  742. // _____________________________ Sheen Environment Oclusion __________________________
  743. #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
  744. /*#ifdef SHEEN_SOFTER
  745. vec3 environmentSheenBrdf = vec3(0., 0., getBRDFLookupCharlieSheen(NdotV, sheenRoughness));
  746. #else*/
  747. #ifdef SHEEN_ROUGHNESS
  748. vec3 environmentSheenBrdf = getBRDFLookup(NdotV, sheenRoughness);
  749. #else
  750. vec3 environmentSheenBrdf = environmentBrdf;
  751. #endif
  752. /*#endif*/
  753. vec3 sheenEnvironmentReflectance = getSheenReflectanceFromBRDFLookup(sheenColor, environmentSheenBrdf);
  754. #ifdef RADIANCEOCCLUSION
  755. sheenEnvironmentReflectance *= seo;
  756. #endif
  757. #ifdef HORIZONOCCLUSION
  758. #ifdef BUMP
  759. #ifdef REFLECTIONMAP_3D
  760. sheenEnvironmentReflectance *= eho;
  761. #endif
  762. #endif
  763. #endif
  764. #if defined(SHEEN_ALBEDOSCALING)
  765. // Sheen Lobe Layering.
  766. // environmentSheenBrdf.b is (integral on hemisphere)[f_sheen*cos(theta)*dtheta*dphi], which happens to also be the directional albedo needed for albedo scaling.
  767. // See section 6.2.3 in https://dassaultsystemes-technology.github.io/EnterprisePBRShadingModel/spec-2021x.md.html#components/sheen
  768. float sheenAlbedoScaling = 1.0 - sheenIntensity * max(max(sheenColor.r, sheenColor.g), sheenColor.b) * environmentSheenBrdf.b;
  769. #endif
  770. #endif
  771. // _________________________ Clear Coat Environment Oclusion __________________________
  772. #ifdef CLEARCOAT
  773. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  774. // BRDF Lookup
  775. vec3 environmentClearCoatBrdf = getBRDFLookup(clearCoatNdotV, clearCoatRoughness);
  776. vec3 clearCoatEnvironmentReflectance = getReflectanceFromBRDFLookup(vec3(vClearCoatRefractionParams.x), environmentClearCoatBrdf);
  777. #ifdef RADIANCEOCCLUSION
  778. float clearCoatSeo = environmentRadianceOcclusion(ambientMonochrome, clearCoatNdotVUnclamped);
  779. clearCoatEnvironmentReflectance *= clearCoatSeo;
  780. #endif
  781. #ifdef HORIZONOCCLUSION
  782. #ifdef BUMP
  783. #ifdef REFLECTIONMAP_3D
  784. float clearCoatEho = environmentHorizonOcclusion(-viewDirectionW, clearCoatNormalW, geometricNormalW);
  785. clearCoatEnvironmentReflectance *= clearCoatEho;
  786. #endif
  787. #endif
  788. #endif
  789. #else
  790. // Jones implementation of a well balanced fast analytical solution.
  791. vec3 clearCoatEnvironmentReflectance = getReflectanceFromAnalyticalBRDFLookup_Jones(clearCoatNdotV, vec3(1.), vec3(1.), sqrt(1. - clearCoatRoughness));
  792. #endif
  793. clearCoatEnvironmentReflectance *= clearCoatIntensity;
  794. #if defined(CLEARCOAT_TINT)
  795. // NdotL = NdotV in IBL
  796. absorption = computeClearCoatAbsorption(clearCoatNdotVRefract, clearCoatNdotVRefract, clearCoatColor, clearCoatThickness, clearCoatIntensity);
  797. #ifdef REFLECTION
  798. environmentIrradiance *= absorption;
  799. #endif
  800. #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
  801. sheenEnvironmentReflectance *= absorption;
  802. #endif
  803. specularEnvironmentReflectance *= absorption;
  804. #endif
  805. // clear coat energy conservation
  806. float fresnelIBLClearCoat = fresnelSchlickGGX(clearCoatNdotV, vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90);
  807. fresnelIBLClearCoat *= clearCoatIntensity;
  808. float conservationFactor = (1. - fresnelIBLClearCoat);
  809. #ifdef REFLECTION
  810. environmentIrradiance *= conservationFactor;
  811. #endif
  812. #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
  813. sheenEnvironmentReflectance *= conservationFactor;
  814. #endif
  815. specularEnvironmentReflectance *= conservationFactor;
  816. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  817. vec3 energyConservationFactorClearCoat = getEnergyConservationFactor(specularEnvironmentR0, environmentClearCoatBrdf);
  818. #endif
  819. #endif
  820. // _____________________________ Transmittance + Tint ________________________________
  821. #ifdef SS_REFRACTION
  822. vec3 refractionTransmittance = vec3(refractionIntensity);
  823. #ifdef SS_THICKNESSANDMASK_TEXTURE
  824. vec3 volumeAlbedo = computeColorAtDistanceInMedia(vTintColor.rgb, vTintColor.w);
  825. // // Simulate Flat Surface
  826. // thickness /= dot(refractionVector, -normalW);
  827. // // Simulate Curved Surface
  828. // float NdotRefract = dot(normalW, refractionVector);
  829. // thickness *= -NdotRefract;
  830. refractionTransmittance *= cocaLambert(volumeAlbedo, thickness);
  831. #elif defined(SS_LINKREFRACTIONTOTRANSPARENCY)
  832. // Tint the material with albedo.
  833. float maxChannel = max(max(surfaceAlbedo.r, surfaceAlbedo.g), surfaceAlbedo.b);
  834. vec3 volumeAlbedo = saturate(maxChannel * surfaceAlbedo);
  835. // Tint reflectance
  836. environmentRefraction.rgb *= volumeAlbedo;
  837. #else
  838. // Compute tint from min distance only.
  839. vec3 volumeAlbedo = computeColorAtDistanceInMedia(vTintColor.rgb, vTintColor.w);
  840. refractionTransmittance *= cocaLambert(volumeAlbedo, vThicknessParam.y);
  841. #endif
  842. // Decrease Albedo Contribution
  843. surfaceAlbedo *= (1. - refractionIntensity);
  844. #ifdef REFLECTION
  845. // Decrease irradiance Contribution
  846. environmentIrradiance *= (1. - refractionIntensity);
  847. #endif
  848. // Add Multiple internal bounces.
  849. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  850. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, refractionIntensity);
  851. // In theory T = 1 - R.
  852. refractionTransmittance *= 1.0 - specularEnvironmentReflectance;
  853. #endif
  854. // _______________________________ IBL Translucency ________________________________
  855. #if defined(REFLECTION) && defined(SS_TRANSLUCENCY)
  856. #if defined(NORMAL) && defined(USESPHERICALINVERTEX) || !defined(USESPHERICALFROMREFLECTIONMAP)
  857. vec3 irradianceVector = vec3(reflectionMatrix * vec4(normalW, 0)).xyz;
  858. #ifdef REFLECTIONMAP_OPPOSITEZ
  859. irradianceVector.z *= -1.0;
  860. #endif
  861. #endif
  862. #if defined(USESPHERICALFROMREFLECTIONMAP)
  863. vec4 refractionIrradiance = vec4(computeEnvironmentIrradiance(-irradianceVector), 0.0);
  864. #elif defined(USEIRRADIANCEMAP)
  865. #ifdef REFLECTIONMAP_3D
  866. vec3 irradianceCoords = irradianceVector;
  867. #else
  868. vec2 irradianceCoords = irradianceVector.xy;
  869. #ifdef REFLECTIONMAP_PROJECTION
  870. irradianceCoords /= irradianceVector.z;
  871. #endif
  872. irradianceCoords.y = 1.0 - irradianceCoords.y;
  873. #endif
  874. vec4 refractionIrradiance = sampleReflection(irradianceSampler, -irradianceCoords);
  875. #ifdef RGBDREFLECTION
  876. refractionIrradiance.rgb = fromRGBD(refractionIrradiance);
  877. #endif
  878. #ifdef GAMMAREFLECTION
  879. refractionIrradiance.rgb = toLinearSpace(refractionIrradiance.rgb);
  880. #endif
  881. #else
  882. vec4 refractionIrradiance = vec4(0.);
  883. #endif
  884. refractionIrradiance.rgb *= transmittance;
  885. #endif
  886. // ______________________________________________________________________________
  887. // _____________________________ Energy Conservation ___________________________
  888. // Apply Energy Conservation.
  889. #ifndef METALLICWORKFLOW
  890. #ifdef SPECULAR_GLOSSINESS_ENERGY_CONSERVATION
  891. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  892. #endif
  893. #endif
  894. #if defined(SHEEN) && defined(SHEEN_ALBEDOSCALING) && defined(ENVIRONMENTBRDF)
  895. surfaceAlbedo.rgb = sheenAlbedoScaling * surfaceAlbedo.rgb;
  896. #endif
  897. // _____________________________ Irradiance ______________________________________
  898. #ifdef REFLECTION
  899. vec3 finalIrradiance = environmentIrradiance;
  900. #if defined(SS_TRANSLUCENCY)
  901. finalIrradiance += refractionIrradiance.rgb;
  902. #endif
  903. finalIrradiance *= surfaceAlbedo.rgb;
  904. #endif
  905. // _____________________________ Specular ________________________________________
  906. #ifdef SPECULARTERM
  907. vec3 finalSpecular = specularBase;
  908. finalSpecular = max(finalSpecular, 0.0);
  909. // Full value needed for alpha.
  910. vec3 finalSpecularScaled = finalSpecular * vLightingIntensity.x * vLightingIntensity.w;
  911. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  912. finalSpecularScaled *= energyConservationFactor;
  913. #endif
  914. #if defined(SHEEN) && defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
  915. finalSpecularScaled *= sheenAlbedoScaling;
  916. #endif
  917. #endif
  918. // _____________________________ Radiance ________________________________________
  919. #ifdef REFLECTION
  920. vec3 finalRadiance = environmentRadiance.rgb;
  921. finalRadiance *= specularEnvironmentReflectance;
  922. // Full value needed for alpha.
  923. vec3 finalRadianceScaled = finalRadiance * vLightingIntensity.z;
  924. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  925. finalRadianceScaled *= energyConservationFactor;
  926. #endif
  927. #if defined(SHEEN) && defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
  928. finalRadianceScaled *= sheenAlbedoScaling;
  929. #endif
  930. #endif
  931. // _____________________________ Refraction ______________________________________
  932. #ifdef SS_REFRACTION
  933. vec3 finalRefraction = environmentRefraction.rgb;
  934. finalRefraction *= refractionTransmittance;
  935. #endif
  936. // ________________________________ Sheen ________________________________________
  937. #ifdef SHEEN
  938. vec3 finalSheen = sheenBase * sheenColor;
  939. finalSheen = max(finalSheen, 0.0);
  940. vec3 finalSheenScaled = finalSheen * vLightingIntensity.x * vLightingIntensity.w;
  941. // #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  942. // The sheen does not use the same BRDF so not energy conservation is possible
  943. // Should be less a problem as it is usually not metallic
  944. // finalSheenScaled *= energyConservationFactor;
  945. // #endif
  946. #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
  947. vec3 finalSheenRadiance = environmentSheenRadiance.rgb;
  948. finalSheenRadiance *= sheenEnvironmentReflectance;
  949. // Full value needed for alpha.
  950. vec3 finalSheenRadianceScaled = finalSheenRadiance * vLightingIntensity.z;
  951. #endif
  952. #endif
  953. // _____________________________ Clear Coat _______________________________________
  954. #ifdef CLEARCOAT
  955. vec3 finalClearCoat = clearCoatBase;
  956. finalClearCoat = max(finalClearCoat, 0.0);
  957. // Full value needed for alpha.
  958. vec3 finalClearCoatScaled = finalClearCoat * vLightingIntensity.x * vLightingIntensity.w;
  959. #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION)
  960. finalClearCoatScaled *= energyConservationFactorClearCoat;
  961. #endif
  962. // ____________________________ Clear Coat Radiance _______________________________
  963. #ifdef REFLECTION
  964. vec3 finalClearCoatRadiance = environmentClearCoatRadiance.rgb;
  965. finalClearCoatRadiance *= clearCoatEnvironmentReflectance;
  966. // Full value needed for alpha.
  967. vec3 finalClearCoatRadianceScaled = finalClearCoatRadiance * vLightingIntensity.z;
  968. #endif
  969. #ifdef SS_REFRACTION
  970. finalRefraction *= conservationFactor;
  971. #ifdef CLEARCOAT_TINT
  972. finalRefraction *= absorption;
  973. #endif
  974. #endif
  975. #endif
  976. // _____________________________ Highlights on Alpha _____________________________
  977. #ifdef ALPHABLEND
  978. float luminanceOverAlpha = 0.0;
  979. #if defined(REFLECTION) && defined(RADIANCEOVERALPHA)
  980. luminanceOverAlpha += getLuminance(finalRadianceScaled);
  981. #if defined(CLEARCOAT)
  982. luminanceOverAlpha += getLuminance(finalClearCoatRadianceScaled);
  983. #endif
  984. #endif
  985. #if defined(SPECULARTERM) && defined(SPECULAROVERALPHA)
  986. luminanceOverAlpha += getLuminance(finalSpecularScaled);
  987. #endif
  988. #if defined(CLEARCOAT) && defined(CLEARCOATOVERALPHA)
  989. luminanceOverAlpha += getLuminance(finalClearCoatScaled);
  990. #endif
  991. #if defined(RADIANCEOVERALPHA) || defined(SPECULAROVERALPHA)
  992. alpha = saturate(alpha + luminanceOverAlpha * luminanceOverAlpha);
  993. #endif
  994. #endif
  995. #endif
  996. // _______________ Not done before as it is unlit only __________________________
  997. // _____________________________ Diffuse ________________________________________
  998. vec3 finalDiffuse = diffuseBase;
  999. finalDiffuse *= surfaceAlbedo.rgb;
  1000. finalDiffuse = max(finalDiffuse, 0.0);
  1001. // _____________________________ Ambient ________________________________________
  1002. vec3 finalAmbient = vAmbientColor;
  1003. finalAmbient *= surfaceAlbedo.rgb;
  1004. // _____________________________ Emissive ________________________________________
  1005. vec3 finalEmissive = vEmissiveColor;
  1006. #ifdef EMISSIVE
  1007. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb;
  1008. finalEmissive *= toLinearSpace(emissiveColorTex.rgb);
  1009. finalEmissive *= vEmissiveInfos.y;
  1010. #endif
  1011. // ______________________________ Ambient ________________________________________
  1012. #ifdef AMBIENT
  1013. vec3 ambientOcclusionForDirectDiffuse = mix(vec3(1.), ambientOcclusionColor, vAmbientInfos.w);
  1014. #else
  1015. vec3 ambientOcclusionForDirectDiffuse = ambientOcclusionColor;
  1016. #endif
  1017. // _______________________________________________________________________________
  1018. // _____________________________ Composition _____________________________________
  1019. // Reflection already includes the environment intensity.
  1020. vec4 finalColor = vec4(
  1021. finalAmbient * ambientOcclusionColor +
  1022. finalDiffuse * ambientOcclusionForDirectDiffuse * vLightingIntensity.x +
  1023. #ifndef UNLIT
  1024. #ifdef REFLECTION
  1025. finalIrradiance * ambientOcclusionColor * vLightingIntensity.z +
  1026. #endif
  1027. #ifdef SPECULARTERM
  1028. // Computed in the previous step to help with alpha luminance.
  1029. // finalSpecular * vLightingIntensity.x * vLightingIntensity.w +
  1030. finalSpecularScaled +
  1031. #endif
  1032. #ifdef SHEEN
  1033. // Computed in the previous step to help with alpha luminance.
  1034. // finalSheen * vLightingIntensity.x * vLightingIntensity.w +
  1035. finalSheenScaled +
  1036. #endif
  1037. #ifdef CLEARCOAT
  1038. // Computed in the previous step to help with alpha luminance.
  1039. // finalClearCoat * vLightingIntensity.x * vLightingIntensity.w +
  1040. finalClearCoatScaled +
  1041. #endif
  1042. #ifdef REFLECTION
  1043. // Comupted in the previous step to help with alpha luminance.
  1044. // finalRadiance * vLightingIntensity.z +
  1045. finalRadianceScaled +
  1046. #if defined(SHEEN) && defined(ENVIRONMENTBRDF)
  1047. // Comupted in the previous step to help with alpha luminance.
  1048. // finalSheenRadiance * vLightingIntensity.z
  1049. finalSheenRadianceScaled +
  1050. #endif
  1051. #ifdef CLEARCOAT
  1052. // Comupted in the previous step to help with alpha luminance.
  1053. // finalClearCoatRadiance * vLightingIntensity.z
  1054. finalClearCoatRadianceScaled +
  1055. #endif
  1056. #endif
  1057. #ifdef SS_REFRACTION
  1058. finalRefraction * vLightingIntensity.z +
  1059. #endif
  1060. #endif
  1061. finalEmissive * vLightingIntensity.y,
  1062. alpha);
  1063. // _____________________________ LightMappping _____________________________________
  1064. #ifdef LIGHTMAP
  1065. #ifndef LIGHTMAPEXCLUDED
  1066. #ifdef USELIGHTMAPASSHADOWMAP
  1067. finalColor.rgb *= lightmapColor.rgb;
  1068. #else
  1069. finalColor.rgb += lightmapColor.rgb;
  1070. #endif
  1071. #endif
  1072. #endif
  1073. #define CUSTOM_FRAGMENT_BEFORE_FOG
  1074. // _____________________________ Finally ___________________________________________
  1075. finalColor = max(finalColor, 0.0);
  1076. #include<logDepthFragment>
  1077. #include<fogFragment>(color, finalColor)
  1078. #ifdef IMAGEPROCESSINGPOSTPROCESS
  1079. // Sanitize output incase invalid normals or tangents have caused div by 0 or undefined behavior
  1080. // this also limits the brightness which helpfully reduces over-sparkling in bloom (native handles this in the bloom blur shader)
  1081. finalColor.rgb = clamp(finalColor.rgb, 0., 30.0);
  1082. #else
  1083. // Alway run to ensure we are going back to gamma space.
  1084. finalColor = applyImageProcessing(finalColor);
  1085. #endif
  1086. finalColor.a *= visibility;
  1087. #ifdef PREMULTIPLYALPHA
  1088. // Convert to associative (premultiplied) format if needed.
  1089. finalColor.rgb *= finalColor.a;
  1090. #endif
  1091. #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
  1092. gl_FragColor = finalColor;
  1093. #include<pbrDebug>
  1094. }