【Kinect】Kinect2函数使用

【Kinect】Kinect2函数使用
开发部分实现方法:
===============================================================================
获取人物关节点的注意事项:
//1.先检查人物是否被检测
if (_manager.IsUserDetected())
//2.获取人物ID
long userId = _manager.GetPrimaryUserID();
//3.关节索引
int jointIndex = (int)KinectInterop.JointType.HandLeft;
//4.判断需要跟踪的关节点是否已经被识别
if (_manager.IsJointTracked(userId, jointIndex))
//5.获取骨骼节点信息
Vector3 leftHandPos = _manager.GetJointKinectPosition(userId, jointIndex);

===============================================================================
获取手势信息 
 //获取右手状态
KinectInterop.HandState rightHandState = _manager.GetRightHandState(userId);
//判断右手状态值
if (rightHandState == KinectInterop.HandState.Closed)
{
debugText.text = "右手握拳";
}
else if (rightHandState == KinectInterop.HandState.Open)
{
debugText.text = "右手展开";
}
else if (rightHandState == KinectInterop.HandState.Lasso)
{
debugText.text = "yes 手势";
}
===============================================================================
Kinect坐标到屏幕坐标的转换
//1.获取骨骼节点坐标
Vector3 jointKinectPos = _manager.GetJointKinectPosition(userId, jointIndex);
//2.空间坐标转换成深度坐标
Vector2 posDepth = _manager.MapSpacePointToDepthCoords(jointKinectPos);
//3.根据深度坐标获取深度值
ushort depthValue = _manager.GetDepthForPixel((int)posDepth.x, (int)posDepth.y);
if (depthValue > 0)//深度值大于0
{
//4.深度图像转换成彩色图像
Vector2 posColor = _manager.MapDepthPointToColorCoords(posDepth, depthValue);
//5.按彩色图像转换坐标
float xNorm = (float)posColor.x / _manager.GetColorImageWidth();
float yNorm = (float)posColor.y / _manager.GetColorImageHeight();

if (overlayObject)
{
//将视口坐标转换为空间坐标
    Vector3 vpos = Camera.main.ViewportToWorldPoint(xNorm, yNorm, distanceToCamera);
    overlayObject.transform.position = Vector3.Lerp(overlayObject.transform.position, vpos, Time.deltaTime * 5);
}
}
===============================================================================
//UI点击的实现
1.获取右手(左手/关节点)的3D坐标
Vector3 rightHandPos = _manager.GetJointKinectPosition(userId, jointIndex);//右手坐标
2.3D坐标转换成屏幕坐标
Vector3 rightScreenPos = Camera.main.WorldToScreenPoint(rightHandPos);//转换成屏幕坐标
Vector2 screenPosTemp = new Vector2(rightScreenPos.x, rightScreenPos.y);//屏幕坐标
3.屏幕坐标转换成UGUI坐标(得到手的位置在UGUI的位置)
 Vector2 uguiPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPosTemp, null, out uguiPos))
{
hand.anchoredPosition = uguiPos;
}
4.判断手的位置是否在UGUI控件上方
bool isOverBtn1 = RectTransformUtility.RectangleContainsScreenPoint(btn1, screenPosTemp, null);
5.手如果在控件上方,判断是否握拳
 //获取右手状态
KinectInterop.HandState rightHandState = _manager.GetRightHandState(userId);
//判断右手状态值
if (rightHandState == KinectInterop.HandState.Closed)
{
if (isOverBtn1)
print("握住btn1");
}
else if (rightHandState == KinectInterop.HandState.Open){}
else if (rightHandState == KinectInterop.HandState.Lasso){}