UnityMol  0.9.6-875
UnityMol viewer / In developement
Sprite.cs
Go to the documentation of this file.
1 
66 //-----------------------------------------------------------------
67 // Sprite (part of SpriteManager) v0.633 (8-03-2009)
68 // Copyright 2009 Brady Wright and Above and Beyond Software
69 // All rights reserved
70 //-----------------------------------------------------------------
71 // A class to allow the drawing of multiple "quads" as part of a
72 // single aggregated mesh so as to achieve multiple, independently
73 // moving objects using a single draw call.
74 //-----------------------------------------------------------------
75 
76 
77 using UnityEngine;
78 using System.Collections;
79 
80 //-----------------------------------------------------------------
81 // Describes a sprite
82 //-----------------------------------------------------------------
83 public class Sprite
84 {
85  protected float m_width; // Width and Height of the sprite in worldspace units
86  protected float m_height;
87  protected Vector2 m_lowerLeftUV; // UV coordinate for the upper-left corner of the sprite
88  protected Vector2 m_UVDimensions; // Distance from the upper-left UV to place the other UVs
89  protected GameObject m_client; // Reference to the client GameObject
90  protected SpriteManager m_manager; // Reference to the sprite manager in which this sprite resides
91  protected bool m_billboarded = false; // Is the sprite to be billboarded?
92  public bool m_hidden___DoNotAccessExternally = false; // Indicates whether this sprite is currently hidden (has to be public because C# has no "friend" feature, just don't access directly from outside)
93 
94  protected Vector3[] meshVerts; // Pointer to the array of vertices in the mesh
95  protected Vector2[] UVs; // Pointer to the array of UVs in the mesh
96 
97  public Transform clientTransform; // Transform of the client GameObject
98  public Vector3 offset = new Vector3(); // Offset of sprite from center of client GameObject
99  public Color color; // The color to be used by all four vertices
100 
101  public int index; // Index of this sprite in its SpriteManager's list
102  public int drawLayer; // The draw layer indicating the order in which this sprite should be rendered relative to other sprites
103 
104  public Vector3 v1 = new Vector3(); // The sprite's vertices in local space
105  public Vector3 v2 = new Vector3();
106  public Vector3 v3 = new Vector3();
107  public Vector3 v4 = new Vector3();
108 
109  public int mv1; // Indices of the associated vertices in the actual mesh (this just provides a quicker way for the SpriteManager to get straight to the right vertices in the vertex array)
110  public int mv2;
111  public int mv3;
112  public int mv4;
113 
114  public int uv1; // Indices of the associated UVs in the mesh
115  public int uv2;
116  public int uv3;
117  public int uv4;
118 
119  public int cv1; // Indices of the associated color values in the mesh
120  public int cv2;
121  public int cv3;
122  public int cv4;
123 
124  // Animation-related vars and types:
125  public delegate void AnimCompleteDelegate(); // Definition of delegate to be called upon animation completion
126 
127  protected ArrayList animations = new ArrayList(); // Array of available animations
128  protected UVAnimation curAnim = null; // The current animation
129  protected AnimCompleteDelegate animCompleteDelegate = null; // Delegate to be called upon animation completion
130  protected float timeSinceLastFrame = 0; // The total time since our last animation frame change
131  protected float timeBetweenAnimFrames; // The amount of time we want to pass before moving to the next frame of animation
132  protected int framesToAdvance; // (working) The number of animation frames to advance given the time elapsed
133 
135  {
136  }
137 
138 
139  public Sprite()
140  {
141  m_width = 0;
142  m_height = 0;
143  m_client = null;
144  m_manager = null;
145  clientTransform = null;
146  index = 0;
147  drawLayer = 0;
148  color = Color.white;
149 
150  offset = Vector3.zero;
151  }
152 
153  public SpriteManager manager
154  {
155  get { return m_manager; }
156  set { m_manager = value; }
157  }
158 
159  public GameObject client
160  {
161  get { return m_client; }
162  set
163  {
164  m_client = value;
165  if (m_client != null)
166  clientTransform = m_client.transform;
167  else
168  clientTransform = null;
169  }
170  }
171 
172  public Vector2 lowerLeftUV
173  {
174  get { return m_lowerLeftUV; }
175  set
176  {
177  m_lowerLeftUV = value;
178  m_manager.UpdateUV(this);
179  }
180  }
181 
182  public Vector2 uvDimensions
183  {
184  get { return m_UVDimensions; }
185  set
186  {
187  m_UVDimensions = value;
188  m_manager.UpdateUV(this);
189  }
190  }
191 
192  public float width
193  {
194  get { return m_width; }
195  }
196 
197  public float height
198  {
199  get { return m_height; }
200  }
201 
202  public bool billboarded
203  {
204  get { return m_billboarded; }
205  set
206  {
207  m_billboarded = value;
208  }
209  }
210 
211  public bool hidden
212  {
213  get { return m_hidden___DoNotAccessExternally; }
214  set
215  {
216  // No need to do anything if we're
217  // already in this state:
218  if (value == m_hidden___DoNotAccessExternally)
219  return;
220 
221  if (value)
222  m_manager.HideSprite(this);
223  else
224  m_manager.ShowSprite(this);
225  }
226  }
227 
228  // Resets all sprite values to defaults for reuse:
229  public void Clear()
230  {
231  client = null;
232  billboarded = false;
233  hidden = false;
234  SetColor(Color.white);
235  offset = Vector3.zero;
236 
237  PauseAnim();
238  animations.Clear();
239  curAnim = null;
240  animCompleteDelegate = null;
241  }
242 
243  // Does the same as assigning the drawLayer value, except that
244  // SortDrawingOrder() is called automatically.
245  // The draw layer indicates the order in which this sprite should be
246  // rendered relative to other sprites. Higher values result in a later
247  // drawing order relative to sprites with lower values:
248  public void SetDrawLayer(int v)
249  {
250  drawLayer = v;
251  m_manager.SortDrawingOrder();
252  }
253 
254  // Sets the physical dimensions of the sprite in the XY plane:
255  public void SetSizeXY(float width, float height)
256  {
257  m_width = width;
258  m_height = height;
259  v1 = offset + new Vector3(-m_width / 2, m_height / 2, 0); // Upper-left
260  v2 = offset + new Vector3(-m_width / 2, -m_height / 2, 0); // Lower-left
261  v3 = offset + new Vector3(m_width / 2, -m_height / 2, 0); // Lower-right
262  v4 = offset + new Vector3(m_width / 2, m_height / 2, 0); // Upper-right
263 
264  Transform();
265  }
266 
267  // Sets the physical dimensions of the sprite in the XZ plane:
268  public void SetSizeXZ(float width, float height)
269  {
270  m_width = width;
271  m_height = height;
272  v1 = offset + new Vector3(-m_width / 2, 0, m_height / 2); // Upper-left
273  v2 = offset + new Vector3(-m_width / 2, 0, -m_height / 2); // Lower-left
274  v3 = offset + new Vector3(m_width / 2, 0, -m_height / 2); // Lower-right
275  v4 = offset + new Vector3(m_width / 2, 0, m_height / 2); // Upper-right
276 
277  Transform();
278  }
279 
280  // Sets the physical dimensions of the sprite in the YZ plane:
281  public void SetSizeYZ(float width, float height)
282  {
283  m_width = width;
284  m_height = height;
285  v1 = offset + new Vector3(0, m_height / 2, -m_width / 2); // Upper-left
286  v2 = offset + new Vector3(0, -m_height / 2, -m_width / 2); // Lower-left
287  v3 = offset + new Vector3(0, -m_height / 2, m_width / 2); // Lower-right
288  v4 = offset + new Vector3(0, m_height / 2, m_width / 2); // Upper-right
289 
290  Transform();
291  }
292 
293  // Sets the vertex and UV buffers
294  public void SetBuffers(Vector3[] v, Vector2[] uv)
295  {
296  meshVerts = v;
297  UVs = uv;
298  }
299 
300  // Applies the transform of the client GameObject and stores
301  // the results in the associated vertices of the overall mesh:
302  public void Transform()
303  {
304  meshVerts[mv1] = clientTransform.TransformPoint(v1);
305  meshVerts[mv2] = clientTransform.TransformPoint(v2);
306  meshVerts[mv3] = clientTransform.TransformPoint(v3);
307  meshVerts[mv4] = clientTransform.TransformPoint(v4);
308 
309  m_manager.UpdatePositions();
310  }
311 
312  // Applies the transform of the client GameObject and stores
313  // the results in the associated vertices of the overall mesh:
315  {
316  Vector3 pos = clientTransform.position;
317 
318  meshVerts[mv1] = pos + t.InverseTransformDirection(v1);
319  meshVerts[mv2] = pos + t.InverseTransformDirection(v2);
320  meshVerts[mv3] = pos + t.InverseTransformDirection(v3);
321  meshVerts[mv4] = pos + t.InverseTransformDirection(v4);
322 
323  m_manager.UpdatePositions();
324  }
325 
326  // Sets the specified color and automatically notifies the
327  // SpriteManager to update the colors:
328  public void SetColor(Color c)
329  {
330  color = c;
331  m_manager.UpdateColors(this);
332  }
333 
334  //-----------------------------------------------------------------
335  // Animation-related routines:
336  //-----------------------------------------------------------------
337 
338  // Sets the delegate to be called upon animation completion:
340  {
341  animCompleteDelegate = del;
342  }
343 
344  // Adds an animation to the sprite
345  public void AddAnimation(UVAnimation anim)
346  {
347  animations.Add(anim);
348  }
349 
350  // Steps to the next frame of sprite animation
351  public bool StepAnim(float time)
352  {
353  if (curAnim == null)
354  return false;
355 
356  timeSinceLastFrame += time;
357 
358  framesToAdvance = (int) (timeSinceLastFrame / timeBetweenAnimFrames);
359 
360  // If there's nothing to do, return:
361  if (framesToAdvance < 1)
362  return true;
363 
364  while(framesToAdvance > 0)
365  {
366  if (curAnim.GetNextFrame(ref m_lowerLeftUV))
367  --framesToAdvance;
368  else
369  {
370  // We reached the end of our animation
371  if (animCompleteDelegate != null)
373 
374  m_manager.UpdateUV(this);
375 
376  return false;
377  }
378  }
379 
380  m_manager.UpdateUV(this);
381  timeSinceLastFrame = 0;
382 
383  return true;
384  }
385 
386  // Starts playing the specified animation
387  // (Note: this doesn't resume from a pause,
388  // it completely restarts the animation. To
389  // unpause, use UnpauseAnim):
390  public void PlayAnim(UVAnimation anim)
391  {
392  // First stop any currently playing animation:
393  m_manager.StopAnimation(this);
394 
395  curAnim = anim;
396  curAnim.Reset();
397  timeBetweenAnimFrames = 1f / anim.framerate;
398  timeSinceLastFrame = timeBetweenAnimFrames;
399  StepAnim(0);
400 
401  m_manager.AnimateSprite(this);
402  }
403 
404  // Starts playing the specified animation:
405  public void PlayAnim(string name)
406  {
407  for (int i = 0; i < animations.Count; ++i)
408  {
409  if (((UVAnimation)animations[i]).name == name)
410  PlayAnim((UVAnimation)animations[i]);
411  }
412  }
413 
414  // Like PlayAnim but plays in reverse:
415  public void PlayAnimInReverse(UVAnimation anim)
416  {
417  // First stop any currently playing animation:
418  m_manager.StopAnimation(this);
419 
420  curAnim = anim;
421  curAnim.Reset();
422  curAnim.PlayInReverse();
423  timeBetweenAnimFrames = 1f / anim.framerate;
424  timeSinceLastFrame = timeBetweenAnimFrames;
425  StepAnim(0);
426 
427  m_manager.AnimateSprite(this);
428  }
429 
430  // Starts playing the specified animation in reverse:
431  public void PlayAnimInReverse(string name)
432  {
433  for (int i = 0; i < animations.Count; ++i)
434  {
435  if (((UVAnimation)animations[i]).name == name)
436  {
437  ((UVAnimation)animations[i]).PlayInReverse();
438  PlayAnimInReverse((UVAnimation)animations[i]);
439  }
440  }
441  }
442 
443  // Pauses the currently-playing animation:
444  public void PauseAnim()
445  {
446  m_manager.StopAnimation(this);
447  }
448 
449  // Unpauses the currently-playing animation:
450  public void UnpauseAnim()
451  {
452  if (curAnim == null) return;
453 
454  m_manager.AnimateSprite(this);
455  }
456 }
457 
458 
459 // Compares drawing layers of sprites
460 public class SpriteDrawLayerComparer : IComparer
461 {
462  static Sprite s1;
463  static Sprite s2;
464 
465  int IComparer.Compare(object a, object b)
466  {
467  s1 = (Sprite)a;
468  s2 = (Sprite)b;
469 
470  if (s1.drawLayer > s2.drawLayer)
471  return 1;
472  else if (s1.drawLayer < s2.drawLayer)
473  return -1;
474  else
475  return 0;
476  }
477 }
478 
float framerate
Vector2[] UVs
Definition: Sprite.cs:95
GameObject client
Definition: Sprite.cs:160
delegate void AnimCompleteDelegate()
void PlayAnim(string name)
Definition: Sprite.cs:405
float m_height
Definition: Sprite.cs:86
void UpdateColors(Sprite sprite)
int mv1
Definition: Sprite.cs:109
void Transform()
Definition: Sprite.cs:302
void AnimateSprite(Sprite s)
float height
Definition: Sprite.cs:198
UVAnimation curAnim
Definition: Sprite.cs:128
Vector2 lowerLeftUV
Definition: Sprite.cs:173
void SetAnimCompleteDelegate(AnimCompleteDelegate del)
Definition: Sprite.cs:339
void PlayAnimInReverse(UVAnimation anim)
Definition: Sprite.cs:415
bool hidden
Definition: Sprite.cs:212
int cv1
Definition: Sprite.cs:119
void PlayInReverse()
bool StepAnim(float time)
Definition: Sprite.cs:351
void SetSizeXZ(float width, float height)
Definition: Sprite.cs:268
int cv4
Definition: Sprite.cs:122
bool m_hidden___DoNotAccessExternally
Definition: Sprite.cs:92
Color color
Definition: Sprite.cs:99
int uv4
Definition: Sprite.cs:117
float timeSinceLastFrame
Definition: Sprite.cs:130
int uv3
Definition: Sprite.cs:116
float m_width
Definition: Sprite.cs:85
Vector3 v3
Definition: Sprite.cs:106
Vector2 m_lowerLeftUV
Definition: Sprite.cs:87
~Sprite()
Definition: Sprite.cs:134
void Clear()
Definition: Sprite.cs:229
int mv2
Definition: Sprite.cs:110
Vector3 v4
Definition: Sprite.cs:107
void StopAnimation(Sprite s)
void SortDrawingOrder()
void AddAnimation(UVAnimation anim)
Definition: Sprite.cs:345
int uv2
Definition: Sprite.cs:115
SpriteManager m_manager
Definition: Sprite.cs:90
bool billboarded
Definition: Sprite.cs:203
GameObject m_client
Definition: Sprite.cs:89
AnimCompleteDelegate animCompleteDelegate
Definition: Sprite.cs:129
void UpdatePositions()
int cv2
Definition: Sprite.cs:120
int index
Definition: Sprite.cs:101
int mv3
Definition: Sprite.cs:111
void SetDrawLayer(int v)
Definition: Sprite.cs:248
Definition: Sprite.cs:83
void ShowSprite(Sprite sprite)
void HideSprite(Sprite sprite)
void UnpauseAnim()
Definition: Sprite.cs:450
static Sprite s2
Definition: Sprite.cs:463
void SetColor(Color c)
Definition: Sprite.cs:328
Transform clientTransform
Definition: Sprite.cs:97
void SetBuffers(Vector3[] v, Vector2[] uv)
Definition: Sprite.cs:294
Sprite()
Definition: Sprite.cs:139
Vector3 offset
Definition: Sprite.cs:98
void SetSizeXY(float width, float height)
Definition: Sprite.cs:255
void PlayAnimInReverse(string name)
Definition: Sprite.cs:431
float width
Definition: Sprite.cs:193
bool m_billboarded
Definition: Sprite.cs:91
float timeBetweenAnimFrames
Definition: Sprite.cs:131
int framesToAdvance
Definition: Sprite.cs:132
void UpdateUV(Sprite sprite)
void PauseAnim()
Definition: Sprite.cs:444
int drawLayer
Definition: Sprite.cs:102
void SetSizeYZ(float width, float height)
Definition: Sprite.cs:281
static Sprite s1
Definition: Sprite.cs:462
Vector3 v2
Definition: Sprite.cs:105
int uv1
Definition: Sprite.cs:114
Vector2 uvDimensions
Definition: Sprite.cs:183
int cv3
Definition: Sprite.cs:121
Vector3 v1
Definition: Sprite.cs:104
void TransformBillboarded(Transform t)
Definition: Sprite.cs:314
void PlayAnim(UVAnimation anim)
Definition: Sprite.cs:390
ArrayList animations
Definition: Sprite.cs:127
int mv4
Definition: Sprite.cs:112
Vector3[] meshVerts
Definition: Sprite.cs:94
Vector2 m_UVDimensions
Definition: Sprite.cs:88
bool GetNextFrame(ref Vector2 uv)
SpriteManager manager
Definition: Sprite.cs:154