BezierPoint.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. namespace BezierSolution
  3. {
  4. public class BezierPoint : MonoBehaviour
  5. {
  6. public enum HandleMode { Free, Aligned, Mirrored };
  7. public Vector3 localPosition
  8. {
  9. get { return transform.localPosition; }
  10. set { transform.localPosition = value; }
  11. }
  12. [SerializeField]
  13. [HideInInspector]
  14. private Vector3 m_position;
  15. public Vector3 position
  16. {
  17. get
  18. {
  19. if( transform.hasChanged )
  20. Revalidate();
  21. return m_position;
  22. }
  23. set { transform.position = value; }
  24. }
  25. public Quaternion localRotation
  26. {
  27. get { return transform.localRotation; }
  28. set { transform.localRotation = value; }
  29. }
  30. public Quaternion rotation
  31. {
  32. get { return transform.rotation; }
  33. set { transform.rotation = value; }
  34. }
  35. public Vector3 localEulerAngles
  36. {
  37. get { return transform.localEulerAngles; }
  38. set { transform.localEulerAngles = value; }
  39. }
  40. public Vector3 eulerAngles
  41. {
  42. get { return transform.eulerAngles; }
  43. set { transform.eulerAngles = value; }
  44. }
  45. public Vector3 localScale
  46. {
  47. get { return transform.localScale; }
  48. set { transform.localScale = value; }
  49. }
  50. [SerializeField]
  51. [HideInInspector]
  52. private Vector3 m_precedingControlPointLocalPosition = Vector3.left;
  53. public Vector3 precedingControlPointLocalPosition
  54. {
  55. get
  56. {
  57. return m_precedingControlPointLocalPosition;
  58. }
  59. set
  60. {
  61. m_precedingControlPointLocalPosition = value;
  62. m_precedingControlPointPosition = transform.TransformPoint( value );
  63. if( m_handleMode == HandleMode.Aligned )
  64. {
  65. m_followingControlPointLocalPosition = -m_precedingControlPointLocalPosition.normalized * m_followingControlPointLocalPosition.magnitude;
  66. m_followingControlPointPosition = transform.TransformPoint( m_followingControlPointLocalPosition );
  67. }
  68. else if( m_handleMode == HandleMode.Mirrored )
  69. {
  70. m_followingControlPointLocalPosition = -m_precedingControlPointLocalPosition;
  71. m_followingControlPointPosition = transform.TransformPoint( m_followingControlPointLocalPosition );
  72. }
  73. }
  74. }
  75. [SerializeField]
  76. [HideInInspector]
  77. private Vector3 m_precedingControlPointPosition;
  78. public Vector3 precedingControlPointPosition
  79. {
  80. get
  81. {
  82. if( transform.hasChanged )
  83. Revalidate();
  84. return m_precedingControlPointPosition;
  85. }
  86. set
  87. {
  88. m_precedingControlPointPosition = value;
  89. m_precedingControlPointLocalPosition = transform.InverseTransformPoint( value );
  90. if( transform.hasChanged )
  91. {
  92. m_position = transform.position;
  93. transform.hasChanged = false;
  94. }
  95. if( m_handleMode == HandleMode.Aligned )
  96. {
  97. m_followingControlPointPosition = m_position - ( m_precedingControlPointPosition - m_position ).normalized *
  98. ( m_followingControlPointPosition - m_position ).magnitude;
  99. m_followingControlPointLocalPosition = transform.InverseTransformPoint( m_followingControlPointPosition );
  100. }
  101. else if( m_handleMode == HandleMode.Mirrored )
  102. {
  103. m_followingControlPointPosition = 2f * m_position - m_precedingControlPointPosition;
  104. m_followingControlPointLocalPosition = transform.InverseTransformPoint( m_followingControlPointPosition );
  105. }
  106. }
  107. }
  108. [SerializeField]
  109. [HideInInspector]
  110. private Vector3 m_followingControlPointLocalPosition = Vector3.right;
  111. public Vector3 followingControlPointLocalPosition
  112. {
  113. get
  114. {
  115. return m_followingControlPointLocalPosition;
  116. }
  117. set
  118. {
  119. m_followingControlPointLocalPosition = value;
  120. m_followingControlPointPosition = transform.TransformPoint( value );
  121. if( m_handleMode == HandleMode.Aligned )
  122. {
  123. m_precedingControlPointLocalPosition = -m_followingControlPointLocalPosition.normalized * m_precedingControlPointLocalPosition.magnitude;
  124. m_precedingControlPointPosition = transform.TransformPoint( m_precedingControlPointLocalPosition );
  125. }
  126. else if( m_handleMode == HandleMode.Mirrored )
  127. {
  128. m_precedingControlPointLocalPosition = -m_followingControlPointLocalPosition;
  129. m_precedingControlPointPosition = transform.TransformPoint( m_precedingControlPointLocalPosition );
  130. }
  131. }
  132. }
  133. [SerializeField]
  134. [HideInInspector]
  135. private Vector3 m_followingControlPointPosition;
  136. public Vector3 followingControlPointPosition
  137. {
  138. get
  139. {
  140. if( transform.hasChanged )
  141. Revalidate();
  142. return m_followingControlPointPosition;
  143. }
  144. set
  145. {
  146. m_followingControlPointPosition = value;
  147. m_followingControlPointLocalPosition = transform.InverseTransformPoint( value );
  148. if( transform.hasChanged )
  149. {
  150. m_position = transform.position;
  151. transform.hasChanged = false;
  152. }
  153. if( m_handleMode == HandleMode.Aligned )
  154. {
  155. m_precedingControlPointPosition = m_position - ( m_followingControlPointPosition - m_position ).normalized *
  156. ( m_precedingControlPointPosition - m_position ).magnitude;
  157. m_precedingControlPointLocalPosition = transform.InverseTransformPoint( m_precedingControlPointPosition );
  158. }
  159. else if( m_handleMode == HandleMode.Mirrored )
  160. {
  161. m_precedingControlPointPosition = 2f * m_position - m_followingControlPointPosition;
  162. m_precedingControlPointLocalPosition = transform.InverseTransformPoint( m_precedingControlPointPosition );
  163. }
  164. }
  165. }
  166. [SerializeField]
  167. [HideInInspector]
  168. private HandleMode m_handleMode = HandleMode.Mirrored;
  169. public HandleMode handleMode
  170. {
  171. get
  172. {
  173. return m_handleMode;
  174. }
  175. set
  176. {
  177. m_handleMode = value;
  178. if( value == HandleMode.Aligned || value == HandleMode.Mirrored )
  179. precedingControlPointLocalPosition = m_precedingControlPointLocalPosition;
  180. }
  181. }
  182. private void Awake()
  183. {
  184. transform.hasChanged = true;
  185. }
  186. public void CopyTo( BezierPoint other )
  187. {
  188. other.transform.localPosition = transform.localPosition;
  189. other.transform.localRotation = transform.localRotation;
  190. other.transform.localScale = transform.localScale;
  191. other.m_handleMode = m_handleMode;
  192. other.m_precedingControlPointLocalPosition = m_precedingControlPointLocalPosition;
  193. other.m_followingControlPointLocalPosition = m_followingControlPointLocalPosition;
  194. }
  195. private void Revalidate()
  196. {
  197. m_position = transform.position;
  198. m_precedingControlPointPosition = transform.TransformPoint( m_precedingControlPointLocalPosition );
  199. m_followingControlPointPosition = transform.TransformPoint( m_followingControlPointLocalPosition );
  200. transform.hasChanged = false;
  201. }
  202. public void Reset()
  203. {
  204. localPosition = Vector3.zero;
  205. localRotation = Quaternion.identity;
  206. localScale = Vector3.one;
  207. precedingControlPointLocalPosition = Vector3.left;
  208. followingControlPointLocalPosition = Vector3.right;
  209. transform.hasChanged = true;
  210. }
  211. }
  212. }