From 0d8b9832447b84fa62bd5f6f41af2507bf3001d5 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 2 Sep 2021 21:31:30 +0800 Subject: *curve --- .../ThirdParty/BezierCurves/Scripts/BezierPoint.cs | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Assets/ThirdParty/BezierCurves/Scripts/BezierPoint.cs (limited to 'Assets/ThirdParty/BezierCurves/Scripts/BezierPoint.cs') diff --git a/Assets/ThirdParty/BezierCurves/Scripts/BezierPoint.cs b/Assets/ThirdParty/BezierCurves/Scripts/BezierPoint.cs new file mode 100644 index 00000000..5abe769d --- /dev/null +++ b/Assets/ThirdParty/BezierCurves/Scripts/BezierPoint.cs @@ -0,0 +1,170 @@ +#region UsingStatements + +using UnityEngine; +using System; +using System.Collections; + +#endregion + +/// +/// - Helper class for storing and manipulating Bezier Point data +/// - Ensures that handles are in correct relation to one another +/// - Handles adding/removing self from curve point lists +/// - Calls SetDirty() on curve when edited +/// +[Serializable] +public class BezierPoint : MonoBehaviour{ + + #region PublicEnumerations + + /// + /// - Enumeration describing the relationship between a point's handles + /// - Connected : The point's handles are mirrored across the point + /// - Broken : Each handle moves independently of the other + /// - None : This point has no handles (both handles are located ON the point) + /// + public enum HandleStyle + { + Connected, + Broken, + None, + } + + #endregion + + #region PublicProperties + + /// + /// - Curve this point belongs to + /// - Changing this value will automatically remove this point from the current curve and add it to the new one + /// + [SerializeField] + private BezierCurve _curve; + public BezierCurve curve + { + get{return _curve;} + set + { + if(_curve) _curve.RemovePoint(this); + _curve = value; + _curve.AddPoint(this); + } + } + + /// + /// - Value describing the relationship between this point's handles + /// + public HandleStyle handleStyle; + + /// + /// - Shortcut to transform.position + /// + /// + /// - The point's world position + /// + public Vector3 position + { + get { return transform.position; } + set { transform.position = value; } + } + + /// + /// - Shortcut to transform.localPosition + /// + /// + /// - The point's local position. + /// + public Vector3 localPosition + { + get { return transform.localPosition; } + set { transform.localPosition = value; } + } + + /// + /// - Local position of the first handle + /// - Setting this value will cause the curve to become dirty + /// - This handle effects the curve generated from this point and the point proceeding it in curve.points + /// + [SerializeField] + private Vector3 _handle1; + public Vector3 handle1 + { + get { return _handle1; } + set + { + if(_handle1 == value) return; + _handle1 = value; + if(handleStyle == HandleStyle.None) handleStyle = HandleStyle.Broken; + else if(handleStyle == HandleStyle.Connected) _handle2 = -value; + _curve.SetDirty(); + } + } + + /// + /// - Global position of the first handle + /// - Ultimately stored in the 'handle1' variable + /// - Setting this value will cause the curve to become dirty + /// - This handle effects the curve generated from this point and the point proceeding it in curve.points + /// + public Vector3 globalHandle1 + { + get{return transform.TransformPoint(handle1);} + set{handle1 = transform.InverseTransformPoint(value);} + } + + /// + /// - Local position of the second handle + /// - Setting this value will cause the curve to become dirty + /// - This handle effects the curve generated from this point and the point coming after it in curve.points + /// + [SerializeField] + private Vector3 _handle2; + public Vector3 handle2 + { + get { return _handle2; } + set + { + if(_handle2 == value) return; + _handle2 = value; + if(handleStyle == HandleStyle.None) handleStyle = HandleStyle.Broken; + else if(handleStyle == HandleStyle.Connected) _handle1 = -value; + _curve.SetDirty(); + } + } + + /// + /// - Global position of the second handle + /// - Ultimately stored in the 'handle2' variable + /// - Setting this value will cause the curve to become dirty + /// - This handle effects the curve generated from this point and the point coming after it in curve.points + /// + public Vector3 globalHandle2 + { + get{return transform.TransformPoint(handle2);} + set{handle2 = transform.InverseTransformPoint(value);} + } + + #endregion + + #region PrivateVariables + + /// + /// - Used to determine if this point has moved since the last frame + /// + private Vector3 lastPosition; + + #endregion + + #region MonoBehaviourFunctions + + void Update() + { + if(!_curve.dirty && transform.position != lastPosition) + { + _curve.SetDirty(); + lastPosition = transform.position; + } + } + + #endregion +} -- cgit v1.1-26-g67d0