Beispiel #1
0
 private int[] CreateMaterialTranslation(List <xxMaterial> srcMaterials, int srcFormat, List <xxTexture> srcTextures, bool appendIfMissing)
 {
     int[] matTranslation = srcMaterials.Count > 0 ? new int[srcMaterials.Count] : null;
     for (int i = 0; i < srcMaterials.Count; i++)
     {
         matTranslation[i] = -1;
         for (int j = 0; j < Parser.MaterialList.Count; j++)
         {
             if (Parser.MaterialList[j].Name == srcMaterials[i].Name)
             {
                 matTranslation[i] = j;
                 break;
             }
         }
         if (appendIfMissing && matTranslation[i] == -1)
         {
             matTranslation[i] = Parser.MaterialList.Count;
             MergeMaterial(srcMaterials[i], srcFormat);
             for (int j = 0; j < Parser.MaterialList[matTranslation[i]].Textures.Length; j++)
             {
                 xxMaterialTexture matTex = Parser.MaterialList[matTranslation[i]].Textures[j];
                 if (FindTexture(matTex.Name, Parser.TextureList) == null)
                 {
                     xxTexture tex = FindTexture(matTex.Name, srcTextures);
                     if (tex != null)
                     {
                         MergeTexture(tex);
                     }
                 }
             }
         }
     }
     return(matTranslation);
 }
Beispiel #2
0
        public xxMaterialTexture Clone()
        {
            xxMaterialTexture matTex = new xxMaterialTexture();

            matTex.Name     = Name;
            matTex.Unknown1 = (byte[])Unknown1.Clone();
            return(matTex);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
            }