Inheritance: UnityEngine.MonoBehaviour
Beispiel #1
0
 private static void RenamePoints(BezierCurve3D curve)
 {
     for (int i = 0; i < curve.KeyPointsCount; i++)
     {
         curve.KeyPoints[i].name = "Point " + i;
     }
 }
        /// <summary>
        /// Add a key point at a specified index
        /// </summary>
        /// <param name="index">The index at which the key point will be added</param>
        /// <returns>The new key point</returns>
        public BezierPoint3D AddKeyPointAt(int index)
        {
            BezierPoint3D newPoint = new GameObject("Point " + this.KeyPoints.Count, typeof(BezierPoint3D)).GetComponent <BezierPoint3D>();

            newPoint.Curve                   = this;
            newPoint.transform.parent        = this.transform;
            newPoint.transform.localRotation = Quaternion.identity;

            if (this.KeyPointsCount == 0 || this.KeyPointsCount == 1)
            {
                newPoint.LocalPosition = Vector3.zero;
            }
            else
            {
                if (index == 0)
                {
                    newPoint.Position = (this.KeyPoints[0].Position - this.KeyPoints[1].Position).normalized + this.KeyPoints[0].Position;
                }
                else if (index == this.KeyPointsCount)
                {
                    newPoint.Position = (this.KeyPoints[index - 1].Position - this.KeyPoints[index - 2].Position).normalized + this.KeyPoints[index - 1].Position;
                }
                else
                {
                    newPoint.Position = BezierCurve3D.GetPointOnCubicCurve(0.5f, this.KeyPoints[index - 1], this.KeyPoints[index]);
                }
            }

            this.KeyPoints.Insert(index, newPoint);

            return(newPoint);
        }
        public Quaternion GetRotation(float time, Vector3 up)
        {
            BezierPoint3D startPoint;
            BezierPoint3D endPoint;
            float         timeRelativeToSegment;

            this.GetCubicSegment(time, out startPoint, out endPoint, out timeRelativeToSegment);

            return(BezierCurve3D.GetRotationOnCubicCurve(timeRelativeToSegment, up, startPoint, endPoint));
        }
        public Vector3 GetNormal(float time, Vector3 up)
        {
            BezierPoint3D startPoint;
            BezierPoint3D endPoint;
            float         timeRelativeToSegment;

            this.GetCubicSegment(time, out startPoint, out endPoint, out timeRelativeToSegment);

            return(BezierCurve3D.GetNormalOnCubicCurve(timeRelativeToSegment, up, startPoint, endPoint));
        }
        /// <summary>
        /// Evaluates a position along the curve at a specified normalized time [0, 1]
        /// </summary>
        /// <param name="time">The normalized length at which we want to get a position [0, 1]</param>
        /// <returns>The evaluated Vector3 position</returns>
        public Vector3 GetPoint(float time)
        {
            // The evaluated points is between these two points
            BezierPoint3D startPoint;
            BezierPoint3D endPoint;
            float         timeRelativeToSegment;

            this.GetCubicSegment(time, out startPoint, out endPoint, out timeRelativeToSegment);

            return(BezierCurve3D.GetPointOnCubicCurve(timeRelativeToSegment, startPoint, endPoint));
        }
Beispiel #6
0
        private static void AddDefaultPoints(BezierCurve3D curve)
        {
            BezierPoint3D startPoint = curve.AddKeyPoint();

            startPoint.LocalPosition           = new Vector3(-1f, 0f, 0f);
            startPoint.LeftHandleLocalPosition = new Vector3(-0.35f, -0.35f, 0f);

            BezierPoint3D endPoint = curve.AddKeyPoint();

            endPoint.LocalPosition           = new Vector3(1f, 0f, 0f);
            endPoint.LeftHandleLocalPosition = new Vector3(-0.35f, 0.35f, 0f);
        }
        public float GetApproximateLength()
        {
            float length           = 0;
            int   subCurveSampling = (this.Sampling / (this.KeyPointsCount - 1)) + 1;

            for (int i = 0; i < this.KeyPointsCount - 1; i++)
            {
                length += BezierCurve3D.GetApproximateLengthOfCubicCurve(this.KeyPoints[i], this.KeyPoints[i + 1], subCurveSampling);
            }

            return(length);
        }
        public static void DrawPointsSceneGUI(BezierCurve3D curve, BezierPoint3D exclude = null)
        {
            for (int i = 0; i < curve.KeyPointsCount; i++)
            {
                if (curve.KeyPoints[i] == exclude)
                {
                    continue;
                }

                BezierPoint3DEditor.handleCapSize = BezierPoint3DEditor.CircleCapSize;
                BezierPoint3DEditor.DrawPointSceneGUI(curve.KeyPoints[i]);
            }
        }
Beispiel #9
0
        public static void DrawPointsSceneGUI(BezierCurve3D curve, BezierPoint3D exclude = null)
        {
            for (int i = 0; i < curve.KeyPointsCount; i++)
            {
                if (curve.KeyPoints[i] == exclude)
                {
                    continue;
                }

                BezierPoint3DEditor.handleCapSize = BezierPoint3DEditor.CircleCapSize;
                BezierPoint3DEditor.DrawPointSceneGUI(curve.KeyPoints[i]);
            }
        }
Beispiel #10
0
        private static bool RemoveKeyPointAt(BezierCurve3D curve, int index)
        {
            if (curve.KeyPointsCount < 2)
            {
                return(false);
            }

            var point = curve.KeyPoints[index];

            Undo.IncrementCurrentGroup();
            Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            curve.KeyPoints.RemoveAt(index);
            RenamePoints(curve);

            //Undo.RegisterCompleteObjectUndo(curve, "Save Curve");
            Undo.DestroyObjectImmediate(point.gameObject);

            return(true);
        }
Beispiel #11
0
        protected virtual void OnEnable()
        {
            this.curve = (BezierCurve3D)this.target;
            if (curve.KeyPointsCount < 2)
            {
                while (curve.KeyPointsCount != 0)
                {
                    curve.RemoveKeyPointAt(this.curve.KeyPointsCount - 1);
                }

                BezierCurve3DEditor.AddDefaultPoints(this.curve);
            }

            this.keyPoints = new ReorderableList(this.serializedObject, serializedObject.FindProperty("keyPoints"), true, true, false, false);
            this.keyPoints.drawElementCallback = this.DrawElementCallback;
            this.keyPoints.drawHeaderCallback  =
                (Rect rect) =>
            {
                EditorGUI.LabelField(rect, string.Format("Points: {0}", this.keyPoints.serializedProperty.arraySize), EditorStyles.boldLabel);
            };
        }
        protected virtual void OnEnable()
        {
            this.curve = (BezierCurve3D)this.target;
            if (curve.KeyPointsCount < 2)
            {
                while (curve.KeyPointsCount != 0)
                {
                    curve.RemoveKeyPointAt(this.curve.KeyPointsCount - 1);
                }

                BezierCurve3DEditor.AddDefaultPoints(this.curve);
            }

            this.keyPoints = new ReorderableList(this.serializedObject, serializedObject.FindProperty("keyPoints"), true, true, false, false);
            this.keyPoints.drawElementCallback = this.DrawElementCallback;
            this.keyPoints.drawHeaderCallback =
                (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, string.Format("Reorderable List | Points: {0}", this.keyPoints.serializedProperty.arraySize), EditorStyles.boldLabel);
                };
        }
Beispiel #13
0
        private static BezierPoint3D AddKeyPointAt(BezierCurve3D curve, int index)
        {
            BezierPoint3D newPoint = new GameObject("Point " + curve.KeyPointsCount, typeof(BezierPoint3D)).GetComponent <BezierPoint3D>();

            newPoint.transform.parent        = curve.transform;
            newPoint.transform.localRotation = Quaternion.identity;
            newPoint.Curve = curve;

            if (curve.KeyPointsCount == 0 || curve.KeyPointsCount == 1)
            {
                newPoint.LocalPosition = Vector3.zero;
            }
            else
            {
                if (index == 0)
                {
                    newPoint.Position = (curve.KeyPoints[0].Position - curve.KeyPoints[1].Position).normalized + curve.KeyPoints[0].Position;
                }
                else if (index == curve.KeyPointsCount)
                {
                    newPoint.Position = (curve.KeyPoints[index - 1].Position - curve.KeyPoints[index - 2].Position).normalized + curve.KeyPoints[index - 1].Position;
                }
                else
                {
                    newPoint.Position = BezierCurve3D.GetPointOnCubicCurve(0.5f, curve.KeyPoints[index - 1], curve.KeyPoints[index]);
                }
            }

            Undo.IncrementCurrentGroup();
            Undo.RegisterCreatedObjectUndo(newPoint.gameObject, "Create Point");
            Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            curve.KeyPoints.Insert(index, newPoint);
            RenamePoints(curve);

            //Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            return(newPoint);
        }
        public void GetCubicSegment(float time, out BezierPoint3D startPoint, out BezierPoint3D endPoint, out float timeRelativeToSegment)
        {
            startPoint            = null;
            endPoint              = null;
            timeRelativeToSegment = 0f;

            float subCurvePercent   = 0f;
            float totalPercent      = 0f;
            float approximateLength = this.GetApproximateLength();
            int   subCurveSampling  = (this.Sampling / (this.KeyPointsCount - 1)) + 1;

            for (int i = 0; i < this.KeyPointsCount - 1; i++)
            {
                subCurvePercent = BezierCurve3D.GetApproximateLengthOfCubicCurve(this.KeyPoints[i], this.KeyPoints[i + 1], subCurveSampling) / approximateLength;
                if (subCurvePercent + totalPercent > time)
                {
                    startPoint = this.KeyPoints[i];
                    endPoint   = this.KeyPoints[i + 1];

                    break;
                }

                totalPercent += subCurvePercent;
            }

            if (endPoint == null)
            {
                // If the evaluated point is very near to the end of the curve we are in the last segment
                startPoint = this.KeyPoints[this.KeyPointsCount - 2];
                endPoint   = this.KeyPoints[this.KeyPointsCount - 1];

                // We remove the percentage of the last sub-curve
                totalPercent -= subCurvePercent;
            }

            timeRelativeToSegment = (time - totalPercent) / subCurvePercent;
        }
        private static BezierPoint3D AddKeyPointAt(BezierCurve3D curve, int index)
        {
            BezierPoint3D newPoint = new GameObject("Point " + curve.KeyPointsCount, typeof(BezierPoint3D)).GetComponent<BezierPoint3D>();
            newPoint.transform.parent = curve.transform;
            newPoint.transform.localRotation = Quaternion.identity;
            newPoint.Curve = curve;

            if (curve.KeyPointsCount == 0 || curve.KeyPointsCount == 1)
            {
                newPoint.LocalPosition = Vector3.zero;
            }
            else
            {
                if (index == 0)
                {
                    newPoint.Position = (curve.KeyPoints[0].Position - curve.KeyPoints[1].Position).normalized + curve.KeyPoints[0].Position;
                }
                else if (index == curve.KeyPointsCount)
                {
                    newPoint.Position = (curve.KeyPoints[index - 1].Position - curve.KeyPoints[index - 2].Position).normalized + curve.KeyPoints[index - 1].Position;
                }
                else
                {
                    newPoint.Position = BezierCurve3D.GetPointOnCubicCurve(0.5f, curve.KeyPoints[index - 1], curve.KeyPoints[index]);
                }
            }

            Undo.IncrementCurrentGroup();
            Undo.RegisterCreatedObjectUndo(newPoint.gameObject, "Create Point");
            Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            curve.KeyPoints.Insert(index, newPoint);
            RenamePoints(curve);

            //Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            return newPoint;
        }
        private static void AddDefaultPoints(BezierCurve3D curve)
        {
            BezierPoint3D startPoint = curve.AddKeyPoint();
            startPoint.LocalPosition = new Vector3(-1f, 0f, 0f);
            startPoint.LeftHandleLocalPosition = new Vector3(-0.35f, -0.35f, 0f);

            BezierPoint3D endPoint = curve.AddKeyPoint();
            endPoint.LocalPosition = new Vector3(1f, 0f, 0f);
            endPoint.LeftHandleLocalPosition = new Vector3(-0.35f, 0.35f, 0f);
        }
        private static bool RemoveKeyPointAt(BezierCurve3D curve, int index)
        {
            if (curve.KeyPointsCount < 2)
            {
                return false;
            }

            var point = curve.KeyPoints[index];

            Undo.IncrementCurrentGroup();
            Undo.RegisterCompleteObjectUndo(curve, "Save Curve");

            curve.KeyPoints.RemoveAt(index);
            RenamePoints(curve);

            //Undo.RegisterCompleteObjectUndo(curve, "Save Curve");
            Undo.DestroyObjectImmediate(point.gameObject);

            return true;
        }
 private static void RenamePoints(BezierCurve3D curve)
 {
     for (int i = 0; i < curve.KeyPointsCount; i++)
     {
         curve.KeyPoints[i].name = "Point " + i;
     }
 }