default.fragment.fx 19 KB

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