default.fragment.fx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. depth = 0.5 * depth + vec3(0.5);
  191. vec2 uv = depth.xy;
  192. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  193. {
  194. return 1.0;
  195. }
  196. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  197. if (depth.z > shadow)
  198. {
  199. return darkness;
  200. }
  201. return 1.;
  202. }
  203. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias)
  204. {
  205. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  206. depth = 0.5 * depth + vec3(0.5);
  207. vec2 uv = depth.xy;
  208. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  209. {
  210. return 1.0;
  211. }
  212. float visibility = 1.;
  213. vec2 poissonDisk[4];
  214. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  215. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  216. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  217. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  218. // Poisson Sampling
  219. float biasedDepth = depth.z - bias;
  220. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
  221. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
  222. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
  223. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
  224. return visibility;
  225. }
  226. // Thanks to http://devmaster.net/
  227. float linstep(float low, float high, float v) {
  228. return clamp((v - low) / (high - low), 0.0, 1.0);
  229. }
  230. float ChebychevInequality(vec2 moments, float compare, float bias)
  231. {
  232. float p = smoothstep(compare - bias, compare, moments.x);
  233. float variance = max(moments.y - moments.x * moments.x, 0.02);
  234. float d = compare - moments.x;
  235. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  236. return clamp(max(p, p_max), 0.0, 1.0);
  237. }
  238. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias)
  239. {
  240. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  241. depth = 0.5 * depth + vec3(0.5);
  242. vec2 uv = depth.xy;
  243. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  244. {
  245. return 1.0;
  246. }
  247. vec4 texel = texture2D(shadowSampler, uv);
  248. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  249. return 1.0 - ChebychevInequality(moments, depth.z, bias);
  250. }
  251. #endif
  252. // Bump
  253. #ifdef BUMP
  254. #extension GL_OES_standard_derivatives : enable
  255. varying vec2 vBumpUV;
  256. uniform vec2 vBumpInfos;
  257. uniform sampler2D bumpSampler;
  258. // Thanks to http://www.thetenthplanet.de/archives/1180
  259. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  260. {
  261. // get edge vectors of the pixel triangle
  262. vec3 dp1 = dFdx(p);
  263. vec3 dp2 = dFdy(p);
  264. vec2 duv1 = dFdx(uv);
  265. vec2 duv2 = dFdy(uv);
  266. // solve the linear system
  267. vec3 dp2perp = cross(dp2, normal);
  268. vec3 dp1perp = cross(normal, dp1);
  269. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  270. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  271. // construct a scale-invariant frame
  272. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  273. return mat3(tangent * invmax, binormal * invmax, normal);
  274. }
  275. vec3 perturbNormal(vec3 viewDir)
  276. {
  277. vec3 map = texture2D(bumpSampler, vBumpUV).xyz;
  278. map = map * 255. / 127. - 128. / 127.;
  279. mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);
  280. return normalize(TBN * map);
  281. }
  282. #endif
  283. #ifdef CLIPPLANE
  284. varying float fClipDistance;
  285. #endif
  286. // Fog
  287. #ifdef FOG
  288. #define FOGMODE_NONE 0.
  289. #define FOGMODE_EXP 1.
  290. #define FOGMODE_EXP2 2.
  291. #define FOGMODE_LINEAR 3.
  292. #define E 2.71828
  293. uniform vec4 vFogInfos;
  294. uniform vec3 vFogColor;
  295. varying float fFogDistance;
  296. float CalcFogFactor()
  297. {
  298. float fogCoeff = 1.0;
  299. float fogStart = vFogInfos.y;
  300. float fogEnd = vFogInfos.z;
  301. float fogDensity = vFogInfos.w;
  302. if (FOGMODE_LINEAR == vFogInfos.x)
  303. {
  304. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  305. }
  306. else if (FOGMODE_EXP == vFogInfos.x)
  307. {
  308. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  309. }
  310. else if (FOGMODE_EXP2 == vFogInfos.x)
  311. {
  312. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  313. }
  314. return clamp(fogCoeff, 0.0, 1.0);
  315. }
  316. #endif
  317. // Light Computing
  318. struct lightingInfo
  319. {
  320. vec3 diffuse;
  321. vec3 specular;
  322. };
  323. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range) {
  324. lightingInfo result;
  325. vec3 lightVectorW;
  326. float attenuation = 1.0;
  327. if (lightData.w == 0.)
  328. {
  329. vec3 direction = lightData.xyz - vPositionW;
  330. attenuation = max(0., 1.0 - length(direction) / range);
  331. lightVectorW = normalize(direction);
  332. }
  333. else
  334. {
  335. lightVectorW = normalize(-lightData.xyz);
  336. }
  337. // diffuse
  338. float ndl = max(0., dot(vNormal, lightVectorW));
  339. // Specular
  340. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  341. float specComp = max(0., dot(vNormal, angleW));
  342. specComp = pow(specComp, max(1., vSpecularColor.a));
  343. result.diffuse = ndl * diffuseColor * attenuation;
  344. result.specular = specComp * specularColor * attenuation;
  345. return result;
  346. }
  347. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range) {
  348. lightingInfo result;
  349. vec3 direction = lightData.xyz - vPositionW;
  350. vec3 lightVectorW = normalize(direction);
  351. float attenuation = max(0., 1.0 - length(direction) / range);
  352. // diffuse
  353. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  354. float spotAtten = 0.0;
  355. if (cosAngle >= lightDirection.w)
  356. {
  357. cosAngle = max(0., pow(cosAngle, lightData.w));
  358. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  359. // Diffuse
  360. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  361. // Specular
  362. vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
  363. float specComp = max(0., dot(vNormal, angleW));
  364. specComp = pow(specComp, vSpecularColor.a);
  365. result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
  366. result.specular = specComp * specularColor * spotAtten * attenuation;
  367. return result;
  368. }
  369. result.diffuse = vec3(0.);
  370. result.specular = vec3(0.);
  371. return result;
  372. }
  373. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor) {
  374. lightingInfo result;
  375. // Diffuse
  376. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  377. // Specular
  378. vec3 angleW = normalize(viewDirectionW + lightData.xyz);
  379. float specComp = max(0., dot(vNormal, angleW));
  380. specComp = pow(specComp, vSpecularColor.a);
  381. result.diffuse = mix(groundColor, diffuseColor, ndl);
  382. result.specular = specComp * specularColor;
  383. return result;
  384. }
  385. void main(void) {
  386. // Clip plane
  387. #ifdef CLIPPLANE
  388. if (fClipDistance > 0.0)
  389. discard;
  390. #endif
  391. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  392. // Base color
  393. vec4 baseColor = vec4(1., 1., 1., 1.);
  394. vec3 diffuseColor = vDiffuseColor.rgb;
  395. // Alpha
  396. float alpha = vDiffuseColor.a;
  397. #ifdef VERTEXCOLOR
  398. baseColor.rgb *= vColor.rgb;
  399. #endif
  400. #ifdef DIFFUSE
  401. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  402. #ifdef ALPHATEST
  403. if (baseColor.a < 0.4)
  404. discard;
  405. #endif
  406. #ifdef ALPHAFROMDIFFUSE
  407. alpha *= baseColor.a;
  408. #endif
  409. baseColor.rgb *= vDiffuseInfos.y;
  410. #endif
  411. // Bump
  412. #ifdef NORMAL
  413. vec3 normalW = normalize(vNormalW);
  414. #else
  415. vec3 normalW = vec3(1.0, 1.0, 1.0);
  416. #endif
  417. #ifdef BUMP
  418. normalW = perturbNormal(viewDirectionW);
  419. #endif
  420. // Ambient color
  421. vec3 baseAmbientColor = vec3(1., 1., 1.);
  422. #ifdef AMBIENT
  423. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  424. #endif
  425. // Lighting
  426. vec3 diffuseBase = vec3(0., 0., 0.);
  427. vec3 specularBase = vec3(0., 0., 0.);
  428. float shadow = 1.;
  429. #ifdef LIGHT0
  430. #ifdef SPOTLIGHT0
  431. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a);
  432. #endif
  433. #ifdef HEMILIGHT0
  434. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0);
  435. #endif
  436. #ifdef POINTDIRLIGHT0
  437. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a);
  438. #endif
  439. #ifdef SHADOW0
  440. #ifdef SHADOWVSM0
  441. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z);
  442. #else
  443. #ifdef SHADOWPCF0
  444. shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z);
  445. #else
  446. shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  447. #endif
  448. #endif
  449. #else
  450. shadow = 1.;
  451. #endif
  452. diffuseBase += info.diffuse * shadow;
  453. specularBase += info.specular * shadow;
  454. #endif
  455. #ifdef LIGHT1
  456. #ifdef SPOTLIGHT1
  457. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a);
  458. #endif
  459. #ifdef HEMILIGHT1
  460. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1);
  461. #endif
  462. #ifdef POINTDIRLIGHT1
  463. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a);
  464. #endif
  465. #ifdef SHADOW1
  466. #ifdef SHADOWVSM1
  467. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z);
  468. #else
  469. #ifdef SHADOWPCF1
  470. shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z);
  471. #else
  472. shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  473. #endif
  474. #endif
  475. #else
  476. shadow = 1.;
  477. #endif
  478. diffuseBase += info.diffuse * shadow;
  479. specularBase += info.specular * shadow;
  480. #endif
  481. #ifdef LIGHT2
  482. #ifdef SPOTLIGHT2
  483. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a);
  484. #endif
  485. #ifdef HEMILIGHT2
  486. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2);
  487. #endif
  488. #ifdef POINTDIRLIGHT2
  489. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a);
  490. #endif
  491. #ifdef SHADOW2
  492. #ifdef SHADOWVSM2
  493. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z);
  494. #else
  495. #ifdef SHADOWPCF2
  496. shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z);
  497. #else
  498. shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  499. #endif
  500. #endif
  501. #else
  502. shadow = 1.;
  503. #endif
  504. diffuseBase += info.diffuse * shadow;
  505. specularBase += info.specular * shadow;
  506. #endif
  507. #ifdef LIGHT3
  508. #ifdef SPOTLIGHT3
  509. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a);
  510. #endif
  511. #ifdef HEMILIGHT3
  512. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3);
  513. #endif
  514. #ifdef POINTDIRLIGHT3
  515. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a);
  516. #endif
  517. #ifdef SHADOW3
  518. #ifdef SHADOWVSM3
  519. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z);
  520. #else
  521. #ifdef SHADOWPCF3
  522. shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z);
  523. #else
  524. shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  525. #endif
  526. #endif
  527. #else
  528. shadow = 1.;
  529. #endif
  530. diffuseBase += info.diffuse * shadow;
  531. specularBase += info.specular * shadow;
  532. #endif
  533. // Reflection
  534. vec3 reflectionColor = vec3(0., 0., 0.);
  535. #ifdef REFLECTION
  536. vec3 vReflectionUVW = computeReflectionCoords(vReflectionInfos.x, vec4(vPositionW, 1.0), normalW);
  537. if (vReflectionInfos.z != 0.0)
  538. {
  539. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.y * shadow;
  540. }
  541. else
  542. {
  543. vec2 coords = vReflectionUVW.xy;
  544. if (vReflectionInfos.x == MAP_PROJECTION)
  545. {
  546. coords /= vReflectionUVW.z;
  547. }
  548. coords.y = 1.0 - coords.y;
  549. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.y * shadow;
  550. }
  551. #ifdef REFLECTIONFRESNEL
  552. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  553. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  554. #endif
  555. #endif
  556. #ifdef OPACITY
  557. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  558. #ifdef OPACITYRGB
  559. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  560. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  561. #else
  562. alpha *= opacityMap.a * vOpacityInfos.y;
  563. #endif
  564. #endif
  565. #ifdef VERTEXALPHA
  566. alpha *= vColor.a;
  567. #endif
  568. #ifdef OPACITYFRESNEL
  569. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  570. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  571. #endif
  572. // Emissive
  573. vec3 emissiveColor = vEmissiveColor;
  574. #ifdef EMISSIVE
  575. emissiveColor += texture2D(emissiveSampler, vEmissiveUV).rgb * vEmissiveInfos.y;
  576. #endif
  577. #ifdef EMISSIVEFRESNEL
  578. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  579. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  580. #endif
  581. // Specular map
  582. vec3 specularColor = vSpecularColor.rgb;
  583. #ifdef SPECULAR
  584. specularColor = texture2D(specularSampler, vSpecularUV).rgb * vSpecularInfos.y;
  585. #endif
  586. // Fresnel
  587. #ifdef DIFFUSEFRESNEL
  588. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  589. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  590. #endif
  591. // Composition
  592. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  593. vec3 finalSpecular = specularBase * specularColor;
  594. #ifdef SPECULAROVERALPHA
  595. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  596. #endif
  597. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
  598. #ifdef FOG
  599. float fog = CalcFogFactor();
  600. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  601. #endif
  602. gl_FragColor = color;
  603. }