参考链接 : http://catlikecoding.com/unity/tutorials/curves-and-splines/
demo下载链接 : https://github.com/Eagle-Lai/Line 如有疑问,欢迎评论。
使用方法:创建一个空物体,添加 BezierSpline 然后可以编辑路径。
创建一个对象,添加 SplineDecorator,作为沿着这条路径移动的物体
效果如下图 :
Bezier.cs
using UnityEngine;
public static class Bezier
{
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * p0 +
2f * oneMinusT * t * p1 +
t * t * p2;
}
public static Vector3 GetFirstDerivative(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
return
2f * (1f - t) * (p1 - p0) +
2f * t * (p2 - p1);
}
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
t = Mathf.Clamp01(t);
float OneMinusT = 1f - t;
return
OneMinusT * OneMinusT * OneMinusT * p0 +
3f * OneMinusT * OneMinusT * t * p1 +
3f * OneMinusT * t * t * p2 +
t * t * t * p3;
}
public static Vector3 GetFirstDerivative(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
3f * oneMinusT * oneMinusT * (p1 - p0) +
6f * oneMinusT * t * (p2 - p1) +
3f * t * t * (p3 - p2);
}
}
BezierSpline.cs
using UnityEngine;
using System;
public class BezierSpline : MonoBehaviour
{
[SerializeField]
public Vector3[] points;
[SerializeField]
private BezierControlPointMode[] modes;
[SerializeField]
private bool loop;
public bool Loop
{
get
{
return loop;
}
set
{
loop = value;
if (value == true)
{
modes[modes.Length - 1] = modes[0];
SetControlPoint(0, points[0]);
}
}
}
public int ControlPointCount
{
get
{
return points.Length;
}
}
public Vector3 GetControlPoint(int index)
{
return points[index];
}
public void SetControlPoint(int index, Vector3 point)
{
if (index % 3 == 0)
{
Vector3 delta = point - points[index];
if (loop)
{
if (index == 0)
{
points[1] += delta;
points[points.Length - 2] += delta;
points[points.Length - 1] = point;
}
else if (index == points.Length - 1)
{
points[0] = point;
points[1] += delta;
points[index - 1] += delta;
}
else
{
points[index - 1] += delta;
points[index + 1] += delta;
}
}
else
{
if (index > 0)
{
points[index - 1] += delta;
}
if (index + 1 < points.Length)
{
points[index + 1] += delta;
}
}
}
points[index] = point;
EnforceMode(index);
}
public BezierControlPointMode GetControlPointMode(int index)
{
return modes[(index + 1) / 3];
}
public void SetControlPointMode(int index, BezierControlPointMode mode)
{
int modeIndex = (index + 1) / 3;
modes[modeIndex] = mode;
if (loop)
{
if (modeIndex == 0)
{
modes[modes.Length - 1] = mode;
}
else if (modeIndex == modes.Length - 1)
{
modes[0] = mode;
}
}
EnforceMode(index);
}
private void EnforceMode(int index)
{
int modeIndex = (index + 1) / 3;
BezierControlPointMode mode = modes[modeIndex];
if (mode == BezierControlPointMode.Free || !loop && (modeIndex == 0 || modeIndex == modes.Length - 1))
{
return;
}
int middleIndex = modeIndex * 3;
int fixedIndex, enforcedIndex;
if (index <= middleIndex)
{
fixedIndex = middleIndex - 1;
if (fixedIndex < 0)
{
fixedIndex = points.Length - 2;
}
enforcedIndex = middleIndex + 1;
if (enforcedIndex >= points.Length)
{
enforcedIndex = 1;
}
}
else
{
fixedIndex = middleIndex + 1;
if (fixedIndex >= points.Length)
{
fixedIndex = 1;
}
enforcedIndex = middleIndex - 1;
if (enforcedIndex < 0)
{
enforcedIndex = points.Length - 2;
}
}
Vector3 middle = points[middleIndex];
Vector3 enforcedTangent = middle - points[fixedIndex];
if (mode == BezierControlPointMode.Aligned)
{
enforcedTangent = enforcedTangent.normalized * Vector3.Distance(middle, points[enforcedIndex]);
}
points[enforcedIndex] = middle + enforcedTangent;
}
public int CurveCount
{
get
{
return (points.Length - 1) / 3;
}
}
public Vector3 GetPoint(float t)
{
int i;
if (t >= 1f)
{
t = 1f;
i = points.Length - 4;
}
else
{
t = Mathf.Clamp01(t) * CurveCount;
i = (int)t;
t -= i;
i *= 3;
}
return transform.TransformPoint(Bezier.GetPoint(points[i], points[i + 1], points[i + 2], points[i + 3], t));
}
public Vector3 GetVelocity(float t)
{
int i;
if (t >= 1f)
{
t = 1f;
i = points.Length - 4;
}
else
{
t = Mathf.Clamp01(t) * CurveCount;
i = (int)t;
t -= i;
i *= 3;
}
return transform.TransformPoint(Bezier.GetFirstDerivative(points[i], points[i + 1], points[i + 2], points[i + 3], t)) - transform.position;
}
public Vector3 GetDirection(float t)
{
return GetVelocity(t).normalized;
}
public void AddCurve()
{
Vector3 point = points[points.Length - 1];
Array.Resize(ref points, points.Length + 3);
point.x += 1f;
points[points.Length - 3] = point;
point.x += 1f;
points[points.Length - 2] = point;
point.x += 1f;
points[points.Length - 1] = point;
Array.Resize(ref modes, modes.Length + 1);
modes[modes.Length - 1] = modes[modes.Length - 2];
EnforceMode(points.Length - 4);
if (loop)
{
points[points.Length - 1] = points[0];
modes[modes.Length - 1] = modes[0];
EnforceMode(0);
}
}
public void Reset()
{
points = new Vector3[] {
new Vector3(1f, 0f, 0f),
new Vector3(2f, 0f, 0f),
new Vector3(3f, 0f, 0f),
new Vector3(4f, 0f, 0f)
};
modes = new BezierControlPointMode[] {
BezierControlPointMode.Free,
BezierControlPointMode.Free
};
}
}
public enum BezierControlPointMode
{
Free,
Aligned,
Mirrored
}
SplineDecorator.cs
using UnityEngine;
using System.Collections;
public class SplineDecorator : MonoBehaviour {
public BezierSpline spline;
public int frequency = 10;
public bool lookForward;
public Transform[] items;
private void Awake()
{
if (frequency <= 0 || items == null || items.Length == 0) return;
float stepSize = frequency * items.Length;
if(spline.Loop || stepSize == 1)
{
stepSize = 1f / stepSize;
}
else
{
stepSize = 1f / (stepSize - 1);
}
for (int p = 0, f = 0; f < frequency; f++)
{
for (int i = 0; i < items.Length ; i++, p++)
{
Transform item = Instantiate(items[i]) as Transform;
Vector3 position = spline.GetPoint(p * stepSize);
item.transform.localPosition = position;
if(lookForward)
{
item.transform.LookAt(position + spline.GetDirection(p * stepSize));
}
item.transform.parent = transform;
}
}
}
}
测试用的脚本 :
using UnityEngine;
using System.Collections;
public enum SplineWalkerMode
{
Once,
Loop,
PingPong
}
public class SplineWalker : MonoBehaviour {
public BezierSpline spline;
public float duration = 5;
public bool lookForward;
public SplineWalkerMode mode;
private float progress;
private bool goingForward = true;
void Update()
{
if(goingForward)
{
progress += Time.deltaTime / duration;
if(progress > 1f)
{
if(mode == SplineWalkerMode.Once)
{
progress = 1f;
}
else if(mode == SplineWalkerMode.Loop)
{
progress = 0f;
}
else
{
progress = 2f - progress;
goingForward = false;
}
}
}
else
{
progress -= Time.deltaTime / duration;
if(progress < 0f)
{
progress = -progress;
goingForward = true;
}
}
Vector3 position = spline.GetPoint(progress);
transform.localPosition = position;
if(lookForward)
{
transform.LookAt(position + spline.GetDirection(progress));
}
}
}
放置在Editor文件夹下的脚本:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(BezierSpline))]
public class BezierSplineInspector : Editor
{
private const int stepsPerCurve = 10;
private const float directionScale = 0.5f;
private const float handleSize = 0.04f;
private const float pickSize = 0.06f;
private static Color[] modeColors = {
Color.white,
Color.yellow,
Color.cyan
};
private BezierSpline spline;
private Transform handleTransform;
private Quaternion handleRotation;
private int selectedIndex = -1;
public override void OnInspectorGUI()
{
spline = target as BezierSpline;
EditorGUI.BeginChangeCheck();
bool loop = EditorGUILayout.Toggle("Loop", spline.Loop);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(spline, "Toggle Loop");
EditorUtility.SetDirty(spline);
spline.Loop = loop;
}
if (selectedIndex >= 0 && selectedIndex < spline.ControlPointCount)
{
DrawSelectedPointInspector();
}
if (GUILayout.Button("Add Curve"))
{
Undo.RecordObject(spline, "Add Curve");
spline.AddCurve();
EditorUtility.SetDirty(spline);
}
}
private void DrawSelectedPointInspector()
{
GUILayout.Label("Selected Point");
EditorGUI.BeginChangeCheck();
Vector3 point = EditorGUILayout.Vector3Field("Position", spline.GetControlPoint(selectedIndex));
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(spline, "Move Point");
EditorUtility.SetDirty(spline);
spline.SetControlPoint(selectedIndex, point);
}
EditorGUI.BeginChangeCheck();
BezierControlPointMode mode = (BezierControlPointMode)EditorGUILayout.EnumPopup("Mode", spline.GetControlPointMode(selectedIndex));
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(spline, "Change Point Mode");
spline.SetControlPointMode(selectedIndex, mode);
EditorUtility.SetDirty(spline);
}
}
private void OnSceneGUI()
{
spline = target as BezierSpline;
handleTransform = spline.transform;
handleRotation = Tools.pivotRotation == PivotRotation.Local ?
handleTransform.rotation : Quaternion.identity;
Vector3 p0 = ShowPoint(0);
for (int i = 1; i < spline.ControlPointCount; i += 3)
{
Vector3 p1 = ShowPoint(i);
Vector3 p2 = ShowPoint(i + 1);
Vector3 p3 = ShowPoint(i + 2);
Handles.color = Color.gray;
Handles.DrawLine(p0, p1);
Handles.DrawLine(p2, p3);
Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f);
p0 = p3;
}
ShowDirections();
}
private void ShowDirections()
{
Handles.color = Color.green;
Vector3 point = spline.GetPoint(0f);
Handles.DrawLine(point, point + spline.GetDirection(0f) * directionScale);
int steps = stepsPerCurve * spline.CurveCount;
for (int i = 1; i <= steps; i++)
{
point = spline.GetPoint(i / (float)steps);
Handles.DrawLine(point, point + spline.GetDirection(i / (float)steps) * directionScale);
}
}
private Vector3 ShowPoint(int index)
{
Vector3 point = handleTransform.TransformPoint(spline.GetControlPoint(index));
float size = HandleUtility.GetHandleSize(point);
if (index == 0)
{
size *= 2f;
}
Handles.color = modeColors[(int)spline.GetControlPointMode(index)];
if (Handles.Button(point, handleRotation, size * handleSize, size * pickSize, Handles.DotCap))
{
selectedIndex = index;
Repaint();
}
if (selectedIndex == index)
{
EditorGUI.BeginChangeCheck();
point = Handles.DoPositionHandle(point, handleRotation);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(spline, "Move Point");
EditorUtility.SetDirty(spline);
spline.SetControlPoint(index, handleTransform.InverseTransformPoint(point));
}
}
return point;
}
}