Skip to content

Commit

Permalink
feat(SDK): allow additional tracked controller usage
Browse files Browse the repository at this point in the history
The HTC Trackers are now supported in a basic way by allowing for
additional tracked controllers to be supported by the SteamVR SDK
Bridge. The tracker support still requires manual configuration of
the tracker objects and only works with SteamVR. But until more
tracking solutions come out, this will be ok as the HTC trackers
can only be used with SteamVR at the moment anyway.

To set a tracker up:

 * Follow HTC Tracker instructions to set object up with SteamVR
 * Add `VRTK_TrackedController` to tracker object
 * Add `VRTK_ControllerTracker` to tracker object
 * The tracker object is now usable by VRTK
 * Attach any controller scripts directly to the tracker object
  • Loading branch information
thestonefox committed Mar 30, 2017
1 parent 38c1c2f commit ea345ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
31 changes: 27 additions & 4 deletions Assets/VRTK/SDK/SteamVR/SDK_SteamVRController.cs
Expand Up @@ -21,6 +21,8 @@ public class SDK_SteamVRController
#if VRTK_DEFINE_SDK_STEAMVR
private SteamVR_TrackedObject cachedLeftTrackedObject;
private SteamVR_TrackedObject cachedRightTrackedObject;
private Dictionary<GameObject, SteamVR_TrackedObject> cachedTrackedObjectsByGameObject = new Dictionary<GameObject, SteamVR_TrackedObject>();
private Dictionary<uint, SteamVR_TrackedObject> cachedTrackedObjectsByIndex = new Dictionary<uint, SteamVR_TrackedObject>();
private ushort maxHapticVibration = 3999;

/// <summary>
Expand Down Expand Up @@ -130,6 +132,12 @@ public override GameObject GetControllerByIndex(uint index, bool actual = false)
return (actual ? sdkManager.actualRightController : sdkManager.scriptAliasRightController);
}
}

if (cachedTrackedObjectsByIndex.ContainsKey(index) && cachedTrackedObjectsByIndex[index] != null)
{
return cachedTrackedObjectsByIndex[index].gameObject;
}

return null;
}

Expand Down Expand Up @@ -860,6 +868,8 @@ private void SetTrackedControllerCaches(bool forceRefresh = false)
{
cachedLeftTrackedObject = null;
cachedRightTrackedObject = null;
cachedTrackedObjectsByGameObject.Clear();
cachedTrackedObjectsByIndex.Clear();
}

var sdkManager = VRTK_SDKManager.instance;
Expand All @@ -879,17 +889,30 @@ private void SetTrackedControllerCaches(bool forceRefresh = false)
private SteamVR_TrackedObject GetTrackedObject(GameObject controller)
{
SetTrackedControllerCaches();
SteamVR_TrackedObject trackedObject = null;

if (IsControllerLeftHand(controller))
{
trackedObject = cachedLeftTrackedObject;
return cachedLeftTrackedObject;
}
else if (IsControllerRightHand(controller))
{
trackedObject = cachedRightTrackedObject;
return cachedRightTrackedObject;
}

if (cachedTrackedObjectsByGameObject.ContainsKey(controller) && cachedTrackedObjectsByGameObject[controller] != null)
{
return cachedTrackedObjectsByGameObject[controller];
}
else
{
var trackedObject = controller.GetComponent<SteamVR_TrackedObject>();
if (trackedObject != null)
{
cachedTrackedObjectsByGameObject.Add(controller, trackedObject);
cachedTrackedObjectsByIndex.Add((uint)trackedObject.index, trackedObject);
}
return trackedObject;
}
return trackedObject;
}

private bool IsButtonPressed(uint index, ButtonPressTypes type, ulong button)
Expand Down
8 changes: 2 additions & 6 deletions Assets/VRTK/Scripts/Internal/VRTK_ControllerTracker.cs
Expand Up @@ -8,12 +8,8 @@ public class VRTK_ControllerTracker : MonoBehaviour

protected virtual void OnEnable()
{
var actualController = VRTK_DeviceFinder.GetActualController(gameObject);
if (actualController)
{
trackedController = actualController.GetComponent<VRTK_TrackedController>();
}

GameObject actualController = VRTK_DeviceFinder.GetActualController(gameObject);
trackedController = (actualController != null ? actualController.GetComponent<VRTK_TrackedController>() : GetComponent<VRTK_TrackedController>());
Update();
}

Expand Down
6 changes: 5 additions & 1 deletion Assets/VRTK/Scripts/Internal/VRTK_TrackedController.cs
Expand Up @@ -57,6 +57,10 @@ protected virtual VRTKTrackedControllerEventArgs SetEventPayload(uint previousIn
protected virtual void OnEnable()
{
aliasController = VRTK_DeviceFinder.GetScriptAliasController(gameObject);
if (aliasController == null)
{
aliasController = gameObject;
}

if (enableControllerCoroutine != null)
{
Expand Down Expand Up @@ -97,7 +101,7 @@ protected virtual void Update()

VRTK_SDK_Bridge.ControllerProcessUpdate(index);

if (aliasController && gameObject.activeInHierarchy && !aliasController.activeSelf)
if (aliasController != null && gameObject.activeInHierarchy && !aliasController.activeSelf)
{
aliasController.SetActive(true);
}
Expand Down

0 comments on commit ea345ef

Please sign in to comment.