Skip to content

Instantly share code, notes, and snippets.

@tarukosu
Last active April 2, 2017 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarukosu/cff2f4167a5d5cf7f3b4463888c9502e to your computer and use it in GitHub Desktop.
Save tarukosu/cff2f4167a5d5cf7f3b4463888c9502e to your computer and use it in GitHub Desktop.
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