void PrepareBoneTransforms(Node node, ref Matrix4 parentTransform) { Matrix4 g; Matrix4.Mult(ref node.transform, ref parentTransform, out g); if (_boneSet.Contains(node)) Matrix4.Mult(ref node.Bone._offset, ref g, out _boneTransforms[node.Bone.Index]); if (node.Children != null) { foreach (var child in node.Children) this.PrepareBoneTransforms(child, ref g); } }
internal Node (string name, Node parent, ref Matrix4 transform, Mesh[] meshes) { _name = name; _parent = parent; this.transform = transform; _meshes = meshes; }
void InitModel (BinaryReader reader, Dictionary<string, Texture> textures) { var materials = this.ReadMaterials(reader, textures); var meshes = this.ReadMeshes(reader, materials); _meshes = meshes.ToDictionary<Mesh,string>(m => m.Name); _anims = this.ReadAnimations(reader); _root = this.ReadNode(reader, null, meshes); this.Link(_root); foreach (var anim in _anims.Values) { this.Link(anim); } }
void Link(Node node) { if (node.Meshes != null) { foreach (var mesh in node.Meshes) { mesh.Node = node; if (mesh.Bones != null) { foreach (var bone in mesh.Bones) { bone.Node = (node.Parent ?? node).FindByName(bone.Name); if (bone.Node == null) throw new ContentException("Could not find bone node " + bone.Name); bone.Node.Bone = bone; } } } } if (node.Children != null) { foreach (var child in node.Children) this.Link(child); } }
Node ReadNode(BinaryReader reader, Node parent, Mesh[] meshes) { var name = reader.ReadString(); Matrix4 transform; reader.ReadMatrix4(out transform); var meshCount = reader.ReadInt32(); Mesh[] meshList = null; if(meshCount > 0) { meshList = new Mesh[meshCount]; for (var i = 0; i < meshCount; i++) { var idx = reader.ReadInt32(); if (idx < 0 || idx >= meshes.Length) throw new ContentException("Invalid mesh index " + idx); meshList[i] = meshes[idx]; } } var node = new Node(name, parent, ref transform, meshList); var childCount = reader.ReadInt32(); Node[] children = null; if(childCount > 0) { children = new Node[childCount]; for (var i = 0; i < childCount; i++) { children[i] = this.ReadNode(reader, node, meshes); } } node.Children = children; return node; }