default.fragment.fx 18 KB

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