I’m a noob in shaders programming, making a game with 2DToolkit and I want to get a shader to animate tiles in a tilemap ( I know I could control the animation with c# script but I want to do with a shader)
Is this possible?
Here is my shader:
Shader "Custom/AnimSprite"
{
Properties
{
_MainTex ("Main Texture (RGB)", 2D) = "white" {}
// Create the properties below
_TotalFrames ("Total FRames", float) = 0.0
_InitFrame ("Init Frame", float) = 0.0
_FPS ("FPS", float) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
//Create the connection to the properties inside of the
//CG program
float _TotalFrames;
float _InitFrame;
float _FPS;
int _Tick;
struct Input
{float2 uv_MainTex;};
void surf (Input IN, inout SurfaceOutput o)
{
//Lets store our UVs in a seperate variable
float2 spriteUV = IN.uv_MainTex;
//Lets calculate the width of a single frame in our
//sprite sheet and get a uv percentage that each frame takes up.
float frameUVPercentage = 1.0/_TotalFrames;
//Animate the uv's tiling
float xValue = spriteUV.x;
_Tick = _Time.y * _FPS;
xValue += frameUVPercentage * _Tick;
spriteUV = float2(xValue, spriteUV.y);
half4 c = tex2D (_MainTex, spriteUV);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Using _Time.y the _Tick is increasing and I don’t know how to reset the _Tick value.
I have read about the use of sin with _Time but my idea is increasing the texture tiling and upon reaching at the final tiling, reset the tiling to the first tile.
Something like that:
Thank you.