default.fragment.fx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
  4. #define MAP_PROJECTION 4.
  5. // Constants
  6. uniform vec3 vEyePosition;
  7. uniform vec3 vAmbientColor;
  8. uniform vec4 vDiffuseColor;
  9. uniform vec4 vSpecularColor;
  10. uniform vec3 vEmissiveColor;
  11. // Input
  12. varying vec3 vPositionW;
  13. varying vec3 vNormalW;
  14. #ifdef VERTEXCOLOR
  15. varying vec3 vColor;
  16. #endif
  17. // Lights
  18. #ifdef LIGHT0
  19. uniform vec4 vLightData0;
  20. uniform vec3 vLightDiffuse0;
  21. uniform vec3 vLightSpecular0;
  22. #ifdef SHADOW0
  23. varying vec4 vPositionFromLight0;
  24. uniform sampler2D shadowSampler0;
  25. #endif
  26. #ifdef SPOTLIGHT0
  27. uniform vec4 vLightDirection0;
  28. #endif
  29. #ifdef HEMILIGHT0
  30. uniform vec3 vLightGround0;
  31. #endif
  32. #endif
  33. #ifdef LIGHT1
  34. uniform vec4 vLightData1;
  35. uniform vec3 vLightDiffuse1;
  36. uniform vec3 vLightSpecular1;
  37. #ifdef SHADOW1
  38. varying vec4 vPositionFromLight1;
  39. uniform sampler2D shadowSampler1;
  40. #endif
  41. #ifdef SPOTLIGHT1
  42. uniform vec4 vLightDirection1;
  43. #endif
  44. #ifdef HEMILIGHT1
  45. uniform vec3 vLightGround1;
  46. #endif
  47. #endif
  48. #ifdef LIGHT2
  49. uniform vec4 vLightData2;
  50. uniform vec3 vLightDiffuse2;
  51. uniform vec3 vLightSpecular2;
  52. #ifdef SHADOW2
  53. varying vec4 vPositionFromLight2;
  54. uniform sampler2D shadowSampler2;
  55. #endif
  56. #ifdef SPOTLIGHT2
  57. uniform vec4 vLightDirection2;
  58. #endif
  59. #ifdef HEMILIGHT2
  60. uniform vec3 vLightGround2;
  61. #endif
  62. #endif
  63. #ifdef LIGHT3
  64. uniform vec4 vLightData3;
  65. uniform vec3 vLightDiffuse3;
  66. uniform vec3 vLightSpecular3;
  67. #ifdef SHADOW3
  68. varying vec4 vPositionFromLight3;
  69. uniform sampler2D shadowSampler3;
  70. #endif
  71. #ifdef SPOTLIGHT3
  72. uniform vec4 vLightDirection3;
  73. #endif
  74. #ifdef HEMILIGHT3
  75. uniform vec3 vLightGround3;
  76. #endif
  77. #endif
  78. // Samplers
  79. #ifdef DIFFUSE
  80. varying vec2 vDiffuseUV;
  81. uniform sampler2D diffuseSampler;
  82. uniform vec2 vDiffuseInfos;
  83. #endif
  84. #ifdef AMBIENT
  85. varying vec2 vAmbientUV;
  86. uniform sampler2D ambientSampler;
  87. uniform vec2 vAmbientInfos;
  88. #endif
  89. #ifdef OPACITY
  90. varying vec2 vOpacityUV;
  91. uniform sampler2D opacitySampler;
  92. uniform vec2 vOpacityInfos;
  93. #endif
  94. #ifdef REFLECTION
  95. varying vec3 vReflectionUVW;
  96. uniform samplerCube reflectionCubeSampler;
  97. uniform sampler2D reflection2DSampler;
  98. uniform vec3 vReflectionInfos;
  99. #endif
  100. #ifdef EMISSIVE
  101. varying vec2 vEmissiveUV;
  102. uniform vec2 vEmissiveInfos;
  103. uniform sampler2D emissiveSampler;
  104. #endif
  105. #ifdef SPECULAR
  106. varying vec2 vSpecularUV;
  107. uniform vec2 vSpecularInfos;
  108. uniform sampler2D specularSampler;
  109. #endif
  110. // Shadows
  111. #ifdef SHADOWS
  112. float unpack(vec4 color)
  113. {
  114. const vec4 bitShift = vec4(1. / (255. * 255. * 255.), 1. / (255. * 255.), 1. / 255., 1.);
  115. return dot(color, bitShift);
  116. }
  117. float unpackHalf(vec2 color)
  118. {
  119. return color.x + (color.y / 255.0);
  120. }
  121. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler)
  122. {
  123. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  124. vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
  125. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  126. {
  127. return 1.0;
  128. }
  129. float shadow = unpack(texture2D(shadowSampler, uv));
  130. if (depth.z > shadow)
  131. {
  132. return 0.;
  133. }
  134. return 1.;
  135. }
  136. // Thanks to http://devmaster.net/
  137. float ChebychevInequality(vec2 moments, float t)
  138. {
  139. if (t <= moments.x)
  140. {
  141. return 1.0;
  142. }
  143. float variance = moments.y - (moments.x * moments.x);
  144. variance = max(variance, 0.);
  145. float d = t - moments.x;
  146. return variance / (variance + d * d);
  147. }
  148. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler)
  149. {
  150. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  151. vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
  152. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  153. {
  154. return 1.0;
  155. }
  156. vec4 texel = texture2D(shadowSampler, uv);
  157. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  158. return clamp(1.3 - ChebychevInequality(moments, depth.z), 0., 1.0);
  159. }
  160. #endif
  161. // Bump
  162. #ifdef BUMP
  163. #extension GL_OES_standard_derivatives : enable
  164. varying vec2 vBumpUV;
  165. uniform vec2 vBumpInfos;
  166. uniform sampler2D bumpSampler;
  167. // Thanks to http://www.thetenthplanet.de/archives/1180
  168. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  169. {
  170. // get edge vectors of the pixel triangle
  171. vec3 dp1 = dFdx(p);
  172. vec3 dp2 = dFdy(p);
  173. vec2 duv1 = dFdx(uv);
  174. vec2 duv2 = dFdy(uv);
  175. // solve the linear system
  176. vec3 dp2perp = cross(dp2, normal);
  177. vec3 dp1perp = cross(normal, dp1);
  178. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  179. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  180. // construct a scale-invariant frame
  181. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  182. return mat3(tangent * invmax, binormal * invmax, normal);
  183. }
  184. vec3 perturbNormal(vec3 viewDir)
  185. {
  186. vec3 map = texture2D(bumpSampler, vBumpUV).xyz * vBumpInfos.y;
  187. map = map * 255. / 127. - 128. / 127.;
  188. mat3 TBN = cotangent_frame(vNormalW, -viewDir, vBumpUV);
  189. return normalize(TBN * map);
  190. }
  191. #endif
  192. #ifdef CLIPPLANE
  193. varying float fClipDistance;
  194. #endif
  195. // Fog
  196. #ifdef FOG
  197. #define FOGMODE_NONE 0.
  198. #define FOGMODE_EXP 1.
  199. #define FOGMODE_EXP2 2.
  200. #define FOGMODE_LINEAR 3.
  201. #define E 2.71828
  202. uniform vec4 vFogInfos;
  203. uniform vec3 vFogColor;
  204. varying float fFogDistance;
  205. float CalcFogFactor()
  206. {
  207. float fogCoeff = 1.0;
  208. float fogStart = vFogInfos.y;
  209. float fogEnd = vFogInfos.z;
  210. float fogDensity = vFogInfos.w;
  211. if (FOGMODE_LINEAR == vFogInfos.x)
  212. {
  213. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  214. }
  215. else if (FOGMODE_EXP == vFogInfos.x)
  216. {
  217. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  218. }
  219. else if (FOGMODE_EXP2 == vFogInfos.x)
  220. {
  221. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  222. }
  223. return clamp(fogCoeff, 0.0, 1.0);
  224. }
  225. #endif
  226. // Light Computing
  227. struct lightingInfo
  228. {
  229. vec3 diffuse;
  230. vec3 specular;
  231. };
  232. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor) {
  233. lightingInfo result;
  234. vec3 lightVectorW;
  235. if (lightData.w == 0.)
  236. {
  237. lightVectorW = normalize(lightData.xyz - vPositionW);
  238. }
  239. else
  240. {
  241. lightVectorW = normalize(-lightData.xyz);
  242. }
  243. // diffuse
  244. float ndl = max(0., dot(vNormal, lightVectorW));
  245. // Specular
  246. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  247. float specComp = max(0., dot(vNormal, angleW));
  248. specComp = pow(specComp, vSpecularColor.a);
  249. result.diffuse = ndl * diffuseColor;
  250. result.specular = specComp * specularColor;
  251. return result;
  252. }
  253. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor) {
  254. lightingInfo result;
  255. vec3 lightVectorW = normalize(lightData.xyz - vPositionW);
  256. // diffuse
  257. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  258. float spotAtten = 0.0;
  259. if (cosAngle >= lightDirection.w)
  260. {
  261. cosAngle = max(0., pow(cosAngle, lightData.w));
  262. spotAtten = max(0., (cosAngle - lightDirection.w) / (1. - cosAngle));
  263. // Diffuse
  264. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  265. // Specular
  266. vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
  267. float specComp = max(0., dot(vNormal, angleW));
  268. specComp = pow(specComp, vSpecularColor.a);
  269. result.diffuse = ndl * spotAtten * diffuseColor;
  270. result.specular = specComp * specularColor * spotAtten;
  271. return result;
  272. }
  273. result.diffuse = vec3(0.);
  274. result.specular = vec3(0.);
  275. return result;
  276. }
  277. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor) {
  278. lightingInfo result;
  279. // Diffuse
  280. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  281. // Specular
  282. vec3 angleW = normalize(viewDirectionW + lightData.xyz);
  283. float specComp = max(0., dot(vNormal, angleW));
  284. specComp = pow(specComp, vSpecularColor.a);
  285. result.diffuse = mix(groundColor, diffuseColor, ndl);
  286. result.specular = specComp * specularColor;
  287. return result;
  288. }
  289. void main(void) {
  290. // Clip plane
  291. #ifdef CLIPPLANE
  292. if (fClipDistance > 0.0)
  293. discard;
  294. #endif
  295. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  296. // Base color
  297. vec4 baseColor = vec4(1., 1., 1., 1.);
  298. vec3 diffuseColor = vDiffuseColor.rgb;
  299. #ifdef VERTEXCOLOR
  300. diffuseColor *= vColor;
  301. #endif
  302. #ifdef DIFFUSE
  303. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  304. #ifdef ALPHATEST
  305. if (baseColor.a < 0.4)
  306. discard;
  307. #endif
  308. baseColor.rgb *= vDiffuseInfos.y;
  309. #endif
  310. // Bump
  311. vec3 normalW = vNormalW;
  312. #ifdef BUMP
  313. normalW = perturbNormal(viewDirectionW);
  314. #endif
  315. // Ambient color
  316. vec3 baseAmbientColor = vec3(1., 1., 1.);
  317. #ifdef AMBIENT
  318. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  319. #endif
  320. // Lighting
  321. vec3 diffuseBase = vec3(0., 0., 0.);
  322. vec3 specularBase = vec3(0., 0., 0.);
  323. float shadow = 1.;
  324. #ifdef LIGHT0
  325. #ifdef SPOTLIGHT0
  326. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0, vLightSpecular0);
  327. #endif
  328. #ifdef HEMILIGHT0
  329. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0, vLightGround0);
  330. #endif
  331. #ifdef POINTDIRLIGHT0
  332. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0);
  333. #endif
  334. #ifdef SHADOW0
  335. #ifdef SHADOWVSM0
  336. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0);
  337. #else
  338. shadow = computeShadow(vPositionFromLight0, shadowSampler0);
  339. #endif
  340. #else
  341. shadow = 1.;
  342. #endif
  343. diffuseBase += info.diffuse * shadow;
  344. specularBase += info.specular * shadow;
  345. #endif
  346. #ifdef LIGHT1
  347. #ifdef SPOTLIGHT1
  348. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1, vLightSpecular1);
  349. #endif
  350. #ifdef HEMILIGHT1
  351. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1, vLightGround1);
  352. #endif
  353. #ifdef POINTDIRLIGHT1
  354. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1);
  355. #endif
  356. #ifdef SHADOW1
  357. #ifdef SHADOWVSM1
  358. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1);
  359. #else
  360. shadow = computeShadow(vPositionFromLight1, shadowSampler1);
  361. #endif
  362. #else
  363. shadow = 1.;
  364. #endif
  365. diffuseBase += info.diffuse * shadow;
  366. specularBase += info.specular * shadow;
  367. #endif
  368. #ifdef LIGHT2
  369. #ifdef SPOTLIGHT2
  370. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2, vLightSpecular2);
  371. #endif
  372. #ifdef HEMILIGHT2
  373. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2, vLightGround2);
  374. #endif
  375. #ifdef POINTDIRLIGHT2
  376. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2);
  377. #endif
  378. #ifdef SHADOW2
  379. #ifdef SHADOWVSM2
  380. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2);
  381. #else
  382. shadow = computeShadow(vPositionFromLight2, shadowSampler2);
  383. #endif
  384. #else
  385. shadow = 1.;
  386. #endif
  387. diffuseBase += info.diffuse * shadow;
  388. specularBase += info.specular * shadow;
  389. #endif
  390. #ifdef LIGHT3
  391. #ifdef SPOTLIGHT3
  392. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3, vLightSpecular3);
  393. #endif
  394. #ifdef HEMILIGHT3
  395. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3, vLightGround3);
  396. #endif
  397. #ifdef POINTDIRLIGHT3
  398. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3);
  399. #endif
  400. #ifdef SHADOW3
  401. #ifdef SHADOWVSM3
  402. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3);
  403. #else
  404. shadow = computeShadow(vPositionFromLight3, shadowSampler3);
  405. #endif
  406. #else
  407. shadow = 1.;
  408. #endif
  409. diffuseBase += info.diffuse * shadow;
  410. specularBase += info.specular * shadow;
  411. #endif
  412. // Reflection
  413. vec3 reflectionColor = vec3(0., 0., 0.);
  414. #ifdef REFLECTION
  415. if (vReflectionInfos.z != 0.0)
  416. {
  417. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.y;
  418. }
  419. else
  420. {
  421. vec2 coords = vReflectionUVW.xy;
  422. if (vReflectionInfos.x == MAP_PROJECTION)
  423. {
  424. coords /= vReflectionUVW.z;
  425. }
  426. coords.y = 1.0 - coords.y;
  427. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.y;
  428. }
  429. #endif
  430. // Alpha
  431. float alpha = vDiffuseColor.a;
  432. #ifdef OPACITY
  433. vec3 opacityMap = texture2D(opacitySampler, vOpacityUV).rgb * vec3(0.3, 0.59, 0.11);
  434. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  435. #endif
  436. // Emissive
  437. vec3 emissiveColor = vEmissiveColor;
  438. #ifdef EMISSIVE
  439. emissiveColor += texture2D(emissiveSampler, vEmissiveUV).rgb * vEmissiveInfos.y;
  440. #endif
  441. // Specular map
  442. vec3 specularColor = vSpecularColor.rgb;
  443. #ifdef SPECULAR
  444. specularColor = texture2D(specularSampler, vSpecularUV).rgb * vSpecularInfos.y;
  445. #endif
  446. // Composition
  447. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  448. vec3 finalSpecular = specularBase * specularColor;
  449. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
  450. #ifdef FOG
  451. float fog = CalcFogFactor();
  452. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  453. #endif
  454. gl_FragColor = color;
  455. }