Created
April 2, 2017 12:45
-
-
Save tarukosu/61baa8ed82a59a4496fa1f5a82aecd5d to your computer and use it in GitHub Desktop.
KinectReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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