public static Spline ToSpline(this IEditablePath path)
        {
            var count = path.pointCount * 3;

            if (path.isOpenEnded)
            {
                count -= 2;
            }

            var spline = new Spline()
            {
                isOpenEnded = path.isOpenEnded,
                points      = new Vector3[count]
            };

            for (var i = 0; i < path.pointCount; ++i)
            {
                var point = path.GetPoint(i);

                spline.points[i * 3] = point.position;

                if (i * 3 + 1 < count)
                {
                    var nextIndex = EditablePathUtility.Mod(i + 1, path.pointCount);

                    spline.points[i * 3 + 1] = path.CalculateRightTangent(i);
                    spline.points[i * 3 + 2] = path.CalculateLeftTangent(nextIndex);
                }
            }

            return(spline);
        }
 private int PrevIndex(int index)
 {
     return(EditablePathUtility.Mod(index - 1, editablePath.pointCount));
 }
 private int NextIndex(int index)
 {
     return(EditablePathUtility.Mod(index + 1, editablePath.pointCount));
 }
 public static ControlPoint GetNextPoint(this IEditablePath path, int index)
 {
     return(path.GetPoint(EditablePathUtility.Mod(index + 1, path.pointCount)));
 }
 private int PrevIndex(int index)
 {
     return(EditablePathUtility.Mod(index - 1, GetPointCount()));
 }
 private int NextIndex(int index)
 {
     return(EditablePathUtility.Mod(index + 1, GetPointCount()));
 }
Exemple #7
0
        ControlPoint[] IShape.ToControlPoints()
        {
            if (points == null)
            {
                throw new NullReferenceException("Points array is null");
            }

            if (!points.IsSpline(isOpenEnded))
            {
                throw new Exception("The provided control point array can't conform a Spline.");
            }

            var controlPoints = new List <ControlPoint>();
            var leftTangent   = Vector3.zero;
            var rightTangent  = Vector3.zero;
            var pointCount    = points.Length;

            for (var i = 0; i < pointCount; i += 3)
            {
                if (i == 0)
                {
                    if (isOpenEnded)
                    {
                        leftTangent = points[0];
                    }
                    else
                    {
                        leftTangent = points[EditablePathUtility.Mod(-1, pointCount)];
                    }
                }

                if (i == pointCount - 1 && isOpenEnded)
                {
                    rightTangent = points[i];
                }
                else
                {
                    rightTangent = points[i + 1];
                }


                controlPoints.Add(
                    new ControlPoint()
                {
                    position     = points[i],
                    leftTangent  = leftTangent,
                    rightTangent = rightTangent,
                    tangentMode  = TangentMode.Broken
                });

                if (i == pointCount - 1 && isOpenEnded)
                {
                    leftTangent = Vector3.zero;
                }
                else
                {
                    leftTangent = points[i + 2];
                }
            }

            pointCount = controlPoints.Count;

            for (var i = 0; i < pointCount; ++i)
            {
                var prevIndex        = EditablePathUtility.Mod(i - 1, pointCount);
                var nextIndex        = EditablePathUtility.Mod(i + 1, pointCount);
                var controlPoint     = controlPoints[i];
                var prevControlPoint = controlPoints[prevIndex];
                var nextControlPoint = controlPoints[nextIndex];

                var liniarLeftPosition  = (prevControlPoint.position - controlPoint.position) / 3f;
                var isLeftTangentLinear = (controlPoint.localLeftTangent - liniarLeftPosition).sqrMagnitude < 0.001f;

                if (isLeftTangentLinear)
                {
                    controlPoint.localLeftTangent = Vector3.zero;
                }

                var liniarRightPosition  = (nextControlPoint.position - controlPoint.position) / 3f;
                var isRightTangentLinear = (controlPoint.localRightTangent - liniarRightPosition).sqrMagnitude < 0.001f;

                if (isRightTangentLinear)
                {
                    controlPoint.localRightTangent = Vector3.zero;
                }

                var tangentDotProduct = Vector3.Dot(controlPoint.localLeftTangent.normalized, controlPoint.localRightTangent.normalized);
                var isContinous       = tangentDotProduct < 0f && (tangentDotProduct + 1) * (tangentDotProduct + 1) < 0.001f;

                if (isLeftTangentLinear && isRightTangentLinear)
                {
                    controlPoint.tangentMode = TangentMode.Linear;
                }
                else if (isLeftTangentLinear || isRightTangentLinear)
                {
                    controlPoint.tangentMode = TangentMode.Broken;
                }
                else if (isContinous)
                {
                    controlPoint.tangentMode = TangentMode.Continuous;
                }

                controlPoints[i] = controlPoint;
            }

            return(controlPoints.ToArray());
        }