default.fragment.fx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. // Fresnel
  102. #include<fresnelFunction>
  103. // Reflection
  104. #ifdef REFLECTION
  105. #ifdef REFLECTIONMAP_3D
  106. uniform samplerCube reflectionCubeSampler;
  107. #else
  108. uniform sampler2D reflection2DSampler;
  109. #endif
  110. #ifdef REFLECTIONMAP_SKYBOX
  111. varying vec3 vPositionUVW;
  112. #else
  113. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
  114. varying vec3 vDirectionW;
  115. #endif
  116. #endif
  117. #include<reflectionFunction>
  118. #endif
  119. #include<imageProcessingDeclaration>
  120. #include<imageProcessingFunctions>
  121. #include<bumpFragmentFunctions>
  122. #include<clipPlaneFragmentDeclaration>
  123. #include<logDepthDeclaration>
  124. #include<fogFragmentDeclaration>
  125. #define CUSTOM_FRAGMENT_DEFINITIONS
  126. void main(void) {
  127. #define CUSTOM_FRAGMENT_MAIN_BEGIN
  128. #include<clipPlaneFragment>
  129. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  130. // Base color
  131. vec4 baseColor = vec4(1., 1., 1., 1.);
  132. vec3 diffuseColor = vDiffuseColor.rgb;
  133. // Alpha
  134. float alpha = vDiffuseColor.a;
  135. // Bump
  136. #ifdef NORMAL
  137. vec3 normalW = normalize(vNormalW);
  138. #else
  139. vec3 normalW = normalize(-cross(dFdx(vPositionW), dFdy(vPositionW)));
  140. #endif
  141. #include<bumpFragment>
  142. #ifdef TWOSIDEDLIGHTING
  143. normalW = gl_FrontFacing ? normalW : -normalW;
  144. #endif
  145. #ifdef DIFFUSE
  146. baseColor = texture2D(diffuseSampler, vDiffuseUV + uvOffset);
  147. #ifdef ALPHATEST
  148. if (baseColor.a < 0.4)
  149. discard;
  150. #endif
  151. #ifdef ALPHAFROMDIFFUSE
  152. alpha *= baseColor.a;
  153. #endif
  154. #define CUSTOM_FRAGMENT_UPDATE_ALPHA
  155. baseColor.rgb *= vDiffuseInfos.y;
  156. #endif
  157. #include<depthPrePass>
  158. #ifdef VERTEXCOLOR
  159. baseColor.rgb *= vColor.rgb;
  160. #endif
  161. #define CUSTOM_FRAGMENT_UPDATE_DIFFUSE
  162. // Ambient color
  163. vec3 baseAmbientColor = vec3(1., 1., 1.);
  164. #ifdef AMBIENT
  165. baseAmbientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
  166. #endif
  167. #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
  168. // Specular map
  169. #ifdef SPECULARTERM
  170. float glossiness = vSpecularColor.a;
  171. vec3 specularColor = vSpecularColor.rgb;
  172. #ifdef SPECULAR
  173. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV + uvOffset);
  174. specularColor = specularMapColor.rgb;
  175. #ifdef GLOSSINESS
  176. glossiness = glossiness * specularMapColor.a;
  177. #endif
  178. #endif
  179. #else
  180. float glossiness = 0.;
  181. #endif
  182. // Lighting
  183. vec3 diffuseBase = vec3(0., 0., 0.);
  184. lightingInfo info;
  185. #ifdef SPECULARTERM
  186. vec3 specularBase = vec3(0., 0., 0.);
  187. #endif
  188. float shadow = 1.;
  189. #ifdef LIGHTMAP
  190. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
  191. #endif
  192. #include<lightFragment>[0..maxSimultaneousLights]
  193. // Refraction
  194. vec3 refractionColor = vec3(0., 0., 0.);
  195. #ifdef REFRACTION
  196. vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
  197. #ifdef REFRACTIONMAP_3D
  198. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  199. if (dot(refractionVector, viewDirectionW) < 1.0)
  200. {
  201. refractionColor = textureCube(refractionCubeSampler, refractionVector).rgb * vRefractionInfos.x;
  202. }
  203. #else
  204. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  205. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  206. refractionCoords.y = 1.0 - refractionCoords.y;
  207. refractionColor = texture2D(refraction2DSampler, refractionCoords).rgb * vRefractionInfos.x;
  208. #endif
  209. #endif
  210. // Reflection
  211. vec3 reflectionColor = vec3(0., 0., 0.);
  212. #ifdef REFLECTION
  213. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  214. #ifdef REFLECTIONMAP_3D
  215. #ifdef ROUGHNESS
  216. float bias = vReflectionInfos.y;
  217. #ifdef SPECULARTERM
  218. #ifdef SPECULAR
  219. #ifdef GLOSSINESS
  220. bias *= (1.0 - specularMapColor.a);
  221. #endif
  222. #endif
  223. #endif
  224. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  225. #else
  226. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.x;
  227. #endif
  228. #else
  229. vec2 coords = vReflectionUVW.xy;
  230. #ifdef REFLECTIONMAP_PROJECTION
  231. coords /= vReflectionUVW.z;
  232. #endif
  233. coords.y = 1.0 - coords.y;
  234. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
  235. #endif
  236. #ifdef REFLECTIONFRESNEL
  237. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  238. #ifdef REFLECTIONFRESNELFROMSPECULAR
  239. #ifdef SPECULARTERM
  240. reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  241. #else
  242. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  243. #endif
  244. #else
  245. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  246. #endif
  247. #endif
  248. #endif
  249. #ifdef REFRACTIONFRESNEL
  250. float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
  251. refractionColor *= refractionLeftColor.rgb * (1.0 - refractionFresnelTerm) + refractionFresnelTerm * refractionRightColor.rgb;
  252. #endif
  253. #ifdef OPACITY
  254. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  255. #ifdef OPACITYRGB
  256. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  257. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  258. #else
  259. alpha *= opacityMap.a * vOpacityInfos.y;
  260. #endif
  261. #endif
  262. #ifdef VERTEXALPHA
  263. alpha *= vColor.a;
  264. #endif
  265. #ifdef OPACITYFRESNEL
  266. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  267. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  268. #endif
  269. // Emissive
  270. vec3 emissiveColor = vEmissiveColor;
  271. #ifdef EMISSIVE
  272. emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
  273. #endif
  274. #ifdef EMISSIVEFRESNEL
  275. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  276. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  277. #endif
  278. // Fresnel
  279. #ifdef DIFFUSEFRESNEL
  280. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  281. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  282. #endif
  283. // Composition
  284. #ifdef EMISSIVEASILLUMINATION
  285. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  286. #else
  287. #ifdef LINKEMISSIVEWITHDIFFUSE
  288. vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  289. #else
  290. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  291. #endif
  292. #endif
  293. #ifdef SPECULARTERM
  294. vec3 finalSpecular = specularBase * specularColor;
  295. #ifdef SPECULAROVERALPHA
  296. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  297. #endif
  298. #else
  299. vec3 finalSpecular = vec3(0.0);
  300. #endif
  301. #ifdef REFLECTIONOVERALPHA
  302. alpha = clamp(alpha + dot(reflectionColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
  303. #endif
  304. // Composition
  305. #ifdef EMISSIVEASILLUMINATION
  306. vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
  307. #else
  308. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
  309. #endif
  310. //Old lightmap calculation method
  311. #ifdef LIGHTMAP
  312. #ifndef LIGHTMAPEXCLUDED
  313. #ifdef USELIGHTMAPASSHADOWMAP
  314. color.rgb *= lightmapColor;
  315. #else
  316. color.rgb += lightmapColor;
  317. #endif
  318. #endif
  319. #endif
  320. #define CUSTOM_FRAGMENT_BEFORE_FOG
  321. color.rgb = max(color.rgb, 0.);
  322. #include<logDepthFragment>
  323. #include<fogFragment>
  324. // Apply image processing if relevant. As this applies in linear space,
  325. // We first move from gamma to linear.
  326. #ifdef IMAGEPROCESSINGPOSTPROCESS
  327. color.rgb = toLinearSpace(color.rgb);
  328. #else
  329. #ifdef IMAGEPROCESSING
  330. color.rgb = toLinearSpace(color.rgb);
  331. color = applyImageProcessing(color);
  332. #endif
  333. #endif
  334. #ifdef PREMULTIPLYALPHA
  335. // Convert to associative (premultiplied) format if needed.
  336. color.rgb *= color.a;
  337. #endif
  338. #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
  339. gl_FragColor = color;
  340. }