SvrPlugin.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using System;
  4. using System.Collections;
  5. using System.Runtime.InteropServices;
  6. namespace Ximmerse.XR
  7. {
  8. public abstract class SvrPlugin
  9. {
  10. private static SvrPlugin instance = null;
  11. public enum DeviceModel
  12. {
  13. Default = 0x100,
  14. RhinoXPro = 0x200,
  15. HMD20 = 0x300,
  16. RhinoXH = 0x400
  17. };
  18. public static DeviceModel deviceModel = DeviceModel.Default;
  19. public static SvrPlugin Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. {
  25. if(!Application.isEditor && Application.platform == RuntimePlatform.Android)
  26. {
  27. //deviceModel = SvrManager.Instance.settings.deviceModel;
  28. switch (deviceModel) {
  29. case DeviceModel.Default:
  30. case DeviceModel.RhinoXPro:
  31. case DeviceModel.HMD20:
  32. {
  33. instance = SvrPluginAndroid.Create();
  34. }
  35. break;
  36. case DeviceModel.RhinoXH:
  37. {
  38. // instance = SvrPluginHiXR.Create();
  39. }
  40. break;
  41. default:break;
  42. }
  43. }
  44. else
  45. {
  46. //instance = SvrPluginWin.Create();
  47. }
  48. }
  49. return instance;
  50. }
  51. }
  52. public SvrManager svrCamera = null;
  53. public DeviceInfo deviceInfo;
  54. public CommandBuffer beginEyeCommandBuffer = null;
  55. public enum EyeMask
  56. {
  57. kLeft = 0x00000001,
  58. kRight = 0x00000002,
  59. kBoth = 0x00000003,
  60. };
  61. public enum TextureType
  62. {
  63. kTypeTexture = 0, //!< Standard texture
  64. kTypeTextureArray, //!< Standard texture array (Left eye is first layer, right eye is second layer)
  65. kTypeImage, //!< EGL Image texture
  66. kTypeEquiRectTexture, //!< Equirectangular texture
  67. kTypeEquiRectImage, //!< Equirectangular Image texture
  68. kTypeCubemapTexture, //!< Cubemap texture (Not supporting cubemap image)
  69. kTypeVulkan, //!< Vulkan texture
  70. kTypeCamera, //!< Video camera frame texture
  71. };
  72. public enum LayerFlags
  73. {
  74. kLayerFlagNone = 0x00000000,
  75. kLayerFlagHeadLocked = 0x00000001,
  76. kLayerFlagOpaque = 0x00000002,
  77. kLayerFlagSubsampled = 0x00000004,
  78. };
  79. public enum PerfLevel
  80. {
  81. kPerfSystem = 0,
  82. kPerfMaximum = 1,
  83. kPerfNormal = 2,
  84. kPerfMinimum = 3
  85. }
  86. public enum TrackingMode
  87. {
  88. kTrackingOrientation = (1 << 0),
  89. kTrackingPosition = (1 << 1),
  90. kTrackingEye = (1 << 2),
  91. }
  92. public enum EyePoseStatus
  93. {
  94. kGazePointValid = (1 << 0),
  95. kGazeVectorValid = (1 << 1),
  96. kEyeOpennessValid = (1 << 2),
  97. kEyePupilDilationValid = (1 << 3),
  98. kEyePositionGuideValid = (1 << 4),
  99. kEyeBlinkValid = (1 << 5),
  100. };
  101. public enum ServiceCapabilities
  102. {
  103. kCapabilityCombinedGaze = 0x00000001,
  104. kCapabilityConvergenceDistance = 0x00000002,
  105. kCapabilityFoveatedGaze = 0x00000004,
  106. kCapabilityPerEyeGazeOrigin = 0x00000008,
  107. kCapabilityPerEyeGazeDirection = 0x00000010,
  108. kCapabilityPerEyeGazePoint = 0x00000020,
  109. kCapabilityPerEyeGazeOpenness = 0x00000040,
  110. kCapabilityPerEyePupilDilation = 0x00000080,
  111. kCapabilityPerEyePositionGuide = 0x00000100,
  112. kCapabilityPerEyeBlink = 0x00000200,
  113. };
  114. public enum FrameOption
  115. {
  116. kDisableDistortionCorrection = (1 << 0), //!< Disables the lens distortion correction (useful for debugging)
  117. kDisableReprojection = (1 << 1), //!< Disables re-projection
  118. kEnableMotionToPhoton = (1 << 2), //!< Enables motion to photon testing
  119. kDisableChromaticCorrection = (1 << 3) //!< Disables the lens chromatic aberration correction (performance optimization)
  120. };
  121. public struct HeadPose
  122. {
  123. public UInt64 timestamp;
  124. public Vector3 position;
  125. public Quaternion orientation;
  126. }
  127. public struct EyePose
  128. {
  129. public UInt64 timestamp; //!< Eye pose timestamp
  130. public int leftStatus; //!< Bit field (svrEyePoseStatus) indicating left eye pose status
  131. public int rightStatus; //!< Bit field (svrEyePoseStatus) indicating right eye pose status
  132. public int combinedStatus; //!< Bit field (svrEyePoseStatus) indicating combined eye pose status
  133. public Vector3 leftPosition; //!< Left Eye Gaze Point
  134. public Vector3 rightPosition; //!< Right Eye Gaze Point
  135. public Vector3 combinedPosition; //!< Combined Eye Gaze Point (HMD center-eye point)
  136. public Vector3 leftDirection; //!< Left Eye Gaze Point
  137. public Vector3 rightDirection; //!< Right Eye Gaze Point
  138. public Vector3 combinedDirection; //!< Comnbined Eye Gaze Vector (HMD center-eye point)
  139. public bool leftBlink; //!< Left eye value indicating lid up or down.
  140. public bool rightBlink; //!< Right eye value indicating lid up or down.
  141. public float leftOpenness; //!< Left eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  142. public float rightOpenness; //!< Right eye value between 0.0 and 1.0 where 1.0 means fully open and 0.0 closed.
  143. public float leftDilation; //!< Left eye value in millimeters indicating the pupil dilation
  144. public float rightDilation; //!< Right eye value in millimeters indicating the pupil dilation
  145. public Vector3 leftGuide; //!< Position of the inner corner of the left eye in meters from the HMD center-eye coordinate system's origin.
  146. public Vector3 rightGuide; //!< Position of the inner corner of the right eye in meters from the HMD center-eye coordinate system's origin.
  147. }
  148. public struct ViewFrustum
  149. {
  150. public float left; //!< Left Plane of Frustum
  151. public float right; //!< Right Plane of Frustum
  152. public float top; //!< Top Plane of Frustum
  153. public float bottom; //!< Bottom Plane of Frustum
  154. public float near; //!< Near Plane of Frustum
  155. public float far; //!< Far Plane of Frustum (Arbitrary)
  156. public Vector3 position; //!< Position Offset of Frustum
  157. public Quaternion rotation; //!< Rotation Quaternion of Frustum
  158. }
  159. public struct Foveation
  160. {
  161. public Vector2 Gain; //!< Foveation Gain Rate [1, ...]
  162. public float Area; //!< Foveation Area Size [0, ...]
  163. public float Minimum; //!< Foveation Minimum Resolution [1, 1/2, 1/4, ..., 1/16, 0]
  164. }
  165. public struct CameraIntrinsics
  166. {
  167. public Vector2 PrincipalPoint;
  168. public Vector2 FocalLength;
  169. public float Distortion0;
  170. public float Distortion1;
  171. public float Distortion2;
  172. public float Distortion3;
  173. public float Distortion4;
  174. public float Distortion5;
  175. public float Distortion6;
  176. public float Distortion7;
  177. }
  178. public struct CloudPoint
  179. {
  180. public float x;
  181. public float y;
  182. public float z;
  183. public UInt32 id;
  184. }
  185. public struct AnchorUuid
  186. {
  187. [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
  188. public byte[] uuid;
  189. public override string ToString() { return SvrPlugin.Instance.AnchorToString(this); }
  190. }
  191. public struct AnchorPose
  192. {
  193. public Quaternion orientation;
  194. public Vector3 position;
  195. public float poseQuality;
  196. }
  197. public struct AnchorInfo
  198. {
  199. public AnchorUuid id;
  200. public UInt32 revision;
  201. public AnchorPose pose;
  202. }
  203. public struct DeviceInfo
  204. {
  205. public int displayWidthPixels;
  206. public int displayHeightPixels;
  207. public float displayRefreshRateHz;
  208. public int targetEyeWidthPixels;
  209. public int targetEyeHeightPixels;
  210. public float targetFovXRad;
  211. public float targetFovYRad;
  212. public ViewFrustum targetFrustumLeft;
  213. public ViewFrustum targetFrustumRight;
  214. public float targetFrustumConvergence;
  215. public float targetFrustumPitch;
  216. public Foveation lowFoveation;
  217. public Foveation medFoveation;
  218. public Foveation highFoveation;
  219. public Matrix4x4 trackingCalibration;
  220. public CameraIntrinsics trackingIntrinsics;
  221. public UInt64 trackingCapabilities; //ServiceCapabilities
  222. }
  223. // public virtual bool PollEvent(ref SvrManager.SvrEvent frameEvent) { return false; }
  224. public virtual bool IsInitialized() { return false; }
  225. public virtual bool IsRunning() { return false; }
  226. public virtual IEnumerator Initialize ()
  227. {
  228. svrCamera = SvrManager.Instance;
  229. if (svrCamera == null)
  230. {
  231. Debug.LogError("SvrManager object not found!");
  232. yield break;
  233. }
  234. yield break;
  235. }
  236. public virtual void BeginVr(int cpuPerfLevel, int gpuPerfLevel, int optionFlags)
  237. {
  238. // yield break;
  239. }
  240. public virtual void EndVr()
  241. {
  242. }
  243. public virtual void PauseXr() { }
  244. public virtual void ResumeXr() { }
  245. public virtual void BeginEye(int renderIndex, int sideMask, float[] frameDelta) { }
  246. public virtual void OccludeEye(int renderIndex, Matrix4x4 proj, Matrix4x4 view) { }
  247. public virtual void EndEye(int renderIndex, int sideMask, int layerMask) { }
  248. public virtual void SetTrackingMode(int mode) { }
  249. public virtual void SetFoveationParameters(int renderIndex, int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum) {}
  250. public virtual void ApplyFoveation() { }
  251. public virtual int GetTrackingMode() { return 0; }
  252. public virtual void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel) { }
  253. public virtual void SetFrameOption(FrameOption frameOption) { }
  254. public virtual void UnsetFrameOption(FrameOption frameOption) { }
  255. public virtual void SetVSyncCount(int vSyncCount) { }
  256. public virtual bool RecenterTracking() { return true; }
  257. public virtual void SubmitFrame(int frameIndex, float fieldOfView, int frameType) { }
  258. public virtual int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex = -1)
  259. {
  260. orientation = Quaternion.identity;
  261. position = Vector3.zero;
  262. return 0;
  263. }
  264. public virtual int GetHeadPose(ref HeadPose headPose, int frameIndex = -1)
  265. {
  266. headPose.timestamp = 0;
  267. headPose.orientation = Quaternion.identity;
  268. headPose.position = Vector3.zero;
  269. return 0;
  270. }
  271. public virtual int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  272. {
  273. eyePose.leftStatus = 0;
  274. eyePose.rightStatus = 0;
  275. eyePose.combinedStatus = 0;
  276. return 0;
  277. }
  278. public virtual int GetEyeFocalPoint(ref Vector2 focalPoint)
  279. {
  280. focalPoint = Vector2.zero;
  281. return 0;
  282. }
  283. public virtual bool Is3drOcclusion()
  284. {
  285. return false;
  286. }
  287. public virtual void GetOcclusionMesh()
  288. {
  289. }
  290. public abstract DeviceInfo GetDeviceInfo ();
  291. public virtual void Shutdown()
  292. {
  293. SvrPlugin.instance = null;
  294. }
  295. //---------------------------------------------------------------------------------------------
  296. public virtual int ControllerStartTracking(string desc) {
  297. return -1;
  298. }
  299. //---------------------------------------------------------------------------------------------
  300. public virtual void ControllerStopTracking(int handle) {
  301. }
  302. public virtual int CloudPointData(CloudPoint[] points) { return 0; }
  303. public virtual string AnchorToString(AnchorUuid id) { return string.Empty; }
  304. public virtual bool AnchorCreate(Vector3 position, Quaternion rotation, ref AnchorUuid id) { return true; }
  305. public virtual bool AnchorDestroy(AnchorUuid id) { return true; }
  306. public virtual bool AnchorSave(AnchorUuid id) { return true; }
  307. public virtual int AnchorGetData(AnchorInfo[] anchorData) { return 0; }
  308. public virtual bool AnchorStartRelocating() { return true; }
  309. public virtual bool AnchorStopRelocating() { return true; }
  310. }
  311. }