triplanar.fragment.fx 19 KB

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