terrain.fragment.fx 20 KB

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