GSXRPlugin.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. public abstract class GSXRPlugin
  8. {
  9. private static GSXRPlugin instance;
  10. public static GSXRPlugin Instance
  11. {
  12. get
  13. {
  14. if (instance == null)
  15. {
  16. if(!Application.isEditor && Application.platform == RuntimePlatform.Android)
  17. {
  18. if (API_Module_Device.CurrentAndroid != null && API_Module_Device.CurrentAndroid.type == AndroidDeviceType.Other) {
  19. instance = GSXRPluginAndroidOther.Create();
  20. } else {
  21. if (GSXRManager.Instance != null) {
  22. if (GSXRManager.Instance.specificationType == GSXRManager.SpecificationType.GSXR) {
  23. instance = GSXRPluginAndroid.Create();
  24. } else {
  25. instance = SvrPluginAndroid.Create();
  26. }
  27. }
  28. }
  29. }
  30. else
  31. {
  32. instance = GSXRPluginWin.Create();
  33. }
  34. }
  35. return instance;
  36. }
  37. }
  38. public GSXRManager svrCamera = null;
  39. public GSXREye[] eyes = null;
  40. public GSXROverlay[] overlays = null;
  41. public DeviceInfo deviceInfo;
  42. public enum PerfLevel
  43. {
  44. kPerfSystem = 0,
  45. kPerfMaximum = 1,
  46. kPerfNormal = 2,
  47. kPerfMinimum = 3
  48. }
  49. public enum TrackingMode
  50. {
  51. kTrackingOrientation = (1 << 0),
  52. kTrackingPosition = (1 << 1),
  53. kTrackingEye = (1 << 2),
  54. }
  55. public enum EyePoseStatus
  56. {
  57. kGazePointValid = (1 << 0),
  58. kGazeVectorValid = (1 << 1),
  59. kEyeOpennessValid = (1 << 2),
  60. kEyePupilDilationValid = (1 << 3),
  61. kEyePositionGuideValid = (1 << 4)
  62. };
  63. public enum FrameOption
  64. {
  65. kDisableDistortionCorrection = (1 << 0), //!< Disables the lens distortion correction (useful for debugging)
  66. kDisableReprojection = (1 << 1), //!< Disables re-projection
  67. kEnableMotionToPhoton = (1 << 2), //!< Enables motion to photon testing
  68. kDisableChromaticCorrection = (1 << 3) //!< Disables the lens chromatic aberration correction (performance optimization)
  69. };
  70. public struct HeadPose
  71. {
  72. public Vector3 position;
  73. public Quaternion orientation;
  74. }
  75. public struct EyePose
  76. {
  77. public int leftStatus; //!< Bit field (svrEyePoseStatus) indicating left eye pose status
  78. public int rightStatus; //!< Bit field (svrEyePoseStatus) indicating right eye pose status
  79. public int combinedStatus; //!< Bit field (svrEyePoseStatus) indicating combined eye pose status
  80. public Vector3 leftPosition; //!< Left Eye Gaze Point
  81. public Vector3 rightPosition; //!< Right Eye Gaze Point
  82. public Vector3 combinedPosition; //!< Combined Eye Gaze Point (HMD center-eye point)
  83. public Vector3 leftDirection; //!< Left Eye Gaze Point
  84. public Vector3 rightDirection; //!< Right Eye Gaze Point
  85. public Vector3 combinedDirection; //!< Comnbined Eye Gaze Vector (HMD center-eye point)
  86. public float leftOpenness; //!< Left eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  87. public float rightOpenness; //!< Right eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  88. //public float leftDilation; //!< Left eye value in millimeters indicating the pupil dilation
  89. //public float rightDilation; //!< Right eye value in millimeters indicating the pupil dilation
  90. //public Vector3 leftGuide; //!< Position of the inner corner of the left eye in meters from the HMD center-eye coordinate system's origin.
  91. //public Vector3 rightGuide; //!< Position of the inner corner of the right eye in meters from the HMD center-eye coordinate system's origin.
  92. }
  93. public struct ViewFrustum
  94. {
  95. public float left; //!< Left Plane of Frustum
  96. public float right; //!< Right Plane of Frustum
  97. public float top; //!< Top Plane of Frustum
  98. public float bottom; //!< Bottom Plane of Frustum
  99. public float near; //!< Near Plane of Frustum
  100. public float far; //!< Far Plane of Frustum (Arbitrary)
  101. }
  102. public struct DeviceInfo
  103. {
  104. public int displayWidthPixels;
  105. public int displayHeightPixels;
  106. public float displayRefreshRateHz;
  107. public int targetEyeWidthPixels;
  108. public int targetEyeHeightPixels;
  109. public float targetFovXRad;
  110. public float targetFovYRad;
  111. public ViewFrustum targetFrustumLeft;
  112. public ViewFrustum targetFrustumRight;
  113. public float targetFrustumConvergence;
  114. public float targetFrustumPitch;
  115. }
  116. public virtual int GetPredictedPoseModify(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  117. {
  118. return 0;
  119. }
  120. public virtual bool PollEvent(ref GSXRManager.SvrEvent frameEvent) { return false; }
  121. public virtual bool IsInitialized() { return false; }
  122. public virtual bool IsRunning() { return false; }
  123. public virtual IEnumerator Initialize ()
  124. {
  125. svrCamera = GSXRManager.Instance;
  126. if (svrCamera == null)
  127. {
  128. Debug.LogError("GSXRManager object not found!");
  129. yield break;
  130. }
  131. yield break;
  132. }
  133. public virtual IEnumerator BeginVr(int cpuPerfLevel =0, int gpuPerfLevel =0)
  134. {
  135. if (eyes == null)
  136. {
  137. eyes = GSXREye.Instances.ToArray();
  138. if (eyes == null)
  139. {
  140. Debug.Log("Components with GSXREye not found!");
  141. }
  142. Array.Sort(eyes);
  143. }
  144. if (overlays == null)
  145. {
  146. overlays = GSXROverlay.Instances.ToArray();
  147. if (overlays == null)
  148. {
  149. Debug.Log("Components with GSXROverlay not found!");
  150. }
  151. Array.Sort(overlays);
  152. }
  153. yield break;
  154. }
  155. public virtual void EndVr()
  156. {
  157. eyes = null;
  158. overlays = null;
  159. }
  160. public virtual void BeginEye(int sideMask, float[] frameDelta) { }
  161. public virtual void EndEye(int sideMask, int layerMask) { }
  162. public virtual void SetTrackingMode(int mode) { }
  163. public virtual void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum) {}
  164. public virtual void ApplyFoveation() { }
  165. public virtual int GetTrackingMode() { return 0; }
  166. public virtual void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel) { }
  167. public virtual void SetFrameOption(FrameOption frameOption) { }
  168. public virtual void UnsetFrameOption(FrameOption frameOption) { }
  169. public virtual void SetVSyncCount(int vSyncCount) { }
  170. public virtual bool RecenterTracking() { return true; }
  171. public virtual void SubmitFrame(int frameIndex, float fieldOfView, int frameType) { }
  172. public virtual int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  173. {
  174. orientation = Quaternion.identity;
  175. position = Vector3.zero;
  176. return 0;
  177. }
  178. public virtual int GetHeadPose(ref HeadPose headPose, int frameIndex = -1)
  179. {
  180. headPose.orientation = Quaternion.identity;
  181. headPose.position = Vector3.zero;
  182. return 0;
  183. }
  184. public virtual int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  185. {
  186. eyePose.leftStatus = 0;
  187. eyePose.rightStatus = 0;
  188. eyePose.combinedStatus = 0;
  189. return 0;
  190. }
  191. public abstract DeviceInfo GetDeviceInfo ();
  192. public virtual void Shutdown()
  193. {
  194. GSXRPlugin.instance = null;
  195. }
  196. //---------------------------------------------------------------------------------------------
  197. public virtual int ControllerStartTracking(string desc) {
  198. return -1;
  199. }
  200. //---------------------------------------------------------------------------------------------
  201. public virtual void ControllerStopTracking(int handle) {
  202. }
  203. //---------------------------------------------------------------------------------------------
  204. public virtual GSXRControllerState ControllerGetState(int handle, int space) {
  205. return new GSXRControllerState();
  206. }
  207. //---------------------------------------------------------------------------------------------
  208. public virtual void ControllerSendMessage(int handle, GSXRController.svrControllerMessageType what, int arg1, int arg2) {
  209. }
  210. //---------------------------------------------------------------------------------------------
  211. public virtual object ControllerQuery(int handle, GSXRController.svrControllerQueryType what) {
  212. return null;
  213. }
  214. //-----------------------KS---------------------
  215. //-----------------------For A11G---------------------
  216. //-----------------------For HeadsetInput---------------------
  217. //-----------------------For GreyCamera---------------------
  218. #region HMD
  219. public virtual bool HeadSetEnterKeyDown() { return Input.GetMouseButtonDown(0); }
  220. public virtual bool HeadSetEnterKeyUp() { return Input.GetMouseButtonUp(0); }
  221. public virtual bool HeadSetBackKeyDown() { return Input.GetKeyDown(KeyCode.Escape); }
  222. public virtual bool HeadSetBackKeyUp() { return Input.GetKeyUp(KeyCode.Escape); }
  223. #endregion HMD
  224. #region Controller
  225. public delegate void OnKeyEvent(int keycode, int action, int lr);
  226. public delegate void OnKeyTouchEvent(bool key1, bool key2, bool key3, bool key4, int lr);
  227. public delegate void OnTouchEvent(int touch_x, int touch_y, int lr);
  228. public delegate void OnHallEvent(int hall_x, int hall_y, int lr);
  229. public delegate void OnChargingEvent(bool isCharging, int lr);
  230. public delegate void OnBatteryEvent(int battery, int lr);
  231. public delegate void OnConnectEvent(bool isConnected, int lr);
  232. public virtual bool GSXR_Is_SupportController() { return false; }
  233. public virtual int GSXR_Get_ControllerMode() { return 3; }
  234. public virtual int GSXR_Get_ControllerNum() { return 2; }
  235. public virtual void GSXR_Set_ControllerKeyEventCallback(OnKeyEvent _event) { }
  236. public virtual void GSXR_Set_ControllerKeyTouchEventCallback(OnKeyTouchEvent _event) { }
  237. public virtual void GSXR_Set_ControllerRockerCallback(OnTouchEvent _event) { }
  238. public virtual void GSXR_Set_ControllerHallCallback(OnHallEvent _event) { }
  239. public virtual void GSXR_Set_ControllerChargingEventCallback(OnChargingEvent _event) { }
  240. public virtual void GSXR_Set_ControllerBatteryEventCallback(OnBatteryEvent _event) { }
  241. public virtual void GSXR_Set_ControllerConnectEventCallback(OnConnectEvent _event) { }
  242. public virtual int GSXR_Get_ControllerBattery(int lr) { return -1; }
  243. public virtual int GSXR_Get_ControllerVersion(int lr) { return -1; }
  244. public virtual int GSXR_Get_ControllerList(int lr) { return -1; }
  245. public virtual bool GSXR_Is_ControllerConnect(int lr) { return false; }
  246. public virtual void GSXR_Set_ControllerLed(int lr) { return; }
  247. public virtual void GSXR_Set_ControllerVibrate(int lr) { return; }
  248. public virtual int GSXR_Get_ControllerPosture(float[] outOrientationArray, int lr) { return -1; }
  249. [Obsolete("Only For SVR")]
  250. public virtual int Fetch3dofHandShank(float[] outOrientationArray, int lr) { return -1; }
  251. [Obsolete("Only For SVR")]
  252. public virtual int Fetch6dofHandShank(float[] outOrientationArray, int lr) { return -1; }
  253. #endregion Controller
  254. #region HandTracking
  255. public virtual bool GSXR_Is_SupportHandTracking() { return false; }
  256. public virtual void GSXR_StartHandTracking(Action<int> func) { }
  257. public virtual void GSXR_StopHandTracking() { }
  258. public virtual void GSXR_Get_HandTrackingData(float[] mode, float[] pose) { return; }
  259. public virtual int GSXR_Set_HandTrackingData(float[] mode, float[] pose) { return 0; }
  260. public virtual int GSXR_Get_HandTrackingGestureIdx(ref UInt64 index, float[] model, float[] pose) { return 0; }
  261. public virtual void GSXR_Set_HandTrackingCallBack(Action<int> callback) { }
  262. public virtual int GSXR_Set_HandTrackingModelDataCallBack(Action callback) { return 0; }
  263. public virtual void GSXR_Set_HandTrackingLowPowerWarningCallback(Action<int> func) { }
  264. #endregion HandTracking
  265. #region Deflection
  266. public virtual float GSXR_Get_Deflection() { return 0; }
  267. public virtual void GSXR_Set_RelativeDeflection(int deflection) { }
  268. #endregion Deflection
  269. #region PointCloud & Map
  270. public virtual int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray) { return 0; }
  271. public virtual int GSXR_Get_OfflineMapRelocState() { return 0; }
  272. public virtual int GSXR_ResaveMap(string path) { return 0; }
  273. public virtual void GSXR_SaveMap() { }
  274. public virtual int GSXR_Get_Gnss(ref double dt, float[] gnss) { return 0; }
  275. public virtual int GSXR_Get_PanelNum() { return 0; }
  276. public virtual int GSXR_Get_PanelInfo(float[] info) { return 0; }
  277. #endregion PointCloud & Map
  278. #region Grey Camera Data
  279. public virtual void GSXR_Get_LatestQVRCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  280. byte[] outFrameData, float[] outTRDataArray) { outBUdate = false; }
  281. public virtual void GSXR_Get_LatestQVRCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  282. byte[] outFrameData, float[] outTRDataArray) { outBUdate = false; }
  283. public virtual int GSXR_Get_LatestQVRCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData) { outBUdate = false; return 0; }
  284. #endregion Grey Camera Data
  285. #region Optics Calibration
  286. public Matrix4x4 leftViewMatrix, rightViewMatrix;
  287. public virtual bool GSXR_Is_SupportOpticsCalibration() { return false; }
  288. public virtual int GSXR_Get_TransformMatrix(ref bool outBLoaded, float[] outTransformArray) { outBLoaded = false; return 0; }
  289. public virtual int GSXR_Get_LatestEyeMatrices(float[] outLeftEyeMatrix,
  290. float[] outRightEyeMatrix,
  291. float[] outT,
  292. float[] outR,
  293. int frameIndex,
  294. bool isMultiThreadedRender) { return 0; }
  295. #endregion Optics Calibration
  296. #region USBDisconnect
  297. public virtual void SetGlassDisconnectedCallBack(Action callBack) { }
  298. #endregion USBDisconnect
  299. #region luncher
  300. public virtual int initLayer() { return -1; }
  301. public virtual int startLayerRendering() { return -1; }
  302. public virtual int getAllLayersData(ref GSXRManager.SCAllLayers outAllLayers) { return -1; }
  303. public virtual int endLayerRendering(ref GSXRManager.SCAllLayers allLayers) { return -1; }
  304. public virtual int destroyLayer() { return -1; }
  305. public virtual int updateModelMatrix(UInt32 layerId, float[] modelMatrixArray) { return -1; }
  306. public virtual int sendActionBarCMD(UInt32 layerId, int cmd) { return -1; }
  307. public virtual int injectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y) { return -1; }
  308. #endregion luncher
  309. #region Device
  310. public virtual XRType GSXR_Get_XRType() { return API_Module_Device.Current.XRType; }
  311. public virtual void GSXR_Get_DeviceName(ref string deviceName) { deviceName = API_Module_Device.Current.modelName; }
  312. #endregion
  313. }