public static MATL LibraryToMATL(MaterialLibrary library) { MATL matl = new MATL { Entries = new MatlEntry[library.material.Length] }; for (int i = 0; i < library.material.Length; i++) { MatlEntry entry = new MatlEntry { MaterialLabel = library.material[i].label, MaterialName = library.material[i].name, Attributes = new MatlAttribute[library.material[i].param.Length] }; for (int j = 0; j < library.material[i].param.Length; j++) { entry.Attributes[j] = new MatlAttribute { ParamID = library.material[i].param[j].name, DataObject = library.material[i].param[j].value }; } matl.Entries[i] = entry; } return(matl); }
public static MaterialLibrary MATLtoLibrary(MATL matlFile) { MaterialLibrary library = new MaterialLibrary { material = new Material[matlFile.Entries.Length] }; int entryIndex = 0; foreach (var entry in matlFile.Entries) { Material mat = new Material(); mat.name = entry.MaterialName; mat.label = entry.MaterialLabel; mat.param = new MatlXmlAttribute[entry.Attributes.Length]; int attribIndex = 0; foreach (var attr in entry.Attributes) { MatlXmlAttribute attrib = new MatlXmlAttribute(); attrib.name = attr.ParamID; attrib.value = attr.DataObject; mat.param[attribIndex++] = attrib; } library.material[entryIndex] = mat; entryIndex++; } return(library); }
public override void Open(string Path) { if (SSBH.TryParseSSBHFile(Path, out ISSBH_File ssbhFile)) { if (ssbhFile is MATL) { Material = (MATL)ssbhFile; } } }
public override void Open() { if (SSBH.TryParseSSBHFile(AbsolutePath, out ISSBH_File ssbhFile)) { if (ssbhFile is MATL) { Material = (MATL)ssbhFile; } } }
private static void DeserializeXml(string inputPath, string outputPath, XmlSerializer serializer) { Console.WriteLine($"Converting {Path.GetFileName(inputPath)} to {outputPath}_out.numatb..."); using (TextReader reader = new StringReader(File.ReadAllText(inputPath))) { var result = (MaterialLibrary)serializer.Deserialize(reader); MATL newmatl = LibraryToMATL(result); SSBH.TrySaveSSBHFile(outputPath + "_out.numatb", newmatl); } }
private static void SerializeMatl(string inputPath, string outputPath, XmlSerializer serializer) { Console.WriteLine($"Converting {Path.GetFileName(inputPath)} to {outputPath}_out.xml..."); if (SSBH.TryParseSSBHFile(inputPath, out ISSBH_File file)) { MATL matlFile = (MATL)file; MaterialLibrary library = MATLtoLibrary(matlFile); using (TextWriter writer = new StringWriter()) { serializer.Serialize(writer, library); string serial = writer.ToString(); File.WriteAllText(outputPath + "_out.xml", serial); } } else { Console.WriteLine("Error reading matl file"); } }
public IOModel GetIOModel() { IOModel outModel = new IOModel(); MESH meshFile = null; MATL materialFile = null; foreach (FileNode n in Parent.Nodes) { if (n.Text.Equals(_model.MeshString)) { meshFile = ((NUMSHB_Node)n).mesh; } if (n.Text.Equals(_model.SkeletonFileName)) { outModel.Skeleton = (RSkeleton)((SKEL_Node)n).GetRenderableNode(); } if (n.Text.Equals(_model.MaterialFileNames[0].MaterialFileName)) { materialFile = ((MATL_Node)n).Material; } } Dictionary <string, int> indexByBoneName = new Dictionary <string, int>(); if (outModel.Skeleton != null) { for (int i = 0; i < outModel.Skeleton.Bones.Count; i++) { indexByBoneName.Add(outModel.Skeleton.Bones[i].Name, i); } } Dictionary <string, int> materialNameToIndex = new Dictionary <string, int>(); if (materialFile != null) { int materialIndex = 0; foreach (var entry in materialFile.Entries) { materialNameToIndex.Add(entry.MaterialLabel, materialIndex++); IOMaterial material = new IOMaterial(); material.Name = entry.MaterialLabel; outModel.Materials.Add(material); foreach (var attr in entry.Attributes) { if (attr.ParamID == MatlEnums.ParamId.Texture0) { IOTexture dif = new IOTexture(); dif.Name = attr.DataObject.ToString(); material.DiffuseTexture = dif; } } } } if (meshFile != null) { SSBHVertexAccessor vertexAccessor = new SSBHVertexAccessor(meshFile); { SSBHRiggingAccessor riggingAccessor = new SSBHRiggingAccessor(meshFile); foreach (MeshObject obj in meshFile.Objects) { IOMesh outMesh = new IOMesh() { Name = obj.Name, }; outModel.Meshes.Add(outMesh); // get material if (materialFile != null) { foreach (var entry in _model.ModelEntries) { if (entry.MeshName.Equals(obj.Name) && entry.SubIndex == obj.SubMeshIndex) { outMesh.MaterialIndex = materialNameToIndex[entry.MaterialName]; break; } } } IOVertex[] vertices = new IOVertex[obj.VertexCount]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new IOVertex(); } foreach (MeshAttribute attr in obj.Attributes) { SSBHVertexAttribute[] values = vertexAccessor.ReadAttribute(attr.AttributeStrings[0].Name, 0, obj.VertexCount, obj); if (attr.AttributeStrings[0].Name.Equals("Position0")) { outMesh.HasPositions = true; for (int i = 0; i < values.Length; i++) { vertices[i].Position = new OpenTK.Vector3(values[i].X, values[i].Y, values[i].Z); } } if (attr.AttributeStrings[0].Name.Equals("Normal0")) { outMesh.HasNormals = true; for (int i = 0; i < values.Length; i++) { vertices[i].Normal = new OpenTK.Vector3(values[i].X, values[i].Y, values[i].Z); } } // Flip UVs vertically for export. if (attr.AttributeStrings[0].Name.Equals("map1")) { outMesh.HasUV0 = true; for (int i = 0; i < values.Length; i++) { vertices[i].UV0 = new OpenTK.Vector2(values[i].X, 1 - values[i].Y); } } if (attr.AttributeStrings[0].Name.Equals("uvSet")) { outMesh.HasUV1 = true; for (int i = 0; i < values.Length; i++) { vertices[i].UV1 = new OpenTK.Vector2(values[i].X, 1 - values[i].Y); } } if (attr.AttributeStrings[0].Name.Equals("uvSet1")) { outMesh.HasUV2 = true; for (int i = 0; i < values.Length; i++) { vertices[i].UV2 = new OpenTK.Vector2(values[i].X, 1 - values[i].Y); } } if (attr.AttributeStrings[0].Name.Equals("uvSet2")) { outMesh.HasUV3 = true; for (int i = 0; i < values.Length; i++) { vertices[i].UV3 = new OpenTK.Vector2(values[i].X, 1 - values[i].Y); } } if (attr.AttributeStrings[0].Name.Equals("colorSet1")) { outMesh.HasColor = true; for (int i = 0; i < values.Length; i++) { vertices[i].Color = new OpenTK.Vector4(values[i].X, values[i].Y, values[i].Z, values[i].W) / 127f; } } } // Fix SingleBinds if (outModel.Skeleton != null && !obj.ParentBoneName.Equals("")) { int parentIndex = outModel.Skeleton.GetBoneIndex(obj.ParentBoneName); if (parentIndex != -1) { for (int i = 0; i < vertices.Length; i++) { vertices[i].Position = OpenTK.Vector3.TransformPosition(vertices[i].Position, outModel.Skeleton.Bones[parentIndex].WorldTransform); vertices[i].Normal = OpenTK.Vector3.TransformNormal(vertices[i].Normal, outModel.Skeleton.Bones[parentIndex].WorldTransform); vertices[i].BoneIndices.X = indexByBoneName[obj.ParentBoneName]; vertices[i].BoneWeights.X = 1; outMesh.HasBoneWeights = true; } } } // Apply Rigging SSBHVertexInfluence[] influences = riggingAccessor.ReadRiggingBuffer(obj.Name, (int)obj.SubMeshIndex); foreach (SSBHVertexInfluence influence in influences) { outMesh.HasBoneWeights = true; // Some influences refer to bones that don't exist in the skeleton. // _eff bones? if (!indexByBoneName.ContainsKey(influence.BoneName)) { continue; } if (vertices[influence.VertexIndex].BoneWeights.X == 0) { vertices[influence.VertexIndex].BoneIndices.X = indexByBoneName[influence.BoneName]; vertices[influence.VertexIndex].BoneWeights.X = influence.Weight; } else if (vertices[influence.VertexIndex].BoneWeights.Y == 0) { vertices[influence.VertexIndex].BoneIndices.Y = indexByBoneName[influence.BoneName]; vertices[influence.VertexIndex].BoneWeights.Y = influence.Weight; } else if (vertices[influence.VertexIndex].BoneWeights.Z == 0) { vertices[influence.VertexIndex].BoneIndices.Z = indexByBoneName[influence.BoneName]; vertices[influence.VertexIndex].BoneWeights.Z = influence.Weight; } else if (vertices[influence.VertexIndex].BoneWeights.W == 0) { vertices[influence.VertexIndex].BoneIndices.W = indexByBoneName[influence.BoneName]; vertices[influence.VertexIndex].BoneWeights.W = influence.Weight; } } outMesh.Vertices.AddRange(vertices); outMesh.Indices.AddRange(vertexAccessor.ReadIndices(0, obj.IndexCount, obj)); } } } return(outModel); }