No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransformCache.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class TransformCache : SkinningObject, IEnumerable<TransformCache>
  7. {
  8. [SerializeField]
  9. TransformCache m_Parent;
  10. [SerializeField]
  11. List<TransformCache> m_Children = new List<TransformCache>();
  12. [SerializeField]
  13. Vector3 m_LocalPosition;
  14. [SerializeField]
  15. Quaternion m_LocalRotation = Quaternion.identity;
  16. [SerializeField]
  17. Vector3 m_LocalScale = Vector3.one;
  18. [SerializeField]
  19. Matrix4x4 m_LocalToWorldMatrix = Matrix4x4.identity;
  20. public TransformCache parent => m_Parent;
  21. public TransformCache[] children => m_Children.ToArray();
  22. internal int siblingIndex
  23. {
  24. get => GetSiblingIndex();
  25. set => SetSiblingIndex(value);
  26. }
  27. public int childCount => m_Children.Count;
  28. public Vector3 localPosition
  29. {
  30. get => m_LocalPosition;
  31. set
  32. {
  33. m_LocalPosition = value;
  34. Update();
  35. }
  36. }
  37. public Quaternion localRotation
  38. {
  39. get => m_LocalRotation;
  40. set
  41. {
  42. m_LocalRotation = MathUtility.NormalizeQuaternion(value);
  43. Update();
  44. }
  45. }
  46. public Vector3 localScale
  47. {
  48. get => m_LocalScale;
  49. set
  50. {
  51. m_LocalScale = value;
  52. Update();
  53. }
  54. }
  55. public Vector3 position
  56. {
  57. get => parentMatrix.MultiplyPoint3x4(localPosition);
  58. set => localPosition = parentMatrix.inverse.MultiplyPoint3x4(value);
  59. }
  60. public Quaternion rotation
  61. {
  62. get => GetGlobalRotation();
  63. set => SetGlobalRotation(value);
  64. }
  65. public Vector3 right
  66. {
  67. get => localToWorldMatrix.MultiplyVector(Vector3.right).normalized;
  68. set => MatchDirection(Vector3.right, value);
  69. }
  70. public Vector3 up
  71. {
  72. get => localToWorldMatrix.MultiplyVector(Vector3.up).normalized;
  73. set => MatchDirection(Vector3.up, value);
  74. }
  75. public Vector3 forward
  76. {
  77. get => localToWorldMatrix.MultiplyVector(Vector3.forward).normalized;
  78. set => MatchDirection(Vector3.forward, value);
  79. }
  80. public Matrix4x4 localToWorldMatrix => m_LocalToWorldMatrix;
  81. public Matrix4x4 worldToLocalMatrix => localToWorldMatrix.inverse;
  82. Matrix4x4 parentMatrix
  83. {
  84. get
  85. {
  86. var matrix = Matrix4x4.identity;
  87. if (parent != null)
  88. matrix = parent.localToWorldMatrix;
  89. return matrix;
  90. }
  91. }
  92. internal override void OnDestroy()
  93. {
  94. if (parent != null)
  95. parent.RemoveChild(this);
  96. m_Parent = null;
  97. m_Children.Clear();
  98. }
  99. void Update()
  100. {
  101. m_LocalToWorldMatrix = parentMatrix * Matrix4x4.TRS(localPosition, localRotation, localScale);
  102. foreach (var child in m_Children)
  103. child.Update();
  104. }
  105. void AddChild(TransformCache transform)
  106. {
  107. m_Children.Add(transform);
  108. }
  109. void InsertChildAt(int index, TransformCache transform)
  110. {
  111. m_Children.Insert(index, transform);
  112. }
  113. void RemoveChild(TransformCache transform)
  114. {
  115. m_Children.Remove(transform);
  116. }
  117. void RemoveChildAt(int index)
  118. {
  119. m_Children.RemoveAt(index);
  120. }
  121. int GetSiblingIndex()
  122. {
  123. if (parent == null)
  124. return -1;
  125. return parent.m_Children.IndexOf(this);
  126. }
  127. void SetSiblingIndex(int index)
  128. {
  129. if (parent == null)
  130. return;
  131. var currentIndex = parent.m_Children.IndexOf(this);
  132. var indexToRemove = index < currentIndex ? currentIndex + 1 : currentIndex;
  133. parent.InsertChildAt(index, this);
  134. parent.RemoveChildAt(indexToRemove);
  135. }
  136. public void SetParent(TransformCache newParent, bool worldPositionStays = true)
  137. {
  138. if (m_Parent == newParent)
  139. return;
  140. var oldPosition = position;
  141. var oldRotation = rotation;
  142. if (m_Parent != null)
  143. m_Parent.RemoveChild(this);
  144. m_Parent = newParent;
  145. if (m_Parent != null)
  146. m_Parent.AddChild(this);
  147. if (worldPositionStays)
  148. {
  149. position = oldPosition;
  150. rotation = oldRotation;
  151. }
  152. else
  153. {
  154. Update();
  155. }
  156. }
  157. Quaternion GetGlobalRotation()
  158. {
  159. var globalRotation = localRotation;
  160. var currentParent = parent;
  161. while (currentParent != null)
  162. {
  163. globalRotation = ScaleMulQuaternion(currentParent.localScale, globalRotation);
  164. globalRotation = currentParent.localRotation * globalRotation;
  165. currentParent = currentParent.parent;
  166. }
  167. return globalRotation;
  168. }
  169. void SetGlobalRotation(Quaternion r)
  170. {
  171. if (parent != null)
  172. r = parent.InverseTransformRotation(r);
  173. localRotation = r;
  174. }
  175. Quaternion InverseTransformRotation(Quaternion r)
  176. {
  177. if (parent != null)
  178. r = parent.InverseTransformRotation(r);
  179. r = Quaternion.Inverse(localRotation) * r;
  180. r = ScaleMulQuaternion(localScale, r);
  181. return r;
  182. }
  183. static Quaternion ScaleMulQuaternion(Vector3 scale, Quaternion q)
  184. {
  185. var s = new Vector3(ChangeSign(1f, scale.x), ChangeSign(1f, scale.y), ChangeSign(1f, scale.z));
  186. q.x = ChangeSign(q.x, s.y * s.z);
  187. q.y = ChangeSign(q.y, s.x * s.z);
  188. q.z = ChangeSign(q.z, s.x * s.y);
  189. return q;
  190. }
  191. static float ChangeSign(float x, float y)
  192. {
  193. return y < 0f ? -x : x;
  194. }
  195. void MatchDirection(Vector3 localDirection, Vector3 worldDirection)
  196. {
  197. var direction = worldToLocalMatrix.MultiplyVector(worldDirection);
  198. direction = Matrix4x4.TRS(Vector3.zero, localRotation, localScale).MultiplyVector(direction);
  199. var scaledLocalDirection = Vector3.Scale(localDirection, localScale);
  200. var deltaRotation = Quaternion.identity;
  201. if (scaledLocalDirection.sqrMagnitude > 0f)
  202. {
  203. var axis = Vector3.Cross(scaledLocalDirection, direction);
  204. var angle = Vector3.SignedAngle(scaledLocalDirection, direction, axis);
  205. deltaRotation = Quaternion.AngleAxis(angle, axis);
  206. }
  207. localRotation = deltaRotation;
  208. }
  209. IEnumerator<TransformCache> IEnumerable<TransformCache>.GetEnumerator()
  210. {
  211. return m_Children.GetEnumerator();
  212. }
  213. IEnumerator IEnumerable.GetEnumerator()
  214. {
  215. return (IEnumerator)m_Children.GetEnumerator();
  216. }
  217. }
  218. }