pbr.fragment.fx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 vec2 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)
  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<bumpFragmentFunctions>
  137. #include<clipPlaneFragmentDeclaration>
  138. #include<logDepthDeclaration>
  139. // Fog
  140. #include<fogFragmentDeclaration>
  141. void main(void) {
  142. #include<clipPlaneFragment>
  143. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  144. // Albedo
  145. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  146. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  147. // Alpha
  148. float alpha = vAlbedoColor.a;
  149. #ifdef ALBEDO
  150. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);
  151. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  152. #ifndef LINKREFRACTIONTOTRANSPARENCY
  153. #ifdef ALPHATEST
  154. if (surfaceAlbedo.a < 0.4)
  155. discard;
  156. #endif
  157. #endif
  158. #ifdef ALPHAFROMALBEDO
  159. alpha *= surfaceAlbedo.a;
  160. #endif
  161. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  162. #else
  163. // No Albedo texture.
  164. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  165. surfaceAlbedoContribution = vec3(1., 1., 1.);
  166. #endif
  167. #ifdef VERTEXCOLOR
  168. surfaceAlbedo.rgb *= vColor.rgb;
  169. #endif
  170. #ifdef OVERLOADEDVALUES
  171. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  172. #endif
  173. // Bump
  174. #ifdef NORMAL
  175. vec3 normalW = normalize(vNormalW);
  176. #else
  177. vec3 normalW = vec3(1.0, 1.0, 1.0);
  178. #endif
  179. #include<bumpFragment>
  180. // Ambient color
  181. vec3 ambientColor = vec3(1., 1., 1.);
  182. #ifdef AMBIENT
  183. ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  184. #ifdef OVERLOADEDVALUES
  185. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  186. #endif
  187. #endif
  188. // Reflectivity map
  189. float microSurface = vReflectivityColor.a;
  190. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  191. #ifdef OVERLOADEDVALUES
  192. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  193. #endif
  194. #ifdef REFLECTIVITY
  195. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);
  196. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  197. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  198. #ifdef OVERLOADEDVALUES
  199. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  200. #endif
  201. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  202. microSurface = surfaceReflectivityColorMap.a;
  203. #else
  204. #ifdef MICROSURFACEAUTOMATIC
  205. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  206. #endif
  207. #endif
  208. #endif
  209. #ifdef OVERLOADEDVALUES
  210. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  211. #endif
  212. // Compute N dot V.
  213. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  214. // Adapt microSurface.
  215. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  216. // Compute roughness.
  217. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  218. // Lighting
  219. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  220. #ifdef OVERLOADEDSHADOWVALUES
  221. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  222. #endif
  223. #ifdef SPECULARTERM
  224. vec3 lightSpecularContribution = vec3(0., 0., 0.);
  225. #endif
  226. float notShadowLevel = 1.; // 1 - shadowLevel
  227. #ifdef LIGHTMAP
  228. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  229. #endif
  230. float NdotL = -1.;
  231. lightingInfo info;
  232. // Compute reflectance.
  233. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  234. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  235. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
  236. float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
  237. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  238. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  239. #include<pbrLightFunctionsCall>[0..maxSimultaneousLights]
  240. #ifdef SPECULARTERM
  241. lightSpecularContribution *= vLightingIntensity.w;
  242. #endif
  243. #ifdef OPACITY
  244. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  245. #ifdef OPACITYRGB
  246. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  247. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  248. #else
  249. alpha *= opacityMap.a * vOpacityInfos.y;
  250. #endif
  251. #endif
  252. #ifdef VERTEXALPHA
  253. alpha *= vColor.a;
  254. #endif
  255. #ifdef OPACITYFRESNEL
  256. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  257. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  258. #endif
  259. // Refraction
  260. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  261. // Go mat -> blurry reflexion according to microSurface
  262. #ifdef LODBASEDMICROSFURACE
  263. float alphaG = convertRoughnessToAverageSlope(roughness);
  264. #endif
  265. #ifdef REFRACTION
  266. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  267. #ifdef LODBASEDMICROSFURACE
  268. #ifdef USEPMREMREFRACTION
  269. float lodRefraction = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.y, alphaG);
  270. #else
  271. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  272. #endif
  273. #else
  274. float biasRefraction = (vMicrosurfaceTextureLods.y + 2.) * (1.0 - microSurface);
  275. #endif
  276. #ifdef REFRACTIONMAP_3D
  277. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  278. if (dot(refractionVector, viewDirectionW) < 1.0)
  279. {
  280. #ifdef LODBASEDMICROSFURACE
  281. #ifdef USEPMREMREFRACTION
  282. // Empiric Threshold
  283. if ((vMicrosurfaceTextureLods.y - lodRefraction) > 4.0)
  284. {
  285. // Bend to not reach edges.
  286. float scaleRefraction = 1. - exp2(lodRefraction) / exp2(vMicrosurfaceTextureLods.y); // CubemapSize is the size of the base mipmap
  287. float maxRefraction = max(max(abs(refractionVector.x), abs(refractionVector.y)), abs(refractionVector.z));
  288. if (abs(refractionVector.x) != maxRefraction) refractionVector.x *= scaleRefraction;
  289. if (abs(refractionVector.y) != maxRefraction) refractionVector.y *= scaleRefraction;
  290. if (abs(refractionVector.z) != maxRefraction) refractionVector.z *= scaleRefraction;
  291. }
  292. #endif
  293. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  294. #else
  295. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, biasRefraction).rgb * vRefractionInfos.x;
  296. #endif
  297. }
  298. #ifndef REFRACTIONMAPINLINEARSPACE
  299. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  300. #endif
  301. #else
  302. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  303. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  304. refractionCoords.y = 1.0 - refractionCoords.y;
  305. #ifdef LODBASEDMICROSFURACE
  306. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  307. #else
  308. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, biasRefraction).rgb * vRefractionInfos.x;
  309. #endif
  310. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  311. #endif
  312. #endif
  313. // Reflection
  314. vec3 environmentRadiance = vReflectionColor.rgb;
  315. vec3 environmentIrradiance = vReflectionColor.rgb;
  316. #ifdef REFLECTION
  317. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  318. #ifdef LODBASEDMICROSFURACE
  319. #ifdef USEPMREMREFLECTION
  320. float lodReflection = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.x, alphaG);
  321. #else
  322. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  323. #endif
  324. #else
  325. float biasReflection = (vMicrosurfaceTextureLods.x + 2.) * (1.0 - microSurface);
  326. #endif
  327. #ifdef REFLECTIONMAP_3D
  328. #ifdef LODBASEDMICROSFURACE
  329. #ifdef USEPMREMREFLECTION
  330. // Empiric Threshold
  331. if ((vMicrosurfaceTextureLods.y - lodReflection) > 4.0)
  332. {
  333. // Bend to not reach edges.
  334. float scaleReflection = 1. - exp2(lodReflection) / exp2(vMicrosurfaceTextureLods.x); // CubemapSize is the size of the base mipmap
  335. float maxReflection = max(max(abs(vReflectionUVW.x), abs(vReflectionUVW.y)), abs(vReflectionUVW.z));
  336. if (abs(vReflectionUVW.x) != maxReflection) vReflectionUVW.x *= scaleReflection;
  337. if (abs(vReflectionUVW.y) != maxReflection) vReflectionUVW.y *= scaleReflection;
  338. if (abs(vReflectionUVW.z) != maxReflection) vReflectionUVW.z *= scaleReflection;
  339. }
  340. #endif
  341. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  342. #else
  343. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, biasReflection).rgb * vReflectionInfos.x;
  344. #endif
  345. #ifdef USESPHERICALFROMREFLECTIONMAP
  346. #ifndef REFLECTIONMAP_SKYBOX
  347. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  348. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  349. #endif
  350. #else
  351. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  352. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  353. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  354. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  355. #endif
  356. #else
  357. vec2 coords = vReflectionUVW.xy;
  358. #ifdef REFLECTIONMAP_PROJECTION
  359. coords /= vReflectionUVW.z;
  360. #endif
  361. coords.y = 1.0 - coords.y;
  362. #ifdef LODBASEDMICROSFURACE
  363. environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  364. #else
  365. environmentRadiance = texture2D(reflection2DSampler, coords, biasReflection).rgb * vReflectionInfos.x;
  366. #endif
  367. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  368. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  369. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  370. #endif
  371. #endif
  372. #ifdef OVERLOADEDVALUES
  373. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  374. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  375. #endif
  376. environmentRadiance *= vLightingIntensity.z;
  377. environmentIrradiance *= vLightingIntensity.z;
  378. // Specular Environment Fresnel.
  379. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  380. // Compute refractance
  381. vec3 refractance = vec3(0.0, 0.0, 0.0);
  382. #ifdef REFRACTION
  383. vec3 transmission = vec3(1.0, 1.0, 1.0);
  384. #ifdef LINKREFRACTIONTOTRANSPARENCY
  385. // Transmission based on alpha.
  386. transmission *= (1.0 - alpha);
  387. // Tint the material with albedo.
  388. // TODO. PBR Tinting.
  389. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  390. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  391. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  392. // Decrease Albedo Contribution
  393. surfaceAlbedoContribution *= alpha;
  394. // Decrease irradiance Contribution
  395. environmentIrradiance *= alpha;
  396. // Tint reflectance
  397. surfaceRefractionColor *= tint;
  398. // Put alpha back to 1;
  399. alpha = 1.0;
  400. #endif
  401. // Add Multiple internal bounces.
  402. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  403. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  404. // In theory T = 1 - R.
  405. transmission *= 1.0 - specularEnvironmentReflectance;
  406. // Should baked in diffuse.
  407. refractance = surfaceRefractionColor * transmission;
  408. #endif
  409. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  410. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  411. refractance *= vLightingIntensity.z;
  412. environmentRadiance *= specularEnvironmentReflectance;
  413. // Emissive
  414. vec3 surfaceEmissiveColor = vEmissiveColor;
  415. #ifdef EMISSIVE
  416. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  417. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  418. #endif
  419. #ifdef OVERLOADEDVALUES
  420. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  421. #endif
  422. #ifdef EMISSIVEFRESNEL
  423. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  424. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  425. #endif
  426. // Composition
  427. #ifdef EMISSIVEASILLUMINATION
  428. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  429. #ifdef OVERLOADEDSHADOWVALUES
  430. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  431. #endif
  432. #else
  433. #ifdef LINKEMISSIVEWITHALBEDO
  434. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  435. #ifdef OVERLOADEDSHADOWVALUES
  436. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  437. #endif
  438. #else
  439. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  440. #ifdef OVERLOADEDSHADOWVALUES
  441. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  442. #endif
  443. #endif
  444. #endif
  445. #ifdef OVERLOADEDSHADOWVALUES
  446. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  447. #endif
  448. #ifdef SPECULARTERM
  449. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  450. #else
  451. vec3 finalSpecular = vec3(0.0);
  452. #endif
  453. #ifdef SPECULAROVERALPHA
  454. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  455. #endif
  456. #ifdef RADIANCEOVERALPHA
  457. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  458. #endif
  459. // Composition
  460. // Reflection already includes the environment intensity.
  461. #ifdef EMISSIVEASILLUMINATION
  462. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  463. #else
  464. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  465. #endif
  466. #ifdef LIGHTMAP
  467. #ifndef LIGHTMAPEXCLUDED
  468. #ifdef USELIGHTMAPASSHADOWMAP
  469. finalColor.rgb *= lightmapColor;
  470. #else
  471. finalColor.rgb += lightmapColor;
  472. #endif
  473. #endif
  474. #endif
  475. finalColor = max(finalColor, 0.0);
  476. #ifdef CAMERATONEMAP
  477. finalColor.rgb = toneMaps(finalColor.rgb);
  478. #endif
  479. finalColor.rgb = toGammaSpace(finalColor.rgb);
  480. #include<logDepthFragment>
  481. #include<fogFragment>(color, finalColor)
  482. #ifdef CAMERACONTRAST
  483. finalColor = contrasts(finalColor);
  484. #endif
  485. finalColor.rgb = clamp(finalColor.rgb, 0., 1.);
  486. #ifdef CAMERACOLORGRADING
  487. finalColor = colorGrades(finalColor);
  488. #endif
  489. #ifdef CAMERACOLORCURVES
  490. finalColor.rgb = applyColorCurves(finalColor.rgb);
  491. #endif
  492. // Normal Display.
  493. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  494. // Ambient reflection color.
  495. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  496. // Reflection color.
  497. // gl_FragColor = vec4(reflectionColor, 1.0);
  498. // Base color.
  499. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  500. // Specular color.
  501. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  502. // MicroSurface color.
  503. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  504. // Specular Map
  505. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  506. // Refractance
  507. // gl_FragColor = vec4(refractance.rgb, 1.0);
  508. //// Emissive Color
  509. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  510. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  511. gl_FragColor = finalColor;
  512. }