使用 Leap 動作和 Ultraleap — MRTK2

需要 Leap Motion Controller才能使用此資料提供者。

Leap Motion Data Provider 可針對 VR 啟用清楚的手部追蹤,而且在編輯器中快速原型設計很有用。 資料提供者可以設定為使用掛接在頭戴式裝置上的 Leap Motion Controller,或放在桌面上。

LeapMotionIntroGif

在獨立平臺上,此提供者可以在編輯器和裝置上使用。 它也可以在 UWP 平臺上的編輯器中使用,但在 UWP 組建中不能使用。

MRTK 版本 支援 Leap Motion Unity 模組版本
2.6.x 4.5.0, 4.5.1
2.7.0 - 2.7.2 4.5.0, 4.5.1, 4.6.0, 4.7.0, 4.7.1, 4.8.0
2.7.3 4.5.0、4.5.1、4.6.0、4.7.0、4.7.1、4.8.0、4.9.1 (請參閱下列附注)

注意

Unity 模組 4.9.1 需要 Gemini 5.2 追蹤服務或更高版本。 Unity 外掛程式 V4.9.1 僅在 Unity 2019 上正式支援。 請參閱 4.9.1 的版本資訊

Unity 模組已重新命名為 V5.0.0 中的 Unity 外掛程式。

MRTK 版本 支援的 Ultraleap Unity 外掛程式版本
2.8.0 5.3.0 (請參閱下列附注)

注意

任何版本的 MRTK 都不支援 Unity 外掛程式 5.0.0、5.1.0 和 5.2.0 版。 建議使用者升級至 5.3.0。 Unity 外掛程式 5.3.0 需要 Gemini 5.2 追蹤服務或更高版本。

在 MRTK 中使用 Leap Motion (by Ultraleap) 手部追蹤

  1. 匯入 MRTK 和 Leap Motion Unity 模組/Unity 外掛程式

    • 安裝最新的 Leap Motion Tracking Software ,以在尚未安裝追蹤服務時取得追蹤服務
    • Microsoft.MixedReality.Toolkit.Foundation 套件匯入 Unity 專案中。
    • 將最新版 的 Leap Motion Unity 模組/Unity 外掛程式 下載並匯入專案中
      • 針對 Unity 模組:僅匯入專案中 的核心 套件
      • 若為 Unity 外掛程式:僅匯入專案中的 追蹤 套件。 目前僅支援 .unitypackage。

    重要

    Unity 已淘汰的 SelectionMode.OnlyUserModifiable 在 Unity 2019.4.19 之後,這會導致來自 Ultraleap Hotkeys.cs 腳本的下列錯誤:Assets\Plugins\LeapMotion\Core\Editor\Hotkeys.cs (24,91) :錯誤 CS0619:'SelectionMode.OnlyUserModifiable' 已過時:''OnlyUserModifiable' 已過時。 請改用 [可編輯]。 (UnityUpgradeable) - > Editable'。

    若要解決此問題,請將 Hotkeys.cs 腳本中的 SelectionMode.OnlyUserModifiable 變更為 SelectionMode.Editable,位於 Plugins/LeapMotion/Core/Editor/Hotkeys.cs。

  2. 整合 Leap Motion Unity 模組/Unity 外掛程式與 MRTK

    • Unity 模組/Unity 外掛程式位於專案中之後,流覽至[Mixed Reality>Toolkit>公用程式>Leap Motion 整合 Leap 動作> Unity 模組]。

    注意

    Unity 模組與 MRTK 整合會將 10 個元件定義新增至專案,並將參考新增至Microsoft.MixedReality.Toolkit.Providers.LeapMotion元件定義。 確認已關閉 Visual Studio。

    LeapMotionIntegration

  3. 新增 Leap 動作資料提供者

    • 建立新的 Unity 場景
    • 流覽至Mixed Reality [將MRTK新增至場景] 和[設定] > ,以將 MRTK 新增至場景
    • 選取階層中的 MixedRealityToolkit 遊戲物件,然後選取 [複製和自訂 ] 以複製預設混合實境設定檔。

    LeapMotionProfileClone

    • 選取 輸入 組態設定檔

    輸入組態設定檔 1

    • 在輸入系統設定檔中選取 [複製 ],以啟用修改。

    LeapMotionInputProfileClone

    • 開啟 [ 輸入資料提供者 ] 區段,選取頂端的 [ 新增資料提供者 ],清單結尾將會新增新的資料提供者。 開啟新的資料提供者,並將 Type 設定為 Microsoft.MixedReality.Toolkit.LeapMotion.Input > LeapMotionDeviceManager

    Leap 新增資料提供者

    • 選取 [複製 ] 以變更預設的 Leap Motion 設定。

    LeapDataProviderPreClone

    • Leap Motion Data Provider 包含 LeapControllerOrientation 屬性,這是 Leap Motion Controller 的位置。 LeapControllerOrientation.Headset 表示控制器已掛接在頭戴式裝置上。 LeapControllerOrientation.Desk 表示控制器放在桌面上。 預設值設定為 LeapControllerOrientation.Headset

    • 每個控制器方向都包含位移屬性:

      • 頭戴式裝置方向位移屬性會鏡像 LeapXRServiceProvider 元件中的位移屬性。 有 LeapVRDeviceOffsetMode 三個選項:預設、手動前端位移和轉換。 如果位移模式為 Default,則不會將位移套用至 Leap Motion Controller。 手動前端位移模式允許修改三個屬性: LeapVRDeviceOffsetYLeapVRDeviceOffsetZLeapVRDeviceTiltX 。 然後,軸位移屬性值會套用至預設控制器位置。 轉換位移模式包含 LeapVRDeviceOrigin Transform 屬性,指定 Leap Motion Controller 的新原點。

      • Desk方向包含 屬性, LeapControllerOffset 可定義桌面閏手的錨點位置。 位移會相對於主要相機位置進行計算,預設值為 (0,-0.2,0.35) ,以確保手部出現在相機前方和檢視中。

        注意

        設定檔中的位移屬性會在應用程式啟動時套用一次。 若要在執行時間修改值,請從 Leap Motion 裝置管理員取得 Leap Motion 服務提供者:

        LeapMotionDeviceManager leapMotionDeviceManager = CoreServices.GetInputSystemDataProvider<LeapMotionDeviceManager>();
        LeapXRServiceProvider leapXRServiceProvider = leapMotionDeviceManager.LeapMotionServiceProvider as LeapXRServiceProvider; 
        
    • EnterPinchDistanceExitPinchDistance 是捏合/空中點選手勢偵測的距離閾值。 捏合手勢的計算方式是測量食指尖與指尖之間的距離。 若要在輸入關閉事件上引發 ,預設值 EnterPinchDistance 會設定為 0.02。 若要在輸入事件上引發 , (結束捏合) ,索引指尖與指尖之間的預設距離為 0.05。

    LeapControllerOrientation:耳機 (預設)

    LeapHeadsetGif

    LeapHeadsetInspector

    LeapControllerOrientation:桌子

    LeapDeskGif

    LeapDeskInspector

  4. 測試 Leap 動作資料提供者

    • 將 Leap 動作資料提供者新增至輸入系統設定檔之後,按播放,將手移到 Leap 動作控制器前方,您應該會看到手部的聯合標記法。
  5. 建置您的專案

取得手部接合

使用 Leap Motion Data Provider 取得聯合,與 MRTK 接合手部的手部擷取相同。 如需詳細資訊,請參閱 手部追蹤

在 Unity 場景中使用 MRTK,並將 Leap Motion Data Provider 新增為輸入系統設定檔中的輸入資料提供者,建立空的遊戲物件並附加下列範例腳本。

此腳本是一個簡單的範例,說明如何在 Leap Motion Hand 中擷取手部手部的手部姿勢。 球體會遵循左 Leap 手,而立方體則遵循右 Leap 手。

using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using System.Collections.Generic;
using UnityEngine;

public class LeapHandJoints : MonoBehaviour, IMixedRealityHandJointHandler
{
    private GameObject leftHandSphere;
    private GameObject rightHandCube;

    private void Start()
    {
        // Register the HandJointHandler as a global listener
        CoreServices.InputSystem.RegisterHandler<IMixedRealityHandJointHandler>(this);

        // Create a sphere to follow the left hand palm position
        leftHandSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        leftHandSphere.transform.localScale = Vector3.one * 0.03f;

        // Create a cube to follow the right hand palm position
        rightHandCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        rightHandCube.transform.localScale = Vector3.one * 0.03f;
    }

    public void OnHandJointsUpdated(InputEventData<IDictionary<TrackedHandJoint, MixedRealityPose>> eventData)
    {
        if (eventData.Handedness == Handedness.Left)
        {
            Vector3 leftHandPalmPosition = eventData.InputData[TrackedHandJoint.Palm].Position;
            leftHandSphere.transform.position = leftHandPalmPosition;
        }

        if (eventData.Handedness == Handedness.Right)
        {
            Vector3 rightHandPalmPosition = eventData.InputData[TrackedHandJoint.Palm].Position;
            rightHandCube.transform.position = rightHandPalmPosition;
        }
    }
}

Unity 編輯器工作流程提示

使用 Leap Motion 資料提供者不需要 VR 頭戴式裝置。 您可以使用 Leap 手部在編輯器中測試 MRTK 應用程式的變更,而不需頭戴式裝置。

Leap Motion Hands 會顯示在編輯器中,而不會插入 VR 頭戴式裝置。 LeapControllerOrientation如果 設定為頭戴式裝置,則 Leap Motion 控制器必須由一手與相機向前向前按住。

注意

如果在編輯器中使用 WASD 鍵移動相機,且 LeapControllerOrientation頭戴式裝置,則手部將不會遵循相機。 如果 VR 頭戴式裝置在 LeapControllerOrientation 設定 戴式裝置時插入,則手部只會遵循相機移動。 如果 LeapControllerOrientation 設定為 Desk,Leap 手將會遵循編輯器中的相機移動。

從專案移除 Leap 動作

  1. 流覽至Mixed Reality Toolkit>Leap Motion>個別 Leap Motion Unity 模組
    • 在此步驟中,讓 Unity 重新整理為 Microsoft.MixedReality.Toolkit.Providers.LeapMotion.asmdef 檔案中的參考
  2. 關閉 Unity
  3. 如果 Visual Studio 已開啟,請關閉
  4. 開啟檔案總管並流覽至 MRTK Unity 專案的根目錄
    • 刪除 UnityProjectName/Library 目錄
    • 刪除 UnityProjectName/Assets/Plugins/LeapMotion 目錄
    • 刪除 UnityProjectName/Assets/Plugins/LeapMotion.meta 檔案
  5. 重新開啟 Unity

在 Unity 2018.4 中,您可能會注意到在刪除程式庫和 Leap Motion Core 資產之後,仍會保留在主控台中。 如果在重新開啟之後記錄錯誤,請再次重新開機 Unity。

一般錯誤

Leap Motion 未與 MRTK 整合

若要測試 Leap Motion Unity 模組是否已與 MRTK 整合:

  • 流覽至Mixed Reality工具組 > 公用程式 > Leap Motion > Check 整合狀態
    • 這會顯示快顯視窗,其中包含有關 Leap Motion Unity 模組是否已與 MRTK 整合的訊息。
  • 如果訊息指出資產尚未整合:
    • 確定 Leap Motion Unity 模組位於專案中
    • 請確定支援新增的版本,請參閱頁面頂端的資料表,以瞭解支援的版本。
    • 嘗試Mixed Reality工具組 > 公用程式 > Leap Motion > 整合 Leap Motion Unity 模組

複製元件多人遊戲 HLAPI 失敗

在匯入 Leap Motion Unity 核心資產時,可能會記錄此錯誤:

Copying assembly from 'Temp/com.unity.multiplayer-hlapi.Runtime.dll' to 'Library/ScriptAssemblies/com.unity.multiplayer-hlapi.Runtime.dll' failed

解決方案

  • 短期解決方案是重新開機 Unity。 如需詳細資訊,請參閱 問題 7761

Leap 動作範例場景

此範例場景使用 DefaultLeapMotionConfiguration 設定檔,並判斷 Unity 專案是否已正確設定為使用 Leap Motion Data Provider。

範例場景包含在MRTK/Examples/Demos/HandTracking/目錄中的Microsoft.MixedReality.Toolkit.Examples套件中。

另請參閱