lava.fragment.fx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. precision highp float;
  2. // Constants
  3. uniform vec3 vEyePosition;
  4. uniform vec4 vDiffuseColor;
  5. // Input
  6. varying vec3 vPositionW;
  7. // MAGMAAAA
  8. uniform float time;
  9. uniform float speed;
  10. uniform float movingSpeed;
  11. uniform vec3 fogColor;
  12. uniform sampler2D noiseTexture;
  13. uniform float fogDensity;
  14. // Varying
  15. varying float noise;
  16. #ifdef NORMAL
  17. varying vec3 vNormalW;
  18. #endif
  19. #ifdef VERTEXCOLOR
  20. varying vec4 vColor;
  21. #endif
  22. // Lights
  23. #ifdef LIGHT0
  24. uniform vec4 vLightData0;
  25. uniform vec4 vLightDiffuse0;
  26. #ifdef SHADOW0
  27. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  28. varying vec4 vPositionFromLight0;
  29. uniform sampler2D shadowSampler0;
  30. #else
  31. uniform samplerCube shadowSampler0;
  32. #endif
  33. uniform vec3 shadowsInfo0;
  34. #endif
  35. #ifdef SPOTLIGHT0
  36. uniform vec4 vLightDirection0;
  37. #endif
  38. #ifdef HEMILIGHT0
  39. uniform vec3 vLightGround0;
  40. #endif
  41. #endif
  42. #ifdef LIGHT1
  43. uniform vec4 vLightData1;
  44. uniform vec4 vLightDiffuse1;
  45. #ifdef SHADOW1
  46. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  47. varying vec4 vPositionFromLight1;
  48. uniform sampler2D shadowSampler1;
  49. #else
  50. uniform samplerCube shadowSampler1;
  51. #endif
  52. uniform vec3 shadowsInfo1;
  53. #endif
  54. #ifdef SPOTLIGHT1
  55. uniform vec4 vLightDirection1;
  56. #endif
  57. #ifdef HEMILIGHT1
  58. uniform vec3 vLightGround1;
  59. #endif
  60. #endif
  61. #ifdef LIGHT2
  62. uniform vec4 vLightData2;
  63. uniform vec4 vLightDiffuse2;
  64. #ifdef SHADOW2
  65. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  66. varying vec4 vPositionFromLight2;
  67. uniform sampler2D shadowSampler2;
  68. #else
  69. uniform samplerCube shadowSampler2;
  70. #endif
  71. uniform vec3 shadowsInfo2;
  72. #endif
  73. #ifdef SPOTLIGHT2
  74. uniform vec4 vLightDirection2;
  75. #endif
  76. #ifdef HEMILIGHT2
  77. uniform vec3 vLightGround2;
  78. #endif
  79. #endif
  80. #ifdef LIGHT3
  81. uniform vec4 vLightData3;
  82. uniform vec4 vLightDiffuse3;
  83. #ifdef SHADOW3
  84. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  85. varying vec4 vPositionFromLight3;
  86. uniform sampler2D shadowSampler3;
  87. #else
  88. uniform samplerCube shadowSampler3;
  89. #endif
  90. uniform vec3 shadowsInfo3;
  91. #endif
  92. #ifdef SPOTLIGHT3
  93. uniform vec4 vLightDirection3;
  94. #endif
  95. #ifdef HEMILIGHT3
  96. uniform vec3 vLightGround3;
  97. #endif
  98. #endif
  99. // Samplers
  100. #ifdef DIFFUSE
  101. varying vec2 vDiffuseUV;
  102. uniform sampler2D diffuseSampler;
  103. uniform vec2 vDiffuseInfos;
  104. #endif
  105. // Shadows
  106. #ifdef SHADOWS
  107. float unpack(vec4 color)
  108. {
  109. 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);
  110. return dot(color, bit_shift);
  111. }
  112. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  113. uniform vec2 depthValues;
  114. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  115. {
  116. vec3 directionToLight = vPositionW - lightPosition;
  117. float depth = length(directionToLight);
  118. depth = clamp(depth, 0., 1.0);
  119. directionToLight = normalize(directionToLight);
  120. directionToLight.y = - directionToLight.y;
  121. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  122. if (depth > shadow)
  123. {
  124. return darkness;
  125. }
  126. return 1.0;
  127. }
  128. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness, float mapSize)
  129. {
  130. vec3 directionToLight = vPositionW - lightPosition;
  131. float depth = length(directionToLight);
  132. depth = clamp(depth, 0., 1.0);
  133. float diskScale = 2.0 / mapSize;
  134. directionToLight = normalize(directionToLight);
  135. directionToLight.y = -directionToLight.y;
  136. float visibility = 1.;
  137. vec3 poissonDisk[4];
  138. poissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);
  139. poissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);
  140. poissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);
  141. poissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);
  142. // Poisson Sampling
  143. float biasedDepth = depth - bias;
  144. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;
  145. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;
  146. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;
  147. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;
  148. return min(1.0, visibility + darkness);
  149. }
  150. #endif
  151. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  152. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  153. {
  154. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  155. depth = 0.5 * depth + vec3(0.5);
  156. vec2 uv = depth.xy;
  157. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  158. {
  159. return 1.0;
  160. }
  161. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  162. if (depth.z > shadow)
  163. {
  164. return darkness;
  165. }
  166. return 1.;
  167. }
  168. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  169. {
  170. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  171. depth = 0.5 * depth + vec3(0.5);
  172. vec2 uv = depth.xy;
  173. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  174. {
  175. return 1.0;
  176. }
  177. float visibility = 1.;
  178. vec2 poissonDisk[4];
  179. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  180. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  181. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  182. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  183. // Poisson Sampling
  184. float biasedDepth = depth.z - bias;
  185. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
  186. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
  187. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
  188. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
  189. return min(1.0, visibility + darkness);
  190. }
  191. // Thanks to http://devmaster.net/
  192. float unpackHalf(vec2 color)
  193. {
  194. return color.x + (color.y / 255.0);
  195. }
  196. float linstep(float low, float high, float v) {
  197. return clamp((v - low) / (high - low), 0.0, 1.0);
  198. }
  199. float ChebychevInequality(vec2 moments, float compare, float bias)
  200. {
  201. float p = smoothstep(compare - bias, compare, moments.x);
  202. float variance = max(moments.y - moments.x * moments.x, 0.02);
  203. float d = compare - moments.x;
  204. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  205. return clamp(max(p, p_max), 0.0, 1.0);
  206. }
  207. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  208. {
  209. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  210. depth = 0.5 * depth + vec3(0.5);
  211. vec2 uv = depth.xy;
  212. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  213. {
  214. return 1.0;
  215. }
  216. vec4 texel = texture2D(shadowSampler, uv);
  217. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  218. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  219. }
  220. #endif
  221. #endif
  222. #ifdef CLIPPLANE
  223. varying float fClipDistance;
  224. #endif
  225. // Fog
  226. #ifdef FOG
  227. #define FOGMODE_NONE 0.
  228. #define FOGMODE_EXP 1.
  229. #define FOGMODE_EXP2 2.
  230. #define FOGMODE_LINEAR 3.
  231. #define E 2.71828
  232. uniform vec4 vFogInfos;
  233. uniform vec3 vFogColor;
  234. varying float fFogDistance;
  235. float CalcFogFactor()
  236. {
  237. float fogCoeff = 1.0;
  238. float fogStart = vFogInfos.y;
  239. float fogEnd = vFogInfos.z;
  240. float fogDensity = vFogInfos.w;
  241. if (FOGMODE_LINEAR == vFogInfos.x)
  242. {
  243. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  244. }
  245. else if (FOGMODE_EXP == vFogInfos.x)
  246. {
  247. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  248. }
  249. else if (FOGMODE_EXP2 == vFogInfos.x)
  250. {
  251. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  252. }
  253. return clamp(fogCoeff, 0.0, 1.0);
  254. }
  255. #endif
  256. // Light Computing
  257. struct lightingInfo
  258. {
  259. vec3 diffuse;
  260. };
  261. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {
  262. lightingInfo result;
  263. vec3 lightVectorW;
  264. float attenuation = 1.0;
  265. if (lightData.w == 0.)
  266. {
  267. vec3 direction = lightData.xyz - vPositionW;
  268. attenuation = max(0., 1.0 - length(direction) / range);
  269. lightVectorW = normalize(direction);
  270. }
  271. else
  272. {
  273. lightVectorW = normalize(-lightData.xyz);
  274. }
  275. // diffuse
  276. float ndl = max(0., dot(vNormal, lightVectorW));
  277. result.diffuse = ndl * diffuseColor * attenuation;
  278. return result;
  279. }
  280. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {
  281. lightingInfo result;
  282. vec3 direction = lightData.xyz - vPositionW;
  283. vec3 lightVectorW = normalize(direction);
  284. float attenuation = max(0., 1.0 - length(direction) / range);
  285. // diffuse
  286. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  287. float spotAtten = 0.0;
  288. if (cosAngle >= lightDirection.w)
  289. {
  290. cosAngle = max(0., pow(cosAngle, lightData.w));
  291. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  292. // Diffuse
  293. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  294. result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
  295. return result;
  296. }
  297. result.diffuse = vec3(0.);
  298. return result;
  299. }
  300. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {
  301. lightingInfo result;
  302. // Diffuse
  303. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  304. result.diffuse = mix(groundColor, diffuseColor, ndl);
  305. return result;
  306. }
  307. float random( vec3 scale, float seed ){
  308. return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed ) ;
  309. }
  310. void main(void) {
  311. // Clip plane
  312. #ifdef CLIPPLANE
  313. if (fClipDistance > 0.0)
  314. discard;
  315. #endif
  316. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  317. // Base color
  318. vec4 baseColor = vec4(1., 1., 1., 1.);
  319. vec3 diffuseColor = vDiffuseColor.rgb;
  320. // Alpha
  321. float alpha = vDiffuseColor.a;
  322. #ifdef DIFFUSE
  323. ////// MAGMA ///
  324. vec4 noiseTex = texture2D( noiseTexture, vDiffuseUV );
  325. vec2 T1 = vDiffuseUV + vec2( 1.5, -1.5 ) * time * 0.02;
  326. vec2 T2 = vDiffuseUV + vec2( -0.5, 2.0 ) * time * 0.01 * speed;
  327. T1.x += noiseTex.x * 2.0;
  328. T1.y += noiseTex.y * 2.0;
  329. T2.x -= noiseTex.y * 0.2 + time*0.001*movingSpeed;
  330. T2.y += noiseTex.z * 0.2 + time*0.002*movingSpeed;
  331. float p = texture2D( noiseTexture, T1 * 3.0 ).a;
  332. vec4 lavaColor = texture2D( diffuseSampler, T2 * 4.0);
  333. vec4 temp = lavaColor * ( vec4( p, p, p, p ) * 2. ) + ( lavaColor * lavaColor - 0.1 );
  334. baseColor = temp;
  335. float depth = gl_FragCoord.z * 4.0;
  336. const float LOG2 = 1.442695;
  337. float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );
  338. fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );
  339. baseColor = mix( baseColor, vec4( fogColor, baseColor.w ), fogFactor );
  340. ///// END MAGMA ////
  341. // baseColor = texture2D(diffuseSampler, vDiffuseUV);
  342. #ifdef ALPHATEST
  343. if (baseColor.a < 0.4)
  344. discard;
  345. #endif
  346. baseColor.rgb *= vDiffuseInfos.y;
  347. #endif
  348. #ifdef VERTEXCOLOR
  349. baseColor.rgb *= vColor.rgb;
  350. #endif
  351. // Bump
  352. #ifdef NORMAL
  353. vec3 normalW = normalize(vNormalW);
  354. #else
  355. vec3 normalW = vec3(1.0, 1.0, 1.0);
  356. #endif
  357. // Lighting
  358. vec3 diffuseBase = vec3(0., 0., 0.);
  359. float shadow = 1.;
  360. #ifdef LIGHT0
  361. #ifdef SPOTLIGHT0
  362. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);
  363. #endif
  364. #ifdef HEMILIGHT0
  365. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);
  366. #endif
  367. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  368. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);
  369. #endif
  370. #ifdef SHADOW0
  371. #ifdef SHADOWVSM0
  372. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  373. #else
  374. #ifdef SHADOWPCF0
  375. #if defined(POINTLIGHT0)
  376. shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x, shadowsInfo0.y);
  377. #else
  378. shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  379. #endif
  380. #else
  381. #if defined(POINTLIGHT0)
  382. shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  383. #else
  384. shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  385. #endif
  386. #endif
  387. #endif
  388. #else
  389. shadow = 1.;
  390. #endif
  391. diffuseBase += info.diffuse * shadow;
  392. #endif
  393. #ifdef LIGHT1
  394. #ifdef SPOTLIGHT1
  395. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);
  396. #endif
  397. #ifdef HEMILIGHT1
  398. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1.a);
  399. #endif
  400. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  401. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);
  402. #endif
  403. #ifdef SHADOW1
  404. #ifdef SHADOWVSM1
  405. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  406. #else
  407. #ifdef SHADOWPCF1
  408. #if defined(POINTLIGHT1)
  409. shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x, shadowsInfo1.y);
  410. #else
  411. shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  412. #endif
  413. #else
  414. #if defined(POINTLIGHT1)
  415. shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  416. #else
  417. shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  418. #endif
  419. #endif
  420. #endif
  421. #else
  422. shadow = 1.;
  423. #endif
  424. diffuseBase += info.diffuse * shadow;
  425. #endif
  426. #ifdef LIGHT2
  427. #ifdef SPOTLIGHT2
  428. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);
  429. #endif
  430. #ifdef HEMILIGHT2
  431. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);
  432. #endif
  433. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  434. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);
  435. #endif
  436. #ifdef SHADOW2
  437. #ifdef SHADOWVSM2
  438. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  439. #else
  440. #ifdef SHADOWPCF2
  441. #if defined(POINTLIGHT2)
  442. shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x, shadowsInfo2.y);
  443. #else
  444. shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  445. #endif
  446. #else
  447. #if defined(POINTLIGHT2)
  448. shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  449. #else
  450. shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  451. #endif
  452. #endif
  453. #endif
  454. #else
  455. shadow = 1.;
  456. #endif
  457. diffuseBase += info.diffuse * shadow;
  458. #endif
  459. #ifdef LIGHT3
  460. #ifdef SPOTLIGHT3
  461. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);
  462. #endif
  463. #ifdef HEMILIGHT3
  464. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);
  465. #endif
  466. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  467. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);
  468. #endif
  469. #ifdef SHADOW3
  470. #ifdef SHADOWVSM3
  471. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  472. #else
  473. #ifdef SHADOWPCF3
  474. #if defined(POINTLIGHT3)
  475. shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x, shadowsInfo3.y);
  476. #else
  477. shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  478. #endif
  479. #else
  480. #if defined(POINTLIGHT3)
  481. shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  482. #else
  483. shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  484. #endif
  485. #endif
  486. #endif
  487. #else
  488. shadow = 1.;
  489. #endif
  490. diffuseBase += info.diffuse * shadow;
  491. #endif
  492. #ifdef VERTEXALPHA
  493. alpha *= vColor.a;
  494. #endif
  495. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;
  496. // Composition
  497. vec4 color = vec4(finalDiffuse, alpha);
  498. #ifdef FOG
  499. float fog = CalcFogFactor();
  500. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  501. #endif
  502. gl_FragColor = color;
  503. }