Guest User

h

a guest
Jun 5th, 2015
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. Lasers, hum.
  2.  
  3. This is the way I've implemented them. Arrange it for your system.
  4.  
  5. [code]using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8.  
  9. public sealed class Trails {
  10. public readonly Material material;
  11. private static List<Trails> allTrails = new List<Trails>(128);
  12. List<Trail> trails = new List<Trail>();
  13. List<Trail> disabledTrails = new List<Trail>();
  14. Mesh mesh;
  15.  
  16. public sealed class Trail {
  17. public int maxResetTimes;
  18. public Rect materialTextureRectangle;
  19. public bool Unused { get; private set; }
  20.  
  21. public int Points { get { return pointCount; } }
  22.  
  23. int pointsPooled;
  24. int pointCount;
  25. int resetTimes = -1;
  26. Points points, last;
  27.  
  28. static int i;
  29.  
  30. public Trail(Rect textureRect, int maxResetTimes) {
  31. this.maxResetTimes = maxResetTimes;
  32. this.materialTextureRectangle = textureRect;
  33. }
  34.  
  35. public void Push(Vector3 position, Color32 tint, float pixelsWidth = 1) {
  36. if (pointsPooled > pointCount) {
  37. last.position = position;
  38. last.width = pixelsWidth * 0.01f;
  39. last.tint = tint;
  40. if (last.next != null)
  41. last = last.next;
  42. } else {
  43. last = new Points(position, pixelsWidth * 0.01f, tint, last);
  44. if (points == null)
  45. points = last;
  46. }
  47. pointCount++;
  48. }
  49.  
  50. public void AddDataTo(List<Vector3> Vertices, List<Vector2> UVs, List<Color32> Colours, List<int> Indexes, Material mat) {
  51. if (pointCount >= 2) {
  52. Rect temp = materialTextureRectangle;
  53. if (temp == new Rect(0, 0, 0, 0)) {
  54. temp.width = mat.mainTexture.width;
  55. temp.height = mat.mainTexture.height;
  56. }
  57.  
  58. temp.height /= (pointCount - 1);
  59.  
  60. int verticesLength = pointCount * 2;
  61. Vector3[] vertices = new Vector3[verticesLength];
  62. Vector2[] uvs = new Vector2[verticesLength];
  63. Color32[] cols = new Color32[verticesLength];
  64. int[] indexs = new int[pointCount * 6 - 6];
  65.  
  66. Points current = points;
  67. Vector3 currentPos, currentPolar;
  68. Vector3 d = current.next.position - current.position;
  69. float currentRot = Mathf.Atan2(d.y, d.x);
  70. int triangle = 0;
  71. int offset = Vertices.Count;
  72. Rect uv = temp.GetUVs(new Vector2(mat.mainTexture.width, mat.mainTexture.height));
  73. for (i = 0; i < verticesLength; i += 2) {
  74. currentPos = current.position;
  75. currentPolar = Helpers.Polar(currentRot, current.width);
  76.  
  77. vertices[i] = currentPos + currentPolar;
  78. vertices[i + 1] = currentPos - currentPolar;
  79.  
  80. cols[i] = current.tint;
  81. cols[i + 1] = current.tint;
  82.  
  83. if (current.next != null) {
  84. d = current.next.position - currentPos;
  85. currentRot = Mathf.Atan2(d.y, d.x);
  86. current = current.next;
  87. }
  88.  
  89. uvs[i] = new Vector2(uv.x + uv.width, uv.y);
  90. uvs[i + 1] = new Vector2(uv.x, uv.y);
  91.  
  92. uv.y += uv.height;
  93.  
  94. if (i + 2 >= verticesLength)
  95. continue;
  96.  
  97. indexs[triangle] = offset + i;
  98. triangle++;
  99.  
  100. indexs[triangle] = offset + i + 1;
  101. triangle++;
  102.  
  103. indexs[triangle] = offset + i + 2;
  104. triangle++;
  105.  
  106.  
  107. indexs[triangle] = offset + i + 1;
  108. triangle++;
  109.  
  110. indexs[triangle] = offset + i + 3;
  111. triangle++;
  112.  
  113. indexs[triangle] = offset + i + 2;
  114. triangle++;
  115. }
  116.  
  117. Vertices.AddRange(vertices);
  118. UVs.AddRange(uvs);
  119. Colours.AddRange(cols);
  120. Indexes.AddRange(indexs);
  121. }
  122. }
  123.  
  124. public void Recreate(Rect textureRect, int maxResetTimes) {
  125. this.maxResetTimes = maxResetTimes;
  126. this.materialTextureRectangle = textureRect;
  127. }
  128. public bool Reset() {
  129. resetTimes++;
  130.  
  131. if (points != null) {
  132. resetTimes = -1;
  133. last = points;
  134. pointsPooled = Mathf.Max(pointsPooled, pointCount);
  135. pointCount = 0;
  136. }
  137.  
  138. if (resetTimes >= maxResetTimes)
  139. Unused = true;
  140.  
  141. return Unused;
  142. }
  143. }
  144.  
  145. sealed class Points {
  146. public Points next;
  147. public Vector3 position;
  148. public float width;
  149. public Color32 tint;
  150.  
  151. public Points(Vector3 position, float width, Color32 tint, Points last) {
  152. this.position = position;
  153. this.width = width;
  154. this.tint = tint;
  155. if (last != null)
  156. last.next = this;
  157. }
  158. }
  159.  
  160. GameObject temp;
  161.  
  162. private Trails(Material mat) {
  163. material = mat;
  164. temp = new GameObject(material.name + "_Trail");
  165.  
  166. MeshFilter m = temp.AddComponent<MeshFilter>();
  167. var render = temp.AddComponent<MeshRenderer>();
  168.  
  169. render.receiveShadows = false;
  170. render.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  171. render.useLightProbes = false;
  172. render.material = mat;
  173.  
  174. mesh = new Mesh();
  175. mesh.MarkDynamic();
  176. m.mesh = mesh;
  177. }
  178.  
  179. public static Trails GetTrailManager(Material mat) {
  180. for (int i = 0; i < allTrails.Count; i++) {
  181. if (allTrails[i].material == mat) {
  182. return allTrails[i];
  183. }
  184. }
  185.  
  186. Trails ret = new MikeithoriaEngine.Trails(mat);
  187. allTrails.Add(ret);
  188.  
  189. return ret;
  190. }
  191. public static void FlushAllTrails() {
  192. for (int i = 0; i < allTrails.Count; i++)
  193. allTrails[i].Flush();
  194. }
  195.  
  196. void LateUpdate() {
  197. Flush();
  198. }
  199.  
  200. public Trail Instantiate(Sprite sprite) {
  201. Trail t;
  202. if (disabledTrails.Count > 0) {
  203. t = disabledTrails[0];
  204. t.Recreate(sprite.rect, 4);
  205. disabledTrails.Remove(t);
  206. trails.Add(t);
  207.  
  208. return t;
  209. }
  210.  
  211. t = new Trail(sprite.rect, 4);
  212. trails.Add(t);
  213.  
  214. return t;
  215. }
  216.  
  217. public Trail Instantiate(Sprite sprite, int maxTimes) {
  218. Trail t;
  219. if (disabledTrails.Count > 0) {
  220. t = disabledTrails[0];
  221. t.Recreate(sprite.rect, maxTimes);
  222. disabledTrails.Remove(t);
  223. trails.Add(t);
  224.  
  225. return t;
  226. }
  227.  
  228. t = new Trail(sprite.rect, maxTimes);
  229. trails.Add(t);
  230.  
  231. return t;
  232. }
  233.  
  234. public void Flush() {
  235. mesh.Clear();
  236.  
  237. List<Vector3> ve = new List<Vector3>(1024);
  238. List<Vector2> uvs = new List<Vector2>(1024);
  239. List<Color32> cols = new List<Color32>(1024);
  240. List<int> ints = new List<int>(1024);
  241.  
  242. for (int i = 0; i < trails.Count; i++) {
  243. trails[i].AddDataTo(ve, uvs, cols, ints, material);
  244. if (trails[i].Reset()) {
  245. disabledTrails.Add(trails[i]);
  246. trails.RemoveAt(i);
  247. i--;
  248. }
  249. }
  250.  
  251. mesh.vertices = ve.ToArray();
  252. mesh.uv = uvs.ToArray();
  253. mesh.colors32 = cols.ToArray();
  254. mesh.triangles = ints.ToArray();
  255. mesh.RecalculateBounds();
  256. }
  257. }[/code]
  258.  
  259. Dunno if it's the best way tough, but it works :D.
Advertisement
Add Comment
Please, Sign In to add comment