Kinect結(jié)合Unity基礎(chǔ)使用(二)

一桶唐、Kinect for Unity插件

插件:Kinect v2.9.unitypackage

官方案例:

  • Kinect for Unity官方示例演示:

  • OverlayDemo:小球跟收手走

  • GesturesDemo:手滑動在跳,翻轉(zhuǎn)立方體

  • InteractionDemo: 握拳移動物體

  • AvatarsDemo:場景中的人物跟著玩家運動

二昆码、Kinect Manager使用

第一步:camera上掛載Kinect Manager腳本

1祝蝠、顯示彩色數(shù)據(jù)流

KinectManager.Instance.GetUsersClrTex();獲取用戶彩色貼圖

勾選compute color map

public RawImage kinectImg;
void Update()
    {
        bool isInit =  KinectManager.Instance.IsInitialized();
        if (isInit)
        {
            //Kinect準備好了
            if (kinectImg!=null && kinectImg.texture==null)
            {
                //彩色數(shù)據(jù)流
                Texture2D kinectPic = KinectManager.Instance.GetUsersClrTex();//獲取用戶彩色貼圖
                kinectImg.texture = kinectPic;  //把彩色數(shù)據(jù)給控件顯示
            }
        }
}

2魔慷、顯示深度數(shù)據(jù)流

KinectManager.Instance.GetUsersLblTex();//獲取深度數(shù)據(jù)

勾選 Kinect Manager腳本組件的 compute use map

(同時勾選compute color map蕉陋、compute use map)類似PS摳圖

public RawImage kinectImg;
void Update()
    {
        bool isInit =  KinectManager.Instance.IsInitialized();
        if (isInit)
        {
            //Kinect準備好了
            if (kinectImg!=null && kinectImg.texture==null)
            {
               //深度數(shù)據(jù)流
                Texture2D kinectUseMap = KinectManager.Instance.GetUsersLblTex();//獲取深度數(shù)據(jù)
                kinectImg.texture = kinectUseMap;
            }
        }
}

三闭专、Kinect Manager使用2

1、獲取人物ID

long userId = KinectManager.Instance.GetPrimaryUserID();

2逗余、獲取關(guān)節(jié)信息

獲取左手位置

  GetJointKinectPosition() 獲取的是相對于Kinect的坐標

  GetJointPosition() 獲取的是經(jīng)過轉(zhuǎn)換得到的坐標(Y值加上Kinect設(shè)備高度)
if (KinectManager.Instance.IsUserDetected()){
    //檢測到玩家
    long userId = KinectManager.Instance.GetPrimaryUserID();
    int jointType = (int)KinectInterop.JointType.HandLeft;
    if (KinectManager.Instance.IsJointTracked(userId, jointType))
    {
         //關(guān)節(jié)點被追蹤到
         //GetJointKinectPosition坐標
        Vector3 leftHandKinectPos = KinectManager.Instance.GetJointKinectPosition(userId, jointType);
        print("kx = " + leftHandKinectPos.x + " ky = " + leftHandKinectPos.y + " kz = " + leftHandKinectPos.z);  
        //GetJointPosition坐標                      
        Vector3 leftHandPos =KinectManager.Instance.GetJointPosition(userId, jointType);
        print("x = " + leftHandPos.x + " y = " + leftHandPos.y + " z = " + leftHandPos.z);
    }
}

3特咆、獲取手勢狀態(tài)

  KinectManager.Instance.GetLeftHandState(userId);獲取左手手勢狀態(tài)
if (KinectManager.Instance.IsUserDetected()){
    //檢測到玩家
    long userId = KinectManager.Instance.GetPrimaryUserID();
    int jointType = (int)KinectInterop.JointType.HandLeft;
    if (KinectManager.Instance.IsJointTracked(userId, jointType))
    {
         //關(guān)節(jié)點被追蹤到
         KinectInterop.HandState leftHandState = KinectManager.Instance.GetLeftHandState(userId);
         if(leftHandState == KinectInterop.HandState.Closed)
        {
             print("左手握拳");
        }else if (leftHandState == KinectInterop.HandState.Open)
        {
             print("左手打開");
        }else if (leftHandState == KinectInterop.HandState.Lasso)
        {
             print("Yes 手勢");
        }
    }
}

4、獲取用戶信息

if (KinectManager.Instance.IsUserDetected()){
    //檢測到玩家
    long userId = KinectManager.Instance.GetPrimaryUserID();
    //獲取用戶離體感多遠
    Vector3 userPos = KinectManager.Instance.GetUserPosition(userId);
    print("userPos x ="+userPos.x+ " userPos y =" + userPos.y+ " userPos z =" + userPos.z);
}

四录粱、練習

1腻格、人物摳圖背景半透明

clipboard.png

2、坐標系

  • 屏幕坐標系:(左下角為原點)

  • 鼠標坐標系:(與屏幕坐標系一樣啥繁,左下角是原點)

  • UGUI坐標系:(與鼠標坐標系類似菜职,往右,x增大旗闽,往上酬核,y增 大。原點與錨點有關(guān))

3适室、UI點擊的實現(xiàn):

    1. 獲取右手(左手/關(guān)節(jié))的3D坐標
    1. 3D坐標轉(zhuǎn)換為屏幕坐標
    1. 屏幕坐標轉(zhuǎn)換為UGUI坐標(得到手在UGUI的位置)
    1. 判斷手的位置是否在UGUI控件上
    1. 手如果在UGUI控件上嫡意,判斷是否握拳
    1. 握拳就表示點擊了該UGUI
clipboard2.png

代碼實現(xiàn):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UGUI_Test : MonoBehaviour
{
    public Canvas canvas;
    public Image rightHand;
    public Image btn1;
    void Update()
    {
        if (KinectManager.Instance.IsUserDetected())
        {
            long userId = KinectManager.Instance.GetPrimaryUserID();//獲取用戶ID
            int jointType = (int)KinectInterop.JointType.HandRight;//表示要追蹤的是右手
            if (KinectManager.Instance.IsJointTracked(userId, jointType))
            {
                Vector3 rightHandPos = KinectManager.Instance.GetJointKinectPosition(userId, jointType);//步驟1
                Vector3 rightHandScreenPos = Camera.main.WorldToScreenPoint(rightHandPos);//右手位置轉(zhuǎn)化到屏幕坐標 步驟2
                Vector2 rightHandSenPos = new Vector2(rightHandScreenPos.x,rightHandScreenPos.y);
                Vector2 rightHandUGuiPos;
                if(RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, 
                rightHandSenPos,null, out rightHandUGuiPos))
                {
                    //表示右手在canvas所表示的矩形范圍
                    RectTransform rightRectTf = rightHand.transform as RectTransform;
                    rightRectTf.anchoredPosition = rightHandUGuiPos;
                }

                if (RectTransformUtility.RectangleContainsScreenPoint(btn1.rectTransform, 
                rightHandScreenPos, null))
                {
                    //檢測到點在rect里面的時候才會返回true
                    print("手在按鈕一上懸停");
                    KinectInterop.HandState rightHandState = KinectManager.Instance.GetRightHandState(userId);
                    if(rightHandState == KinectInterop.HandState.Closed)
                    {
                        print("右手握拳");
                    }
                }
                else
                {
                    print("手從按鈕一離開");
                }
            }
        }
    }
}

五、RectTransformUtility靜態(tài)方法:

RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform rect ,Vector2 screenPoint,Camera cam,out Vector3 worldPoint);

轉(zhuǎn)換屏幕空間捣辆,指向RectTransform位于矩形平面的局部空間中的位置鹅很。

根據(jù)屏幕坐標與相對矩形rect攝像機和cam而言來計算本地坐標。

RectTransformUtility.ScreenPointToWorldPointInRectangle(RectTransform rect ,Vector2 screenPoint,Camera cam,out Vector3 worldPoint)

轉(zhuǎn)換屏幕空間罪帖,指向世界空間中給定的RectTransform平面上的位置促煮。

根據(jù)屏幕坐標與相對矩形rect和攝像機cam而言來計算出世界坐標邮屁。

4、UGUI動畫效果:

Animator

Animation

DoTween插件

六菠齿、姿勢的檢測與監(jiān)聽

1佑吝、人物進入場景監(jiān)聽

clipboard3.png

2、使用接口進行人物姿勢監(jiān)聽

接口:KinectGestures.GestureListenerInterface
public class GestureTest : MonoBehaviour,KinectGestures.GestureListenerInterface
{
    private Text text;
    void Start()
    {
        text = GetComponent<Text>();
    }
    void Update()
    {

    }
    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        return true;
    }

    public void UserDetected(long userId, int userIndex)
    {
        if(text != null)
        {
            text.text += "  檢測到用戶了  ";
        }
        
    }

    public void UserLost(long userId, int userIndex)
    {
        if (text != null)
        {
            text.text += "  用戶離開攝像頭  ";
        }
    }

    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            text.text += "  用戶做了SwipeRight手勢   ";
        }
        else if (gesture == KinectGestures.Gestures.SwipeLeft)
        {
            text.text += "  用戶做了SwipeLeft手勢   ";
        }

        return true;
    }

    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        
    }
    
}

補充細節(jié):腳本在上

clipboard4.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绳匀,一起剝皮案震驚了整個濱河市芋忿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疾棵,老刑警劉巖戈钢,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異是尔,居然都是意外死亡殉了,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門拟枚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來薪铜,“玉大人,你說我怎么就攤上這事恩溅「艄浚” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵脚乡,是天一觀的道長蜒滩。 經(jīng)常有香客問我,道長奶稠,這世上最難降的妖魔是什么俯艰? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮窒典,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘稽莉。我一直安慰自己瀑志,他們只是感情好,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布污秆。 她就那樣靜靜地躺著劈猪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪良拼。 梳的紋絲不亂的頭發(fā)上战得,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音庸推,去河邊找鬼常侦。 笑死浇冰,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的聋亡。 我是一名探鬼主播肘习,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坡倔!你這毒婦竟也來了漂佩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤罪塔,失蹤者是張志新(化名)和其女友劉穎投蝉,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體征堪,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡瘩缆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了请契。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咳榜。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖爽锥,靈堂內(nèi)的尸體忽然破棺而出涌韩,到底是詐尸還是另有隱情,我是刑警寧澤氯夷,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布苟耻,位于F島的核電站,受9級特大地震影響醉旦,放射性物質(zhì)發(fā)生泄漏曲横。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一踩蔚、第九天 我趴在偏房一處隱蔽的房頂上張望棚放。 院中可真熱鬧,春花似錦馅闽、人聲如沸飘蚯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽局骤。三九已至,卻和暖如春暴凑,著一層夾襖步出監(jiān)牢的瞬間峦甩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工现喳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凯傲,地道東北人犬辰。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像泣洞,于是被迫代替她去往敵國和親忧风。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 這篇文字是Kinect v2 Examples with MS-SDK 這個插件的作者編寫,筆者覺得非常值得一看呕诉,...
    雨落隨風閱讀 16,320評論 0 18
  • Unity編輯器基礎(chǔ) 1.請描述游戲動畫有幾種缘厢,以及其原理。 主要有關(guān)節(jié)動畫甩挫、單一網(wǎng)格模型動畫(關(guān)鍵幀動畫)贴硫、骨骼...
    豆錚閱讀 4,531評論 0 6
  • 一、Unity3D中有哪些坐標系伊者? 坐標系這個概念最早是由法國數(shù)學家笛卡爾提出的英遭,并由此創(chuàng)造了用代數(shù)方法來研究幾何...
    OneMore2018閱讀 4,565評論 0 7
  • 無論是2d還是3d游戲開發(fā),圖形學都是基礎(chǔ)亦渗,解析幾何的基本思想是將幾何圖形抽象成點的運動軌跡挖诸,從而點可以作為組成圖...
    tmgg閱讀 12,809評論 2 4
  • 一直被延誤的黃山賽記,終于在昨天學習的間隙順利完成法精,可一場大雨讓我沒有復制一份在備忘錄多律,以為簡書保存就能完好,結(jié)果...
    songer007閱讀 129評論 0 1