void Update() { var frame = _Reader.AcquireLatestFrame(); if (frame == null) { return; } if (_Data == null) { _Data = new Windows.Kinect.Body[_Sensor.BodyFrameSource.BodyCount]; } frame.GetAndRefreshBodyData(_Data); frame.Dispose(); frame = null; if (_Data == null) { return; } // ここのデータには何が入っているかよくわからない。1人しか映っていなくても、配列の長さは6だった。多分数フレーム分のデータが入っている? foreach (var body in _Data) { if (body == null) { continue; } if (body.IsTracked) { RefreshBodyObject(body); break; // とりあえず、一回でもRefreshBodyObjectが呼ばれたらbreak; } } }
void Update() { if (_Reader != null) { var frame = _Reader.AcquireLatestFrame(); if (frame != null) { if (_Data == null) { _Data = new Windows.Kinect.Body[_Sensor.BodyFrameSource.BodyCount]; } frame.GetAndRefreshBodyData(_Data); frame.Dispose(); frame = null; } } }
void Update() { if (_reader != null) { bool hasBodyData = false; using (var frame = _reader.AcquireLatestFrame()) { if (frame != null) { hasBodyData = true; if (_bodies == null) { _bodies = new Kinect.Body[_sensor.BodyFrameSource.BodyCount]; } frame.GetAndRefreshBodyData(_bodies); UnityEngine.Vector4 floorPlane = new UnityEngine.Vector4(frame.FloorClipPlane.X, frame.FloorClipPlane.Y, frame.FloorClipPlane.Z, frame.FloorClipPlane.W); // update plane Helpers.FloorClipPlane = floorPlane; } } // correct for floorPlane if (hasBodyData) { // get a local copy UnityEngine.Vector4 floorClipPlane = Helpers.FloorClipPlane; // y - up Vector3 up = floorClipPlane; // z - forward Vector3 forward = new Vector3(0.0f, 0.0f, 1.0f); // x - right Vector3 right = Vector3.Cross(up, forward); right.Normalize(); // update matrix Matrix4x4 correctionMatrix = Matrix4x4.identity; correctionMatrix.SetColumn(0, right); correctionMatrix.m00 = right.x; correctionMatrix.m01 = right.y; correctionMatrix.m02 = right.z; correctionMatrix.SetColumn(1, up); correctionMatrix.m10 = up.x; correctionMatrix.m11 = up.y; correctionMatrix.m12 = up.z; correctionMatrix.SetColumn(2, forward); correctionMatrix.m20 = forward.x; correctionMatrix.m21 = forward.y; correctionMatrix.m23 = forward.z; // may need to be transposed correctionMatrix.m13 = floorClipPlane.w; //correctionMatrix.m33 = floorClipPlane.w; } } }
protected virtual void Update() { if (Reader != null) { var frame = Reader.AcquireLatestFrame(); if (frame != null) { if (Data == null) { Data = new Kinect.Body[Sensor.BodyFrameSource.BodyCount]; } frame.GetAndRefreshBodyData(Data); frame.Dispose(); frame = null; } } if (Data == null) { return; //No data yet } List <ulong> trackedIds = new List <ulong>(); foreach (Kinect.Body body in Data) { if (body == null) { continue; } if (body.IsTracked) { trackedIds.Add(body.TrackingId); } } List <ulong> knownIds = new List <ulong>(Avatars.Keys); // First delete untracked bodies foreach (ulong trackingId in knownIds) { if (trackedIds.Contains(trackingId) == false) { Avatars[trackingId].Kill(); Avatars.Remove(trackingId); } } // Then add bodies / update joint positions for bodies that exist foreach (Kinect.Body body in Data) { if (body == null) { continue; } if (body.IsTracked) { if (Avatars.ContainsKey(body.TrackingId) == false) { GameObject newAvatar = GameObject.Instantiate <GameObject>(AvatarPrefab); newAvatar.transform.parent = this.transform; //localize position to the kinect manager newAvatar.transform.localPosition = Vector3.zero; newAvatar.transform.localRotation = Quaternion.identity; KinectAvatar avatar = newAvatar.GetComponent <KinectAvatar>(); avatar.Id = body.TrackingId; Avatars.Add(body.TrackingId, avatar); } Avatars[body.TrackingId].UpdateBodyData(body); } } if (Avatars.Count > 0 && Avatars.ContainsKey(CurrentAvatarID) == false) { CurrentAvatarID = Avatars.OrderBy(avatar => avatar.Value.GetDistanceToKinect()).First().Key; Avatars[CurrentAvatarID].SetActiveAvatar(); } }
void Update() { if (_reader != null) { bool hasBodyData = false; using (var frame = _reader.AcquireLatestFrame()) { if (frame != null) { hasBodyData = true; if (_bodies == null) { _bodies = new Kinect.Body[_sensor.BodyFrameSource.BodyCount]; } frame.GetAndRefreshBodyData(_bodies); UnityEngine.Vector4 floorPlane = new UnityEngine.Vector4(frame.FloorClipPlane.X, frame.FloorClipPlane.Y, frame.FloorClipPlane.Z, frame.FloorClipPlane.W); // update plane Helpers.FloorClipPlane = floorPlane; } } // correct for floorPlane if (hasBodyData) { // get a local copy UnityEngine.Vector4 floorClipPlane = Helpers.FloorClipPlane; // y - up Vector3 up = floorClipPlane; // z - forward Vector3 forward = new Vector3(0.0f, 0.0f, 1.0f); // x - right Vector3 right = Vector3.Cross(up, forward); right.Normalize(); // update matrix Matrix4x4 correctionMatrix = Matrix4x4.identity; correctionMatrix.SetColumn(0, right); correctionMatrix.m00 = right.x; correctionMatrix.m01 = right.y; correctionMatrix.m02 = right.z; correctionMatrix.SetColumn(1, up); correctionMatrix.m10 = up.x; correctionMatrix.m11 = up.y; correctionMatrix.m12 = up.z; correctionMatrix.SetColumn(2, forward); correctionMatrix.m20 = forward.x; correctionMatrix.m21 = forward.y; correctionMatrix.m23 = forward.z; // may need to be transposed correctionMatrix.m13 = floorClipPlane.w; //correctionMatrix.m33 = floorClipPlane.w; } if (Bodies == null || Bodies.Length == 0) { return; } List <ulong> uList = new List <ulong>(); foreach (KeyValuePair <ulong, GameObject> entry in bodies) { uList.Add(entry.Key); } foreach (Kinect.Body b in Bodies) { if (b.IsTracked) { uList.Remove(b.TrackingId); GameObject bObj = null; if (bodies.ContainsKey(b.TrackingId)) { bObj = bodies[b.TrackingId]; KinectVisualizer vis = bObj.GetComponent <KinectVisualizer>(); vis.DrawBoneModel = true; } else { bObj = Instantiate(pref); KinectVisualizer vis = bObj.GetComponent <KinectVisualizer>(); GameObject shirtObj = Instantiate(shirt); shirtObj.transform.parent = bObj.transform; vis.shirt = shirtObj; bObj.name = b.TrackingId.ToString(); bodies.Add(b.TrackingId, bObj); } bObj.SetActive(true); KinectSkeleton skel = bObj.GetComponent <KinectSkeleton>(); skel.UpdateJoints(b); } else { GameObject bod = GameObject.Find(b.TrackingId.ToString()); Destroy(bod); GameObject sket = GameObject.Find(b.TrackingId.ToString() + "_Skeleton"); Destroy(sket); } } foreach (ulong u in uList) { GameObject bObj = bodies[u]; GameObject skelObj = GameObject.Find(bObj.name + "_Skeleton"); Destroy(bObj); Destroy(skelObj); bodies.Remove(u); } } }