gradient.fragment.fx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. precision highp float;
  2. // Constants
  3. uniform vec3 vEyePosition;
  4. // Gradient variables
  5. uniform vec3 topColor;
  6. uniform vec3 bottomColor;
  7. varying vec2 vUV;
  8. // Input
  9. varying vec3 vPositionW;
  10. #ifdef NORMAL
  11. varying vec3 vNormalW;
  12. #endif
  13. #ifdef VERTEXCOLOR
  14. varying vec4 vColor;
  15. #endif
  16. // Lights
  17. #ifdef LIGHT0
  18. uniform vec4 vLightData0;
  19. uniform vec4 vLightDiffuse0;
  20. #ifdef 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 SHADOW1
  40. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  41. varying vec4 vPositionFromLight1;
  42. uniform sampler2D shadowSampler1;
  43. #else
  44. uniform samplerCube shadowSampler1;
  45. #endif
  46. uniform vec3 shadowsInfo1;
  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. #ifdef SHADOW2
  59. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  60. varying vec4 vPositionFromLight2;
  61. uniform sampler2D shadowSampler2;
  62. #else
  63. uniform samplerCube shadowSampler2;
  64. #endif
  65. uniform vec3 shadowsInfo2;
  66. #endif
  67. #ifdef SPOTLIGHT2
  68. uniform vec4 vLightDirection2;
  69. #endif
  70. #ifdef HEMILIGHT2
  71. uniform vec3 vLightGround2;
  72. #endif
  73. #endif
  74. #ifdef LIGHT3
  75. uniform vec4 vLightData3;
  76. uniform vec4 vLightDiffuse3;
  77. #ifdef SHADOW3
  78. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  79. varying vec4 vPositionFromLight3;
  80. uniform sampler2D shadowSampler3;
  81. #else
  82. uniform samplerCube shadowSampler3;
  83. #endif
  84. uniform vec3 shadowsInfo3;
  85. #endif
  86. #ifdef SPOTLIGHT3
  87. uniform vec4 vLightDirection3;
  88. #endif
  89. #ifdef HEMILIGHT3
  90. uniform vec3 vLightGround3;
  91. #endif
  92. #endif
  93. // Samplers
  94. #ifdef DIFFUSE
  95. varying vec2 vDiffuseUV;
  96. uniform sampler2D diffuseSampler;
  97. uniform vec2 vDiffuseInfos;
  98. #endif
  99. // Shadows
  100. #ifdef SHADOWS
  101. float unpack(vec4 color)
  102. {
  103. 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);
  104. return dot(color, bit_shift);
  105. }
  106. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  107. uniform vec2 depthValues;
  108. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  109. {
  110. vec3 directionToLight = vPositionW - lightPosition;
  111. float depth = length(directionToLight);
  112. depth = clamp(depth, 0., 1.0);
  113. directionToLight = normalize(directionToLight);
  114. directionToLight.y = - directionToLight.y;
  115. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  116. if (depth > shadow)
  117. {
  118. return darkness;
  119. }
  120. return 1.0;
  121. }
  122. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness, float mapSize)
  123. {
  124. vec3 directionToLight = vPositionW - lightPosition;
  125. float depth = length(directionToLight);
  126. depth = clamp(depth, 0., 1.0);
  127. float diskScale = 2.0 / mapSize;
  128. directionToLight = normalize(directionToLight);
  129. directionToLight.y = -directionToLight.y;
  130. float visibility = 1.;
  131. vec3 poissonDisk[4];
  132. poissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);
  133. poissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);
  134. poissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);
  135. poissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);
  136. // Poisson Sampling
  137. float biasedDepth = depth - bias;
  138. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;
  139. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;
  140. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;
  141. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;
  142. return min(1.0, visibility + darkness);
  143. }
  144. #endif
  145. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  146. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  147. {
  148. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  149. depth = 0.5 * depth + vec3(0.5);
  150. vec2 uv = depth.xy;
  151. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  152. {
  153. return 1.0;
  154. }
  155. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  156. if (depth.z > shadow)
  157. {
  158. return darkness;
  159. }
  160. return 1.;
  161. }
  162. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  163. {
  164. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  165. depth = 0.5 * depth + vec3(0.5);
  166. vec2 uv = depth.xy;
  167. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  168. {
  169. return 1.0;
  170. }
  171. float visibility = 1.;
  172. vec2 poissonDisk[4];
  173. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  174. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  175. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  176. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  177. // Poisson Sampling
  178. float biasedDepth = depth.z - bias;
  179. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
  180. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
  181. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
  182. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
  183. return min(1.0, visibility + darkness);
  184. }
  185. // Thanks to http://devmaster.net/
  186. float unpackHalf(vec2 color)
  187. {
  188. return color.x + (color.y / 255.0);
  189. }
  190. float linstep(float low, float high, float v) {
  191. return clamp((v - low) / (high - low), 0.0, 1.0);
  192. }
  193. float ChebychevInequality(vec2 moments, float compare, float bias)
  194. {
  195. float p = smoothstep(compare - bias, compare, moments.x);
  196. float variance = max(moments.y - moments.x * moments.x, 0.02);
  197. float d = compare - moments.x;
  198. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  199. return clamp(max(p, p_max), 0.0, 1.0);
  200. }
  201. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  202. {
  203. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  204. depth = 0.5 * depth + vec3(0.5);
  205. vec2 uv = depth.xy;
  206. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  207. {
  208. return 1.0;
  209. }
  210. vec4 texel = texture2D(shadowSampler, uv);
  211. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  212. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  213. }
  214. #endif
  215. #endif
  216. #ifdef CLIPPLANE
  217. varying float fClipDistance;
  218. #endif
  219. // Fog
  220. #ifdef FOG
  221. #define FOGMODE_NONE 0.
  222. #define FOGMODE_EXP 1.
  223. #define FOGMODE_EXP2 2.
  224. #define FOGMODE_LINEAR 3.
  225. #define E 2.71828
  226. uniform vec4 vFogInfos;
  227. uniform vec3 vFogColor;
  228. varying float fFogDistance;
  229. float CalcFogFactor()
  230. {
  231. float fogCoeff = 1.0;
  232. float fogStart = vFogInfos.y;
  233. float fogEnd = vFogInfos.z;
  234. float fogDensity = vFogInfos.w;
  235. if (FOGMODE_LINEAR == vFogInfos.x)
  236. {
  237. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  238. }
  239. else if (FOGMODE_EXP == vFogInfos.x)
  240. {
  241. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  242. }
  243. else if (FOGMODE_EXP2 == vFogInfos.x)
  244. {
  245. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  246. }
  247. return clamp(fogCoeff, 0.0, 1.0);
  248. }
  249. #endif
  250. // Light Computing
  251. struct lightingInfo
  252. {
  253. vec3 diffuse;
  254. };
  255. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {
  256. lightingInfo result;
  257. vec3 lightVectorW;
  258. float attenuation = 1.0;
  259. if (lightData.w == 0.)
  260. {
  261. vec3 direction = lightData.xyz - vPositionW;
  262. attenuation = max(0., 1.0 - length(direction) / range);
  263. lightVectorW = normalize(direction);
  264. }
  265. else
  266. {
  267. lightVectorW = normalize(-lightData.xyz);
  268. }
  269. // diffuse
  270. float ndl = max(0., dot(vNormal, lightVectorW));
  271. result.diffuse = ndl * diffuseColor * attenuation;
  272. return result;
  273. }
  274. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {
  275. lightingInfo result;
  276. vec3 direction = lightData.xyz - vPositionW;
  277. vec3 lightVectorW = normalize(direction);
  278. float attenuation = max(0., 1.0 - length(direction) / range);
  279. // diffuse
  280. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  281. float spotAtten = 0.0;
  282. if (cosAngle >= lightDirection.w)
  283. {
  284. cosAngle = max(0., pow(cosAngle, lightData.w));
  285. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  286. // Diffuse
  287. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  288. result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
  289. return result;
  290. }
  291. result.diffuse = vec3(0.);
  292. return result;
  293. }
  294. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {
  295. lightingInfo result;
  296. // Diffuse
  297. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  298. result.diffuse = mix(groundColor, diffuseColor, ndl);
  299. return result;
  300. }
  301. void main(void) {
  302. // Clip plane
  303. #ifdef CLIPPLANE
  304. if (fClipDistance > 0.0)
  305. discard;
  306. #endif
  307. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  308. // Base color
  309. vec4 baseColor = vec4(mix(topColor, bottomColor, vUV.y), 1.0);
  310. vec3 diffuseColor = vec3(1,1,1);
  311. // Alpha
  312. float alpha = 1.0;
  313. #ifdef ALPHATEST
  314. if (baseColor.a < 0.4)
  315. discard;
  316. #endif
  317. #ifdef VERTEXCOLOR
  318. baseColor.rgb *= vColor.rgb;
  319. #endif
  320. // Bump
  321. #ifdef NORMAL
  322. vec3 normalW = normalize(vNormalW);
  323. #else
  324. vec3 normalW = vec3(1.0, 1.0, 1.0);
  325. #endif
  326. // Lighting
  327. vec3 diffuseBase = vec3(0., 0., 0.);
  328. float shadow = 1.;
  329. #ifdef LIGHT0
  330. #ifdef SPOTLIGHT0
  331. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);
  332. #endif
  333. #ifdef HEMILIGHT0
  334. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);
  335. #endif
  336. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  337. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);
  338. #endif
  339. #ifdef SHADOW0
  340. #ifdef SHADOWVSM0
  341. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  342. #else
  343. #ifdef SHADOWPCF0
  344. #if defined(POINTLIGHT0)
  345. shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x, shadowsInfo0.y);
  346. #else
  347. shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  348. #endif
  349. #else
  350. #if defined(POINTLIGHT0)
  351. shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  352. #else
  353. shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  354. #endif
  355. #endif
  356. #endif
  357. #else
  358. shadow = 1.;
  359. #endif
  360. diffuseBase += info.diffuse * shadow;
  361. #endif
  362. #ifdef LIGHT1
  363. #ifdef SPOTLIGHT1
  364. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);
  365. #endif
  366. #ifdef HEMILIGHT1
  367. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1.a);
  368. #endif
  369. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  370. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);
  371. #endif
  372. #ifdef SHADOW1
  373. #ifdef SHADOWVSM1
  374. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  375. #else
  376. #ifdef SHADOWPCF1
  377. #if defined(POINTLIGHT1)
  378. shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x, shadowsInfo1.y);
  379. #else
  380. shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  381. #endif
  382. #else
  383. #if defined(POINTLIGHT1)
  384. shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  385. #else
  386. shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  387. #endif
  388. #endif
  389. #endif
  390. #else
  391. shadow = 1.;
  392. #endif
  393. diffuseBase += info.diffuse * shadow;
  394. #endif
  395. #ifdef LIGHT2
  396. #ifdef SPOTLIGHT2
  397. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);
  398. #endif
  399. #ifdef HEMILIGHT2
  400. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);
  401. #endif
  402. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  403. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);
  404. #endif
  405. #ifdef SHADOW2
  406. #ifdef SHADOWVSM2
  407. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  408. #else
  409. #ifdef SHADOWPCF2
  410. #if defined(POINTLIGHT2)
  411. shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x, shadowsInfo2.y);
  412. #else
  413. shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  414. #endif
  415. #else
  416. #if defined(POINTLIGHT2)
  417. shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  418. #else
  419. shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  420. #endif
  421. #endif
  422. #endif
  423. #else
  424. shadow = 1.;
  425. #endif
  426. diffuseBase += info.diffuse * shadow;
  427. #endif
  428. #ifdef LIGHT3
  429. #ifdef SPOTLIGHT3
  430. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);
  431. #endif
  432. #ifdef HEMILIGHT3
  433. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);
  434. #endif
  435. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  436. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);
  437. #endif
  438. #ifdef SHADOW3
  439. #ifdef SHADOWVSM3
  440. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  441. #else
  442. #ifdef SHADOWPCF3
  443. #if defined(POINTLIGHT3)
  444. shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x, shadowsInfo3.y);
  445. #else
  446. shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  447. #endif
  448. #else
  449. #if defined(POINTLIGHT3)
  450. shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  451. #else
  452. shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  453. #endif
  454. #endif
  455. #endif
  456. #else
  457. shadow = 1.;
  458. #endif
  459. diffuseBase += info.diffuse * shadow;
  460. #endif
  461. #ifdef VERTEXALPHA
  462. alpha *= vColor.a;
  463. #endif
  464. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;
  465. // Composition
  466. vec4 color = vec4(finalDiffuse, alpha);
  467. #ifdef FOG
  468. float fog = CalcFogFactor();
  469. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  470. #endif
  471. gl_FragColor = color;
  472. }