SvrPluginAndroid.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. class SvrPluginAndroid : SvrPlugin
  6. {
  7. [DllImport("svrplugin")]
  8. private static extern IntPtr GetRenderEventFunc();
  9. [DllImport("svrplugin")]
  10. private static extern bool SvrIsInitialized();
  11. [DllImport("svrplugin")]
  12. private static extern bool SvrIsRunning();
  13. [DllImport("svrplugin")]
  14. private static extern bool SvrCanBeginVR();
  15. [DllImport("svrplugin")]
  16. private static extern void SvrInitializeEventData(IntPtr activity);
  17. [DllImport("svrplugin")]
  18. private static extern void SvrSubmitFrameEventData(int frameIndex, float fieldOfView, int frameType);
  19. [DllImport("svrplugin")]
  20. private static extern void SvrSetupLayerCoords(int layerIndex, float[] lowerLeft, float[] lowerRight, float[] upperLeft, float[] upperRight);
  21. [DllImport("svrplugin")]
  22. private static extern void SvrSetupLayerData(int layerIndex, int sideMask, int textureId, int textureType, int layerFlags);
  23. [DllImport("svrplugin")]
  24. private static extern void SvrSetTrackingModeEventData(int mode);
  25. [DllImport("svrplugin")]
  26. private static extern void SvrSetPerformanceLevelsEventData(int newCpuPerfLevel, int newGpuPerfLevel);
  27. [DllImport("svrplugin")]
  28. private static extern void SvrSetEyeEventData(int sideMask, int layerMask);
  29. [DllImport("svrplugin")]
  30. private static extern void SvrSetColorSpace(int colorSpace);
  31. [DllImport("svrplugin")]
  32. private static extern void SvrSetFrameOption(uint frameOption);
  33. [DllImport("svrplugin")]
  34. private static extern void SvrUnsetFrameOption(uint frameOption);
  35. [DllImport("svrplugin")]
  36. private static extern void SvrSetVSyncCount(int vSyncCount);
  37. [DllImport("svrplugin")]
  38. private static extern int SvrGetPredictedPose(ref float rx,
  39. ref float ry,
  40. ref float rz,
  41. ref float rw,
  42. ref float px,
  43. ref float py,
  44. ref float pz,
  45. int frameIndex,
  46. bool isMultiThreadedRender);
  47. [DllImport("svrplugin")]
  48. private static extern int SvrGetPredictedPoseModify(ref float rx,
  49. ref float ry,
  50. ref float rz,
  51. ref float rw,
  52. ref float px,
  53. ref float py,
  54. ref float pz,
  55. int frameIndex,
  56. bool isMultiThreadedRender);
  57. [DllImport("svrplugin")]
  58. private static extern int SvrGetEyePose(ref int leftStatus,
  59. ref int rightStatus,
  60. ref int combinedStatus,
  61. ref float leftOpenness,
  62. ref float rightOpenness,
  63. ref float leftDirectionX,
  64. ref float leftDirectionY,
  65. ref float leftDirectionZ,
  66. ref float leftPositionX,
  67. ref float leftPositionY,
  68. ref float leftPositionZ,
  69. ref float rightDirectionX,
  70. ref float rightDirectionY,
  71. ref float rightDirectionZ,
  72. ref float rightPositionX,
  73. ref float rightPositionY,
  74. ref float rightPositionZ,
  75. ref float combinedDirectionX,
  76. ref float combinedDirectionY,
  77. ref float combinedDirectionZ,
  78. ref float combinedPositionX,
  79. ref float combinedPositionY,
  80. ref float combinedPositionZ,
  81. int frameIndex);
  82. [DllImport("svrplugin")]
  83. private static extern bool SvrRecenterTrackingPose();
  84. [DllImport("svrplugin")]
  85. private static extern int SvrGetTrackingMode();
  86. [DllImport("svrplugin")]
  87. private static extern void SvrGetDeviceInfo(ref int displayWidthPixels,
  88. ref int displayHeightPixels,
  89. ref float displayRefreshRateHz,
  90. ref int targetEyeWidthPixels,
  91. ref int targetEyeHeightPixels,
  92. ref float targetFovXRad,
  93. ref float targetFovYRad,
  94. ref float leftFrustumLeft, ref float leftFrustumRight, ref float leftFrustumBottom, ref float leftFrustumTop, ref float leftFrustumNear, ref float leftEyeFrustumFar,
  95. ref float rightFrustumLeft, ref float rightFrustumRight, ref float rightFrustumBottom, ref float rightFrustumTop, ref float rightFrustumNear, ref float rightFrustumFar,
  96. ref float targetfrustumConvergence, ref float targetFrustumPitch);
  97. [DllImport("svrplugin")]
  98. private static extern void SvrSetFrameOffset(float[] delta);
  99. [DllImport("svrplugin")]
  100. private static extern void SvrSetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum);
  101. [DllImport("svrplugin")]
  102. private static extern bool SvrPollEvent(ref int eventType, ref uint deviceId, ref float eventTimeStamp, int eventDataCount, uint[] eventData);
  103. [DllImport("svrplugin")]
  104. private static extern bool SvrGetTransformMatrix(ref bool outBLoaded, ref uint deviceId, ref float eventTimeStamp, int eventDataCount, uint[] eventData);
  105. [DllImport("svrplugin")]
  106. private static extern int SvrGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray);
  107. //---------------------------------------------------------------------------------------------
  108. // Conotroller Apis
  109. //---------------------------------------------------------------------------------------------
  110. [DllImport("svrplugin")]
  111. private static extern int SvrControllerStartTracking(string desc);
  112. [DllImport("svrplugin")]
  113. private static extern void SvrControllerStopTracking(int handle);
  114. [DllImport("svrplugin")]
  115. private static extern void SvrControllerGetState(int handle, int space, ref SvrControllerState state);
  116. [DllImport("svrplugin")]
  117. private static extern void SvrControllerSendMessage (int handle, int what, int arg1, int arg2);
  118. [DllImport("svrplugin")]
  119. private static extern int SvrControllerQuery (int handle, int what, IntPtr mem, int size);
  120. [DllImport("svrplugin")]
  121. private static extern void InvisionStartHandTracking();
  122. [DllImport("svrplugin")]
  123. private static extern void InvisionStopHandTracking();
  124. [DllImport("svrplugin")]
  125. private static extern void InvisionGetHandData(float[] model, float[] pose);
  126. //---------------------------------------------------------------------------------------------
  127. //---------------------------KS-----------------------------
  128. [DllImport("svrplugin")]
  129. public static extern void ScHandShank_SetKeyEventCallback(OnKeyEvent _event);
  130. [DllImport("svrplugin")]
  131. public static extern void ScHandShank_SetKeyTouchEventCallback(OnKeyTouchEvent _event);
  132. [DllImport("svrplugin")]
  133. public static extern void ScHandShank_SetTouchEventCallback(OnTouchEvent _event);
  134. [DllImport("svrplugin")]
  135. public static extern void ScHandShank_SetHallEventCallback(OnHallEvent _event);
  136. [DllImport("svrplugin")]
  137. public static extern void ScHandShank_SetChargingEventCallback(OnChargingEvent _event);
  138. [DllImport("svrplugin")]
  139. public static extern void ScHandShank_SetBatteryEventCallback(OnBatteryEvent _event);
  140. [DllImport("svrplugin")]
  141. public static extern void ScHandShank_SetConnectEventCallback(OnConnectEvent _event);
  142. [DllImport("svrplugin")]
  143. public static extern int ScHandShank_GetBattery(int lr);
  144. [DllImport("svrplugin")]
  145. public static extern int ScHandShank_GetVersion(int lr);
  146. [DllImport("svrplugin")]
  147. public static extern int ScHandShank_Getbond(int lr);
  148. [DllImport("svrplugin")]
  149. public static extern bool ScHandShank_GetConnectState(int lr);
  150. [DllImport("svrplugin")]
  151. public static extern int ScFetch3dofHandShank(float[] outOrientationArray, int lr);
  152. [DllImport("svrplugin")]
  153. public static extern int ScFetch6dofHandShank(float[] outOrientationArray, int lr);
  154. public override void HandShank_SetKeyEventCallback(OnKeyEvent _event) { ScHandShank_SetKeyEventCallback(_event); }
  155. public override void HandShank_SetKeyTouchEventCallback(OnKeyTouchEvent _event) { ScHandShank_SetKeyTouchEventCallback(_event); }
  156. public override void HandShank_SetTouchEventCallback(OnTouchEvent _event) { ScHandShank_SetTouchEventCallback(_event); }
  157. public override void HandShank_SetHallEventCallback(OnHallEvent _event) { ScHandShank_SetHallEventCallback(_event); }
  158. public override void HandShank_SetChargingEventCallback(OnChargingEvent _event) { ScHandShank_SetChargingEventCallback(_event); }
  159. public override void HandShank_SetBatteryEventCallback(OnBatteryEvent _event) { ScHandShank_SetBatteryEventCallback(_event); }
  160. public override void HandShank_SetConnectEventCallback(OnConnectEvent _event) { ScHandShank_SetConnectEventCallback(_event); }
  161. public override int HandShank_GetBattery(int lr) { return ScHandShank_GetBattery(lr); }
  162. public override int HandShank_GetVersion(int lr) { return ScHandShank_Getbond(lr); }
  163. public override int HandShank_Getbond(int lr) { return ScHandShank_Getbond(lr); }
  164. public override bool HandShank_GetConnectState(int lr) { return ScHandShank_GetConnectState(lr); }
  165. public override int Fetch3dofHandShank(float[] outOrientationArray,int lr) { return ScFetch3dofHandShank(outOrientationArray,lr); }
  166. public override int Fetch6dofHandShank(float[] outOrientationArray,int lr) { return ScFetch6dofHandShank(outOrientationArray,lr); }
  167. //-------------------------------------------------------------
  168. [DllImport("svrplugin")]
  169. public static extern int SvrGetLatestQVRCameraFrameData(ref bool outBUpdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outFrameData, float[] outTRDataArray);
  170. [DllImport("svrplugin")]
  171. public static extern int SvrGetLatestQVRCameraFrameDataNoTransform(ref bool outBUpdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outFrameData, float[] outTRDataArray);
  172. [DllImport("svrplugin")]
  173. private static extern int SvrGetLatestEyeMatrices(float[] outLeftEyeMatrix,
  174. float[] outRightEyeMatrix,
  175. float[] outT,
  176. float[] outR,
  177. int frameIndex,
  178. bool isMultiThreadedRender);
  179. public override int SVRGetLatestEyeMatrices(float[] outLeftEyeMatrix, float[] outRightEyeMatrix, float[] outT, float[] outR,int frameIndex,bool isMultiThreadedRender) {
  180. return SvrGetLatestEyeMatrices(outLeftEyeMatrix, outRightEyeMatrix, outT, outR, frameIndex, isMultiThreadedRender);
  181. }
  182. private enum RenderEvent
  183. {
  184. Initialize,
  185. BeginVr,
  186. EndVr,
  187. BeginEye,
  188. EndEye,
  189. SubmitFrame,
  190. Foveation,
  191. Shutdown,
  192. RecenterTracking,
  193. SetTrackingMode,
  194. SetPerformanceLevels
  195. };
  196. public static SvrPluginAndroid Create()
  197. {
  198. if(Application.isEditor)
  199. {
  200. Debug.LogError("SvrPlugin not supported in unity editor!");
  201. throw new InvalidOperationException();
  202. }
  203. return new SvrPluginAndroid ();
  204. }
  205. private SvrPluginAndroid() {}
  206. private void IssueEvent(RenderEvent e)
  207. {
  208. // Queue a specific callback to be called on the render thread
  209. GL.IssuePluginEvent(GetRenderEventFunc(), (int)e);
  210. }
  211. public override bool IsInitialized() { return SvrIsInitialized(); }
  212. public override bool IsRunning() { return SvrIsRunning(); }
  213. public override IEnumerator Initialize()
  214. {
  215. //yield return new WaitUntil(() => SvrIsInitialized() == false); // Wait for shutdown
  216. yield return base.Initialize();
  217. if(Application.platform == RuntimePlatform.Android) {
  218. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  219. AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  220. SvrInitializeEventData(activity.GetRawObject());
  221. }
  222. IssueEvent(RenderEvent.Initialize);
  223. yield return new WaitUntil (() => SvrIsInitialized () == true);
  224. yield return null; // delay one frame - fix for re-init w multi-threaded rendering
  225. deviceInfo = GetDeviceInfo();
  226. }
  227. public override IEnumerator BeginVr(int cpuPerfLevel, int gpuPerfLevel)
  228. {
  229. //yield return new WaitUntil(() => SvrIsRunning() == false); // Wait for EndVr
  230. yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel);
  231. // float[6]: x, y, z, w, u, v
  232. float[] lowerLeft = { -1f, -1f, 0f, 1f, 0f, 0f };
  233. float[] upperLeft = { -1f, 1f, 0f, 1f, 0f, 1f };
  234. float[] upperRight = { 1f, 1f, 0f, 1f, 1f, 1f };
  235. float[] lowerRight = { 1f, -1f, 0f, 1f, 1f, 0f };
  236. SvrSetupLayerCoords(-1, lowerLeft, lowerRight, upperLeft, upperRight); // Layers/All
  237. SvrSetPerformanceLevelsEventData(cpuPerfLevel, gpuPerfLevel);
  238. ColorSpace space = QualitySettings.activeColorSpace;
  239. if(space == ColorSpace.Gamma)
  240. {
  241. // 0 == kColorSpaceLinear from svrApi.h
  242. SvrSetColorSpace(0); //Unity will be supplying gamma space eye buffers into warp so we want
  243. //to setup a linear color space display surface so no further gamma
  244. //correction is performed
  245. }
  246. else
  247. {
  248. // 1 == kColorSpaceSRGB from svrApi.h
  249. SvrSetColorSpace(1); //Unity will be supplying linear space eye buffers into warp so we want
  250. //to setup an sRGB color space display surface to properly convert
  251. //incoming linear values into sRGB
  252. }
  253. yield return new WaitUntil(() => SvrCanBeginVR() == true);
  254. IssueEvent (RenderEvent.BeginVr);
  255. //yield return new WaitUntil(() => SvrIsRunning() == true);
  256. }
  257. public override void EndVr()
  258. {
  259. base.EndVr();
  260. Debug.Log("SVRW End VR...."+Time.frameCount);
  261. IssueEvent (RenderEvent.EndVr);
  262. }
  263. public override void BeginEye(int sideMask, float[] frameDelta)
  264. {
  265. SvrSetFrameOffset(frameDelta); // Enabled for foveation head orientation delta
  266. SvrSetEyeEventData(sideMask, 0);
  267. IssueEvent (RenderEvent.BeginEye);
  268. }
  269. public override void EndEye(int sideMask, int layerMask)
  270. {
  271. SvrSetEyeEventData(sideMask, layerMask);
  272. IssueEvent(RenderEvent.EndEye);
  273. }
  274. public override void SetTrackingMode(int modeMask)
  275. {
  276. SvrSetTrackingModeEventData(modeMask);
  277. IssueEvent (RenderEvent.SetTrackingMode);
  278. }
  279. public override void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum)
  280. {
  281. SvrSetFoveationParameters(textureId, previousId, focalPointX, focalPointY, foveationGainX, foveationGainY, foveationArea, foveationMinimum);
  282. }
  283. public override void ApplyFoveation()
  284. {
  285. IssueEvent(RenderEvent.Foveation);
  286. }
  287. public override int GetTrackingMode()
  288. {
  289. return SvrGetTrackingMode();
  290. }
  291. public override void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel)
  292. {
  293. SvrSetPerformanceLevelsEventData((int)newCpuPerfLevel, (int)newGpuPerfLevel);
  294. IssueEvent (RenderEvent.SetPerformanceLevels);
  295. }
  296. public override void SetFrameOption(FrameOption frameOption)
  297. {
  298. SvrSetFrameOption((uint)frameOption);
  299. }
  300. public override void UnsetFrameOption(FrameOption frameOption)
  301. {
  302. SvrUnsetFrameOption((uint)frameOption);
  303. }
  304. public override void SetVSyncCount(int vSyncCount)
  305. {
  306. SvrSetVSyncCount(vSyncCount);
  307. }
  308. public override bool RecenterTracking()
  309. {
  310. //IssueEvent (RenderEvent.RecenterTracking);
  311. return SvrRecenterTrackingPose();
  312. }
  313. public override int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex)
  314. {
  315. orientation.z = -orientation.z;
  316. position.x = -position.x;
  317. position.y = -position.y;
  318. int rv = SvrGetPredictedPose(ref orientation.x, ref orientation.y, ref orientation.z, ref orientation.w,
  319. ref position.x, ref position.y, ref position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  320. orientation.z = -orientation.z;
  321. position.x = -position.x;
  322. position.y = -position.y;
  323. return rv;
  324. }
  325. private float[] leftViewData = new float[16];
  326. private float[] rightViewData = new float[16];
  327. private float[] TData = new float[3];
  328. private float[] RData = new float[4];
  329. public override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  330. {
  331. int rv1,rv2;
  332. headPose.orientation.z = -headPose.orientation.z;
  333. headPose.position.x = -headPose.position.x;
  334. headPose.position.y = -headPose.position.y;
  335. rv1 = SvrGetPredictedPose(ref headPose.orientation.x, ref headPose.orientation.y, ref headPose.orientation.z, ref headPose.orientation.w,
  336. ref headPose.position.x, ref headPose.position.y, ref headPose.position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  337. rv2 = SVRGetLatestEyeMatrices(leftViewData, rightViewData, TData, RData, frameIndex, SystemInfo.graphicsMultiThreaded);
  338. if (Input.GetMouseButton(0)) {
  339. headPose.orientation.z = -headPose.orientation.z;
  340. headPose.position.x = -headPose.position.x;
  341. headPose.position.y = -headPose.position.y;
  342. return rv1;
  343. } else {
  344. leftViewMatrix.SetColumn(0, new Vector4(leftViewData[0], leftViewData[1], leftViewData[2], leftViewData[3]));
  345. leftViewMatrix.SetColumn(1, new Vector4(leftViewData[4], leftViewData[5], leftViewData[6], leftViewData[7]));
  346. leftViewMatrix.SetColumn(2, new Vector4(leftViewData[8], leftViewData[9], leftViewData[10], leftViewData[11]));
  347. leftViewMatrix.SetColumn(3, new Vector4(leftViewData[12], leftViewData[13], leftViewData[14], leftViewData[15]));
  348. rightViewMatrix.SetColumn(0, new Vector4(rightViewData[0], rightViewData[1], rightViewData[2], rightViewData[3]));
  349. rightViewMatrix.SetColumn(1, new Vector4(rightViewData[4], rightViewData[5], rightViewData[6], rightViewData[7]));
  350. rightViewMatrix.SetColumn(2, new Vector4(rightViewData[8], rightViewData[9], rightViewData[10], rightViewData[11]));
  351. rightViewMatrix.SetColumn(3, new Vector4(rightViewData[12], rightViewData[13], rightViewData[14], rightViewData[15]));
  352. headPose.orientation.x = RData[0];
  353. headPose.orientation.y = RData[1];
  354. headPose.orientation.z = RData[2];
  355. headPose.orientation.w = RData[3];
  356. headPose.position.x = TData[0];
  357. headPose.position.y = TData[1];
  358. headPose.position.z = TData[2];
  359. headPose.orientation.z = -headPose.orientation.z;
  360. headPose.position.x = -headPose.position.x;
  361. headPose.position.y = -headPose.position.y;
  362. return rv2;
  363. }
  364. // Debug.Log($"leftViewMatrix [{leftViewMatrix.m00}, {leftViewMatrix.m01}, {leftViewMatrix.m02}, {leftViewMatrix.m03};" +
  365. //$"{leftViewMatrix.m10}, {leftViewMatrix.m11}, {leftViewMatrix.m12}, {leftViewMatrix.m13};" +
  366. //$"{leftViewMatrix.m20}, {leftViewMatrix.m21}, {leftViewMatrix.m22}, {leftViewMatrix.m23};" +
  367. //$"{leftViewMatrix.m30}, {leftViewMatrix.m31}, {leftViewMatrix.m32}, {leftViewMatrix.m33}]");
  368. // Debug.Log($"rightViewMatrix [{rightViewMatrix.m00}, {rightViewMatrix.m01}, {rightViewMatrix.m02}, {rightViewMatrix.m03};" +
  369. //$"{rightViewMatrix.m10}, {rightViewMatrix.m11}, {rightViewMatrix.m12}, {rightViewMatrix.m13};" +
  370. //$"{rightViewMatrix.m20}, {rightViewMatrix.m21}, {rightViewMatrix.m22}, {rightViewMatrix.m23};" +
  371. //$"{rightViewMatrix.m30}, {rightViewMatrix.m31}, {rightViewMatrix.m32}, {rightViewMatrix.m33}]");
  372. }
  373. public override int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  374. {
  375. int rv = SvrGetEyePose(
  376. ref eyePose.leftStatus, ref eyePose.rightStatus, ref eyePose.combinedStatus,
  377. ref eyePose.leftOpenness, ref eyePose.rightOpenness,
  378. ref eyePose.leftDirection.x, ref eyePose.leftDirection.y, ref eyePose.leftDirection.z,
  379. ref eyePose.leftPosition.x, ref eyePose.leftPosition.y, ref eyePose.leftPosition.z,
  380. ref eyePose.rightDirection.x, ref eyePose.rightDirection.y, ref eyePose.rightDirection.z,
  381. ref eyePose.rightPosition.x, ref eyePose.rightPosition.y, ref eyePose.rightPosition.z,
  382. ref eyePose.combinedDirection.x, ref eyePose.combinedDirection.y, ref eyePose.combinedDirection.z,
  383. ref eyePose.combinedPosition.x, ref eyePose.combinedPosition.y, ref eyePose.combinedPosition.z,
  384. frameIndex);
  385. return rv;
  386. }
  387. public override int GetPredictedPoseModify(ref Quaternion orientation, ref Vector3 position, int frameIndex)
  388. {
  389. orientation.z = -orientation.z;
  390. position.x = -position.x;
  391. position.y = -position.y;
  392. int rv = SvrGetPredictedPoseModify(ref orientation.x, ref orientation.y, ref orientation.z, ref orientation.w,
  393. ref position.x, ref position.y, ref position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  394. orientation.z = -orientation.z;
  395. position.x = -position.x;
  396. position.y = -position.y;
  397. return rv;
  398. }
  399. public override DeviceInfo GetDeviceInfo()
  400. {
  401. DeviceInfo info = new DeviceInfo();
  402. SvrGetDeviceInfo (ref info.displayWidthPixels,
  403. ref info.displayHeightPixels,
  404. ref info.displayRefreshRateHz,
  405. ref info.targetEyeWidthPixels,
  406. ref info.targetEyeHeightPixels,
  407. ref info.targetFovXRad,
  408. ref info.targetFovYRad,
  409. ref info.targetFrustumLeft.left, ref info.targetFrustumLeft.right, ref info.targetFrustumLeft.bottom, ref info.targetFrustumLeft.top, ref info.targetFrustumLeft.near, ref info.targetFrustumLeft.far,
  410. ref info.targetFrustumRight.left, ref info.targetFrustumRight.right, ref info.targetFrustumRight.bottom, ref info.targetFrustumRight.top, ref info.targetFrustumRight.near, ref info.targetFrustumRight.far,
  411. ref info.targetFrustumConvergence, ref info.targetFrustumPitch);
  412. return info;
  413. }
  414. public override int SVRGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray) {
  415. return SvrGetTransformMatrix(ref outBLoaded, outTransformArray);
  416. }
  417. public override void SubmitFrame(int frameIndex, float fieldOfView, int frameType)
  418. {
  419. int i;
  420. int layerCount = 0;
  421. if (eyes != null) for (i = 0; i < eyes.Length; i++)
  422. {
  423. var eye = eyes[i];
  424. if (eyes[i].isActiveAndEnabled == false || eye.TextureId == 0 || eye.Side == 0) continue;
  425. if (eye.imageTransform != null && eye.imageTransform.gameObject.activeSelf == false) continue;
  426. SvrSetupLayerData(layerCount, (int)eye.Side, eye.TextureId, eye.ImageType == SvrEye.eType.EglTexture ? 2 : 0, eye.layerDepth > 0 ? 0x0 : 0x2);
  427. float[] lowerLeft = { eye.clipLowerLeft.x, eye.clipLowerLeft.y, eye.clipLowerLeft.z, eye.clipLowerLeft.w, eye.uvLowerLeft.x, eye.uvLowerLeft.y };
  428. float[] upperLeft = { eye.clipUpperLeft.x, eye.clipUpperLeft.y, eye.clipUpperLeft.z, eye.clipUpperLeft.w, eye.uvUpperLeft.x, eye.uvUpperLeft.y };
  429. float[] upperRight = { eye.clipUpperRight.x, eye.clipUpperRight.y, eye.clipUpperRight.z, eye.clipUpperRight.w, eye.uvUpperRight.x, eye.uvUpperRight.y };
  430. float[] lowerRight = { eye.clipLowerRight.x, eye.clipLowerRight.y, eye.clipLowerRight.z, eye.clipLowerRight.w, eye.uvLowerRight.x, eye.uvLowerRight.y };
  431. SvrSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  432. layerCount++;
  433. }
  434. if (overlays != null) for (i = 0; i < overlays.Length; i++)
  435. {
  436. var overlay = overlays[i];
  437. if (overlay.isActiveAndEnabled == false || overlay.TextureId == 0 || overlay.Side == 0) continue;
  438. if (overlay.imageTransform != null && overlay.imageTransform.gameObject.activeSelf == false) continue;
  439. SvrSetupLayerData(layerCount, (int)overlay.Side, overlay.TextureId, overlay.ImageType == SvrOverlay.eType.EglTexture ? 2 : 0, 0x1);
  440. float[] lowerLeft = { overlay.clipLowerLeft.x, overlay.clipLowerLeft.y, overlay.clipLowerLeft.z, overlay.clipLowerLeft.w, overlay.uvLowerLeft.x, overlay.uvLowerLeft.y };
  441. float[] upperLeft = { overlay.clipUpperLeft.x, overlay.clipUpperLeft.y, overlay.clipUpperLeft.z, overlay.clipUpperLeft.w, overlay.uvUpperLeft.x, overlay.uvUpperLeft.y };
  442. float[] upperRight = { overlay.clipUpperRight.x, overlay.clipUpperRight.y, overlay.clipUpperRight.z, overlay.clipUpperRight.w, overlay.uvUpperRight.x, overlay.uvUpperRight.y };
  443. float[] lowerRight = { overlay.clipLowerRight.x, overlay.clipLowerRight.y, overlay.clipLowerRight.z, overlay.clipLowerRight.w, overlay.uvLowerRight.x, overlay.uvLowerRight.y };
  444. SvrSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  445. layerCount++;
  446. }
  447. for (i = layerCount; i < SvrManager.RenderLayersMax; i++)
  448. {
  449. SvrSetupLayerData(i, 0, 0, 0, 0);
  450. }
  451. SvrSubmitFrameEventData(frameIndex, fieldOfView, frameType);
  452. IssueEvent (RenderEvent.SubmitFrame);
  453. }
  454. public override void Shutdown()
  455. {
  456. IssueEvent (RenderEvent.Shutdown);
  457. base.Shutdown();
  458. }
  459. public override bool PollEvent(ref SvrManager.SvrEvent frameEvent)
  460. {
  461. uint[] dataBuffer = new uint[2];
  462. int dataCount = Marshal.SizeOf(frameEvent.eventData) / sizeof(uint);
  463. int eventType = 0;
  464. bool isEvent = SvrPollEvent(ref eventType, ref frameEvent.deviceId, ref frameEvent.eventTimeStamp, dataCount, dataBuffer);
  465. frameEvent.eventType = (SvrManager.svrEventType)(eventType);
  466. switch (frameEvent.eventType)
  467. {
  468. case SvrManager.svrEventType.kEventThermal:
  469. //Debug.LogFormat("PollEvent: data {0} {1}", dataBuffer[0], dataBuffer[1]);
  470. frameEvent.eventData.thermal.zone = (SvrManager.svrThermalZone)dataBuffer[0];
  471. frameEvent.eventData.thermal.level = (SvrManager.svrThermalLevel)dataBuffer[1];
  472. break;
  473. }
  474. return isEvent;
  475. }
  476. //---------------------------------------------------------------------------------------------
  477. //Controller Apis
  478. //---------------------------------------------------------------------------------------------
  479. /// <summary>
  480. /// Controllers the start tracking.
  481. /// </summary>
  482. /// <returns>The start tracking.</returns>
  483. /// <param name="desc">Desc.</param>
  484. //---------------------------------------------------------------------------------------------
  485. public override int ControllerStartTracking(string desc)
  486. {
  487. return SvrControllerStartTracking(desc);
  488. }
  489. /// <summary>
  490. /// Controllers the stop tracking.
  491. /// </summary>
  492. /// <param name="handle">Handle.</param>
  493. //---------------------------------------------------------------------------------------------
  494. public override void ControllerStopTracking(int handle)
  495. {
  496. SvrControllerStopTracking(handle);
  497. }
  498. /// <summary>
  499. /// Dumps the state.
  500. /// </summary>
  501. /// <param name="state">State.</param>
  502. //---------------------------------------------------------------------------------------------
  503. private void dumpState(SvrControllerState state)
  504. {
  505. String s = "{" + state.rotation + "}\n";
  506. s += "[" + state.position + "]\n";
  507. s += "<" + state.timestamp + ">\n";
  508. Debug.Log (s);
  509. }
  510. /// <summary>
  511. /// Controllers the state of the get.
  512. /// </summary>
  513. /// <returns>The get state.</returns>
  514. /// <param name="handle">Handle.</param>
  515. //---------------------------------------------------------------------------------------------
  516. public override SvrControllerState ControllerGetState(int handle, int space)
  517. {
  518. SvrControllerState state = new SvrControllerState();
  519. SvrControllerGetState (handle, space, ref state);
  520. //dumpState (state);
  521. return state;
  522. }
  523. /// <summary>
  524. /// Controllers the send event.
  525. /// </summary>
  526. /// <param name="handle">Handle.</param>
  527. /// <param name="what">What.</param>
  528. /// <param name="arg1">Arg1.</param>
  529. /// <param name="arg2">Arg2.</param>
  530. //---------------------------------------------------------------------------------------------
  531. public override void ControllerSendMessage(int handle, SvrController.svrControllerMessageType what, int arg1, int arg2)
  532. {
  533. SvrControllerSendMessage (handle, (int)what, arg1, arg2);
  534. }
  535. /// <summary>
  536. /// Controllers the query.
  537. /// </summary>
  538. /// <returns>The query.</returns>
  539. /// <param name="handle">Handle.</param>
  540. /// <param name="what">What.</param>
  541. /// <param name="mem">Mem.</param>
  542. /// <param name="size">Size.</param>
  543. //---------------------------------------------------------------------------------------------
  544. public override object ControllerQuery(int handle, SvrController.svrControllerQueryType what)
  545. {
  546. int memorySize = 0;
  547. IntPtr memory = IntPtr.Zero;
  548. object result = null;
  549. System.Type typeOfObject = null;
  550. switch(what)
  551. {
  552. case SvrController.svrControllerQueryType.kControllerBatteryRemaining:
  553. {
  554. typeOfObject = typeof(int);
  555. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  556. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  557. }
  558. break;
  559. case SvrController.svrControllerQueryType.kControllerControllerCaps:
  560. {
  561. typeOfObject = typeof(SvrControllerCaps);
  562. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  563. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  564. }
  565. break;
  566. }
  567. int writtenBytes = SvrControllerQuery (handle, (int)what, memory, memorySize);
  568. if (memorySize == writtenBytes) {
  569. result = System.Runtime.InteropServices.Marshal.PtrToStructure (memory, typeOfObject);
  570. }
  571. if (memory != IntPtr.Zero) {
  572. Marshal.FreeHGlobal (memory);
  573. }
  574. return result;
  575. }
  576. public override void StartHandTracking() {
  577. InvisionStartHandTracking();
  578. }
  579. public override void StopHandTracking() {
  580. InvisionStopHandTracking();
  581. }
  582. public override void GetHandData(float[] mode, float[] pose) {
  583. InvisionGetHandData(mode, pose);
  584. }
  585. //-----------------------For A11G---------------------
  586. [DllImport("svrplugin")]
  587. public static extern void Sc_setAppExitCallback(GlassDisconnectedCallBack callBack);
  588. public override void SetGlassDisconnectedCallBack(GlassDisconnectedCallBack callBack) {
  589. Sc_setAppExitCallback(callBack);
  590. }
  591. }