SphericalBillboard.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class SphericalBillboard : RenderObject
  6. {
  7. public SphericalBillboard(VertexPool.VertexSegment segment)
  8. {
  9. this.UVChanged = (this.ColorChanged = false);
  10. this.Vertexsegment = segment;
  11. this.ResetSegment();
  12. }
  13. public void SetUVCoord(Vector2 lowerleft, Vector2 dimensions)
  14. {
  15. this.LowerLeftUV = lowerleft;
  16. this.UVDimensions = dimensions;
  17. XftTools.TopLeftUVToLowerLeft(ref this.LowerLeftUV, ref this.UVDimensions);
  18. this.UVChanged = true;
  19. }
  20. public void SetColor(Color c)
  21. {
  22. this.Color = c;
  23. this.ColorChanged = true;
  24. }
  25. public void SetPosition(Vector3 pos)
  26. {
  27. this.mCenterPos = pos;
  28. }
  29. public void ResetSegment()
  30. {
  31. VertexPool pool = this.Vertexsegment.Pool;
  32. int indexStart = this.Vertexsegment.IndexStart;
  33. int vertStart = this.Vertexsegment.VertStart;
  34. pool.Indices[indexStart] = vertStart;
  35. pool.Indices[indexStart + 1] = vertStart + 3;
  36. pool.Indices[indexStart + 2] = vertStart + 1;
  37. pool.Indices[indexStart + 3] = vertStart + 3;
  38. pool.Indices[indexStart + 4] = vertStart + 2;
  39. pool.Indices[indexStart + 5] = vertStart + 1;
  40. pool.Vertices[vertStart] = Vector3.zero;
  41. pool.Vertices[vertStart + 1] = Vector3.zero;
  42. pool.Vertices[vertStart + 2] = Vector3.zero;
  43. pool.Vertices[vertStart + 3] = Vector3.zero;
  44. pool.Colors[vertStart] = Color.white;
  45. pool.Colors[vertStart + 1] = Color.white;
  46. pool.Colors[vertStart + 2] = Color.white;
  47. pool.Colors[vertStart + 3] = Color.white;
  48. pool.UVs[vertStart] = Vector2.zero;
  49. pool.UVs[vertStart + 1] = Vector2.zero;
  50. pool.UVs[vertStart + 2] = Vector2.zero;
  51. pool.UVs[vertStart + 3] = Vector2.zero;
  52. pool.UVChanged = (pool.IndiceChanged = (pool.ColorChanged = (pool.VertChanged = true)));
  53. }
  54. public void UpdateUV()
  55. {
  56. VertexPool pool = this.Vertexsegment.Pool;
  57. int vertStart = this.Vertexsegment.VertStart;
  58. if (this.UVDimensions.y > 0f)
  59. {
  60. pool.UVs[vertStart] = this.LowerLeftUV + Vector2.up * this.UVDimensions.y;
  61. pool.UVs[vertStart + 1] = this.LowerLeftUV;
  62. pool.UVs[vertStart + 2] = this.LowerLeftUV + Vector2.right * this.UVDimensions.x;
  63. pool.UVs[vertStart + 3] = this.LowerLeftUV + this.UVDimensions;
  64. }
  65. else
  66. {
  67. pool.UVs[vertStart] = this.LowerLeftUV;
  68. pool.UVs[vertStart + 1] = this.LowerLeftUV + Vector2.up * this.UVDimensions.y;
  69. pool.UVs[vertStart + 2] = this.LowerLeftUV + this.UVDimensions;
  70. pool.UVs[vertStart + 3] = this.LowerLeftUV + Vector2.right * this.UVDimensions.x;
  71. }
  72. this.Vertexsegment.Pool.UVChanged = true;
  73. }
  74. public void UpdateColor()
  75. {
  76. VertexPool pool = this.Vertexsegment.Pool;
  77. int vertStart = this.Vertexsegment.VertStart;
  78. pool.Colors[vertStart] = this.Color;
  79. pool.Colors[vertStart + 1] = this.Color;
  80. pool.Colors[vertStart + 2] = this.Color;
  81. pool.Colors[vertStart + 3] = this.Color;
  82. this.Vertexsegment.Pool.ColorChanged = true;
  83. }
  84. public void UpdateCenterPos()
  85. {
  86. VertexPool pool = this.Vertexsegment.Pool;
  87. int vertStart = this.Vertexsegment.VertStart;
  88. pool.Vertices[vertStart] = this.mCenterPos;
  89. pool.Vertices[vertStart + 1] = this.mCenterPos;
  90. pool.Vertices[vertStart + 2] = this.mCenterPos;
  91. pool.Vertices[vertStart + 3] = this.mCenterPos;
  92. this.Vertexsegment.Pool.VertChanged = true;
  93. }
  94. public override void Update(float deltaTime)
  95. {
  96. this.SetColor(this.Node.Color);
  97. if (this.Node.Owner.UVAffectorEnable || this.Node.Owner.UVRotAffectorEnable || this.Node.Owner.UVScaleAffectorEnable)
  98. {
  99. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  100. }
  101. this.SetPosition(this.Node.CurWorldPos);
  102. if (this.UVChanged)
  103. {
  104. this.UpdateUV();
  105. }
  106. if (this.ColorChanged)
  107. {
  108. this.UpdateColor();
  109. }
  110. this.UVChanged = (this.ColorChanged = false);
  111. this.UpdateCenterPos();
  112. this.mRadius = this.Node.Scale.x * this.Node.Owner.SpriteWidth * this.Node.OriScaleX;
  113. VertexPool pool = this.Vertexsegment.Pool;
  114. int vertStart = this.Vertexsegment.VertStart;
  115. Vector2 one = Vector2.one;
  116. one.x = this.mRadius;
  117. one.y = ((float)this.Node.OriRotateAngle + this.Node.RotateAngle) * 3.14159274f / 180f;
  118. pool.UVs2[vertStart] = one;
  119. pool.UVs2[vertStart + 1] = one;
  120. pool.UVs2[vertStart + 2] = one;
  121. pool.UVs2[vertStart + 3] = one;
  122. this.Vertexsegment.Pool.UV2Changed = true;
  123. }
  124. public override void Initialize(EffectNode node)
  125. {
  126. base.Initialize(node);
  127. this.SetUVCoord(node.LowerLeftUV, node.UVDimensions);
  128. this.SetColor(this.Node.Color);
  129. if (this.Node.Owner.MyCamera.depthTextureMode == DepthTextureMode.None)
  130. {
  131. this.Node.Owner.MyCamera.depthTextureMode = DepthTextureMode.Depth;
  132. }
  133. }
  134. public override void Reset()
  135. {
  136. this.SetPosition(this.Node.Position);
  137. this.SetColor(Color.clear);
  138. this.mRadius = 0f;
  139. this.Update(0f);
  140. }
  141. protected Vector2 LowerLeftUV;
  142. protected Vector2 UVDimensions;
  143. protected Vector3 v1 = Vector3.zero;
  144. protected Vector3 v2 = Vector3.zero;
  145. protected Vector3 v3 = Vector3.zero;
  146. protected Vector3 v4 = Vector3.zero;
  147. protected VertexPool.VertexSegment Vertexsegment;
  148. public Color Color;
  149. protected bool UVChanged;
  150. protected bool ColorChanged;
  151. protected Vector3 mCenterPos = Vector3.zero;
  152. protected float mRadius;
  153. }
  154. }