//=============================================== // helper function to map an actor to a skeleton //=============================================== public void ConvertActor(CapturyActor actor, IntPtr actorData, ref CapturySkeleton skel) { if (skel == null) { Debug.Log("Null skeleton reference"); return; } // copy data over skel.name = System.Text.Encoding.UTF8.GetString(actor.name); skel.id = actor.id; skel.rawData = actorData; // create joints int szStruct = Marshal.SizeOf(typeof(CapturyJoint)); skel.joints = new CapturySkeletonJoint[actor.numJoints]; for (uint i = 0; i < actor.numJoints; i++) { // marshall the joints into a new joint struct CapturyJoint joint = new CapturyJoint(); joint = (CapturyJoint)Marshal.PtrToStructure(new IntPtr(actor.joints.ToInt64() + (szStruct * i)), typeof(CapturyJoint)); skel.joints[i] = new CapturySkeletonJoint(); skel.joints[i].name = System.Text.Encoding.ASCII.GetString(joint.name); int jpos = skel.joints[i].name.IndexOf("\0"); skel.joints[i].name = skel.joints[i].name.Substring(0, jpos); skel.joints[i].offset.Set(joint.ox, joint.oy, joint.oz); skel.joints[i].orientation.Set(joint.rx, joint.ry, joint.rz); skel.joints[i].parent = joint.parent; //Debug.Log ("Got joint " + skel.joints[i].name + " at " + joint.ox + joint.oy + joint.oz); } }
private System.IntPtr ConvertGameObjectToCapturyActor(GameObject g) { CapturyActor actor = new CapturyActor(); Transform[] trafos = g.GetComponentsInChildren <Transform>(); //Debug.Log("have " + (trafos.Length - 1) + " children"); actor.name = System.Text.Encoding.ASCII.GetBytes(g.name); actor.id = 17; actor.numJoints = trafos.Length - 1; System.IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CapturyActor)) + actor.numJoints * Marshal.SizeOf(typeof(CapturyJoint))); actor.joints = new System.IntPtr(mem.ToInt64() + Marshal.SizeOf(typeof(CapturyActor))); actor.numBlobs = 0; actor.blobs = System.IntPtr.Zero; Marshal.StructureToPtr(actor, mem, false); CapturyJoint[] joints = new CapturyJoint[trafos.Length - 1]; Vector3[] globalPositions = new Vector3[trafos.Length - 1]; Vector3[] globalPositionsUnity = new Vector3[trafos.Length - 1]; Vector3[] trafoPos = new Vector3[trafos.Length]; Dictionary <string, int> names = new Dictionary <string, int>(); names[trafos[0].name] = -1; // this is the overall parent System.IntPtr j = actor.joints; for (int i = 0; i < actor.numJoints; ++i) { names[trafos[i + 1].name] = i; joints[i].parent = names[trafos[i + 1].parent.name]; joints[i].name = System.Text.Encoding.ASCII.GetBytes(trafos[i + 1].name); Vector3 pos; int parent = joints[i].parent; if (parent != -1) { pos = networkPlugin.ConvertPositionToLive(trafos[i + 1].position - trafos[i + 1].parent.position); } else { pos = networkPlugin.ConvertPositionToLive(trafos[i + 1].position); } joints[i].ox = pos.x; joints[i].oy = pos.y; joints[i].oz = pos.z; Quaternion delta = trafos[i + 1].rotation; Quaternion liveDelta = networkPlugin.ConvertRotationToLive(delta); Vector3 rot = networkPlugin.ConvertToEulerAngles(liveDelta); joints[i].rx = 0.0f; // rot.x; joints[i].ry = 0.0f; // rot.y; joints[i].rz = 0.0f; // rot.z; trafoPos[i] = trafos[i + 1].position; globalPositions[i] = new Vector3(joints[i].ox, joints[i].oy, joints[i].oz); int p = joints[i].parent; if (p > -1) { globalPositions[i] += globalPositions[p]; } globalPositionsUnity[i] = networkPlugin.ConvertPosition(globalPositions[i]) - trafoPos[i]; Marshal.StructureToPtr(joints[i], j, false); j = new System.IntPtr(j.ToInt64() + Marshal.SizeOf(typeof(CapturyJoint))); } return(mem); }