Created
February 5, 2016 07:50
-
-
Save beinteractive/7b0b8a10219f035a67ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class CustomTrailRenderer : MonoBehaviour { | |
IEnumerator Start() { | |
var target = (Transform)null; | |
while (target == null) { | |
target = transform.parent; | |
yield return null; | |
} | |
var target_g = target.gameObject; | |
transform.parent = null; | |
transform.position = new Vector3(0f, 0f, transform.position.z); | |
transform.rotation = Quaternion.identity; | |
transform.localScale = Vector3.one; | |
var size = 0.6f; | |
var color = (Color)(new Color32(0, 88, 231, 255)); | |
var max = 40 * 2; | |
var step = 1.0f; | |
var mesh = GetComponent<MeshFilter>().mesh; | |
var last = target.position; | |
var dt = 0f; | |
List<Vector3> vertices = new List<Vector3>(); | |
List<Vector2> uv = new List<Vector2>(); | |
List<float> alphas = new List<float>(); | |
List<Color> colors = new List<Color>(); | |
List<int> triangles = new List<int>(); | |
while (true) { | |
if (target_g == null) { | |
Destroy(gameObject); | |
yield break; | |
} | |
var modified = false; | |
var current = target.position; | |
var speed = dt > 0f ? Vector2.Distance(last, current) / dt : 0f; | |
var alpha = Mathf.Min(1f, 0.05f + 0.95f * (speed / 100f)); | |
while (Vector2.Distance(last, current) >= step) { | |
var p = Vector3.Lerp(last, current, step / Vector2.Distance(last, current)); | |
var r = target.eulerAngles.z * Mathf.Deg2Rad; | |
var v = new Vector3(Mathf.Cos(r), Mathf.Sin(r), 0f) * size; | |
vertices.Add(p + v); | |
vertices.Add(p - v); | |
alphas.Add(alpha); | |
if (vertices.Count > max) { | |
vertices.RemoveRange(0, 2); | |
alphas.RemoveAt(0); | |
} else { | |
uv.Add(Vector2.zero); | |
uv.Add(Vector2.zero); | |
} | |
last = p; | |
dt = 0f; | |
modified = true; | |
} | |
if (modified) { | |
if (vertices.Count > 2) { | |
{ | |
colors.Clear(); | |
var num = alphas.Count; | |
var n = (float)num; | |
var m = (float)max / 2; | |
for (var i = 0; i < num; ++i) { | |
var a = alphas[i]; | |
var c = color; | |
c.a = a * (1f - (n - i) / m); | |
colors.Add(c); | |
colors.Add(c); | |
} | |
} | |
{ | |
triangles.Clear(); | |
var num = (vertices.Count - 2) / 2; | |
for (var i = 0; i < num; ++i) { | |
triangles.Add(i * 2); | |
triangles.Add(i * 2 + 1); | |
triangles.Add(i * 2 + 3); | |
triangles.Add(i * 2); | |
triangles.Add(i * 2 + 3); | |
triangles.Add(i * 2 + 2); | |
} | |
} | |
mesh.vertices = vertices.ToArray(); | |
mesh.uv = uv.ToArray(); | |
mesh.colors = colors.ToArray(); | |
mesh.triangles = triangles.ToArray(); | |
mesh.RecalculateNormals(); | |
mesh.RecalculateBounds(); | |
} | |
} | |
yield return null; | |
dt += Time.deltaTime; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment