pbr.fragment.fx 53 KB

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