貝塞爾曲線

Brand123147發表於2020-12-14

掛上指令碼,放入gameObject即可看到效果

/*
 * t = [0, 1]
 * 一階貝塞爾曲線公式:a = (1 - t) * P0
 *                   b = t * P1
 *                   B = a + b
 * 二階貝塞爾公式:a = (1 - t) * (1 - t) * P0
 *               b = 2 * t * (1 - t)P1
 *               c = t * t * P2
 *               B = a + b + c
 * 三階貝塞爾曲線:    a = (1 - t) * (1 - t) * (1 - t) * P0
 *                   b = 3 * t * (1 - t) * (1 - t) * P1
 *                   c = 3 * t * t * (1 - t) * P2
 *                   d = t * t * t * P3
 *                   B = a + b + c + d
 */

using System;
using System.Collections;
using UnityEngine;

public class Bezier : MonoBehaviour
{
    private IEnumerator coTmp;

    private float moveSpeed;

    [Range(0, 1)] public float tt = 0f;

    public Transform obj;

    public Transform p0;

    public Transform p1;

    public Transform p2;

    // Start is called before the first frame update
    void Start()
    {
        coTmp = NumberADD(tt, 1, 0.01f, 0.01f, (f) => { tt = f; });
        StartCoroutine(coTmp);
    }

    // Update is called once per frame
    void Update()
    {
        obj.position = Bezier2(tt, p0.position, p1.position, p2.position);
    }
    // 數值遞增
    public IEnumerator NumberADD(  float curNum, float targetNum,float le , float time, Action<float> func)
    {
        for (; curNum <= targetNum; curNum += le)
        {
            yield return new WaitForSeconds(time);
            func(curNum);
        }
        coTmp = NumberADD(curNum, targetNum, le, time, func);
        StopCoroutine(coTmp);
    }

    /// <summary>
    /// 二階貝塞爾曲線
    /// </summary>
    /// <param name="t">[0, 1]</param>
    /// <param name="P0">起點</param>
    /// <param name="P1">中間點,越遠彎曲越大</param>
    /// <param name="P2">終點</param>
    /// <returns></returns>
    Vector3 Bezier2(float t, Vector3 P0, Vector3 P1, Vector3 P2)
    {
        Vector3 a = (1 - t) * (1 - t) * P0;
        Vector3 b = 2 * t * (1 - t) * P1;
        Vector3 c = t * t * P2;
        Vector3 pos = a + b + c;
        return pos;
    }
}

相關文章