default.fragment.fx 9.1 KB

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