RKCameraRig.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Rokid.UXR.Native;
  2. using Rokid.UXR.Utility;
  3. using UnityEngine;
  4. using UnityEngine.SpatialTracking;
  5. namespace Rokid.UXR.Module
  6. {
  7. public class RKCameraRig : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// This enum is used to indicate which parts of the pose will be applied to the parent transform
  11. /// </summary>
  12. public enum HeadTrackingType
  13. {
  14. /// <summary>
  15. /// With this setting, both the pose's rotation and position will be applied to the parent transform
  16. /// </summary>
  17. RotationAndPosition = 0,
  18. /// <summary>
  19. /// With this setting, only the pose's rotation will be applied to the parent transform
  20. /// </summary>
  21. RotationOnly = 1,
  22. /// <summary>
  23. /// With this setting, only the pose's position will be applied to the parent transform
  24. /// </summary>
  25. PositionOnly = 2,
  26. /// <summary>
  27. /// With this setting, none value will be applied to the parent transform
  28. /// </summary>
  29. None = 3
  30. }
  31. [SerializeField]
  32. HeadTrackingType m_HeadTrackingType;
  33. HeadTrackingType m_PreHeadTrackingType;
  34. /// <summary>
  35. /// The tracking type being used by the tracked pose driver
  36. /// </summary>
  37. public HeadTrackingType headTrackingType
  38. {
  39. get { return m_HeadTrackingType; }
  40. set { m_HeadTrackingType = value; }
  41. }
  42. /// <summary>
  43. /// The update type being used by the tracked pose driver
  44. /// </summary>
  45. public enum UpdateType
  46. {
  47. /// <summary>
  48. /// Sample input at both update, and directly before rendering. For smooth head pose tracking,
  49. /// we recommend using this value as it will provide the lowest input latency for the device.
  50. /// This is the default value for the UpdateType option
  51. /// </summary>
  52. UpdateAndBeforeRender,
  53. /// <summary>
  54. /// Only sample input during the update phase of the frame.
  55. /// </summary>
  56. Update,
  57. /// <summary>
  58. /// Only sample input directly before rendering
  59. /// </summary>
  60. BeforeRender,
  61. }
  62. [SerializeField]
  63. UpdateType m_UpdateType;
  64. UpdateType m_PreUpdateType;
  65. public UpdateType updateType
  66. {
  67. get
  68. {
  69. return m_UpdateType;
  70. }
  71. set
  72. {
  73. m_UpdateType = value;
  74. }
  75. }
  76. private TrackedPoseDriver m_TrackedPoseDriver;
  77. private void OnRenderImage(RenderTexture src, RenderTexture dest)
  78. {
  79. Graphics.Blit(src, dest);
  80. }
  81. private void Start()
  82. {
  83. if (Utils.IsAndroidPlatfrom())
  84. {
  85. // GetComponent<Camera>().fieldOfView = NativeInterface.NativeAPI.GetVFov(true);
  86. m_TrackedPoseDriver = gameObject.GetComponent<TrackedPoseDriver>();
  87. if (m_TrackedPoseDriver == null)
  88. {
  89. m_TrackedPoseDriver = gameObject.AddComponent<TrackedPoseDriver>();
  90. }
  91. if (m_HeadTrackingType != HeadTrackingType.None)
  92. {
  93. switch (m_HeadTrackingType)
  94. {
  95. case HeadTrackingType.RotationAndPosition:
  96. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;
  97. break;
  98. case HeadTrackingType.RotationOnly:
  99. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;
  100. break;
  101. case HeadTrackingType.PositionOnly:
  102. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.PositionOnly;
  103. break;
  104. }
  105. }
  106. else
  107. {
  108. m_TrackedPoseDriver.enabled = false;
  109. }
  110. RKLog.Info($"====RKCameraRig====: SetHeadTrackingType :{m_HeadTrackingType}");
  111. NativeInterface.NativeAPI.SetTrackingType((int)m_HeadTrackingType);
  112. m_PreHeadTrackingType = m_HeadTrackingType;
  113. m_PreUpdateType = m_UpdateType;
  114. }
  115. }
  116. private void Update()
  117. {
  118. if (Utils.IsAndroidPlatfrom())
  119. {
  120. if (m_PreHeadTrackingType != m_HeadTrackingType)
  121. {
  122. m_PreHeadTrackingType = m_HeadTrackingType;
  123. if (m_HeadTrackingType == HeadTrackingType.None)
  124. {
  125. m_TrackedPoseDriver.enabled = false;
  126. }
  127. else
  128. {
  129. m_TrackedPoseDriver.enabled = true;
  130. switch (m_HeadTrackingType)
  131. {
  132. case HeadTrackingType.RotationAndPosition:
  133. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;
  134. break;
  135. case HeadTrackingType.RotationOnly:
  136. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;
  137. break;
  138. case HeadTrackingType.PositionOnly:
  139. m_TrackedPoseDriver.trackingType = TrackedPoseDriver.TrackingType.PositionOnly;
  140. break;
  141. }
  142. }
  143. NativeInterface.NativeAPI.SetTrackingType((int)m_HeadTrackingType);
  144. }
  145. if (m_PreUpdateType != m_UpdateType)
  146. {
  147. m_PreUpdateType = m_UpdateType;
  148. m_TrackedPoseDriver.updateType = (TrackedPoseDriver.UpdateType)m_UpdateType;
  149. }
  150. }
  151. }
  152. private void OnApplicationPause(bool pauseStatus)
  153. {
  154. if (pauseStatus == false)
  155. {
  156. NativeInterface.NativeAPI.SetTrackingType((int)m_HeadTrackingType);
  157. }
  158. }
  159. }
  160. }