FrontBodyAnchor.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.Interaction.Toolkit;
  5. namespace Ximmerse.XR.Interactions
  6. {
  7. /// <summary>
  8. /// 身体前向锚点.
  9. /// 代表XR用户身体前面一定偏移量的坐标锚点。
  10. /// </summary>
  11. public class FrontBodyAnchor : MonoBehaviour
  12. {
  13. #if UNITY_ANDROID || UNITY_EDITOR
  14. public static FrontBodyAnchor instance
  15. {
  16. get; private set;
  17. }
  18. Camera m_MainCamera;
  19. public Transform mainCameraT
  20. {
  21. get
  22. {
  23. if(!m_MainCamera)
  24. {
  25. m_MainCamera = Camera.main;
  26. }
  27. return m_MainCamera.transform;
  28. }
  29. }
  30. [SerializeField]
  31. [Tooltip("Distance along Z-axis from head to dock position.")]
  32. float m_ZDepth = 4;
  33. /// <summary>
  34. /// Z-depth : canvas z-distance to head.
  35. /// </summary>
  36. /// <value>The z depth.</value>
  37. public float Z_Depth
  38. {
  39. get
  40. {
  41. return m_ZDepth;
  42. }
  43. set
  44. {
  45. m_ZDepth = value;
  46. }
  47. }
  48. [SerializeField, Tooltip("Force docking every frame")]
  49. bool m_ForceEveryFrame;
  50. /// <summary>
  51. /// Gets or sets a value indicating whether this <see cref="T:Ximmerse.RhinoX.FrontDocker"/> force every frame.
  52. /// </summary>
  53. /// <value><c>true</c> if force every frame; otherwise, <c>false</c>.</value>
  54. public bool ForceEveryFrame
  55. {
  56. get
  57. {
  58. return m_ForceEveryFrame;
  59. }
  60. set
  61. {
  62. m_ForceEveryFrame = value;
  63. }
  64. }
  65. [SerializeField, Tooltip("Angular diff error to re-dock canvas, smaller values cause frequent re-docking.")]
  66. float m_AngleDiffError = 35;
  67. /// <summary>
  68. /// Angular diff error to re-dock canvas, smaller values cause frequent re-docking.
  69. /// </summary>
  70. /// <value>The diff error.</value>
  71. public float AngleDiffError
  72. {
  73. get
  74. {
  75. return m_AngleDiffError;
  76. }
  77. set
  78. {
  79. m_AngleDiffError = value;
  80. }
  81. }
  82. [SerializeField, Tooltip("Delay time to start docking.")]
  83. float m_DockDelay = 0.5f;
  84. /// <summary>
  85. /// Gets or sets the dock delay.
  86. /// </summary>
  87. /// <value>The dock delay.</value>
  88. public float DockDelay
  89. {
  90. get
  91. {
  92. return m_DockDelay;
  93. }
  94. set
  95. {
  96. m_DockDelay = value;
  97. }
  98. }
  99. [SerializeField, Min(0.1f)]
  100. [Tooltip("The smaller damp time is , the faster the transform dock to eye's front.")]
  101. float m_DampTime = 0.3f;
  102. /// <summary>
  103. /// Gets or sets the damp time. The smaller damp time is , the faster the transform dock to eye's front.
  104. /// </summary>
  105. /// <value>The damp time.</value>
  106. public float DampTime
  107. {
  108. get
  109. {
  110. return m_DampTime;
  111. }
  112. set
  113. {
  114. m_DampTime = value;
  115. }
  116. }
  117. [SerializeField]
  118. //[Tooltip("The smaller damp time is , the faster the transform dock to eye's front.")]
  119. bool m_FollowWithPitch;
  120. /// <summary>
  121. /// Gets or sets the damp time. The smaller damp time is , the faster the transform dock to eye's front.
  122. /// </summary>
  123. /// <value>The damp time.</value>
  124. public bool FollowWithPitch
  125. {
  126. get
  127. {
  128. return m_FollowWithPitch;
  129. }
  130. set
  131. {
  132. m_FollowWithPitch = value;
  133. }
  134. }
  135. /// <summary>
  136. /// offset of docker position.
  137. /// modify by leaf.2019.08.30
  138. /// </summary>
  139. public Vector3 PositionOffset;
  140. bool m_Docking = false;
  141. float dampSpeed = 0;
  142. float dockEulerY;
  143. float dockEulerX;
  144. float? m_DockingStartTime;
  145. Transform m_HeadTransform;
  146. Transform headTransform
  147. {
  148. get
  149. {
  150. if (m_HeadTransform == null)
  151. {
  152. m_HeadTransform = Camera.main.transform;
  153. }
  154. return m_HeadTransform;
  155. }
  156. }
  157. private void Awake()
  158. {
  159. instance = this;
  160. }
  161. // Start is called before the first frame update
  162. IEnumerator Start()
  163. {
  164. while (!mainCameraT)
  165. {
  166. yield return null;
  167. }
  168. //calculate dock position and rotation:
  169. Quaternion fwdQ = Quaternion.Euler(0, headTransform.eulerAngles.y, 0);
  170. transform.position = headTransform.position + fwdQ * Vector3.forward * m_ZDepth;
  171. transform.rotation = fwdQ;
  172. m_Docking = false;
  173. dockEulerY = headTransform.eulerAngles.y;
  174. dockEulerX = headTransform.eulerAngles.x;
  175. }
  176. // Update is called once per frame
  177. void LateUpdate()
  178. {
  179. var headT = headTransform;
  180. float headEulerY = headT.eulerAngles.y;
  181. float headEulerX = headT.eulerAngles.x;
  182. float eulerYDiff = Quaternion.Angle(Quaternion.Euler(0, headEulerY, 0), Quaternion.Euler(0, dockEulerY, 0));
  183. float eulerXDiff = Quaternion.Angle(Quaternion.Euler(headEulerX, 0, 0), Quaternion.Euler(dockEulerX, 0, 0));
  184. if (m_ForceEveryFrame || Mathf.Abs(eulerYDiff) >= m_AngleDiffError || (FollowWithPitch && Mathf.Abs(eulerXDiff) >= m_AngleDiffError))
  185. {
  186. m_Docking = true;
  187. if (m_DockingStartTime.HasValue == false)
  188. {
  189. m_DockingStartTime = Time.time + m_DockDelay;
  190. }
  191. }
  192. if (m_Docking)
  193. {
  194. if (m_DockingStartTime.HasValue && Time.time < m_DockingStartTime.Value)
  195. {
  196. //wait for delay time
  197. }
  198. else
  199. {
  200. if (m_DampTime > 0)
  201. {
  202. dockEulerY = Mathf.SmoothDampAngle(dockEulerY, headEulerY, ref dampSpeed, m_DampTime);
  203. float eulerYDiff2 = Quaternion.Angle(Quaternion.Euler(0, headEulerY, 0), Quaternion.Euler(0, dockEulerY, 0));
  204. if (eulerYDiff2 <= 2.5f)
  205. {
  206. m_Docking = false;
  207. m_DockingStartTime = null;//reset docking start time
  208. }
  209. if (FollowWithPitch)
  210. {
  211. dockEulerX = Mathf.SmoothDampAngle(dockEulerX, headEulerX, ref dampSpeed, m_DampTime);
  212. float eulerXDiff2 = Quaternion.Angle(Quaternion.Euler(0, headEulerX, 0), Quaternion.Euler(0, dockEulerX, 0));
  213. if (eulerXDiff2 <= 2.5f)
  214. {
  215. m_Docking = false;
  216. m_DockingStartTime = null;//reset docking start time
  217. }
  218. }
  219. }
  220. else
  221. {
  222. dockEulerY = headEulerY;
  223. if (FollowWithPitch)
  224. {
  225. dockEulerX = headEulerX;
  226. }
  227. }
  228. }
  229. if (FollowWithPitch)
  230. {
  231. transform.rotation = Quaternion.Euler(dockEulerX, dockEulerY, 0);
  232. }
  233. else
  234. {
  235. transform.rotation = Quaternion.Euler(0, dockEulerY, 0);
  236. }
  237. }
  238. //Update pos:
  239. // modify by leaf.2019.08.30
  240. if (FollowWithPitch)
  241. {
  242. transform.position = headTransform.position + Quaternion.Euler(dockEulerX, dockEulerY, 0) * Vector3.forward * m_ZDepth + PositionOffset;
  243. }
  244. else
  245. {
  246. transform.position = headTransform.position + Quaternion.Euler(0, dockEulerY, 0) * Vector3.forward * m_ZDepth + PositionOffset;
  247. }
  248. }
  249. #endif
  250. }
  251. }