/
FlipBook.shader
94 lines (77 loc) · 1.81 KB
/
FlipBook.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// https://unitycoder.com/blog/2018/11/30/sprite-sheet-flip-book-shader/
Shader "UnityLibrary/Sprites/FlipBook (Cutout)"
{
Properties
{
[Header(Texture Sheet)]
_MainTex("Texture", 2D) = "white" {}
_Cutoff("Alpha Cutoff", Range(0,1)) = 0.15
[Header(Settings)]
_ColumnsX("Columns (X)", int) = 1
_RowsY("Rows (Y)", int) = 1
_AnimationSpeed("Frames Per Seconds", float) = 10
}
SubShader
{
Tags {
"Queue" = "AlphaTest"
"IgnoreProjector" = "True"
"PreviewType" = "Plane"
"RenderType" = "TransparentCutout"
"DisableBatching" = "True"
}
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;
};
float _Cutoff;
sampler2D _MainTex;
float4 _MainTex_ST;
uint _ColumnsX;
uint _RowsY;
float _AnimationSpeed;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// get single sprite size
float2 size = float2(1.0f / _ColumnsX, 1.0f / _RowsY);
uint totalFrames = _ColumnsX * _RowsY;
// use timer to increment index
uint index = _Time.y*_AnimationSpeed;
// wrap x and y indexes
uint indexX = index % _ColumnsX;
uint indexY = floor((index % totalFrames) / _ColumnsX);
// get offsets to our sprite index
float2 offset = float2(size.x*indexX,-size.y*indexY);
// get single sprite UV
float2 newUV = v.uv*size;
// flip Y (to start 0 from top)
newUV.y = newUV.y + size.y*(_RowsY - 1);
o.uv = newUV + offset;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// cutout
clip(col.a - _Cutoff);
return col;
}
ENDCG
}
}
}