pbr.fragment.fx 20 KB

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