拖尾效果
using System.Collections.Generic; using UnityEngine; class Wp { public Vector3 point; public Vector3 upDir; public float time; public Wp() { } public Wp(Vector3 p, float t) { point = p; time = t; } } public class Tuowei : MonoBehaviour { private List<Wp> sections = new List<Wp>();//光效点列表 private Mesh mesh; public float time = 2.0f; public Color startColor = Color.white; public Color endColor = new Color(1, 1, 1, 0); private MeshRenderer meshRenderer; private Material trailMaterial; public float height = 2.0f; public bool isPlay = false; void Awake() { MeshFilter meshF = GetComponent(typeof(MeshFilter)) as MeshFilter; mesh = meshF.mesh; meshRenderer = GetComponent(typeof(MeshRenderer)) as MeshRenderer; trailMaterial = meshRenderer.material; } void Start() { } // 每帧进行显示更新 void FixedUpdate() { if (isPlay) { Itterate(Time.time); UpdateTrail(Time.time); } } public void weStart() { isPlay = true; } public void weStop() { isPlay = false; ClearTrail(); } public void Itterate(float itterateTime) { //记录拖尾点信息 Vector3 position = transform.position; float now = itterateTime; // 将当前信息添加进列表 if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > 0) { Wp section = new Wp(); section.point = position; section.upDir = transform.TransformDirection(Vector3.up);//存入世界方向 section.time = now; sections.Insert(0, section); } } public void UpdateTrail(float currentTime) { // ** call once a frame ** // 清空mesh显示 mesh.Clear(); // // 将超时点从列表移除 while (sections.Count > 0 && currentTime > sections[sections.Count - 1].time + time) { sections.RemoveAt(sections.Count - 1); } // 两个点才能形成一个光效 if (sections.Count < 2) return; //顶点 颜色 UV数组定义 Vector3[] vertices = new Vector3[sections.Count * 2]; Color[] colors = new Color[sections.Count * 2]; Vector2[] uv = new Vector2[sections.Count * 2]; //获取列表第一个点 Wp currentSection = sections[0]; // // 用矩阵代替transform 性能较高 Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix; // 设置 顶点 颜色 UV信息 for (var i = 0; i < sections.Count; i++) { currentSection = sections[i]; float u = 0.0f; if (i != 0) u = Mathf.Clamp01((currentTime - currentSection.time) / time); // // 获取朝向 Vector3 upDir = currentSection.upDir; // 根据记录点 创建两个顶点 vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point); vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height); uv[i * 2 + 0] = new Vector2(u, 0); uv[i * 2 + 1] = new Vector2(u, 1); // 计算插值颜色 Color interpolatedColor = Color.Lerp(startColor, endColor, u); colors[i * 2 + 0] = interpolatedColor; colors[i * 2 + 1] = interpolatedColor; } // 创建三角形信息 // 1---3---5 // |\ |\ | // | \ | \ | // | \| \| // 0---2---4 int[] triangles = new int[(sections.Count - 1) * 2 * 3]; for (int i = 0; i < triangles.Length / 6; i++) { triangles[i * 6 + 0] = i * 2; triangles[i * 6 + 1] = i * 2 + 1; triangles[i * 6 + 2] = i * 2 + 2; triangles[i * 6 + 3] = i * 2 + 2; triangles[i * 6 + 4] = i * 2 + 1; triangles[i * 6 + 5] = i * 2 + 3; } // 对mesh赋值 mesh.vertices = vertices; mesh.colors = colors; mesh.uv = uv; mesh.triangles = triangles; } public void ClearTrail() { //清空显示对象 if (mesh != null) { mesh.Clear(); sections.Clear(); } } }
转自:http://www.lai18.com/content/1398005.html