Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
BodySender
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 BodySender : MonoBehaviour {
public LLAPINetworkManager NetworkManager;
public GameObject BodySourceManager;
private BodySourceManager _BodyManager;
void Update()
{
if (BodySourceManager == null)
{
return;
}
_BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
if (_BodyManager == null)
{
return;
}
Kinect.Body[] data = _BodyManager.GetData();
if (data == null)
{
return;
}
SimpleBody simpleBody = new SimpleBody();
foreach (var body in data) {
simpleBody = GenerateSimpleBody(body);
if (body.IsTracked)
{
break;
}
}
byte[] sendData;
string serialisedMsg = JsonUtility.ToJson(simpleBody);
using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new Unity.IO.Compression.GZipStream(memoryStream, Unity.IO.Compression.CompressionMode.Compress))
using (var writer = new StreamWriter(gzipStream))
{
writer.Write(serialisedMsg);
}
sendData = memoryStream.ToArray();
}
NetworkManager.SendPacketData(sendData, UnityEngine.Networking.QosType.Unreliable);
}
SimpleBody GenerateSimpleBody(Kinect.Body body)
{
SimpleBody simpleBody;
var joints = new List<SimpleJoint>();
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
Kinect.Joint joint = body.Joints[jt];
SimpleJoint sendJoint;
sendJoint.Position = GetVector3FromJoint(joint);
sendJoint.TrackingState = (int)joint.TrackingState;
joints.Add(sendJoint);
}
simpleBody.Joints = joints;
return simpleBody;
}
private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
{
return new Vector3(joint.Position.X, joint.Position.Y, -joint.Position.Z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment