NREmulatorHeadPose.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. #if UNITY_EDITOR
  12. using UnityEngine;
  13. public class NREmulatorHeadPose : MonoBehaviour
  14. {
  15. private GameObject m_CameraTarget;
  16. /// <summary> regular speed. </summary>
  17. public float HeadMoveSpeed = 1.0f;
  18. /// <summary> How sensitive it with mouse. </summary>
  19. public float HeadRotateSpeed = 1.0f;
  20. public Pose headPose { get; private set; }
  21. private float m_HeadRotationLerpSpeed = 5f;
  22. private void Start()
  23. {
  24. DontDestroyOnLoad(this);
  25. if (m_CameraTarget == null)
  26. {
  27. m_CameraTarget = new GameObject("NREmulatorCameraTarget");
  28. m_CameraTarget.transform.position = Vector3.zero;
  29. m_CameraTarget.transform.rotation = Quaternion.identity;
  30. DontDestroyOnLoad(m_CameraTarget);
  31. }
  32. NRHMDPoseTracker.OnChangeTrackingMode += OnChangeTrackingMode;
  33. }
  34. private void OnChangeTrackingMode(NRHMDPoseTracker.TrackingType origin, NRHMDPoseTracker.TrackingType target)
  35. {
  36. m_CameraTarget.transform.position = Vector3.zero;
  37. m_CameraTarget.transform.rotation = Quaternion.identity;
  38. headPose = Pose.identity;
  39. }
  40. private void Update()
  41. {
  42. if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift))
  43. {
  44. UpdateHeadPosByInput();
  45. }
  46. }
  47. void UpdateHeadPosByInput()
  48. {
  49. var trackMode = NRSessionManager.Instance.NRHMDPoseTracker.TrackingMode;
  50. if (trackMode == NRHMDPoseTracker.TrackingType.Tracking0Dof)
  51. return;
  52. Vector3 pos = m_CameraTarget.transform.position;
  53. Quaternion q = m_CameraTarget.transform.rotation;
  54. if (Input.GetKey(KeyCode.Space))
  55. {
  56. float mouse_x = Input.GetAxis("Mouse X") * HeadRotateSpeed;
  57. float mouse_y = Input.GetAxis("Mouse Y") * HeadRotateSpeed;
  58. Vector3 mouseMove = new Vector3(m_CameraTarget.transform.eulerAngles.x - mouse_y, m_CameraTarget.transform.eulerAngles.y + mouse_x, 0);
  59. q = Quaternion.Euler(mouseMove);
  60. }
  61. if (trackMode == NRHMDPoseTracker.TrackingType.Tracking0DofStable)
  62. {
  63. q = Quaternion.Slerp(q, Quaternion.identity, m_HeadRotationLerpSpeed * Time.deltaTime);
  64. }
  65. else if (trackMode == NRHMDPoseTracker.TrackingType.Tracking6Dof)
  66. {
  67. Vector3 p = GetBaseInput();
  68. p = p * HeadMoveSpeed * Time.deltaTime;
  69. pos += p;
  70. }
  71. m_CameraTarget.transform.position = pos;
  72. m_CameraTarget.transform.rotation = q;
  73. headPose = new Pose(pos, q);
  74. }
  75. private Vector3 GetBaseInput()
  76. {
  77. Vector3 p_Velocity = new Vector3();
  78. if (Input.GetKey(KeyCode.W))
  79. {
  80. p_Velocity += m_CameraTarget.transform.forward.normalized;
  81. }
  82. if (Input.GetKey(KeyCode.S))
  83. {
  84. p_Velocity += -m_CameraTarget.transform.forward.normalized;
  85. }
  86. if (Input.GetKey(KeyCode.A))
  87. {
  88. p_Velocity += -m_CameraTarget.transform.right.normalized;
  89. }
  90. if (Input.GetKey(KeyCode.D))
  91. {
  92. p_Velocity += m_CameraTarget.transform.right.normalized;
  93. }
  94. return p_Velocity;
  95. }
  96. }
  97. #endif
  98. }