default.fragment.fx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. #define MAP_EXPLICIT 0.
  5. #define MAP_SPHERICAL 1.
  6. #define MAP_PLANAR 2.
  7. #define MAP_CUBIC 3.
  8. #define MAP_PROJECTION 4.
  9. #define MAP_SKYBOX 5.
  10. // Constants
  11. uniform vec3 vEyePosition;
  12. uniform vec3 vAmbientColor;
  13. uniform vec4 vDiffuseColor;
  14. #ifdef SPECULARTERM
  15. uniform vec4 vSpecularColor;
  16. #endif
  17. uniform vec3 vEmissiveColor;
  18. // Input
  19. varying vec3 vPositionW;
  20. #ifdef NORMAL
  21. varying vec3 vNormalW;
  22. #endif
  23. #ifdef VERTEXCOLOR
  24. varying vec4 vColor;
  25. #endif
  26. // Lights
  27. #ifdef LIGHT0
  28. uniform vec4 vLightData0;
  29. uniform vec4 vLightDiffuse0;
  30. #ifdef SPECULARTERM
  31. uniform vec3 vLightSpecular0;
  32. #endif
  33. #ifdef SHADOW0
  34. varying vec4 vPositionFromLight0;
  35. uniform sampler2D shadowSampler0;
  36. uniform vec3 shadowsInfo0;
  37. #endif
  38. #ifdef SPOTLIGHT0
  39. uniform vec4 vLightDirection0;
  40. #endif
  41. #ifdef HEMILIGHT0
  42. uniform vec3 vLightGround0;
  43. #endif
  44. #endif
  45. #ifdef LIGHT1
  46. uniform vec4 vLightData1;
  47. uniform vec4 vLightDiffuse1;
  48. #ifdef SPECULARTERM
  49. uniform vec3 vLightSpecular1;
  50. #endif
  51. #ifdef SHADOW1
  52. varying vec4 vPositionFromLight1;
  53. uniform sampler2D shadowSampler1;
  54. uniform vec3 shadowsInfo1;
  55. #endif
  56. #ifdef SPOTLIGHT1
  57. uniform vec4 vLightDirection1;
  58. #endif
  59. #ifdef HEMILIGHT1
  60. uniform vec3 vLightGround1;
  61. #endif
  62. #endif
  63. #ifdef LIGHT2
  64. uniform vec4 vLightData2;
  65. uniform vec4 vLightDiffuse2;
  66. #ifdef SPECULARTERM
  67. uniform vec3 vLightSpecular2;
  68. #endif
  69. #ifdef SHADOW2
  70. varying vec4 vPositionFromLight2;
  71. uniform sampler2D shadowSampler2;
  72. uniform vec3 shadowsInfo2;
  73. #endif
  74. #ifdef SPOTLIGHT2
  75. uniform vec4 vLightDirection2;
  76. #endif
  77. #ifdef HEMILIGHT2
  78. uniform vec3 vLightGround2;
  79. #endif
  80. #endif
  81. #ifdef LIGHT3
  82. uniform vec4 vLightData3;
  83. uniform vec4 vLightDiffuse3;
  84. #ifdef SPECULARTERM
  85. uniform vec3 vLightSpecular3;
  86. #endif
  87. #ifdef SHADOW3
  88. varying vec4 vPositionFromLight3;
  89. uniform sampler2D shadowSampler3;
  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. #ifdef AMBIENT
  106. varying vec2 vAmbientUV;
  107. uniform sampler2D ambientSampler;
  108. uniform vec2 vAmbientInfos;
  109. #endif
  110. #ifdef OPACITY
  111. varying vec2 vOpacityUV;
  112. uniform sampler2D opacitySampler;
  113. uniform vec2 vOpacityInfos;
  114. #endif
  115. #ifdef EMISSIVE
  116. varying vec2 vEmissiveUV;
  117. uniform vec2 vEmissiveInfos;
  118. uniform sampler2D emissiveSampler;
  119. #endif
  120. #ifdef LIGHTMAP
  121. varying vec2 vLightmapUV;
  122. uniform vec3 vLightmapInfos;
  123. uniform sampler2D lightmapSampler;
  124. #endif
  125. #if defined(SPECULAR) && defined(SPECULARTERM)
  126. varying vec2 vSpecularUV;
  127. uniform vec2 vSpecularInfos;
  128. uniform sampler2D specularSampler;
  129. #endif
  130. // Fresnel
  131. #ifdef FRESNEL
  132. float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
  133. {
  134. float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
  135. return clamp(fresnelTerm, 0., 1.);
  136. }
  137. #endif
  138. #ifdef DIFFUSEFRESNEL
  139. uniform vec4 diffuseLeftColor;
  140. uniform vec4 diffuseRightColor;
  141. #endif
  142. #ifdef OPACITYFRESNEL
  143. uniform vec4 opacityParts;
  144. #endif
  145. #ifdef REFLECTIONFRESNEL
  146. uniform vec4 reflectionLeftColor;
  147. uniform vec4 reflectionRightColor;
  148. #endif
  149. #ifdef EMISSIVEFRESNEL
  150. uniform vec4 emissiveLeftColor;
  151. uniform vec4 emissiveRightColor;
  152. #endif
  153. // Reflection
  154. #ifdef REFLECTION
  155. varying vec3 vPositionUVW;
  156. uniform samplerCube reflectionCubeSampler;
  157. uniform sampler2D reflection2DSampler;
  158. uniform vec3 vReflectionInfos;
  159. uniform mat4 reflectionMatrix;
  160. uniform mat4 view;
  161. #ifdef ROUGHNESS
  162. uniform float roughness;
  163. #endif
  164. vec3 computeReflectionCoords(float mode, vec4 worldPos, vec3 worldNormal)
  165. {
  166. if (mode == MAP_SPHERICAL)
  167. {
  168. vec3 coords = vec3(view * vec4(worldNormal, 0.0));
  169. return vec3(reflectionMatrix * vec4(coords, 1.0));
  170. }
  171. else if (mode == MAP_PLANAR)
  172. {
  173. vec3 viewDir = worldPos.xyz - vEyePosition;
  174. vec3 coords = normalize(reflect(viewDir, worldNormal));
  175. return vec3(reflectionMatrix * vec4(coords, 1));
  176. }
  177. else if (mode == MAP_CUBIC)
  178. {
  179. vec3 viewDir = worldPos.xyz - vEyePosition;
  180. vec3 coords = reflect(viewDir, worldNormal);
  181. return vec3(reflectionMatrix * vec4(coords, 0));
  182. }
  183. else if (mode == MAP_PROJECTION)
  184. {
  185. return vec3(reflectionMatrix * (view * worldPos));
  186. }
  187. else if (mode == MAP_SKYBOX)
  188. {
  189. return vPositionUVW;
  190. }
  191. return vec3(0, 0, 0);
  192. }
  193. #endif
  194. // Shadows
  195. #ifdef SHADOWS
  196. float unpack(vec4 color)
  197. {
  198. 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);
  199. return dot(color, bit_shift);
  200. }
  201. float unpackHalf(vec2 color)
  202. {
  203. return color.x + (color.y / 255.0);
  204. }
  205. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  206. {
  207. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  208. depth = 0.5 * depth + vec3(0.5);
  209. vec2 uv = depth.xy;
  210. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  211. {
  212. return 1.0;
  213. }
  214. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  215. if (depth.z > shadow)
  216. {
  217. return darkness;
  218. }
  219. return 1.;
  220. }
  221. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, 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)
  227. {
  228. return 1.0;
  229. }
  230. float visibility = 1.;
  231. vec2 poissonDisk[4];
  232. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  233. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  234. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  235. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  236. // Poisson Sampling
  237. float biasedDepth = depth.z - bias;
  238. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
  239. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
  240. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
  241. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
  242. return min(1.0, visibility + darkness);
  243. }
  244. // Thanks to http://devmaster.net/
  245. float linstep(float low, float high, float v) {
  246. return clamp((v - low) / (high - low), 0.0, 1.0);
  247. }
  248. float ChebychevInequality(vec2 moments, float compare, float bias)
  249. {
  250. float p = smoothstep(compare - bias, compare, moments.x);
  251. float variance = max(moments.y - moments.x * moments.x, 0.02);
  252. float d = compare - moments.x;
  253. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  254. return clamp(max(p, p_max), 0.0, 1.0);
  255. }
  256. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  257. {
  258. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  259. depth = 0.5 * depth + vec3(0.5);
  260. vec2 uv = depth.xy;
  261. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  262. {
  263. return 1.0;
  264. }
  265. vec4 texel = texture2D(shadowSampler, uv);
  266. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  267. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  268. }
  269. #endif
  270. // Bump
  271. #ifdef BUMP
  272. #extension GL_OES_standard_derivatives : enable
  273. varying vec2 vBumpUV;
  274. uniform vec2 vBumpInfos;
  275. uniform sampler2D bumpSampler;
  276. // Thanks to http://www.thetenthplanet.de/archives/1180
  277. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  278. {
  279. // get edge vectors of the pixel triangle
  280. vec3 dp1 = dFdx(p);
  281. vec3 dp2 = dFdy(p);
  282. vec2 duv1 = dFdx(uv);
  283. vec2 duv2 = dFdy(uv);
  284. // solve the linear system
  285. vec3 dp2perp = cross(dp2, normal);
  286. vec3 dp1perp = cross(normal, dp1);
  287. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  288. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  289. // construct a scale-invariant frame
  290. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  291. return mat3(tangent * invmax, binormal * invmax, normal);
  292. }
  293. vec3 perturbNormal(vec3 viewDir)
  294. {
  295. vec3 map = texture2D(bumpSampler, vBumpUV).xyz;
  296. map = map * 255. / 127. - 128. / 127.;
  297. mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);
  298. return normalize(TBN * map);
  299. }
  300. #endif
  301. #ifdef CLIPPLANE
  302. varying float fClipDistance;
  303. #endif
  304. // Fog
  305. #ifdef FOG
  306. #define FOGMODE_NONE 0.
  307. #define FOGMODE_EXP 1.
  308. #define FOGMODE_EXP2 2.
  309. #define FOGMODE_LINEAR 3.
  310. #define E 2.71828
  311. uniform vec4 vFogInfos;
  312. uniform vec3 vFogColor;
  313. varying float fFogDistance;
  314. float CalcFogFactor()
  315. {
  316. float fogCoeff = 1.0;
  317. float fogStart = vFogInfos.y;
  318. float fogEnd = vFogInfos.z;
  319. float fogDensity = vFogInfos.w;
  320. if (FOGMODE_LINEAR == vFogInfos.x)
  321. {
  322. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  323. }
  324. else if (FOGMODE_EXP == vFogInfos.x)
  325. {
  326. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  327. }
  328. else if (FOGMODE_EXP2 == vFogInfos.x)
  329. {
  330. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  331. }
  332. return clamp(fogCoeff, 0.0, 1.0);
  333. }
  334. #endif
  335. // Light Computing
  336. struct lightingInfo
  337. {
  338. vec3 diffuse;
  339. #ifdef SPECULARTERM
  340. vec3 specular;
  341. #endif
  342. };
  343. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
  344. lightingInfo result;
  345. vec3 lightVectorW;
  346. float attenuation = 1.0;
  347. if (lightData.w == 0.)
  348. {
  349. vec3 direction = lightData.xyz - vPositionW;
  350. attenuation = max(0., 1.0 - length(direction) / range);
  351. lightVectorW = normalize(direction);
  352. }
  353. else
  354. {
  355. lightVectorW = normalize(-lightData.xyz);
  356. }
  357. // diffuse
  358. float ndl = max(0., dot(vNormal, lightVectorW));
  359. result.diffuse = ndl * diffuseColor * attenuation;
  360. #ifdef SPECULARTERM
  361. // Specular
  362. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  363. float specComp = max(0., dot(vNormal, angleW));
  364. specComp = pow(specComp, max(1., glossiness));
  365. result.specular = specComp * specularColor * attenuation;
  366. #endif
  367. return result;
  368. }
  369. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
  370. lightingInfo result;
  371. vec3 direction = lightData.xyz - vPositionW;
  372. vec3 lightVectorW = normalize(direction);
  373. float attenuation = max(0., 1.0 - length(direction) / range);
  374. // diffuse
  375. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  376. float spotAtten = 0.0;
  377. if (cosAngle >= lightDirection.w)
  378. {
  379. cosAngle = max(0., pow(cosAngle, lightData.w));
  380. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  381. // Diffuse
  382. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  383. result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
  384. #ifdef SPECULARTERM
  385. // Specular
  386. vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
  387. float specComp = max(0., dot(vNormal, angleW));
  388. specComp = pow(specComp, max(1., glossiness));
  389. result.specular = specComp * specularColor * spotAtten * attenuation;
  390. #endif
  391. return result;
  392. }
  393. result.diffuse = vec3(0.);
  394. #ifdef SPECULARTERM
  395. result.specular = vec3(0.);
  396. #endif
  397. return result;
  398. }
  399. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {
  400. lightingInfo result;
  401. // Diffuse
  402. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  403. result.diffuse = mix(groundColor, diffuseColor, ndl);
  404. #ifdef SPECULARTERM
  405. // Specular
  406. vec3 angleW = normalize(viewDirectionW + lightData.xyz);
  407. float specComp = max(0., dot(vNormal, angleW));
  408. specComp = pow(specComp, max(1., glossiness));
  409. result.specular = specComp * specularColor;
  410. #endif
  411. return result;
  412. }
  413. void main(void) {
  414. // Clip plane
  415. #ifdef CLIPPLANE
  416. if (fClipDistance > 0.0)
  417. discard;
  418. #endif
  419. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  420. // Base color
  421. vec4 baseColor = vec4(1., 1., 1., 1.);
  422. vec3 diffuseColor = vDiffuseColor.rgb;
  423. // Alpha
  424. float alpha = vDiffuseColor.a;
  425. #ifdef DIFFUSE
  426. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  427. #ifdef ALPHATEST
  428. if (baseColor.a < 0.4)
  429. discard;
  430. #endif
  431. #ifdef ALPHAFROMDIFFUSE
  432. alpha *= baseColor.a;
  433. #endif
  434. baseColor.rgb *= vDiffuseInfos.y;
  435. #endif
  436. #ifdef VERTEXCOLOR
  437. baseColor.rgb *= vColor.rgb;
  438. #endif
  439. // Bump
  440. #ifdef NORMAL
  441. vec3 normalW = normalize(vNormalW);
  442. #else
  443. vec3 normalW = vec3(1.0, 1.0, 1.0);
  444. #endif
  445. #ifdef BUMP
  446. normalW = perturbNormal(viewDirectionW);
  447. #endif
  448. // Ambient color
  449. vec3 baseAmbientColor = vec3(1., 1., 1.);
  450. #ifdef AMBIENT
  451. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  452. #endif
  453. // Specular map
  454. #ifdef SPECULARTERM
  455. float glossiness = vSpecularColor.a;
  456. vec3 specularColor = vSpecularColor.rgb;
  457. #ifdef SPECULAR
  458. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);
  459. specularColor = specularMapColor.rgb;
  460. #ifdef GLOSSINESS
  461. glossiness = glossiness * specularMapColor.a;
  462. #endif
  463. #endif
  464. #else
  465. float glossiness = 0.;
  466. #endif
  467. // Lighting
  468. vec3 diffuseBase = vec3(0., 0., 0.);
  469. #ifdef SPECULARTERM
  470. vec3 specularBase = vec3(0., 0., 0.);
  471. #endif
  472. float shadow = 1.;
  473. #ifdef LIGHT0
  474. #ifndef SPECULARTERM
  475. vec3 vLightSpecular0 = vec3(0.0);
  476. #endif
  477. #ifdef SPOTLIGHT0
  478. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
  479. #endif
  480. #ifdef HEMILIGHT0
  481. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);
  482. #endif
  483. #ifdef POINTDIRLIGHT0
  484. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
  485. #endif
  486. #ifdef SHADOW0
  487. #ifdef SHADOWVSM0
  488. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  489. #else
  490. #ifdef SHADOWPCF0
  491. shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  492. #else
  493. shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  494. #endif
  495. #endif
  496. #else
  497. shadow = 1.;
  498. #endif
  499. diffuseBase += info.diffuse * shadow;
  500. #ifdef SPECULARTERM
  501. specularBase += info.specular * shadow;
  502. #endif
  503. #endif
  504. #ifdef LIGHT1
  505. #ifndef SPECULARTERM
  506. vec3 vLightSpecular1 = vec3(0.0);
  507. #endif
  508. #ifdef SPOTLIGHT1
  509. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
  510. #endif
  511. #ifdef HEMILIGHT1
  512. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, glossiness);
  513. #endif
  514. #ifdef POINTDIRLIGHT1
  515. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
  516. #endif
  517. #ifdef SHADOW1
  518. #ifdef SHADOWVSM1
  519. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  520. #else
  521. #ifdef SHADOWPCF1
  522. shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  523. #else
  524. shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  525. #endif
  526. #endif
  527. #else
  528. shadow = 1.;
  529. #endif
  530. diffuseBase += info.diffuse * shadow;
  531. #ifdef SPECULARTERM
  532. specularBase += info.specular * shadow;
  533. #endif
  534. #endif
  535. #ifdef LIGHT2
  536. #ifndef SPECULARTERM
  537. vec3 vLightSpecular2 = vec3(0.0);
  538. #endif
  539. #ifdef SPOTLIGHT2
  540. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
  541. #endif
  542. #ifdef HEMILIGHT2
  543. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);
  544. #endif
  545. #ifdef POINTDIRLIGHT2
  546. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
  547. #endif
  548. #ifdef SHADOW2
  549. #ifdef SHADOWVSM2
  550. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  551. #else
  552. #ifdef SHADOWPCF2
  553. shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  554. #else
  555. shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  556. #endif
  557. #endif
  558. #else
  559. shadow = 1.;
  560. #endif
  561. diffuseBase += info.diffuse * shadow;
  562. #ifdef SPECULARTERM
  563. specularBase += info.specular * shadow;
  564. #endif
  565. #endif
  566. #ifdef LIGHT3
  567. #ifndef SPECULARTERM
  568. vec3 vLightSpecular3 = vec3(0.0);
  569. #endif
  570. #ifdef SPOTLIGHT3
  571. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
  572. #endif
  573. #ifdef HEMILIGHT3
  574. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);
  575. #endif
  576. #ifdef POINTDIRLIGHT3
  577. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
  578. #endif
  579. #ifdef SHADOW3
  580. #ifdef SHADOWVSM3
  581. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  582. #else
  583. #ifdef SHADOWPCF3
  584. shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  585. #else
  586. shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  587. #endif
  588. #endif
  589. #else
  590. shadow = 1.;
  591. #endif
  592. diffuseBase += info.diffuse * shadow;
  593. #ifdef SPECULARTERM
  594. specularBase += info.specular * shadow;
  595. #endif
  596. #endif
  597. // Reflection
  598. vec3 reflectionColor = vec3(0., 0., 0.);
  599. #ifdef REFLECTION
  600. vec3 vReflectionUVW = computeReflectionCoords(vReflectionInfos.x, vec4(vPositionW, 1.0), normalW);
  601. if (vReflectionInfos.z != 0.0)
  602. {
  603. float bias = 0.;
  604. #ifdef ROUGHNESS
  605. bias = roughness;
  606. #endif
  607. #ifdef SPECULARTERM
  608. #ifdef SPECULAR
  609. #ifdef GLOSSINESS
  610. bias *= (1.0 - specularMapColor.a);
  611. #endif
  612. #endif
  613. #endif
  614. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.y * shadow;
  615. }
  616. else
  617. {
  618. vec2 coords = vReflectionUVW.xy;
  619. if (vReflectionInfos.x == MAP_PROJECTION)
  620. {
  621. coords /= vReflectionUVW.z;
  622. }
  623. coords.y = 1.0 - coords.y;
  624. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.y * shadow;
  625. }
  626. #ifdef REFLECTIONFRESNEL
  627. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  628. #ifdef REFLECTIONFRESNELFROMSPECULAR
  629. #ifdef SPECULARTERM
  630. reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  631. #else
  632. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  633. #endif
  634. #else
  635. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  636. #endif
  637. #endif
  638. #endif
  639. #ifdef OPACITY
  640. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  641. #ifdef OPACITYRGB
  642. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  643. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  644. #else
  645. alpha *= opacityMap.a * vOpacityInfos.y;
  646. #endif
  647. #endif
  648. #ifdef VERTEXALPHA
  649. alpha *= vColor.a;
  650. #endif
  651. #ifdef OPACITYFRESNEL
  652. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  653. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  654. #endif
  655. // Emissive
  656. vec3 emissiveColor = vEmissiveColor;
  657. #ifdef EMISSIVE
  658. emissiveColor += texture2D(emissiveSampler, vEmissiveUV).rgb * vEmissiveInfos.y;
  659. #endif
  660. #ifdef EMISSIVEFRESNEL
  661. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  662. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  663. #endif
  664. // Fresnel
  665. #ifdef DIFFUSEFRESNEL
  666. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  667. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  668. #endif
  669. // Composition
  670. #ifdef EMISSIVEASILLUMINATION
  671. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  672. #else
  673. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  674. #endif
  675. #ifdef SPECULARTERM
  676. vec3 finalSpecular = specularBase * specularColor;
  677. #else
  678. vec3 finalSpecular = vec3(0.0);
  679. #endif
  680. #ifdef SPECULAROVERALPHA
  681. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  682. #endif
  683. // Composition
  684. #ifdef EMISSIVEASILLUMINATION
  685. vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor, 0.0, 1.0), alpha);
  686. #else
  687. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
  688. #endif
  689. #ifdef LIGHTMAP
  690. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  691. float lightmapIllum = clamp(dot(lightmapColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
  692. if (lightmapIllum > vLightmapInfos.z)
  693. {
  694. color.rgb += lightmapColor;
  695. }
  696. else
  697. {
  698. color.rgb *= lightmapColor;
  699. }
  700. #endif
  701. #ifdef FOG
  702. float fog = CalcFogFactor();
  703. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  704. #endif
  705. gl_FragColor = color;
  706. }