default.fragment.fx 27 KB

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