default.fragment.fx 14 KB

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