default.fragment.fx 9.7 KB

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