Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
KinectReceiver
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnetLLAPISample;
using UnityEngine;
using Kinect = Windows.Kinect;
[Serializable]
public struct SimpleBody
{
public List<SimpleJoint> Joints;
}
[Serializable]
public struct SimpleJoint
{
public Vector3 Position;
public int TrackingState;
}
public class Body
{
public Dictionary<Kinect.JointType, Joint> Joints;
}
public class Joint
{
public Vector3 Position;
public Kinect.TrackingState TrackingState;
}
public class KinectReceiver : MonoBehaviour
{
public LLAPINetworkManager NetworkManager;
public GameObject JointPrefab;
private Body localBody;
private GameObject bodyObject;
private void Awake()
{
localBody = CreateBody();
bodyObject = CreateBodyObject();
}
void Start()
{
NetworkManager.OnDataReceived += OnDataReceived;
}
Body CreateBody()
{
var body = new Body();
var joints = new Dictionary<Kinect.JointType, Joint>();
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
var joint = new Joint();
joint.Position = Vector3.zero;
joint.TrackingState = Kinect.TrackingState.NotTracked;
joints.Add(jt, joint);
}
body.Joints = joints;
return body;
}
void OnDataReceived(object o, LLAPINetworkEventArgs e)
{
SimpleBody simpleBody = new SimpleBody();
string output;
var data = e.data;
using (var inStream = new MemoryStream(data))
using (var bigStream = new System.IO.Compression.GZipStream(inStream, System.IO.Compression.CompressionMode.Decompress))
using (var reader = new StreamReader(bigStream))
{
output = reader.ReadToEnd();
}
simpleBody = JsonUtility.FromJson<SimpleBody>(output);
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
var j = localBody.Joints[jt];
var sj = simpleBody.Joints[(int)jt];
j.Position = sj.Position;
j.TrackingState = (Kinect.TrackingState)sj.TrackingState;
}
DisplayBodyObject();
}
GameObject CreateBodyObject()
{
GameObject bodyObj = new GameObject("Body");
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
GameObject jointObj = Instantiate(JointPrefab);
jointObj.name = jt.ToString();
jointObj.transform.parent = bodyObj.transform;
}
return bodyObj;
}
void DisplayBodyObject()
{
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
Joint sourceJoint = localBody.Joints[jt];
Transform jointObj = bodyObject.transform.FindChild(jt.ToString());
jointObj.localPosition = sourceJoint.Position;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment