using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class BezierFunctions
{
///
/// 用于计算参考点的位置
/// 我们想像一个立方体,开始点与结束点分别位于这个立方体的两个对角,那么剩下的六个点即为各个参考点
///
public static Vector3 GetReferencePoint(Vector3 startPoint, Vector3 endPoint, ReferencePointLocation location)
{
switch (location)
{
case ReferencePointLocation.Near: return new Vector3(endPoint.x, endPoint.y, startPoint.z);
case ReferencePointLocation.Far: return new Vector3(startPoint.x, startPoint.y, endPoint.z);
case ReferencePointLocation.RightTop: return new Vector3(endPoint.x, startPoint.y, startPoint.z);
case ReferencePointLocation.RightBottom: return new Vector3(endPoint.x, endPoint.y, startPoint.z);
case ReferencePointLocation.LeftTop: return new Vector3(startPoint.x, startPoint.y, endPoint.z);
case ReferencePointLocation.LeftBottom: return new Vector3(startPoint.x, endPoint.y, endPoint.z);
}
return new Vector3();
}
///
/// 计算贝塞尔曲线上的点,任意参考点
///
/// 取值区间为0-1,它代表取值的位置
/// 开始点
/// 结束点
/// 控制点
public static Vector3 GetBezierPoint(Vector3 startPoint, Vector3 endPoint, Vector3 vertice, float t)
{
//需要注意,当你的所谓的曲线是一条直线,即输入参数vertice为一个0向量,那么该方法会失效,你需要在计算参考点时解决这个问题
float u = 1 - t;
float tt = t * t;
float uu = u * u;
return uu * startPoint + 2 * u * t * endPoint + tt * vertice;
}
///
/// 计算贝塞尔曲线上的点,特殊参考点
///
/// 取值区间为0-1,它代表取值的位置
/// 开始点
/// 结束点
/// 控制点
///
public static Vector3 GetBezierPoint(Vector3 startPoint, Vector3 endPoint, ReferencePointLocation location, float t)
{
var vertice = GetReferencePoint(startPoint, endPoint, location);
float u = 1 - t;
float tt = t * t;
float uu = u * u;
return uu * startPoint + 2 * u * t * vertice + tt * endPoint;
}
}