PXR_Loader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Runtime.InteropServices;
  14. using UnityEngine;
  15. using UnityEngine.Rendering;
  16. using UnityEngine.XR.Management;
  17. using UnityEngine.XR;
  18. using AOT;
  19. #if UNITY_INPUT_SYSTEM
  20. using UnityEngine.InputSystem;
  21. using UnityEngine.InputSystem.Layouts;
  22. using UnityEngine.InputSystem.XR;
  23. using Unity.XR.PXR.Input;
  24. #endif
  25. #if UNITY_EDITOR
  26. using UnityEditor;
  27. #endif
  28. #if XR_HANDS
  29. using UnityEngine.XR.Hands;
  30. #endif
  31. namespace Unity.XR.PXR
  32. {
  33. #if UNITY_INPUT_SYSTEM
  34. #if UNITY_EDITOR
  35. [InitializeOnLoad]
  36. #endif
  37. static class InputLayoutLoader
  38. {
  39. static InputLayoutLoader()
  40. {
  41. RegisterInputLayouts();
  42. }
  43. public static void RegisterInputLayouts()
  44. {
  45. InputSystem.RegisterLayout<PXR_HMD>(matches: new InputDeviceMatcher().WithInterface(XRUtilities.InterfaceMatchAnyVersion).WithProduct(@"^(PICO HMD)|^(PICO Neo)|^(PICO G)"));
  46. InputSystem.RegisterLayout<PXR_Controller>(matches: new InputDeviceMatcher().WithInterface(XRUtilities.InterfaceMatchAnyVersion).WithProduct(@"^(PICO Controller)"));
  47. }
  48. }
  49. #endif
  50. public class PXR_Loader : XRLoaderHelper
  51. #if UNITY_EDITOR
  52. , IXRLoaderPreInit
  53. #endif
  54. {
  55. private static List<XRDisplaySubsystemDescriptor> displaySubsystemDescriptors = new List<XRDisplaySubsystemDescriptor>();
  56. private static List<XRInputSubsystemDescriptor> inputSubsystemDescriptors = new List<XRInputSubsystemDescriptor>();
  57. #if XR_HANDS
  58. private static List<XRHandSubsystemDescriptor> handSubsystemDescriptors = new List<XRHandSubsystemDescriptor>();
  59. #endif
  60. public delegate Quaternion ConvertRotationWith2VectorDelegate(Vector3 from, Vector3 to);
  61. public XRDisplaySubsystem displaySubsystem
  62. {
  63. get
  64. {
  65. return GetLoadedSubsystem<XRDisplaySubsystem>();
  66. }
  67. }
  68. public XRInputSubsystem inputSubsystem
  69. {
  70. get
  71. {
  72. return GetLoadedSubsystem<XRInputSubsystem>();
  73. }
  74. }
  75. public override bool Initialize()
  76. {
  77. #if UNITY_INPUT_SYSTEM
  78. InputLayoutLoader.RegisterInputLayouts();
  79. #endif
  80. #if UNITY_ANDROID
  81. PXR_Settings settings = GetSettings();
  82. if (settings != null)
  83. {
  84. UserDefinedSettings userDefinedSettings = new UserDefinedSettings
  85. {
  86. stereoRenderingMode = settings.GetStereoRenderingMode(),
  87. colorSpace = (ushort)((QualitySettings.activeColorSpace == ColorSpace.Linear) ? 1 : 0),
  88. useContentProtect = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().useContentProtect),
  89. systemDisplayFrequency = settings.GetSystemDisplayFrequency(),
  90. optimizeBufferDiscards = settings.GetOptimizeBufferDiscards(),
  91. enableAppSpaceWarp = Convert.ToUInt16(settings.enableAppSpaceWarp),
  92. enableSubsampled = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().enableSubsampled),
  93. lateLatchingDebug = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().latelatchingDebug),
  94. enableStageMode = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().stageMode),
  95. enableSuperResolution = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().superResolution),
  96. normalSharpening = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().normalSharpening),
  97. qualitySharpening = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().qualitySharpening),
  98. fixedFoveatedSharpening = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().fixedFoveatedSharpening),
  99. selfAdaptiveSharpening = Convert.ToUInt16(PXR_ProjectSetting.GetProjectConfig().selfAdaptiveSharpening)
  100. };
  101. PXR_Plugin.System.UPxr_Construct(ConvertRotationWith2Vector);
  102. PXR_Plugin.System.UPxr_SetInputDeviceChangedCallBack(InputDeviceChangedFunction);
  103. PXR_Plugin.System.UPxr_SetSeethroughStateChangedCallBack(SeethroughStateChangedFunction);
  104. PXR_Plugin.System.UPxr_SetFitnessBandNumberOfConnectionsCallBack(FitnessBandNumberOfConnectionsFunction);
  105. PXR_Plugin.System.UPxr_SetFitnessBandElectricQuantityCallBack(FitnessBandElectricQuantityFunction);
  106. PXR_Plugin.System.UPxr_SetFitnessBandAbnormalCalibrationDataCallBack(FitnessBandAbnormalCalibrationDataFunction);
  107. PXR_Plugin.System.UPxr_SetLoglevelChangedCallBack(LoglevelChangedFunction);
  108. PXR_Plugin.System.UPxr_SetUserDefinedSettings(userDefinedSettings);
  109. }
  110. #endif
  111. CreateSubsystem<XRDisplaySubsystemDescriptor, XRDisplaySubsystem>(displaySubsystemDescriptors, "PICO Display");
  112. CreateSubsystem<XRInputSubsystemDescriptor, XRInputSubsystem>(inputSubsystemDescriptors, "PICO Input");
  113. #if XR_HANDS
  114. CreateSubsystem<XRHandSubsystemDescriptor, XRHandSubsystem>(handSubsystemDescriptors, "PICO Hands");
  115. #endif
  116. if (displaySubsystem == null && inputSubsystem == null)
  117. {
  118. Debug.LogError("PXRLog Unable to start PICO Plugin.");
  119. }
  120. else if (displaySubsystem == null)
  121. {
  122. Debug.LogError("PXRLog Failed to load display subsystem.");
  123. }
  124. else if (inputSubsystem == null)
  125. {
  126. Debug.LogError("PXRLog Failed to load input subsystem.");
  127. }
  128. else
  129. {
  130. PXR_Plugin.System.UPxr_InitializeFocusCallback();
  131. }
  132. #if XR_HANDS
  133. var handSubSystem = GetLoadedSubsystem<XRHandSubsystem>();
  134. if (handSubSystem == null)
  135. {
  136. Debug.LogError("PXRLog Failed to load XRHandSubsystem.");
  137. }
  138. #endif
  139. return displaySubsystem != null;
  140. }
  141. public override bool Start()
  142. {
  143. StartSubsystem<XRDisplaySubsystem>();
  144. StartSubsystem<XRInputSubsystem>();
  145. #if XR_HANDS
  146. StartSubsystem<XRHandSubsystem>();
  147. #endif
  148. return true;
  149. }
  150. public override bool Stop()
  151. {
  152. StopSubsystem<XRDisplaySubsystem>();
  153. StopSubsystem<XRInputSubsystem>();
  154. #if XR_HANDS
  155. StopSubsystem<XRHandSubsystem>();
  156. #endif
  157. return true;
  158. }
  159. public override bool Deinitialize()
  160. {
  161. DestroySubsystem<XRDisplaySubsystem>();
  162. DestroySubsystem<XRInputSubsystem>();
  163. #if XR_HANDS
  164. DestroySubsystem<XRHandSubsystem>();
  165. #endif
  166. PXR_Plugin.System.UPxr_DeinitializeFocusCallback();
  167. return true;
  168. }
  169. [MonoPInvokeCallback(typeof(ConvertRotationWith2VectorDelegate))]
  170. static Quaternion ConvertRotationWith2Vector(Vector3 from, Vector3 to)
  171. {
  172. return Quaternion.FromToRotation(from, to);
  173. }
  174. [MonoPInvokeCallback(typeof(InputDeviceChangedCallBack))]
  175. static void InputDeviceChangedFunction(int value)
  176. {
  177. if (PXR_Plugin.System.InputDeviceChanged != null)
  178. {
  179. PXR_Plugin.System.InputDeviceChanged(value);
  180. }
  181. }
  182. [MonoPInvokeCallback(typeof(SeethroughStateChangedCallBack))]
  183. static void SeethroughStateChangedFunction(int value)
  184. {
  185. if (PXR_Plugin.System.SeethroughStateChangedChanged != null)
  186. {
  187. PXR_Plugin.System.SeethroughStateChangedChanged(value);
  188. }
  189. }
  190. [MonoPInvokeCallback(typeof(FitnessBandNumberOfConnectionsCallBack))]
  191. static void FitnessBandNumberOfConnectionsFunction(int state, int value)
  192. {
  193. if (PXR_Plugin.System.FitnessBandNumberOfConnections != null)
  194. {
  195. PXR_Plugin.System.FitnessBandNumberOfConnections(state, value);
  196. }
  197. }
  198. [MonoPInvokeCallback(typeof(FitnessBandElectricQuantityCallBack))]
  199. static void FitnessBandElectricQuantityFunction(int trackerID, int battery)
  200. {
  201. if (PXR_Plugin.System.FitnessBandElectricQuantity != null)
  202. {
  203. PXR_Plugin.System.FitnessBandElectricQuantity(trackerID, battery);
  204. }
  205. }
  206. [MonoPInvokeCallback(typeof(FitnessBandAbnormalCalibrationDataCallBack))]
  207. static void FitnessBandAbnormalCalibrationDataFunction(int state, int value)
  208. {
  209. if (PXR_Plugin.System.FitnessBandAbnormalCalibrationData != null)
  210. {
  211. PXR_Plugin.System.FitnessBandAbnormalCalibrationData(state, value);
  212. }
  213. }
  214. [MonoPInvokeCallback(typeof(LoglevelChangedCallBack))]
  215. static void LoglevelChangedFunction(int value)
  216. {
  217. if (PXR_Plugin.System.LoglevelChangedChanged != null)
  218. {
  219. PXR_Plugin.System.LoglevelChangedChanged(value);
  220. }
  221. }
  222. public PXR_Settings GetSettings()
  223. {
  224. PXR_Settings settings = null;
  225. #if UNITY_EDITOR
  226. UnityEditor.EditorBuildSettings.TryGetConfigObject<PXR_Settings>("Unity.XR.PXR.Settings", out settings);
  227. #endif
  228. #if UNITY_ANDROID && !UNITY_EDITOR
  229. settings = PXR_Settings.settings;
  230. #endif
  231. return settings;
  232. }
  233. #if UNITY_EDITOR
  234. public string GetPreInitLibraryName(BuildTarget buildTarget, BuildTargetGroup buildTargetGroup)
  235. {
  236. return "PxrPlatform";
  237. }
  238. #endif
  239. #if UNITY_ANDROID && !UNITY_EDITOR
  240. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
  241. static void RuntimeLoadPicoPlugin()
  242. {
  243. PXR_Plugin.System.UPxr_LoadPICOPlugin();
  244. string version = "UnityXR_" + PXR_Plugin.System.UPxr_GetSDKVersion() + "_" + Application.unityVersion;
  245. PXR_Plugin.System.UPxr_SetConfigString( ConfigType.EngineVersion, version );
  246. }
  247. #endif
  248. }
  249. }
粤ICP备19079148号