default.fragment.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. // Lights
  26. #include<light0FragmentDeclaration>
  27. #include<light1FragmentDeclaration>
  28. #include<light2FragmentDeclaration>
  29. #include<light3FragmentDeclaration>
  30. #include<lightsFragmentFunctions>
  31. #include<shadowsFragmentFunctions>
  32. // Samplers
  33. #ifdef DIFFUSE
  34. varying vec2 vDiffuseUV;
  35. uniform sampler2D diffuseSampler;
  36. uniform vec2 vDiffuseInfos;
  37. #endif
  38. #ifdef AMBIENT
  39. varying vec2 vAmbientUV;
  40. uniform sampler2D ambientSampler;
  41. uniform vec2 vAmbientInfos;
  42. #endif
  43. #ifdef OPACITY
  44. varying vec2 vOpacityUV;
  45. uniform sampler2D opacitySampler;
  46. uniform vec2 vOpacityInfos;
  47. #endif
  48. #ifdef EMISSIVE
  49. varying vec2 vEmissiveUV;
  50. uniform vec2 vEmissiveInfos;
  51. uniform sampler2D emissiveSampler;
  52. #endif
  53. #ifdef LIGHTMAP
  54. varying vec2 vLightmapUV;
  55. uniform vec2 vLightmapInfos;
  56. uniform sampler2D lightmapSampler;
  57. #endif
  58. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  59. uniform mat4 view;
  60. #endif
  61. #ifdef REFRACTION
  62. uniform vec4 vRefractionInfos;
  63. #ifdef REFRACTIONMAP_3D
  64. uniform samplerCube refractionCubeSampler;
  65. #else
  66. uniform sampler2D refraction2DSampler;
  67. uniform mat4 refractionMatrix;
  68. #endif
  69. #ifdef REFRACTIONFRESNEL
  70. uniform vec4 refractionLeftColor;
  71. uniform vec4 refractionRightColor;
  72. #endif
  73. #endif
  74. #if defined(SPECULAR) && defined(SPECULARTERM)
  75. varying vec2 vSpecularUV;
  76. uniform vec2 vSpecularInfos;
  77. uniform sampler2D specularSampler;
  78. #endif
  79. // Fresnel
  80. #ifdef FRESNEL
  81. float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
  82. {
  83. float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
  84. return clamp(fresnelTerm, 0., 1.);
  85. }
  86. #endif
  87. #ifdef DIFFUSEFRESNEL
  88. uniform vec4 diffuseLeftColor;
  89. uniform vec4 diffuseRightColor;
  90. #endif
  91. #ifdef OPACITYFRESNEL
  92. uniform vec4 opacityParts;
  93. #endif
  94. #ifdef EMISSIVEFRESNEL
  95. uniform vec4 emissiveLeftColor;
  96. uniform vec4 emissiveRightColor;
  97. #endif
  98. // Reflection
  99. #ifdef REFLECTION
  100. uniform vec2 vReflectionInfos;
  101. #ifdef REFLECTIONMAP_3D
  102. uniform samplerCube reflectionCubeSampler;
  103. #else
  104. uniform sampler2D reflection2DSampler;
  105. #endif
  106. #ifdef REFLECTIONMAP_SKYBOX
  107. varying vec3 vPositionUVW;
  108. #else
  109. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  110. varying vec3 vDirectionW;
  111. #endif
  112. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  113. uniform mat4 reflectionMatrix;
  114. #endif
  115. #endif
  116. vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
  117. {
  118. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  119. vec3 direction = normalize(vDirectionW);
  120. float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);
  121. float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;
  122. return vec3(s, t, 0);
  123. #endif
  124. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  125. vec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition);
  126. vec3 r = reflect(cameraToVertex, worldNormal);
  127. float t = clamp(r.y * -0.5 + 0.5, 0., 1.0);
  128. float s = atan(r.z, r.x) * RECIPROCAL_PI2 + 0.5;
  129. return vec3(s, t, 0);
  130. #endif
  131. #ifdef REFLECTIONMAP_SPHERICAL
  132. vec3 viewDir = normalize(vec3(view * worldPos));
  133. vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
  134. vec3 r = reflect(viewDir, viewNormal);
  135. r.z = r.z - 1.0;
  136. float m = 2.0 * length(r);
  137. return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
  138. #endif
  139. #ifdef REFLECTIONMAP_PLANAR
  140. vec3 viewDir = worldPos.xyz - vEyePosition;
  141. vec3 coords = normalize(reflect(viewDir, worldNormal));
  142. return vec3(reflectionMatrix * vec4(coords, 1));
  143. #endif
  144. #ifdef REFLECTIONMAP_CUBIC
  145. vec3 viewDir = worldPos.xyz - vEyePosition;
  146. vec3 coords = reflect(viewDir, worldNormal);
  147. #ifdef INVERTCUBICMAP
  148. coords.y = 1.0 - coords.y;
  149. #endif
  150. return vec3(reflectionMatrix * vec4(coords, 0));
  151. #endif
  152. #ifdef REFLECTIONMAP_PROJECTION
  153. return vec3(reflectionMatrix * (view * worldPos));
  154. #endif
  155. #ifdef REFLECTIONMAP_SKYBOX
  156. return vPositionUVW;
  157. #endif
  158. #ifdef REFLECTIONMAP_EXPLICIT
  159. return vec3(0, 0, 0);
  160. #endif
  161. }
  162. #ifdef REFLECTIONFRESNEL
  163. uniform vec4 reflectionLeftColor;
  164. uniform vec4 reflectionRightColor;
  165. #endif
  166. #endif
  167. // Bump
  168. #ifdef BUMP
  169. varying vec2 vBumpUV;
  170. uniform vec2 vBumpInfos;
  171. uniform sampler2D bumpSampler;
  172. // Thanks to http://www.thetenthplanet.de/archives/1180
  173. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  174. {
  175. // get edge vectors of the pixel triangle
  176. vec3 dp1 = dFdx(p);
  177. vec3 dp2 = dFdy(p);
  178. vec2 duv1 = dFdx(uv);
  179. vec2 duv2 = dFdy(uv);
  180. // solve the linear system
  181. vec3 dp2perp = cross(dp2, normal);
  182. vec3 dp1perp = cross(normal, dp1);
  183. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  184. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  185. // construct a scale-invariant frame
  186. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  187. return mat3(tangent * invmax, binormal * invmax, normal);
  188. }
  189. vec3 perturbNormal(vec3 viewDir)
  190. {
  191. vec3 map = texture2D(bumpSampler, vBumpUV).xyz;
  192. map = map * 255. / 127. - 128. / 127.;
  193. mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);
  194. return normalize(TBN * map);
  195. }
  196. #endif
  197. #ifdef CLIPPLANE
  198. varying float fClipDistance;
  199. #endif
  200. #ifdef LOGARITHMICDEPTH
  201. uniform float logarithmicDepthConstant;
  202. varying float vFragmentDepth;
  203. #endif
  204. #include<fogFragmentDeclaration>
  205. void main(void) {
  206. // Clip plane
  207. #ifdef CLIPPLANE
  208. if (fClipDistance > 0.0)
  209. discard;
  210. #endif
  211. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  212. // Base color
  213. vec4 baseColor = vec4(1., 1., 1., 1.);
  214. vec3 diffuseColor = vDiffuseColor.rgb;
  215. // Alpha
  216. float alpha = vDiffuseColor.a;
  217. #ifdef DIFFUSE
  218. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  219. #ifdef ALPHATEST
  220. if (baseColor.a < 0.4)
  221. discard;
  222. #endif
  223. #ifdef ALPHAFROMDIFFUSE
  224. alpha *= baseColor.a;
  225. #endif
  226. baseColor.rgb *= vDiffuseInfos.y;
  227. #endif
  228. #ifdef VERTEXCOLOR
  229. baseColor.rgb *= vColor.rgb;
  230. #endif
  231. // Bump
  232. #ifdef NORMAL
  233. vec3 normalW = normalize(vNormalW);
  234. #else
  235. vec3 normalW = vec3(1.0, 1.0, 1.0);
  236. #endif
  237. #ifdef BUMP
  238. normalW = perturbNormal(viewDirectionW);
  239. #endif
  240. // Ambient color
  241. vec3 baseAmbientColor = vec3(1., 1., 1.);
  242. #ifdef AMBIENT
  243. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  244. #endif
  245. // Specular map
  246. #ifdef SPECULARTERM
  247. float glossiness = vSpecularColor.a;
  248. vec3 specularColor = vSpecularColor.rgb;
  249. #ifdef SPECULAR
  250. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);
  251. specularColor = specularMapColor.rgb;
  252. #ifdef GLOSSINESS
  253. glossiness = glossiness * specularMapColor.a;
  254. #endif
  255. #endif
  256. #else
  257. float glossiness = 0.;
  258. #endif
  259. // Lighting
  260. vec3 diffuseBase = vec3(0., 0., 0.);
  261. #ifdef SPECULARTERM
  262. vec3 specularBase = vec3(0., 0., 0.);
  263. #endif
  264. float shadow = 1.;
  265. #include<light0Fragment>
  266. #include<light1Fragment>
  267. #include<light2Fragment>
  268. #include<light3Fragment>
  269. // Refraction
  270. vec3 refractionColor = vec3(0., 0., 0.);
  271. #ifdef REFRACTION
  272. vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
  273. #ifdef REFRACTIONMAP_3D
  274. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  275. if (dot(refractionVector, viewDirectionW) < 1.0)
  276. {
  277. refractionColor = textureCube(refractionCubeSampler, refractionVector).rgb * vRefractionInfos.x;
  278. }
  279. #else
  280. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  281. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  282. refractionCoords.y = 1.0 - refractionCoords.y;
  283. refractionColor = texture2D(refraction2DSampler, refractionCoords).rgb * vRefractionInfos.x;
  284. #endif
  285. #endif
  286. // Reflection
  287. vec3 reflectionColor = vec3(0., 0., 0.);
  288. #ifdef REFLECTION
  289. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  290. #ifdef REFLECTIONMAP_3D
  291. #ifdef ROUGHNESS
  292. float bias = vReflectionInfos.y;
  293. #ifdef SPECULARTERM
  294. #ifdef SPECULAR
  295. #ifdef GLOSSINESS
  296. bias *= (1.0 - specularMapColor.a);
  297. #endif
  298. #endif
  299. #endif
  300. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  301. #else
  302. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.x;
  303. #endif
  304. #else
  305. vec2 coords = vReflectionUVW.xy;
  306. #ifdef REFLECTIONMAP_PROJECTION
  307. coords /= vReflectionUVW.z;
  308. #endif
  309. coords.y = 1.0 - coords.y;
  310. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
  311. #endif
  312. #ifdef REFLECTIONFRESNEL
  313. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  314. #ifdef REFLECTIONFRESNELFROMSPECULAR
  315. #ifdef SPECULARTERM
  316. reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  317. #else
  318. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  319. #endif
  320. #else
  321. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  322. #endif
  323. #endif
  324. #endif
  325. #ifdef REFRACTIONFRESNEL
  326. float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
  327. refractionColor *= refractionLeftColor.rgb * (1.0 - refractionFresnelTerm) + refractionFresnelTerm * refractionRightColor.rgb;
  328. #endif
  329. #ifdef OPACITY
  330. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  331. #ifdef OPACITYRGB
  332. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  333. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  334. #else
  335. alpha *= opacityMap.a * vOpacityInfos.y;
  336. #endif
  337. #endif
  338. #ifdef VERTEXALPHA
  339. alpha *= vColor.a;
  340. #endif
  341. #ifdef OPACITYFRESNEL
  342. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  343. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  344. #endif
  345. // Emissive
  346. vec3 emissiveColor = vEmissiveColor;
  347. #ifdef EMISSIVE
  348. emissiveColor += texture2D(emissiveSampler, vEmissiveUV).rgb * vEmissiveInfos.y;
  349. #endif
  350. #ifdef EMISSIVEFRESNEL
  351. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  352. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  353. #endif
  354. // Fresnel
  355. #ifdef DIFFUSEFRESNEL
  356. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  357. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  358. #endif
  359. // Composition
  360. #ifdef EMISSIVEASILLUMINATION
  361. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  362. #else
  363. #ifdef LINKEMISSIVEWITHDIFFUSE
  364. vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  365. #else
  366. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  367. #endif
  368. #endif
  369. #ifdef SPECULARTERM
  370. vec3 finalSpecular = specularBase * specularColor;
  371. #else
  372. vec3 finalSpecular = vec3(0.0);
  373. #endif
  374. #ifdef SPECULAROVERALPHA
  375. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  376. #endif
  377. // Composition
  378. #ifdef EMISSIVEASILLUMINATION
  379. vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
  380. #else
  381. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
  382. #endif
  383. #ifdef LIGHTMAP
  384. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  385. #ifdef USELIGHTMAPASSHADOWMAP
  386. color.rgb *= lightmapColor;
  387. #else
  388. color.rgb += lightmapColor;
  389. #endif
  390. #endif
  391. #ifdef LOGARITHMICDEPTH
  392. gl_FragDepthEXT = log2(vFragmentDepth) * logarithmicDepthConstant * 0.5;
  393. #endif
  394. #include<fogFragment>
  395. gl_FragColor = color;
  396. }