HP Reverb G2-controllers in Unity

HP Motion controllers zijn een gloednieuw type Windows Mixed Reality controllers: allemaal dezelfde trackingtechnologie met een iets andere set beschikbare ingangen:

  • Touchpad is vervangen door twee knoppen: A en B voor de rechtercontroller en X en Y voor de linkercontroller.
  • Grasp is nu een trigger die een stroom van waarden tussen 0,0 en 1,0 publiceert in plaats van een knop met de status Ingedrukt en Niet ingedrukt.

Omdat de nieuwe invoer niet toegankelijk is via bestaande Windows- en Unity-API's, hebt u het speciale UPM-pakket Microsoft.MixedReality.Input nodig.

Belangrijk

Klassen in dit pakket zijn geen vervanging van bestaande Windows- en Unity-API's, maar vormen een aanvulling op deze api's. Functies die algemeen beschikbaar zijn voor zowel klassieke Windows Mixed Reality-controllers als HP-bewegingscontrollers zijn toegankelijk via hetzelfde codepad met behulp van bestaande API's. Alleen voor de nieuwe invoer is het gebruik van het extra Microsoft.MixedReality.Input-pakket vereist.

Overzicht van HP Motion Controller

Microsoft.MixedReality.Input.MotionController vertegenwoordigt een bewegingscontroller. Elk MotionController-exemplaar heeft een XR. WSA. Input.InteractionSource-peer , die kan worden gecorreleerd aan de hand van handigheid, leverancier-id, product-id en versie.

U kunt MotionController-exemplaren ophalen door een MotionControllerWatcher te maken en u te abonneren op de bijbehorende gebeurtenissen, vergelijkbaar met het gebruik van InteractionManager-gebeurtenissen om nieuwe InteractionSource-exemplaren te detecteren. De methoden en eigenschappen van de MotionController beschrijven de invoer die door de controller wordt ondersteund, inclusief de knoppen, triggers, 2D-as en duimstick. De klasse MotionController biedt ook methoden voor toegang tot invoerstatussen via de klasse MotionControllerReading . De klasse MotionControllerReading vertegenwoordigt een momentopname van de status van de controller op een bepaald moment.

Microsoft.MixedReality.Input installeren met het Mixed Reality-functiehulpprogramma

Installeer de invoegtoepassing Microsoft.MixedReality.Input met de nieuwe Mixed Reality Feature Tool-toepassing. Volg de installatie- en gebruiksinstructies en selecteer het Mixed Reality Invoerpakket in de categorie Mixed Reality Toolkit:

Mixed Reality venster Pakketten van functiehulpprogramma's met mixed reality-invoer gemarkeerd

Microsoft.MixedReality.Input gebruiken

Invoerwaarden

Een MotionController kan twee soorten invoer beschikbaar maken:

  • Knoppen en triggerstatussen worden uitgedrukt door een unieke float-waarde tussen 0,0 en 1,0 die aangeeft hoeveel ze worden ingedrukt.
    • Een knop kan alleen 0,0 (wanneer niet ingedrukt) of 1,0 (wanneer ingedrukt) worden geretourneerd, terwijl een trigger doorlopende waarden kan retourneren tussen 0,0 (volledig losgelaten) en 1,0 (volledig ingedrukt).
  • De toestand van de duimstick wordt uitgedrukt door een Vector2 waarvan de X- en Y-componenten liggen tussen -1,0 en 1,0.

U kunt MotionController.GetPressableInputs() gebruiken om een lijst met invoerwaarden te retourneren die een ingedrukt waarde (knoppen en triggers) retourneren of de methode MotionController.GetXYInputs() om een lijst met invoerwaarden te retourneren die een waarde met twee assen retourneren.

Een MotionControllerReading-exemplaar vertegenwoordigt de status van de controller op een bepaald moment:

  • Met GetPressedValue() wordt de status van een knop of trigger opgehaald.
  • Met GetXYValue() wordt de status van een duimstick opgehaald.

Een cache maken om een verzameling MotionController-exemplaren en hun statussen te onderhouden

Begin met het instantiëren van een MotionControllerWatcher en het registreren van handlers voor de gebeurtenissen MotionControllerAdded en MotionControllerRemoved om een cache met beschikbare MotionController-exemplaren te behouden. Deze cache moet een MonoBehavior zijn die is gekoppeld aan een GameObject, zoals wordt gedemonstreerd in de volgende code:

public class MotionControllerStateCache : MonoBehaviour 
{ 
    /// <summary> 
    /// Internal helper class which associates a Motion Controller 
    /// and its known state 
    /// </summary> 
    private class MotionControllerState 
    { 
        /// <summary> 
        /// Construction 
        /// </summary> 
        /// <param name="mc">motion controller</param>` 
        public MotionControllerState(MotionController mc) 
        { 
            this.MotionController = mc; 
        } 

        /// <summary> 
        /// Motion Controller that the state represents 
        /// </summary> 
        public MotionController MotionController { get; private set; } 
        … 
    } 

    private MotionControllerWatcher _watcher; 
    private Dictionary<Handedness, MotionControllerState> 
        _controllers = new Dictionary<Handedness, MotionControllerState>(); 

    /// <summary> 
    /// Starts monitoring controller's connections and disconnections 
    /// </summary> 
    public void Start() 
    { 
        _watcher = new MotionControllerWatcher(); 
        _watcher.MotionControllerAdded += _watcher_MotionControllerAdded; 
        _watcher.MotionControllerRemoved += _watcher_MotionControllerRemoved; 
        var nowait = _watcher.StartAsync(); 
    } 

    /// <summary> 
    /// Stops monitoring controller's connections and disconnections 
    /// </summary> 
    public void Stop() 
    { 
        if (_watcher != null) 
        { 
            _watcher.MotionControllerAdded -= _watcher_MotionControllerAdded; 
            _watcher.MotionControllerRemoved -= _watcher_MotionControllerRemoved; 
            _watcher.Stop(); 
        } 
    }

    /// <summary> 
    /// called when a motion controller has been removed from the system: 
    /// Remove a motion controller from the cache 
    /// </summary> 
    /// <param name="sender">motion controller watcher</param> 
    /// <param name="e">motion controller </param> 
    private void _watcher_MotionControllerRemoved(object sender, MotionController e) 
    { 
        lock (_controllers) 
        { 
            _controllers.Remove(e.Handedness); 
        } 
    }

    /// <summary> 
    /// called when a motion controller has been added to the system: 
    /// Remove a motion controller from the cache 
    /// </summary> 
    /// <param name="sender">motion controller watcher</param> 
    /// <param name="e">motion controller </param> 
    private void _watcher_MotionControllerAdded(object sender, MotionController e) 
    { 
        lock (_controllers) 
        { 
            _controllers[e.Handedness] = new MotionControllerState(e); 
        } 
    } 
} 

Nieuwe invoer lezen door polling

U kunt de huidige status van elke bekende controller lezen via MotionController.TryGetReadingAtTime tijdens de Update-methode van de klasse MonoBehavior. U wilt DateTime.Now doorgeven als tijdstempelparameter om ervoor te zorgen dat de meest recente status van de controller wordt gelezen.

public class MotionControllerStateCache : MonoBehaviour 
{ 
    … 

    private class MotionControllerState 
    {
        … 

        /// <summary> 
        /// Update the current state of the motion controller 
        /// </summary> 
        /// <param name="when">time of the reading</param> 
        public void Update(DateTime when) 
        { 
            this.CurrentReading = this.MotionController.TryGetReadingAtTime(when); 
        } 

        /// <summary> 
        /// Last reading from the controller 
        /// </summary> 
        public MotionControllerReading CurrentReading { get; private set; } 
    } 

    /// <summary> 
    /// Updates the input states of the known motion controllers 
    /// </summary> 
    public void Update() 
    { 
        var now = DateTime.Now; 

        lock (_controllers) 
        { 
            foreach (var controller in _controllers) 
            { 
                controller.Value.Update(now); 
            } 
        } 
    } 
} 

U kunt de huidige invoerwaarde van de controllers ophalen met behulp van de hand van de controller:

public class MotionControllerStateCache : MonoBehaviour 
{ 
    … 
    /// <summary> 
    /// Returns the current value of a controller input such as button or trigger 
    /// </summary> 
    /// <param name="handedness">Handedness of the controller</param> 
    /// <param name="input">Button or Trigger to query</param> 
    /// <returns>float value between 0.0 (not pressed) and 1.0 
    /// (fully pressed)</returns> 
    public float GetValue(Handedness handedness, ControllerInput input) 
    { 
        MotionControllerReading currentReading = null; 

        lock (_controllers) 
        { 
            if (_controllers.TryGetValue(handedness, out MotionControllerState mc)) 
            { 
                currentReading = mc.CurrentReading; 
            } 
        } 

        return (currentReading == null) ? 0.0f : currentReading.GetPressedValue(input); 
    } 

    /// <summary> 
    /// Returns the current value of a controller input such as button or trigger 
    /// </summary> 
    /// <param name="handedness">Handedness of the controller</param> 
    /// <param name="input">Button or Trigger to query</param> 
    /// <returns>float value between 0.0 (not pressed) and 1.0 
    /// (fully pressed)</returns> 
    public float GetValue(UnityEngine.XR.WSA.Input.InteractionSourceHandedness handedness, ControllerInput input) 
    { 
        return GetValue(Convert(handedness), input); 
    } 

    /// <summary> 
    /// Returns a boolean indicating whether a controller input such as button or trigger is pressed 
    /// </summary> 
    /// <param name="handedness">Handedness of the controller</param> 
    /// <param name="input">Button or Trigger to query</param> 
    /// <returns>true if pressed, false if not pressed</returns> 
    public bool IsPressed(Handedness handedness, ControllerInput input) 
    { 
        return GetValue(handedness, input) >= PressedThreshold; 
    } 
} 

Als u bijvoorbeeld de analoge begripswaarde van een InteractionSource wilt lezen:

/// Read the analog grasp value of all connected interaction sources 
void Update() 
{ 
    … 
    var stateCache = gameObject.GetComponent<MotionControllerStateCache>(); 
    foreach (var sourceState in InteractionManager.GetCurrentReading()) 
    { 
        float graspValue = stateCache.GetValue(sourceState.source.handedness, 
            Microsoft.MixedReality.Input.ControllerInput.Grasp);
        … 
    }
} 

Gebeurtenissen genereren op de nieuwe invoer

In plaats van de status van een controller eenmaal per frame te peilen, hebt u de mogelijkheid om alle statuswijzigingen als gebeurtenissen te verwerken, zodat u zelfs de snelste acties kunt afhandelen die minder dan een frame duren. Deze aanpak werkt alleen als de cache van bewegingscontrollers alle statussen verwerkt die zijn gepubliceerd door een controller sinds het laatste frame. Dit kunt u doen door de tijdstempel op te slaan van de laatste MotionControllerReading die is opgehaald uit een MotionController en MotionController.TryGetReadingAfterTime():

private class MotionControllerState 
{ 
    … 
    /// <summary> 
    /// Returns an array representng buttons which are pressed 
    /// </summary> 
    /// <param name="reading">motion controller reading</param> 
    /// <returns>array of booleans</returns> 
    private bool[] GetPressed(MotionControllerReading reading) 
    { 
        if (reading == null) 
        { 
            return null; 
        } 
        else 
        { 
            bool[] ret = new bool[this.pressableInputs.Length]; 
            for (int i = 0; i < pressableInputs.Length; ++i) 
            { 
                ret[i] = reading.GetPressedValue(pressableInputs[i]) >= PressedThreshold; 
            } 

            return ret; 
        } 
    } 

    /// <summary> 
    /// Get the next available state of the motion controller 
    /// </summary> 
    /// <param name="lastReading">previous reading</param> 
    /// <param name="newReading">new reading</param> 
    /// <returns>true is a new reading was available</returns> 
    private bool GetNextReading(MotionControllerReading lastReading, out MotionControllerReading newReading) 
    { 
        if (lastReading == null) 
        { 
            // Get the first state published by the controller 
            newReading = this.MotionController.TryGetReadingAfterSystemRelativeTime(TimeSpan.FromSeconds(0.0)); 
        } 
        else 
        { 
            // Get the next state published by the controller 
            newReading = this.MotionController.TryGetReadingAfterTime(lastReading.InputTime); 
        } 

        return newReading != null; 
    } 

    /// <summary> 
    /// Processes all the new states published by the controller since the last call 
    /// </summary> 
    public IEnumerable<MotionControllerEventArgs> GetNextEvents() 
    {
        MotionControllerReading lastReading = this.CurrentReading; 
        bool[] lastPressed = GetPressed(lastReading); 
        MotionControllerReading newReading; 
        bool[] newPressed; 

        while (GetNextReading(lastReading, out newReading)) 
        { 
            newPressed = GetPressed(newReading); 

            // If we have two readings, compare and generate events 
            if (lastPressed != null) 
            { 
                for (int i = 0; i < pressableInputs.Length; ++i) 
                { 
                    if (newPressed[i] != lastPressed[i]) 
                    { 
                        yield return new MotionControllerEventArgs(this.MotionController.Handedness, newPressed[i], this.pressableInputs[i], newReading.InputTime); 
                    } 
                } 
            } 

            lastPressed = newPressed; 
            lastReading = newReading; 
        } 

        // No more reading 
        this.CurrentReading = lastReading; 
    } 
} 

Nu u de interne cacheklassen hebt bijgewerkt, kan de klasse MonoBehavior twee gebeurtenissen ( Pressed en Released ) weergeven en deze verhogen vanuit de update()-methode:

/// <summary> 
/// Event argument class for InputPressed and InputReleased events 
/// </summary> 
public class MotionControllerEventArgs : EventArgs 
{ 
    public MotionControllerEventArgs(Handedness handedness, bool isPressed, rollerInput input, DateTime inputTime) 
    { 
        this.Handedness = handedness; 
        this.Input = input; 
        this.InputTime = inputTime; 
        this.IsPressed = isPressed; 
    } 

    /// <summary> 
    /// Handedness of the controller raising the event 
    /// </summary> 
    public Handedness Handedness { get; private set; } 

    /// <summary> 
    /// Button pressed or released 
    /// </summary> 
    public ControllerInput Input { get; private set; } 

    /// <summary> 
    /// Time of the event 
    /// </summary> 
    public DateTime InputTime { get; private set; } 

    /// <summary> 
    /// true if button is pressed, false otherwise 
    /// </summary> 
    public bool IsPressed { get; private set; } 
} 

/// <summary> 
/// Event raised when a button is pressed 
/// </summary> 
public event EventHandler<MotionControllerEventArgs> InputPressed; 

/// <summary> 
/// Event raised when a button is released 
/// </summary> 
public event EventHandler<MotionControllerEventArgs> InputReleased; 

/// <summary> 
/// Updates the input states of the known motion controllers 
/// </summary> 
public void Update() 
{ 
    // If some event handler has been registered, we need to process all states  
    // since the last update, to avoid missing a quick press / release 
    if ((InputPressed != null) || (InputReleased != null)) 
    { 
        List<MotionControllerEventArgs> events = new <MotionControllerEventArgs>(); 

        lock (_controllers) 
        { 
            foreach (var controller in _controllers) 
            { 
                events.AddRange(controller.Value.GetNextEvents()); 
            } 
        } 
 
        // Sort the events by time 
        events.Sort((e1, e2) => DateTime.Compare(e1.InputTime, e2.InputTime)); 

        foreach (MotionControllerEventArgs evt in events) 
        { 
            if (evt.IsPressed && (InputPressed != null)) 
            { 
                InputPressed(this, evt); 
            } 
            else if (!evt.IsPressed && (InputReleased != null)) 
            { 
                InputReleased(this, evt); 
            } 
        } 
    } 
    else 
    { 
        // As we do not predict button presses and the timestamp of the next e is in the future 
        // DateTime.Now is correct in this context as it will return the latest e of controllers 
        // which is the best we have at the moment for the frame. 
        var now = DateTime.Now; 
        lock (_controllers) 
        { 
            foreach (var controller in _controllers) 
            { 
                controller.Value.Update(now); 
            } 
        } 
    } 
} 

De structuur in de bovenstaande codevoorbeelden maakt het registreren van gebeurtenissen veel beter leesbaar:

public InteractionSourceHandedness handedness; 
public Microsoft.MixedReality.Input.ControllerInput redButton;

// Start of the Mono Behavior: register handlers for events from cache 
void Start() 
{ 
    var stateCache = gameObject.GetComponent<MotionControllerStateCache>(); 
    stateCache.InputPressed += stateCache_InputPressed; 
    stateCache.InputReleased += stateCache_InputReleased; 
    … 
} 

// Called when a button is released 
private void stateCache_InputReleased(object sender, MotionControllerStateCache.MotionControllerEventArgs e) 
{ 
    if ((e.SourceHandedness == handedness) && (e.Input == redButton)) 
    { 
        … 
    } 
} 

// Called when a button is pressed 
private void stateCache_InputPressed(object sender, MotionControllerStateCache.MotionControllerEventArgs e) 
{ 
    if ((e.SourceHandedness == handedness) && (e.Input == redButton)) 
    { 
        … 
    } 
} 

Zie ook