VertexPool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class VertexPool
  6. {
  7. public VertexPool(Mesh mesh, Material material)
  8. {
  9. this.VertexTotal = (this.VertexUsed = 0);
  10. this.VertCountChanged = false;
  11. this.Mesh = mesh;
  12. this.Material = material;
  13. this.InitArrays();
  14. this.IndiceChanged = (this.ColorChanged = (this.UVChanged = (this.UV2Changed = (this.VertChanged = true))));
  15. }
  16. public void RecalculateBounds()
  17. {
  18. this.Mesh.RecalculateBounds();
  19. }
  20. public CustomMesh AddCustomMesh(Mesh mesh, Vector3 dir, float maxFps)
  21. {
  22. VertexPool.VertexSegment vertices = this.GetVertices(mesh.vertices.Length, mesh.triangles.Length);
  23. return new CustomMesh(vertices, mesh, dir, maxFps);
  24. }
  25. public Cone AddCone(Vector2 size, int numSegment, float angle, Vector3 dir, int uvStretch, float maxFps, bool usedelta, AnimationCurve deltaAngle)
  26. {
  27. VertexPool.VertexSegment vertices = this.GetVertices((numSegment + 1) * 2, numSegment * 6);
  28. return new Cone(vertices, size, numSegment, angle, dir, uvStretch, maxFps)
  29. {
  30. UseDeltaAngle = usedelta,
  31. CurveAngle = deltaAngle
  32. };
  33. }
  34. public XftSprite AddSprite(float width, float height, STYPE type, ORIPOINT ori, float maxFps, bool simple)
  35. {
  36. VertexPool.VertexSegment vertices = this.GetVertices(4, 6);
  37. return new XftSprite(vertices, width, height, type, ori, maxFps, simple);
  38. }
  39. public RibbonTrail AddRibbonTrail(bool useFaceObj, Transform faceobj, float width, int maxelemnt, float len, Vector3 pos, float maxFps)
  40. {
  41. VertexPool.VertexSegment vertices = this.GetVertices(maxelemnt * 2, (maxelemnt - 1) * 6);
  42. return new RibbonTrail(vertices, useFaceObj, faceobj, width, maxelemnt, len, pos, maxFps);
  43. }
  44. public VertexPool.VertexSegment GetRopeVertexSeg(int maxcount)
  45. {
  46. return this.GetVertices(maxcount * 2, (maxcount - 1) * 6);
  47. }
  48. public Rope AddRope()
  49. {
  50. return new Rope();
  51. }
  52. public SphericalBillboard AddSphericalBillboard()
  53. {
  54. VertexPool.VertexSegment vertices = this.GetVertices(4, 6);
  55. return new SphericalBillboard(vertices);
  56. }
  57. public Material GetMaterial()
  58. {
  59. return this.Material;
  60. }
  61. public VertexPool.VertexSegment GetVertices(int vcount, int icount)
  62. {
  63. int num = 0;
  64. int num2 = 0;
  65. if (this.VertexUsed + vcount >= this.VertexTotal)
  66. {
  67. num = (vcount / 108 + 1) * 108;
  68. }
  69. if (this.IndexUsed + icount >= this.IndexTotal)
  70. {
  71. num2 = (icount / 108 + 1) * 108;
  72. }
  73. this.VertexUsed += vcount;
  74. this.IndexUsed += icount;
  75. if (num != 0 || num2 != 0)
  76. {
  77. this.EnlargeArrays(num, num2);
  78. this.VertexTotal += num;
  79. this.IndexTotal += num2;
  80. }
  81. return new VertexPool.VertexSegment(this.VertexUsed - vcount, vcount, this.IndexUsed - icount, icount, this);
  82. }
  83. private void InitDefaultShaderParam(Vector2[] uv2)
  84. {
  85. for (int i = 0; i < uv2.Length; i++)
  86. {
  87. uv2[i].x = 1f;
  88. uv2[i].y = 0f;
  89. }
  90. }
  91. protected void InitArrays()
  92. {
  93. this.Vertices = new Vector3[4];
  94. this.UVs = new Vector2[4];
  95. this.UVs2 = new Vector2[4];
  96. this.Colors = new Color[4];
  97. this.Indices = new int[6];
  98. this.VertexTotal = 4;
  99. this.IndexTotal = 6;
  100. this.InitDefaultShaderParam(this.UVs2);
  101. }
  102. public void EnlargeArrays(int count, int icount)
  103. {
  104. Vector3[] vertices = this.Vertices;
  105. this.Vertices = new Vector3[this.Vertices.Length + count];
  106. vertices.CopyTo(this.Vertices, 0);
  107. Vector2[] uvs = this.UVs;
  108. this.UVs = new Vector2[this.UVs.Length + count];
  109. uvs.CopyTo(this.UVs, 0);
  110. Vector2[] uvs2 = this.UVs2;
  111. this.UVs2 = new Vector2[this.UVs2.Length + count];
  112. uvs2.CopyTo(this.UVs2, 0);
  113. this.InitDefaultShaderParam(this.UVs2);
  114. Color[] colors = this.Colors;
  115. this.Colors = new Color[this.Colors.Length + count];
  116. colors.CopyTo(this.Colors, 0);
  117. int[] indices = this.Indices;
  118. this.Indices = new int[this.Indices.Length + icount];
  119. indices.CopyTo(this.Indices, 0);
  120. this.VertCountChanged = true;
  121. this.IndiceChanged = true;
  122. this.ColorChanged = true;
  123. this.UVChanged = true;
  124. this.VertChanged = true;
  125. this.UV2Changed = true;
  126. }
  127. public void LateUpdate()
  128. {
  129. if (this.VertCountChanged)
  130. {
  131. this.Mesh.Clear();
  132. }
  133. this.Mesh.vertices = this.Vertices;
  134. if (this.UVChanged)
  135. {
  136. this.Mesh.uv = this.UVs;
  137. }
  138. if (this.UV2Changed)
  139. {
  140. this.Mesh.uv2 = this.UVs2;
  141. }
  142. if (this.ColorChanged)
  143. {
  144. this.Mesh.colors = this.Colors;
  145. }
  146. if (this.IndiceChanged)
  147. {
  148. this.Mesh.triangles = this.Indices;
  149. }
  150. this.ElapsedTime += Time.deltaTime;
  151. if (this.ElapsedTime > this.BoundsScheduleTime || this.FirstUpdate)
  152. {
  153. this.RecalculateBounds();
  154. this.ElapsedTime = 0f;
  155. }
  156. if (this.ElapsedTime > this.BoundsScheduleTime)
  157. {
  158. this.FirstUpdate = false;
  159. }
  160. this.VertCountChanged = false;
  161. this.IndiceChanged = false;
  162. this.ColorChanged = false;
  163. this.UVChanged = false;
  164. this.UV2Changed = false;
  165. this.VertChanged = false;
  166. }
  167. public Vector3[] Vertices;
  168. public int[] Indices;
  169. public Vector2[] UVs;
  170. public Color[] Colors;
  171. public Vector2[] UVs2;
  172. public bool IndiceChanged;
  173. public bool ColorChanged;
  174. public bool UVChanged;
  175. public bool VertChanged;
  176. public bool UV2Changed;
  177. public Mesh Mesh;
  178. public Material Material;
  179. protected int VertexTotal;
  180. protected int VertexUsed;
  181. protected int IndexTotal;
  182. protected int IndexUsed;
  183. public bool FirstUpdate = true;
  184. protected bool VertCountChanged;
  185. public const int BlockSize = 108;
  186. public float BoundsScheduleTime = 1f;
  187. public float ElapsedTime;
  188. public class VertexSegment
  189. {
  190. public VertexSegment(int start, int count, int istart, int icount, VertexPool pool)
  191. {
  192. this.VertStart = start;
  193. this.VertCount = count;
  194. this.IndexCount = icount;
  195. this.IndexStart = istart;
  196. this.Pool = pool;
  197. }
  198. public void ClearIndices()
  199. {
  200. for (int i = this.IndexStart; i < this.IndexStart + this.IndexCount; i++)
  201. {
  202. this.Pool.Indices[i] = 0;
  203. }
  204. this.Pool.IndiceChanged = true;
  205. }
  206. public int VertStart;
  207. public int IndexStart;
  208. public int VertCount;
  209. public int IndexCount;
  210. public VertexPool Pool;
  211. }
  212. }
  213. }