ViveSoftware / ViveInputUtility-Unity

A toolkit that helps developing/prototyping VR apps.
http://u3d.as/uF7
Other
352 stars 82 forks source link

Vibration/Haptic feedback for Oculus Rift #213

Closed nezix closed 3 years ago

nezix commented 3 years ago

Haptic feedback does not work for Oculus rift controllers when not using SteamVR, calling TriggerHapticPulse does not make the controllers vibrate.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.Vive;
using HTC.UnityPlugin.Utility;

public class TestHaptic : MonoBehaviour{
    int idf = 0;

    //Vibrate both controllers every 100 frames
    void Update() {
        idf++;
        if (idf % 100 == 0) {
            idf = 0;
            goHaptic();
        }
    }

    public void goHaptic() {
        Debug.Log("Wizzzz");
        ViveInput.TriggerHapticPulse(HandRole.LeftHand, 500);
        ViveInput.TriggerHapticPulse(HandRole.RightHand, 500);
    }
}

This works using SteamVR but not using the Oculus runtime.

I also have this warning spammed:

Device Oculus Touch Controller - Left doesn't have float feature ThumbTouch. Return default value instead.
UnityEngine.Debug:LogWarningFormat (string,object[])
HTC.UnityPlugin.VRModuleManagement.UnityXRModule:GetDeviceFeatureValueOrDefault (UnityEngine.XR.InputDevice,UnityEngine.XR.InputFeatureUsage`1<single>) (at Assets/HTC.UnityPlugin/VRModule/Modules/UnityXRModule.cs:893)
HTC.UnityPlugin.VRModuleManagement.UnityXRModule:UpdateOculusControllerState (HTC.UnityPlugin.VRModuleManagement.IVRModuleDeviceStateRW,UnityEngine.XR.InputDevice) (at Assets/HTC.UnityPlugin/VRModule/Modules/UnityXRModule.cs:673)
HTC.UnityPlugin.VRModuleManagement.UnityXRModule:UpdateControllerState (HTC.UnityPlugin.VRModuleManagement.IVRModuleDeviceStateRW,UnityEngine.XR.InputDevice) (at Assets/HTC.UnityPlugin/VRModule/Modules/UnityXRModule.cs:412)
HTC.UnityPlugin.VRModuleManagement.UnityXRModule:BeforeRenderUpdate () (at Assets/HTC.UnityPlugin/VRModule/Modules/UnityXRModule.cs:275)
HTC.UnityPlugin.VRModuleManagement.VRModule:BeforeRenderUpdateModule () (at Assets/HTC.UnityPlugin/VRModule/VRModuleManager.cs:326)
UnityEngine.GUIUtility:ProcessEvent (int,intptr)
chengnay commented 3 years ago

@nezix Simply update your haptic API to TriggerHapticVibration. The warning spam message is because ThumbTouch and IndexTouch features not supported. If you don't want to see the warning messages, please modify the script as follows,

In Assets\HTC.UnityPlugin\VRModule\Modules\UnityXRModule.cs, line 671,

bool thumbrest = GetDeviceFeatureValueOrDefault(device, new InputFeatureUsage<bool>("Thumbrest"));
//float indexTouch = GetDeviceFeatureValueOrDefault(device, new InputFeatureUsage<float>("IndexTouch"));
//float thumbTouch = GetDeviceFeatureValueOrDefault(device, new InputFeatureUsage<float>("ThumbTouch")); // Not in use

state.SetButtonTouch(VRModuleRawButton.Touchpad, thumbrest);
//state.SetButtonTouch(VRModuleRawButton.Trigger, indexTouch >= 1.0f);
if ((device.characteristics & InputDeviceCharacteristics.Left) != 0)
{
    bool menuButton = GetDeviceFeatureValueOrDefault(device, CommonUsages.menuButton);
    state.SetButtonPress(VRModuleRawButton.System, menuButton);
}

I will fix the TriggerHapticPulse API later, thanks!

nezix commented 3 years ago

Thanks for your answer,

I tried to replace ViveInput.TriggerHapticPulse(HandRole.LeftHand, 500); by ViveInput.TriggerHapticVibration(HandRole.LeftHand); but no luck on my part.

If I directly call OVRInput.SetControllerVibration(0.9f, 0.5f, OVRInput.Controller.RTouch); the right controller vibrates.

chengnay commented 3 years ago

@nezix I use ViveInput.TriggerHapticVibration(HandRole.LeftHand, 500); Could you try this?

nezix commented 3 years ago

I tried this too, same result. Is there something I am missing ?

chengnay commented 3 years ago

Where did you attach your script to? The vibration is kinda weak for the value 500.

nezix commented 3 years ago

I deleted and recreated an empty project and follow the first post recipe and it works now with TriggerHapticVibration... Weird ! Sorry for the waste of time.

chengnay commented 3 years ago

@nezix No worries. Let me know if you still have questions, cheers! I will update the TriggerHapticPulse API later.