Skip to content

Instantly share code, notes, and snippets.

@rygo6
Created December 14, 2021 23:10
Show Gist options
  • Save rygo6/84ed375e8104eab6fb3a683c8f91312d to your computer and use it in GitHub Desktop.
Save rygo6/84ed375e8104eab6fb3a683c8f91312d to your computer and use it in GitHub Desktop.
ViveTrackerControllerProfile for com.unity.xr.openxr Unity OpenXR Plugin
// this doesnt work
using System.Collections.Generic;
using Unity.XR.OpenVR;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.XR;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.XR.OpenXR.Features.Interactions
{
#if UNITY_EDITOR
[UnityEditor.XR.OpenXR.Features.OpenXRFeature(UiName = "Vive Tracker Profile",
BuildTargetGroups = new[] { BuildTargetGroup.Standalone},
Company = "HTC",
Desc = "Allows for mapping input to HTC Vive Tracker.",
DocumentationLink = "https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#XR_HTCX_vive_tracker_interaction",
OpenxrExtensionStrings = extensionString,
Version = "0.0.1",
Category = UnityEditor.XR.OpenXR.Features.FeatureCategory.Interaction,
FeatureId = featureId)]
#endif
public class ViveTrackerProfile : OpenXRInteractionFeature
{
public const string featureId = "com.unity.openxr.feature.input.vivetrackerprofile";
public const string profile = "/interaction_profiles/htc/vive_tracker_htcx";
public const string grip = "/input/grip/pose";
private const string kDeviceLocalizedName = "VIVE Tracker";
public const string extensionString = "XR_HTCX_vive_tracker_interaction";
protected override bool OnInstanceCreate(ulong instance)
{
if (!OpenXRRuntime.IsExtensionEnabled(extensionString))
{
Debug.Log($"{extensionString} not supported!");
return false;
}
foreach (string extension in OpenXRRuntime.GetEnabledExtensions())
{
Debug.Log(extension);
}
return base.OnInstanceCreate(instance);
}
protected override void RegisterDeviceLayout()
{
InputSystem.InputSystem.RegisterLayout(typeof(HandedViveTracker),
matches: new InputDeviceMatcher()
.WithInterface(XRUtilities.InterfaceMatchAnyVersion)
.WithProduct(kDeviceLocalizedName));
}
protected override void UnregisterDeviceLayout()
{
InputSystem.InputSystem.RemoveLayout(typeof(HandedViveTracker).Name);
}
protected override void RegisterActionMapsWithRuntime()
{
ActionMapConfig actionMap = new ActionMapConfig()
{
name = "vivetracker",
localizedName = kDeviceLocalizedName,
desiredInteractionProfile = profile,
manufacturer = "HTC",
serialNumber = "",
deviceInfos = new List<DeviceConfig>()
{
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HandTracking | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left),
userPath = "/user/vive_tracker_htcx/role/handheld_object"
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HandTracking | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right),
userPath = "/user/vive_tracker_htcx/role/handheld_object"
}
},
actions = new List<ActionConfig>()
{
new ActionConfig()
{
name = "devicePose",
localizedName = "Device Pose",
type = ActionType.Pose,
usages = new List<string>()
{
"Device"
},
bindings = new List<ActionBinding>()
{
new ActionBinding()
{
interactionPath = grip,
interactionProfileName = profile,
}
}
},
}
};
AddActionMap(actionMap);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment