Example #1
0
        public static void ReplaceMaterial(xxParser parser, ImportedMaterial material)
        {
            xxMaterial mat = xx.CreateMaterial(material);

            bool found = false;

            for (int i = 0; i < parser.MaterialList.Count; i++)
            {
                if (parser.MaterialList[i].Name == material.Name)
                {
                    CopyUnknowns(parser.MaterialList[i], mat);

                    parser.MaterialList.RemoveAt(i);
                    parser.MaterialList.Insert(i, mat);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                CreateUnknowns(mat, parser.Format);
                parser.MaterialList.Add(mat);
            }
        }
Example #2
0
        public void MergeMaterial(xxMaterial mat, int srcFormat)
        {
            var newMat = mat.Clone();

            xx.ConvertFormat(newMat, srcFormat, Parser.Format);

            bool found = false;

            for (int i = 0; i < Parser.MaterialList.Count; i++)
            {
                var oldMat = Parser.MaterialList[i];
                if (oldMat.Name == newMat.Name)
                {
                    if (Parser.Format > srcFormat)
                    {
                        xx.CopyUnknowns(oldMat, newMat);
                    }

                    Parser.MaterialList.RemoveAt(i);
                    Parser.MaterialList.Insert(i, newMat);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                Parser.MaterialList.Add(newMat);
            }
        }
Example #3
0
        public static xxMaterial CreateMaterial(ImportedMaterial material)
        {
            xxMaterial xxMat = new xxMaterial();

            xxMat.Name     = material.Name;
            xxMat.Diffuse  = material.Diffuse;
            xxMat.Ambient  = material.Ambient;
            xxMat.Specular = material.Specular;
            xxMat.Emissive = material.Emissive;
            xxMat.Power    = material.Power;

            xxMat.Textures = new xxMaterialTexture[4];
            for (int i = 0; i < xxMat.Textures.Length; i++)
            {
                xxMat.Textures[i] = new xxMaterialTexture();
                if ((material.Textures != null) && (i < material.Textures.Length) && (material.Textures[i] != null))
                {
                    xxMat.Textures[i].Name = material.Textures[i];
                }
                else
                {
                    xxMat.Textures[i].Name = String.Empty;
                }
            }
            return(xxMat);
        }
Example #4
0
        protected xxMaterial ParseMaterial()
        {
            xxMaterial mat = new xxMaterial();

            mat.Name     = reader.ReadName();
            mat.Diffuse  = reader.ReadColor4();
            mat.Ambient  = reader.ReadColor4();
            mat.Specular = reader.ReadColor4();
            mat.Emissive = reader.ReadColor4();
            mat.Power    = reader.ReadSingle();

            mat.Textures = new xxMaterialTexture[4];
            for (int i = 0; i < mat.Textures.Length; i++)
            {
                mat.Textures[i]          = new xxMaterialTexture();
                mat.Textures[i].Name     = reader.ReadName();
                mat.Textures[i].Unknown1 = reader.ReadBytes(16);
            }

            if (Format < 0)
            {
                mat.Unknown1 = reader.ReadBytes(4);
            }
            else
            {
                mat.Unknown1 = reader.ReadBytes(88);
            }
            return(mat);
        }
Example #5
0
 public static void CopyUnknowns(xxMaterial src, xxMaterial dest)
 {
     dest.Unknown1 = (byte[])src.Unknown1.Clone();
     for (int j = 0; j < src.Textures.Length; j++)
     {
         dest.Textures[j].Unknown1 = (byte[])src.Textures[j].Unknown1.Clone();
     }
 }
Example #6
0
        public void SetMaterialPhong(int id, object[] diffuse, object[] ambient, object[] specular, object[] emissive, double shininess)
        {
            xxMaterial mat = Parser.MaterialList[id];

            mat.Diffuse  = new Color4((float)(double)diffuse[3], (float)(double)diffuse[0], (float)(double)diffuse[1], (float)(double)diffuse[2]);
            mat.Ambient  = new Color4((float)(double)ambient[3], (float)(double)ambient[0], (float)(double)ambient[1], (float)(double)ambient[2]);
            mat.Specular = new Color4((float)(double)specular[3], (float)(double)specular[0], (float)(double)specular[1], (float)(double)specular[2]);
            mat.Emissive = new Color4((float)(double)emissive[3], (float)(double)emissive[0], (float)(double)emissive[1], (float)(double)emissive[2]);
            mat.Power    = (float)shininess;
        }
Example #7
0
        public void SetMaterialUnknowns(int id, byte[] unknown1, byte[] tex1, byte[] tex2, byte[] tex3, byte[] tex4)
        {
            xxMaterial mat = Parser.MaterialList[id];

            mat.Unknown1             = (byte[])unknown1.Clone();
            mat.Textures[0].Unknown1 = (byte[])tex1.Clone();
            mat.Textures[1].Unknown1 = (byte[])tex2.Clone();
            mat.Textures[2].Unknown1 = (byte[])tex3.Clone();
            mat.Textures[3].Unknown1 = (byte[])tex4.Clone();
        }
Example #8
0
        private void InitTables()
        {
            xxTable.Rows.Add(new object[] { 0, "Header", Utility.BytesToString(parser.Header) });
            xxTable.Rows.Add(new object[] { 1, "MaterialSection", Utility.BytesToString(parser.MaterialSectionUnknown) });
            if (parser.Format >= 2)
            {
                xxTable.Rows.Add(new object[] { 2, "Footer", Utility.BytesToString(parser.Footer) });                 // 3
            }

            for (int i = 0; i < editor.Frames.Count; i++)
            {
                xxFrame frame = editor.Frames[i];
                frameTable.Rows.Add(new object[] { i, frame.Name, Utility.BytesToString(frame.Unknown1), Utility.BytesToString(frame.Unknown2) });
            }

            for (int i = 0; i < editor.Meshes.Count; i++)
            {
                xxFrame frame = editor.Meshes[i];
                meshTable.Rows.Add(new object[] { i, frame.Name, frame.Mesh.NumVector2PerVertex.ToString("X2"), Utility.BytesToString(frame.Mesh.VertexListDuplicateUnknown) });

                for (int j = 0; j < frame.Mesh.SubmeshList.Count; j++)
                {
                    xxSubmesh submesh = frame.Mesh.SubmeshList[j];
                    submeshTable.Rows.Add(new object[] { new int[] { i, j }, frame.Name + "[" + j + "]",
                                                         Utility.BytesToString(submesh.Unknown1),
                                                         Utility.BytesToString(submesh.Unknown2),
                                                         Utility.BytesToString(submesh.Unknown3),
                                                         Utility.BytesToString(submesh.Unknown4),
                                                         Utility.BytesToString(submesh.Unknown5),
                                                         Utility.BytesToString(submesh.Unknown6) });
                }
            }

            for (int i = 0; i < parser.MaterialList.Count; i++)
            {
                xxMaterial mat = parser.MaterialList[i];
                materialTable.Rows.Add(new object[] { i, mat.Name,
                                                      Utility.BytesToString(mat.Unknown1),
                                                      Utility.BytesToString(mat.Textures[0].Unknown1),
                                                      Utility.BytesToString(mat.Textures[1].Unknown1),
                                                      Utility.BytesToString(mat.Textures[2].Unknown1),
                                                      Utility.BytesToString(mat.Textures[3].Unknown1) });
            }

            for (int i = 0; i < parser.TextureList.Count; i++)
            {
                xxTexture tex = parser.TextureList[i];
                textureTable.Rows.Add(new object[] { i, tex.Name,
                                                     Utility.BytesToString(tex.Unknown1) });
            }
        }
Example #9
0
 public static void ConvertFormat(xxMaterial mat, int srcFormat, int destFormat)
 {
     if ((srcFormat < 0) && (destFormat >= 0))
     {
         byte[] unknown1 = mat.Unknown1;
         mat.Unknown1 = new byte[88];
         Array.Copy(unknown1, mat.Unknown1, unknown1.Length);
     }
     else if ((srcFormat >= 0) && (destFormat < 0))
     {
         byte[] unknown1 = mat.Unknown1;
         mat.Unknown1 = new byte[4];
         Array.Copy(unknown1, mat.Unknown1, mat.Unknown1.Length);
     }
 }
Example #10
0
        public xxMaterial Clone()
        {
            xxMaterial mat = new xxMaterial();

            mat.Name     = Name;
            mat.Diffuse  = Diffuse;
            mat.Ambient  = Ambient;
            mat.Specular = Specular;
            mat.Emissive = Emissive;
            mat.Power    = Power;
            mat.Textures = new xxMaterialTexture[Textures.Length];
            for (int i = 0; i < Textures.Length; i++)
            {
                mat.Textures[i] = Textures[i].Clone();
            }
            mat.Unknown1 = (byte[])Unknown1.Clone();
            return(mat);
        }
Example #11
0
        public static void CreateUnknowns(xxMaterial mat, int xxFormat)
        {
            byte[] buf = (xxFormat < 0) ? new byte[4] : new byte[88];
            if (xxFormat < 0)
            {
                buf = new byte[4];
            }
            else
            {
                buf = new byte[88];
            }
            buf[1]       = 0x01;
            buf[2]       = 0x02;
            buf[3]       = 0x03;
            mat.Unknown1 = buf;

            for (int i = 0; i < mat.Textures.Length; i++)
            {
                var    matTex      = mat.Textures[i];
                byte[] matTexBytes = new byte[16];
                if (matTex.Name != String.Empty)
                {
                    string ext = Path.GetExtension(matTex.Name).ToLowerInvariant();
                    if ((i == 0) && (ext == ".tga"))
                    {
                        matTexBytes[0] = 0x01;
                    }
                    if ((i == 1) && (ext == ".bmp"))
                    {
                        matTexBytes[0] = 0x07;
                        matTexBytes[3] = 0x03;
                    }
                }
                matTex.Unknown1 = matTexBytes;
            }
        }
Example #12
0
        private AnimationFrame CreateFrame(xxFrame frame, xxParser parser, HashSet <string> extractFrames, HashSet <string> meshNames, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames)
        {
            AnimationFrame animationFrame = new AnimationFrame();

            animationFrame.Name = frame.Name;
            animationFrame.TransformationMatrix = frame.Matrix;
            animationFrame.OriginalTransform    = animationFrame.TransformationMatrix;
            animationFrame.CombinedTransform    = combinedParent * animationFrame.TransformationMatrix;

            xxMesh mesh = frame.Mesh;

            if (meshNames.Contains(frame.Name) && (mesh != null))
            {
                List <xxBone> boneList = mesh.BoneList;

                string[] boneNames   = new string[boneList.Count];
                Matrix[] boneOffsets = new Matrix[boneList.Count];
                for (int i = 0; i < boneList.Count; i++)
                {
                    xxBone bone = boneList[i];
                    boneNames[i]   = bone.Name;
                    boneOffsets[i] = bone.Matrix;
                }

                AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.SubmeshList.Count];
                Vector3 min = new Vector3(Single.MaxValue);
                Vector3 max = new Vector3(Single.MinValue);
                for (int i = 0; i < mesh.SubmeshList.Count; i++)
                {
                    xxSubmesh       submesh    = mesh.SubmeshList[i];
                    List <xxFace>   faceList   = submesh.FaceList;
                    List <xxVertex> vertexList = submesh.VertexList;

                    Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format);

                    using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None))
                    {
                        for (int j = 0; j < faceList.Count; j++)
                        {
                            ushort[] indices = faceList[j].VertexIndices;
                            indexStream.Write(indices[0]);
                            indexStream.Write(indices[2]);
                            indexStream.Write(indices[1]);
                        }
                        animationMesh.UnlockIndexBuffer();
                    }

                    FillVertexBuffer(animationMesh, vertexList, -1);

                    var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2];
                    for (int j = 0; j < vertexList.Count; j++)
                    {
                        xxVertex vertex = vertexList[j];

                        normalLines[j * 2]       = new PositionBlendWeightsIndexedColored(vertex.Position, vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb());
                        normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(vertex.Position + (vertex.Normal / 16), vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb());

                        min = Vector3.Minimize(min, vertex.Position);
                        max = Vector3.Maximize(max, vertex.Position);
                    }

                    AnimationMeshContainer meshContainer = new AnimationMeshContainer();
                    meshContainer.Name        = animationFrame.Name;
                    meshContainer.MeshData    = new MeshData(animationMesh);
                    meshContainer.NormalLines = normalLines;
                    meshContainers[i]         = meshContainer;

                    int matIdx = submesh.MaterialIndex;
                    if ((matIdx >= 0) && (matIdx < parser.MaterialList.Count))
                    {
                        int texIdx;
                        if (!MatTexIndices.TryGetValue(matIdx, out texIdx))
                        {
                            texIdx = -1;

                            xxMaterial mat         = parser.MaterialList[matIdx];
                            Material   materialD3D = new Material();
                            materialD3D.Ambient  = mat.Ambient;
                            materialD3D.Diffuse  = mat.Diffuse;
                            materialD3D.Emissive = mat.Emissive;
                            materialD3D.Specular = mat.Specular;
                            materialD3D.Power    = mat.Power;
                            Materials[matIdx]    = materialD3D;

                            xxMaterialTexture matTex     = mat.Textures[0];
                            string            matTexName = matTex.Name;
                            if (matTexName != String.Empty)
                            {
                                for (int j = 0; j < parser.TextureList.Count; j++)
                                {
                                    xxTexture tex = parser.TextureList[j];
                                    if (tex.Name == matTexName)
                                    {
                                        texIdx = j;
                                        if (Textures[j] == null)
                                        {
                                            ImportedTexture importedTex = xx.ImportedTexture(tex);
                                            Textures[j] = Texture.FromMemory(device, importedTex.Data);
                                        }
                                        break;
                                    }
                                }
                            }

                            MatTexIndices.Add(matIdx, texIdx);
                        }

                        meshContainer.MaterialIndex = matIdx;
                        meshContainer.TextureIndex  = texIdx;
                    }
                }

                for (int i = 0; i < (meshContainers.Length - 1); i++)
                {
                    meshContainers[i].NextMeshContainer = meshContainers[i + 1];
                }
                for (int i = 0; i < meshContainers.Length; i++)
                {
                    meshContainers[i].BoneNames   = boneNames;
                    meshContainers[i].BoneOffsets = boneOffsets;
                }

                min = Vector3.TransformCoordinate(min, animationFrame.CombinedTransform);
                max = Vector3.TransformCoordinate(max, animationFrame.CombinedTransform);
                animationFrame.Bounds        = new BoundingBox(min, max);
                animationFrame.MeshContainer = meshContainers[0];
                meshFrames.Add(animationFrame);
            }

            for (int i = 0; i < frame.Count; i++)
            {
                xxFrame child = frame[i];
                if (extractFrames.Contains(child.Name))
                {
                    AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshNames, device, animationFrame.CombinedTransform, meshFrames);
                    childAnimationFrame.Parent = animationFrame;
                    animationFrame.AppendChild(childAnimationFrame);
                }
            }

            numFrames++;
            return(animationFrame);
        }
Example #13
0
            private static List <xxTexture> Export(string dest, xxParser parser, List <xxFrame> meshParents, bool worldCoords)
            {
                List <xxTexture> usedTextures = new List <xxTexture>(parser.TextureList.Count);
                DirectoryInfo    dir          = new DirectoryInfo(Path.GetDirectoryName(dest));

                if (!dir.Exists)
                {
                    dir.Create();
                }

                List <int> materialList = new List <int>(parser.MaterialList.Count);

                using (StreamWriter writer = new StreamWriter(dest, false))
                {
                    for (int i = 0; i < meshParents.Count; i++)
                    {
                        xxMesh meshListSome = meshParents[i].Mesh;
                        for (int j = 0; j < meshListSome.SubmeshList.Count; j++)
                        {
                            xxSubmesh meshObj       = meshListSome.SubmeshList[j];
                            int       meshObjMatIdx = meshObj.MaterialIndex;
                            if ((meshObjMatIdx >= 0) && (meshObjMatIdx < parser.MaterialList.Count))
                            {
                                if (!materialList.Contains(meshObjMatIdx))
                                {
                                    materialList.Add(meshObjMatIdx);
                                }
                            }
                            else
                            {
                                Report.ReportLog("Warning: Mesh " + meshParents[i].Name + " Object " + j + " has an invalid material");
                            }
                        }
                    }

                    writer.WriteLine("Metasequoia Document");
                    writer.WriteLine("Format Text Ver 1.0");
                    writer.WriteLine();
                    writer.WriteLine("Material " + materialList.Count + " {");
                    foreach (int matIdx in materialList)
                    {
                        xxMaterial mat        = parser.MaterialList[matIdx];
                        string     s          = "\t\"" + mat.Name + "\" col(0.800 0.800 0.800 1.000) dif(0.500) amb(0.100) emi(0.500) spc(0.100) power(30.00)";
                        string     matTexName = mat.Textures[0].Name;
                        if (matTexName != String.Empty)
                        {
                            s += " tex(\"" + Path.GetFileName(matTexName) + "\")";
                        }
                        writer.WriteLine(s);
                    }
                    writer.WriteLine("}");

                    Random rand = new Random();
                    for (int i = 0; i < meshParents.Count; i++)
                    {
                        Matrix transform = Matrix.Identity;
                        if (worldCoords)
                        {
                            xxFrame parent = meshParents[i];
                            while (parent != null)
                            {
                                transform = parent.Matrix * transform;
                                parent    = (xxFrame)parent.Parent;
                            }
                        }

                        string meshName     = meshParents[i].Name;
                        xxMesh meshListSome = meshParents[i].Mesh;
                        for (int j = 0; j < meshListSome.SubmeshList.Count; j++)
                        {
                            xxSubmesh meshObj       = meshListSome.SubmeshList[j];
                            int       meshObjMatIdx = meshObj.MaterialIndex;
                            int       mqoMatIdx     = -1;
                            if ((meshObjMatIdx >= 0) && (meshObjMatIdx < parser.MaterialList.Count))
                            {
                                mqoMatIdx = materialList.IndexOf(meshObjMatIdx);
                            }
                            float[] color = new float[3];
                            for (int k = 0; k < color.Length; k++)
                            {
                                color[k] = (float)((rand.NextDouble() / 2) + 0.5);
                            }

                            string mqoName = meshName + "[" + j + "]";
                            if (worldCoords)
                            {
                                mqoName += "[W]";
                            }
                            writer.WriteLine("Object \"" + mqoName + "\" {");
                            writer.WriteLine("\tshading 1");
                            writer.WriteLine("\tcolor " + color[0].ToFloatString() + " " + color[1].ToFloatString() + " " + color[2].ToFloatString());
                            writer.WriteLine("\tcolor_type 1");

                            List <ImportedVertex> vertList = xx.ImportedVertexList(meshObj.VertexList, xx.IsSkinned(meshListSome));
                            List <ImportedFace>   faceList = xx.ImportedFaceList(meshObj.FaceList);
                            if (worldCoords)
                            {
                                for (int k = 0; k < vertList.Count; k++)
                                {
                                    vertList[k].Position = Vector3.TransformCoordinate(vertList[k].Position, transform);
                                }
                            }

                            ExporterCommon.WriteMeshObject(writer, vertList, faceList, mqoMatIdx, null);
                            writer.WriteLine("}");
                        }
                    }
                    writer.WriteLine("Eof");
                }

                foreach (int matIdx in materialList)
                {
                    xxMaterial        mat        = parser.MaterialList[matIdx];
                    xxMaterialTexture matTex     = mat.Textures[0];
                    string            matTexName = matTex.Name;
                    if (matTexName != String.Empty)
                    {
                        for (int i = 0; i < parser.TextureList.Count; i++)
                        {
                            xxTexture tex     = parser.TextureList[i];
                            string    texName = tex.Name;
                            if ((texName == matTexName) && !usedTextures.Contains(tex))
                            {
                                usedTextures.Add(tex);
                                break;
                            }
                        }
                    }
                }
                return(usedTextures);
            }
Example #14
0
            private void Export(string dest, int matIdx)
            {
                DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dest));

                if (!dir.Exists)
                {
                    dir.Create();
                }

                usedTextures = new List <xxTexture>(xxParser.TextureList.Count);
                using (StreamWriter writer = new StreamWriter(dest, false))
                {
                    writer.WriteLine("Metasequoia Document");
                    writer.WriteLine("Format Text Ver 1.0");
                    writer.WriteLine();

                    if ((matIdx >= 0) && (matIdx < xxParser.MaterialList.Count))
                    {
                        writer.WriteLine("Material 1 {");
                        xxMaterial mat        = xxParser.MaterialList[matIdx];
                        string     s          = "\t\"" + mat.Name + "\" vcol(1) col(0.800 0.800 0.800 1.000) dif(0.500) amb(0.100) emi(0.500) spc(0.100) power(30.00)";
                        string     matTexName = mat.Textures[0].Name;
                        if (matTexName != String.Empty)
                        {
                            s += " tex(\"" + Path.GetFileName(matTexName) + "\")";

                            for (int i = 0; i < xxParser.TextureList.Count; i++)
                            {
                                xxTexture tex = xxParser.TextureList[i];
                                if (tex.Name == matTexName)
                                {
                                    usedTextures.Add(tex);
                                    break;
                                }
                            }
                        }
                        writer.WriteLine(s);
                        writer.WriteLine("}");

                        matIdx = 0;
                    }
                    else
                    {
                        matIdx = -1;
                    }

                    Random rand = new Random();
                    for (int i = 0; i < vertLists.Count; i++)
                    {
                        float[] color = new float[3];
                        for (int k = 0; k < color.Length; k++)
                        {
                            color[k] = (float)((rand.NextDouble() / 2) + 0.5);
                        }

                        writer.WriteLine("Object \"" + morphNames[i] + "\" {");
                        writer.WriteLine("\tvisible 0");
                        writer.WriteLine("\tshading 1");
                        writer.WriteLine("\tcolor " + color[0].ToFloatString() + " " + color[1].ToFloatString() + " " + color[2].ToFloatString());
                        writer.WriteLine("\tcolor_type 1");
                        ExporterCommon.WriteMeshObject(writer, vertLists[i], faceList, matIdx, colorVertex);
                        writer.WriteLine("}");
                    }

                    writer.WriteLine("Eof");
                }
            }