/// <summary> /// Updates benmarker system with algorithm rotating. /// </summary> void BenmarkerUpdate_AlgorithmRotating() { Vector3 rawPos = Vector3.zero; Quaternion rawRot = Quaternion.identity; m_SubMarkerMask = 0; if (!TagTrackingUtil.GetMarkerState(this.MarkerID, out rawPos, out rawRot, out m_SubMarkerMask)) { return; } if (m_FixedQuaternionError) { rawRot = rawRot * Quaternion.Euler(-90, 0, 0); } var trackingAnchor = TagTrackingUtil.GetNode(UnityEngine.XR.XRNode.TrackingReference); var head = TagTrackingUtil.GetNode(UnityEngine.XR.XRNode.CenterEye); if (this.SmoothMarkerRotation) { Quaternion rawRot1 = default(Quaternion); switch (this.smoothRotationMethod) { case SmoothRotationMethod.Lerp: rawRot1 = Quaternion.Lerp(prevRawQ, rawRot, this.SmoothRotationLerpRate); break; case SmoothRotationMethod.Angular: rawRot1 = Quaternion.RotateTowards(prevRawQ, rawRot, this.SmoothRotationAnglarSpeed * Time.deltaTime); break; } prevRawQ = rawRot; rawRot = rawRot1; } else { prevRawQ = rawRot; } Matrix4x4 markerWorldTRS = Matrix4x4.TRS(trackingAnchor.TransformPoint(rawPos), trackingAnchor.rotation * rawRot, Vector3.one).inverse; var headInMarkerWorldXYZ = markerWorldTRS.MultiplyPoint3x4(head.position); var headInMarkerWorldEuler = markerWorldTRS.rotation * head.rotation; if (this.PositionPrediction) { headInMarkerWorldXYZ = this.XYZPrediction.GetPrediction(headInMarkerWorldXYZ); } if (this.RotationPrediciton) { this.PosePrediction.Update(headInMarkerWorldEuler); headInMarkerWorldEuler = this.PosePrediction.GetRotationPrediction(headInMarkerWorldEuler, this.RotationPredictionDelayFrame); } var markerTrans = this.transform; head.position = markerTrans.TransformPoint(headInMarkerWorldXYZ); head.rotation = markerTrans.rotation * headInMarkerWorldEuler; }
/// <summary> /// Initializes the device module. /// </summary> public static void InitializeDeviceModule(bool isFirst = true) { if (isInitialized) { return; } if (Application.platform == RuntimePlatform.Android) { Ximmerse.InputSystem.XDevicePlugin.Init(); //Apply logs: XDevicePlugin.SetLogger((int level, string _tag, string log) => { Debug.LogFormat("level:{0} ,tag:{1}, log:{2}", level, _tag, log); }); if (isFirst) { XDevicePlugin.CopyAssetsToPath(kConfigFileDirectory); } DevicerHandle.SlideInContext = XDevicePlugin.NewContext(XDevicePlugin.XContextTypes.kXContextTypeSlideIn);/// 创建SlideIn设备上下文 DevicerHandle.HmdHandle = XDevicePlugin.GetDeviceHandle(DevicerHandle.SlideInContext, "XHawk-0"); if (isFirst) { TagTrackingUtil.ApplyDefaultConfig(); } DeviceConnectionState vpuConn = (DeviceConnectionState)XDevicePlugin.GetInt(DevicerHandle.HmdHandle, XDevicePlugin.XVpuAttributes.kXVpuAttr_Int_ConnectionState, 0); isVPUConnected = vpuConn == DeviceConnectionState.Connected; XDevicePlugin.RegisterObserver(DevicerHandle.HmdHandle, XDevicePlugin.XVpuAttributes.kXVpuAttr_Int_ConnectionState, new XDevicePlugin.XDeviceConnectStateChangeDelegate(OnVPUConnectionStateChanged), DevicerHandle.HmdHandle); isInitialized = true; Debug.LogFormat("Slide in HLAPI initialized successfully. HLAPI version : {0}, Algorithm version: {1}", HLAPIVersion.Version, HLAPIVersion.AlgVersion); Debug.LogFormat("VPU device state {0} ", vpuConn); } else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) { Ximmerse.InputSystem.XDevicePlugin.Init(); XDevicePlugin.SetLogger((int level, string _tag, string log) => { Debug.LogFormat("level:{0} ,tag:{1}, log:{2}", level, _tag, log); }); DevicerHandle.SlideInContext = XDevicePlugin.NewContext(XDevicePlugin.XContextTypes.kXContextTypeSlideIn);/// 创建SlideIn设备上下文 DevicerHandle.HmdHandle = XDevicePlugin.GetDeviceHandle(DevicerHandle.SlideInContext, "XHawk-0"); Debug.LogFormat("VPU Context: {0}, Hmd handle:{1}", DevicerHandle.SlideInContext.mNativeHandle.ToInt32(), DevicerHandle.HmdHandle.mNativeHandle.ToInt32()); TagTrackingUtil.ApplyDefaultConfig(); var vpuConn = (DeviceConnectionState)XDevicePlugin.GetInt(DevicerHandle.HmdHandle, XDevicePlugin.XVpuAttributes.kXVpuAttr_Int_ConnectionState, 0); isVPUConnected = vpuConn == DeviceConnectionState.Connected; XDevicePlugin.RegisterObserver(DevicerHandle.HmdHandle, XDevicePlugin.XVpuAttributes.kXVpuAttr_Int_ConnectionState, new XDevicePlugin.XDeviceConnectStateChangeDelegate(OnVPUConnectionStateChanged), DevicerHandle.HmdHandle); isInitialized = true; Debug.LogFormat("Slide in HLAPI initialized successfully. HLAPI version : {0}, Algorithm version: {1}", HLAPIVersion.Version, HLAPIVersion.AlgVersion); Debug.LogFormat("VPU device state {0} ", vpuConn); } else { Debug.LogWarningFormat("Tracking Library not available for current platform : {0}. Currently we supports Android/Windows platform.", Application.platform); } }
public void RevertToDefaultSetting() { var tagTracker = this; tagTracker.TrackingAnchorPosition = TrackingConfig.TrackingAnchor_Positional_Offset; tagTracker.TrackingAnchorTilt = TrackingConfig.TrackingAnchor_Euler_Offset; tagTracker.TrackingAnchorScale = TrackingConfig.TrackingAnchor_Scale; TagTrackingUtil.ApplyDefaultConfig(); }
/// <summary> /// Inits the tag tracking module. /// </summary> void InitTagTrackingModule() { //Create tracking reference anchor: m_TrackingAnchorPosition = TrackingConfig.TrackingAnchor_Positional_Offset; m_TrackingAnchorTilt = TrackingConfig.TrackingAnchor_Euler_Offset; m_TrackingAnchorScale = TrackingConfig.TrackingAnchor_Scale; this.m_MarkerPosePreTilt = TrackingConfig.MarkerPosePreTilt; this.m_MarkerPosePostTilt = TrackingConfig.MarkerPosePostTilt; m_TrackingAnchor = new GameObject("Tracking-Anchor").transform; m_TrackingAnchor.transform.SetParent(this.transform, false); OnTrackingAnchorConfigDirty(); TagTrackingUtil.InitializeDeviceModule(isFirst: true); Debug.LogFormat("Tag tracking module init done, tracking anchor: {0}-{1}-{2} ", m_TrackingAnchorPosition, m_TrackingAnchorTilt, m_TrackingAnchorScale); }
protected void UpdateMarkerPose() { //raw pos : the tracked position retrieved from VPU Vector3 rawPos = Vector3.zero; //raw rot : the tracked oritentation retrieved from VPU Quaternion rawRot = Quaternion.identity; var markerTrans = this.transform; m_SubMarkerMask = 0; if (TagTrackingUtil.GetMarkerState(this.MarkerID, out rawPos, out rawRot, out this.m_SubMarkerMask)) { //Anti-jiggering: markers become invisible when TOooo far: if (this.HasLimitedTrackingDistance && rawPos.magnitude >= MaxTrackingDistance) { prevRawPosition = rawPos; prevRawOritentation = rawRot; prevWorldPosition = transform.position; //too far this.Identity.IsVisible = false; return; } Vector3 rawTrackedPosition = rawPos; this.Identity.IsVisible = true; var trackingAnchor = TagTrackingUtil.GetNode(UnityEngine.XR.XRNode.TrackingReference); //smooth XYZ movement: if (this.SmoothMarkerPosition) { switch (this.smoothPositionMethod) { case SmoothPositionMethod.Lerp: rawTrackedPosition = Vector3.Lerp(prevRawPosition, rawPos, this.PositionSmoothLerp); break; case SmoothPositionMethod.Velocity: rawTrackedPosition = Vector3.MoveTowards(prevRawPosition, rawPos, this.PositionSmoothVelocity * Time.deltaTime); break; case SmoothPositionMethod.LerpByDistance: float distance = Vector3.Distance(rawPos, prevRawPosition); float lerpByDistance = this.SmoothPositionCurveControl_LerpByDistance.Evaluate(distance); rawTrackedPosition = Vector3.Lerp(prevRawPosition, rawPos, lerpByDistance); break; case SmoothPositionMethod.VelocityByDistance: distance = Vector3.Distance(rawPos, prevRawPosition); float velocityByDistance = this.SmoothPositionCurveControl_LerpByDistance.Evaluate(distance); rawTrackedPosition = Vector3.MoveTowards(prevRawPosition, rawPos, velocityByDistance * Time.deltaTime); break; } } //basic tracking: Vector3 tPos = trackingAnchor.TransformPoint(rawTrackedPosition); //prediction: if (this.PositionPrediction) { tPos = this.XYZPrediction.GetPrediction(tPos); } markerTrans.position = tPos; //Handle Rotation: if (this.ApplyAlgorithmRotation) { Quaternion RawTrackedOritentation = rawRot; if (this.SmoothMarkerRotation) { switch (this.smoothRotationMethod) { case SmoothRotationMethod.Lerp: RawTrackedOritentation = Quaternion.Lerp(prevRawOritentation, rawRot, this.SmoothRotationLerpRate); break; case SmoothRotationMethod.Angular: RawTrackedOritentation = Quaternion.RotateTowards(prevRawOritentation, rawRot, this.SmoothRotationAnglarSpeed * Time.deltaTime); break; case SmoothRotationMethod.LerpByDistance: float distance = rawPos.magnitude; float lerp = this.SmoothRotationCurveControl_LerpByDistance.Evaluate(distance); RawTrackedOritentation = Quaternion.Lerp(prevRawOritentation, rawRot, lerp); break; case SmoothRotationMethod.AngularByDistance: distance = rawPos.magnitude; float angular = this.SmoothRotationCurveControl_AngularByDistance.Evaluate(distance); RawTrackedOritentation = Quaternion.RotateTowards(prevRawOritentation, rawRot, angular * Time.deltaTime); break; } } Quaternion worldQ = trackingAnchor.rotation * RawTrackedOritentation; if (this.RotationPrediciton) { this.PosePrediction.Update(worldQ); worldQ = this.PosePrediction.GetRotationPrediction(worldQ, this.RotationPredictionDelayFrame); } markerTrans.rotation = worldQ; } prevRawPosition = rawPos; prevRawOritentation = rawRot; prevWorldPosition = transform.position; prevWorldRotation = transform.rotation; } else { this.Identity.IsVisible = false; } }