private static void addJointsToRig(ColladaRigData rig, XmlNode xmlNode, ColladaJointData parentJoint) { // Find the next child joint node. foreach (XmlNode node in xmlNode.ChildNodes) { bool isJoint = false; string id = string.Empty; // Look at the attributes to check if this node is a joint foreach (XmlAttribute attribute in node.Attributes) { if (attribute.Name == id_label) { id = attribute.Value; } if (attribute.Name == type && attribute.Value == JOINT) { isJoint = true; } } if (isJoint) // found a child joint. Create the data, get the parent, add it to the rig { ColladaJointData jointData = getColladaJointFromNode(node, id); Quaternion lhsRotation = QuaternionHelper.RHStoLHS(jointData.RotationVector); jointData.LHSTransformationMatrix = Matrix4x4.TRS(new Vector3(-jointData.Translation.x, jointData.Translation.y, jointData.Translation.z), lhsRotation, Vector3.one); // Get parent joint if (parentJoint != null) { jointData.LHSWorldTransformationMatrix = parentJoint.LHSWorldTransformationMatrix * jointData.LHSTransformationMatrix; rig.Add(id, parentJoint.Id, jointData); } else { rig.Add(id, jointData); } // look for children joints. addJointsToRig(rig, node, jointData); } } }
/// <summary> /// Get the structure for what is expected in this output method. /// </summary> /// <returns></returns> public override NUISkeleton GetTargetStructure() { // Define the skeletal structure SkeletonStructure structure = new SkeletonStructure(); structure.SetRootJoint(NUIJointType.SpineBase); structure.AddBone(ColladaToNUIJointMapping("SpineMid"), ColladaToNUIJointMapping("SpineBase")); structure.AddBone(ColladaToNUIJointMapping("SpineShoulder"), ColladaToNUIJointMapping("SpineMid")); structure.AddBone(ColladaToNUIJointMapping("Neck"), ColladaToNUIJointMapping("SpineShoulder")); structure.AddBone(ColladaToNUIJointMapping("Head"), ColladaToNUIJointMapping("Neck")); structure.AddBone(ColladaToNUIJointMapping("ShoulderLeft"), ColladaToNUIJointMapping("SpineShoulder")); structure.AddBone(ColladaToNUIJointMapping("ElbowLeft"), ColladaToNUIJointMapping("ShoulderLeft")); structure.AddBone(ColladaToNUIJointMapping("WristLeft"), ColladaToNUIJointMapping("ElbowLeft")); structure.AddBone(ColladaToNUIJointMapping("HandLeft"), ColladaToNUIJointMapping("WristLeft")); structure.AddBone(ColladaToNUIJointMapping("HandTipLeft"), ColladaToNUIJointMapping("HandLeft")); structure.AddBone(ColladaToNUIJointMapping("ThumbLeft"), ColladaToNUIJointMapping("HandLeft")); structure.AddBone(ColladaToNUIJointMapping("ShoulderRight"), ColladaToNUIJointMapping("SpineShoulder")); structure.AddBone(ColladaToNUIJointMapping("ElbowRight"), ColladaToNUIJointMapping("ShoulderRight")); structure.AddBone(ColladaToNUIJointMapping("WristRight"), ColladaToNUIJointMapping("ElbowRight")); structure.AddBone(ColladaToNUIJointMapping("HandRight"), ColladaToNUIJointMapping("WristRight")); structure.AddBone(ColladaToNUIJointMapping("HandTipRight"), ColladaToNUIJointMapping("HandRight")); structure.AddBone(ColladaToNUIJointMapping("ThumbRight"), ColladaToNUIJointMapping("HandRight")); structure.AddBone(ColladaToNUIJointMapping("HipLeft"), ColladaToNUIJointMapping("SpineBase")); structure.AddBone(ColladaToNUIJointMapping("KneeLeft"), ColladaToNUIJointMapping("HipLeft")); structure.AddBone(ColladaToNUIJointMapping("AnkleLeft"), ColladaToNUIJointMapping("KneeLeft")); structure.AddBone(ColladaToNUIJointMapping("FootLeft"), ColladaToNUIJointMapping("AnkleLeft")); structure.AddBone(ColladaToNUIJointMapping("HipRight"), ColladaToNUIJointMapping("SpineBase")); structure.AddBone(ColladaToNUIJointMapping("KneeRight"), ColladaToNUIJointMapping("HipRight")); structure.AddBone(ColladaToNUIJointMapping("AnkleRight"), ColladaToNUIJointMapping("KneeRight")); structure.AddBone(ColladaToNUIJointMapping("FootRight"), ColladaToNUIJointMapping("AnkleRight")); // Define the skeleton in Unity terms. NUISkeleton skeleton = new NUISkeleton(structure); // Get the hip right and the chest right. Vector3 hipRightTranslation = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3); Vector3 hipLeftTranslation = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3); Vector3 shoulderRightTranslation = rigData.GetJoint("ShoulderRight").LHSWorldTransformationMatrix.GetColumn(3); Vector3 shoulderLeftTranslation = rigData.GetJoint("ShoulderLeft").LHSWorldTransformationMatrix.GetColumn(3); Vector3 hipRight = hipRightTranslation - hipLeftTranslation; Vector3 chestRight = shoulderRightTranslation - shoulderLeftTranslation; skeleton.SpineBaseRight = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.inverse.MultiplyVector(hipRight); skeleton.ChestRight = rigData.GetJoint("SpineMid").LHSWorldTransformationMatrix.inverse.MultiplyVector(chestRight); foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData) { NUIJointType jointType = ColladaToNUIJointMapping(jointData.Key); NUIJoint joint = new NUIJoint(jointType); // Convert the Collada Joint rotation from RHS to LHS (Unity) ColladaJointData currentJoint = jointData.Value; joint.Position = currentJoint.Translation; joint.Rotation = QuaternionHelper.RHStoLHS(currentJoint.RotationVector); joint.TransformationMatrix = currentJoint.LHSTransformationMatrix; joint.WorldTransformationMatrix = currentJoint.LHSWorldTransformationMatrix; Vector3 directionToChild = Vector3.zero; if (!structure.IsJointAnExtremity(jointType)) // directionToChild is not needed for extremeties. { NUIJointType childType = structure.GetChildJoint(jointType); if (childType != NUIJointType.Unspecified) { Vector3 child = rigData.GetJoint(NUIJointToColladaMapping(childType)).LHSWorldTransformationMatrix.GetColumn(3); Vector3 parent = currentJoint.LHSWorldTransformationMatrix.GetColumn(3); directionToChild = child - parent; } } if (jointType == NUIJointType.SpineBase) // The Hip is a special case. { Vector3 rightHipWorldPosition = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3); Vector3 leftHipWorldPosition = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3); Vector3 hipWorldPosition = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.GetColumn(3); directionToChild = ((rightHipWorldPosition + leftHipWorldPosition) / 2F) - hipWorldPosition; //float angle = Vector3.Angle(Vector3.up, directionToChild); //directionToChild = Quaternion.AngleAxis(40 - angle, hipRight) * directionToChild; } joint.directionToChild = currentJoint.LHSWorldTransformationMatrix.inverse.MultiplyVector(directionToChild); skeleton.Joints.Add(jointType, joint); } skeleton.ChestRight -= Vector3.Project(skeleton.ChestRight, skeleton.Joints[NUIJointType.SpineMid].directionToChild); return(skeleton); }