Skip to content

Commit 22e6695

Browse files
author
lawwong
committed
Fix setting role index to invalid tracked hand InputDevice
1 parent 9a03d96 commit 22e6695

4 files changed

Lines changed: 80 additions & 27 deletions

File tree

Assets/HTC.UnityPlugin/VRModule/Modules/SteamVRv2Module.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ private static bool inputFocus
9696
}
9797
}
9898

99+
private static string shortIdx(uint i) { return i == INVALID_DEVICE_INDEX ? "X" : i.ToString(); }
100+
99101
public static void InitializePaths()
100102
{
101103
if (s_pathInitialized) { return; }
@@ -402,6 +404,11 @@ private void UpdateDeviceConnectionAndPose(bool obj)
402404
{
403405
currState.Reset();
404406
}
407+
408+
if (prevState.isConnected)
409+
{
410+
Debug.LogFormat("[SteamVRv2Module] OpenVR device disconnected: [{0}] i={1}", prevState.deviceIndex, i);
411+
}
405412
}
406413
}
407414
else
@@ -419,13 +426,16 @@ private void UpdateDeviceConnectionAndPose(bool obj)
419426

420427
if (!prevState.isConnected)
421428
{
429+
var vrDeviceClass = vrSystem.GetTrackedDeviceClass(i);
422430
currState.isConnected = true;
423-
currState.deviceClass = ToVRModuleDeviceClass(vrSystem.GetTrackedDeviceClass(i));
431+
currState.deviceClass = ToVRModuleDeviceClass(vrDeviceClass);
424432
currState.serialNumber = QueryDeviceStringProperty(vrSystem, i, ETrackedDeviceProperty.Prop_SerialNumber_String);
425433
currState.modelNumber = QueryDeviceStringProperty(vrSystem, i, ETrackedDeviceProperty.Prop_ModelNumber_String);
426434
currState.renderModelName = QueryDeviceStringProperty(vrSystem, i, ETrackedDeviceProperty.Prop_RenderModelName_String);
427435

428436
SetupKnownDeviceModel(currState);
437+
438+
Debug.LogFormat("[SteamVRv2Module] OpenVR device connected: [{0}] i={1} cl={2}", currState.deviceIndex, i, vrDeviceClass);
429439
}
430440

431441
// update device status
@@ -446,6 +456,23 @@ private void UpdateDeviceConnectionAndPose(bool obj)
446456
m_submodules.UpdateAllModulesActivity();
447457
m_submodules.UpdateModulesDeviceConnectionAndPoses();
448458

459+
for (uint i = 0u, imax = GetDeviceStateLength(); i < imax; ++i)
460+
{
461+
if (!TryGetValidDeviceState(i, out prevState, out currState)) { continue; }
462+
if (prevState.isConnected != currState.isConnected)
463+
{
464+
var isDisconnect = prevState.isConnected;
465+
var readState = isDisconnect ? prevState : (IVRModuleDeviceState)currState;
466+
Debug.LogFormat("[SteamVRv2Module] device {0}connected: [{1}] sn=\"{5}\" cl={2} md={3} mn=\"{4}\"",
467+
isDisconnect ? "dis" : "",
468+
readState.deviceIndex,
469+
readState.deviceClass,
470+
readState.deviceModel,
471+
readState.modelNumber,
472+
readState.serialNumber);
473+
}
474+
}
475+
449476
// process hand role
450477
bool roleChanged = false;
451478
if (vrSystem == null)
@@ -469,8 +496,13 @@ private void UpdateDeviceConnectionAndPose(bool obj)
469496

470497
var moduleRight = m_openvrRightIndex != INVALID_DEVICE_INDEX ? m_openvrRightIndex : m_submodules.GetFirstRightHandedIndex();
471498
var moduleLeft = m_openvrLeftIndex != INVALID_DEVICE_INDEX ? m_openvrLeftIndex : m_submodules.GetFirstLeftHandedIndex();
472-
roleChanged |= ChangeProp.Set(ref m_moduleRightIndex, moduleRight);
473-
roleChanged |= ChangeProp.Set(ref m_moduleLeftIndex, moduleLeft);
499+
if ((m_moduleRightIndex != moduleRight) | (m_moduleLeftIndex != moduleLeft))
500+
{
501+
Debug.Log("[SteamVRv2Module] role changed: [" + shortIdx(m_moduleLeftIndex) + "=>" + shortIdx(moduleLeft) + "]L [" + shortIdx(m_moduleRightIndex) + "=>" + shortIdx(moduleRight) + "]R");
502+
m_moduleRightIndex = moduleRight;
503+
m_moduleLeftIndex = moduleLeft;
504+
roleChanged = true;
505+
}
474506

475507
UpdateHandJoints(m_openvrLeftIndex, skeletonActionHandleLeft, true);
476508
UpdateHandJoints(m_openvrRightIndex, skeletonActionHandleRight, false);

Assets/HTC.UnityPlugin/VRModule/Modules/UnityXRModuleBase.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ private static int CompareRoleIdx(RoleIdx a, RoleIdx b)
128128
return a.ds.deviceIndex.CompareTo(b.ds.deviceIndex);
129129
}
130130

131+
private static string shortIdx(uint i) { return i == INVALID_DEVICE_INDEX ? "X" : i.ToString(); }
132+
131133
private VRModule.SubmoduleBase.Collection submodules = new VRModule.SubmoduleBase.Collection(
132134
new ViveHandTrackingSubmodule(),
133135
new WaveHandTrackingSubmodule(),
@@ -202,6 +204,7 @@ public sealed override void BeforeRenderUpdate()
202204

203205
foreach (var device in connectedDevices)
204206
{
207+
if (!device.isValid) { continue; }
205208
if (!indexMap.TryGetIndex(device, out deviceIndex))
206209
{
207210
string deviceName;
@@ -220,20 +223,19 @@ public sealed override void BeforeRenderUpdate()
220223
deviceName = device.name;
221224
}
222225

226+
// will not identify tracked hand here, currently all tracked hand should created by submodule
223227
currState.deviceClass = GetDeviceClass(device.name, device.characteristics);
224228
currState.serialNumber = deviceName + " " + device.serialNumber + " " + (int)device.characteristics;
225229
currState.modelNumber = deviceName + " (" + device.characteristics + ")";
226230
currState.renderModelName = deviceName + " (" + device.characteristics + ")";
227231

228232
SetupKnownDeviceModel(currState);
229233

230-
Debug.LogFormat("[UnityXRModule] Device connected: {0} / {1} / {2} / {3} / {4} / {5} ({6})",
234+
Debug.LogFormat("[UnityXRModule] found new InputDevice: [{0}] nm=\"{2}\" sn=\"{1}\" ch=({4}) mf=\"{3}\"",
231235
currState.deviceIndex,
232-
currState.deviceClass,
233-
currState.deviceModel,
234-
currState.modelNumber,
235-
currState.serialNumber,
236+
device.serialNumber,
236237
device.name,
238+
device.manufacturer,
237239
device.characteristics);
238240

239241
UpdateNewConnectedInputDevice(currState, device);
@@ -243,11 +245,9 @@ public sealed override void BeforeRenderUpdate()
243245
EnsureValidDeviceState(deviceIndex, out prevState, out currState);
244246
}
245247

246-
currState.isConnected = true;
247248
// update device Poses
249+
currState.isConnected = true;
248250
currState.isPoseValid = GetDeviceFeatureValueOrDefault(device, CommonUsages.isTracked);
249-
if ((device.characteristics & InputDeviceCharacteristics.Right) != 0u) { rightIndice.Add(new RoleIdx(currState)); }
250-
else if ((device.characteristics & InputDeviceCharacteristics.Left) != 0u) { leftIndice.Add(new RoleIdx(currState)); }
251251

252252
if (VIUSettings.preferUnityXRPointerPose)
253253
{
@@ -267,11 +267,17 @@ public sealed override void BeforeRenderUpdate()
267267
currDeviceConnected[deviceIndex] = true;
268268
if (currMaxConnectedIndex < (int)deviceIndex) { currMaxConnectedIndex = (int)deviceIndex; }
269269

270+
if (currState.deviceClass != VRModuleDeviceClass.Invalid)
271+
{
272+
if ((device.characteristics & InputDeviceCharacteristics.Right) != 0u) { rightIndice.Add(new RoleIdx(currState)); }
273+
else if ((device.characteristics & InputDeviceCharacteristics.Left) != 0u) { leftIndice.Add(new RoleIdx(currState)); }
274+
}
275+
270276
// TODO: update hand skeleton pose
271277
}
272278

273279
// unmap index for disconnected device state
274-
for (uint i = 0u, imax = (uint)(prevMaxConnectedIndex + 1); i < imax; ++i)
280+
for (uint i = 0u, imax = (uint)prevMaxConnectedIndex; i <= imax; ++i)
275281
{
276282
if (prevDeviceConnected[i] && !currDeviceConnected[i])
277283
{
@@ -281,7 +287,7 @@ public sealed override void BeforeRenderUpdate()
281287
}
282288
else
283289
{
284-
Debug.LogWarning("[UnityXRModule] Device disconnected: [" + i + "] already unmapped");
290+
Debug.LogWarning("[UnityXRModule] unmap failed: [" + i + "] already unmapped");
285291
}
286292

287293
if (TryGetValidDeviceState(i, out prevState, out currState) && currState.isConnected)
@@ -290,19 +296,30 @@ public sealed override void BeforeRenderUpdate()
290296
}
291297
else
292298
{
293-
Debug.LogWarning("[UnityXRModule] Device disconnected: [" + i + "] already been reset");
299+
Debug.LogWarning("[UnityXRModule] reset state failed: [" + i + "] already been reset");
294300
}
295-
296-
Debug.LogFormat("[UnityXRModule] Device disconnected: {0} / {1} / {2} / {3} / {4}",
297-
prevState.deviceIndex,
298-
prevState.deviceClass,
299-
prevState.deviceModel,
300-
prevState.modelNumber,
301-
prevState.serialNumber);
302301
}
303302
}
304303

305304
submodules.UpdateModulesDeviceConnectionAndPoses();
305+
306+
for (uint i = 0u, imax = GetDeviceStateLength(); i < imax; ++i)
307+
{
308+
if (!TryGetValidDeviceState(i, out prevState, out currState)) { continue; }
309+
if (prevState.isConnected != currState.isConnected)
310+
{
311+
var isDisconnect = prevState.isConnected;
312+
var readState = isDisconnect ? prevState : (IVRModuleDeviceState)currState;
313+
Debug.LogFormat("[UnityXRModule] device {0}connected: [{1}] sn=\"{5}\" cl={2} md={3} mn=\"{4}\"",
314+
isDisconnect ? "dis" : "",
315+
readState.deviceIndex,
316+
readState.deviceClass,
317+
readState.deviceModel,
318+
readState.modelNumber,
319+
readState.serialNumber);
320+
}
321+
}
322+
306323
for (int i = 0, imax = submodules.ModuleCount; i < imax; ++i)
307324
{
308325
if (!submodules[i].isActivated) { continue; }
@@ -329,8 +346,11 @@ public sealed override void BeforeRenderUpdate()
329346
if (leftIndice.Count != 1) { leftIndice.Sort(CompareRoleIdx); }
330347
newLeftIndex = leftIndice[0].index;
331348
}
332-
if (ChangeProp.Set(ref moduleRightIndex, newRightIndex) | ChangeProp.Set(ref moduleLeftIndex, newLeftIndex))
349+
if ((moduleRightIndex != newRightIndex) | (moduleLeftIndex != newLeftIndex))
333350
{
351+
Debug.Log("[UnityXRModule] role changed: [" + shortIdx(moduleLeftIndex) + "=>" + shortIdx(newLeftIndex) + "]L [" + shortIdx(moduleRightIndex) + "=>" + shortIdx(newRightIndex) + "]R");
352+
moduleRightIndex = newRightIndex;
353+
moduleLeftIndex = newLeftIndex;
334354
InvokeControllerRoleChangedEvent();
335355
}
336356

@@ -395,10 +415,7 @@ protected static VRModuleDeviceClass GetDeviceClass(string name, InputDeviceChar
395415
return VRModuleDeviceClass.GenericTracker;
396416
}
397417

398-
if ((characteristics & InputDeviceCharacteristics.HandTracking) != 0)
399-
{
400-
return VRModuleDeviceClass.TrackedHand;
401-
}
418+
// will not identify tracked hand here, currently all tracked hand should created by submodule
402419

403420
return VRModuleDeviceClass.Invalid;
404421
}

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/Teleportable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ public IEnumerator StartTeleport(RaycastResult hitResult, Vector3 position, Quat
281281
if (!m_steamVRFadeInitialized)
282282
{
283283
// add SteamVR_Fade to the last rendered stereo camera
284+
#if UNITY_6000_0_OR_NEWER
285+
var fadeScripts = FindObjectsByType<SteamVR_Fade>(FindObjectsSortMode.None);
286+
#else
284287
var fadeScripts = FindObjectsOfType<SteamVR_Fade>();
288+
#endif
285289
if (fadeScripts == null || fadeScripts.Length <= 0)
286290
{
287291
var topCam = SteamVR_Render.Top();

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/RoleMaps/HandRole.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private void MappingLeftRightHands()
174174
{
175175
if (i == rightIndex || i == leftIndex) { continue; }
176176
var state = VRModule.GetCurrentDeviceState(i);
177-
if (!state.isConnected || !state.isPoseValid || state.deviceClass != VRModuleDeviceClass.Controller) { continue; }
177+
if (!state.isConnected || state.deviceClass != VRModuleDeviceClass.Controller) { continue; }
178178
if (RoleMap.IsDeviceBound(state.serialNumber)) { continue; }
179179
m_sortedDeviceList.Add(i);
180180
}

0 commit comments

Comments
 (0)