terrain.fragment.fx 19 KB

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