Unity の HP Reverb G2 コントローラー

HP モーション コントローラーは、まったく新しいタイプの Windows Mixed Reality コントローラーです。すべて同じトラッキングテクノロジーで、使用可能な入力のセットがわずかに異なります。

  • タッチパッドは、右コントローラーについては A と B、左コントローラーについては X と Y の 2 つのボタンに置き換えられました。
  • grasp は、ボタンが押されているか押されていないかの状態の代わりに、0.0 から 1.0 までの値のストリームを発行するトリガーになりました。

新しい入力には既存のWindowsおよびUnityAPIからアクセスできないため、専用の Microsoft.MixedReality.Input UPM パッケージが必要です。

重要

このパッケージ内のクラスは、既存の Windows API と Unity API を置き換えるものではなく、それらを補完するものです。 従来の Windows Mixed Reality コントローラーと HP モーション コントローラーの両方で一般的に利用できる機能には、既存のAPIを使用して同じコードパスからアクセスできます。 新しい入力についてのみ、追加の Microsoft.MixedReality.Input パッケージを使用する必要があります。

HP モーション コントローラーの概要

Microsoft.MixedReality.Input.MotionController はモーション コントローラーを表します。 各 MotionController インスタンス には、XR.WSA.Input.InteractionSource ピアがあり、利き手、ベンダー ID、製品 ID、およびバージョンを使用して相互に関連付けることができます。

MotionControllerインスタンスをつかむには、InteractionManagerイベントを使用して新しいInteractionSourceインスタンスを検出するのと同様に、MotionControllerWatcherを作成し、そのイベントをサブスクライブします。 MotionController のメソッドとプロパティは、ボタン、トリガー、2D 軸、サムスティックなど、コントローラーでサポートされている入力を記述します。 MotionController クラスでは、MotionControllerReading クラスを介して入力状態にアクセスするためのメソッドも公開します。 MotionControllerReading クラスは、特定の時点における、コントローラーの状態のスナップショットを表します。

Mixed Reality Feature ツール を使用して Microsoft.MixedReality.Input をインストールする

Microsoft.MixedReality.Input プラグインは、新しい Mixed Reality Feature Tool アプリケーションを使用してインストールします。 インストールと使用方法の説明に従い、MixedRealityToolkitカテゴリーでMixedRealityInputパッケージを選択します。

mixed Reality 入力が強調表示された [機能ツール パッケージ] ウィンドウをMixed Realityする

Microsoft.MixedReality.Input を使用する

入力値

MotionController では、次の 2 種類の入力を公開できます。

  • ボタンとトリガー状態は、押された量を示す 0.0 から 1.0 までの一意のfloat値で表されます。
    • ボタンは 0.0 (押されていない) または 1.0 (押されている) のみを返すことができますが、トリガーは 0.0 (完全に離されている) から 1.0 (完全に押されている) までの連続値を返せます。
  • サムスティックの状態は、Vector2 で表され、X コンポーネントと Y コンポーネントが、-1.0 から 1.0 までの値で示されます。

MotionController.GetPressableInputs() を使って入力のリストを返すと押下の値 (ボタンとトリガー) を取得でき、MotionController.GetXYInputs() メソッドを使って入力のリストを返すと 2 軸の値を取得できます。

MotionControllerReading インスタンスは、特定の時点でのコントローラーの状態を表します。

  • GetPressedValue() は、ボタンまたはトリガーの状態を取得します。
  • GetXYValue() は、サムスティックの状態を取得します。

キャッシュを作成して、MotionController インスタンスとその状態のコレクションを保持する

まず、MotionControllerWatcher をインスタンス化し、その MotionControllerAdded および MotionControllerRemoved イベントのハンドラーを登録して、使用可能な MotionController インスタンスのキャッシュを保持します。 このキャッシュは、次のコードに示す GameObject にアタッチされた、MonoBehavior である必要があります。

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); 
        } 
    } 
} 

ポーリングによって新しい入力を読み取り

各既知のコントローラーの現在の状態は、MonoBehavior クラスの Update メソッドの実行時に、MotionController.TryGetReadingAtTime を通じて読み取ることができます。 コントローラーの最新の状態が読み取られるようにするには、タイムスタンプ パラメーターとして DateTime.Now を渡す必要があります。

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); 
            } 
        } 
    } 
} 

コントローラの Handedness を使用して、コントローラの現在の入力値を取得できます。

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; 
    } 
} 

たとえば、InteractionSource のアナログのつかみ値を読み取るには、次のようにします。

/// 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);
        … 
    }
} 

新しい入力からイベントを生成する

コントローラーの状態をフレームごとに 1 回ずつポーリングする代わりに、すべての状態の変更をイベントとして処理することもできます。この方法を使えば、フレームよりも短い時間で最も迅速なアクションを処理できます。 このアプローチを機能させるには、モーションコントローラのキャッシュは最後のフレーム以降にコントローラによって発行されたすべての状態を処理する必要があります。これを行うには、MotionController から取得した最後の MotionControllerReading のタイムスタンプを保存し、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; 
    } 
} 

キャッシュの内部クラスを更新したので、MonoBehavior クラスで 2 つのイベント (Pressed と Released) を公開し、それらを Update() メソッドから発生させることができます。

/// <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); 
            } 
        } 
    } 
} 

上記のコード例の構造を使用すると、イベントの登録がはるかに読み取りやすくなります。

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)) 
    { 
        … 
    } 
} 

関連項目