一桶唐、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):
- 獲取右手(左手/關(guān)節(jié))的3D坐標
- 3D坐標轉(zhuǎn)換為屏幕坐標
- 屏幕坐標轉(zhuǎn)換為UGUI坐標(得到手在UGUI的位置)
- 判斷手的位置是否在UGUI控件上
- 手如果在UGUI控件上嫡意,判斷是否握拳
- 握拳就表示點擊了該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