public int Load(Stream source_stream) { try { reader = new BinaryReader(source_stream, System.Text.Encoding.Default); } catch (Exception) { Console.WriteLine("Error: This file cannot be read or does not exist."); return(-1); } byte[] file_header = new byte[4]; file_header = reader.ReadBytes(4); if (!System.Text.Encoding.ASCII.GetString(file_header).Contains("TSO1")) { Console.WriteLine("Error: This seems not to be a TSO file."); } int node_count = reader.ReadInt32(); nodes = new TSONode[node_count]; nodemap = new Dictionary <string, TSONode>(); for (int i = 0; i < node_count; i++) { nodes[i] = new TSONode(); nodes[i].id = i; nodes[i].name = ReadString(); nodes[i].sname = nodes[i].name.Substring(nodes[i].name.LastIndexOf('|') + 1); nodemap.Add(nodes[i].name, nodes[i]); //Console.WriteLine(i + ": " + nodes[i].sname); } for (int i = 0; i < node_count; i++) { int index = nodes[i].name.LastIndexOf('|'); if (index <= 0) { continue; } string pname = nodes[i].name.Substring(0, index); nodes[i].parent = nodemap[pname]; nodes[i].parent.child_nodes.Add(nodes[i]); } { TSONode node; if (nodemap.TryGetValue("|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|Chichi_Right1", out node)) { node.SlowChildren(); } if (nodemap.TryGetValue("|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|Chichi_Left1", out node)) { node.SlowChildren(); } } int node_matrix_count = reader.ReadInt32(); for (int i = 0; i < node_matrix_count; i++) { nodes[i].transformation_matrix = ReadMatrix(); } for (int i = 0; i < node_matrix_count; i++) { nodes[i].ComputeOffsetMatrix(); } UInt32 texture_count = reader.ReadUInt32(); textures = new TSOTex[texture_count]; for (int i = 0; i < texture_count; i++) { textures[i] = read_texture(); } UInt32 script_count = reader.ReadUInt32(); scripts = new TSOScript[script_count]; for (int i = 0; i < script_count; i++) { scripts[i] = read_script(); } UInt32 sub_script_count = reader.ReadUInt32(); TSOScript[] sub_scripts = new TSOScript[sub_script_count]; for (int i = 0; i < sub_script_count; i++) { sub_scripts[i] = read_sub_script(); } scripts[0].sub_scripts = sub_scripts; UInt32 mesh_count = reader.ReadUInt32(); meshes = new TSOMesh[mesh_count]; for (int i = 0; i < mesh_count; i++) { TSOMesh mesh = read_mesh(); meshes[i] = mesh; //Console.WriteLine("mesh name {0} len {1}", mesh.name, mesh.sub_meshes.Length); } reader.Close(); return(0); }
public void SwitchShader(TSOMesh tm_sub) { SwitchShader(scripts[0].sub_scripts[tm_sub.spec].shader); }
public TSOMesh read_mesh() { TSOMesh mesh = new TSOMesh(); mesh.name = ReadString(); mesh.name = mesh.name.Replace(":", "_colon_").Replace("#", "_sharp_"); //should be compatible with directx naming conventions mesh.transform_matrix = ReadMatrix(); mesh.unknown1 = reader.ReadUInt32(); mesh.sub_mesh_count = reader.ReadUInt32(); mesh.sub_meshes = new TSOMesh[mesh.sub_mesh_count]; mesh.spec = 0; mesh.bone_index_LUT = new List <UInt32>(); mesh.bone_LUT = new List <TSONode>(); mesh.vertices = new vertex_field[0]; for (int a = 0; a < mesh.sub_mesh_count; a++) { TSOMesh act_mesh = new TSOMesh(); mesh.sub_meshes[a] = act_mesh; act_mesh.name = mesh.name + "_sub_" + a.ToString(); act_mesh.transform_matrix = mesh.transform_matrix; act_mesh.unknown1 = mesh.unknown1; act_mesh.sub_mesh_count = 0; act_mesh.spec = reader.ReadInt32(); int bone_index_LUT_entry_count = reader.ReadInt32(); //numbones act_mesh.maxPalettes = 16; if (act_mesh.maxPalettes > bone_index_LUT_entry_count) { act_mesh.maxPalettes = bone_index_LUT_entry_count; } act_mesh.bone_index_LUT = new List <UInt32>(); act_mesh.bone_LUT = new List <TSONode>(); for (int i = 0; i < bone_index_LUT_entry_count; i++) { act_mesh.bone_index_LUT.Add(reader.ReadUInt32()); } int vertex_count = reader.ReadInt32(); //numvertices act_mesh.vertices = new vertex_field[vertex_count]; for (int i = 0; i < vertex_count; i++) { ReadVertex(ref act_mesh.vertices[i]); } } for (int a = 0; a < mesh.sub_mesh_count; a++) { TSOMesh sub_mesh = mesh.sub_meshes[a]; for (int i = 0; i < sub_mesh.bone_index_LUT.Count; i++) { UInt32 bone_index = sub_mesh.bone_index_LUT[i]; TSONode bone = nodes[bone_index]; sub_mesh.bone_LUT.Add(bone); } } return(mesh); }