pbr.fragment.fx 19 KB

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