XRLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //-----------------------------------------------------------------------
  2. //-----------------------------------------------------------------------
  3. namespace Rokid.XR.Core
  4. {
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using UnityEngine;
  9. using UnityEngine.Rendering;
  10. using UnityEngine.XR;
  11. using UnityEngine.XR.Management;
  12. /// <summary>
  13. /// XR Loader for UXR Plugin.
  14. /// Loads Display and Input Subsystems.
  15. /// </summary>
  16. public class XRLoader : XRLoaderHelper
  17. {
  18. private static List<XRDisplaySubsystemDescriptor> _displaySubsystemDescriptors =
  19. new List<XRDisplaySubsystemDescriptor>();
  20. private static List<XRInputSubsystemDescriptor> _inputSubsystemDescriptors =
  21. new List<XRInputSubsystemDescriptor>();
  22. /// <summary>
  23. /// Pairs the native enum to set the graphics API being used.
  24. /// </summary>
  25. private enum UXRGraphicsApi
  26. {
  27. kOpenGlEs2 = 1,
  28. kOpenGlEs3 = 2,
  29. kVulkan = 4,
  30. kNone = -1,
  31. }
  32. /// <summary>
  33. /// Describes the possible orientation of the viewport.
  34. /// </summary>
  35. private enum UXRViewportOrientation
  36. {
  37. kLandscapeLeft = 0,
  38. kLandscapeRight = 1,
  39. kPortrait = 2,
  40. kPortraitUpsideDown = 3,
  41. }
  42. /// <summary>
  43. /// Gets a value indicating whether the subsystems are initialized or not.
  44. /// </summary>
  45. ///
  46. /// <returns>
  47. /// True after a successful call to Initialize() without a posterior call to
  48. /// Deinitialize().
  49. /// </returns>
  50. //internal static bool _isInitialized { get; private set; }
  51. public static bool _isInitialized { get; set; }
  52. /// <summary>
  53. /// Gets a value indicating whether the subsystems are started or not.
  54. /// </summary>
  55. ///
  56. /// <returns>
  57. /// True after a successful call to Start() without a posterior call to Stop().
  58. /// </returns>
  59. internal static bool _isStarted { get; private set; }
  60. /// <summary>
  61. /// Initialize the loader. This should initialize all subsystems to support the desired
  62. /// runtime setup this loader represents.
  63. /// </summary>
  64. ///
  65. /// <returns>Whether or not initialization succeeded.</returns>
  66. public override bool Initialize()
  67. {
  68. UXRSDKInitialize();
  69. CreateSubsystem<XRDisplaySubsystemDescriptor, XRDisplaySubsystem>(
  70. _displaySubsystemDescriptors, "CardboardDisplay");
  71. CreateSubsystem<XRInputSubsystemDescriptor, XRInputSubsystem>(
  72. _inputSubsystemDescriptors, "CardboardInput");
  73. // add for XRI
  74. OnXRILoaderInitialize?.Invoke(this);
  75. _isInitialized = true;
  76. return true;
  77. }
  78. /// <summary>
  79. /// Ask loader to start all initialized subsystems.
  80. /// </summary>
  81. ///
  82. /// <returns>Whether or not all subsystems were successfully started.</returns>
  83. public override bool Start()
  84. {
  85. StartSubsystem<XRDisplaySubsystem>();
  86. StartSubsystem<XRInputSubsystem>();
  87. // add for XRI
  88. OnXRILoaderStart?.Invoke(this);
  89. _isStarted = true;
  90. return true;
  91. }
  92. /// <summary>
  93. /// Ask loader to stop all initialized subsystems.
  94. /// </summary>
  95. ///
  96. /// <returns>Whether or not all subsystems were successfully stopped.</returns>
  97. public override bool Stop()
  98. {
  99. StopSubsystem<XRDisplaySubsystem>();
  100. StopSubsystem<XRInputSubsystem>();
  101. // add for XRI
  102. OnXRILoaderStop?.Invoke(this);
  103. _isStarted = false;
  104. return true;
  105. }
  106. /// <summary>
  107. /// Ask loader to deinitialize all initialized subsystems.
  108. /// </summary>
  109. ///
  110. /// <returns>Whether or not deinitialization succeeded.</returns>
  111. public override bool Deinitialize()
  112. {
  113. DestroySubsystem<XRDisplaySubsystem>();
  114. DestroySubsystem<XRInputSubsystem>();
  115. // add for XRI
  116. OnXRILoaderDeinitialize?.Invoke(this);
  117. UXRSDKDeinitialize();
  118. _isInitialized = false;
  119. return true;
  120. }
  121. /// <summary>
  122. /// Sets the screen parameters in the XR scene.
  123. /// </summary>
  124. ///
  125. /// <param name="renderingArea">
  126. /// The rectangle where the XR scene will be rendered.
  127. /// </param>
  128. public static void RecalculateRectangles(Rect renderingArea)
  129. {
  130. setScreenParams((int)renderingArea.width, (int)renderingArea.height);
  131. Debug.Log("-uxr- ScreenParam h:" + Screen.height + ",renderingArea: "+ renderingArea.ToString());
  132. Debug.Log("-uxr- displays len:" + Display.displays.Length + ",glass systemHeight: " + Display.displays[Display.displays.Length-1].systemHeight +
  133. ",renderingHeight: " + Display.displays[Display.displays.Length-1].renderingHeight);
  134. }
  135. /// <summary>
  136. /// Sets which viewport orientation is being used by Unity to the native implementation.
  137. /// </summary>
  138. ///
  139. /// <param name="screenOrientation">
  140. /// The required screen orientation.
  141. /// </param>
  142. public static void SetViewportOrientation(ScreenOrientation screenOrientation)//TODO
  143. {
  144. /*switch (screenOrientation)
  145. {
  146. case ScreenOrientation.LandscapeLeft:
  147. CardboardUnity_setViewportOrientation(
  148. UXRViewportOrientation.kLandscapeLeft);
  149. break;
  150. case ScreenOrientation.LandscapeRight:
  151. CardboardUnity_setViewportOrientation(
  152. UXRViewportOrientation.kLandscapeRight);
  153. break;
  154. case ScreenOrientation.Portrait:
  155. CardboardUnity_setViewportOrientation(UXRViewportOrientation.kLandscapeLeft);
  156. break;
  157. case ScreenOrientation.PortraitUpsideDown:
  158. CardboardUnity_setViewportOrientation(
  159. UXRViewportOrientation.kPortraitUpsideDown);
  160. break;
  161. default:
  162. Debug.LogWarning(
  163. "The UXR Plugin does not support the selected screen orientation." +
  164. "Setting landscape left as default.");
  165. CardboardUnity_setViewportOrientation(
  166. UXRViewportOrientation.kLandscapeLeft);
  167. break;
  168. }*/
  169. }
  170. /// <summary>
  171. /// Sets which Graphics API is being used by Unity to the native implementation.
  172. /// </summary>
  173. private static void SetGraphicsApi()
  174. {
  175. switch (SystemInfo.graphicsDeviceType)
  176. {
  177. case GraphicsDeviceType.OpenGLES2:
  178. CardboardUnity_setGraphicsApi(UXRGraphicsApi.kOpenGlEs2);
  179. break;
  180. case GraphicsDeviceType.OpenGLES3:
  181. CardboardUnity_setGraphicsApi(UXRGraphicsApi.kOpenGlEs3);
  182. break;
  183. #if UNITY_ANDROID
  184. case GraphicsDeviceType.Vulkan:
  185. CardboardUnity_setGraphicsApi(UXRGraphicsApi.kVulkan);
  186. break;
  187. #endif
  188. default:
  189. Debug.LogErrorFormat(
  190. "The UXR Plugin cannot be initialized given that the selected " +
  191. "Graphics API ({0}) is not supported. Please use OpenGL ES 2.0, " +
  192. "OpenGL ES 3.0.", SystemInfo.graphicsDeviceType);
  193. break;
  194. }
  195. }
  196. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  197. private static extern void setScreenParams(int viewport_width, int viewport_height);
  198. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  199. private static extern void CardboardUnity_setGraphicsApi(UXRGraphicsApi graphics_api);
  200. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  201. private static extern void CardboardUnity_setViewportOrientation(
  202. UXRViewportOrientation viewport_orientation);
  203. #if UNITY_ANDROID
  204. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  205. private static extern void UXRUnity_initializeAndroid(IntPtr context);
  206. #endif
  207. /// <summary>
  208. /// For Android, initializes JavaVM and Android activity context.
  209. /// Then,it sets the screen size in pixels.
  210. /// </summary>
  211. private void UXRSDKInitialize()
  212. {
  213. #if UNITY_ANDROID
  214. // TODO(b/169797155): Move this to UnityPluginLoad().
  215. // Gets Unity context (Main Activity).
  216. var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  217. var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  218. var context = activity.Call<AndroidJavaObject>("getApplicationContext");
  219. // Initializes SDK.
  220. UXRUnity_initializeAndroid(activity.GetRawObject());
  221. #endif
  222. //SetGraphicsApi();//TODO
  223. SetViewportOrientation(Screen.orientation);
  224. // Safe area is required to avoid rendering behind the notch. If the device does not
  225. // have any notch, it will be equivalent to the full screen area.
  226. RecalculateRectangles(Screen.safeArea);
  227. }
  228. /// <summary>
  229. /// the XR provider is deinitialized.
  230. /// </summary>
  231. private void UXRSDKDeinitialize()
  232. {
  233. }
  234. #region ForXRI
  235. public static Action<XRLoader> OnXRILoaderInitialize;
  236. public static Action<XRLoader> OnXRILoaderStart;
  237. public static Action<XRLoader> OnXRILoaderStop;
  238. public static Action<XRLoader> OnXRILoaderDeinitialize;
  239. public void CreateCustomSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
  240. where TDescriptor : ISubsystemDescriptor
  241. where TSubsystem : ISubsystem
  242. {
  243. CreateSubsystem<TDescriptor, TSubsystem>(descriptors, id);
  244. }
  245. public void StartCustomSubsystem<T>() where T : class, ISubsystem
  246. {
  247. StartSubsystem<T>();
  248. }
  249. public void StopCustomSubsystem<T>() where T : class, ISubsystem
  250. {
  251. StopSubsystem<T>();
  252. }
  253. public void DestroyCustomSubsystem<T>() where T : class, ISubsystem
  254. {
  255. DestroySubsystem<T>();
  256. }
  257. #endregion
  258. }
  259. }