using System; using System.Runtime.InteropServices; using AOT; using Rokid.UXR.Utility; //using Google.XR.Cardboard; namespace Rokid.UXR.Native { public partial class NativeInterface { public partial class NativeAPI { /// /// Glasses IMU update /// Acc Gyt Gnt timeStamp /// public static event Action OnGlassIMUSensorUpdate; /// /// Glasses IMU rotation callback /// Input Value GameRotation Rotation timeStamp /// public static event Action OnGlassIMURotationUpdate; /// /// Register glasses IMU rotation /// public static void RegisterRotationEvent() { if (!Utils.IsAndroidPlatfrom()) { return; } setOnRotationUpdate(OnRotationUpdateCallByC);//注册 } /// /// Unregister glasses IMU rotation /// public static void UnregisterRotationEvent() { if (!Utils.IsAndroidPlatfrom()) { return; } clearOnRotationUpdate(); } /// /// Open phone IMU tracking /// public static void OpenPhoneTracker() { if (Utils.IsAndroidPlatfrom()) Api.openPhoneTracker(); } /// /// Close phone IMU tracking /// public static void ClosePhoneTracker() { if (Utils.IsAndroidPlatfrom()) Api.closePhoneTracker(); } /// /// Get phone IMU pose /// /// public static void GetPhonePose(float[] oritation) { if (Utils.IsAndroidPlatfrom()) Api.getPhonePose(oritation); } /// /// Reset the y-axis of the phone's IMU /// public static void RecenterPhonePose() { if (Utils.IsAndroidPlatfrom()) Api.recenterPhonePose(); } /// /// Reset the three axes of the phone's IMU /// public static void RecenterPhonePoseYPR() { if (Utils.IsAndroidPlatfrom()) Api.recenterPhonePoseYPR(); } /// /// Register glasses imu sensor events /// public static void RegisterGlassSensorEvent() { if (!Utils.IsAndroidPlatfrom()) { return; } setOnSensorUpdate(OnSensorUpdateCallByC);//注册 } /// /// Unregister glasses imu sensor events /// public static void UnregisterGlassSensorEvent() { if (!Utils.IsAndroidPlatfrom()) { return; } clearOnSensorUpdate();//注销 } #region NativeInterface [DllImport(ApiConstants.UXR_GFX_PLUGIN)] static extern void setOnSensorUpdate(OnSensorUpdate cb); [DllImport(ApiConstants.UXR_GFX_PLUGIN)] static extern void clearOnSensorUpdate(); delegate void OnSensorUpdate(IntPtr ptrAcc, IntPtr ptrGyt, IntPtr ptrGnt, long timestamp); static float[] resultAcc = new float[3]; static float[] resultGyt = new float[3]; static float[] resultGnt = new float[3]; [MonoPInvokeCallback(typeof(OnSensorUpdate))] static void OnSensorUpdateCallByC(IntPtr ptrAcc, IntPtr ptrGyr, IntPtr ptrGnt, long timestamp) { RKLog.Debug("OnSensorUpdateCallByC " + timestamp); Marshal.Copy(ptrAcc, resultAcc, 0, 3); Marshal.Copy(ptrGyr, resultGyt, 0, 3); Marshal.Copy(ptrGnt, resultGnt, 0, 3); OnGlassIMUSensorUpdate?.Invoke(resultAcc, resultGyt, resultGnt, timestamp); } [DllImport(ApiConstants.UXR_GFX_PLUGIN)] static extern void clearOnRotationUpdate(); [DllImport(ApiConstants.UXR_GFX_PLUGIN)] static extern void setOnRotationUpdate(OnRotationUpdate cb); delegate void OnRotationUpdate(IntPtr ptrGameRotation, IntPtr ptrRotation, long timestamp); static float[] resultGameRotation = new float[4]; static float[] resultRotation = new float[5]; [MonoPInvokeCallback(typeof(OnRotationUpdate))] static void OnRotationUpdateCallByC(IntPtr ptrGameRotation, IntPtr ptrRotation, long timestamp) { RKLog.Debug("OnRotationUpdateCallByC: " + timestamp); Marshal.Copy(ptrGameRotation, resultGameRotation, 0, 4); Marshal.Copy(ptrRotation, resultRotation, 0, 5); OnGlassIMURotationUpdate?.Invoke(resultGameRotation, resultRotation, timestamp); } } #endregion } }