NxrHead.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using NibiruAxis;
  15. using UnityEngine;
  16. /// This script provides head tracking support for a camera.
  17. ///
  18. /// Attach this script to any game object that should match the user's head motion.
  19. /// By default, it continuously updates the local transform to NxrViewer.HeadView.
  20. /// A target object may be specified to provide an alternate reference frame for the motion.
  21. ///
  22. /// This script will typically be attached directly to a _Camera_ object, or to its
  23. /// parent if you need to offset the camera from the origin.
  24. /// Alternatively it can be inserted as a child of the _Camera_ but parent of the
  25. /// NxrEye camera. Do this if you already have steering logic driving the
  26. /// mono Camera and wish to have the user's head motion be relative to that. Note
  27. /// that in the latter setup, head tracking is visible only when VR Mode is enabled.
  28. ///
  29. /// In some cases you may need two instances of NxrHead, referring to two
  30. /// different targets (one of which may be the parent), in order to split where
  31. /// the rotation is applied from where the positional offset is applied. Use the
  32. /// #trackRotation and #trackPosition properties in this case.
  33. namespace Nxr.Internal
  34. {
  35. [AddComponentMenu("NXR/NxrHead")]
  36. public class NxrHead : MonoBehaviour
  37. {
  38. public Vector3 BasePosition { set; get; }
  39. /// Determines whether to apply the user's head rotation to this gameobject's
  40. /// orientation. True means to update the gameobject's orientation with the
  41. /// user's head rotation, and false means don't modify the gameobject's orientation.
  42. private bool trackRotation = true;
  43. /// Determines whether to apply ther user's head offset to this gameobject's
  44. /// position. True means to update the gameobject's position with the user's head offset,
  45. /// and false means don't modify the gameobject's position.
  46. private bool trackPosition = false;
  47. /// <summary>
  48. /// Whether track position
  49. /// </summary>
  50. /// <param name="b"></param>
  51. public void SetTrackPosition(bool b)
  52. {
  53. Debug.Log("NxrHead.SetTrackPosition." + b);
  54. trackPosition = b;
  55. }
  56. /// <summary>
  57. /// Whether track rotation
  58. /// </summary>
  59. /// <param name="b"></param>
  60. public void SetTrackRotation(bool b)
  61. {
  62. trackRotation = b;
  63. }
  64. public bool IsTrackRotation()
  65. {
  66. return trackRotation;
  67. }
  68. public bool IsTrackPosition()
  69. {
  70. return trackPosition;
  71. }
  72. public void Update3rdPartyPosition(Vector3 pos)
  73. {
  74. if (NxrViewer.Instance.UseThirdPartyPosition)
  75. {
  76. mTransform.position = pos;
  77. }
  78. }
  79. void Awake()
  80. {
  81. NxrViewer.Create();
  82. }
  83. protected Transform mTransform;
  84. public Transform GetTransform()
  85. {
  86. return mTransform;
  87. }
  88. void Start()
  89. {
  90. mTransform = this.transform;
  91. }
  92. // Normally, update head pose now.
  93. void LateUpdate()
  94. {
  95. NxrViewer.Instance.UpdateHeadPose();
  96. UpdateHead();
  97. }
  98. // 初始的Yaw欧拉角,有时进入时正方向有偏转,此时需要校正一下
  99. private float initEulerYAngle = float.MaxValue;
  100. private float totalTime = 0;
  101. private bool triggerLerp = false;
  102. // 初次进入,方向回正后,进行reset操作
  103. private bool hasResetTracker = true;
  104. private float moveSpeed = 2.0f;
  105. // Compute new head pose.
  106. private void UpdateHead()
  107. {
  108. if (NxrGlobal.hasInfinityARSDK)
  109. {
  110. trackRotation = false;
  111. trackPosition = false;
  112. }
  113. if (NxrViewer.Instance.GetNibiruService() != null && NxrViewer.Instance.GetNibiruService().IsMarkerRecognizeRunning)
  114. {
  115. if (NxrGlobal.isMarkerVisible)
  116. {
  117. // Marker识别成功时,取消NxrHead的效果
  118. trackRotation = false;
  119. trackPosition = false;
  120. return;
  121. }
  122. else
  123. {
  124. // Marker识别失败时,恢复头部转动效果
  125. trackRotation = true;
  126. trackPosition = false;
  127. }
  128. }
  129. if (trackRotation)
  130. {
  131. float[] eulerRange = NxrViewer.Instance.GetHeadEulerAnglesRange();
  132. Quaternion rot = NxrViewer.Instance.HeadPose.Orientation;
  133. /*
  134. if (rot.eulerAngles.y != 0 && initEulerYAngle == float.MaxValue)
  135. {
  136. initEulerYAngle = rot.eulerAngles.y;
  137. if (float.IsNaN(initEulerYAngle))
  138. {
  139. Debug.Log("DATA IS ABNORMAL--------------------------->>>>>>>>>");
  140. initEulerYAngle = float.MaxValue;
  141. }
  142. }
  143. if (initEulerYAngle != float.MaxValue && NxrViewer.Instance.InitialRecenter && !triggerLerp
  144. && (Mathf.Abs(initEulerYAngle) <= 345 && Mathf.Abs(initEulerYAngle) >= 15))
  145. {
  146. // 初始位置有偏移,只要角度偏差很大时才进行校正操作
  147. triggerLerp = true;
  148. moveSpeed += (Mathf.Abs(initEulerYAngle) > 60 ? 0.3f : Mathf.Abs(initEulerYAngle) > 90 ? 0.5f : 0);
  149. Debug.Log("triggerLerp.yaw=" + initEulerYAngle + ", sp=" + moveSpeed);
  150. }
  151. if (triggerLerp)
  152. {
  153. totalTime += Time.deltaTime * 2.10f;
  154. if (totalTime > 1)
  155. {
  156. if (!hasResetTracker)
  157. {
  158. // 校正完毕,进行reset操作
  159. rot.eulerAngles = new Vector3(rot.eulerAngles.x, rot.eulerAngles.y - initEulerYAngle, rot.eulerAngles.z);
  160. hasResetTracker = true;
  161. NxrViewer.Instance.ResetHeadTrackerFromAndroid();
  162. }
  163. }
  164. else
  165. {
  166. rot.eulerAngles = new Vector3(rot.eulerAngles.x, Mathf.LerpAngle(initEulerYAngle, 0, totalTime), rot.eulerAngles.z);
  167. }
  168. }
  169. */
  170. if(!hasResetTracker)
  171. {
  172. hasResetTracker = true;
  173. NxrViewer.Instance.Recenter();
  174. }
  175. Vector3 eulerAngles = rot.eulerAngles;
  176. if (eulerRange == null ||
  177. (
  178. // 水平有限制
  179. (eulerRange != null && (eulerAngles[1] >= eulerRange[0] || eulerAngles[1] < eulerRange[1]) &&
  180. // 垂直有限制
  181. (eulerAngles[0] >= eulerRange[2] || eulerAngles[0] < eulerRange[3]))
  182. )
  183. )
  184. {
  185. mTransform.localRotation = rot;
  186. }
  187. }
  188. #if UNITY_STANDALONE_WIN || ANDROID_REMOTE_NRR
  189. Vector3 pos = NxrViewer.Instance.HeadPose.Position;
  190. if (pos.x !=0 && pos.y !=0 && pos.z != 0)
  191. {
  192. mTransform.localPosition = BasePosition + pos;
  193. }
  194. #elif UNITY_ANDROID
  195. if (trackPosition)
  196. {
  197. Vector3 pos = NxrViewer.Instance.HeadPose.Position;
  198. mTransform.localPosition = BasePosition + pos;
  199. if (NxrPlayerCtrl.Instance != null)
  200. {
  201. NxrPlayerCtrl.Instance.HeadPosition = mTransform.position;
  202. }
  203. }
  204. #endif
  205. }
  206. public void ResetInitEulerYAngle()
  207. {
  208. initEulerYAngle = 0;
  209. }
  210. #if UNITY_EDITOR
  211. private void Update()
  212. {
  213. Vector3 start = transform.position;
  214. Vector3 vector = transform.TransformDirection(Vector3.forward);
  215. UnityEngine.Debug.DrawRay(start, vector * 20, Color.red);
  216. }
  217. #endif
  218. }
  219. }