NRDataSourceProvider.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using System;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. namespace NRKernal.Experimental.NRExamples
  13. {
  14. public interface IPoseProvider
  15. {
  16. Int64 getRecommendedTimeNanos();
  17. float[] getHeadPose(Int64 nanos);
  18. float[] getEyePoseFromHead(int eye);
  19. float[] getEyeFov(int eye);
  20. byte[] getControllerData();
  21. }
  22. public class NRDataSourceProvider : IPoseProvider
  23. {
  24. private WebXRController controllerInfo;
  25. private byte[] rawData;
  26. public struct PoseInfo
  27. {
  28. public Int64 timestamp;
  29. public Pose pose;
  30. }
  31. public Queue<PoseInfo> m_CachePose;
  32. private const int MaxCacheCount = 10;
  33. public bool GetCachePoseByTime(Int64 timestamp, ref Pose pose)
  34. {
  35. while (m_CachePose.Count != 0)
  36. {
  37. var item = m_CachePose.Dequeue();
  38. if (item.timestamp == timestamp)
  39. {
  40. pose.position = item.pose.position;
  41. pose.rotation = item.pose.rotation;
  42. return true;
  43. }
  44. }
  45. NRDebugger.Info("[NRDataSourceProvider] Missing cache pose:" + timestamp);
  46. return false;
  47. }
  48. public NRDataSourceProvider()
  49. {
  50. controllerInfo = new WebXRController();
  51. m_CachePose = new Queue<PoseInfo>();
  52. }
  53. public float[] getEyeFov(int eye)
  54. {
  55. NativeFov4f fov = new NativeFov4f();
  56. NRFrame.GetEyeFov((NativeDevice)eye, ref fov);
  57. return fov.ToXRFloats();
  58. }
  59. public float[] getEyePoseFromHead(int eye)
  60. {
  61. var eyePoseFromHead = NRFrame.EyePoseFromHead;
  62. if (eye == (int)NativeDevice.LEFT_DISPLAY)
  63. {
  64. return PoseToFloatArray(eyePoseFromHead.LEyePose);
  65. }
  66. else
  67. {
  68. return PoseToFloatArray(eyePoseFromHead.REyePose);
  69. }
  70. }
  71. public float[] getHeadPose(Int64 nanos)
  72. {
  73. Pose pose = Pose.identity;
  74. NRFrame.GetHeadPoseByTime(ref pose, (UInt64)nanos);
  75. if (m_CachePose.Count > MaxCacheCount)
  76. {
  77. m_CachePose.Dequeue();
  78. }
  79. m_CachePose.Enqueue(new PoseInfo()
  80. {
  81. timestamp = nanos,
  82. pose = pose
  83. });
  84. return PoseToFloatArray(pose);
  85. }
  86. private static float[] PoseToFloatArray(Pose pose)
  87. {
  88. NativeMat4f matrix = NativeMat4f.identity;
  89. ConversionUtility.UnityPoseToApiPose(pose, out matrix);
  90. return matrix.ToFloats();
  91. }
  92. public Int64 getRecommendedTimeNanos()
  93. {
  94. ulong timestamp = 0;
  95. if (NRFrame.SessionStatus == SessionState.Running)
  96. {
  97. NRSessionManager.Instance.TrackingSubSystem.GetFramePresentTimeByCount(2, ref timestamp);
  98. }
  99. return (Int64)timestamp;
  100. }
  101. public byte[] getControllerData()
  102. {
  103. var handControllerAnchor = NRInput.DomainHand == ControllerHandEnum.Left ? ControllerAnchorEnum.LeftLaserAnchor : ControllerAnchorEnum.RightLaserAnchor;
  104. Transform laserAnchor = NRInput.AnchorsHelper.GetAnchor(NRInput.RaycastMode == RaycastModeEnum.Gaze ? ControllerAnchorEnum.GazePoseTrackerAnchor : handControllerAnchor);
  105. controllerInfo.UpdateData(
  106. true,
  107. NRInput.DomainHand == ControllerHandEnum.Left,
  108. laserAnchor.transform.position,
  109. laserAnchor.transform.rotation,
  110. NRInput.GetTouch(),
  111. NRVirtualDisplayer.SystemButtonState
  112. );
  113. return controllerInfo.Serialize();
  114. }
  115. }
  116. }