SvrPlugin.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. public abstract class SvrPlugin
  6. {
  7. private static SvrPlugin instance;
  8. public static SvrPlugin Instance
  9. {
  10. get
  11. {
  12. if (instance == null)
  13. {
  14. if(!Application.isEditor && Application.platform == RuntimePlatform.Android)
  15. {
  16. instance = SvrPluginAndroid.Create();
  17. }
  18. else
  19. {
  20. instance = SvrPluginWin.Create();
  21. }
  22. }
  23. return instance;
  24. }
  25. }
  26. public SvrManager svrCamera = null;
  27. public SvrEye[] eyes = null;
  28. public SvrOverlay[] overlays = null;
  29. public DeviceInfo deviceInfo;
  30. public enum PerfLevel
  31. {
  32. kPerfSystem = 0,
  33. kPerfMaximum = 1,
  34. kPerfNormal = 2,
  35. kPerfMinimum = 3
  36. }
  37. public enum TrackingMode
  38. {
  39. kTrackingOrientation = (1 << 0),
  40. kTrackingPosition = (1 << 1),
  41. kTrackingEye = (1 << 2),
  42. }
  43. public enum EyePoseStatus
  44. {
  45. kGazePointValid = (1 << 0),
  46. kGazeVectorValid = (1 << 1),
  47. kEyeOpennessValid = (1 << 2),
  48. kEyePupilDilationValid = (1 << 3),
  49. kEyePositionGuideValid = (1 << 4)
  50. };
  51. public enum FrameOption
  52. {
  53. kDisableDistortionCorrection = (1 << 0), //!< Disables the lens distortion correction (useful for debugging)
  54. kDisableReprojection = (1 << 1), //!< Disables re-projection
  55. kEnableMotionToPhoton = (1 << 2), //!< Enables motion to photon testing
  56. kDisableChromaticCorrection = (1 << 3) //!< Disables the lens chromatic aberration correction (performance optimization)
  57. };
  58. public struct HeadPose
  59. {
  60. public Vector3 position;
  61. public Quaternion orientation;
  62. }
  63. public struct EyePose
  64. {
  65. public int leftStatus; //!< Bit field (svrEyePoseStatus) indicating left eye pose status
  66. public int rightStatus; //!< Bit field (svrEyePoseStatus) indicating right eye pose status
  67. public int combinedStatus; //!< Bit field (svrEyePoseStatus) indicating combined eye pose status
  68. public Vector3 leftPosition; //!< Left Eye Gaze Point
  69. public Vector3 rightPosition; //!< Right Eye Gaze Point
  70. public Vector3 combinedPosition; //!< Combined Eye Gaze Point (HMD center-eye point)
  71. public Vector3 leftDirection; //!< Left Eye Gaze Point
  72. public Vector3 rightDirection; //!< Right Eye Gaze Point
  73. public Vector3 combinedDirection; //!< Comnbined Eye Gaze Vector (HMD center-eye point)
  74. public float leftOpenness; //!< Left eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  75. public float rightOpenness; //!< Right eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  76. //public float leftDilation; //!< Left eye value in millimeters indicating the pupil dilation
  77. //public float rightDilation; //!< Right eye value in millimeters indicating the pupil dilation
  78. //public Vector3 leftGuide; //!< Position of the inner corner of the left eye in meters from the HMD center-eye coordinate system's origin.
  79. //public Vector3 rightGuide; //!< Position of the inner corner of the right eye in meters from the HMD center-eye coordinate system's origin.
  80. }
  81. public struct ViewFrustum
  82. {
  83. public float left; //!< Left Plane of Frustum
  84. public float right; //!< Right Plane of Frustum
  85. public float top; //!< Top Plane of Frustum
  86. public float bottom; //!< Bottom Plane of Frustum
  87. public float near; //!< Near Plane of Frustum
  88. public float far; //!< Far Plane of Frustum (Arbitrary)
  89. }
  90. public struct DeviceInfo
  91. {
  92. public int displayWidthPixels;
  93. public int displayHeightPixels;
  94. public float displayRefreshRateHz;
  95. public int targetEyeWidthPixels;
  96. public int targetEyeHeightPixels;
  97. public float targetFovXRad;
  98. public float targetFovYRad;
  99. public ViewFrustum targetFrustumLeft;
  100. public ViewFrustum targetFrustumRight;
  101. public float targetFrustumConvergence;
  102. public float targetFrustumPitch;
  103. }
  104. public virtual int GetPredictedPoseModify(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  105. {
  106. return 0;
  107. }
  108. public virtual bool PollEvent(ref SvrManager.SvrEvent frameEvent) { return false; }
  109. public virtual bool IsInitialized() { return false; }
  110. public virtual bool IsRunning() { return false; }
  111. public virtual IEnumerator Initialize ()
  112. {
  113. svrCamera = SvrManager.Instance;
  114. if (svrCamera == null)
  115. {
  116. Debug.LogError("SvrManager object not found!");
  117. yield break;
  118. }
  119. yield break;
  120. }
  121. public virtual IEnumerator BeginVr(int cpuPerfLevel =0, int gpuPerfLevel =0)
  122. {
  123. if (eyes == null)
  124. {
  125. eyes = SvrEye.Instances.ToArray();
  126. if (eyes == null)
  127. {
  128. Debug.Log("Components with SvrEye not found!");
  129. }
  130. Array.Sort(eyes);
  131. }
  132. if (overlays == null)
  133. {
  134. overlays = SvrOverlay.Instances.ToArray();
  135. if (overlays == null)
  136. {
  137. Debug.Log("Components with SvrOverlay not found!");
  138. }
  139. Array.Sort(overlays);
  140. }
  141. yield break;
  142. }
  143. public virtual void EndVr()
  144. {
  145. eyes = null;
  146. overlays = null;
  147. }
  148. public virtual void BeginEye(int sideMask, float[] frameDelta) { }
  149. public virtual void EndEye(int sideMask, int layerMask) { }
  150. public virtual void SetTrackingMode(int mode) { }
  151. public virtual void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum) {}
  152. public virtual void ApplyFoveation() { }
  153. public virtual int GetTrackingMode() { return 0; }
  154. public virtual void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel) { }
  155. public virtual void SetFrameOption(FrameOption frameOption) { }
  156. public virtual void UnsetFrameOption(FrameOption frameOption) { }
  157. public virtual void SetVSyncCount(int vSyncCount) { }
  158. public virtual bool RecenterTracking() { return true; }
  159. public virtual void SubmitFrame(int frameIndex, float fieldOfView, int frameType) { }
  160. public virtual int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  161. {
  162. orientation = Quaternion.identity;
  163. position = Vector3.zero;
  164. return 0;
  165. }
  166. public virtual int GetHeadPose(ref HeadPose headPose, int frameIndex = -1)
  167. {
  168. headPose.orientation = Quaternion.identity;
  169. headPose.position = Vector3.zero;
  170. return 0;
  171. }
  172. public virtual int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  173. {
  174. eyePose.leftStatus = 0;
  175. eyePose.rightStatus = 0;
  176. eyePose.combinedStatus = 0;
  177. return 0;
  178. }
  179. public abstract DeviceInfo GetDeviceInfo ();
  180. public virtual int SVRGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray) { return 0; }
  181. public Matrix4x4 leftViewMatrix, rightViewMatrix;
  182. public virtual int SVRGetLatestEyeMatrices(float[] outLeftEyeMatrix,
  183. float[] outRightEyeMatrix,
  184. float[] outT,
  185. float[] outR,
  186. int frameIndex,
  187. bool isMultiThreadedRender) { return 0; }
  188. public virtual void Shutdown()
  189. {
  190. SvrPlugin.instance = null;
  191. }
  192. //---------------------------------------------------------------------------------------------
  193. public virtual int ControllerStartTracking(string desc) {
  194. return -1;
  195. }
  196. //---------------------------------------------------------------------------------------------
  197. public virtual void ControllerStopTracking(int handle) {
  198. }
  199. //---------------------------------------------------------------------------------------------
  200. public virtual SvrControllerState ControllerGetState(int handle, int space) {
  201. return new SvrControllerState();
  202. }
  203. //---------------------------------------------------------------------------------------------
  204. public virtual void ControllerSendMessage(int handle, SvrController.svrControllerMessageType what, int arg1, int arg2) {
  205. }
  206. //---------------------------------------------------------------------------------------------
  207. public virtual object ControllerQuery(int handle, SvrController.svrControllerQueryType what) {
  208. return null;
  209. }
  210. public virtual void StartHandTracking() {}
  211. public virtual void StopHandTracking() {}
  212. public virtual void GetHandData(float[] mode, float[] pose) {
  213. //public virtual int GetHandData(ref HandPose[] handsPos) {
  214. return ;
  215. }
  216. //-----------------------KS---------------------
  217. public delegate void OnKeyEvent(int keycode, int action, int lr);
  218. public delegate void OnKeyTouchEvent(bool key1, bool key2, bool key3, bool key4, 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 virtual void HandShank_SetKeyEventCallback(OnKeyEvent _event) { }
  225. public virtual void HandShank_SetKeyTouchEventCallback(OnKeyTouchEvent _event) { }
  226. public virtual void HandShank_SetTouchEventCallback(OnTouchEvent _event) { }
  227. public virtual void HandShank_SetHallEventCallback(OnHallEvent _event) { }
  228. public virtual void HandShank_SetChargingEventCallback(OnChargingEvent _event) { }
  229. public virtual void HandShank_SetBatteryEventCallback(OnBatteryEvent _event) { }
  230. public virtual void HandShank_SetConnectEventCallback(OnConnectEvent _event) { }
  231. public virtual int HandShank_GetBattery(int lr) { return -1; }
  232. public virtual int HandShank_GetVersion(int lr) { return -1; }
  233. public virtual int HandShank_Getbond(int lr) { return -1; }
  234. public virtual bool HandShank_GetConnectState(int lr) { return false; }
  235. public virtual int Fetch3dofHandShank(float[] outOrientationArray, int lr) { return -1; }
  236. public virtual int Fetch6dofHandShank(float[] outOrientationArray, int lr) { return -1; }
  237. //-----------------------For A11G---------------------
  238. public delegate void GlassDisconnectedCallBack();
  239. public virtual void SetGlassDisconnectedCallBack(GlassDisconnectedCallBack callBack) { }
  240. //-----------------------For HeadsetInput---------------------
  241. public virtual bool HeadSetEnterKeyDown() { return Input.GetMouseButtonDown(0); }
  242. public virtual bool HeadSetEnterKeyUp() { return Input.GetMouseButtonUp(0); }
  243. public virtual bool HeadSetBackKeyDown() { return Input.GetKeyDown(KeyCode.Escape); }
  244. public virtual bool HeadSetBackKeyUp() { return Input.GetKeyUp(KeyCode.Escape); }
  245. }