Cone.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class Cone : RenderObject
  6. {
  7. public Cone(VertexPool.VertexSegment segment, Vector2 size, int numseg, float angle, Vector3 dir, int uvStretch, float maxFps)
  8. {
  9. this.Vertexsegment = segment;
  10. this.Size = size;
  11. this.Direction = dir;
  12. this.SetColor(Color.white);
  13. this.NumSegment = numseg;
  14. this.SpreadAngle = angle;
  15. this.OriSpreadAngle = angle;
  16. this.InitVerts();
  17. }
  18. public override void ApplyShaderParam(float x, float y)
  19. {
  20. Vector2 one = Vector2.one;
  21. one.x = x;
  22. one.y = y;
  23. VertexPool pool = this.Vertexsegment.Pool;
  24. int vertStart = this.Vertexsegment.VertStart;
  25. for (int i = 0; i < this.NumVerts; i++)
  26. {
  27. pool.UVs2[vertStart + i] = one;
  28. }
  29. this.Vertexsegment.Pool.UV2Changed = true;
  30. }
  31. public override void Initialize(EffectNode node)
  32. {
  33. base.Initialize(node);
  34. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  35. this.SetColor(this.Node.Color);
  36. this.SetRotation((float)this.Node.OriRotateAngle);
  37. }
  38. public override void Reset()
  39. {
  40. this.SetRotation((float)this.Node.OriRotateAngle);
  41. this.SetColor(Color.clear);
  42. this.SetPosition(this.Node.Position);
  43. this.ResetAngle();
  44. this.MyUpdate(true, 0f);
  45. }
  46. public override void Update(float deltaTime)
  47. {
  48. this.SetScale(this.Node.Scale.x * this.Node.OriScaleX, this.Node.Scale.y * this.Node.OriScaleY);
  49. this.SetColor(this.Node.Color);
  50. if (this.Node.Owner.UVAffectorEnable || this.Node.Owner.UVRotAffectorEnable || this.Node.Owner.UVScaleAffectorEnable)
  51. {
  52. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  53. }
  54. this.SetRotation((float)this.Node.OriRotateAngle + this.Node.RotateAngle);
  55. this.SetPosition(this.Node.CurWorldPos);
  56. this.MyUpdate(false, deltaTime);
  57. }
  58. public void SetUVCoord(Vector2 topleft, Vector2 dimensions)
  59. {
  60. this.LowerLeftUV = topleft;
  61. this.UVDimensions = dimensions;
  62. XftTools.TopLeftUVToLowerLeft(ref this.LowerLeftUV, ref this.UVDimensions);
  63. this.LowerLeftUV.y = this.LowerLeftUV.y - dimensions.y;
  64. this.UVDimensions.y = -this.UVDimensions.y;
  65. this.UVChanged = true;
  66. }
  67. public void SetColor(Color c)
  68. {
  69. this.Color = c;
  70. this.ColorChanged = true;
  71. }
  72. public void SetPosition(Vector3 pos)
  73. {
  74. this.Position = pos;
  75. }
  76. public void SetScale(float width, float height)
  77. {
  78. this.Scale.x = width;
  79. this.Scale.y = height;
  80. }
  81. public void SetRotation(float angle)
  82. {
  83. this.OriRotAngle = angle;
  84. }
  85. public void ResetAngle()
  86. {
  87. this.SpreadAngle = this.OriSpreadAngle;
  88. }
  89. protected void UpdateRotAngle(float deltaTime)
  90. {
  91. if (!this.UseDeltaAngle)
  92. {
  93. return;
  94. }
  95. this.SpreadAngle = this.CurveAngle.Evaluate(this.Node.GetElapsedTime());
  96. for (int i = this.NumVerts / 2; i < this.NumVerts; i++)
  97. {
  98. this.Verts[i] = this.Verts[i - this.NumVerts / 2] + Vector3.up * this.Size.y;
  99. this.Verts[i] = Vector3.RotateTowards(this.Verts[i], this.Verts[i - this.NumVerts / 2], this.SpreadAngle * 0.0174532924f, 0f);
  100. }
  101. }
  102. public void UpdateVerts()
  103. {
  104. Vector3 point = Vector3.forward * this.Size.x;
  105. for (int i = 0; i < this.NumVerts / 2; i++)
  106. {
  107. this.Verts[i] = Quaternion.Euler(0f, this.OriRotAngle + this.SegmentAngle * (float)i, 0f) * point;
  108. }
  109. for (int j = this.NumVerts / 2; j < this.NumVerts; j++)
  110. {
  111. this.Verts[j] = this.Verts[j - this.NumVerts / 2] + Vector3.up * this.Size.y;
  112. this.Verts[j] = Vector3.RotateTowards(this.Verts[j], this.Verts[j - this.NumVerts / 2], this.SpreadAngle * 0.0174532924f, 0f);
  113. }
  114. }
  115. public void InitVerts()
  116. {
  117. this.NumVerts = (this.NumSegment + 1) * 2;
  118. this.SegmentAngle = 360f / (float)this.NumSegment;
  119. this.Verts = new Vector3[this.NumVerts];
  120. this.VertsTemp = new Vector3[this.NumVerts];
  121. this.UpdateVerts();
  122. VertexPool pool = this.Vertexsegment.Pool;
  123. int indexStart = this.Vertexsegment.IndexStart;
  124. int vertStart = this.Vertexsegment.VertStart;
  125. for (int i = 0; i < this.NumSegment; i++)
  126. {
  127. int num = indexStart + i * 6;
  128. int num2 = vertStart + i;
  129. pool.Indices[num] = num2 + this.NumSegment + 1;
  130. pool.Indices[num + 1] = num2 + this.NumSegment + 2;
  131. pool.Indices[num + 2] = num2;
  132. pool.Indices[num + 3] = num2 + this.NumSegment + 2;
  133. pool.Indices[num + 4] = num2 + 1;
  134. pool.Indices[num + 5] = num2;
  135. pool.Vertices[num2 + this.NumSegment + 1] = Vector3.zero;
  136. pool.Vertices[num2 + this.NumSegment + 2] = Vector3.zero;
  137. pool.Vertices[num2] = Vector3.zero;
  138. pool.Vertices[num2 + 1] = Vector3.zero;
  139. }
  140. }
  141. public void UpdateUV()
  142. {
  143. VertexPool pool = this.Vertexsegment.Pool;
  144. int vertStart = this.Vertexsegment.VertStart;
  145. float num = this.UVDimensions.x / (float)this.NumSegment;
  146. for (int i = 0; i < this.NumSegment + 1; i++)
  147. {
  148. pool.UVs[vertStart + i] = this.LowerLeftUV;
  149. Vector2[] uvs = pool.UVs;
  150. int num2 = vertStart + i;
  151. uvs[num2].x = uvs[num2].x + (float)i * num;
  152. }
  153. for (int j = this.NumSegment + 1; j < this.NumVerts; j++)
  154. {
  155. pool.UVs[vertStart + j] = this.LowerLeftUV + Vector2.up * this.UVDimensions.y;
  156. Vector2[] uvs2 = pool.UVs;
  157. int num3 = vertStart + j;
  158. uvs2[num3].x = uvs2[num3].x + (float)(j - this.NumSegment - 1) * num;
  159. }
  160. this.Vertexsegment.Pool.UVChanged = true;
  161. }
  162. public void UpdateColor()
  163. {
  164. VertexPool pool = this.Vertexsegment.Pool;
  165. int vertStart = this.Vertexsegment.VertStart;
  166. for (int i = 0; i < this.NumVerts; i++)
  167. {
  168. pool.Colors[vertStart + i] = this.Color;
  169. }
  170. this.Vertexsegment.Pool.ColorChanged = true;
  171. }
  172. public void Transform()
  173. {
  174. if (this.Node.Owner.RotAffectorEnable || this.OriRotAngle != 0f)
  175. {
  176. this.UpdateVerts();
  177. }
  178. Quaternion rotation;
  179. if (this.Node.Owner.AlwaysSyncRotation)
  180. {
  181. rotation = Quaternion.FromToRotation(Vector3.up, this.Node.Owner.transform.rotation * this.Direction);
  182. }
  183. else
  184. {
  185. rotation = Quaternion.FromToRotation(Vector3.up, this.Direction);
  186. }
  187. for (int i = 0; i < this.NumSegment + 1; i++)
  188. {
  189. this.VertsTemp[i] = this.Verts[i] * this.Scale.x;
  190. this.VertsTemp[i] = rotation * this.VertsTemp[i];
  191. this.VertsTemp[i] = this.VertsTemp[i] + this.Position;
  192. }
  193. for (int j = this.NumSegment + 1; j < this.NumVerts; j++)
  194. {
  195. this.VertsTemp[j] = this.Verts[j] * this.Scale.x;
  196. this.VertsTemp[j].y = this.Verts[j].y * this.Scale.y;
  197. this.VertsTemp[j] = rotation * this.VertsTemp[j];
  198. this.VertsTemp[j] = this.VertsTemp[j] + this.Position;
  199. }
  200. VertexPool pool = this.Vertexsegment.Pool;
  201. int vertStart = this.Vertexsegment.VertStart;
  202. for (int k = 0; k < this.NumVerts; k++)
  203. {
  204. pool.Vertices[vertStart + k] = this.VertsTemp[k];
  205. }
  206. }
  207. public void MyUpdate(bool force, float deltaTime)
  208. {
  209. this.ElapsedTime += deltaTime;
  210. if (this.ElapsedTime > base.Fps || force)
  211. {
  212. this.UpdateRotAngle(deltaTime);
  213. this.Transform();
  214. if (this.UVChanged)
  215. {
  216. this.UpdateUV();
  217. }
  218. if (this.ColorChanged)
  219. {
  220. this.UpdateColor();
  221. }
  222. this.UVChanged = (this.ColorChanged = false);
  223. if (!force)
  224. {
  225. this.ElapsedTime -= base.Fps;
  226. }
  227. }
  228. }
  229. public Vector2 Size;
  230. public Vector3 Direction;
  231. public int NumSegment = 4;
  232. public float SpreadAngle;
  233. public float OriSpreadAngle;
  234. public float OriRotAngle = 45f;
  235. public bool UseDeltaAngle;
  236. public AnimationCurve CurveAngle;
  237. protected int NumVerts;
  238. protected float SegmentAngle;
  239. protected VertexPool.VertexSegment Vertexsegment;
  240. protected Vector3[] Verts;
  241. protected Vector3[] VertsTemp;
  242. protected float ElapsedTime;
  243. protected bool UVChanged = true;
  244. protected bool ColorChanged = true;
  245. protected float OriUVX;
  246. public Vector3 Position = Vector3.zero;
  247. public Color Color;
  248. public Vector2 Scale;
  249. protected Vector2 LowerLeftUV = Vector2.zero;
  250. protected Vector2 UVDimensions = Vector2.one;
  251. }
  252. }