pbr.fragment.fx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. #ifdef BUMP
  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. uniform vec3 vEyePosition;
  12. uniform vec3 vAmbientColor;
  13. uniform vec3 vReflectionColor;
  14. uniform vec4 vAlbedoColor;
  15. // CUSTOM CONTROLS
  16. uniform vec4 vLightingIntensity;
  17. uniform vec4 vCameraInfos;
  18. #ifdef OVERLOADEDVALUES
  19. uniform vec4 vOverloadedIntensity;
  20. uniform vec3 vOverloadedAmbient;
  21. uniform vec3 vOverloadedAlbedo;
  22. uniform vec3 vOverloadedReflectivity;
  23. uniform vec3 vOverloadedEmissive;
  24. uniform vec3 vOverloadedReflection;
  25. uniform vec3 vOverloadedMicroSurface;
  26. #endif
  27. #ifdef OVERLOADEDSHADOWVALUES
  28. uniform vec4 vOverloadedShadowIntensity;
  29. #endif
  30. #if defined(REFLECTION) || defined(REFRACTION)
  31. uniform vec2 vMicrosurfaceTextureLods;
  32. #endif
  33. uniform vec4 vReflectivityColor;
  34. uniform vec3 vEmissiveColor;
  35. // Input
  36. varying vec3 vPositionW;
  37. #ifdef NORMAL
  38. varying vec3 vNormalW;
  39. #endif
  40. #ifdef VERTEXCOLOR
  41. varying vec4 vColor;
  42. #endif
  43. // Lights
  44. #include<lightFragmentDeclaration>[0..maxSimultaneousLights]
  45. // Samplers
  46. #ifdef ALBEDO
  47. varying vec2 vAlbedoUV;
  48. uniform sampler2D albedoSampler;
  49. uniform vec2 vAlbedoInfos;
  50. #endif
  51. #ifdef AMBIENT
  52. varying vec2 vAmbientUV;
  53. uniform sampler2D ambientSampler;
  54. uniform vec3 vAmbientInfos;
  55. #endif
  56. #ifdef OPACITY
  57. varying vec2 vOpacityUV;
  58. uniform sampler2D opacitySampler;
  59. uniform vec2 vOpacityInfos;
  60. #endif
  61. #ifdef EMISSIVE
  62. varying vec2 vEmissiveUV;
  63. uniform vec2 vEmissiveInfos;
  64. uniform sampler2D emissiveSampler;
  65. #endif
  66. #ifdef LIGHTMAP
  67. varying vec2 vLightmapUV;
  68. uniform vec2 vLightmapInfos;
  69. uniform sampler2D lightmapSampler;
  70. #endif
  71. #if defined(REFLECTIVITY) || defined(METALLICWORKFLOW)
  72. varying vec2 vReflectivityUV;
  73. uniform vec2 vReflectivityInfos;
  74. uniform sampler2D reflectivitySampler;
  75. #endif
  76. // Fresnel
  77. #include<fresnelFunction>
  78. #ifdef OPACITYFRESNEL
  79. uniform vec4 opacityParts;
  80. #endif
  81. #ifdef EMISSIVEFRESNEL
  82. uniform vec4 emissiveLeftColor;
  83. uniform vec4 emissiveRightColor;
  84. #endif
  85. // Refraction Reflection
  86. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  87. uniform mat4 view;
  88. #endif
  89. // Refraction
  90. #ifdef REFRACTION
  91. uniform vec4 vRefractionInfos;
  92. #ifdef REFRACTIONMAP_3D
  93. uniform samplerCube refractionCubeSampler;
  94. #else
  95. uniform sampler2D refraction2DSampler;
  96. uniform mat4 refractionMatrix;
  97. #endif
  98. #endif
  99. // Reflection
  100. #ifdef REFLECTION
  101. uniform vec2 vReflectionInfos;
  102. #ifdef REFLECTIONMAP_3D
  103. uniform samplerCube reflectionCubeSampler;
  104. #else
  105. uniform sampler2D reflection2DSampler;
  106. #endif
  107. #ifdef REFLECTIONMAP_SKYBOX
  108. varying vec3 vPositionUVW;
  109. #else
  110. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  111. varying vec3 vDirectionW;
  112. #endif
  113. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  114. uniform mat4 reflectionMatrix;
  115. #endif
  116. #endif
  117. #include<reflectionFunction>
  118. #endif
  119. #ifdef CAMERACOLORGRADING
  120. #include<colorGradingDefinition>
  121. #endif
  122. #ifdef CAMERACOLORCURVES
  123. #include<colorCurvesDefinition>
  124. #endif
  125. // PBR
  126. #include<pbrShadowFunctions>
  127. #include<pbrFunctions>
  128. #ifdef CAMERACOLORGRADING
  129. #include<colorGrading>
  130. #endif
  131. #ifdef CAMERACOLORCURVES
  132. #include<colorCurves>
  133. #endif
  134. #include<harmonicsFunctions>
  135. #include<pbrLightFunctions>
  136. #include<helperFunctions>
  137. #include<bumpFragmentFunctions>
  138. #include<clipPlaneFragmentDeclaration>
  139. #include<logDepthDeclaration>
  140. // Fog
  141. #include<fogFragmentDeclaration>
  142. void main(void) {
  143. #include<clipPlaneFragment>
  144. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  145. // Bump
  146. #ifdef NORMAL
  147. vec3 normalW = normalize(vNormalW);
  148. #else
  149. vec3 normalW = vec3(1.0, 1.0, 1.0);
  150. #endif
  151. #include<bumpFragment>
  152. #ifdef TWOSIDEDLIGHTING
  153. normalW = gl_FrontFacing ? normalW : -normalW;
  154. #endif
  155. // Albedo
  156. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  157. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  158. // Alpha
  159. float alpha = vAlbedoColor.a;
  160. #ifdef ALBEDO
  161. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV + uvOffset);
  162. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  163. #ifndef LINKREFRACTIONTOTRANSPARENCY
  164. #ifdef ALPHATEST
  165. if (surfaceAlbedo.a < 0.4)
  166. discard;
  167. #endif
  168. #endif
  169. #ifdef ALPHAFROMALBEDO
  170. alpha *= surfaceAlbedo.a;
  171. #endif
  172. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  173. #else
  174. // No Albedo texture.
  175. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  176. surfaceAlbedoContribution = vec3(1., 1., 1.);
  177. #endif
  178. #ifdef VERTEXCOLOR
  179. surfaceAlbedo.rgb *= vColor.rgb;
  180. #endif
  181. #ifdef OVERLOADEDVALUES
  182. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  183. #endif
  184. // Ambient color
  185. vec3 ambientColor = vec3(1., 1., 1.);
  186. #ifdef AMBIENT
  187. ambientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
  188. ambientColor = vec3(1., 1., 1.) - ((vec3(1., 1., 1.) - ambientColor) * vAmbientInfos.z);
  189. #ifdef OVERLOADEDVALUES
  190. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  191. #endif
  192. #endif
  193. // Reflectivity map
  194. float microSurface = vReflectivityColor.a;
  195. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  196. #ifdef OVERLOADEDVALUES
  197. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  198. #endif
  199. #ifdef REFLECTIVITY
  200. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  201. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  202. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  203. #ifdef OVERLOADEDVALUES
  204. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  205. #endif
  206. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  207. microSurface = surfaceReflectivityColorMap.a;
  208. #else
  209. #ifdef MICROSURFACEAUTOMATIC
  210. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  211. #endif
  212. #endif
  213. #endif
  214. #ifdef METALLICWORKFLOW
  215. vec2 metallicRoughness = surfaceReflectivityColor.rg;
  216. #ifdef METALLICROUGHNESSMAP
  217. vec4 surfaceMetallicColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  218. // No gamma space from the metallic map in metallic workflow.
  219. metallicRoughness.r *= surfaceMetallicColorMap.r;
  220. #ifdef METALLICROUGHNESSGSTOREINALPHA
  221. metallicRoughness.g *= surfaceMetallicColorMap.a;
  222. #else
  223. #ifdef METALLICROUGHNESSGSTOREINGREEN
  224. metallicRoughness.g *= surfaceMetallicColorMap.g;
  225. #endif
  226. #endif
  227. #endif
  228. // Diffuse is used as the base of the reflectivity.
  229. vec3 baseColor = surfaceAlbedo.rgb;
  230. // Drop the surface diffuse by the 1.0 - metalness.
  231. surfaceAlbedo.rgb *= (1.0 - metallicRoughness.r);
  232. // Default specular reflectance at normal incidence.
  233. // 4% corresponds to index of refraction (IOR) of 1.50, approximately equal to glass.
  234. const vec3 DefaultSpecularReflectanceDielectric = vec3(0.04, 0.04, 0.04);
  235. // Compute the converted reflectivity.
  236. surfaceReflectivityColor = mix(DefaultSpecularReflectanceDielectric, baseColor, metallicRoughness.r);
  237. #ifdef OVERLOADEDVALUES
  238. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  239. #endif
  240. microSurface = 1.0 - metallicRoughness.g;
  241. #endif
  242. #ifdef OVERLOADEDVALUES
  243. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  244. #endif
  245. // Compute N dot V.
  246. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  247. // Adapt microSurface.
  248. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  249. // Compute roughness.
  250. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  251. // Lighting
  252. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  253. #ifdef OVERLOADEDSHADOWVALUES
  254. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  255. #endif
  256. #ifdef SPECULARTERM
  257. vec3 lightSpecularContribution = vec3(0., 0., 0.);
  258. #endif
  259. float notShadowLevel = 1.; // 1 - shadowLevel
  260. #ifdef LIGHTMAP
  261. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
  262. #endif
  263. float NdotL = -1.;
  264. lightingInfo info;
  265. // Compute reflectance.
  266. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  267. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  268. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
  269. float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
  270. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  271. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  272. #include<pbrLightFunctionsCall>[0..maxSimultaneousLights]
  273. #ifdef SPECULARTERM
  274. lightSpecularContribution *= vLightingIntensity.w;
  275. #endif
  276. #ifdef OPACITY
  277. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  278. #ifdef OPACITYRGB
  279. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  280. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  281. #else
  282. alpha *= opacityMap.a * vOpacityInfos.y;
  283. #endif
  284. #endif
  285. #ifdef VERTEXALPHA
  286. alpha *= vColor.a;
  287. #endif
  288. #ifdef OPACITYFRESNEL
  289. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  290. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  291. #endif
  292. // Refraction
  293. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  294. // Go mat -> blurry reflexion according to microSurface
  295. #ifdef LODBASEDMICROSFURACE
  296. float alphaG = convertRoughnessToAverageSlope(roughness);
  297. #endif
  298. #ifdef REFRACTION
  299. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  300. #ifdef LODBASEDMICROSFURACE
  301. #ifdef USEPMREMREFRACTION
  302. float lodRefraction = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.y, alphaG);
  303. #else
  304. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  305. #endif
  306. #else
  307. float biasRefraction = (vMicrosurfaceTextureLods.y + 2.) * (1.0 - microSurface);
  308. #endif
  309. #ifdef REFRACTIONMAP_3D
  310. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  311. if (dot(refractionVector, viewDirectionW) < 1.0)
  312. {
  313. #ifdef LODBASEDMICROSFURACE
  314. #ifdef USEPMREMREFRACTION
  315. // Empiric Threshold
  316. if ((vMicrosurfaceTextureLods.y - lodRefraction) > 4.0)
  317. {
  318. // Bend to not reach edges.
  319. float scaleRefraction = 1. - exp2(lodRefraction) / exp2(vMicrosurfaceTextureLods.y); // CubemapSize is the size of the base mipmap
  320. float maxRefraction = max(max(abs(refractionVector.x), abs(refractionVector.y)), abs(refractionVector.z));
  321. if (abs(refractionVector.x) != maxRefraction) refractionVector.x *= scaleRefraction;
  322. if (abs(refractionVector.y) != maxRefraction) refractionVector.y *= scaleRefraction;
  323. if (abs(refractionVector.z) != maxRefraction) refractionVector.z *= scaleRefraction;
  324. }
  325. #endif
  326. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  327. #else
  328. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, biasRefraction).rgb * vRefractionInfos.x;
  329. #endif
  330. }
  331. #ifndef REFRACTIONMAPINLINEARSPACE
  332. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  333. #endif
  334. #else
  335. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  336. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  337. refractionCoords.y = 1.0 - refractionCoords.y;
  338. #ifdef LODBASEDMICROSFURACE
  339. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  340. #else
  341. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, biasRefraction).rgb * vRefractionInfos.x;
  342. #endif
  343. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  344. #endif
  345. #endif
  346. // Reflection
  347. vec3 environmentRadiance = vReflectionColor.rgb;
  348. vec3 environmentIrradiance = vReflectionColor.rgb;
  349. #ifdef REFLECTION
  350. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  351. #ifdef LODBASEDMICROSFURACE
  352. #ifdef USEPMREMREFLECTION
  353. float lodReflection = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.x, alphaG);
  354. #else
  355. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  356. #endif
  357. #else
  358. float biasReflection = (vMicrosurfaceTextureLods.x + 2.) * (1.0 - microSurface);
  359. #endif
  360. #ifdef REFLECTIONMAP_3D
  361. #ifdef LODBASEDMICROSFURACE
  362. #ifdef USEPMREMREFLECTION
  363. // Empiric Threshold
  364. if ((vMicrosurfaceTextureLods.y - lodReflection) > 4.0)
  365. {
  366. // Bend to not reach edges.
  367. float scaleReflection = 1. - exp2(lodReflection) / exp2(vMicrosurfaceTextureLods.x); // CubemapSize is the size of the base mipmap
  368. float maxReflection = max(max(abs(vReflectionUVW.x), abs(vReflectionUVW.y)), abs(vReflectionUVW.z));
  369. if (abs(vReflectionUVW.x) != maxReflection) vReflectionUVW.x *= scaleReflection;
  370. if (abs(vReflectionUVW.y) != maxReflection) vReflectionUVW.y *= scaleReflection;
  371. if (abs(vReflectionUVW.z) != maxReflection) vReflectionUVW.z *= scaleReflection;
  372. }
  373. #endif
  374. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  375. #else
  376. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, biasReflection).rgb * vReflectionInfos.x;
  377. #endif
  378. #ifdef USESPHERICALFROMREFLECTIONMAP
  379. #ifndef REFLECTIONMAP_SKYBOX
  380. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  381. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  382. #endif
  383. #else
  384. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  385. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  386. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  387. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  388. #endif
  389. #else
  390. vec2 coords = vReflectionUVW.xy;
  391. #ifdef REFLECTIONMAP_PROJECTION
  392. coords /= vReflectionUVW.z;
  393. #endif
  394. coords.y = 1.0 - coords.y;
  395. #ifdef LODBASEDMICROSFURACE
  396. environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  397. #else
  398. environmentRadiance = texture2D(reflection2DSampler, coords, biasReflection).rgb * vReflectionInfos.x;
  399. #endif
  400. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  401. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  402. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  403. #endif
  404. #endif
  405. #ifdef OVERLOADEDVALUES
  406. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  407. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  408. #endif
  409. environmentRadiance *= vLightingIntensity.z;
  410. environmentIrradiance *= vLightingIntensity.z;
  411. // Specular Environment Fresnel.
  412. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  413. // Compute refractance
  414. vec3 refractance = vec3(0.0, 0.0, 0.0);
  415. #ifdef REFRACTION
  416. vec3 transmission = vec3(1.0, 1.0, 1.0);
  417. #ifdef LINKREFRACTIONTOTRANSPARENCY
  418. // Transmission based on alpha.
  419. transmission *= (1.0 - alpha);
  420. // Tint the material with albedo.
  421. // TODO. PBR Tinting.
  422. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  423. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  424. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  425. // Decrease Albedo Contribution
  426. surfaceAlbedoContribution *= alpha;
  427. // Decrease irradiance Contribution
  428. environmentIrradiance *= alpha;
  429. // Tint reflectance
  430. surfaceRefractionColor *= tint;
  431. // Put alpha back to 1;
  432. alpha = 1.0;
  433. #endif
  434. // Add Multiple internal bounces.
  435. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  436. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  437. // In theory T = 1 - R.
  438. transmission *= 1.0 - specularEnvironmentReflectance;
  439. // Should baked in diffuse.
  440. refractance = surfaceRefractionColor * transmission;
  441. #endif
  442. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  443. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  444. refractance *= vLightingIntensity.z;
  445. environmentRadiance *= specularEnvironmentReflectance;
  446. // Emissive
  447. vec3 surfaceEmissiveColor = vEmissiveColor;
  448. #ifdef EMISSIVE
  449. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb;
  450. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  451. #endif
  452. #ifdef OVERLOADEDVALUES
  453. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  454. #endif
  455. #ifdef EMISSIVEFRESNEL
  456. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  457. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  458. #endif
  459. // Composition
  460. #ifdef EMISSIVEASILLUMINATION
  461. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  462. #ifdef OVERLOADEDSHADOWVALUES
  463. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  464. #endif
  465. #else
  466. #ifdef LINKEMISSIVEWITHALBEDO
  467. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  468. #ifdef OVERLOADEDSHADOWVALUES
  469. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  470. #endif
  471. #else
  472. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  473. #ifdef OVERLOADEDSHADOWVALUES
  474. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  475. #endif
  476. #endif
  477. #endif
  478. #ifdef OVERLOADEDSHADOWVALUES
  479. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  480. #endif
  481. #ifdef SPECULARTERM
  482. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  483. #else
  484. vec3 finalSpecular = vec3(0.0);
  485. #endif
  486. #ifdef SPECULAROVERALPHA
  487. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  488. #endif
  489. #ifdef RADIANCEOVERALPHA
  490. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  491. #endif
  492. // Composition
  493. // Reflection already includes the environment intensity.
  494. #ifdef EMISSIVEASILLUMINATION
  495. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  496. #else
  497. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  498. #endif
  499. #ifdef LIGHTMAP
  500. #ifndef LIGHTMAPEXCLUDED
  501. #ifdef USELIGHTMAPASSHADOWMAP
  502. finalColor.rgb *= lightmapColor;
  503. #else
  504. finalColor.rgb += lightmapColor;
  505. #endif
  506. #endif
  507. #endif
  508. finalColor = max(finalColor, 0.0);
  509. #ifdef CAMERATONEMAP
  510. finalColor.rgb = toneMaps(finalColor.rgb);
  511. #endif
  512. finalColor.rgb = toGammaSpace(finalColor.rgb);
  513. #include<logDepthFragment>
  514. #include<fogFragment>(color, finalColor)
  515. #ifdef CAMERACONTRAST
  516. finalColor = contrasts(finalColor);
  517. #endif
  518. finalColor.rgb = clamp(finalColor.rgb, 0., 1.);
  519. #ifdef CAMERACOLORGRADING
  520. finalColor = colorGrades(finalColor);
  521. #endif
  522. #ifdef CAMERACOLORCURVES
  523. finalColor.rgb = applyColorCurves(finalColor.rgb);
  524. #endif
  525. // Normal Display.
  526. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  527. // Ambient reflection color.
  528. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  529. // Reflection color.
  530. // gl_FragColor = vec4(reflectionColor, 1.0);
  531. // Base color.
  532. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  533. // Specular color.
  534. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  535. // MicroSurface color.
  536. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  537. // Specular Map
  538. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  539. // Refractance
  540. // gl_FragColor = vec4(refractance.rgb, 1.0);
  541. //// Emissive Color
  542. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  543. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  544. gl_FragColor = finalColor;
  545. }