pbr.fragment.fx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #if defined(BUMP) || !defined(NORMAL) || defined(FORCENORMALFORWARD)
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. #ifdef LODBASEDMICROSFURACE
  5. #extension GL_EXT_shader_texture_lod : enable
  6. #endif
  7. #ifdef LOGARITHMICDEPTH
  8. #extension GL_EXT_frag_depth : enable
  9. #endif
  10. precision highp float;
  11. #include<__decl__pbrFragment>
  12. uniform vec4 vEyePosition;
  13. uniform vec3 vAmbientColor;
  14. uniform vec4 vCameraInfos;
  15. // Input
  16. varying vec3 vPositionW;
  17. #ifdef MAINUV1
  18. varying vec2 vMainUV1;
  19. #endif
  20. #ifdef MAINUV2
  21. varying vec2 vMainUV2;
  22. #endif
  23. #ifdef NORMAL
  24. varying vec3 vNormalW;
  25. #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
  26. varying vec3 vEnvironmentIrradiance;
  27. #endif
  28. #endif
  29. #ifdef VERTEXCOLOR
  30. varying vec4 vColor;
  31. #endif
  32. // Lights
  33. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  34. // Samplers
  35. #ifdef ALBEDO
  36. #if ALBEDODIRECTUV == 1
  37. #define vAlbedoUV vMainUV1
  38. #elif ALBEDODIRECTUV == 2
  39. #define vAlbedoUV vMainUV2
  40. #else
  41. varying vec2 vAlbedoUV;
  42. #endif
  43. uniform sampler2D albedoSampler;
  44. #endif
  45. #ifdef AMBIENT
  46. #if AMBIENTDIRECTUV == 1
  47. #define vAmbientUV vMainUV1
  48. #elif AMBIENTDIRECTUV == 2
  49. #define vAmbientUV vMainUV2
  50. #else
  51. varying vec2 vAmbientUV;
  52. #endif
  53. uniform sampler2D ambientSampler;
  54. #endif
  55. #ifdef OPACITY
  56. #if OPACITYDIRECTUV == 1
  57. #define vOpacityUV vMainUV1
  58. #elif OPACITYDIRECTUV == 2
  59. #define vOpacityUV vMainUV2
  60. #else
  61. varying vec2 vOpacityUV;
  62. #endif
  63. uniform sampler2D opacitySampler;
  64. #endif
  65. #ifdef EMISSIVE
  66. #if EMISSIVEDIRECTUV == 1
  67. #define vEmissiveUV vMainUV1
  68. #elif EMISSIVEDIRECTUV == 2
  69. #define vEmissiveUV vMainUV2
  70. #else
  71. varying vec2 vEmissiveUV;
  72. #endif
  73. uniform sampler2D emissiveSampler;
  74. #endif
  75. #ifdef LIGHTMAP
  76. #if LIGHTMAPDIRECTUV == 1
  77. #define vLightmapUV vMainUV1
  78. #elif LIGHTMAPDIRECTUV == 2
  79. #define vLightmapUV vMainUV2
  80. #else
  81. varying vec2 vLightmapUV;
  82. #endif
  83. uniform sampler2D lightmapSampler;
  84. #endif
  85. #ifdef REFLECTIVITY
  86. #if REFLECTIVITYDIRECTUV == 1
  87. #define vReflectivityUV vMainUV1
  88. #elif REFLECTIVITYDIRECTUV == 2
  89. #define vReflectivityUV vMainUV2
  90. #else
  91. varying vec2 vReflectivityUV;
  92. #endif
  93. uniform sampler2D reflectivitySampler;
  94. #endif
  95. #ifdef MICROSURFACEMAP
  96. #if MICROSURFACEMAPDIRECTUV == 1
  97. #define vMicroSurfaceSamplerUV vMainUV1
  98. #elif MICROSURFACEMAPDIRECTUV == 2
  99. #define vMicroSurfaceSamplerUV vMainUV2
  100. #else
  101. varying vec2 vMicroSurfaceSamplerUV;
  102. #endif
  103. uniform sampler2D microSurfaceSampler;
  104. #endif
  105. // Refraction
  106. #ifdef REFRACTION
  107. #ifdef REFRACTIONMAP_3D
  108. #define sampleRefraction(s, c) textureCube(s, c)
  109. uniform samplerCube refractionSampler;
  110. #ifdef LODBASEDMICROSFURACE
  111. #define sampleRefractionLod(s, c, l) textureCubeLodEXT(s, c, l)
  112. #else
  113. uniform samplerCube refractionSamplerLow;
  114. uniform samplerCube refractionSamplerHigh;
  115. #endif
  116. #else
  117. #define sampleRefraction(s, c) texture2D(s, c)
  118. uniform sampler2D refractionSampler;
  119. #ifdef LODBASEDMICROSFURACE
  120. #define sampleRefractionLod(s, c, l) texture2DLodEXT(s, c, l)
  121. #else
  122. uniform samplerCube refractionSamplerLow;
  123. uniform samplerCube refractionSamplerHigh;
  124. #endif
  125. #endif
  126. #endif
  127. // Reflection
  128. #ifdef REFLECTION
  129. #ifdef REFLECTIONMAP_3D
  130. #define sampleReflection(s, c) textureCube(s, c)
  131. uniform samplerCube reflectionSampler;
  132. #ifdef LODBASEDMICROSFURACE
  133. #define sampleReflectionLod(s, c, l) textureCubeLodEXT(s, c, l)
  134. #else
  135. uniform samplerCube reflectionSamplerLow;
  136. uniform samplerCube reflectionSamplerHigh;
  137. #endif
  138. #else
  139. #define sampleReflection(s, c) texture2D(s, c)
  140. uniform sampler2D reflectionSampler;
  141. #ifdef LODBASEDMICROSFURACE
  142. #define sampleReflectionLod(s, c, l) texture2DLodEXT(s, c, l)
  143. #else
  144. uniform samplerCube reflectionSamplerLow;
  145. uniform samplerCube reflectionSamplerHigh;
  146. #endif
  147. #endif
  148. #ifdef REFLECTIONMAP_SKYBOX
  149. varying vec3 vPositionUVW;
  150. #else
  151. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
  152. varying vec3 vDirectionW;
  153. #endif
  154. #endif
  155. #include<reflectionFunction>
  156. #endif
  157. #ifdef ENVIRONMENTBRDF
  158. uniform sampler2D environmentBrdfSampler;
  159. #endif
  160. // Forces linear space for image processing
  161. #ifndef FROMLINEARSPACE
  162. #define FROMLINEARSPACE;
  163. #endif
  164. #include<imageProcessingDeclaration>
  165. #include<helperFunctions>
  166. #include<imageProcessingFunctions>
  167. // PBR
  168. #include<shadowsFragmentFunctions>
  169. #include<pbrFunctions>
  170. #include<harmonicsFunctions>
  171. #include<pbrLightFunctions>
  172. #include<bumpFragmentFunctions>
  173. #include<clipPlaneFragmentDeclaration>
  174. #include<logDepthDeclaration>
  175. // Fog
  176. #include<fogFragmentDeclaration>
  177. void main(void) {
  178. #include<clipPlaneFragment>
  179. // _______________________________________________________________________________
  180. // _____________________________ Geometry Information ____________________________
  181. vec3 viewDirectionW = normalize(vEyePosition.xyz - vPositionW);
  182. #ifdef NORMAL
  183. vec3 normalW = normalize(vNormalW);
  184. #else
  185. vec3 normalW = normalize(cross(dFdx(vPositionW), dFdy(vPositionW))) * vEyePosition.w;
  186. #endif
  187. #include<bumpFragment>
  188. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  189. vec3 faceNormal = normalize(cross(dFdx(vPositionW), dFdy(vPositionW))) * vEyePosition.w;
  190. #if defined(TWOSIDEDLIGHTING)
  191. faceNormal = gl_FrontFacing ? faceNormal : -faceNormal;
  192. #endif
  193. normalW *= sign(dot(normalW, faceNormal));
  194. #endif
  195. #if defined(TWOSIDEDLIGHTING) && defined(NORMAL)
  196. normalW = gl_FrontFacing ? normalW : -normalW;
  197. #endif
  198. // _____________________________ Albedo Information ______________________________
  199. // Albedo
  200. vec3 surfaceAlbedo = vAlbedoColor.rgb;
  201. // Alpha
  202. float alpha = vAlbedoColor.a;
  203. #ifdef ALBEDO
  204. vec4 albedoTexture = texture2D(albedoSampler, vAlbedoUV + uvOffset);
  205. #if defined(ALPHAFROMALBEDO) || defined(ALPHATEST)
  206. alpha *= albedoTexture.a;
  207. #endif
  208. surfaceAlbedo *= toLinearSpace(albedoTexture.rgb);
  209. surfaceAlbedo *= vAlbedoInfos.y;
  210. #endif
  211. // _____________________________ Alpha Information _______________________________
  212. #ifdef OPACITY
  213. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  214. #ifdef OPACITYRGB
  215. alpha = getLuminance(opacityMap.rgb);
  216. #else
  217. alpha *= opacityMap.a;
  218. #endif
  219. alpha *= vOpacityInfos.y;
  220. #endif
  221. #ifdef VERTEXALPHA
  222. alpha *= vColor.a;
  223. #endif
  224. #if !defined(LINKREFRACTIONTOTRANSPARENCY) && !defined(ALPHAFRESNEL)
  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. #include<depthPrePass>
  235. #ifdef VERTEXCOLOR
  236. surfaceAlbedo *= vColor.rgb;
  237. #endif
  238. // _____________________________ AO Information _______________________________
  239. vec3 ambientOcclusionColor = vec3(1., 1., 1.);
  240. #ifdef AMBIENT
  241. vec3 ambientOcclusionColorMap = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
  242. #ifdef AMBIENTINGRAYSCALE
  243. ambientOcclusionColorMap = vec3(ambientOcclusionColorMap.r, ambientOcclusionColorMap.r, ambientOcclusionColorMap.r);
  244. #endif
  245. ambientOcclusionColor = mix(ambientOcclusionColor, ambientOcclusionColorMap, vAmbientInfos.z);
  246. #endif
  247. // _____________________________ Reflectivity Info _______________________________
  248. float microSurface = vReflectivityColor.a;
  249. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  250. #ifdef METALLICWORKFLOW
  251. vec2 metallicRoughness = surfaceReflectivityColor.rg;
  252. #ifdef REFLECTIVITY
  253. vec4 surfaceMetallicColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  254. #ifdef AOSTOREINMETALMAPRED
  255. vec3 aoStoreInMetalMap = vec3(surfaceMetallicColorMap.r, surfaceMetallicColorMap.r, surfaceMetallicColorMap.r);
  256. ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, vReflectivityInfos.z);
  257. #endif
  258. #ifdef METALLNESSSTOREINMETALMAPBLUE
  259. metallicRoughness.r *= surfaceMetallicColorMap.b;
  260. #else
  261. metallicRoughness.r *= surfaceMetallicColorMap.r;
  262. #endif
  263. #ifdef ROUGHNESSSTOREINMETALMAPALPHA
  264. metallicRoughness.g *= surfaceMetallicColorMap.a;
  265. #else
  266. #ifdef ROUGHNESSSTOREINMETALMAPGREEN
  267. metallicRoughness.g *= surfaceMetallicColorMap.g;
  268. #endif
  269. #endif
  270. #endif
  271. #ifdef MICROSURFACEMAP
  272. vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
  273. metallicRoughness.g *= microSurfaceTexel.r;
  274. #endif
  275. // Compute microsurface form roughness.
  276. microSurface = 1.0 - metallicRoughness.g;
  277. // Diffuse is used as the base of the reflectivity.
  278. vec3 baseColor = surfaceAlbedo;
  279. // Default specular reflectance at normal incidence.
  280. // 4% corresponds to index of refraction (IOR) of 1.50, approximately equal to glass.
  281. const vec3 DefaultSpecularReflectanceDielectric = vec3(0.04, 0.04, 0.04);
  282. // Compute the converted diffuse.
  283. surfaceAlbedo = mix(baseColor.rgb * (1.0 - DefaultSpecularReflectanceDielectric.r), vec3(0., 0., 0.), metallicRoughness.r);
  284. // Compute the converted reflectivity.
  285. surfaceReflectivityColor = mix(DefaultSpecularReflectanceDielectric, baseColor, metallicRoughness.r);
  286. #else
  287. #ifdef REFLECTIVITY
  288. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  289. surfaceReflectivityColor *= toLinearSpace(surfaceReflectivityColorMap.rgb);
  290. surfaceReflectivityColor *= vReflectivityInfos.y;
  291. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  292. microSurface *= surfaceReflectivityColorMap.a;
  293. microSurface *= vReflectivityInfos.z;
  294. #else
  295. #ifdef MICROSURFACEAUTOMATIC
  296. microSurface *= computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  297. #endif
  298. #ifdef MICROSURFACEMAP
  299. vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
  300. microSurface *= microSurfaceTexel.r;
  301. #endif
  302. #endif
  303. #endif
  304. #endif
  305. // Adapt microSurface.
  306. microSurface = clamp(microSurface, 0., 1.);
  307. // Compute roughness.
  308. float roughness = 1. - microSurface;
  309. // _____________________________ Alpha Fresnel ___________________________________
  310. #ifdef ALPHAFRESNEL
  311. #if defined(ALPHATEST) || defined(ALPHABLEND)
  312. // Convert approximate perceptual opacity (gamma-encoded opacity) to linear opacity (absorptance, or inverse transmission)
  313. // for use with the linear HDR render target. The final composition will be converted back to gamma encoded values for eventual display.
  314. // 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.
  315. float opacityPerceptual = alpha;
  316. #ifdef LINEARALPHAFRESNEL
  317. float opacity0 = opacityPerceptual;
  318. #else
  319. float opacity0 = opacityPerceptual * opacityPerceptual;
  320. #endif
  321. float opacity90 = fresnelGrazingReflectance(opacity0);
  322. vec3 normalForward = faceforward(normalW, -viewDirectionW, normalW);
  323. // Calculate the appropriate linear opacity for the current viewing angle (formally, this quantity is the "directional absorptance").
  324. alpha = fresnelSchlickEnvironmentGGX(clamp(dot(viewDirectionW, normalForward), 0.0, 1.0), vec3(opacity0), vec3(opacity90), sqrt(microSurface)).x;
  325. #ifdef ALPHATEST
  326. if (alpha <= ALPHATESTVALUE)
  327. discard;
  328. #ifndef ALPHABLEND
  329. // Prevent to blend with the canvas.
  330. alpha = 1.0;
  331. #endif
  332. #endif
  333. #endif
  334. #endif
  335. // _____________________________ Compute LODs Fetch ____________________________________
  336. // Compute N dot V.
  337. float NdotVUnclamped = dot(normalW, viewDirectionW);
  338. float NdotV = clamp(NdotVUnclamped,0., 1.) + 0.00001;
  339. float alphaG = convertRoughnessToAverageSlope(roughness);
  340. // _____________________________ Refraction Info _______________________________________
  341. #ifdef REFRACTION
  342. vec3 environmentRefraction = vec3(0., 0., 0.);
  343. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  344. #ifdef REFRACTIONMAP_OPPOSITEZ
  345. refractionVector.z *= -1.0;
  346. #endif
  347. // _____________________________ 2D vs 3D Maps ________________________________
  348. #ifdef REFRACTIONMAP_3D
  349. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  350. vec3 refractionCoords = refractionVector;
  351. refractionCoords = vec3(refractionMatrix * vec4(refractionCoords, 0));
  352. #else
  353. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  354. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  355. refractionCoords.y = 1.0 - refractionCoords.y;
  356. #endif
  357. #ifdef LODINREFRACTIONALPHA
  358. float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
  359. #else
  360. float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG, 1.0);
  361. #endif
  362. #ifdef LODBASEDMICROSFURACE
  363. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  364. refractionLOD = refractionLOD * vRefractionMicrosurfaceInfos.y + vRefractionMicrosurfaceInfos.z;
  365. #ifdef LODINREFRACTIONALPHA
  366. // Automatic LOD adjustment to ensure that the smoothness-based environment LOD selection
  367. // is constrained to appropriate LOD levels in order to prevent aliasing.
  368. // The environment map is first sampled without custom LOD selection to determine
  369. // the hardware-selected LOD, and this is then used to constrain the final LOD selection
  370. // so that excessive surface smoothness does not cause aliasing (e.g. on curved geometry
  371. // where the normal is varying rapidly).
  372. // Note: Shader Model 4.1 or higher can provide this directly via CalculateLevelOfDetail(), and
  373. // manual calculation via derivatives is also possible, but for simplicity we use the
  374. // hardware LOD calculation with the alpha channel containing the LOD for each mipmap.
  375. float automaticRefractionLOD = UNPACK_LOD(sampleRefraction(refractionSampler, refractionCoords).a);
  376. float requestedRefractionLOD = max(automaticRefractionLOD, refractionLOD);
  377. #else
  378. float requestedRefractionLOD = refractionLOD;
  379. #endif
  380. environmentRefraction = sampleRefractionLod(refractionSampler, refractionCoords, requestedRefractionLOD).rgb;
  381. #else
  382. float lodRefractionNormalized = clamp(refractionLOD / log2(vRefractionMicrosurfaceInfos.x), 0., 1.);
  383. float lodRefractionNormalizedDoubled = lodRefractionNormalized * 2.0;
  384. vec3 environmentRefractionMid = sampleRefraction(refractionSampler, refractionCoords).rgb;
  385. if(lodRefractionNormalizedDoubled < 1.0){
  386. environmentRefraction = mix(
  387. sampleRefraction(refractionSamplerHigh, refractionCoords).rgb,
  388. environmentRefractionMid,
  389. lodRefractionNormalizedDoubled
  390. );
  391. }else{
  392. environmentRefraction = mix(
  393. environmentRefractionMid,
  394. sampleRefraction(refractionSamplerLow, refractionCoords).rgb,
  395. lodRefractionNormalizedDoubled - 1.0
  396. );
  397. }
  398. #endif
  399. #ifdef GAMMAREFRACTION
  400. environmentRefraction = toLinearSpace(environmentRefraction.rgb);
  401. #endif
  402. // _____________________________ Levels _____________________________________
  403. environmentRefraction *= vRefractionInfos.x;
  404. #endif
  405. // _____________________________ Reflection Info _______________________________________
  406. #ifdef REFLECTION
  407. vec3 environmentRadiance = vec3(0., 0., 0.);
  408. vec3 environmentIrradiance = vec3(0., 0., 0.);
  409. vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  410. #ifdef REFLECTIONMAP_OPPOSITEZ
  411. reflectionVector.z *= -1.0;
  412. #endif
  413. // _____________________________ 2D vs 3D Maps ________________________________
  414. #ifdef REFLECTIONMAP_3D
  415. vec3 reflectionCoords = reflectionVector;
  416. #else
  417. vec2 reflectionCoords = reflectionVector.xy;
  418. #ifdef REFLECTIONMAP_PROJECTION
  419. reflectionCoords /= reflectionVector.z;
  420. #endif
  421. reflectionCoords.y = 1.0 - reflectionCoords.y;
  422. #endif
  423. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  424. float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
  425. #else
  426. float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, 1.);
  427. #endif
  428. #ifdef LODBASEDMICROSFURACE
  429. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  430. reflectionLOD = reflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  431. #ifdef LODINREFLECTIONALPHA
  432. // Automatic LOD adjustment to ensure that the smoothness-based environment LOD selection
  433. // is constrained to appropriate LOD levels in order to prevent aliasing.
  434. // The environment map is first sampled without custom LOD selection to determine
  435. // the hardware-selected LOD, and this is then used to constrain the final LOD selection
  436. // so that excessive surface smoothness does not cause aliasing (e.g. on curved geometry
  437. // where the normal is varying rapidly).
  438. // Note: Shader Model 4.1 or higher can provide this directly via CalculateLevelOfDetail(), and
  439. // manual calculation via derivatives is also possible, but for simplicity we use the
  440. // hardware LOD calculation with the alpha channel containing the LOD for each mipmap.
  441. float automaticReflectionLOD = UNPACK_LOD(sampleReflection(reflectionSampler, reflectionCoords).a);
  442. float requestedReflectionLOD = max(automaticReflectionLOD, reflectionLOD);
  443. #else
  444. float requestedReflectionLOD = reflectionLOD;
  445. #endif
  446. environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, requestedReflectionLOD).rgb;
  447. #else
  448. float lodReflectionNormalized = clamp(reflectionLOD / log2(vReflectionMicrosurfaceInfos.x), 0., 1.);
  449. float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
  450. vec3 environmentSpecularMid = sampleReflection(reflectionSampler, reflectionCoords).rgb;
  451. if(lodReflectionNormalizedDoubled < 1.0){
  452. environmentRadiance = mix(
  453. sampleReflection(reflectionSamplerHigh, reflectionCoords).rgb,
  454. environmentSpecularMid,
  455. lodReflectionNormalizedDoubled
  456. );
  457. }else{
  458. environmentRadiance = mix(
  459. environmentSpecularMid,
  460. sampleReflection(reflectionSamplerLow, reflectionCoords).rgb,
  461. lodReflectionNormalizedDoubled - 1.0
  462. );
  463. }
  464. #endif
  465. #ifdef GAMMAREFLECTION
  466. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  467. #endif
  468. // _____________________________ Irradiance ________________________________
  469. #ifdef USESPHERICALFROMREFLECTIONMAP
  470. #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
  471. environmentIrradiance = vEnvironmentIrradiance;
  472. #else
  473. vec3 irradianceVector = vec3(reflectionMatrix * vec4(normalW, 0)).xyz;
  474. #ifdef REFLECTIONMAP_OPPOSITEZ
  475. irradianceVector.z *= -1.0;
  476. #endif
  477. environmentIrradiance = environmentIrradianceJones(irradianceVector);
  478. #endif
  479. #endif
  480. // _____________________________ Levels _____________________________________
  481. environmentRadiance *= vReflectionInfos.x;
  482. environmentRadiance *= vReflectionColor.rgb;
  483. environmentIrradiance *= vReflectionColor.rgb;
  484. #endif
  485. // ____________________________________________________________________________________
  486. // _____________________________ Direct Lighting Param ________________________________
  487. // Compute reflectance.
  488. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  489. float reflectance90 = fresnelGrazingReflectance(reflectance);
  490. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  491. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  492. // _____________________________ Direct Lighting Info __________________________________
  493. vec3 diffuseBase = vec3(0., 0., 0.);
  494. #ifdef SPECULARTERM
  495. vec3 specularBase = vec3(0., 0., 0.);
  496. #endif
  497. #ifdef LIGHTMAP
  498. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
  499. #endif
  500. lightingInfo info;
  501. float shadow = 1.; // 1 - shadowLevel
  502. float NdotL = -1.;
  503. #include<lightFragment>[0..maxSimultaneousLights]
  504. // _________________________ Specular Environment Oclusion __________________________
  505. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  506. // Indexed on cos(theta) and roughness
  507. vec2 brdfSamplerUV = vec2(NdotV, roughness);
  508. // We can find the scale and offset to apply to the specular value.
  509. vec4 environmentBrdf = texture2D(environmentBrdfSampler, brdfSamplerUV);
  510. vec3 specularEnvironmentReflectance = specularEnvironmentR0 * environmentBrdf.x + environmentBrdf.y;
  511. #ifdef RADIANCEOCCLUSION
  512. #ifdef AMBIENTINGRAYSCALE
  513. float ambientMonochrome = ambientOcclusionColor.r;
  514. #else
  515. float ambientMonochrome = getLuminance(ambientOcclusionColor);
  516. #endif
  517. float seo = environmentRadianceOcclusion(ambientMonochrome, NdotVUnclamped);
  518. specularEnvironmentReflectance *= seo;
  519. #endif
  520. #ifdef HORIZONOCCLUSION
  521. #ifdef BUMP
  522. #ifdef REFLECTIONMAP_3D
  523. float eho = environmentHorizonOcclusion(reflectionCoords, normalW);
  524. specularEnvironmentReflectance *= eho;
  525. #endif
  526. #endif
  527. #endif
  528. #else
  529. // Jones implementation of a well balanced fast analytical solution.
  530. vec3 specularEnvironmentReflectance = fresnelSchlickEnvironmentGGX(NdotV, specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  531. #endif
  532. // _____________________________ Refractance+Tint ________________________________
  533. #ifdef REFRACTION
  534. vec3 refractance = vec3(0.0, 0.0, 0.0);
  535. vec3 transmission = vec3(1.0, 1.0, 1.0);
  536. #ifdef LINKREFRACTIONTOTRANSPARENCY
  537. // Transmission based on alpha.
  538. transmission *= (1.0 - alpha);
  539. // Tint the material with albedo.
  540. // TODO. PBR Tinting.
  541. vec3 mixedAlbedo = surfaceAlbedo;
  542. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  543. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  544. // Decrease Albedo Contribution
  545. surfaceAlbedo *= alpha;
  546. // Decrease irradiance Contribution
  547. environmentIrradiance *= alpha;
  548. // Tint reflectance
  549. environmentRefraction *= tint;
  550. // Put alpha back to 1;
  551. alpha = 1.0;
  552. #endif
  553. // Add Multiple internal bounces.
  554. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  555. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  556. // In theory T = 1 - R.
  557. transmission *= 1.0 - specularEnvironmentReflectance;
  558. // Should baked in diffuse.
  559. refractance = transmission;
  560. #endif
  561. // ______________________________________________________________________________
  562. // _____________________________ Energy Conservation ___________________________
  563. // Apply Energy Conservation taking in account the environment level only if
  564. // the environment is present.
  565. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  566. // _____________________________ Diffuse ________________________________________
  567. vec3 finalDiffuse = diffuseBase;
  568. finalDiffuse.rgb += vAmbientColor;
  569. finalDiffuse *= surfaceAlbedo.rgb;
  570. finalDiffuse = max(finalDiffuse, 0.0);
  571. // _____________________________ Irradiance ______________________________________
  572. #ifdef REFLECTION
  573. vec3 finalIrradiance = environmentIrradiance;
  574. finalIrradiance *= surfaceAlbedo.rgb;
  575. #endif
  576. // _____________________________ Specular ________________________________________
  577. #ifdef SPECULARTERM
  578. vec3 finalSpecular = specularBase;
  579. finalSpecular = max(finalSpecular, 0.0);
  580. // Full value needed for alpha.
  581. vec3 finalSpecularScaled = finalSpecular * vLightingIntensity.x * vLightingIntensity.w;
  582. #endif
  583. // _____________________________ Radiance_________________________________________
  584. #ifdef REFLECTION
  585. vec3 finalRadiance = environmentRadiance;
  586. finalRadiance *= specularEnvironmentReflectance;
  587. // Full value needed for alpha.
  588. vec3 finalRadianceScaled = finalRadiance * vLightingIntensity.z;
  589. #endif
  590. // _____________________________ Refraction ______________________________________
  591. #ifdef REFRACTION
  592. vec3 finalRefraction = environmentRefraction;
  593. finalRefraction *= refractance;
  594. #endif
  595. // _____________________________ Emissive ________________________________________
  596. vec3 finalEmissive = vEmissiveColor;
  597. #ifdef EMISSIVE
  598. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb;
  599. finalEmissive *= toLinearSpace(emissiveColorTex.rgb);
  600. finalEmissive *= vEmissiveInfos.y;
  601. #endif
  602. // _____________________________ Highlights on Alpha _____________________________
  603. #ifdef ALPHABLEND
  604. float luminanceOverAlpha = 0.0;
  605. #if defined(REFLECTION) && defined(RADIANCEOVERALPHA)
  606. luminanceOverAlpha += getLuminance(finalRadianceScaled);
  607. #endif
  608. #if defined(SPECULARTERM) && defined(SPECULAROVERALPHA)
  609. luminanceOverAlpha += getLuminance(finalSpecularScaled);
  610. #endif
  611. #if defined(RADIANCEOVERALPHA) || defined(SPECULAROVERALPHA)
  612. alpha = clamp(alpha + luminanceOverAlpha * luminanceOverAlpha, 0., 1.);
  613. #endif
  614. #endif
  615. // _______________________________________________________________________________
  616. // _____________________________ Composition _____________________________________
  617. // Reflection already includes the environment intensity.
  618. vec4 finalColor = vec4(finalDiffuse * ambientOcclusionColor * vLightingIntensity.x +
  619. #ifdef REFLECTION
  620. finalIrradiance * ambientOcclusionColor * vLightingIntensity.z +
  621. #endif
  622. #ifdef SPECULARTERM
  623. // Computed in the previous step to help with alpha luminance.
  624. // finalSpecular * vLightingIntensity.x * vLightingIntensity.w +
  625. finalSpecularScaled +
  626. #endif
  627. #ifdef REFLECTION
  628. // Comupted in the previous step to help with alpha luminance.
  629. // finalRadiance * vLightingIntensity.z +
  630. finalRadianceScaled +
  631. #endif
  632. #ifdef REFRACTION
  633. finalRefraction * vLightingIntensity.z +
  634. #endif
  635. finalEmissive * vLightingIntensity.y,
  636. alpha);
  637. // _____________________________ LightMappping _____________________________________
  638. #ifdef LIGHTMAP
  639. #ifndef LIGHTMAPEXCLUDED
  640. #ifdef USELIGHTMAPASSHADOWMAP
  641. finalColor.rgb *= lightmapColor;
  642. #else
  643. finalColor.rgb += lightmapColor;
  644. #endif
  645. #endif
  646. #endif
  647. // _____________________________ Finally ___________________________________________
  648. finalColor = max(finalColor, 0.0);
  649. #include<logDepthFragment>
  650. #include<fogFragment>(color, finalColor)
  651. #ifdef IMAGEPROCESSINGPOSTPROCESS
  652. // Sanitize output incase invalid normals or tangents have caused div by 0 or undefined behavior
  653. // this also limits the brightness which helpfully reduces over-sparkling in bloom (native handles this in the bloom blur shader)
  654. finalColor.rgb = clamp(finalColor.rgb, 0., 30.0);
  655. #else
  656. // Alway run even to ensure going back to gamma space.
  657. finalColor = applyImageProcessing(finalColor);
  658. #endif
  659. #ifdef PREMULTIPLYALPHA
  660. // Convert to associative (premultiplied) format if needed.
  661. finalColor.rgb *= finalColor.a;
  662. #endif
  663. gl_FragColor = finalColor;
  664. // Normal Display.
  665. //gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  666. // Ambient reflection color.
  667. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  668. // Reflection color.
  669. // gl_FragColor = vec4(reflectionColor, 1.0);
  670. // Base color.
  671. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  672. // Diffuse Direct Lighting
  673. // gl_FragColor = vec4(diffuseBase.rgb, 1.0);
  674. // Specular Lighting
  675. // gl_FragColor = vec4(specularBase.rgb, 1.0);
  676. // Final Specular
  677. // gl_FragColor = vec4(finalSpecular.rgb, 1.0);
  678. // Irradiance
  679. //gl_FragColor = vec4(specularEnvironmentReflectance.rgb, 1.0);
  680. //gl_FragColor = vec4(environmentIrradiance.rgb / 3.0, 1.0);
  681. // Specular color.
  682. //gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  683. // MicroSurface color.
  684. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  685. // Roughness.
  686. // gl_FragColor = vec4(roughness, roughness, roughness, 1.0);
  687. // Specular Map
  688. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  689. // Refractance
  690. // gl_FragColor = vec4(refractance.rgb, 1.0);
  691. //// Emissive Color
  692. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  693. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  694. // Specular Environment Occlusion
  695. //gl_FragColor = vec4(seo, seo, seo, 1.0);
  696. //// Horizon Environment Occlusion
  697. //gl_FragColor = vec4(eho, eho, eho, 1.0);
  698. //gl_FragColor = vec4(seo * eho, seo * eho, seo * eho, 1.0);
  699. }