GSXRPlugin.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using SC.XR.Unity;
  6. using SC.XR.Unity.Module_Device;
  7. using SC.XR.Unity.Module_SDKSystem;
  8. public abstract class GSXRPlugin
  9. {
  10. private static GSXRPlugin instance;
  11. public static GSXRPlugin Instance
  12. {
  13. get
  14. {
  15. if (instance == null)
  16. {
  17. if (XRInputManager.isCheckHand|| Application.isEditor)
  18. {
  19. instance = GSXRPluginWin.Create();
  20. //} else if (!Application.isEditor && Application.platform == RuntimePlatform.Android && OpenXRCamera.Instance.SpecificationType == SpecificationType.GSXR) {
  21. }
  22. else if (!Application.isEditor && Application.platform == RuntimePlatform.Android)
  23. {
  24. instance = GSXRPluginAndroid.Create();
  25. }
  26. else
  27. {
  28. instance = GSXRPluginOther.Create();
  29. }
  30. }
  31. return instance;
  32. }
  33. }
  34. public GSXRManager slamManager = null;
  35. public GSXREye[] eyes = null;
  36. public GSXROverlay[] overlays = null;
  37. public DeviceInfo deviceInfo;
  38. public enum PerfLevel
  39. {
  40. kPerfSystem = 0,
  41. kPerfMaximum = 1,
  42. kPerfNormal = 2,
  43. kPerfMinimum = 3
  44. }
  45. public enum TrackingMode
  46. {
  47. kTrackingOrientation = (1 << 0),
  48. kTrackingPosition = (1 << 1),
  49. kTrackingEye = (1 << 2),
  50. }
  51. public enum EyePoseStatus
  52. {
  53. kGazePointValid = (1 << 0),
  54. kGazeVectorValid = (1 << 1),
  55. kEyeOpennessValid = (1 << 2),
  56. kEyePupilDilationValid = (1 << 3),
  57. kEyePositionGuideValid = (1 << 4)
  58. };
  59. public enum FrameOption
  60. {
  61. kDisableDistortionCorrection = (1 << 0), //!< Disables the lens distortion correction (useful for debugging)
  62. kDisableReprojection = (1 << 1), //!< Disables re-projection
  63. kEnableMotionToPhoton = (1 << 2), //!< Enables motion to photon testing
  64. kDisableChromaticCorrection = (1 << 3) //!< Disables the lens chromatic aberration correction (performance optimization)
  65. };
  66. public struct HeadPose
  67. {
  68. public Vector3 position;
  69. public Quaternion orientation;
  70. }
  71. public struct EyePose
  72. {
  73. public int leftStatus; //!< Bit field (slamEyePoseStatus) indicating left eye pose status
  74. public int rightStatus; //!< Bit field (slamEyePoseStatus) indicating right eye pose status
  75. public int combinedStatus; //!< Bit field (slamEyePoseStatus) indicating combined eye pose status
  76. public Vector3 leftPosition; //!< Left Eye Gaze Point
  77. public Vector3 rightPosition; //!< Right Eye Gaze Point
  78. public Vector3 combinedPosition; //!< Combined Eye Gaze Point (HMD center-eye point)
  79. public Vector3 leftDirection; //!< Left Eye Gaze Point
  80. public Vector3 rightDirection; //!< Right Eye Gaze Point
  81. public Vector3 combinedDirection; //!< Comnbined Eye Gaze Vector (HMD center-eye point)
  82. public float leftOpenness; //!< Left eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  83. public float rightOpenness; //!< Right eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  84. //public float leftDilation; //!< Left eye value in millimeters indicating the pupil dilation
  85. //public float rightDilation; //!< Right eye value in millimeters indicating the pupil dilation
  86. //public Vector3 leftGuide; //!< Position of the inner corner of the left eye in meters from the HMD center-eye coordinate system's origin.
  87. //public Vector3 rightGuide; //!< Position of the inner corner of the right eye in meters from the HMD center-eye coordinate system's origin.
  88. }
  89. public struct ViewFrustum
  90. {
  91. public float left; //!< Left Plane of Frustum
  92. public float right; //!< Right Plane of Frustum
  93. public float top; //!< Top Plane of Frustum
  94. public float bottom; //!< Bottom Plane of Frustum
  95. public float near; //!< Near Plane of Frustum
  96. public float far; //!< Far Plane of Frustum (Arbitrary)
  97. }
  98. public struct DeviceInfo
  99. {
  100. public int displayWidthPixels;
  101. public int displayHeightPixels;
  102. public float displayRefreshRateHz;
  103. public int targetEyeWidthPixels;
  104. public int targetEyeHeightPixels;
  105. public float targetFovXRad;
  106. public float targetFovYRad;
  107. public ViewFrustum targetFrustumLeft;
  108. public ViewFrustum targetFrustumRight;
  109. public float targetFrustumConvergence;
  110. public float targetFrustumPitch;
  111. }
  112. public virtual int GetPredictedPoseModify(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  113. {
  114. return 0;
  115. }
  116. public virtual bool PollEvent(ref GSXRManager.SlamEvent frameEvent) { return false; }
  117. public virtual bool IsInitialized() { return false; }
  118. public virtual bool IsRunning() { return false; }
  119. public virtual IEnumerator Initialize()
  120. {
  121. slamManager = GSXRManager.Instance;
  122. if (slamManager == null)
  123. {
  124. Debug.LogError("slamManager object not found!");
  125. yield break;
  126. }
  127. yield break;
  128. }
  129. public virtual IEnumerator BeginVr(int cpuPerfLevel = 0, int gpuPerfLevel = 0)
  130. {
  131. if (eyes == null)
  132. {
  133. eyes = GSXREye.Instances.ToArray();
  134. if (eyes == null)
  135. {
  136. Debug.Log("Components with GSXREye not found!");
  137. }
  138. Array.Sort(eyes);
  139. }
  140. if (overlays == null)
  141. {
  142. overlays = GSXROverlay.Instances.ToArray();
  143. if (overlays == null)
  144. {
  145. Debug.Log("Components with GSXROverlay not found!");
  146. }
  147. Array.Sort(overlays);
  148. }
  149. yield break;
  150. }
  151. public virtual void EndVr()
  152. {
  153. eyes = null;
  154. overlays = null;
  155. }
  156. public virtual void BeginEye(int sideMask, float[] frameDelta) { }
  157. public virtual void EndEye(int sideMask, int layerMask) { }
  158. public virtual void SetTrackingMode(int mode) { }
  159. public virtual void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum) { }
  160. public virtual void ApplyFoveation() { }
  161. public virtual int GetTrackingMode() { return 0; }
  162. public virtual void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel) { }
  163. public virtual void SetFrameOption(FrameOption frameOption) { }
  164. public virtual void UnsetFrameOption(FrameOption frameOption) { }
  165. public virtual void SetVSyncCount(int vSyncCount) { }
  166. public virtual bool RecenterTracking() { return true; }
  167. public virtual void SubmitFrame(int frameIndex, float fieldOfView, int frameType) { }
  168. public virtual int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  169. {
  170. orientation = Quaternion.identity;
  171. position = Vector3.zero;
  172. return 0;
  173. }
  174. public virtual int GetHeadPose(ref HeadPose headPose, int frameIndex = -1)
  175. {
  176. headPose.orientation = Quaternion.identity;
  177. headPose.position = Vector3.zero;
  178. return 0;
  179. }
  180. public abstract DeviceInfo GetDeviceInfo();
  181. public virtual void Shutdown()
  182. {
  183. GSXRPlugin.instance = null;
  184. }
  185. //---------------------------------------------------------------------------------------------
  186. public virtual int ControllerStartTracking(string desc)
  187. {
  188. return -1;
  189. }
  190. //---------------------------------------------------------------------------------------------
  191. public virtual void ControllerStopTracking(int handle)
  192. {
  193. }
  194. //---------------------------------------------------------------------------------------------
  195. public virtual GSXRControllerState ControllerGetState(int handle, int space)
  196. {
  197. return new GSXRControllerState();
  198. }
  199. //---------------------------------------------------------------------------------------------
  200. public virtual void ControllerSendMessage(int handle, GSXRController.slamControllerMessageType what, int arg1, int arg2)
  201. {
  202. }
  203. //---------------------------------------------------------------------------------------------
  204. public virtual object ControllerQuery(int handle, GSXRController.slamControllerQueryType what)
  205. {
  206. return null;
  207. }
  208. public virtual void GSXR_Set_OnFloorOrOnHead(int type) { }
  209. #region HMD
  210. public virtual bool HeadSetEnterKeyDown() { return Input.GetMouseButtonDown(0); }
  211. public virtual bool HeadSetEnterKeyUp() { return Input.GetMouseButtonUp(0); }
  212. public virtual bool HeadSetBackKeyDown() { return Input.GetKeyDown(KeyCode.Escape); }
  213. public virtual bool HeadSetBackKeyUp() { return Input.GetKeyUp(KeyCode.Escape); }
  214. #endregion HMD
  215. #region Controller
  216. public delegate void OnKeyEvent(int keycode, int action, int lr);
  217. public delegate void OnTouchPanelEvent(float x, float y, int touch, int lr);
  218. public delegate void OnKeyTouchEvent(int keycode, bool touch, int lr);
  219. public delegate void OnTouchEvent(int touch_x, int touch_y, int lr);
  220. public delegate void OnHallEvent(int hall_x, int hall_y, int lr);
  221. public delegate void OnChargingEvent(bool isCharging, int lr);
  222. public delegate void OnBatteryEvent(int battery, int lr);
  223. public delegate void OnConnectEvent(bool isConnected, int lr);
  224. public delegate void OnBondStateEvent(int bondState);
  225. public delegate void OnHallRangeEvent(int hall_x, int hall_y, int lr);
  226. public virtual bool GSXR_Is_SupportController() { return false; }
  227. public virtual int GSXR_Get_ControllerMode() { return 3; }
  228. public virtual int GSXR_Get_ControllerNum() { return 2; }
  229. public virtual void GSXR_Set_ControllerKeyEventCallback(OnKeyEvent _event) { }
  230. public virtual void GSXR_Set_ControllerTouchPanelCallback(OnTouchPanelEvent _event) { }
  231. public virtual void GSXR_Set_ControllerKeyTouchEventCallback(OnKeyTouchEvent _event) { }
  232. public virtual void GSXR_Set_ControllerRockerCallback(OnTouchEvent _event) { }
  233. public virtual void GSXR_Set_ControllerHallCallback(OnHallEvent _event) { }
  234. public virtual void GSXR_Set_ControllerChargingEventCallback(OnChargingEvent _event) { }
  235. public virtual void GSXR_Set_ControllerBatteryEventCallback(OnBatteryEvent _event) { }
  236. public virtual void GSXR_Set_ControllerConnectEventCallback(OnConnectEvent _event) { }
  237. public virtual void GSXR_Set_ControllerBondEventCallback(OnBondStateEvent _event) { }
  238. public virtual void GSXR_Set_ControllerHallRangeCallback(OnHallRangeEvent _event) { }
  239. public virtual int GSXR_Get_ControllerBattery(int lr) { return -1; }
  240. public virtual float GSXR_Get_ControllerHSVersion(int lr) { return -1; }
  241. public virtual int GSXR_Get_ControllerList() { return -1; }
  242. public virtual bool GSXR_Is_ControllerConnect(int lr) { return false; }
  243. public virtual void GSXR_Set_ControllerLed(int lr) { return; }
  244. [Obsolete("Use int GSXR_Set_ControllerVibrate(int lr, bool isOn, int amplitude, int frequency, int time)")]
  245. public virtual void GSXR_Set_ControllerVibrate(int value) { return; }
  246. public virtual int GSXR_Set_ControllerVibrate(int lr, bool isOn) { return 0; }
  247. public virtual int GSXR_Set_ControllerVibrate(int lr, bool isOn, int amplitude, int frequency, int time) { return 0; }
  248. public virtual bool GSXR_Get_ControllerVibrateStatus(int lr) { return false; }
  249. public virtual int GSXR_Get_ControllerVibrateState(int lr) { return -1; }
  250. public virtual int GSXR_Get_ControllerPosture(float[] outOrientationArray, int lr) { return -1; }
  251. public virtual int GSXR_Get_ControllerAccelerometer(float[] orientationArray, int lr) { return -4; }
  252. public virtual int GSXR_Get_ControllerGyroscope(float[] orientationArray, int lr) { return -4; }
  253. public virtual void GSXR_Bind_Controller(int lr, int type) { }
  254. public virtual void GSXR_Unbind_Controller(int lr, int type) { }
  255. public virtual int GSXR_Get_ControllerBondState() { return -1; }
  256. public virtual int GSXR_Get_ControllerAngleVelocity(float[] orientationArray, int lr) { return -1; }
  257. public virtual int GSXR_Get_ControllerLinearVelocity(float[] orientationArray, int lr) { return -1; }
  258. #endregion Controller
  259. #region HandTracking
  260. public virtual bool GSXR_Is_SupportHandTracking() { return true; }
  261. public virtual void GSXR_StartHandTracking(Action<int> func) { }
  262. public virtual void GSXR_StopHandTracking() { }
  263. public virtual void GSXR_Get_HandTrackingData(float[] mode, float[] pose) { return; }
  264. public virtual int GSXR_Set_HandTrackingData(float[] mode, float[] pose) { return 0; }
  265. public virtual int GSXR_Get_HandTrackingGestureIdx(ref UInt64 index, float[] model, float[] pose) { return 0; }
  266. public virtual void GSXR_Set_HandTrackingCallBack(Action<int> callback) { }
  267. public virtual int GSXR_Set_HandTrackingModelDataCallBack(Action callback) { return 0; }
  268. public virtual void GSXR_Set_HandTrackingLowPowerWarningCallback(Action<int> func) { }
  269. #endregion HandTracking
  270. #region Deflection
  271. public virtual float GSXR_Get_Deflection() { return 0; }
  272. public virtual void GSXR_Set_RelativeDeflection(int deflection) { }
  273. #endregion Deflection
  274. #region PointCloud & Map
  275. public virtual void GSXR_Send_Unity_Command(int command, float[] args, int arglen) { }
  276. public virtual bool GSXR_Support_Map() { return false; }
  277. public virtual bool GSXR_Support_Panel() { return false; }
  278. public virtual int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray) { return 0; }
  279. public virtual int GSXR_Get_OfflineMapRelocState() { return 1; }
  280. public virtual int GSXR_ResaveMap(string path) { return 0; }
  281. public virtual void GSXR_SaveMap() { }
  282. public virtual int GSXR_Get_Gnss(ref double dt, float[] gnss) { return 0; }
  283. public virtual int GSXR_Get_PanelNum() { return 0; }
  284. public virtual int GSXR_Get_PanelInfo(float[] info) { return 0; }
  285. #endregion PointCloud & Map
  286. #region FishEye Data
  287. public virtual int GSXR_Get_FishEyeInfo(ref int num, ref int outWidth, ref int outHeight) { num = 0; return 0; }
  288. public virtual void GSXR_Get_LatestFishEyeFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  289. byte[] outFrameData, float[] outTRDataArray)
  290. { outBUdate = false; }
  291. public virtual void GSXR_Get_LatestFishEyeFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  292. byte[] outFrameData, float[] outTRDataArray)
  293. { outBUdate = false; }
  294. public virtual int GSXR_Get_LatestFishEyeBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData) { outBUdate = false; return 0; }
  295. #endregion FishEye Data
  296. #region Optics Calibration
  297. public Matrix4x4 leftViewMatrix, rightViewMatrix;
  298. public virtual bool GSXR_Is_SupportOpticsCalibration() { return false; }
  299. public virtual int GSXR_Get_TransformMatrix(ref bool outBLoaded, float[] outTransformArray) { outBLoaded = false; return 0; }
  300. public virtual int GSXR_Get_LatestEyeMatrices(float[] outLeftEyeMatrix,
  301. float[] outRightEyeMatrix,
  302. float[] outT,
  303. float[] outR,
  304. int frameIndex,
  305. bool isMultiThreadedRender)
  306. { return 0; }
  307. #endregion Optics Calibration
  308. #region EyeTracking
  309. public virtual bool GSXR_Support_EyeTracking() { return false; }
  310. public virtual int GSXR_Start_EyeTracking() { return -1; }
  311. public virtual int GSXR_Stop_EyeTracking() { return -1; }
  312. public virtual int GSXR_Get_EyeFocus(ref float outFocusX, ref float outFocusY) { return -1; }
  313. public virtual int GSXR_Get_EyePose(ref EyePose eyePose, int frameIndex = -1)
  314. {
  315. eyePose.leftStatus = 0;
  316. eyePose.rightStatus = 0;
  317. eyePose.combinedStatus = 0;
  318. return 0;
  319. }
  320. #endregion EyeTracking
  321. #region USBDisconnect
  322. public virtual void GSXR_Set_GlassDisconnectedCallBack(Action callBack) { }
  323. #endregion USBDisconnect
  324. #region luncher
  325. public virtual int initLayer() { return -1; }
  326. public virtual int startLayerRendering() { return -1; }
  327. public virtual int getAllLayersData(ref GSXRManager.SCAllLayers outAllLayers) { return -1; }
  328. public virtual int endLayerRendering(ref GSXRManager.SCAllLayers allLayers) { return -1; }
  329. public virtual int destroyLayer() { return -1; }
  330. public virtual int updateModelMatrix(UInt32 layerId, float[] modelMatrixArray) { return -1; }
  331. public virtual int setForcedDisplaySize(int width, int height) { return -1; }
  332. public virtual int sendActionBarCMD(UInt32 layerId, int cmd) { return -1; }
  333. public virtual int injectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y) { return -1; }
  334. #endregion luncher
  335. #region Device
  336. public virtual int GSXR_SetMeshOffset(int type, float value) { return 0; }
  337. public virtual float GSXR_GetMeshOffset(int type) { return 0; }
  338. public virtual int GSXR_Set_6Dof(bool is6dof) { return 0; }
  339. public virtual ulong GSXR_Get_SupportSlamMode() { return 3; }
  340. public virtual string GSXR_Get_Version() { return "0.0.0"; }
  341. public abstract XRType GSXR_Get_XRType();
  342. protected string m_InternalVersion = "";
  343. public virtual string GSXR_Get_InternalVersion()
  344. {
  345. if (m_InternalVersion == "")
  346. {
  347. AndroidJavaClass deviceInfoClass = new AndroidJavaClass("com.xr.sdk.deviceinfo.DeviceInfo");
  348. m_InternalVersion = deviceInfoClass.CallStatic<string>("getInternalVersion");
  349. }
  350. return m_InternalVersion;
  351. }
  352. protected string m_DeviceName = "";
  353. public virtual string GSXR_Get_DeviceName()
  354. {
  355. if (m_DeviceName == "")
  356. {
  357. AndroidJavaClass deviceInfoClass = new AndroidJavaClass("com.xr.sdk.deviceinfo.DeviceInfo");
  358. m_DeviceName = deviceInfoClass.CallStatic<string>("getDeviceName");
  359. }
  360. return m_DeviceName;
  361. }
  362. protected string m_SN = "";
  363. public virtual string SN
  364. {
  365. get
  366. {
  367. if (m_SN == "")
  368. {
  369. AndroidJavaClass deviceInfoClass = new AndroidJavaClass("com.xr.sdk.deviceinfo.DeviceInfo");
  370. m_SN = deviceInfoClass.CallStatic<string>("getSerialno");
  371. }
  372. return m_SN;
  373. }
  374. }
  375. protected string m_RELEASE_VERSION = "";
  376. public virtual string RELEASE_VERSION
  377. {
  378. get
  379. {
  380. if (m_RELEASE_VERSION == "")
  381. {
  382. AndroidJavaClass os = new AndroidJavaClass("android.os.Build$VERSION");
  383. m_RELEASE_VERSION = os.GetStatic<string>("RELEASE");
  384. }
  385. return m_RELEASE_VERSION;
  386. }
  387. }
  388. protected int m_BatteryLevel = -1;
  389. public virtual int BatteryLevel
  390. {
  391. get
  392. {
  393. if (m_BatteryLevel == -1)
  394. {
  395. m_BatteryLevel = API_Module_BatteryStatus.BatteryLevel;
  396. }
  397. return m_BatteryLevel;
  398. }
  399. }
  400. public virtual float GSXR_Get_ControllerVersion() { return -1; }
  401. #endregion
  402. #region Seethrough
  403. public virtual void GSXR_StartSeeThrough() { }
  404. public virtual void GSXR_StopSeeThrough() { }
  405. public virtual void GSXR_Regist_SeeThrough_Callback(Action<bool> callback) { }
  406. public virtual void GSXR_UnRegist_SeeThrough_Callbak(Action<bool> callback) { }
  407. #endregion
  408. }