EZXRTrack3dSession.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. namespace EZXR.Glass.Tracking3D
  7. {
  8. public class EZXRTrack3dSession
  9. {
  10. public struct EZXRPose
  11. {
  12. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  13. public float[] transform;
  14. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  15. public float[] quaternion;
  16. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  17. public float[] center;
  18. public double timestamp;
  19. }
  20. public struct EZXR3DObjectAnchor
  21. {
  22. public EZXRPose pose;
  23. [MarshalAs(UnmanagedType.I1)]
  24. public bool isValid;
  25. }
  26. private bool isCreated = false;
  27. private IntPtr imagebufferPtr = IntPtr.Zero;
  28. public void Create(string path)
  29. {
  30. if (isCreated)
  31. return;
  32. isCreated = NativeApi.nativeInitialize3dtracker(path);
  33. }
  34. public void Destroy()
  35. {
  36. if (!isCreated)
  37. return;
  38. NativeApi.nativeDestroy3dtracker();
  39. if (imagebufferPtr != IntPtr.Zero)
  40. {
  41. Marshal.FreeHGlobal(imagebufferPtr);
  42. }
  43. }
  44. public bool GettTrackedAnchorPose(ref Pose pose)
  45. {
  46. pose = Pose.identity;
  47. if (!isCreated)
  48. {
  49. pose = Pose.identity;
  50. return false;
  51. }
  52. EZXR3DObjectAnchor anchor = NativeApi.nativeGetTrackedAnchor();
  53. if (anchor.isValid == true)
  54. {
  55. pose.position = new Vector3(anchor.pose.center[0], anchor.pose.center[1], anchor.pose.center[2]);
  56. pose.rotation = new Quaternion(anchor.pose.quaternion[0], anchor.pose.quaternion[1], anchor.pose.quaternion[2], anchor.pose.quaternion[3]);
  57. }
  58. return anchor.isValid;
  59. }
  60. public void UpdateImage(IntPtr buffer, double timestamp, int width, int height, int format)
  61. {
  62. if (!isCreated)
  63. {
  64. return;
  65. }
  66. if (buffer == null)
  67. return;
  68. NativeApi.nativeOnImageAvailable(buffer.ToInt64(), timestamp, width, height, format);
  69. }
  70. public void UpdateImage(byte[] buffer, double timestamp, int width, int height, int format)
  71. {
  72. if (!isCreated)
  73. {
  74. return;
  75. }
  76. if (buffer == null)
  77. return;
  78. if (buffer.Length == 0)
  79. return;
  80. if (imagebufferPtr == IntPtr.Zero)
  81. {
  82. imagebufferPtr = Marshal.AllocHGlobal(buffer.Length);
  83. }
  84. Marshal.Copy(buffer, 0, imagebufferPtr, buffer.Length);
  85. //Debug.Log("-10001- OnUpdateFrame length=" + buffer.Length + ",size=" + width + "," + height + " | " + imagebufferPtr.ToInt64() + "," + timestamp);
  86. NativeApi.nativeOnImageAvailable(imagebufferPtr.ToInt64(), timestamp, width, height, format);
  87. }
  88. public void SetCameraIntrics(float[] intrics)
  89. {
  90. if (!isCreated)
  91. {
  92. return;
  93. }
  94. NativeApi.nativeSetImageIntrinsic(intrics);
  95. }
  96. public void SetPoseFromHeadToLocCam(float[] pose)
  97. {
  98. if (!isCreated)
  99. {
  100. return;
  101. }
  102. NativeApi.nativeSetRTFromHeadToLocCam(pose);
  103. }
  104. public void UpdateHeadPose(float[] pose, double timestamp)
  105. {
  106. if (!isCreated)
  107. {
  108. return;
  109. }
  110. NativeApi.nativeOnHeadPoseUpdated(pose, timestamp);
  111. }
  112. public void UpdateHeadPose(Pose pose, double timestamp_sec)
  113. {
  114. if (!isCreated)
  115. {
  116. return;
  117. }
  118. if (pose == null)
  119. return;
  120. float[] posearray = new float[7] {
  121. pose.position.x,pose.position.y,pose.position.z,
  122. pose.rotation.x,pose.rotation.y,pose.rotation.z,pose.rotation.w
  123. };
  124. NativeApi.nativeOnHeadPoseUpdated(posearray, timestamp_sec);
  125. }
  126. private partial struct NativeApi
  127. {
  128. #if UNITY_EDITOR
  129. public static bool nativeInitialize3dtracker(string assetpath)
  130. {
  131. return false;
  132. }
  133. public static void nativeDestroy3dtracker()
  134. {
  135. }
  136. public static EZXR3DObjectAnchor nativeGetTrackedAnchor()
  137. {
  138. EZXR3DObjectAnchor temp = new EZXR3DObjectAnchor();
  139. temp.isValid = false;
  140. return temp;
  141. }
  142. public static void nativeOnImageAvailable(Int64 ptr, double timestamp, int width, int height, int format)
  143. {
  144. }
  145. public static void nativeSetImageIntrinsic([Out, In][MarshalAs(UnmanagedType.LPArray, SizeConst = 8)] float[] ptr)
  146. {
  147. }
  148. public static void nativeSetRTFromHeadToLocCam([Out, In][MarshalAs(UnmanagedType.LPArray, SizeConst = 16)] float[] ptr)
  149. {
  150. }
  151. public static bool nativeOnHeadPoseUpdated([MarshalAs(UnmanagedType.LPArray, SizeConst = 7)] float[] ptr, double timestamp)
  152. {
  153. return false;
  154. }
  155. #else
  156. private const string nativelibraryname = "ezglass3dtrack";
  157. [DllImport(nativelibraryname)]
  158. public static extern bool nativeInitialize3dtracker(string assetpath);
  159. [DllImport(nativelibraryname)]
  160. public static extern void nativeDestroy3dtracker();
  161. [DllImport(nativelibraryname)]
  162. public static extern EZXR3DObjectAnchor nativeGetTrackedAnchor();
  163. [DllImport(nativelibraryname)]
  164. public static extern void nativeOnImageAvailable(Int64 ptr, double timestamp, int width, int height, int format);
  165. [DllImport(nativelibraryname)]
  166. public static extern void nativeSetImageIntrinsic([Out, In][MarshalAs(UnmanagedType.LPArray, SizeConst = 8)] float[] ptr);
  167. [DllImport(nativelibraryname)]
  168. public static extern void nativeSetRTFromHeadToLocCam([Out, In][MarshalAs(UnmanagedType.LPArray, SizeConst = 16)] float[] ptr);
  169. [DllImport(nativelibraryname)]
  170. public static extern bool nativeOnHeadPoseUpdated([MarshalAs(UnmanagedType.LPArray, SizeConst = 7)] float[] ptr, double timestamp);
  171. #endif
  172. }
  173. }
  174. }