11//========= Copyright 2016-2017, HTC Corporation. All rights reserved. ===========
22
3- #if UNITY_2017_1_OR_NEWER && ! UNITY_2017_2_OR_NEWER
3+ #if UNITY_2017_1_OR_NEWER
4+
45using HTC . UnityPlugin . Utility ;
56using System . Collections . Generic ;
67using UnityEngine ;
8+
9+ #if UNITY_2017_2_OR_NEWER
10+ using UnityEngine . XR ;
11+ #else
712using UnityEngine . VR ;
13+ using XRSettings = UnityEngine . VR . VRSettings ;
14+ using XRDevice = UnityEngine . VR . VRDevice ;
15+ using XRNodeState = UnityEngine . VR . VRNodeState ;
16+ using XRNode = UnityEngine . VR . VRNode ;
17+ #endif
18+
819#endif
920
1021namespace HTC . UnityPlugin . VRModuleManagement
1122{
1223 public sealed partial class UnityEngineVRModule : VRModule . ModuleBase
1324 {
14- #if UNITY_2017_1_OR_NEWER && ! UNITY_2017_2_OR_NEWER
25+ #if UNITY_2017_1_OR_NEWER
1526 private uint m_leftIndex = INVALID_DEVICE_INDEX ;
1627 private uint m_rightIndex = INVALID_DEVICE_INDEX ;
1728
1829 private Dictionary < ulong , uint > m_node2Index = new Dictionary < ulong , uint > ( ) ;
1930 private bool [ ] m_nodeStatesValid = new bool [ MAX_DEVICE_COUNT ] ;
20- private List < VRNodeState > m_nodeStateList = new List < VRNodeState > ( ) ;
31+ private List < XRNodeState > m_nodeStateList = new List < XRNodeState > ( ) ;
2132
2233 private IndexedSet < ulong > m_prevExistNodeUids = new IndexedSet < ulong > ( ) ;
2334 private IndexedSet < ulong > m_currExistNodeUids = new IndexedSet < ulong > ( ) ;
2435
36+ private TrackingSpaceType m_prevTrackingSpace ;
37+
38+ public override void OnActivated ( )
39+ {
40+ m_prevTrackingSpace = XRDevice . GetTrackingSpaceType ( ) ;
41+ UpdateTrackingSpaceType ( ) ;
42+ }
43+
44+ public override void OnDeactivated ( )
45+ {
46+ XRDevice . SetTrackingSpaceType ( m_prevTrackingSpace ) ;
47+ }
48+
2549 public override uint GetLeftControllerDeviceIndex ( ) { return m_leftIndex ; }
2650
2751 public override uint GetRightControllerDeviceIndex ( ) { return m_rightIndex ; }
2852
29- private bool IsTrackingDeviceNode ( VRNodeState nodeState )
53+ public override void UpdateTrackingSpaceType ( )
54+ {
55+ switch ( VRModule . trackingSpaceType )
56+ {
57+ case VRModuleTrackingSpaceType . Stationary :
58+ XRDevice . SetTrackingSpaceType ( TrackingSpaceType . Stationary ) ;
59+ break ;
60+ case VRModuleTrackingSpaceType . RoomScale :
61+ XRDevice . SetTrackingSpaceType ( TrackingSpaceType . RoomScale ) ;
62+ break ;
63+ }
64+ }
65+
66+ private bool IsTrackingDeviceNode ( XRNodeState nodeState )
3067 {
3168 switch ( nodeState . nodeType )
3269 {
33- case VRNode . Head :
34- case VRNode . RightHand :
35- case VRNode . LeftHand :
36- case VRNode . GameController :
37- case VRNode . HardwareTracker :
38- case VRNode . TrackingReference :
70+ case XRNode . Head :
71+ case XRNode . RightHand :
72+ case XRNode . LeftHand :
73+ case XRNode . GameController :
74+ case XRNode . HardwareTracker :
75+ case XRNode . TrackingReference :
3976 return true ;
4077 default :
4178 return false ;
4279 }
4380 }
4481
45- private bool TryGetNodeDeviceIndex ( VRNodeState nodeState , out uint deviceIndex )
82+ private bool TryGetNodeDeviceIndex ( XRNodeState nodeState , out uint deviceIndex )
4683 {
4784 // only tracking certain type of node (some nodes share same uniqueID)
4885 if ( ! IsTrackingDeviceNode ( nodeState ) ) { deviceIndex = 0 ; return false ; }
@@ -54,7 +91,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
5491
5592 var validIndexFound = false ;
5693
57- if ( nodeState . nodeType == VRNode . Head )
94+ if ( nodeState . nodeType == XRNode . Head )
5895 {
5996 if ( m_nodeStatesValid [ 0 ] )
6097 {
@@ -79,8 +116,8 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
79116
80117 switch ( nodeState . nodeType )
81118 {
82- case VRNode . RightHand : m_rightIndex = i ; break ;
83- case VRNode . LeftHand : m_leftIndex = i ; break ;
119+ case XRNode . RightHand : m_rightIndex = i ; break ;
120+ case XRNode . LeftHand : m_leftIndex = i ; break ;
84121 }
85122
86123 deviceIndex = i ;
@@ -92,7 +129,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
92129
93130 if ( ! validIndexFound )
94131 {
95- Debug . LogWarning ( "[" + Time . frameCount + "] VRNode added, but device index out of range, drop the node id:" + nodeState . uniqueID . ToString ( "X8" ) + " type:" + nodeState . nodeType + " name:" + InputTracking . GetNodeName ( nodeState . uniqueID ) + " tracked=" + nodeState . tracked ) ;
132+ Debug . LogWarning ( "[" + Time . frameCount + "] XRNode added, but device index out of range, drop the node id:" + nodeState . uniqueID . ToString ( "X8" ) + " type:" + nodeState . nodeType + " name:" + InputTracking . GetNodeName ( nodeState . uniqueID ) + " tracked=" + nodeState . tracked ) ;
96133 return false ;
97134 }
98135
@@ -102,7 +139,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
102139 return true ;
103140 }
104141
105- private void RemoveNodeDeviceIndex ( ulong uniqueID )
142+ private uint RemoveNodeDeviceIndex ( ulong uniqueID )
106143 {
107144 var deviceIndex = INVALID_DEVICE_INDEX ;
108145 if ( m_node2Index . TryGetValue ( uniqueID , out deviceIndex ) )
@@ -113,11 +150,13 @@ private void RemoveNodeDeviceIndex(ulong uniqueID)
113150 if ( deviceIndex == m_rightIndex ) { m_rightIndex = INVALID_DEVICE_INDEX ; }
114151 if ( deviceIndex == m_leftIndex ) { m_leftIndex = INVALID_DEVICE_INDEX ; }
115152 }
153+
154+ return deviceIndex ;
116155 }
117156
118157 public override void UpdateDeviceState ( IVRModuleDeviceState [ ] prevState , IVRModuleDeviceStateRW [ ] currState )
119158 {
120- if ( VRSettings . isDeviceActive && VRDevice . isPresent )
159+ if ( XRSettings . isDeviceActive && XRDevice . isPresent )
121160 {
122161 InputTracking . GetNodeStates ( m_nodeStateList ) ;
123162 }
@@ -143,24 +182,24 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
143182
144183 switch ( m_nodeStateList [ i ] . nodeType )
145184 {
146- case VRNode . Head :
185+ case XRNode . Head :
147186 currDeviceState . deviceClass = VRModuleDeviceClass . HMD ;
148187 break ;
149- case VRNode . RightHand :
188+ case XRNode . RightHand :
150189 currDeviceState . deviceClass = VRModuleDeviceClass . Controller ;
151190 rightIndex = deviceIndex ;
152191 break ;
153- case VRNode . LeftHand :
192+ case XRNode . LeftHand :
154193 currDeviceState . deviceClass = VRModuleDeviceClass . Controller ;
155194 leftIndex = deviceIndex ;
156195 break ;
157- case VRNode . GameController :
196+ case XRNode . GameController :
158197 currDeviceState . deviceClass = VRModuleDeviceClass . Controller ;
159198 break ;
160- case VRNode . HardwareTracker :
199+ case XRNode . HardwareTracker :
161200 currDeviceState . deviceClass = VRModuleDeviceClass . GenericTracker ;
162201 break ;
163- case VRNode . TrackingReference :
202+ case XRNode . TrackingReference :
164203 currDeviceState . deviceClass = VRModuleDeviceClass . TrackingReference ;
165204 break ;
166205 default :
@@ -172,9 +211,9 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
172211 {
173212 // FIXME: getting wrong name in Unity 2017.1f1
174213 //currDeviceState.serialNumber = InputTracking.GetNodeName(m_nodeStateList[i].uniqueID) ?? string.Empty;
175- currDeviceState . serialNumber = VRDevice . model + " " + m_nodeStateList [ i ] . uniqueID . ToString ( "X8" ) ;
176- currDeviceState . modelNumber = VRDevice . model + " " + m_nodeStateList [ i ] . nodeType ;
177- currDeviceState . renderModelName = VRDevice . model + " " + m_nodeStateList [ i ] . nodeType ;
214+ currDeviceState . serialNumber = XRDevice . model + " " + m_nodeStateList [ i ] . uniqueID . ToString ( "X8" ) ;
215+ currDeviceState . modelNumber = XRDevice . model + " " + m_nodeStateList [ i ] . nodeType ;
216+ currDeviceState . renderModelName = XRDevice . model + " " + m_nodeStateList [ i ] . nodeType ;
178217
179218 SetupKnownDeviceModel ( currDeviceState ) ;
180219 }
@@ -273,8 +312,11 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
273312 // remove disconnected nodes
274313 for ( int i = m_prevExistNodeUids . Count - 1 ; i >= 0 ; -- i )
275314 {
276- if ( currState [ i ] . isConnected ) { currState [ i ] . Reset ( ) ; }
277- RemoveNodeDeviceIndex ( m_prevExistNodeUids [ i ] ) ;
315+ var removedIndex = RemoveNodeDeviceIndex ( m_prevExistNodeUids [ i ] ) ;
316+ if ( VRModule . IsValidDeviceIndex ( removedIndex ) )
317+ {
318+ currState [ removedIndex ] . Reset ( ) ;
319+ }
278320 }
279321
280322 var temp = m_prevExistNodeUids ;
0 commit comments