Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lasers, hum.
- This is the way I've implemented them. Arrange it for your system.
- [code]using System;
- using System.Collections.Generic;
- using UnityEngine;
- public sealed class Trails {
- public readonly Material material;
- private static List<Trails> allTrails = new List<Trails>(128);
- List<Trail> trails = new List<Trail>();
- List<Trail> disabledTrails = new List<Trail>();
- Mesh mesh;
- public sealed class Trail {
- public int maxResetTimes;
- public Rect materialTextureRectangle;
- public bool Unused { get; private set; }
- public int Points { get { return pointCount; } }
- int pointsPooled;
- int pointCount;
- int resetTimes = -1;
- Points points, last;
- static int i;
- public Trail(Rect textureRect, int maxResetTimes) {
- this.maxResetTimes = maxResetTimes;
- this.materialTextureRectangle = textureRect;
- }
- public void Push(Vector3 position, Color32 tint, float pixelsWidth = 1) {
- if (pointsPooled > pointCount) {
- last.position = position;
- last.width = pixelsWidth * 0.01f;
- last.tint = tint;
- if (last.next != null)
- last = last.next;
- } else {
- last = new Points(position, pixelsWidth * 0.01f, tint, last);
- if (points == null)
- points = last;
- }
- pointCount++;
- }
- public void AddDataTo(List<Vector3> Vertices, List<Vector2> UVs, List<Color32> Colours, List<int> Indexes, Material mat) {
- if (pointCount >= 2) {
- Rect temp = materialTextureRectangle;
- if (temp == new Rect(0, 0, 0, 0)) {
- temp.width = mat.mainTexture.width;
- temp.height = mat.mainTexture.height;
- }
- temp.height /= (pointCount - 1);
- int verticesLength = pointCount * 2;
- Vector3[] vertices = new Vector3[verticesLength];
- Vector2[] uvs = new Vector2[verticesLength];
- Color32[] cols = new Color32[verticesLength];
- int[] indexs = new int[pointCount * 6 - 6];
- Points current = points;
- Vector3 currentPos, currentPolar;
- Vector3 d = current.next.position - current.position;
- float currentRot = Mathf.Atan2(d.y, d.x);
- int triangle = 0;
- int offset = Vertices.Count;
- Rect uv = temp.GetUVs(new Vector2(mat.mainTexture.width, mat.mainTexture.height));
- for (i = 0; i < verticesLength; i += 2) {
- currentPos = current.position;
- currentPolar = Helpers.Polar(currentRot, current.width);
- vertices[i] = currentPos + currentPolar;
- vertices[i + 1] = currentPos - currentPolar;
- cols[i] = current.tint;
- cols[i + 1] = current.tint;
- if (current.next != null) {
- d = current.next.position - currentPos;
- currentRot = Mathf.Atan2(d.y, d.x);
- current = current.next;
- }
- uvs[i] = new Vector2(uv.x + uv.width, uv.y);
- uvs[i + 1] = new Vector2(uv.x, uv.y);
- uv.y += uv.height;
- if (i + 2 >= verticesLength)
- continue;
- indexs[triangle] = offset + i;
- triangle++;
- indexs[triangle] = offset + i + 1;
- triangle++;
- indexs[triangle] = offset + i + 2;
- triangle++;
- indexs[triangle] = offset + i + 1;
- triangle++;
- indexs[triangle] = offset + i + 3;
- triangle++;
- indexs[triangle] = offset + i + 2;
- triangle++;
- }
- Vertices.AddRange(vertices);
- UVs.AddRange(uvs);
- Colours.AddRange(cols);
- Indexes.AddRange(indexs);
- }
- }
- public void Recreate(Rect textureRect, int maxResetTimes) {
- this.maxResetTimes = maxResetTimes;
- this.materialTextureRectangle = textureRect;
- }
- public bool Reset() {
- resetTimes++;
- if (points != null) {
- resetTimes = -1;
- last = points;
- pointsPooled = Mathf.Max(pointsPooled, pointCount);
- pointCount = 0;
- }
- if (resetTimes >= maxResetTimes)
- Unused = true;
- return Unused;
- }
- }
- sealed class Points {
- public Points next;
- public Vector3 position;
- public float width;
- public Color32 tint;
- public Points(Vector3 position, float width, Color32 tint, Points last) {
- this.position = position;
- this.width = width;
- this.tint = tint;
- if (last != null)
- last.next = this;
- }
- }
- GameObject temp;
- private Trails(Material mat) {
- material = mat;
- temp = new GameObject(material.name + "_Trail");
- MeshFilter m = temp.AddComponent<MeshFilter>();
- var render = temp.AddComponent<MeshRenderer>();
- render.receiveShadows = false;
- render.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
- render.useLightProbes = false;
- render.material = mat;
- mesh = new Mesh();
- mesh.MarkDynamic();
- m.mesh = mesh;
- }
- public static Trails GetTrailManager(Material mat) {
- for (int i = 0; i < allTrails.Count; i++) {
- if (allTrails[i].material == mat) {
- return allTrails[i];
- }
- }
- Trails ret = new MikeithoriaEngine.Trails(mat);
- allTrails.Add(ret);
- return ret;
- }
- public static void FlushAllTrails() {
- for (int i = 0; i < allTrails.Count; i++)
- allTrails[i].Flush();
- }
- void LateUpdate() {
- Flush();
- }
- public Trail Instantiate(Sprite sprite) {
- Trail t;
- if (disabledTrails.Count > 0) {
- t = disabledTrails[0];
- t.Recreate(sprite.rect, 4);
- disabledTrails.Remove(t);
- trails.Add(t);
- return t;
- }
- t = new Trail(sprite.rect, 4);
- trails.Add(t);
- return t;
- }
- public Trail Instantiate(Sprite sprite, int maxTimes) {
- Trail t;
- if (disabledTrails.Count > 0) {
- t = disabledTrails[0];
- t.Recreate(sprite.rect, maxTimes);
- disabledTrails.Remove(t);
- trails.Add(t);
- return t;
- }
- t = new Trail(sprite.rect, maxTimes);
- trails.Add(t);
- return t;
- }
- public void Flush() {
- mesh.Clear();
- List<Vector3> ve = new List<Vector3>(1024);
- List<Vector2> uvs = new List<Vector2>(1024);
- List<Color32> cols = new List<Color32>(1024);
- List<int> ints = new List<int>(1024);
- for (int i = 0; i < trails.Count; i++) {
- trails[i].AddDataTo(ve, uvs, cols, ints, material);
- if (trails[i].Reset()) {
- disabledTrails.Add(trails[i]);
- trails.RemoveAt(i);
- i--;
- }
- }
- mesh.vertices = ve.ToArray();
- mesh.uv = uvs.ToArray();
- mesh.colors32 = cols.ToArray();
- mesh.triangles = ints.ToArray();
- mesh.RecalculateBounds();
- }
- }[/code]
- Dunno if it's the best way tough, but it works :D.
Advertisement
Add Comment
Please, Sign In to add comment