Skip to content

Commit

Permalink
Add new vibration action
Browse files Browse the repository at this point in the history
Seems that CVRSystem.TriggerHapticPulse no longer working correctly with new SteamVR runtime.
So we add new vibration action (need SteamVR v2) and support procedure vibration.

New API Example:
ViveInput.TriggerHapticVibrationEx<TRole>(
  TRole role,
  float durationSeconds = 0.01f,
  float frequency = 85f,
  float amplitude = 0.125f,
  float startSecondsFromNow = 0f)
  • Loading branch information
lawwong committed Jan 17, 2019
1 parent 8e1f883 commit d622337
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 15 deletions.
27 changes: 24 additions & 3 deletions Assets/HTC.UnityPlugin/VRModule/Modules/SteamVRv2Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public void InitiateHandles(CVRInput vrInput)
}
}

public enum HapticStruct { Haptic }

public const string ACTION_SET_NAME = "/actions/htc_viu";

private static SteamVRModule s_moduleInstance;
Expand All @@ -167,6 +169,7 @@ public void InitiateHandles(CVRInput vrInput)
public static ActionArray<VRModuleRawButton> touchActions { get; private set; }
public static ActionArray<VRModuleRawAxis> v1Actions { get; private set; }
public static ActionArray<VRModuleRawAxis> v2Actions { get; private set; }
public static ActionArray<HapticStruct> vibrateActions { get; private set; }

private static ulong[] s_devicePathHandles;
private static ulong s_actionSetHandle;
Expand Down Expand Up @@ -269,6 +272,9 @@ public static void InitializePaths()
v2Actions.Set(VRModuleRawAxis.Axis2X, "2xy", "Axis2 X&Y (Thumbstick)");
v2Actions.Set(VRModuleRawAxis.Axis3X, "3xy", "Axis3 X&Y");
v2Actions.Set(VRModuleRawAxis.Axis4X, "4xy", "Axis4 X&Y");

vibrateActions = new ActionArray<HapticStruct>("/out/viu_vib_", "vibration");
vibrateActions.Set(HapticStruct.Haptic, "01", "Vibration");
}

public static void InitializeHandles()
Expand Down Expand Up @@ -296,6 +302,7 @@ public static void InitializeHandles()
touchActions.InitiateHandles(vrInput);
v1Actions.InitiateHandles(vrInput);
v2Actions.InitiateHandles(vrInput);
vibrateActions.InitiateHandles(vrInput);

s_actionSetHandle = SafeGetActionSetHandle(vrInput, ACTION_SET_NAME);
}
Expand Down Expand Up @@ -655,10 +662,24 @@ private string QueryDeviceStringProperty(CVRSystem system, uint deviceIndex, ETr

public override void TriggerViveControllerHaptic(uint deviceIndex, ushort durationMicroSec = 500)
{
var system = OpenVR.System;
if (system != null)
TriggerHapticVibration(deviceIndex, 0.01f, 85f, Mathf.InverseLerp(0, 4000, durationMicroSec), 0f);
}

public override void TriggerHapticVibration(uint deviceIndex, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
{
var handle = GetInputSrouceHandleForDevice(deviceIndex);
if (handle == OpenVR.k_ulInvalidDriverHandle) { return; }

var vrInput = OpenVR.Input;
if (vrInput != null)
{
system.TriggerHapticPulse(deviceIndex, (uint)EVRButtonId.k_EButton_SteamVR_Touchpad - (uint)EVRButtonId.k_EButton_Axis0, (char)durationMicroSec);
vibrateActions.Reset();

var error = vrInput.TriggerHapticVibrationAction(vibrateActions.CurrentHandle, startSecondsFromNow, durationSeconds, frequency, amplitude, handle);
if (error != EVRInputError.None)
{
Debug.LogError("TriggerViveControllerHaptic failed! error=" + error);
}
}
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions Assets/HTC.UnityPlugin/VRModule/VRModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ public static void TriggerViveControllerHaptic(uint deviceIndex, ushort duration
}
}

public static void TriggerHapticVibration(uint deviceIndex, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
{
if (Instance != null && Instance.m_activatedModuleBase != null && IsValidDeviceIndex(deviceIndex))
{
Instance.m_activatedModuleBase.TriggerHapticVibration(deviceIndex, durationSeconds, frequency, amplitude, startSecondsFromNow);
}
}

public static readonly bool isSteamVRPluginDetected =
#if VIU_STEAMVR
true;
Expand Down
2 changes: 2 additions & 0 deletions Assets/HTC.UnityPlugin/VRModule/VRModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public virtual void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModul

public virtual void TriggerViveControllerHaptic(uint deviceIndex, ushort durationMicroSec = 500) { }

public virtual void TriggerHapticVibration(uint deviceIndex, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f) { }

protected void InvokeInputFocusEvent(bool value)
{
VRModule.InvokeInputFocusEvent(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,17 @@ private static void OnVIUPreferenceGUI()
// });
// actionFile.localization[0].Add(SteamVRModule.v2Actions.CurrentPath, SteamVRModule.v2Actions.CurrentAlias);
// }
// for (SteamVRModule.vibrationActions.Reset(); SteamVRModule.vibrationActions.IsCurrentValid(); SteamVRModule.vibrationActions.MoveNext())
// {
// if (string.IsNullOrEmpty(SteamVRModule.vibrationActions.CurrentPath)) { continue; }
// actionFile.actions.Add(new SteamVRExtension.VIUSteamVRActionFile.Action()
// {
// name = SteamVRModule.vibrationActions.CurrentPath,
// type = SteamVRModule.vibrationActions.DataType,
// requirement = "optional",
// });
// actionFile.localization[0].Add(SteamVRModule.vibrationActions.CurrentPath, SteamVRModule.vibrationActions.CurrentAlias);
// }

// actionFile.Save();
//}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
"name": "/actions/htc_viu/in/viu_axis_4xy",
"type": "vector2",
"requirement": "optional"
},
{
"name": "/actions/htc_viu/out/viu_vib_01",
"type": "vibration",
"requirement": "optional"
}
],
"action_sets": [
Expand All @@ -204,20 +209,28 @@
],
"default_bindings": [
{
"controller_type": "holographic_hmd",
"binding_url": "bindings_holographic_hmd.json"
"controller_type": "vive_controller",
"binding_url": "bindings_vive_controller.json"
},
{
"controller_type": "oculus_touch",
"binding_url": "bindings_oculus_touch.json"
},
{
"controller_type": "knuckles",
"binding_url": "bindings_knuckles.json"
},
{
"controller_type": "holographic_controller",
"binding_url": "bindings_holographic_controller.json"
},
{
"controller_type": "rift",
"binding_url": "bindings_rift.json"
"controller_type": "holographic_hmd",
"binding_url": "bindings_holographic_hmd.json"
},
{
"controller_type": "oculus_touch",
"binding_url": "bindings_oculus_touch.json"
"controller_type": "rift",
"binding_url": "bindings_rift.json"
},
{
"controller_type": "knuckles_ev1",
Expand All @@ -231,10 +244,6 @@
"controller_type": "vive_pro",
"binding_url": "bindings_vive_pro.json"
},
{
"controller_type": "vive_controller",
"binding_url": "bindings_vive_controller.json"
},
{
"controller_type": "vive_tracker",
"binding_url": "bindings_vive_tracker.json"
Expand Down Expand Up @@ -321,7 +330,8 @@
"/actions/htc_viu/in/viu_axis_1xy": "Axis1 X&Y",
"/actions/htc_viu/in/viu_axis_2xy": "Axis2 X&Y (Thumbstick)",
"/actions/htc_viu/in/viu_axis_3xy": "Axis3 X&Y",
"/actions/htc_viu/in/viu_axis_4xy": "Axis4 X&Y"
"/actions/htc_viu/in/viu_axis_4xy": "Axis4 X&Y",
"/actions/htc_viu/out/viu_vib_01": "Vibration"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
},
"path": "/user/hand/right/input/grip"
}
],
"haptics": [
{
"output": "/actions/htc_viu/out/viu_vib_01",
"path": "/user/hand/left/output/haptic"
},
{
"output": "/actions/htc_viu/out/viu_vib_01",
"path": "/user/hand/right/output/haptic"
}
]
}
}
Expand Down
Loading

0 comments on commit d622337

Please sign in to comment.