NativeInterface_IMU.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. using Rokid.UXR.Utility;
  5. //using Google.XR.Cardboard;
  6. namespace Rokid.UXR.Native
  7. {
  8. public partial class NativeInterface
  9. {
  10. public partial class NativeAPI
  11. {
  12. /// <summary>
  13. /// Glasses IMU update
  14. /// Acc Gyt Gnt timeStamp
  15. /// </summary>
  16. public static event Action<float[], float[], float[], long> OnGlassIMUSensorUpdate;
  17. /// <summary>
  18. /// Glasses IMU rotation callback
  19. /// Input Value GameRotation Rotation timeStamp
  20. /// </summary>
  21. public static event Action<float[], float[], long> OnGlassIMURotationUpdate;
  22. /// <summary>
  23. /// Register glasses IMU rotation
  24. /// </summary>
  25. public static void RegisterRotationEvent()
  26. {
  27. if (!Utils.IsAndroidPlatfrom())
  28. {
  29. return;
  30. }
  31. setOnRotationUpdate(OnRotationUpdateCallByC);//注册
  32. }
  33. /// <summary>
  34. /// Unregister glasses IMU rotation
  35. /// </summary>
  36. public static void UnregisterRotationEvent()
  37. {
  38. if (!Utils.IsAndroidPlatfrom())
  39. {
  40. return;
  41. }
  42. clearOnRotationUpdate();
  43. }
  44. /// <summary>
  45. /// Open phone IMU tracking
  46. /// </summary>
  47. public static void OpenPhoneTracker()
  48. {
  49. if (Utils.IsAndroidPlatfrom())
  50. Api.openPhoneTracker();
  51. }
  52. /// <summary>
  53. /// Close phone IMU tracking
  54. /// </summary>
  55. public static void ClosePhoneTracker()
  56. {
  57. if (Utils.IsAndroidPlatfrom())
  58. Api.closePhoneTracker();
  59. }
  60. /// <summary>
  61. /// Get phone IMU pose
  62. /// </summary>
  63. /// <param name="oritation"></param>
  64. public static void GetPhonePose(float[] oritation)
  65. {
  66. if (Utils.IsAndroidPlatfrom())
  67. Api.getPhonePose(oritation);
  68. }
  69. /// <summary>
  70. /// Reset the y-axis of the phone's IMU
  71. /// </summary>
  72. public static void RecenterPhonePose()
  73. {
  74. if (Utils.IsAndroidPlatfrom())
  75. Api.recenterPhonePose();
  76. }
  77. /// <summary>
  78. /// Reset the three axes of the phone's IMU
  79. /// </summary>
  80. public static void RecenterPhonePoseYPR()
  81. {
  82. if (Utils.IsAndroidPlatfrom())
  83. Api.recenterPhonePoseYPR();
  84. }
  85. /// <summary>
  86. /// Register glasses imu sensor events
  87. /// </summary>
  88. public static void RegisterGlassSensorEvent()
  89. {
  90. if (!Utils.IsAndroidPlatfrom())
  91. {
  92. return;
  93. }
  94. setOnSensorUpdate(OnSensorUpdateCallByC);//注册
  95. }
  96. /// <summary>
  97. /// Unregister glasses imu sensor events
  98. /// </summary>
  99. public static void UnregisterGlassSensorEvent()
  100. {
  101. if (!Utils.IsAndroidPlatfrom())
  102. {
  103. return;
  104. }
  105. clearOnSensorUpdate();//注销
  106. }
  107. #region NativeInterface
  108. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  109. static extern void setOnSensorUpdate(OnSensorUpdate cb);
  110. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  111. static extern void clearOnSensorUpdate();
  112. delegate void OnSensorUpdate(IntPtr ptrAcc, IntPtr ptrGyt, IntPtr ptrGnt, long timestamp);
  113. static float[] resultAcc = new float[3];
  114. static float[] resultGyt = new float[3];
  115. static float[] resultGnt = new float[3];
  116. [MonoPInvokeCallback(typeof(OnSensorUpdate))]
  117. static void OnSensorUpdateCallByC(IntPtr ptrAcc, IntPtr ptrGyr, IntPtr ptrGnt, long timestamp)
  118. {
  119. RKLog.Debug("OnSensorUpdateCallByC " + timestamp);
  120. Marshal.Copy(ptrAcc, resultAcc, 0, 3);
  121. Marshal.Copy(ptrGyr, resultGyt, 0, 3);
  122. Marshal.Copy(ptrGnt, resultGnt, 0, 3);
  123. OnGlassIMUSensorUpdate?.Invoke(resultAcc, resultGyt, resultGnt, timestamp);
  124. }
  125. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  126. static extern void clearOnRotationUpdate();
  127. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  128. static extern void setOnRotationUpdate(OnRotationUpdate cb);
  129. delegate void OnRotationUpdate(IntPtr ptrGameRotation, IntPtr ptrRotation, long timestamp);
  130. static float[] resultGameRotation = new float[4];
  131. static float[] resultRotation = new float[5];
  132. [MonoPInvokeCallback(typeof(OnRotationUpdate))]
  133. static void OnRotationUpdateCallByC(IntPtr ptrGameRotation, IntPtr ptrRotation, long timestamp)
  134. {
  135. RKLog.Debug("OnRotationUpdateCallByC: " + timestamp);
  136. Marshal.Copy(ptrGameRotation, resultGameRotation, 0, 4);
  137. Marshal.Copy(ptrRotation, resultRotation, 0, 5);
  138. OnGlassIMURotationUpdate?.Invoke(resultGameRotation, resultRotation, timestamp);
  139. }
  140. }
  141. #endregion
  142. }
  143. }