Tanoda
DefaultXRNodeTrackingProvider.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using UnityEngine;
11
12#if UNITY_2017_2_OR_NEWER
13using UnityEngine.XR;
14#else
15using UnityEngine.VR;
16#endif
17
18namespace Leap.Unity.Interaction {
19
25 public class DefaultXRNodeTrackingProvider : MonoBehaviour,
27
28 private bool _isTrackingController = false;
29 public bool isTracked { get { return _isTrackingController; } }
30
31 private bool _isXRNodeSet = false;
32 #if UNITY_2017_2_OR_NEWER
33 private XRNode _backingXRNode;
34 public XRNode xrNode {
35 get { return _backingXRNode; }
36 set { _backingXRNode = value; _isXRNodeSet = true; }
37 }
38 #else
39 private VRNode _backingXRNode;
40 public VRNode xrNode {
41 get { return _backingXRNode; }
42 set { _backingXRNode = value; _isXRNodeSet = true; }
43 }
44 #endif
45
46 public event Action<Vector3, Quaternion> OnTrackingDataUpdate = (position, rotation) => { };
47
48 void FixedUpdate() {
49 updateTrackingData();
50 }
51
52 void updateTrackingData() {
53 if (_isXRNodeSet) {
54
55 var position = XRSupportUtil.GetXRNodeLocalPosition((int)xrNode);
56 var rotation = XRSupportUtil.GetXRNodeLocalRotation((int)xrNode);
57
58 // Unfortunately, the only alternative to checking the controller's position and
59 // rotation for whether or not it is tracked is to request an allocated string
60 // array of all currently-connected joysticks, which would allocate garbage
61 // every frame, so it's unusable.
62 _isTrackingController = position != Vector3.zero && rotation != Quaternion.identity;
63
64 Transform rigTransform = Camera.main.transform.parent;
65 if (rigTransform != null) {
66 position = rigTransform.TransformPoint(position);
67 rotation = rigTransform.TransformRotation(rotation);
68 }
69
70 OnTrackingDataUpdate(position, rotation);
71 }
72 }
73
74 }
75
76}
Implements IVRControllerTrackingProvider using Unity.XR.InputTracking for XRNodes....
bool isTracked
Gets whether or not this provider is currently tracking the controller for which it provides data.
The interface for providing tracking data to an InteractionVRController.