RKSensorAPIDemo.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using Newtonsoft.Json;
  3. using UnityEngine.UI;
  4. using Rokid.UXR.Module;
  5. using Rokid.UXR.Native;
  6. namespace Rokid.UXR.Demo
  7. {
  8. public class RKSensorAPIDemo : AutoInjectBehaviour
  9. {
  10. [SerializeField, Autowrited]
  11. private Text cameraIntrinsices;
  12. [SerializeField, Autowrited]
  13. private Text imuData;
  14. [SerializeField, Autowrited]
  15. private Text trackingStatus;
  16. [SerializeField, Autowrited]
  17. private Text cameraPose;
  18. [SerializeField, Autowrited]
  19. private Text yuvImage;
  20. [SerializeField, Autowrited]
  21. private Text imuFPS;
  22. [SerializeField, Autowrited]
  23. private Text frustum;
  24. private bool deviceInited = false;
  25. private Pose mCameraPose;
  26. private float fps = 60;
  27. void Start()
  28. {
  29. RKSensorAPI.Instance.Initialize(true, true);
  30. RKSensorAPI.OnUsbDeviceInited += OnUsbDeviceInited;
  31. RKSensorAPI.OnIMUDataCallBack += OnIMUDataCallBack;
  32. RKSensorAPI.OnIMUUpdate += OnIMUUpdate;
  33. }
  34. private void OnIMUUpdate(float delta)
  35. {
  36. string text = "IMUFPS: ";
  37. float interp = delta / (0.5f + delta);
  38. float currentFPS = 1.0f / delta;
  39. fps = Mathf.Lerp(fps, currentFPS, interp);
  40. text += Mathf.RoundToInt(fps);
  41. imuFPS.text = text;
  42. }
  43. private void OnDestroy()
  44. {
  45. RKSensorAPI.OnUsbDeviceInited -= OnUsbDeviceInited;
  46. RKSensorAPI.OnIMUDataCallBack -= OnIMUDataCallBack;
  47. RKSensorAPI.OnIMUUpdate -= OnIMUUpdate;
  48. }
  49. private void OnIMUDataCallBack(RKSensorAPI.IMUData imuData)
  50. {
  51. // this.imuData.text = $"IMUData:{JsonUtility.ToJson(imuData)}";
  52. }
  53. private void OnUsbDeviceInited()
  54. {
  55. deviceInited = true;
  56. cameraIntrinsices.text = $"CameraIntrinsices: {NativeInterface.NativeAPI.GetOSTInfo()}";
  57. float[] frustum_left = new float[4];
  58. float[] frustum_right = new float[4];
  59. NativeInterface.NativeAPI.GetUnityFrustum(ref frustum_left, ref frustum_right);
  60. frustum.text = $"frustum_left: {JsonConvert.SerializeObject(frustum_left)} \r\n frustum_right: {JsonConvert.SerializeObject(frustum_right)} ";
  61. }
  62. void Update()
  63. {
  64. if (deviceInited)
  65. {
  66. RKSensorAPI.YUVImage imageData = RKSensorAPI.Instance.GetYUVImage();
  67. if (imageData.success)
  68. {
  69. yuvImage.text = $"YUVImage timeStamp:{RKSensorAPI.Instance.GetYUVImage().timeStamp}";
  70. }
  71. mCameraPose = NativeInterface.NativeAPI.GetCameraPhysicsPose(out long timeStamp);
  72. cameraPose.text = $"CameraPose eulerAngles:{mCameraPose.rotation.eulerAngles},pos:{mCameraPose.position}";
  73. this.imuData.text = $"IMUData:{JsonUtility.ToJson(RKSensorAPI.Instance.GetIMUData())}";
  74. }
  75. trackingStatus.text = $"HeadTrackingStatus:{NativeInterface.NativeAPI.GetHeadTrackingStatus()}";
  76. }
  77. }
  78. }