Solver.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using System;
  4. using UnityEngine;
  5. namespace Microsoft.MixedReality.Toolkit.Utilities.Solvers
  6. {
  7. /// <summary>
  8. /// The base abstract class for all Solvers to derive from. It provides state tracking, smoothing parameters
  9. /// and implementation, automatic solver system integration, and update order. Solvers may be used without a link,
  10. /// as long as updateLinkedTransform is false.
  11. /// </summary>
  12. [RequireComponent(typeof(SolverHandler))]
  13. [HelpURL("https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/README_Solver.html")]
  14. public abstract class Solver : MonoBehaviour
  15. {
  16. [SerializeField]
  17. [Tooltip("If true, the position and orientation will be calculated, but not applied, for other components to use")]
  18. private bool updateLinkedTransform = false;
  19. /// <summary>
  20. /// If true, the position and orientation will be calculated, but not applied, for other components to use
  21. /// </summary>
  22. public bool UpdateLinkedTransform
  23. {
  24. get => updateLinkedTransform;
  25. set => updateLinkedTransform = value;
  26. }
  27. [SerializeField]
  28. [Tooltip("If 0, the position will update immediately. Otherwise, the greater this attribute the slower the position updates")]
  29. private float moveLerpTime = 0.1f;
  30. /// <summary>
  31. /// If 0, the position will update immediately. Otherwise, the greater this attribute the slower the position updates
  32. /// </summary>
  33. public float MoveLerpTime
  34. {
  35. get => moveLerpTime;
  36. set => moveLerpTime = value;
  37. }
  38. [SerializeField]
  39. [Tooltip("If 0, the rotation will update immediately. Otherwise, the greater this attribute the slower the rotation updates")]
  40. private float rotateLerpTime = 0.1f;
  41. /// <summary>
  42. /// If 0, the rotation will update immediately. Otherwise, the greater this attribute the slower the rotation updates")]
  43. /// </summary>
  44. public float RotateLerpTime
  45. {
  46. get => rotateLerpTime;
  47. set => rotateLerpTime = value;
  48. }
  49. [SerializeField]
  50. [Tooltip("If 0, the scale will update immediately. Otherwise, the greater this attribute the slower the scale updates")]
  51. private float scaleLerpTime = 0;
  52. /// <summary>
  53. /// If 0, the scale will update immediately. Otherwise, the greater this attribute the slower the scale updates
  54. /// </summary>
  55. public float ScaleLerpTime
  56. {
  57. get => scaleLerpTime;
  58. set => scaleLerpTime = value;
  59. }
  60. [SerializeField]
  61. [Tooltip("If true, the Solver will respect the object's original scale values")]
  62. private bool maintainScale = true;
  63. [SerializeField]
  64. [Tooltip("If true, updates are smoothed to the target. Otherwise, they are snapped to the target")]
  65. private bool smoothing = true;
  66. /// <summary>
  67. /// If true, updates are smoothed to the target. Otherwise, they are snapped to the target
  68. /// </summary>
  69. public bool Smoothing
  70. {
  71. get => smoothing;
  72. set => smoothing = value;
  73. }
  74. [SerializeField]
  75. [Tooltip("If > 0, this solver will deactivate after this much time, even if the state is still active")]
  76. private float lifetime = 0;
  77. private float currentLifetime;
  78. /// <summary>
  79. /// The handler reference for this solver that's attached to this <see href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</see>
  80. /// </summary>
  81. [HideInInspector]
  82. protected SolverHandler SolverHandler;
  83. /// <summary>
  84. /// The final position to be attained
  85. /// </summary>
  86. protected Vector3 GoalPosition
  87. {
  88. get { return SolverHandler.GoalPosition; }
  89. set { SolverHandler.GoalPosition = value; }
  90. }
  91. /// <summary>
  92. /// The final rotation to be attained
  93. /// </summary>
  94. protected Quaternion GoalRotation
  95. {
  96. get { return SolverHandler.GoalRotation; }
  97. set { SolverHandler.GoalRotation = value; }
  98. }
  99. /// <summary>
  100. /// The final scale to be attained
  101. /// </summary>
  102. protected Vector3 GoalScale
  103. {
  104. get { return SolverHandler.GoalScale; }
  105. set { SolverHandler.GoalScale = value; }
  106. }
  107. /// <summary>
  108. /// Automatically uses the shared position if the solver is set to use the 'linked transform'.
  109. /// UpdateLinkedTransform may be set to false, and a solver will automatically update the object directly,
  110. /// and not inherit work done by other solvers to the shared position
  111. /// </summary>
  112. public Vector3 WorkingPosition
  113. {
  114. get
  115. {
  116. return updateLinkedTransform ? GoalPosition : transform.position;
  117. }
  118. protected set
  119. {
  120. if (updateLinkedTransform)
  121. {
  122. GoalPosition = value;
  123. }
  124. else
  125. {
  126. transform.position = value;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Rotation version of WorkingPosition
  132. /// </summary>
  133. public Quaternion WorkingRotation
  134. {
  135. get
  136. {
  137. return updateLinkedTransform ? GoalRotation : transform.rotation;
  138. }
  139. protected set
  140. {
  141. if (updateLinkedTransform)
  142. {
  143. GoalRotation = value;
  144. }
  145. else
  146. {
  147. transform.rotation = value;
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Scale version of WorkingPosition
  153. /// </summary>
  154. public Vector3 WorkingScale
  155. {
  156. get
  157. {
  158. return updateLinkedTransform ? GoalScale : transform.localScale;
  159. }
  160. protected set
  161. {
  162. if (updateLinkedTransform)
  163. {
  164. GoalScale = value;
  165. }
  166. else
  167. {
  168. transform.localScale = value;
  169. }
  170. }
  171. }
  172. #region MonoBehaviour Implementation
  173. protected virtual void Awake()
  174. {
  175. if (SolverHandler == null)
  176. {
  177. SolverHandler = GetComponent<SolverHandler>();
  178. }
  179. if (updateLinkedTransform && SolverHandler == null)
  180. {
  181. Debug.LogError("No SolverHandler component found on " + name + " when UpdateLinkedTransform was set to true! Disabling UpdateLinkedTransform.");
  182. updateLinkedTransform = false;
  183. }
  184. GoalScale = maintainScale ? transform.localScale : Vector3.one;
  185. }
  186. /// <summary>
  187. /// Typically when a solver becomes enabled, it should update its internal state to the system, in case it was disabled far away
  188. /// </summary>
  189. protected virtual void OnEnable()
  190. {
  191. if (SolverHandler != null)
  192. {
  193. SnapGoalTo(GoalPosition, GoalRotation, GoalScale);
  194. }
  195. currentLifetime = 0;
  196. }
  197. protected virtual void Start()
  198. {
  199. if (SolverHandler != null)
  200. {
  201. SolverHandler.RegisterSolver(this);
  202. }
  203. }
  204. protected virtual void OnDestroy()
  205. {
  206. if (SolverHandler != null)
  207. {
  208. SolverHandler.UnregisterSolver(this);
  209. }
  210. }
  211. #endregion MonoBehaviour Implementation
  212. /// <summary>
  213. /// Should be implemented in derived classes, but Solver can be used to flush shared transform to real transform
  214. /// </summary>
  215. public abstract void SolverUpdate();
  216. /// <summary>
  217. /// Tracks lifetime of the solver, disabling it when expired, and finally runs the orientation update logic
  218. /// </summary>
  219. public void SolverUpdateEntry()
  220. {
  221. currentLifetime += SolverHandler.DeltaTime;
  222. if (lifetime > 0 && currentLifetime >= lifetime)
  223. {
  224. enabled = false;
  225. return;
  226. }
  227. SolverUpdate();
  228. UpdateWorkingToGoal();
  229. }
  230. /// <summary>
  231. /// Snaps the solver to the desired pose.
  232. /// </summary>
  233. /// <remarks>
  234. /// SnapTo may be used to bypass smoothing to a certain position if the object is teleported or spawned.
  235. /// </remarks>
  236. public virtual void SnapTo(Vector3 position, Quaternion rotation, Vector3 scale)
  237. {
  238. SnapGoalTo(position, rotation, scale);
  239. WorkingPosition = position;
  240. WorkingRotation = rotation;
  241. WorkingScale = scale;
  242. }
  243. /// <summary>
  244. /// SnapGoalTo only sets the goal orientation. Not really useful.
  245. /// </summary>
  246. public virtual void SnapGoalTo(Vector3 position, Quaternion rotation, Vector3 scale)
  247. {
  248. GoalPosition = position;
  249. GoalRotation = rotation;
  250. GoalScale = scale;
  251. }
  252. /// <summary>
  253. /// Snaps the solver to the desired pose.
  254. /// </summary>
  255. /// <remarks>
  256. /// SnapTo may be used to bypass smoothing to a certain position if the object is teleported or spawned.
  257. /// </remarks>
  258. [Obsolete("Use SnapTo(Vector3, Quaternion, Vector3) instead.")]
  259. public virtual void SnapTo(Vector3 position, Quaternion rotation)
  260. {
  261. SnapGoalTo(position, rotation);
  262. WorkingPosition = position;
  263. WorkingRotation = rotation;
  264. }
  265. /// <summary>
  266. /// SnapGoalTo only sets the goal orientation. Not really useful.
  267. /// </summary>
  268. [Obsolete("Use SnapGoalTo(Vector3, Quaternion, Vector3) instead.")]
  269. public virtual void SnapGoalTo(Vector3 position, Quaternion rotation)
  270. {
  271. GoalPosition = position;
  272. GoalRotation = rotation;
  273. }
  274. /// <summary>
  275. /// Add an offset position to the target goal position.
  276. /// </summary>
  277. public virtual void AddOffset(Vector3 offset)
  278. {
  279. GoalPosition += offset;
  280. }
  281. /// <summary>
  282. /// Lerps Vector3 source to goal.
  283. /// </summary>
  284. /// <remarks>
  285. /// Handles lerpTime of 0.
  286. /// </remarks>
  287. public static Vector3 SmoothTo(Vector3 source, Vector3 goal, float deltaTime, float lerpTime)
  288. {
  289. return Vector3.Lerp(source, goal, lerpTime.Equals(0.0f) ? 1f : deltaTime / lerpTime);
  290. }
  291. /// <summary>
  292. /// Slerps Quaternion source to goal, handles lerpTime of 0
  293. /// </summary>
  294. public static Quaternion SmoothTo(Quaternion source, Quaternion goal, float deltaTime, float lerpTime)
  295. {
  296. return Quaternion.Slerp(source, goal, lerpTime.Equals(0.0f) ? 1f : deltaTime / lerpTime);
  297. }
  298. /// <summary>
  299. /// Updates all object orientations to the goal orientation for this solver, with smoothing accounted for (smoothing may be off)
  300. /// </summary>
  301. protected void UpdateTransformToGoal()
  302. {
  303. if (smoothing)
  304. {
  305. Vector3 pos = transform.position;
  306. Quaternion rot = transform.rotation;
  307. Vector3 scale = transform.localScale;
  308. pos = SmoothTo(pos, GoalPosition, SolverHandler.DeltaTime, moveLerpTime);
  309. rot = SmoothTo(rot, GoalRotation, SolverHandler.DeltaTime, rotateLerpTime);
  310. scale = SmoothTo(scale, GoalScale, SolverHandler.DeltaTime, scaleLerpTime);
  311. transform.position = pos;
  312. transform.rotation = rot;
  313. transform.localScale = scale;
  314. }
  315. else
  316. {
  317. transform.position = GoalPosition;
  318. transform.rotation = GoalRotation;
  319. transform.localScale = GoalScale;
  320. }
  321. }
  322. /// <summary>
  323. /// Updates the Working orientation (which may be the object, or the shared orientation) to the goal with smoothing, if enabled
  324. /// </summary>
  325. public void UpdateWorkingToGoal()
  326. {
  327. UpdateWorkingPositionToGoal();
  328. UpdateWorkingRotationToGoal();
  329. UpdateWorkingScaleToGoal();
  330. }
  331. /// <summary>
  332. /// Updates only the working position to goal with smoothing, if enabled
  333. /// </summary>
  334. public void UpdateWorkingPositionToGoal()
  335. {
  336. WorkingPosition = smoothing ? SmoothTo(WorkingPosition, GoalPosition, SolverHandler.DeltaTime, moveLerpTime) : GoalPosition;
  337. }
  338. /// <summary>
  339. /// Updates only the working rotation to goal with smoothing, if enabled
  340. /// </summary>
  341. public void UpdateWorkingRotationToGoal()
  342. {
  343. WorkingRotation = smoothing ? SmoothTo(WorkingRotation, GoalRotation, SolverHandler.DeltaTime, rotateLerpTime) : GoalRotation;
  344. }
  345. /// <summary>
  346. /// Updates only the working scale to goal with smoothing, if enabled
  347. /// </summary>
  348. public void UpdateWorkingScaleToGoal()
  349. {
  350. WorkingScale = smoothing ? SmoothTo(WorkingScale, GoalScale, SolverHandler.DeltaTime, scaleLerpTime) : GoalScale;
  351. }
  352. }
  353. }