spriteMap.fragment.js 2.1 KB

123456789101112131415161718192021222324252627282930
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. const name = "spriteMapPixelShader";
  4. const shader = `#if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
  5. #define TEXTUREFUNC(s,c,l) texture2DLodEXT(s,c,l)
  6. #else
  7. #define TEXTUREFUNC(s,c,b) texture2D(s,c,b)
  8. #endif
  9. precision highp float;varying vec3 vPosition;varying vec2 vUV;varying vec2 tUV;uniform float time;uniform float spriteCount;uniform sampler2D spriteSheet;uniform vec2 spriteMapSize;uniform vec2 outputSize;uniform vec2 stageSize;uniform sampler2D frameMap;uniform sampler2D tileMaps[LAYERS];uniform sampler2D animationMap;uniform vec3 colorMul;float mt;const float fdStep=1./4.;const float aFrameSteps=1./MAX_ANIMATION_FRAMES;mat4 getFrameData(float frameID){float fX=frameID/spriteCount;return mat4(
  10. texture2D(frameMap,vec2(fX,0.),0.),
  11. texture2D(frameMap,vec2(fX,fdStep*1.),0.),
  12. texture2D(frameMap,vec2(fX,fdStep*2.),0.),
  13. vec4(0.)
  14. );}
  15. void main(){vec4 color=vec4(0.);vec2 tileUV=fract(tUV);
  16. #ifdef FLIPU
  17. tileUV.y=1.0-tileUV.y;
  18. #endif
  19. vec2 tileID=floor(tUV);vec2 sheetUnits=1./spriteMapSize;float spriteUnits=1./spriteCount;vec2 stageUnits=1./stageSize;for(int i=0; i<LAYERS; i++) {float frameID;
  20. #define LAYER_ID_SWITCH
  21. vec4 animationData=TEXTUREFUNC(animationMap,vec2((frameID+0.5)/spriteCount,0.),0.);if(animationData.y>0.) {mt=mod(time*animationData.z,1.0);for(float f=0.; f<MAX_ANIMATION_FRAMES; f++){if(animationData.y>mt){frameID=animationData.x;break;}
  22. animationData=TEXTUREFUNC(animationMap,vec2((frameID+0.5)/spriteCount,aFrameSteps*f),0.);}}
  23. mat4 frameData=getFrameData(frameID+0.5);vec2 frameSize=(frameData[0].zw)/spriteMapSize;vec2 offset=frameData[0].xy*sheetUnits;vec2 ratio=frameData[2].xy/frameData[0].zw;if (frameData[2].z==1.){tileUV.xy=tileUV.yx;}
  24. vec4 nc=texture2D(spriteSheet,tileUV*frameSize+offset);if (i==0){color=nc;} else {float alpha=min(color.a+nc.a,1.0);vec3 mixed=mix(color.xyz,nc.xyz,nc.a);color=vec4(mixed,alpha);}}
  25. color.xyz*=colorMul;gl_FragColor=color;}`;
  26. // Sideeffect
  27. ShaderStore.ShadersStore[name] = shader;
  28. /** @internal */
  29. export const spriteMapPixelShader = { name, shader };
  30. //# sourceMappingURL=spriteMap.fragment.js.map