default.fragment.fx 11 KB

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