RadialView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 UnityEngine;
  4. namespace Microsoft.MixedReality.Toolkit.Utilities.Solvers
  5. {
  6. /// <summary>
  7. /// RadialViewPoser solver locks a tag-along type object within a view cone
  8. /// </summary>
  9. [AddComponentMenu("Scripts/MRTK/SDK/RadialView")]
  10. public class RadialView : Solver
  11. {
  12. [SerializeField]
  13. [Tooltip("Which direction to position the element relative to: HeadOriented rolls with the head, HeadFacingWorldUp view direction but ignores head roll, and HeadMoveDirection uses the direction the head last moved without roll")]
  14. private RadialViewReferenceDirection referenceDirection = RadialViewReferenceDirection.FacingWorldUp;
  15. /// <summary>
  16. /// Which direction to position the element relative to:
  17. /// HeadOriented rolls with the head,
  18. /// HeadFacingWorldUp view direction but ignores head roll,
  19. /// and HeadMoveDirection uses the direction the head last moved without roll.
  20. /// </summary>
  21. public RadialViewReferenceDirection ReferenceDirection
  22. {
  23. get => referenceDirection;
  24. set => referenceDirection = value;
  25. }
  26. public Vector3 headOffset = Vector3.up * 0.2f;
  27. [SerializeField]
  28. [Tooltip("Min distance from eye to position element around, i.e. the sphere radius")]
  29. private float minDistance = 1f;
  30. /// <summary>
  31. /// Min distance from eye to position element around, i.e. the sphere radius.
  32. /// </summary>
  33. public float MinDistance
  34. {
  35. get => minDistance;
  36. set => minDistance = value;
  37. }
  38. [SerializeField]
  39. [Tooltip("Max distance from eye to element")]
  40. private float maxDistance = 2f;
  41. /// <summary>
  42. /// Max distance from eye to element.
  43. /// </summary>
  44. public float MaxDistance
  45. {
  46. get => maxDistance;
  47. set => maxDistance = value;
  48. }
  49. [SerializeField]
  50. [Tooltip("The element will stay at least this far away from the center of view")]
  51. private float minViewDegrees = 0f;
  52. /// <summary>
  53. /// The element will stay at least this far away from the center of view.
  54. /// </summary>
  55. public float MinViewDegrees
  56. {
  57. get => minViewDegrees;
  58. set => minViewDegrees = value;
  59. }
  60. [SerializeField]
  61. [Tooltip("The element will stay at least this close to the center of view")]
  62. private float maxViewDegrees = 30f;
  63. /// <summary>
  64. /// The element will stay at least this close to the center of view.
  65. /// </summary>
  66. public float MaxViewDegrees
  67. {
  68. get => maxViewDegrees;
  69. set => maxViewDegrees = value;
  70. }
  71. [SerializeField]
  72. [Tooltip("Apply a different clamp to vertical FOV than horizontal. Vertical = Horizontal * aspectV")]
  73. private float aspectV = 1f;
  74. /// <summary>
  75. /// Apply a different clamp to vertical FOV than horizontal. Vertical = Horizontal * AspectV.
  76. /// </summary>
  77. public float AspectV
  78. {
  79. get => aspectV;
  80. set => aspectV = value;
  81. }
  82. [SerializeField]
  83. [Tooltip("Option to ignore angle clamping")]
  84. private bool ignoreAngleClamp = false;
  85. /// <summary>
  86. /// Option to ignore angle clamping.
  87. /// </summary>
  88. public bool IgnoreAngleClamp
  89. {
  90. get => ignoreAngleClamp;
  91. set => ignoreAngleClamp = value;
  92. }
  93. [SerializeField]
  94. [Tooltip("Option to ignore distance clamping")]
  95. private bool ignoreDistanceClamp = false;
  96. /// <summary>
  97. /// Option to ignore distance clamping.
  98. /// </summary>
  99. public bool IgnoreDistanceClamp
  100. {
  101. get => ignoreDistanceClamp;
  102. set => ignoreDistanceClamp = value;
  103. }
  104. [SerializeField]
  105. [Tooltip("Ignore vertical movement and lock the Y position of the object")]
  106. private bool useFixedVerticalPosition = false;
  107. /// <summary>
  108. /// Ignore vertical movement and lock the Y position of the object.
  109. /// </summary>
  110. public bool UseFixedVerticalPosition
  111. {
  112. get => useFixedVerticalPosition;
  113. set => useFixedVerticalPosition = value;
  114. }
  115. [SerializeField]
  116. [Tooltip("Offset amount of the vertical position")]
  117. private float fixedVerticalPosition = -0.4f;
  118. /// <summary>
  119. /// Offset amount of the vertical position.
  120. /// </summary>
  121. public float FixedVerticalPosition
  122. {
  123. get => fixedVerticalPosition;
  124. set => fixedVerticalPosition = value;
  125. }
  126. [SerializeField]
  127. [Tooltip("If true, element will orient to ReferenceDirection, otherwise it will orient to ref position.")]
  128. private bool orientToReferenceDirection = false;
  129. /// <summary>
  130. /// If true, element will orient to ReferenceDirection, otherwise it will orient to ref position.
  131. /// </summary>
  132. public bool OrientToReferenceDirection
  133. {
  134. get => orientToReferenceDirection;
  135. set => orientToReferenceDirection = value;
  136. }
  137. /// <summary>
  138. /// Position to the view direction, or the movement direction, or the direction of the view cone.
  139. /// </summary>
  140. private Vector3 SolverReferenceDirection => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.forward : Vector3.forward;
  141. /// <summary>
  142. /// The up direction to use for orientation.
  143. /// </summary>
  144. /// <remarks>Cone may roll with head, or not.</remarks>
  145. private Vector3 UpReference
  146. {
  147. get
  148. {
  149. Vector3 upReference = Vector3.up;
  150. if (referenceDirection == RadialViewReferenceDirection.ObjectOriented)
  151. {
  152. upReference = SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.up : Vector3.up;
  153. }
  154. return upReference;
  155. }
  156. }
  157. private Vector3 ReferencePoint => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.position : Vector3.zero;
  158. /// <inheritdoc />
  159. public override void SolverUpdate()
  160. {
  161. Vector3 goalPosition = WorkingPosition;
  162. if (ignoreAngleClamp)
  163. {
  164. if (ignoreDistanceClamp)
  165. {
  166. goalPosition = transform.position;
  167. }
  168. else
  169. {
  170. GetDesiredOrientation_DistanceOnly(ref goalPosition);
  171. }
  172. }
  173. else
  174. {
  175. GetDesiredOrientation(ref goalPosition);
  176. }
  177. // Element orientation
  178. Vector3 refDirUp = UpReference;
  179. Quaternion goalRotation;
  180. if (orientToReferenceDirection)
  181. {
  182. goalRotation = Quaternion.LookRotation(SolverReferenceDirection, refDirUp);
  183. }
  184. else
  185. {
  186. goalRotation = Quaternion.LookRotation(goalPosition - ReferencePoint, refDirUp);
  187. }
  188. // If gravity aligned then zero out the x and z axes on the rotation
  189. if (referenceDirection == RadialViewReferenceDirection.GravityAligned)
  190. {
  191. goalRotation.x = goalRotation.z = 0f;
  192. }
  193. if (UseFixedVerticalPosition)
  194. {
  195. goalPosition.y = ReferencePoint.y + FixedVerticalPosition;
  196. }
  197. if (goalPosition.y > ReferencePoint.y - headOffset.y)
  198. {
  199. goalPosition.y = ReferencePoint.y - headOffset.y;
  200. }
  201. GoalPosition = goalPosition;
  202. GoalRotation = goalRotation;
  203. }
  204. /// <summary>
  205. /// Optimized version of GetDesiredOrientation.
  206. /// </summary>
  207. private void GetDesiredOrientation_DistanceOnly(ref Vector3 desiredPos)
  208. {
  209. // TODO: There should be a different solver for distance constraint.
  210. // Determine reference locations and directions
  211. Vector3 refPoint = ReferencePoint;
  212. Vector3 elementPoint = transform.position;
  213. Vector3 elementDelta = elementPoint - refPoint;
  214. float elementDist = elementDelta.magnitude;
  215. Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one;
  216. // Clamp distance too
  217. float clampedDistance = Mathf.Clamp(elementDist, minDistance, maxDistance);
  218. if (!clampedDistance.Equals(elementDist))
  219. {
  220. desiredPos = refPoint + clampedDistance * elementDir;
  221. }
  222. }
  223. private void GetDesiredOrientation(ref Vector3 desiredPos)
  224. {
  225. // Determine reference locations and directions
  226. Vector3 direction = SolverReferenceDirection;
  227. Vector3 upDirection = UpReference;
  228. Vector3 referencePoint = ReferencePoint;
  229. Vector3 elementPoint = transform.position;
  230. Vector3 elementDelta = elementPoint - referencePoint;
  231. float elementDist = elementDelta.magnitude;
  232. Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one;
  233. // Generate basis: First get axis perpendicular to reference direction pointing toward element
  234. Vector3 perpendicularDirection = (elementDir - direction);
  235. perpendicularDirection -= direction * Vector3.Dot(perpendicularDirection, direction);
  236. perpendicularDirection.Normalize();
  237. // Calculate the clamping angles, accounting for aspect (need the angle relative to view plane)
  238. float heightToViewAngle = Vector3.Angle(perpendicularDirection, upDirection);
  239. float verticalAspectScale = Mathf.Lerp(aspectV, 1f, Mathf.Abs(Mathf.Sin(heightToViewAngle * Mathf.Deg2Rad)));
  240. // Calculate the current angle
  241. float currentAngle = Vector3.Angle(elementDir, direction);
  242. float currentAngleClamped = Mathf.Clamp(currentAngle, minViewDegrees * verticalAspectScale, maxViewDegrees * verticalAspectScale);
  243. // Clamp distance too, if desired
  244. float clampedDistance = ignoreDistanceClamp ? elementDist : Mathf.Clamp(elementDist, minDistance, maxDistance);
  245. // If the angle was clamped, do some special update stuff
  246. if (currentAngle != currentAngleClamped)
  247. {
  248. float angRad = currentAngleClamped * Mathf.Deg2Rad;
  249. // Calculate new position
  250. desiredPos = referencePoint + clampedDistance * (direction * Mathf.Cos(angRad) + perpendicularDirection * Mathf.Sin(angRad));
  251. }
  252. else if (!clampedDistance.Equals(elementDist))
  253. {
  254. // Only need to apply distance
  255. desiredPos = referencePoint + clampedDistance * elementDir;
  256. }
  257. }
  258. }
  259. }