default.fragment.fx 10 KB

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