spriteMap.fragment.fx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. precision highp float;
  2. varying vec3 vPosition;
  3. varying vec2 vUV;
  4. varying vec2 tUV;
  5. uniform float time;
  6. uniform float spriteCount;
  7. uniform sampler2D spriteSheet;
  8. uniform vec2 spriteMapSize;
  9. uniform vec2 outputSize;
  10. uniform vec2 stageSize;
  11. uniform sampler2D frameMap;
  12. uniform sampler2D tileMaps[LAYERS];
  13. uniform sampler2D animationMap;
  14. uniform vec3 colorMul;
  15. float mt;
  16. const float fdStep = 1. / 4.;
  17. const float aFrameSteps = 1. / MAX_ANIMATION_FRAMES;
  18. mat4 getFrameData(float frameID){
  19. float fX = frameID / spriteCount;
  20. return mat4(
  21. texture2D(frameMap, vec2(fX, 0.), 0.),
  22. texture2D(frameMap, vec2(fX, fdStep * 1.), 0.),
  23. texture2D(frameMap, vec2(fX, fdStep * 2.), 0.),
  24. vec4(0.)
  25. );
  26. }
  27. void main(){
  28. vec4 color = vec4(0.);
  29. vec2 tileUV = fract(tUV);
  30. #ifdef FLIPU
  31. tileUV.y = 1.0 - tileUV.y;
  32. #endif
  33. vec2 tileID = floor(tUV);
  34. vec2 sheetUnits = 1. / spriteMapSize;
  35. float spriteUnits = 1. / spriteCount;
  36. vec2 stageUnits = 1. / stageSize;
  37. for(int i = 0; i < LAYERS; i++) {
  38. float frameID;
  39. #define LAYER_ID_SWITCH
  40. vec4 animationData = texture2D(animationMap, vec2((frameID + 0.5) / spriteCount, 0.), 0.);
  41. if(animationData.y > 0.) {
  42. mt = mod(time*animationData.z, 1.0);
  43. for(float f = 0.; f < MAX_ANIMATION_FRAMES; f++){
  44. if(animationData.y > mt){
  45. frameID = animationData.x;
  46. break;
  47. }
  48. animationData = texture2D(animationMap, vec2((frameID + 0.5) / spriteCount, aFrameSteps * f), 0.);
  49. }
  50. }
  51. //Get Animation Frame
  52. mat4 frameData = getFrameData(frameID + 0.5);
  53. vec2 frameSize = (frameData[0].wz) / spriteMapSize;
  54. vec2 offset = frameData[0].xy * sheetUnits;
  55. vec2 ratio = frameData[2].xy / frameData[0].wz;
  56. //rotated
  57. if (frameData[2].z == 1.){
  58. tileUV.xy = tileUV.yx;
  59. }
  60. if (i == 0){
  61. color = texture2D(spriteSheet, tileUV * frameSize+offset);
  62. } else {
  63. vec4 nc = texture2D(spriteSheet, tileUV * frameSize+offset);
  64. float alpha = min(color.a + nc.a, 1.0);
  65. vec3 mixed = mix(color.xyz, nc.xyz, nc.a);
  66. color = vec4(mixed, alpha);
  67. }
  68. }
  69. color.xyz *= colorMul;
  70. gl_FragColor = color;
  71. }