VertexPool.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //----------------------------------------------
  2. // Xffect Editor
  3. // Copyright © 2012- Shallway Studio
  4. // http://shallway.net
  5. //----------------------------------------------
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace XftWeapon {
  10. public class VertexPool
  11. {
  12. public class VertexSegment
  13. {
  14. public int VertStart;
  15. public int IndexStart;
  16. public int VertCount;
  17. public int IndexCount;
  18. public VertexPool Pool;
  19. public VertexSegment(int start, int count, int istart, int icount, VertexPool pool)
  20. {
  21. VertStart = start;
  22. VertCount = count;
  23. IndexCount = icount;
  24. IndexStart = istart;
  25. Pool = pool;
  26. }
  27. public void ClearIndices()
  28. {
  29. for (int i = IndexStart; i < IndexStart + IndexCount; i++)
  30. {
  31. Pool.Indices[i] = 0;
  32. }
  33. Pool.IndiceChanged = true;
  34. }
  35. }
  36. public Vector3[] Vertices;
  37. public int[] Indices;
  38. public Vector2[] UVs;
  39. public Color[] Colors;
  40. public bool IndiceChanged;
  41. public bool ColorChanged;
  42. public bool UVChanged;
  43. public bool VertChanged;
  44. public bool UV2Changed;
  45. protected int VertexTotal;
  46. protected int VertexUsed;
  47. protected int IndexTotal = 0;
  48. protected int IndexUsed = 0;
  49. public bool FirstUpdate = true;
  50. protected bool VertCountChanged;
  51. public const int BlockSize = 108;
  52. public float BoundsScheduleTime = 1f;
  53. public float ElapsedTime = 0f;
  54. protected XWeaponTrail _owner;
  55. protected MeshFilter _meshFilter;
  56. protected Mesh _mesh2d;
  57. protected Material _material;
  58. public Mesh MyMesh {
  59. get {
  60. if(_owner.UseWith2D || _owner.UseWithSRP){
  61. if (_meshFilter == null || _meshFilter.gameObject == null) {
  62. return null;
  63. }
  64. return _meshFilter.sharedMesh;
  65. }
  66. else{
  67. return _mesh2d;
  68. }
  69. }
  70. }
  71. public void RecalculateBounds()
  72. {
  73. MyMesh.RecalculateBounds();
  74. }
  75. public void SetMeshObjectActive(bool flag) {
  76. if (_meshFilter == null) {
  77. return;
  78. }
  79. _meshFilter.gameObject.SetActive(flag);
  80. }
  81. void CreateMeshObj(XWeaponTrail owner, Material material) {
  82. GameObject obj = new GameObject("_XWeaponTrailMesh:" + "|material:" + material.name);
  83. obj.layer = owner.gameObject.layer;
  84. obj.AddComponent<MeshFilter>();
  85. obj.AddComponent<MeshRenderer>();
  86. obj.transform.position = Vector3.zero;
  87. obj.transform.rotation = Quaternion.identity;
  88. MeshRenderer Meshrenderer;
  89. _meshFilter = (MeshFilter)obj.GetComponent(typeof(MeshFilter));
  90. Meshrenderer = (MeshRenderer)obj.GetComponent(typeof(MeshRenderer));
  91. #if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8
  92. Meshrenderer.castShadows = false;
  93. #else
  94. Meshrenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  95. #endif
  96. Meshrenderer.receiveShadows = false;
  97. Meshrenderer.GetComponent<Renderer>().sharedMaterial = material;
  98. Meshrenderer.sortingLayerName = _owner.SortingLayerName;
  99. Meshrenderer.sortingOrder = _owner.SortingOrder;
  100. _meshFilter.sharedMesh = new Mesh();
  101. }
  102. public void Destroy() {
  103. if(_owner.UseWith2D || _owner.UseWithSRP){
  104. if (_meshFilter != null) {
  105. GameObject.Destroy(_meshFilter.gameObject);
  106. }
  107. }
  108. else{
  109. Mesh.DestroyImmediate(_mesh2d);
  110. }
  111. }
  112. public VertexPool(Material material, XWeaponTrail owner)
  113. {
  114. VertexTotal = VertexUsed = 0;
  115. VertCountChanged = false;
  116. _owner = owner;
  117. if (owner.UseWith2D || owner.UseWithSRP) {
  118. CreateMeshObj(owner, material);
  119. }
  120. else {
  121. _mesh2d = new Mesh();
  122. }
  123. _material = material;
  124. InitArrays();
  125. IndiceChanged = ColorChanged = UVChanged = UV2Changed = VertChanged = true;
  126. }
  127. public VertexSegment GetVertices(int vcount, int icount)
  128. {
  129. int vertNeed = 0;
  130. int indexNeed = 0;
  131. if (VertexUsed + vcount >= VertexTotal)
  132. {
  133. vertNeed = (vcount / BlockSize + 1) * BlockSize;
  134. }
  135. if (IndexUsed + icount >= IndexTotal)
  136. {
  137. indexNeed = (icount / BlockSize + 1) * BlockSize;
  138. }
  139. VertexUsed += vcount;
  140. IndexUsed += icount;
  141. if (vertNeed != 0 || indexNeed != 0)
  142. {
  143. EnlargeArrays(vertNeed, indexNeed);
  144. VertexTotal += vertNeed;
  145. IndexTotal += indexNeed;
  146. }
  147. VertexSegment ret = new VertexSegment(VertexUsed - vcount, vcount, IndexUsed - icount, icount, this);
  148. return ret;
  149. }
  150. protected void InitArrays()
  151. {
  152. Vertices = new Vector3[4];
  153. UVs = new Vector2[4];
  154. Colors = new Color[4];
  155. Indices = new int[6];
  156. VertexTotal = 4;
  157. IndexTotal = 6;
  158. }
  159. public void EnlargeArrays(int count, int icount)
  160. {
  161. Vector3[] tempVerts = Vertices;
  162. Vertices = new Vector3[Vertices.Length + count];
  163. tempVerts.CopyTo(Vertices, 0);
  164. Vector2[] tempUVs = UVs;
  165. UVs = new Vector2[UVs.Length + count];
  166. tempUVs.CopyTo(UVs, 0);
  167. Color[] tempColors = Colors;
  168. Colors = new Color[Colors.Length + count];
  169. tempColors.CopyTo(Colors, 0);
  170. int[] tempTris = Indices;
  171. Indices = new int[Indices.Length + icount];
  172. tempTris.CopyTo(Indices, 0);
  173. VertCountChanged = true;
  174. IndiceChanged = true;
  175. ColorChanged = true;
  176. UVChanged = true;
  177. VertChanged = true;
  178. UV2Changed = true;
  179. }
  180. public void LateUpdate()
  181. {
  182. if (MyMesh == null){
  183. return;
  184. }
  185. if (VertCountChanged)
  186. {
  187. MyMesh.Clear();
  188. }
  189. // we assume the vertices are always changed.
  190. MyMesh.vertices = Vertices;
  191. if (UVChanged)
  192. {
  193. MyMesh.uv = UVs;
  194. }
  195. if (ColorChanged)
  196. {
  197. MyMesh.colors = Colors;
  198. }
  199. if (IndiceChanged)
  200. {
  201. MyMesh.triangles = Indices;
  202. }
  203. ElapsedTime += Time.deltaTime;
  204. if (ElapsedTime > BoundsScheduleTime || FirstUpdate)
  205. {
  206. RecalculateBounds();
  207. ElapsedTime = 0f;
  208. }
  209. if (ElapsedTime > BoundsScheduleTime)
  210. FirstUpdate = false;
  211. VertCountChanged = false;
  212. IndiceChanged = false;
  213. ColorChanged = false;
  214. UVChanged = false;
  215. UV2Changed = false;
  216. VertChanged = false;
  217. if (_owner.UseWith2D || _owner.UseWithSRP) {
  218. }
  219. else {
  220. Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
  221. for (int i = 0; i < _material.passCount; i++) {
  222. _material.SetPass(i);
  223. }
  224. if (!_owner.UseWithSRP){
  225. Graphics.DrawMeshNow(MyMesh, matrix);
  226. }
  227. else{
  228. Graphics.DrawMesh(MyMesh, Matrix4x4.identity, _material, _owner.gameObject.layer, null, 0, null, false, false);
  229. }
  230. }
  231. }
  232. }
  233. }