Skip to content

Commit ae26ed8

Browse files
author
lawwong
committed
Add generic xrcontroller (bind to HandRole.RightHand/LeftHand)
1 parent 7043db0 commit ae26ed8

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSyntheticDevice.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,5 +542,161 @@ protected override void FinishSetup()
542542
_axises[(int)ControllerAxis.JoystickY] = GetChildControl<AxisControl>("JoystickAxis/y");
543543
}
544544
}
545+
546+
public struct VIUSyntheticXRHMDState : IInputStateTypeInfo
547+
{
548+
public FourCC format => new FourCC('V', 'I', 'U', 'H');
549+
550+
public TrackingState trackingState;
551+
public bool isTracked;
552+
public Vector3 devicePosition;
553+
public Quaternion deviceRotation;
554+
555+
public Vector3 leftEyePosition;
556+
public Quaternion leftEyeRotation;
557+
public Vector3 rightEyePosition;
558+
public Quaternion rightEyeRotation;
559+
public Vector3 centerEyePosition;
560+
public Quaternion centerEyeRotation;
561+
}
562+
563+
//[InputControlLayout(displayName = "VIU Synthetic XR HMD", stateType = typeof(VIUSyntheticXRHMDState), hideInUI = true)]
564+
//public class VIUSyntheticXRHMD : XRHMD, IInputUpdateCallbackReceiver
565+
//{
566+
// //private
567+
// private ViveRole.IMap<DeviceRole> roleMap;
568+
569+
// void IInputUpdateCallbackReceiver.OnUpdate()
570+
// {
571+
// if (roleMap == null) { return; }
572+
// var deviceIndex = roleMap.GetMappedDeviceByRoleValue((int)DeviceRole.Hmd);
573+
// var deviceState = VRModule.GetCurrentDeviceState(deviceIndex);
574+
// var UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.Head);
575+
// InputSystem.QueueStateEvent(this, new VIUSyntheticXRHMDState()
576+
// {
577+
// isTracked = deviceState.isPoseValid,
578+
// trackingState = TrackingState.Position | TrackingState.Rotation | TrackingState.Velocity | TrackingState.AngularVelocity,
579+
// position = deviceState.position,
580+
// rotation = deviceState.rotation,
581+
// velocity = deviceState.velocity,
582+
// angularVelocity = deviceState.angularVelocity,
583+
// });
584+
// }
585+
586+
// protected override void FinishSetup()
587+
// {
588+
// base.FinishSetup();
589+
590+
// roleMap = ViveRole.GetMap<DeviceRole>();
591+
// }
592+
//}
593+
594+
595+
public struct VIUSyntheticXRControllerState : IInputStateTypeInfo
596+
{
597+
public FourCC format => new FourCC('V', 'I', 'U', 'C');
598+
599+
public TrackingState trackingState;
600+
public bool isTracked;
601+
public Vector3 devicePosition;
602+
public Quaternion deviceRotation;
603+
}
604+
605+
#if UNITY_EDITOR
606+
[UnityEditor.InitializeOnLoad]
607+
#endif
608+
[InputControlLayout(displayName = "VIU Synthetic XR Controller", stateType = typeof(VIUSyntheticXRControllerState), hideInUI = true)]
609+
public class VIUSyntheticXRController : XRController, IInputUpdateCallbackReceiver
610+
{
611+
private ViveInput.ICtrlState<HandRole> ctrlState;
612+
613+
private static VIUSyntheticXRController rightDevice;
614+
private static VIUSyntheticXRController leftDevice;
615+
616+
static VIUSyntheticXRController()
617+
{
618+
InputSystem.RegisterLayout<VIUSyntheticXRController>();
619+
620+
#if UNITY_EDITOR
621+
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
622+
#endif
623+
{
624+
var roleMap = ViveRole.GetMap<HandRole>();
625+
626+
HandleDevice(ref rightDevice, HandRole.RightHand, CommonUsages.RightHand, VRModule.IsValidDeviceIndex(roleMap.GetMappedDeviceByRole(HandRole.RightHand)));
627+
HandleDevice(ref leftDevice, HandRole.LeftHand, CommonUsages.LeftHand, VRModule.IsValidDeviceIndex(roleMap.GetMappedDeviceByRole(HandRole.LeftHand)));
628+
629+
roleMap.onRoleMappingChanged += (map, arg) =>
630+
{
631+
if (arg.role == HandRole.RightHand)
632+
{
633+
HandleDevice(ref rightDevice, HandRole.RightHand, CommonUsages.RightHand, VRModule.IsValidDeviceIndex(arg.currentDeviceIndex));
634+
}
635+
else if (arg.role == HandRole.LeftHand)
636+
{
637+
HandleDevice(ref leftDevice, HandRole.LeftHand, CommonUsages.LeftHand, VRModule.IsValidDeviceIndex(arg.currentDeviceIndex));
638+
}
639+
};
640+
}
641+
}
642+
643+
private static void HandleDevice(ref VIUSyntheticXRController device, HandRole hand, InternedString handStr, bool connected)
644+
{
645+
try
646+
{
647+
if (connected)
648+
{
649+
if (device == null)
650+
{
651+
device = InputSystem.AddDevice<VIUSyntheticXRController>("VIUSyntheticXRController" + hand.ToString());
652+
//device = InputSystem.AddDevice(new InputDeviceDescription()
653+
//{
654+
// interfaceName = typeof(VIUSyntheticXRController).Name,
655+
// manufacturer = "HTC ViveSoftware",
656+
//}) as VIUSyntheticXRController;
657+
658+
InputSystem.AddDeviceUsage(rightDevice, handStr);
659+
device.ctrlState = ViveInput.GetState(hand);
660+
}
661+
else
662+
{
663+
InputSystem.AddDevice(device);
664+
}
665+
}
666+
else
667+
{
668+
if (device != null)
669+
{
670+
InputSystem.RemoveDevice(device);
671+
}
672+
}
673+
}
674+
catch (Exception e)
675+
{
676+
Debug.LogException(e);
677+
}
678+
}
679+
680+
void IInputUpdateCallbackReceiver.OnUpdate()
681+
{
682+
if (ctrlState == null) { return; }
683+
684+
var deviceIndex = ctrlState.RoleMap.GetMappedDeviceByRoleValue(ctrlState.RoleValue);
685+
var deviceState = VRModule.GetCurrentDeviceState(deviceIndex);
686+
InputSystem.QueueStateEvent(this, new VIUSyntheticXRControllerState()
687+
{
688+
isTracked = deviceState.isPoseValid,
689+
trackingState = TrackingState.Position | TrackingState.Rotation,
690+
devicePosition = deviceState.position,
691+
deviceRotation = deviceState.rotation,
692+
});
693+
694+
//Debug.Log("IInputUpdateCallbackReceiver.OnUpdate " + name + " "
695+
// + isTracked.ReadValue() + " "
696+
// + trackingState.ReadValue() + " "
697+
// + devicePosition.ReadValue().ToString("0.00") + " "
698+
// + deviceRotation.ReadValue().eulerAngles.ToString("0"));
699+
}
700+
}
545701
}
546702
#endif

0 commit comments

Comments
 (0)