unity 武器拖尾效果实现的代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//内部类 用于存储每帧光效点信息
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 MyWeapon : 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;
    private 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();
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值