XimmerseXRLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. namespace Ximmerse.XR
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. using UnityEngine.Rendering;
  8. using UnityEngine.XR;
  9. using UnityEngine.XR.Management;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Reflection;
  13. using UnityEngine.XR.ARSubsystems;
  14. using System.IO;
  15. using Object = UnityEngine.Object;
  16. using UnityEngine.SceneManagement;
  17. using UnityEditor;
  18. using Ximmerse.XR.Tag;
  19. #if UNITY_EDITOR
  20. [InitializeOnLoad]
  21. #endif
  22. public static class XRloaderSetting
  23. {
  24. static XRloaderSetting()
  25. {
  26. GetSettings();
  27. }
  28. public static void GetSettings()
  29. {
  30. XimmerseXRSettings settings = null;
  31. #if UNITY_EDITOR
  32. var exampleAsset = AssetDatabase.LoadAssetAtPath<XimmerseXRLoader>("Assets/XR/Loaders/Ximmerse XR Loader.asset");
  33. if (exampleAsset == null)
  34. {
  35. return;
  36. }
  37. if (exampleAsset.settings == null)
  38. {
  39. settings = AssetDatabase.LoadAssetAtPath<XimmerseXRSettings>("Assets/XR/Settings/Ximmerse XR Settings.asset");
  40. exampleAsset.settings = settings;
  41. Debug.Log("Ximmerse XR Setting");
  42. }
  43. if (exampleAsset.settings == null)
  44. {
  45. Debug.LogError("Ximmerse XR loaderSetting is null");
  46. }
  47. #endif
  48. }
  49. }
  50. /// <summary>
  51. /// XR Loader for Cardboard XR Plugin.
  52. /// Loads Display and Input Subsystems.
  53. /// </summary>
  54. [DefaultExecutionOrder(-20000)]
  55. public class XimmerseXRLoader : XRLoaderHelper
  56. {
  57. static NativePluginApi.DeviceType sDeviceType = NativePluginApi.DeviceType.Device_AUTO;
  58. private static List<XRDisplaySubsystemDescriptor> _displaySubsystemDescriptors = new List<XRDisplaySubsystemDescriptor>();
  59. private static List<XRInputSubsystemDescriptor> _inputSubsystemDescriptors = new List<XRInputSubsystemDescriptor>();
  60. public XimmerseXRSettings settings;
  61. internal static bool _isInitialized { get; private set; }
  62. internal static bool _isStarted { get; private set; }
  63. private const string kXimHMD = "Xim_HMD";
  64. private const string kLeftController = "LeftHand";
  65. private const string kRightController = "RightHand";
  66. private void Awake()
  67. {
  68. SetSDKVars();//set sdk vars at main thread
  69. if (!SDKVariants.IsSupported)
  70. {
  71. return;
  72. }
  73. SceneManager.sceneLoaded += SceneManager_sceneLoaded;
  74. int _reticleTextureId = settings.displayReticle && settings.reticleTexture ? settings.reticleTexture.GetNativeTexturePtr().ToInt32() : -1;
  75. int _reticleTexW = settings.displayReticle && settings.reticleTexture ? settings.reticleTexture.width : -1;
  76. int _reticleTexH = settings.displayReticle && settings.reticleTexture ? settings.reticleTexture.height : -1;
  77. //Async op at daemon thread.
  78. Task.Run(async () =>
  79. {
  80. Debug.Log("XR initialization thread starts...");
  81. try
  82. {
  83. while (!SvrPluginAndroid.SvrIsRunning())
  84. {
  85. Thread.Sleep(10);
  86. }
  87. //Set reticle texture id:
  88. Debug.Log("Daemon threads detect SVR system is started.");
  89. if (settings.reticleTexture && settings.displayReticle)
  90. {
  91. XimmerseXR.DisplayReticle = true;
  92. XimmerseXR.SetReticleTexture(_reticleTextureId, _reticleTexW, _reticleTexH);
  93. }
  94. else
  95. {
  96. XimmerseXR.DisplayReticle = false;
  97. }
  98. //Loads default tracking profile:
  99. try
  100. {
  101. if (!ReferenceEquals(null, (System.Object)settings.defaultTrackingProfile))
  102. {
  103. //Load tracking profile :
  104. XimmerseXR.LoadTrackingProfile(this.settings.defaultTrackingProfile.trackingItems);
  105. }
  106. }
  107. catch (System.Exception exc)
  108. {
  109. Debug.LogError("Default tracking profile not loaded.");
  110. Debug.LogException(exc);
  111. }
  112. Debug.Log("Reticle set successfully.");
  113. await Task.Delay(10);//delay a bit to start fusion:
  114. if (SDKVariants.groundPlaneLayout != null && SDKVariants.groundPlaneLayout.IsValid())
  115. {
  116. XimmerseXR.LoadGroundPlaneLayout(SDKVariants.groundPlaneLayout);
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. Debug.LogErrorFormat("XR initialization exception: {0}, {1}", e.Message, e.StackTrace, e);
  122. Debug.LogException(e);
  123. }
  124. });
  125. }
  126. private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
  127. {
  128. //Create XR manager:
  129. var xrMgr = new GameObject("XR Manager", typeof(XRManager));
  130. Object.DontDestroyOnLoad(xrMgr.gameObject);
  131. xrMgr.GetComponent<XRManager>().StartCoroutine(initializeDeivces());
  132. if (this.settings.HandTracking)
  133. {
  134. Ximmerse.XR.InputSystems.HandTracking.EnableHandTracking();
  135. }
  136. SceneManager.sceneLoaded -= SceneManager_sceneLoaded;
  137. }
  138. /// <summary>
  139. /// Init XR devices : HMD , left and right controller.
  140. /// </summary>
  141. /// <returns></returns>
  142. System.Collections.IEnumerator initializeDeivces()
  143. {
  144. bool hmdDeviceEnabled = false;
  145. bool leftHandEnabled = false;
  146. bool rightHandEnabled = false;
  147. UnityEngine.InputSystem.InputSystem.onDeviceChange -= OnDevicesChanged;
  148. UnityEngine.InputSystem.InputSystem.onDeviceChange += OnDevicesChanged;
  149. while (true)
  150. {
  151. if (hmdDeviceEnabled && leftHandEnabled && rightHandEnabled)
  152. {
  153. yield break;
  154. }
  155. for (int i = 0; i < UnityEngine.InputSystem.InputSystem.devices.Count; i++)
  156. {
  157. UnityEngine.InputSystem.InputDevice device = UnityEngine.InputSystem.InputSystem.devices[i];
  158. if (!hmdDeviceEnabled && device.name == kXimHMD && !device.enabled)
  159. {
  160. UnityEngine.InputSystem.InputSystem.EnableDevice(device);
  161. hmdDeviceEnabled = true;
  162. Debug.Log("Ximmerse XR Loader : Enable Xim_HMD.");
  163. }
  164. if (!leftHandEnabled && device.name == kLeftController && !device.enabled)
  165. {
  166. UnityEngine.InputSystem.InputSystem.EnableDevice(device);
  167. leftHandEnabled = true;
  168. Debug.Log("Ximmerse XR Loader : Enable Left Controller.");
  169. }
  170. if (!rightHandEnabled && device.name == kRightController && !device.enabled)
  171. {
  172. UnityEngine.InputSystem.InputSystem.EnableDevice(device);
  173. rightHandEnabled = true;
  174. Debug.Log("Ximmerse XR Loader : Enable Right Controller.");
  175. }
  176. }
  177. yield return null;
  178. }
  179. #if DEVELOPMENT_BUILD
  180. for (int i = 0; i < UnityEngine.InputSystem.InputSystem.devices.Count; i++)
  181. {
  182. UnityEngine.InputSystem.InputDevice device = UnityEngine.InputSystem.InputSystem.devices[i];
  183. System.Text.StringBuilder buffer = new System.Text.StringBuilder();
  184. if (device.name == kLeftController || device.name == kRightController || device.name == kXimHMD)
  185. {
  186. var controls = device.allControls;
  187. for (int j = 0; j < controls.Count; j++)
  188. {
  189. buffer.Clear();
  190. UnityEngine.InputSystem.InputControl ctrl = controls[j];
  191. foreach (var usage in ctrl.usages)
  192. {
  193. buffer.Append(usage.ToString() + ' ');
  194. }
  195. Debug.LogFormat("Xim device layout, name = {0}, path = {1}, value type = {2}, usage = {3}, device type = {4}", ctrl.name, ctrl.path, ctrl.valueType, buffer.ToString(), device.GetType());
  196. }
  197. }
  198. }
  199. #endif
  200. }
  201. /// <summary>
  202. /// Enables devices after reconnection.
  203. /// </summary>
  204. /// <param name="arg1"></param>
  205. /// <param name="arg2"></param>
  206. private void OnDevicesChanged(UnityEngine.InputSystem.InputDevice arg1, UnityEngine.InputSystem.InputDeviceChange arg2)
  207. {
  208. if (arg2 == UnityEngine.InputSystem.InputDeviceChange.Reconnected && arg1.name == kLeftController && !arg1.enabled)
  209. {
  210. UnityEngine.InputSystem.InputSystem.EnableDevice(arg1);
  211. }
  212. if (arg2 == UnityEngine.InputSystem.InputDeviceChange.Reconnected && arg1.name == kRightController && !arg1.enabled)
  213. {
  214. UnityEngine.InputSystem.InputSystem.EnableDevice(arg1);
  215. }
  216. }
  217. public override bool Initialize()
  218. {
  219. SDKInitialize();
  220. CreateSubsystem<XRDisplaySubsystemDescriptor, XRDisplaySubsystem>(_displaySubsystemDescriptors, "XmsXRDisplay");
  221. CreateSubsystem<XRInputSubsystemDescriptor, XRInputSubsystem>(_inputSubsystemDescriptors, "XmsXRInput");
  222. Debug.LogFormat("XRLoader.Initialize, settings: {0}, SubSystems has been created.", settings.name);
  223. #if UNITY_INPUT_SYSTEM
  224. Ximmerse.XR.InputSystems.InputLayout.RegisterInputLayouts();
  225. #endif
  226. UnityEngine.InputSystem.InputSystem.onDeviceChange += (device, changeEvent) =>
  227. {
  228. Debug.LogFormat("Control inputSystem device event : {0}, device : {1}, {2}, {3}, {4}", changeEvent, device.name, device.displayName, device.deviceId, device);
  229. };
  230. _isInitialized = true;
  231. return true;
  232. }
  233. public override bool Start()
  234. {
  235. Debug.Log("[XRLoader] ==>Start");
  236. if (Application.platform == RuntimePlatform.Android)
  237. {
  238. if (sDeviceType == NativePluginApi.DeviceType.Device_AUTO)
  239. {
  240. SvrPlugin.deviceModel = SvrPlugin.DeviceModel.Default;
  241. int trackingMode = (int)SvrPlugin.TrackingMode.kTrackingOrientation | (int)SvrPlugin.TrackingMode.kTrackingPosition;
  242. NativePluginApi.Unity_setTrackingMode(trackingMode);
  243. NativePluginApi.Unity_setVsync(1);
  244. //SvrPlugin.Instance.SetTrackingMode(trackingMode);
  245. //SvrPlugin.Instance.SetVSyncCount(1);
  246. SvrPlugin.Instance.BeginVr(3, 3, 0x0);
  247. }
  248. }
  249. StartSubsystem<XRDisplaySubsystem>();
  250. StartSubsystem<XRInputSubsystem>();
  251. // StartSubsystem<RhinoXGroundPlaneSubSystem>();
  252. _isStarted = true;
  253. return true;
  254. }
  255. public override bool Stop()
  256. {
  257. Debug.Log("[XRLoader] ==>Stop");
  258. StopSubsystem<XRDisplaySubsystem>();
  259. StopSubsystem<XRInputSubsystem>();
  260. // StopSubsystem<RhinoXGroundPlaneSubSystem>();
  261. _isStarted = false;
  262. return true;
  263. }
  264. public override bool Deinitialize()
  265. {
  266. Debug.Log("[XRLoader] ==>Deinitialize");
  267. DestroySubsystem<XRDisplaySubsystem>();
  268. DestroySubsystem<XRInputSubsystem>();
  269. // DestroySubsystem<RhinoXGroundPlaneSubSystem>();
  270. _isInitialized = false;
  271. return true;
  272. }
  273. private void SDKInitialize()
  274. {
  275. if (Application.platform == RuntimePlatform.Android)
  276. {
  277. NativePluginApi.Unity_setDeviceType((int)sDeviceType);
  278. var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  279. var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  280. var context = activity.Call<AndroidJavaObject>("getApplicationContext");
  281. //NativePluginApi.Unity_setRenderResolution(2160, 2160);
  282. NativePluginApi.Unity_initializeAndroid(activity.GetRawObject());
  283. switch (SystemInfo.graphicsDeviceType)
  284. {
  285. case GraphicsDeviceType.OpenGLES2:
  286. NativePluginApi.Unity_setGraphicsApi(NativePluginApi.GraphicsApi.kOpenGlEs2);
  287. break;
  288. case GraphicsDeviceType.OpenGLES3:
  289. NativePluginApi.Unity_setGraphicsApi(NativePluginApi.GraphicsApi.kOpenGlEs3);
  290. break;
  291. default:
  292. Debug.LogErrorFormat(
  293. "The Ximmerse XR Plugin cannot be initialized given that the selected " +
  294. "Graphics API ({0}) is not supported. Please use OpenGL ES 2.0, " +
  295. "OpenGL ES 3.0 or Metal.", SystemInfo.graphicsDeviceType);
  296. break;
  297. }
  298. NativePluginApi.Unity_setScreenParams((int)Screen.width, (int)Screen.height, (int)Screen.safeArea.x, (int)Screen.safeArea.y, (int)Screen.safeArea.width, (int)Screen.safeArea.height);
  299. return;
  300. }
  301. }
  302. private void SetSDKVars()
  303. {
  304. SDKVariants.kTrackingDataDir_Internal = Application.persistentDataPath;
  305. SDKVariants.IsSupported = Application.platform == RuntimePlatform.Android; //todo : verify on android phone
  306. SDKVariants.groundPlaneLayout = settings.defaultGroundPlaneLayoutConfig ? settings.defaultGroundPlaneLayoutConfig.layout : default(GroundPlaneLayout);
  307. SDKVariants.TrackingAnchor = Matrix4x4.TRS(SDKVariants.kVPU_Shift, Quaternion.Euler(SDKVariants.kVPU_TiltEuler), Vector3.one);
  308. SDKVariants.DrawTrackedMarkerGizmos = settings.DrawTrackedMarkerGizmos;
  309. SDKVariants.DrawDetailTrackedInfo = settings.DrawDetailTrackedInfo;
  310. }
  311. }
  312. }