XimmerseXR.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Scripting;
  5. using Unity.Collections;
  6. using Unity.Jobs;
  7. using System.IO;
  8. using System;
  9. using Ximmerse.XR.Utils;
  10. using Ximmerse.XR.Internal;
  11. using SXR;
  12. using Ximmerse.XR.Tag;
  13. namespace Ximmerse.XR
  14. {
  15. /// <summary>
  16. /// Ximmerse XR public interface
  17. /// </summary>
  18. public static class XimmerseXR
  19. {
  20. /// <summary>
  21. /// Gets the front end RGB camera's texture.
  22. /// If it's null, calls RequestWebCamTexture() to request one.
  23. /// </summary>
  24. public static WebCamTexture RGBCameraTexture
  25. {
  26. get; private set;
  27. }
  28. /// <summary>
  29. /// Starts internal web camera rgb camera texture.
  30. /// </summary>
  31. /// <param name="width"></param>
  32. /// <param name="height"></param>
  33. /// <param name="fps"></param>
  34. internal static void RequestOpenRGBCamera(int width, int height, int fps = 60)
  35. {
  36. WebCamDevice[] devices = WebCamTexture.devices;
  37. if (devices.Length == 0)
  38. {
  39. Debug.LogError("RequestOpenRGBCamera() error : Fail to obtain any web camera texture !");
  40. return;
  41. }
  42. if (RGBCameraTexture && RGBCameraTexture.isPlaying)
  43. {
  44. Debug.Log("RequestOpenRGBCamera() : web camera texture is already playing !");
  45. return;
  46. }
  47. for (int i = 0; i < devices.Length; i++)
  48. {
  49. var _device = devices[i];
  50. if (_device.isFrontFacing == true)
  51. {
  52. //RhinoX using front facing camera
  53. RGBCameraTexture = new WebCamTexture(_device.name, width, height, fps);
  54. RGBCameraTexture.Play();
  55. break;
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Stops the internal WebCam rgb camera texture and dispose the webCamTexture object.
  61. /// </summary>
  62. internal static void RequestStopRGBCamera()
  63. {
  64. if (RGBCameraTexture && RGBCameraTexture.isPlaying)
  65. {
  66. RGBCameraTexture.Stop();
  67. RGBCameraTexture = null;
  68. }
  69. }
  70. /// <summary>
  71. /// Display eye reticle app.
  72. /// </summary>
  73. public static bool DisplayReticle
  74. {
  75. get => SvrPluginAndroid.Unity_getReticleRendering();
  76. set
  77. {
  78. SvrPluginAndroid.Unity_setReticleRendering(value);
  79. }
  80. }
  81. /// <summary>
  82. /// Sets reticle texture id.
  83. /// </summary>
  84. public static void SetReticleTexture(int texturePtr, int width, int height)
  85. {
  86. SvrPluginAndroid.Unity_setReticleTextureId(texturePtr, width, height);
  87. }
  88. /// <summary>
  89. /// Toggle overlay rendering on/off.
  90. /// </summary>
  91. public static bool OverlayRendering
  92. {
  93. get => SvrPluginAndroid.Unity_getOverlayRendering();
  94. set
  95. {
  96. SvrPluginAndroid.Unity_setOverlayRendering(value);
  97. }
  98. }
  99. /// <summary>
  100. /// Sets overlay renderer texture id.
  101. /// </summary>
  102. public static void SetOverlayRendererTextureID(int texturePtrLeft, int texturePtrRight)
  103. {
  104. SvrPluginAndroid.Unity_setOverlayRenderingTextureId(texturePtrLeft, texturePtrRight);
  105. }
  106. /// <summary>
  107. /// Loads a track profile.
  108. /// </summary>
  109. /// <param name="profile"></param>
  110. public static void LoadTrackingProfile(TrackingItem[] trackingItems)
  111. {
  112. XDevicePlugin.ResetTrackingMarkerSettings();
  113. foreach (var item in trackingItems)
  114. {
  115. NativeArray<int> ids = new NativeArray<int>(item.MarkerIDs.Length, Allocator.TempJob);
  116. //XDevicePlugin.LoadTrackingMarkerSettingsFile("/sdcard/vpusdk/marker_calib/BEACON/BEACON-500.json", ref ids, 100);
  117. XDevicePlugin.LoadTrackingMarkerSettingsFile(item.jsonName, ref ids, ids.Length);
  118. ids.Dispose();
  119. }
  120. }
  121. /// <summary>
  122. /// Loads ground plane layout.
  123. /// </summary>
  124. /// <param name="layout"></param>
  125. public static void LoadGroundPlaneLayout(GroundPlaneLayout layout)
  126. {
  127. if (SDKVariants.IsSupported)
  128. PluginVioFusion.plugin_vio_fusion_reset(0);
  129. foreach (var _group in layout.groundPlaneGroups)
  130. {
  131. foreach (var groundPlane in _group.groundPlanes)
  132. {
  133. PluginVioFusion.XAttrBeaconInWorldInfo beacon_in_world_info = new PluginVioFusion.XAttrBeaconInWorldInfo(groundPlane.track_id);
  134. //Base setting:
  135. beacon_in_world_info.group_id = _group.groupIndex;
  136. Quaternion q = Quaternion.Euler(groundPlane.euler);
  137. beacon_in_world_info.beacon_id = groundPlane.track_id;
  138. beacon_in_world_info.rotation[0] = q.x;
  139. beacon_in_world_info.rotation[1] = q.y;
  140. beacon_in_world_info.rotation[2] = q.z;
  141. beacon_in_world_info.rotation[3] = q.w;
  142. beacon_in_world_info.position[0] = groundPlane.position.x;
  143. beacon_in_world_info.position[1] = groundPlane.position.y;
  144. beacon_in_world_info.position[2] = groundPlane.position.z;
  145. //Advance setting:
  146. beacon_in_world_info.drift_recenter_angle_threshold = groundPlane.technicalParameter.drift_recenter_angle_threshold;
  147. beacon_in_world_info.drift_recenter_distance_threshold = groundPlane.technicalParameter.drift_recenter_distance_threshold;
  148. beacon_in_world_info.coord_system_flag = 1; //0=right hand coord system, 1 = left hand coord system, constantly left hand for unity
  149. beacon_in_world_info.confidence_thresh = groundPlane.technicalParameter.confidence_thresh;
  150. beacon_in_world_info.max_distance_thresh = groundPlane.technicalParameter.max_distance_thresh;
  151. beacon_in_world_info.min_distance_thresh = groundPlane.technicalParameter.min_distance_thresh;
  152. PluginVioFusion.plugin_vio_fusion_set_param(ref beacon_in_world_info);
  153. Debug.LogFormat("Inject fusion ground plane id : {0}", groundPlane.track_id);
  154. }
  155. }
  156. SDKVariants.groundPlaneLayout = layout;//update current active layout
  157. if (SDKVariants.IsSupported)
  158. PluginVioFusion.plugin_vio_fusion_run(0);
  159. }
  160. }
  161. }