GSXRPlugin.cs 19 KB

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