public static Vector3[] ToPoints(this CatmullRomSpline bezierPath, float stepAngle = 2, float step = 0.01f, float maxSegmentLength = Single.PositiveInfinity) { var previous = bezierPath.Evaluate(0); var points = new List <Vector3>(); points.Add(previous); var previousDirection = bezierPath.Evaluate(0.01f) - previous; Vector3 previousPoint = previous; for (float t = step; t < 1.0f; t += step) { var point = bezierPath.Evaluate(t); var currentDirection = point - previousPoint; if (Vector3.Angle(currentDirection, previousDirection) > stepAngle || Vector3.Distance(point, previous) > maxSegmentLength) { points.Add(point); previous = point; previousDirection = currentDirection; } previousPoint = point; } points.Add(bezierPath.Evaluate(1)); return(points.ToArray()); }
private unsafe void OnEnable() { // Editor mode if (positionsProperty == null) { Awake(); } var vector3s = new Vector3[] { new Vector3(0, 0, 0), new Vector3(-1, 0, 1f), new Vector3(1, 0, 2f), new Vector3(0, 0, 3f), Target != null ? Target.localPosition : new Vector3(0, 0, 5), }; if (Points != null && Points.Length > 2) { vector3s = Points.Select(t => t.localPosition).ToArray(); } var spline = new CatmullRomSpline(vector3s); var points = spline.ToPoints(100); var positionsBuffer = new ComputeBuffer(points.Length, sizeof(Vector3), ComputeBufferType.Structured); positionsBuffer.SetData(points); positionsProperty.Value = positionsBuffer; positionsLengthProperty.Value = positionsBuffer.count; ApplyProperties(); OnValidate(); }
public static Vector3[] ToPoints(this CatmullRomSpline bezierPath, int pointCount) { if (pointCount < 2) { pointCount = 2; } var step = 1.0f / (pointCount - 1); var previous = bezierPath.Evaluate(0); var points = new List <Vector3>(); points.Add(previous); for (float t = step; t < 1.0f; t += step) { var point = bezierPath.Evaluate(t); points.Add(point); } points.Add(bezierPath.Evaluate(1)); return(points.ToArray()); }