NativeHeadTrackingExtension.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. * NRSDK is distributed in the hope that it will be usefull
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental
  10. {
  11. using System;
  12. using UnityEngine;
  13. using System.Runtime.InteropServices;
  14. /// <summary> Only Internal Test Not Release !!!!! </summary>
  15. public static class NativeHeadTrackingExtension
  16. {
  17. /// <summary> A NativeHeadTracking extension method that gets eye pose. </summary>
  18. /// <param name="headTracking"> The headTracking to act on.</param>
  19. /// <param name="outLeftEyePose"> [in,out] The out left eye pose.</param>
  20. /// <param name="outRightEyePose"> [in,out] The out right eye pose.</param>
  21. /// <returns> The eye pose. </returns>
  22. public static NativeResult GetEyePose(this NativeHeadTracking headTracking, ref Pose outLeftEyePose, ref Pose outRightEyePose)
  23. {
  24. NativeMat4f lefteyepos = new NativeMat4f(Matrix4x4.identity);
  25. NativeMat4f righteyepos = new NativeMat4f(Matrix4x4.identity);
  26. NativeResult result = NativeApi.NRHeadTrackingGetEyePose(headTracking.TrackingHandle, headTracking.HeadTrackingHandle, ref lefteyepos, ref righteyepos);
  27. if (result == NativeResult.Success)
  28. {
  29. ConversionUtility.ApiPoseToUnityPose(lefteyepos, out outLeftEyePose);
  30. ConversionUtility.ApiPoseToUnityPose(righteyepos, out outRightEyePose);
  31. }
  32. NRDebugger.Info("[NativeHeadTracking] GetEyePose :" + result);
  33. return result;
  34. }
  35. /// <summary> A NativeHeadTracking extension method that gets projection matrix. </summary>
  36. /// <param name="headTracking"> The headTracking to act on.</param>
  37. /// <param name="outLeftProjectionMatrix"> [in,out] The out left projection matrix.</param>
  38. /// <param name="outRightProjectionMatrix"> [in,out] The out right projection matrix.</param>
  39. /// <returns> True if it succeeds, false if it fails. </returns>
  40. public static bool GetProjectionMatrix(this NativeHeadTracking headTracking, ref Matrix4x4 outLeftProjectionMatrix, ref Matrix4x4 outRightProjectionMatrix)
  41. {
  42. NativeMat4f projectmatrix = new NativeMat4f(Matrix4x4.identity);
  43. NativeResult result_left = NativeApi.NRInternalGetProjectionMatrix((int)NativeDevice.LEFT_DISPLAY, ref projectmatrix);
  44. outLeftProjectionMatrix = projectmatrix.ToUnityMat4f();
  45. NativeResult result_right = NativeApi.NRInternalGetProjectionMatrix((int)NativeDevice.RIGHT_DISPLAY, ref projectmatrix);
  46. outRightProjectionMatrix = projectmatrix.ToUnityMat4f();
  47. return (result_left == NativeResult.Success && result_right == NativeResult.Success);
  48. }
  49. /// <summary> A native api. </summary>
  50. private struct NativeApi
  51. {
  52. /// <summary> Nr head tracking get eye pose. </summary>
  53. /// <param name="session_handle"> Handle of the session.</param>
  54. /// <param name="head_tracking_handle"> Handle of the head tracking.</param>
  55. /// <param name="left_eye"> [in,out] The left eye.</param>
  56. /// <param name="right_eye"> [in,out] The right eye.</param>
  57. /// <returns> A NativeResult. </returns>
  58. [DllImport(NativeConstants.NRNativeLibrary)]
  59. public static extern NativeResult NRHeadTrackingGetEyePose(UInt64 session_handle, UInt64 head_tracking_handle, ref NativeMat4f left_eye, ref NativeMat4f right_eye);
  60. /// <summary> Nr internal get projection matrix. </summary>
  61. /// <param name="index"> Zero-based index of the.</param>
  62. /// <param name="eye"> [in,out] The eye.</param>
  63. /// <returns> A NativeResult. </returns>
  64. [DllImport(NativeConstants.NRNativeLibrary)]
  65. public static extern NativeResult NRInternalGetProjectionMatrix(int index, ref NativeMat4f eye);
  66. };
  67. }
  68. }