Shader "SpriteSheets" {
Properties {
_MainTex("Base (RGB)", 2D) = "white" {} // texture
_TexWidth("Sheet Width", float) = 0.0 // the width of the texture
_CellAmount("Cell Amount", float) = 0.0 // the number of cells
_Speed("Speed", Range(0.01, 32)) = 12
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
float _TexWidth;
float _CellAmount;
float _Speed;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
// Store UVs in a separate variable
float2 spriteUV = IN.uv_MainTex;
// Calculate the width of a single cell in sprite sheet and get a UV percentage that each cell takes up
float cellPixelWidth = _TexWidth/_CellAmount; // the width of a cell
float cellUVPercentage = cellPixelWidth/_TexWidth; // the percentage of a cell
// Get a stair step value out of time to increment the uv offset
float timeVal = fmod(_Time.y * _Speed, _CellAmount); // returns the remainder of x/y with the same sign as x
timeVal = ceil(timeVal); // ceiling
// Animate the uvs forward by the width percentage of each cell
float xValue = spriteUV.x; // current x position of the texture
xValue += cellUVPercentage * timeVal * _CellAmount; //
xValue *= cellUVPercentage; // new position
spriteUV = float2(xValue, spriteUV.y); // x = new position, y = static position
half4 c = tex2D(_MainTex, spriteUV);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}