using UnityEngine; using System.Collections; using System.Collections.Generic; using Kinect = Windows.Kinect; public class BodySourceView : MonoBehaviour { public static Transform[] jointObjs = new Transform[25]; public static bool bodyTracked = false; public static bool kinectActive = false; public GameObject BodySourceManager; private Dictionary _Bodies = new Dictionary(); private BodySourceManager _BodyManager; private Dictionary _BoneMap = new Dictionary() { { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft }, { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft }, { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft }, { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase }, { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight }, { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight }, { Kinect.JointType.KneeRight, Kinect.JointType.HipRight }, { Kinect.JointType.HipRight, Kinect.JointType.SpineBase }, { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft }, { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft }, { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft }, { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft }, { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft }, { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder }, { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight }, { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight }, { Kinect.JointType.HandRight, Kinect.JointType.WristRight }, { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight }, { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight }, { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder }, { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid }, { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder }, { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck }, { Kinect.JointType.Neck, Kinect.JointType.Head }, }; void Update () { if (BodySourceManager == null) { return; } _BodyManager = BodySourceManager.GetComponent(); if (_BodyManager == null) { return; } Kinect.Body[] data = _BodyManager.GetData(); if (data == null) { return; } List trackedIds = new List(); foreach(var body in data) { if (body == null) { continue; } if(body.IsTracked) { bodyTracked = true; trackedIds.Add (body.TrackingId); } } List knownIds = new List(_Bodies.Keys); // First delete untracked bodies foreach(ulong trackingId in knownIds) { if(!trackedIds.Contains(trackingId)) { Destroy(_Bodies[trackingId]); _Bodies.Remove(trackingId); bodyTracked = false; } } foreach (var body in data) { if (body == null) { continue; } if (body.IsTracked) { if (!_Bodies.ContainsKey(body.TrackingId)) { _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId); } RefreshBodyObject(body, _Bodies[body.TrackingId]); } } } private GameObject CreateBodyObject(ulong id) { GameObject body = new GameObject("Body:" + id); for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++) { GameObject jointObj = new GameObject(); jointObj.name = jt.ToString(); jointObj.transform.parent = body.transform; } return body; } private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject) { for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++) { Kinect.Joint sourceJoint = body.Joints[jt]; Kinect.Joint? targetJoint = null; if(_BoneMap.ContainsKey(jt)) { targetJoint = body.Joints[_BoneMap[jt]]; } // JW - capture active joint positions jointObjs[(int)jt] = bodyObject.transform.FindChild(jt.ToString()); jointObjs[(int)jt].localPosition = GetVector3FromJoint(sourceJoint); } } private static Vector3 GetVector3FromJoint(Kinect.Joint joint) { return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10); } }