夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

MRTKでHandMeshを動的にアクティブ、ディアクティブにする。

本日はMRTK機能調査枠です。

MRTKではHandTrackingを使用できるデバイスでHandMeshを作成することができます。

redhologerbera.hatenablog.com

これはMixedRealityToolkitのInputProviderArticulated HandTrackingからHand Mesh Visualization ModesをEditor、もしくはPlayerに指定しているばあいそれぞれエディタ上、実機でメッシュが生成されます。

HandTrackingによる手のメッシュの表示は比較的高コストであるため、MicrosoftのHandTrackingに関するドキュメントではハンドメッシュの表示は無効化することが推奨されています、

docs.microsoft.com

今回は実機内で動的にHandMeshをアクティブ、ディアクティブに変更できるようにします。

〇ToggleHandVisualisation

ToggleHandVisualisationはMRTKで提供されているコンポーネントの一つで、その名の通り手のメッシュおよびJointの表示、非表示をトグルで管理できます。

中身を見てみるとOnToggleHandMesh()およびOnToggleHandJoint()の二つの関数があります。

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Input;
using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.UI
{
    [AddComponentMenu("Scripts/MRTK/SDK/ToggleHandVisualisation")]
    public class ToggleHandVisualisation : MonoBehaviour
    {
        /// <summary>
        /// Toggles hand mesh visualization
        /// </summary>
        public void OnToggleHandMesh()
        {
            MixedRealityInputSystemProfile inputSystemProfile = CoreServices.InputSystem?.InputSystemProfile;//Profileを取得
            if (inputSystemProfile == null)
            {
                return; //nullの場合処理を終了
            }
 //存在する場合さらに細かい情報を取得
            MixedRealityHandTrackingProfile handTrackingProfile = inputSystemProfile.HandTrackingProfile;
            if (handTrackingProfile != null)
            {
                handTrackingProfile.EnableHandMeshVisualization = !handTrackingProfile.EnableHandMeshVisualization;//オンオフの切り替え
            }
        }

        /// <summary>
        /// Toggles hand joint visualization
        /// </summary>
        public void OnToggleHandJoint()
        {
            MixedRealityHandTrackingProfile handTrackingProfile = null;

            if (CoreServices.InputSystem?.InputSystemProfile != null)
            {
                handTrackingProfile = CoreServices.InputSystem.InputSystemProfile.HandTrackingProfile;
            }

            if (handTrackingProfile != null)
            {
                handTrackingProfile.EnableHandJointVisualization = !handTrackingProfile.EnableHandJointVisualization;
            }
        }
    }
}

これらはそれぞれ呼び出すことで手のメッシュのオンオフ、手のJointのオンオフが切り替えることができます。

これはMixedRealityToolkitコンポーネントのProfilerで定義されている画像赤枠の部分に動的にアクセスして変更していることになります。

本日は以上です