Unity小功能记录(二) ------ 使用LineRender绘制Bezier曲线(绘制虚线)

最近有个需求是需要画运行轨迹,虽然最后画出来了,可是我还要移动,用Bezier不太方便,不过也当个小功能记录下来吧

首先原理参考:https://blog.csdn.net/likendsl/article/details/7852658

上代码:

using UnityEngine;

[System.Serializable]
public class Bezier : System.Object
{

    public Vector3 p0;
    public Vector3 p1;
    public Vector3 p2;
    public Vector3 p3;
    public float ti = 0f;
    private Vector3 b0 = Vector3.zero;
    private Vector3 b1 = Vector3.zero;
    private Vector3 b2 = Vector3.zero;
    private Vector3 b3 = Vector3.zero;

    private float Ax;
    private float Ay;
    private float Az;
    private float Bx;
    private float By;
    private float Bz;
    private float Cx;
    private float Cy;
    private float Cz;
    
    public Bezier(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3)
    {
        this.p0 = v0;
        this.p1 = v1;
        this.p2 = v2;
        this.p3 = v3;
    }

    // 0.0 >= t <= 1.0
    public Vector3 GetPointAtTime(float t)
    {
        this.CheckConstant();
        float t2 = t * t;
        float t3 = t * t * t;
        float x = this.Ax * t3 + this.Bx * t2 + this.Cx * t + p0.x;
        float y = this.Ay * t3 + this.By * t2 + this.Cy * t + p0.y;
        float z = this.Az * t3 + this.Bz * t2 + this.Cz * t + p0.z;
        return new Vector3(x, y, z);
    }

    private void SetConstant()
    {
        this.Cx = 3f * ((this.p0.x + this.p1.x) - this.p0.x);
        this.Bx = 3f * ((this.p3.x + this.p2.x) - (this.p0.x + this.p1.x)) - this.Cx;
        this.Ax = this.p3.x - this.p0.x - this.Cx - this.Bx;
        this.Cy = 3f * ((this.p0.y + this.p1.y) - this.p0.y);
        this.By = 3f * ((this.p3.y + this.p2.y) - (this.p0.y + this.p1.y)) - this.Cy;
        this.Ay = this.p3.y - this.p0.y - this.Cy - this.By;
        this.Cz = 3f * ((this.p0.z + this.p1.z) - this.p0.z);
        this.Bz = 3f * ((this.p3.z + this.p2.z) - (this.p0.z + this.p1.z)) - this.Cz;
        this.Az = this.p3.z - this.p0.z - this.Cz - this.Bz;
    }

    // Check if p0, p1, p2 or p3 have changed
    private void CheckConstant()
    {
        if (this.p0 != this.b0 || this.p1 != this.b1 || this.p2 != this.b2 || this.p3 != this.b3)
        {
            this.SetConstant();
            this.b0 = this.p0;
            this.b1 = this.p1;
            this.b2 = this.p2;
            this.b3 = this.p3;
        }
    }
}

测试代码:

using UnityEngine;
using System.Collections;

public class CreateBezier : MonoBehaviour
{

    private Bezier bezier;
    public GameObject line;//曲线的对象
    private LineRenderer lineRenderer;//曲线对象的曲线组件

    public Transform p1;
    public Transform p2;
    public Transform p3;
    public Transform p4;

    public int pointAmount = 100;//值越大曲线越平滑

    void Start()
    {
        lineRenderer = line.GetComponent<LineRenderer>();
        lineRenderer.positionCount = pointAmount;
    }

    void Update()
    {
        float t = 1 / (float)pointAmount;
        bezier = new Bezier(p1.localPosition, p2.localPosition, p3.localPosition, p4.localPosition);
        for (int i = 1; i <= pointAmount; i++)
        {
            //参数的取值范围 0 - 1 返回曲线每一点的位置
            //为了精确这里使用i * 0.01 得到当前点的坐标
            Vector3 vec = bezier.GetPointAtTime((float)(i * t));
            //把每条线段绘制出来 完成贝塞尔曲线的绘制
            lineRenderer.SetPosition(i - 1, vec);
        }
    }
}

其实并没有看懂,如果有理解的朋友可以解释下,多谢啦

如果要绘制虚线呢,可以设置lineRender的材质,材质图片设置为虚线就OK啦

                                   

虚线图:


效果图:

                                    


还有种绘制方法,Mesh绘制,参考链接:http://www.cnblogs.com/JLZT1223/tag/unity%20mesh/

猜你喜欢

转载自blog.csdn.net/dengshunhao/article/details/80402444
今日推荐