Skip to content

Instantly share code, notes, and snippets.

@lu1z
Forked from mattatz/TextureAnimation.shader
Created October 7, 2017 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lu1z/d09304890918ecef011c79ff9446f4f6 to your computer and use it in GitHub Desktop.
Save lu1z/d09304890918ecef011c79ff9446f4f6 to your computer and use it in GitHub Desktop.
Texture animation shader for Unity.
Shader "Mattatz/TextureAnimation"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_Cols ("Cols Count", Int) = 5
_Rows ("Rows Count", Int) = 3
_Frame ("Per Frame Length", Float) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
uint _Cols;
uint _Rows;
float _Frame;
fixed4 shot (sampler2D tex, float2 uv, float dx, float dy, int frame) {
return tex2D(tex, float2(
(uv.x * dx) + fmod(frame, _Cols) * dx,
1.0 - ((uv.y * dy) + (frame / _Cols) * dy)
));
}
v2f vert (appdata v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
int frames = _Rows * _Cols;
float frame = fmod(_Time.y / _Frame, frames);
int current = floor(frame);
float dx = 1.0 / _Cols;
float dy = 1.0 / _Rows;
// not lerping to next frame
// return shot(_MainTex, i.uv, dx, dy, current) * _Color;
int next = floor(fmod(frame + 1, frames));
return lerp(shot(_MainTex, i.uv, dx, dy, current), shot(_MainTex, i.uv, dx, dy, next), frame - current) * _Color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment