Unity绘制贝塞尔曲线

在Unity中绘制贝塞尔曲线

贝塞尔曲线需要四个点,假设A,B,C,D。

首先设A点为曲线开始端点,B点为开始改变曲线的位置,C点为结束改变曲线的位置,D点为曲线终点。


第一步创建一个空游戏对象, 然后添加Line Renderer组件(Component ->Effects->LineRenderer)

第二步:创建一个脚本Bezier脚本如下

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;

    // Init function v0 = 1st point, v1 = handle of the 1st point , v2 = handle of the 2nd point, v3 = 2nd point

    // handle1 = v0 + v1

    // handle2 = v3 + v2

    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;
        }
    }
}


第三步:创建一个脚本 MyBezier代码如下

using UnityEngine;
using System.Collections;

public class MyBezier : MonoBehaviour {
        //贝塞尔曲线算法类
        public Bezier myBezier;
        //曲线的对象
        public GameObject Yellowline;
        //曲线对象的曲线组件
        private LineRenderer YellowlineRenderer;
        //拖动条用来控制贝塞尔曲线的两个点
        public float hSliderValue0;
        public float hSliderValue1;

        void Start()
        {
            //得到曲线组件
            YellowlineRenderer = Yellowline.GetComponent();
            //为了让曲线更加美观,设置曲线由100个点来组成
            YellowlineRenderer.SetVertexCount(100);
        }

        void OnGUI()
        {
            //拖动条得出 -5.0 - 5.0之间的一个数值
            hSliderValue0 = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue0, -5.0F, 5.0F);
            hSliderValue1 = GUI.HorizontalSlider(new Rect(25, 70, 100, 30), hSliderValue1, -5.0F, 5.0F);
        }

        void Update()
        {
            //在这里来计算贝塞尔曲线
            //四个参数 表示当前贝塞尔曲线上的4个点 第一个点和第四个点
            //我们是不需要移动的,中间的两个点是由拖动条来控制的。
            myBezier = new Bezier(new Vector3(-5f, 0f, 0f), new Vector3(hSliderValue1, hSliderValue0, 0f), new Vector3(hSliderValue1, hSliderValue0, 0f), new Vector3(5f, 0f, 0f));

            //循环100遍来绘制贝塞尔曲线每个线段
            for (int i = 1; i <= 100; i++)
            {
                //参数的取值范围 0 - 1 返回曲线没一点的位置
                //为了精确这里使用i * 0.01 得到当前点的坐标
                Vector3 vec = myBezier.GetPointAtTime((float)(i * 0.01));
                //把每条线段绘制出来 完成白塞尔曲线的绘制
                YellowlineRenderer.SetPosition(i - 1, vec);
            }
        }
}

第四步:将MyBeizer脚本拖拽到摄像机上。Bezier不用管

第五步:将添加的LineRender组件的对象拖拽给MyBezier中的

private LineRenderer YellowlineRenderer;对象


运行界面如下:


Unity绘制贝塞尔曲线_第1张图片








你可能感兴趣的:(Unity)