Nothing is being added to frame or being displayed
I am writing a program that pulls Kinect joints from a saved file and then loops through each frame, draws the joints, and stops once the end of file has been reached. I am using the official Kinect plugin from Microsoft. However, when I run the program, no errors are being displayed but nothing is added to the render environment. I am very new to Unity, so I have no idea what could be going on.
The data types CustomBody and CustomFrame are custom objects that I made. The CustomFrame has a list of CustomBodies, and each CustomBody has a dictionary of Kinect joints which is what's needed to actually position the bodies once rendered.
Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Kinect = Windows.Kinect;
using System;
using System.IO;
using UnityEditor;
[InitializeOnLoad]
public class RenderFromFile : MonoBehaviour
{
private GameObject gObject;
static RenderFromFile()
{
Debug.Log("Render file has been started");
}
void Start()
{
if (_frameCollection.Count == 0)
{
readFromFile();
}
else
{
Update();
}
}
void Update()
{
if (frameCount < _frameCollection.Count - 1)
{
showAnimation(frameCount);
frameCount++;
}
else
{
var textArea = new Rect(0, 0, Screen.width, Screen.height);
GUI.Label(textArea, "End of file has been reached");
}
}
private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
{
{ Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
{ Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
{ Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
{ Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
{ Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
{ Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
{ Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
{ Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
{ Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
{ Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
{ Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
{ Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
{ Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
{ Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
{ Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
{ Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
{ Kinect.JointType.HandRight, Kinect.JointType.WristRight },
{ Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
{ Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
{ Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
{ Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
{ Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
{ Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
{ Kinect.JointType.Neck, Kinect.JointType.Head },
};
public Material BoneMaterial;
public GameObject BodySourceManager;
private List<GameObject> _allBodies = new List<GameObject>();
private List<CustomFrame> _frameCollection = new List<CustomFrame>();
private string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private int frameCount = 0;
private void readFromFile()
{
String fileName = System.IO.Path.Combine(path, "KinectTest.bin");
using (Stream stream = File.Open(fileName, FileMode.Open))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
_frameCollection = (List<CustomFrame>)bformatter.Deserialize(stream);
}
/*
//For testing purposes
fileName = System.IO.Path.Combine(path, "KinectDuplicateTest.bin");
using (Stream stream = File.Open(fileName, FileMode.Create))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, frameCollection);
}
*/
var textArea = new Rect(0, 0, Screen.width, Screen.height);
GUI.Label(textArea, "File has been read in");
Update();
}
void showAnimation(int frameNum)
{
//need to delete existing beforehand
_allBodies.Clear();
CustomFrame frame = _frameCollection[frameNum];
List<CustomBody> _bodies = new List<CustomBody>();
_bodies = frame.getBodies();
for (int i = 0; i < _bodies.Count; i++) //create and set each body
{
_allBodies.Add(CreateBodyObject(i));
RefreshBodyObject(_bodies[i], _allBodies[i]);
}
}
private GameObject CreateBodyObject(int location)
{
GameObject body = new GameObject("Body:" + location);
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
LineRenderer lr = jointObj.AddComponent<LineRenderer>();
lr.SetVertexCount(2);
lr.material = BoneMaterial;
lr.SetWidth(0.05f, 0.05f);
jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
jointObj.name = jt.ToString();
jointObj.transform.parent = body.transform;
}
return body;
}
private void RefreshBodyObject(CustomBody body, GameObject bodyObject)
{
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
Kinect.Joint sourceJoint = body.joint[jt];
Kinect.Joint? targetJoint = null;
if (_BoneMap.ContainsKey(jt))
{
targetJoint = body.joint[_BoneMap[jt]];
}
Transform jointObj = bodyObject.transform.FindChild(jt.ToString());
jointObj.localPosition = GetVector3FromJoint(sourceJoint);
LineRenderer lr = jointObj.GetComponent<LineRenderer>();
if (targetJoint.HasValue)
{
lr.SetPosition(0, jointObj.localPosition);
lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
lr.SetColors(Color.green, Color.red);
}
else
{
lr.enabled = false;
}
}
}
private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
{
return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
}
}
Your answer
Follow this Question
Related Questions
Unity doesn't show login screen after startup 1 Answer
Unity windows crashes on startup 2 Answers
Unity 3d exe with graphics auto start of the machine / start unity on machine boot up 0 Answers
I want to make the DepthImage of Kinect.V2 Renderer Mesh Skinned. 0 Answers
How to feed kinect v2 inside a sphere as point clouds in real-time 0 Answers