123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
-
- namespace NRKernal
- {
- using System;
- using System.Runtime.InteropServices;
- using UnityEngine;
-
- public class NativeHeadTracking
- {
-
- private NativeInterface m_NativeInterface;
-
-
- public UInt64 TrackingHandle
- {
- get
- {
- return m_NativeInterface.TrackingHandle;
- }
- }
-
- private UInt64 m_HeadTrackingHandle = 0;
-
-
- public UInt64 HeadTrackingHandle
- {
- get
- {
- return m_HeadTrackingHandle;
- }
- }
-
-
- public NativeHeadTracking(NativeInterface nativeInterface)
- {
- m_NativeInterface = nativeInterface;
- }
-
-
- public bool Start()
- {
- if (m_NativeInterface.TrackingHandle == 0)
- {
- return false;
- }
- var result = NativeApi.NRHeadTrackingCreate(m_NativeInterface.TrackingHandle, ref m_HeadTrackingHandle);
- NativeErrorListener.Check(result, this, "Create");
- return result == NativeResult.Success;
- }
-
-
-
-
-
- public bool GetHeadPose(ref Pose pose, UInt64 timestamp)
- {
- if (m_HeadTrackingHandle == 0)
- {
- pose = Pose.identity;
- return false;
- }
- UInt64 headPoseHandle = 0;
- var acquireTrackingPoseResult = NativeApi.NRHeadTrackingAcquireTrackingPose(m_NativeInterface.TrackingHandle, m_HeadTrackingHandle, timestamp, ref headPoseHandle);
- if (acquireTrackingPoseResult != NativeResult.Success)
- {
- return false;
- }
- NativeMat4f headpos_native = new NativeMat4f(Matrix4x4.identity);
- var getPoseResult = NativeApi.NRTrackingPoseGetPose(m_NativeInterface.TrackingHandle, headPoseHandle, ref headpos_native);
- if (getPoseResult != NativeResult.Success)
- {
- return false;
- }
- ConversionUtility.ApiPoseToUnityPose(headpos_native, out pose);
- NativeApi.NRTrackingPoseDestroy(m_NativeInterface.TrackingHandle, headPoseHandle);
- return (acquireTrackingPoseResult == NativeResult.Success) && (getPoseResult == NativeResult.Success);
- }
- public bool GetHeadPoseRecommend(ref Pose pose)
- {
- if (m_HeadTrackingHandle == 0)
- {
- pose = Pose.identity;
- return false;
- }
- ulong recommend_time = 0;
- ulong predict_time = 0;
- var result = NativeApi.NRTrackingGetHMDTimeNanos(m_NativeInterface.TrackingHandle, ref recommend_time);
- if (result != NativeResult.Success)
- {
- return false;
- }
- result = NativeApi.NRHeadTrackingGetRecommendPredictTime(m_NativeInterface.TrackingHandle, m_HeadTrackingHandle, ref predict_time);
- if (result != NativeResult.Success)
- {
- return false;
- }
- recommend_time += predict_time;
- return GetHeadPose(ref pose, recommend_time);
- }
- public ulong GetHMDTimeNanos()
- {
- ulong timestamp = 0;
- NativeApi.NRTrackingGetHMDTimeNanos(m_NativeInterface.TrackingHandle, ref timestamp);
- return timestamp;
- }
- public bool GetFramePresentHeadPose(ref Pose pose, ref LostTrackingReason lostReason, ref UInt64 timestamp)
- {
- if (m_HeadTrackingHandle == 0 || m_NativeInterface.TrackingHandle == 0)
- {
- NRDebugger.Error("[NativeTrack] GetFramePresentHeadPose: trackingHandle is zero");
- pose = Pose.identity;
- timestamp = 0;
- return false;
- }
- UInt64 headPoseHandle = 0;
- m_NativeInterface.NativeRenderring.GetFramePresentTime(ref timestamp);
- var acquireTrackingPoseResult = NativeApi.NRHeadTrackingAcquireTrackingPose(m_NativeInterface.TrackingHandle, m_HeadTrackingHandle, timestamp, ref headPoseHandle);
- if (acquireTrackingPoseResult != NativeResult.Success)
- {
-
- lostReason = LostTrackingReason.PRE_INITIALIZING;
- return true;
- }
- var trackReasonRst = NativeApi.NRTrackingPoseGetTrackingReason(m_NativeInterface.TrackingHandle, headPoseHandle, ref lostReason);
- NativeErrorListener.Check(trackReasonRst, this, "GetFramePresentHeadPose-LostReason");
-
- NativeMat4f headpos_native = new NativeMat4f(Matrix4x4.identity);
- var getPoseResult = NativeApi.NRTrackingPoseGetPose(m_NativeInterface.TrackingHandle, headPoseHandle, ref headpos_native);
- NativeErrorListener.Check(getPoseResult, this, "GetFramePresentHeadPose");
- ConversionUtility.ApiPoseToUnityPose(headpos_native, out pose);
- if (float.IsNaN(pose.position.x) || float.IsNaN(pose.position.y) || float.IsNaN(pose.position.z) ||
- float.IsNaN(pose.rotation.x) || float.IsNaN(pose.rotation.y) || float.IsNaN(pose.rotation.z) || float.IsNaN(pose.rotation.w))
- {
- NRDebugger.Error("[NativeTrack] GetFramePresentHeadPose invalid: lostReason={0}, unityPose=\n{1}\nnativePose=\n{2}", lostReason, pose.ToString("F6"), headpos_native.ToString());
- }
- NativeApi.NRTrackingPoseDestroy(m_NativeInterface.TrackingHandle, headPoseHandle);
- return true;
- }
- public bool GetFramePresentTimeByCount(int count, ref UInt64 timestamp)
- {
- return m_NativeInterface.NativeRenderring.GetFramePresentTimeByCount(ref timestamp, count);
- }
-
- public void Destroy()
- {
- if (m_HeadTrackingHandle == 0)
- {
- return;
- }
- var result = NativeApi.NRHeadTrackingDestroy(m_NativeInterface.TrackingHandle, m_HeadTrackingHandle);
- m_HeadTrackingHandle = 0;
- NativeErrorListener.Check(result, this, "Destroy");
- }
- private partial struct NativeApi
- {
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRHeadTrackingCreate(UInt64 tracking_handle,
- ref UInt64 outHeadTrackingHandle);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRTrackingGetHMDTimeNanos(UInt64 tracking_handle,
- ref UInt64 out_hmd_time_nanos);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRHeadTrackingGetRecommendPredictTime(
- UInt64 tracking_handle, UInt64 head_tracking_handle, ref UInt64 out_predict_time_nanos);
-
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRHeadTrackingAcquireTrackingPose(UInt64 sessionHandle,
- UInt64 head_tracking_handle, UInt64 hmd_time_nanos, ref UInt64 out_tracking_pose_handle);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRTrackingPoseGetPose(UInt64 tracking_handle,
- UInt64 tracking_pose_handle, ref NativeMat4f out_pose);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRTrackingPoseGetTrackingReason(UInt64 tracking_handle,
- UInt64 tracking_pose_handle, ref LostTrackingReason out_tracking_reason);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRTrackingPoseDestroy(UInt64 tracking_handle, UInt64 tracking_pose_handle);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRHeadTrackingDestroy(UInt64 tracking_handle, UInt64 head_tracking_handle);
- };
- }
- }
|