Kinect Openni在Unity3D中获取人的身高的脚本。
using UnityEngine; using System.Collections; using System; using System.Collections.Generic; public class GetHeight : MonoBehaviour { public static double height=0.0; //人的身高 public float people_kinectSpleed=2.4f; //人到kinect的距离,(kinect自身有效距离大概在1.2m-3.5m之间【XBOX】) public bool isDynamicGetHeight=false; //是否动态获取身高 void Zig_Update(ZigInput zig) { foreach (ZigTrackedUser trackedUser in zig.TrackedUsers.Values) { if(isDynamicGetHeight){ // if(trackedUser.SkeletonTracked){ height=Height(trackedUser.Skeleton); } }else{ if(Math.Round(trackedUser.Position.z / 1000,1)<-people_kinectSpleed){ if (trackedUser.SkeletonTracked && height==0.0) { height=Height(trackedUser.Skeleton); //得到当前使用用户所有的节点 } } } } } public double Height(ZigInputJoint[] skeleton) { const double HEAD_DIVERGENCE = 0.1; ZigInputJoint head = skeleton[(int)ZigJointId.Head]; ZigInputJoint neck = skeleton[(int)ZigJointId.Neck]; ZigInputJoint spine = skeleton[(int)ZigJointId.Torso]; ZigInputJoint waist = skeleton[(int)ZigJointId.Waist]; ZigInputJoint hipLeft = skeleton[(int)ZigJointId.LeftHip]; ZigInputJoint hipRight = skeleton[(int)ZigJointId.RightHip]; ZigInputJoint kneeLeft = skeleton[(int)ZigJointId.LeftKnee]; ZigInputJoint kneeRight = skeleton[(int)ZigJointId.RightKnee]; ZigInputJoint ankleLeft = skeleton[(int)ZigJointId.LeftAnkle]; ZigInputJoint ankleRight = skeleton[(int)ZigJointId.RightAnkle]; ZigInputJoint footLeft = skeleton[(int)ZigJointId.LeftFoot]; ZigInputJoint footRight = skeleton[(int)ZigJointId.RightFoot]; // Find which leg is tracked more accurately. int legLeftTrackedJoints = NumberOfTrackedJoints(hipLeft, kneeLeft, ankleLeft, footLeft); int legRightTrackedJoints = NumberOfTrackedJoints(hipRight, kneeRight, ankleRight, footRight); double legLength = legLeftTrackedJoints > legRightTrackedJoints ? Length(hipLeft, kneeLeft, ankleLeft, footLeft) : Length(hipRight, kneeRight, ankleRight, footRight); return Math.Round(Length(head, neck, spine, waist) + legLength + HEAD_DIVERGENCE,2); } public double Length(ZigInputJoint p1, ZigInputJoint p2) { return Mathf.Sqrt(Mathf.Pow((p1.Position.x - p2.Position.x) / 1000, 2) +Mathf.Pow((p1.Position.y - p2.Position.y) / 1000, 2) + Mathf.Pow((p1.Position.z - p2.Position.z) / 1000, 2)); } public double Length(params ZigInputJoint[] joints) { double length = 0; for (int index = 0; index < joints.Length - 1; index++) { length += Length(joints[index], joints[index + 1]); } return length; } public int NumberOfTrackedJoints(params ZigInputJoint[] joints) { int trackedJoints = 0; foreach (ZigInputJoint joint in joints) { trackedJoints++; } return trackedJoints; } }