SvrPlugin.cs 12 KB

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