/// <summary> /// Reads bone information from the file. /// </summary> protected void ReadBone(XmlNode node) { // bone name string name = node.Attributes["name"].Value; ushort handle = ushort.Parse(node.Attributes["id"].Value); // create a new bone Bone bone = skeleton.CreateBone(name, handle); foreach (XmlNode childNode in node.ChildNodes) { switch (childNode.Name) { case "position": ReadPosition(childNode, bone); break; case "rotation": ReadRotation(childNode, bone); break; default: DebugMessage(childNode); break; } } }
protected void TransformSkeleton(Matrix4 exportTransform) { Matrix4 invExportTransform = exportTransform.Inverse(); Dictionary <string, Matrix4> fullInverseBoneTransforms = new Dictionary <string, Matrix4>(); Skeleton newSkeleton = new Skeleton(skeleton.Name); // Construct new versions of the bones, and build // the inverse bind matrix that will be needed. for (ushort i = 0; i < skeleton.BoneCount; ++i) { Bone bone = skeleton.GetBone(i); Bone newBone = newSkeleton.CreateBone(bone.Name, bone.Handle); fullInverseBoneTransforms[bone.Name] = bone.BindDerivedInverseTransform; } // Build the parenting relationship for the new skeleton for (ushort i = 0; i < skeleton.BoneCount; ++i) { Bone bone = skeleton.GetBone(i); Bone newBone = newSkeleton.GetBone(i); Bone parentBone = (Bone)bone.Parent; if (parentBone != null) { Bone newParentBone = newSkeleton.GetBone(parentBone.Handle); newParentBone.AddChild(newBone); } } // Set the orientation and position for the various bones // B' = T * B * Tinv for (ushort i = 0; i < newSkeleton.BoneCount; ++i) { Bone bone = skeleton.GetBone(i); string boneName = bone.Name; string parentName = (bone.Parent == null) ? null : bone.Parent.Name; Matrix4 transform = GetLocalBindMatrix(fullInverseBoneTransforms, boneName, parentName, true); transform = exportTransform * transform * invExportTransform; Quaternion orientation = GetRotation(transform); Bone newBone = newSkeleton.GetBone(i); newBone.Orientation = orientation; newBone.Position = transform.Translation; //if (newBone.Name == "Lower_Torso_BIND_jjj") { // log.DebugFormat("New Bone Position: {0}", transform.Translation); //} } newSkeleton.SetBindingPose(); for (int i = 0; i < skeleton.AnimationCount; ++i) { Animation anim = skeleton.GetAnimation(i); Animation newAnim = newSkeleton.CreateAnimation(anim.Name, anim.Length); TransformAnimation(exportTransform, newAnim, anim, newSkeleton); } skeleton = newSkeleton; }
/// <summary> /// Reads bone information from the file. /// </summary> protected void ReadBone(BinaryMemoryReader reader) { // bone name string name = ReadString(reader); ushort handle = ReadUShort(reader); // create a new bone Bone bone = skeleton.CreateBone(name, handle); // read and set the position of the bone Vector3 position = ReadVector3(reader); bone.Position = position; // read and set the orientation of the bone Quaternion q = ReadQuat(reader); bone.Orientation = q; }