-
Notifications
You must be signed in to change notification settings - Fork 3
/
UVAnim.js
88 lines (74 loc) · 2.04 KB
/
UVAnim.js
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
#pragma strict
//----------------------------------------
// Given an atlas of frames, this plays those frames by modifying the UV offset
//----------------------------------------
var overrideTexture : Texture2D = null;
var fps : float = 30;
var numFrames : int;
var loop : boolean = true;
var playOnAwake : boolean = true;
//----------------------------------------
// Atlas specs
//----------------------------------------
var framePixels : Vector2; // size of each frame in the atlas in pixels
var numRows:int;
var numCols:int;
//----------------------------------------
//
//----------------------------------------
private var playStartTime : float;
private var playing : boolean = false;
function Awake() {
// set the UV scale properly
for( var mat in GetComponent.<Renderer>().materials )
{
if( overrideTexture != null )
mat.SetTexture('_MainTex', overrideTexture);
var tw = mat.GetTexture('_MainTex').width;
var th = mat.GetTexture('_MainTex').height;
var uScale = framePixels.x / tw;
var vScale = framePixels.y / th;
mat.SetTextureScale( '_MainTex', Vector2(uScale, vScale) );
}
if( playOnAwake )
Play();
}
function Play() {
playStartTime = Time.time;
playing = true;
}
function IsPlaying() : boolean
{
return playing;
}
function GetDuration() : float
{
return numFrames / fps;
}
function Update () {
if( playing )
{
var elapsed : float = Time.time - playStartTime;
if( !loop && elapsed > GetDuration() )
{
// we're done
playing = false;
}
else
{
// update the frame
var currFrame : int = Mathf.Floor( elapsed * fps ) % numFrames;
var col = (currFrame % numCols);
var row = (currFrame - col)/numRows;
for( var mat in GetComponent.<Renderer>().materials )
{
var atlas = mat.GetTexture('_MainTex');
var uOfs = (col * framePixels.x) / atlas.width;
var vOfs = 1.0 - ((row+1) * framePixels.y) / atlas.height; // v = 0 is the BOTTOM, thus we flip Y
var uvOffset = Vector2( uOfs, vOfs );
mat.SetTextureOffset( '_MainTex', uvOffset );
}
}
}
}
@script RequireComponent(Renderer)