default.fragment.fx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #ifdef BUMP
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. #ifdef LOGARITHMICDEPTH
  5. #extension GL_EXT_frag_depth : enable
  6. #endif
  7. // Constants
  8. #define RECIPROCAL_PI2 0.15915494
  9. uniform vec3 vEyePosition;
  10. uniform vec3 vAmbientColor;
  11. uniform vec4 vDiffuseColor;
  12. #ifdef SPECULARTERM
  13. uniform vec4 vSpecularColor;
  14. #endif
  15. uniform vec3 vEmissiveColor;
  16. // Input
  17. varying vec3 vPositionW;
  18. #ifdef NORMAL
  19. varying vec3 vNormalW;
  20. #endif
  21. #ifdef VERTEXCOLOR
  22. varying vec4 vColor;
  23. #endif
  24. // Helper functions
  25. #include<helperFunctions>
  26. // Lights
  27. #include<lightFragmentDeclaration>[0..maxSimultaneousLights]
  28. #include<lightsFragmentFunctions>
  29. #include<shadowsFragmentFunctions>
  30. // Samplers
  31. #ifdef DIFFUSE
  32. varying vec2 vDiffuseUV;
  33. uniform sampler2D diffuseSampler;
  34. uniform vec2 vDiffuseInfos;
  35. #endif
  36. #ifdef AMBIENT
  37. varying vec2 vAmbientUV;
  38. uniform sampler2D ambientSampler;
  39. uniform vec2 vAmbientInfos;
  40. #endif
  41. #ifdef OPACITY
  42. varying vec2 vOpacityUV;
  43. uniform sampler2D opacitySampler;
  44. uniform vec2 vOpacityInfos;
  45. #endif
  46. #ifdef EMISSIVE
  47. varying vec2 vEmissiveUV;
  48. uniform vec2 vEmissiveInfos;
  49. uniform sampler2D emissiveSampler;
  50. #endif
  51. #ifdef LIGHTMAP
  52. varying vec2 vLightmapUV;
  53. uniform vec2 vLightmapInfos;
  54. uniform sampler2D lightmapSampler;
  55. #endif
  56. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  57. uniform mat4 view;
  58. #endif
  59. #ifdef REFRACTION
  60. uniform vec4 vRefractionInfos;
  61. #ifdef REFRACTIONMAP_3D
  62. uniform samplerCube refractionCubeSampler;
  63. #else
  64. uniform sampler2D refraction2DSampler;
  65. uniform mat4 refractionMatrix;
  66. #endif
  67. #ifdef REFRACTIONFRESNEL
  68. uniform vec4 refractionLeftColor;
  69. uniform vec4 refractionRightColor;
  70. #endif
  71. #endif
  72. #if defined(SPECULAR) && defined(SPECULARTERM)
  73. varying vec2 vSpecularUV;
  74. uniform vec2 vSpecularInfos;
  75. uniform sampler2D specularSampler;
  76. #endif
  77. // Fresnel
  78. #include<fresnelFunction>
  79. #ifdef DIFFUSEFRESNEL
  80. uniform vec4 diffuseLeftColor;
  81. uniform vec4 diffuseRightColor;
  82. #endif
  83. #ifdef OPACITYFRESNEL
  84. uniform vec4 opacityParts;
  85. #endif
  86. #ifdef EMISSIVEFRESNEL
  87. uniform vec4 emissiveLeftColor;
  88. uniform vec4 emissiveRightColor;
  89. #endif
  90. // Reflection
  91. #ifdef REFLECTION
  92. uniform vec2 vReflectionInfos;
  93. #ifdef REFLECTIONMAP_3D
  94. uniform samplerCube reflectionCubeSampler;
  95. #else
  96. uniform sampler2D reflection2DSampler;
  97. #endif
  98. #ifdef REFLECTIONMAP_SKYBOX
  99. varying vec3 vPositionUVW;
  100. #else
  101. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  102. varying vec3 vDirectionW;
  103. #endif
  104. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  105. uniform mat4 reflectionMatrix;
  106. #endif
  107. #endif
  108. #include<reflectionFunction>
  109. #ifdef REFLECTIONFRESNEL
  110. uniform vec4 reflectionLeftColor;
  111. uniform vec4 reflectionRightColor;
  112. #endif
  113. #endif
  114. #ifdef CAMERACOLORGRADING
  115. #include<colorGradingDefinition>
  116. #include<colorGrading>
  117. #endif
  118. #ifdef CAMERACOLORCURVES
  119. #include<colorCurvesDefinition>
  120. #include<colorCurves>
  121. #endif
  122. #include<bumpFragmentFunctions>
  123. #include<clipPlaneFragmentDeclaration>
  124. #include<logDepthDeclaration>
  125. #include<fogFragmentDeclaration>
  126. void main(void) {
  127. #include<clipPlaneFragment>
  128. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  129. // Base color
  130. vec4 baseColor = vec4(1., 1., 1., 1.);
  131. vec3 diffuseColor = vDiffuseColor.rgb;
  132. // Alpha
  133. float alpha = vDiffuseColor.a;
  134. // Bump
  135. #ifdef NORMAL
  136. vec3 normalW = normalize(vNormalW);
  137. #else
  138. vec3 normalW = vec3(1.0, 1.0, 1.0);
  139. #endif
  140. #include<bumpFragment>
  141. #ifdef TWOSIDEDLIGHTING
  142. normalW = gl_FrontFacing ? normalW : -normalW;
  143. #endif
  144. #ifdef DIFFUSE
  145. baseColor = texture2D(diffuseSampler, vDiffuseUV + uvOffset);
  146. #ifdef ALPHATEST
  147. if (baseColor.a < 0.4)
  148. discard;
  149. #endif
  150. #ifdef ALPHAFROMDIFFUSE
  151. alpha *= baseColor.a;
  152. #endif
  153. baseColor.rgb *= vDiffuseInfos.y;
  154. #endif
  155. #ifdef VERTEXCOLOR
  156. baseColor.rgb *= vColor.rgb;
  157. #endif
  158. // Ambient color
  159. vec3 baseAmbientColor = vec3(1., 1., 1.);
  160. #ifdef AMBIENT
  161. baseAmbientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
  162. #endif
  163. // Specular map
  164. #ifdef SPECULARTERM
  165. float glossiness = vSpecularColor.a;
  166. vec3 specularColor = vSpecularColor.rgb;
  167. #ifdef SPECULAR
  168. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV + uvOffset);
  169. specularColor = specularMapColor.rgb;
  170. #ifdef GLOSSINESS
  171. glossiness = glossiness * specularMapColor.a;
  172. #endif
  173. #endif
  174. #else
  175. float glossiness = 0.;
  176. #endif
  177. // Lighting
  178. vec3 diffuseBase = vec3(0., 0., 0.);
  179. lightingInfo info;
  180. #ifdef SPECULARTERM
  181. vec3 specularBase = vec3(0., 0., 0.);
  182. #endif
  183. float shadow = 1.;
  184. #ifdef LIGHTMAP
  185. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
  186. #endif
  187. #include<lightFragment>[0..maxSimultaneousLights]
  188. // Refraction
  189. vec3 refractionColor = vec3(0., 0., 0.);
  190. #ifdef REFRACTION
  191. vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
  192. #ifdef REFRACTIONMAP_3D
  193. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  194. if (dot(refractionVector, viewDirectionW) < 1.0)
  195. {
  196. refractionColor = textureCube(refractionCubeSampler, refractionVector).rgb * vRefractionInfos.x;
  197. }
  198. #else
  199. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  200. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  201. refractionCoords.y = 1.0 - refractionCoords.y;
  202. refractionColor = texture2D(refraction2DSampler, refractionCoords).rgb * vRefractionInfos.x;
  203. #endif
  204. #endif
  205. // Reflection
  206. vec3 reflectionColor = vec3(0., 0., 0.);
  207. #ifdef REFLECTION
  208. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  209. #ifdef REFLECTIONMAP_3D
  210. #ifdef ROUGHNESS
  211. float bias = vReflectionInfos.y;
  212. #ifdef SPECULARTERM
  213. #ifdef SPECULAR
  214. #ifdef GLOSSINESS
  215. bias *= (1.0 - specularMapColor.a);
  216. #endif
  217. #endif
  218. #endif
  219. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  220. #else
  221. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.x;
  222. #endif
  223. #else
  224. vec2 coords = vReflectionUVW.xy;
  225. #ifdef REFLECTIONMAP_PROJECTION
  226. coords /= vReflectionUVW.z;
  227. #endif
  228. coords.y = 1.0 - coords.y;
  229. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
  230. #endif
  231. #ifdef REFLECTIONFRESNEL
  232. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  233. #ifdef REFLECTIONFRESNELFROMSPECULAR
  234. #ifdef SPECULARTERM
  235. reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  236. #else
  237. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  238. #endif
  239. #else
  240. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  241. #endif
  242. #endif
  243. #endif
  244. #ifdef REFRACTIONFRESNEL
  245. float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
  246. refractionColor *= refractionLeftColor.rgb * (1.0 - refractionFresnelTerm) + refractionFresnelTerm * refractionRightColor.rgb;
  247. #endif
  248. #ifdef OPACITY
  249. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  250. #ifdef OPACITYRGB
  251. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  252. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  253. #else
  254. alpha *= opacityMap.a * vOpacityInfos.y;
  255. #endif
  256. #endif
  257. #ifdef VERTEXALPHA
  258. alpha *= vColor.a;
  259. #endif
  260. #ifdef OPACITYFRESNEL
  261. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  262. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  263. #endif
  264. // Emissive
  265. vec3 emissiveColor = vEmissiveColor;
  266. #ifdef EMISSIVE
  267. emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
  268. #endif
  269. #ifdef EMISSIVEFRESNEL
  270. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  271. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  272. #endif
  273. // Fresnel
  274. #ifdef DIFFUSEFRESNEL
  275. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  276. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  277. #endif
  278. // Composition
  279. #ifdef EMISSIVEASILLUMINATION
  280. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  281. #else
  282. #ifdef LINKEMISSIVEWITHDIFFUSE
  283. vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  284. #else
  285. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  286. #endif
  287. #endif
  288. #ifdef SPECULARTERM
  289. vec3 finalSpecular = specularBase * specularColor;
  290. #else
  291. vec3 finalSpecular = vec3(0.0);
  292. #endif
  293. #ifdef SPECULAROVERALPHA
  294. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  295. #endif
  296. #ifdef REFLECTIONOVERALPHA
  297. alpha = clamp(alpha + dot(reflectionColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
  298. #endif
  299. // Composition
  300. #ifdef EMISSIVEASILLUMINATION
  301. vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
  302. #else
  303. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
  304. #endif
  305. //Old lightmap calculation method
  306. #ifdef LIGHTMAP
  307. #ifndef LIGHTMAPEXCLUDED
  308. #ifdef USELIGHTMAPASSHADOWMAP
  309. color.rgb *= lightmapColor;
  310. #else
  311. color.rgb += lightmapColor;
  312. #endif
  313. #endif
  314. #endif
  315. #include<logDepthFragment>
  316. #include<fogFragment>
  317. #ifdef CAMERACOLORGRADING
  318. color = colorGrades(color);
  319. #endif
  320. #ifdef CAMERACOLORCURVES
  321. color.rgb = applyColorCurves(color.rgb);
  322. #endif
  323. gl_FragColor = color;
  324. }