2018年3月21日 星期三

Week04_是沫不是沬

🌟LEAP Motion

🌠下載所需的套件(老師已放置在moodle)

Leap_Motion_Orion_Setup_win_3.2.1
Leap_Motion_Core_Assets_4.3.4
Leap_Motion_Interaction_Engine_1.1.1

🌠安裝Leap_Motion_Orion_Setup_win_3.2.1並執行

安裝Leap Motion
 開啟Leap Motion控制器
 亮綠燈表示成功開啟,亮紅燈則需再重新接上USB
 開啟觀察器
 此為失敗畫面
 此為成功畫面

🌠Leap_Motion_Core_Assets_4.3.4

登入Unity,並新增一個專案
匯入已下載好的Package
選擇Leap_Motion_Core_Assets_4.3.4
全部匯入就好
開啟場景Leap Hands Demo(Desktop)
按下執行按鈕
當Leap Motion偵測到手,就會出現在畫面中,執行結果如下圖

🌠Leap_Motion_Interaction_Engine_1.1.1

匯入Leap_Motion_Interaction_Engine_1.1.1
全部都匯入就好
開啟場景Interaction Objects
按下執行按鈕,但會跳出警告訊息,只需依照訊息內容去改就好
由此路徑去找到需要更改的數值Edit→Project Settings→Physics
將Gravity的數值改為-4.905即可
按下執行按鈕,但會跳出警告訊息,只需依照訊息內容去改就好
由此路徑去找到需要更改的數值Edit→Project Settings→Time
將Fixed Timestep改為0.0111
執行結果如下


🌟Homework

🌠按手把的按鈕,並產生文字


前置作業

下載SteamVR的套件
從Asset store搜尋Steam VR
下載此套件
匯入已經下載完的套件
匯入完之後,即可在Project視窗中看到資料夾
開啟Prefabs資料夾
將[CameraRig]拉進場景中
新增一個C#的腳本

程式碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowText : MonoBehaviour {
    public SteamVR_TrackedObject leftController;
    float pressValue=0;

 void Start () {

 }

 void Update () {
        if (leftController == null)  //先確定有沒有偵測到左邊的手把,一定要先做此動作
            return;
        var device = SteamVR_Controller.Input((int)leftController.index);  //找到對應的裝置索引號碼
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) 
              //如果按下Trigger,可將Trigger更換為其他按鈕(Grip,TouchPad,Manu...)
        {
            pressValue = device.GetAxis(Value.VR.EVRButtonId.k_Ebutton_SteamVR_Trigger).x;  //取得x軸的數值
            print("Hello Trigger Press" + "Value:" + pressValue);  //輸出結果
        }
 }
}

在場景中新增一個空物件,並將程式碼加到空物件裡
P.S.要記得將左手手把的物件拉進公開變數中

執行後,可在Console看到操作的結果

🌠將結果顯示在場景中
可延續上述的專案新增一個腳本
其程式碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Homework : MonoBehaviour {
    public SteamVR_TrackedObject leftController;
    public GameObject controllerInfo;
    bool isTriggerPress = false;
    bool isTouchPadTouch = false;
    bool isTouchPadPress = false;
    bool isGripPress = false;
    bool isManuPress = false;
    Vector2 touchpadPosition = new Vector2();
    float pressValue = 0;

    void Start () {

    }

    void Update () {
        string infor = "Left Controller Information : \n";
        if (leftController == null) return;
        var device = SteamVR_Controller.Input((int)leftController.index);
        myTriggerDetect(device);  //Trigger控制
        myTouchPadDetect(device);  //TouchPad控制
        myGripDetect(device);  //Grip控制
        myManuDetect(device);  
//Manu控制

        infor += "Press Trigger : " + isTriggerPress + "\nTrigger Press Value : " + pressValue
            + "\nTouchpad Press : " + isTouchPadPress + "\nTouchpad touch : " + isTouchPadTouch
            + "\nTouchpad Position : " + touchpadPosition + "\nGrip Press : " + isGripPress
            + "\nManu Press : " + isManuPress;
        controllerInfo.GetComponent<TextMesh>().text = infor;
 }

    void myTriggerDetect(SteamVR_Controller.Device device)
    {
        if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
        {
            pressValue = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x;
            isTriggerPress = true;
        }
        else
        {
            isTriggerPress = false;
            pressValue = 0;
        }
    }

    void myTouchPadDetect(SteamVR_Controller.Device device)
    {
        if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
        {
            isTouchPadPress= true;
        }
        else
        {
            isTouchPadPress = false;
        }
        if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
        {
            isTouchPadTouch = true;
            touchpadPosition = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis0);
        }
        else
        {
            isTouchPadTouch = false;
            touchpadPosition = new Vector2(0, 0);
        }
    }

    void myGripDetect(SteamVR_Controller.Device device)
    {
        if (device.GetPress(SteamVR_Controller.ButtonMask.Grip))
        {
            isGripPress = true;
        }
        else
        {
            isGripPress = false;
        }
    }

    void myManuDetect(SteamVR_Controller.Device device)
    {
        if (device.GetPress(SteamVR_Controller.ButtonMask.ApplicationMenu))
        {
            isManuPress = true;
        }
        else
        {
            isManuPress = false;
        }
    }
}

將空物件中原先的腳本移除,並加入此腳本

執行後可從畫面中看到結果

沒有留言:

張貼留言