public static HIE Load(string path) { void walkHierarchy(TDRNode parent, int index, HIE hierarchy) { TDRNode node = hierarchy.Nodes[index]; switch (node.Type) { case TDRNode.NodeType.Matrix: node.Transform = hierarchy.Matrixes[node.Index].Matrix; break; } if (node.Parent != null) { parent.Children.Remove(node); } node.Parent = parent; parent.Children.Add(node); if (node.Child > -1) { walkHierarchy(node, node.Child, hierarchy); } if (node.Sibling > -1) { walkHierarchy(parent, node.Sibling, hierarchy); } } FileInfo fi = new FileInfo(path); Logger.LogToFile(Logger.LogLevel.Info, "{0}", path); HIE hie = new HIE(); H h = new H(); FileInfo[] f = fi.Directory.GetFiles($"{Path.GetFileNameWithoutExtension(path)}.h"); if (f.Length == 0) { f = fi.Directory.Parent.GetFiles($"{Path.GetFileNameWithoutExtension(path)}.h"); } if (f.Length > 0) { h = H.Load(f[0].FullName); } string[] lines; using (StreamReader sr = new StreamReader(fi.OpenRead())) { lines = sr.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); } lines = lines.Select(l => l.Trim()).ToArray(); for (int i = 0; i < lines.Length; i++) { switch (lines[i].ToLower()) { case "//version number": hie.Version = int.Parse(lines[i + 2]); i += 2; break; case "// number of cull nodes": hie.CullNodeCount = int.Parse(lines[++i]); break; case "// cull node list": for (int j = 0; j < hie.CullNodeCount; j++) { string cullNode = lines[++i]; } break; case "// number of collision data meshes": hie.CollisionDataMeshCount = int.Parse(lines[++i]); break; case "// collision data list": for (int j = 0; j < hie.CollisionDataMeshCount; j++) { hie.CollisionDataMeshes.Add(lines[++i]); } break; case "// number of lines": hie.LineCount = int.Parse(lines[++i]); break; case "// line name list": while (!lines[++i].StartsWith("//")) { hie.LineNames.Add(lines[i].Trim().Replace("\"", "")); } i--; break; case "// number of textures": hie.TextureCount = int.Parse(lines[++i]); break; case "// texture name list": for (int j = 0; j < hie.TextureCount; j++) { hie.Textures.Add(lines[++i].Replace("\"", "").Trim()); } break; case "// number of materials": hie.MaterialCount = int.Parse(lines[++i]); break; case "// material name list": for (int j = 0; j < hie.MaterialCount; j++) { hie.Materials.Add(new TDRMaterial(lines[++i])); } break; case "// number of matrices": hie.MatrixCount = int.Parse(lines[++i]); break; case "// matrix name list": for (int j = 0; j < hie.MatrixCount; j++) { string[] parts; TDRMatrix matrix = new TDRMatrix(); parts = lines[++i].Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); matrix.Matrix.M11 = Convert.ToSingle(parts[0], Culture); matrix.Matrix.M12 = Convert.ToSingle(parts[1], Culture); matrix.Matrix.M13 = Convert.ToSingle(parts[2], Culture); parts = lines[++i].Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); matrix.Matrix.M21 = Convert.ToSingle(parts[0], Culture); matrix.Matrix.M22 = Convert.ToSingle(parts[1], Culture); matrix.Matrix.M23 = Convert.ToSingle(parts[2], Culture); parts = lines[++i].Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); matrix.Matrix.M31 = Convert.ToSingle(parts[0], Culture); matrix.Matrix.M32 = Convert.ToSingle(parts[1], Culture); matrix.Matrix.M33 = Convert.ToSingle(parts[2], Culture); parts = lines[++i].Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); matrix.Matrix.M41 = Convert.ToSingle(parts[0], Culture); matrix.Matrix.M42 = Convert.ToSingle(parts[1], Culture); matrix.Matrix.M43 = Convert.ToSingle(parts[2], Culture); matrix.Name = lines[++i].Trim().Replace("\"", ""); hie.Matrixes.Add(matrix); } break; case "// number of meshes": hie.MeshCount = int.Parse(lines[++i]); break; case "// mesh name list": while (!lines[++i].StartsWith("//")) { hie.Meshes.Add(lines[i].Trim().Replace("\"", "")); } i--; break; case "// number of expressions": hie.ExpressionCount = int.Parse(lines[++i]); break; case "// expression list :": if (lines[i + 1].ToLower() == "// high_val low_val") { i++; } for (int j = 0; j < hie.ExpressionCount; j++) { string[] p = lines[++i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); hie.Expressions.Add(new Vector2(p[0].ToSingle(), p[1].ToSingle())); } break; case "// number of nodes": hie.NodeCount = int.Parse(lines[++i]); break; case "// node list :": if (lines[i + 1].ToLower() == "// type index child sibling") { i++; } for (int j = 0; j < hie.NodeCount; j++) { hie.Nodes.Add(new TDRNode((h.Definitions.ContainsKey(j) ? h.Definitions[j] : "DEFAULT"), j, lines[++i])); } break; default: Console.WriteLine(i + "] " + lines[i]); break; } } hie.Root.Name = h.Definitions.ContainsKey(0) ? h.Definitions[0] : "DEFAULT"; hie.Root.Transform = hie.Matrixes[hie.Root.Index].Matrix; walkHierarchy(hie.Root, 1, hie); return(hie); }
private static void walkHierarchy(TDRNode parent, int index, HIE hie) { TDRNode node = hie.nodes[index]; //Console.WriteLine("{0} > {1} : {2}", parent.ID, node.ID, node.Type); switch (node.Type) { case TDRNode.NodeType.Matrix: node.Transform = hie.matrixes[node.Index].Matrix; break; } if (node.Parent != null) { parent.Children.Remove(node); } node.Parent = parent; parent.Children.Add(node); if (node.Child > -1) { walkHierarchy(node, node.Child, hie); } if (node.Sibling > -1) { walkHierarchy(parent, node.Sibling, hie); } }
static void ProcessNode(TDRNode node, Model model, Model mshses, HIE hie, int ParentBoneIndex = 0) { int boneIndex = ParentBoneIndex; if (exit) { return; } switch (node.Type) { case TDRNode.NodeType.Matrix: boneIndex = model.AddMesh(null, boneIndex); model.SetName(node.Name, boneIndex); model.SetTransform( new Matrix4 ( node.Transform.M11, node.Transform.M12, node.Transform.M13, 0, node.Transform.M21, node.Transform.M22, node.Transform.M23, 0, node.Transform.M31, node.Transform.M32, node.Transform.M33, 0, node.Transform.M41, node.Transform.M42, node.Transform.M43, 1 ), boneIndex); break; case TDRNode.NodeType.Mesh: int index = node.Index; mshses.Meshes[index].MeshParts[0].Material = material; if (model.Bones[ParentBoneIndex].Mesh == null) { model.SetMesh(mshses.Meshes[index], boneIndex); //exit = true; //Console.WriteLine("Adding mesh #{0} \"{1}\" to bone #{2} \"{3}\"", index, mshses.Meshes[index].Name, boneIndex, model.Bones[boneIndex].Name); } else { boneIndex = model.AddMesh(mshses.Meshes[index], ParentBoneIndex); model.SetName(mshses.Meshes[index].Name, boneIndex); //model.SetName(mshses.Meshes[index].Name, model.AddMesh(mshses.Meshes[index], ParentBoneIndex)); //Console.WriteLine("Adding mesh #{0} \"{1}\" to brand new bone", index, mshses.Meshes[index].Name); } break; case TDRNode.NodeType.Texture: if (node.Index > -1) { material = SceneManager.Current.Content.Load<Material, TXImporter>(hie.Textures[node.Index]); } else { material = null; } break; } foreach (var child in node.Children) { ProcessNode(child, model, mshses, hie, boneIndex); } }