Ejemplo n.º 1
0
        public static void ReplaceTexture(xxParser parser, ImportedTexture texture)
        {
            xxTexture tex = xx.CreateTexture(texture);

            bool found = false;

            for (int i = 0; i < parser.TextureList.Count; i++)
            {
                if (parser.TextureList[i].Name == texture.Name)
                {
                    CopyUnknowns(parser.TextureList[i], tex);

                    parser.TextureList.RemoveAt(i);
                    parser.TextureList.Insert(i, tex);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                CreateUnknowns(tex);
                parser.TextureList.Add(tex);
            }
        }
Ejemplo n.º 2
0
        public static void SaveXX(xxParser parser, string destPath, bool keepBackup)
        {
            DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(destPath));

            string backup = null;

            if (keepBackup && File.Exists(destPath))
            {
                backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(destPath));
                File.Move(destPath, backup);
            }

            try
            {
                using (BufferedStream bufStr = new BufferedStream(File.OpenWrite(destPath)))
                {
                    parser.WriteTo(bufStr);
                }
            }
            catch
            {
                if (File.Exists(backup))
                {
                    if (File.Exists(destPath))
                    {
                        File.Delete(destPath);
                    }
                    File.Move(backup, destPath);
                }
            }
        }
Ejemplo n.º 3
0
            public static void Export(string dirPath, xxParser xxParser, xxFrame meshFrame, xaParser xaParser, xaMorphClip clip)
            {
                DirectoryInfo dir      = new DirectoryInfo(dirPath);
                ExporterMorph exporter = new ExporterMorph(dir, xxParser, xaParser, clip);

                exporter.Export(dir, meshFrame);
            }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
        public xxEditor(xxParser parser)
        {
            Parser = parser;

            Frames = new List<xxFrame>();
            Meshes = new List<xxFrame>();
            InitFrames(parser.Frame);
        }
Ejemplo n.º 6
0
        public xxEditor(xxParser parser)
        {
            Parser = parser;

            Frames = new List <xxFrame>();
            Meshes = new List <xxFrame>();
            InitFrames(parser.Frame);
        }
Ejemplo n.º 7
0
            public static void Export(string dirPath, xxParser parser, List <xxFrame> meshParents, bool singleMqo, bool worldCoords)
            {
                DirectoryInfo    dir          = new DirectoryInfo(dirPath);
                List <xxTexture> usedTextures = new List <xxTexture>(parser.TextureList.Count);

                if (singleMqo)
                {
                    try
                    {
                        string           dest    = Utility.GetDestFile(dir, "meshes", ".mqo");
                        List <xxTexture> texList = Export(dest, parser, meshParents, worldCoords);
                        foreach (xxTexture tex in texList)
                        {
                            if (!usedTextures.Contains(tex))
                            {
                                usedTextures.Add(tex);
                            }
                        }
                        Report.ReportLog("Finished exporting meshes to " + dest);
                    }
                    catch (Exception ex)
                    {
                        Report.ReportLog("Error exporting meshes: " + ex.Message);
                    }
                }
                else
                {
                    for (int i = 0; i < meshParents.Count; i++)
                    {
                        try
                        {
                            string           frameName = meshParents[i].Name;
                            string           dest      = dir.FullName + @"\" + frameName + ".mqo";
                            List <xxTexture> texList   = Export(dest, parser, new List <xxFrame> {
                                meshParents[i]
                            }, worldCoords);
                            foreach (xxTexture tex in texList)
                            {
                                if (!usedTextures.Contains(tex))
                                {
                                    usedTextures.Add(tex);
                                }
                            }
                            Report.ReportLog("Finished exporting mesh to " + dest);
                        }
                        catch (Exception ex)
                        {
                            Report.ReportLog("Error exporting mesh: " + ex.Message);
                        }
                    }
                }

                foreach (xxTexture tex in usedTextures)
                {
                    xx.ExportTexture(tex, dir.FullName + @"\" + Path.GetFileName(tex.Name));
                }
            }
Ejemplo n.º 8
0
        private AnimationFrame CreateHierarchy(xxParser parser, HashSet <string> meshNames, Device device, out List <AnimationFrame> meshFrames)
        {
            meshFrames = new List <AnimationFrame>(meshNames.Count);
            HashSet <string> extractFrames = xx.SearchHierarchy(parser.Frame, meshNames);
            AnimationFrame   rootFrame     = CreateFrame(parser.Frame, parser, extractFrames, meshNames, device, Matrix.Identity, meshFrames);

            SetupBoneMatrices(rootFrame, rootFrame);
            return(rootFrame);
        }
Ejemplo n.º 9
0
        public static void ExportMqo([DefaultVar] xxParser parser, object[] meshNames, string dirPath, bool singleMqo, bool worldCoords, bool sortMeshes = true)
        {
            List <xxFrame> meshParents = xx.FindMeshFrames(parser.Frame, new List <string>(Utility.Convert <string>(meshNames)));

            if (sortMeshes)
            {
                meshParents.Sort();
            }
            Mqo.Exporter.Export(dirPath, parser, meshParents, singleMqo, worldCoords);
        }
Ejemplo n.º 10
0
        public static void ExportFbx([DefaultVar] xxParser xxParser, object[] meshNames, object[] xaParsers, int startKeyframe, int endKeyframe, bool linear, string path, string exportFormat, bool allFrames, bool skins)
        {
            List <xaParser> xaParserList = null;

            if (xaParsers != null)
            {
                xaParserList = new List <xaParser>(Utility.Convert <xaParser>(xaParsers));
            }

            List <xxFrame> meshFrames = xx.FindMeshFrames(xxParser.Frame, new List <string>(Utility.Convert <string>(meshNames)));

            Fbx.Exporter.Export(path, xxParser, meshFrames, xaParserList, startKeyframe, endKeyframe, linear, exportFormat, allFrames, skins);
        }
Ejemplo n.º 11
0
        public FormXXEditHex(FormXX formXX, List <int[]> gotoCells)
        {
            InitializeComponent();
            this.Text     += " of " + formXX.ToolTipText;
            this.formXX    = formXX;
            this.editor    = formXX.Editor;
            this.parser    = editor.Parser;
            this.gotoCells = gotoCells;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control control in page.Controls)
                {
                    if (control is DataGridViewEditor)
                    {
                        page.Tag = control;
                        break;
                    }
                }
            }

            xxTable.BeginLoadData();
            frameTable.BeginLoadData();
            meshTable.BeginLoadData();
            submeshTable.BeginLoadData();
            materialTable.BeginLoadData();
            textureTable.BeginLoadData();

            InitColumns();
            InitTables();

            xxTable.EndLoadData();
            xxTable.AcceptChanges();
            frameTable.EndLoadData();
            frameTable.AcceptChanges();
            meshTable.EndLoadData();
            meshTable.AcceptChanges();
            submeshTable.EndLoadData();
            submeshTable.AcceptChanges();
            materialTable.EndLoadData();
            materialTable.AcceptChanges();
            textureTable.EndLoadData();
            textureTable.AcceptChanges();

            this.KeyDown += new KeyEventHandler(FormXXEditHex_KeyDown);
            this.Load    += new EventHandler(FormXXEditHex_Load);
        }
Ejemplo n.º 12
0
        public FormXXEditHex(FormXX formXX, List<int[]> gotoCells)
        {
            InitializeComponent();
            this.Text += " of " + formXX.ToolTipText;
            this.formXX = formXX;
            this.editor = formXX.Editor;
            this.parser = editor.Parser;
            this.gotoCells = gotoCells;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control control in page.Controls)
                {
                    if (control is DataGridViewEditor)
                    {
                        page.Tag = control;
                        break;
                    }
                }
            }

            xxTable.BeginLoadData();
            frameTable.BeginLoadData();
            meshTable.BeginLoadData();
            submeshTable.BeginLoadData();
            materialTable.BeginLoadData();
            textureTable.BeginLoadData();

            InitColumns();
            InitTables();

            xxTable.EndLoadData();
            xxTable.AcceptChanges();
            frameTable.EndLoadData();
            frameTable.AcceptChanges();
            meshTable.EndLoadData();
            meshTable.AcceptChanges();
            submeshTable.EndLoadData();
            submeshTable.AcceptChanges();
            materialTable.EndLoadData();
            materialTable.AcceptChanges();
            textureTable.EndLoadData();
            textureTable.AcceptChanges();

            this.KeyDown += new KeyEventHandler(FormXXEditHex_KeyDown);
            this.Load += new EventHandler(FormXXEditHex_Load);
        }
Ejemplo n.º 13
0
Archivo: Fbx.cs Proyecto: kkdevs/sb3u
        public static void ExportFbx([DefaultVar] xxParser xxParser, object[] meshNames, object[] xaParsers, int startKeyframe, int endKeyframe, bool linear, bool EulerFilter, double filterPrecision, string path, string exportFormat, bool allFrames, bool skins, bool embedMedia, bool compatibility)
        {
            List <xaParser> xaParserList = null;

            if (xaParsers != null)
            {
                xaParserList = new List <xaParser>(Utility.Convert <xaParser>(xaParsers));
            }

            List <xxFrame> meshFrames = meshNames != null?xx.FindMeshFrames(xxParser.Frame, new List <string>(Utility.Convert <string>(meshNames))) : null;

            if (meshFrames != null && meshFrames.Count == 0)
            {
                meshFrames = null;
            }
            Fbx.Exporter.Export(path, xxParser, meshFrames, xaParserList, startKeyframe, endKeyframe, linear, EulerFilter, (float)filterPrecision, exportFormat, allFrames, skins, embedMedia, compatibility);
        }
Ejemplo n.º 14
0
        public static void ConvertFormat(xxParser parser, int destFormat)
        {
            int srcFormat = parser.Format;

            if ((srcFormat < 1) && (destFormat >= 1))
            {
                byte[] headerBuf = new byte[26];
                headerBuf[0] = (byte)destFormat;
                parser.Header.CopyTo(headerBuf, 5);
                parser.Header = headerBuf;
            }
            else if ((srcFormat >= 1) && (destFormat < 1))
            {
                byte[] headerBuf = new byte[21];
                Array.Copy(parser.Header, 5, headerBuf, 0, headerBuf.Length);
                parser.Header = headerBuf;
            }
            if (destFormat >= 1)
            {
                MemoryStream stream = new MemoryStream(parser.Header);
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(destFormat);
                }
            }

            ConvertFormat(parser.Frame, srcFormat, destFormat);

            for (int i = 0; i < parser.MaterialList.Count; i++)
            {
                ConvertFormat(parser.MaterialList[i], srcFormat, destFormat);
            }

            if ((srcFormat < 2) && (destFormat >= 2))
            {
                parser.Footer = new byte[10];
            }
            else if ((srcFormat >= 2) && (destFormat < 2))
            {
                parser.Footer = null;
            }

            parser.Format = destFormat;
        }
Ejemplo n.º 15
0
            private Exporter(xxParser xxParser, List<xxFrame> meshParents, bool allFrames)
            {
                try
                {
                    if (xxParser != null)
                    {
                        foreach (var frame in meshParents)
                        {
                            meshNames.Add(frame.Name);
                        }

                        if (!allFrames)
                        {
                            frameNames = xx.SearchHierarchy(xxParser.Frame, meshNames);
                        }
                    }
                }
                catch (Exception e)
                {
                    Report.ReportLog("Error initializing Collada exporter: " + e.Message);
                }
            }
Ejemplo n.º 16
0
        public RenderObjectXX(xxParser parser, HashSet <string> meshNames)
        {
            HighlightSubmesh          = new HashSet <int>();
            highlightMaterial         = new Material();
            highlightMaterial.Ambient = new Color4(1, 1, 1, 1);
            highlightMaterial.Diffuse = new Color4(1, 0, 1, 0);

            this.device          = Gui.Renderer.Device;
            this.tweeningVertDec = new VertexDeclaration(this.device, TweeningMeshesVertexBufferFormat.ThreeStreams);

            Textures  = new Texture[parser.TextureList.Count];
            Materials = new Material[parser.MaterialList.Count];

            rootFrame = CreateHierarchy(parser, meshNames, device, out meshFrames);

            AnimationController = new AnimationController(numFrames, 30, 30, 1);
            Frame.RegisterNamedMatrices(rootFrame, AnimationController);

            Bounds = meshFrames[0].Bounds;
            for (int i = 1; i < meshFrames.Count; i++)
            {
                Bounds = BoundingBox.Merge(Bounds, meshFrames[i].Bounds);
            }
        }
Ejemplo n.º 17
0
        public static void ReplaceTexture(xxParser parser, ImportedTexture texture)
        {
            xxTexture tex = xx.CreateTexture(texture);

            bool found = false;
            for (int i = 0; i < parser.TextureList.Count; i++)
            {
                if (parser.TextureList[i].Name == texture.Name)
                {
                    CopyUnknowns(parser.TextureList[i], tex);

                    parser.TextureList.RemoveAt(i);
                    parser.TextureList.Insert(i, tex);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                CreateUnknowns(tex);
                parser.TextureList.Add(tex);
            }
        }
Ejemplo n.º 18
0
 private ExporterMorph(DirectoryInfo dir, xxParser xxParser, xaParser xaParser, xaMorphClip clip)
 {
     this.xxParser = xxParser;
     this.xaParser = xaParser;
     this.clip     = clip;
 }
Ejemplo n.º 19
0
Archivo: xx.cs Proyecto: kkdevs/sb3u
 public static void ConvertXX([DefaultVar] xxParser parser, int format)
 {
     xx.ConvertFormat(parser, format);
 }
Ejemplo n.º 20
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;
        }
Ejemplo n.º 21
0
            public Exporter(string dest, xxParser parser, HashSet<string> meshNames, List<xaParser> xaParsers, int ticksPerSecond, int keyframeLength)
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dest));
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }

                    writer = new StreamWriter(dest, false);
                    if (parser != null)
                    {
                        WriteXHeader(writer);
                        this.xxParser = parser;
                        ExportFrame(parser.Frame, meshNames, 0);
                        foreach (var pair in usedTextures)
                        {
                            xxTexture tex = pair.Value;
                            xx.ExportTexture(tex, dir.FullName + @"\" + Path.GetFileName(tex.Name));
                        }
                    }
                    if (xaParsers != null && xaParsers.Count > 0)
                    {
                        writer.WriteLine("AnimTicksPerSecond {");
                        writer.WriteLine(" " + ticksPerSecond + ";");
                        writer.WriteLine("}");
                        writer.WriteLine();

                        SortedDictionary<string, int> animationNameDic = new SortedDictionary<string, int>();
                        foreach (xaParser xaParser in xaParsers)
                        {
                            List<xaAnimationTrack> animationList = xaParser.AnimationSection.TrackList;
                            string animationName = xaParser.Name.Replace('.', '_');
                            int animationNameCount;
                            if (animationNameDic.TryGetValue(xaParser.Name, out animationNameCount))
                            {
                                animationNameCount++;
                                animationNameDic[xaParser.Name] = animationNameCount;
                                ExportAnimation(animationList, ticksPerSecond, keyframeLength, animationName + animationNameCount);
                            }
                            else
                            {
                                animationNameDic.Add(xaParser.Name, 1);
                                ExportAnimation(animationList, ticksPerSecond, keyframeLength, animationName);
                            }
                        }
                    }
                    writer.Close();

                    Report.ReportLog("Finished exporting to " + dest);
                }
                catch (Exception ex)
                {
                    Utility.ReportException(ex);
                }
            }
Ejemplo n.º 22
0
 public static void Export(string dest, xxParser parser, xxFrame[] meshParents, List<xaParser> xaSubfiles, int ticksPerSecond, int keyframeLength)
 {
     HashSet<string> meshNames = new HashSet<string>();
     foreach (xxFrame frame in meshParents)
     {
         meshNames.Add(frame.Name);
     }
     Exporter exporter = new Exporter(dest, parser, meshNames, xaSubfiles, ticksPerSecond, keyframeLength);
 }
Ejemplo n.º 23
0
        public static void ReplaceMesh(xxFrame frame, xxParser parser, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod)
        {
            Matrix transform = Matrix.Identity;
            xxFrame transformFrame = frame;
            while (transformFrame != null)
            {
                transform = transformFrame.Matrix * transform;
                transformFrame = (xxFrame)transformFrame.Parent;
            }
            transform.Invert();

            string[] materialNames;
            int[] indices;
            bool[] worldCoords;
            bool[] replaceSubmeshesOption;
            xxMesh xxMesh = CreateMesh(mesh, parser.Format, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            if (frame.Mesh == null)
            {
                CreateUnknowns(xxMesh);
            }
            else
            {
                CopyUnknowns(frame.Mesh, xxMesh);

                if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                {
                    xxMesh.BoneList = new List<xxBone>(frame.Mesh.BoneList.Count);
                    for (int i = 0; i < frame.Mesh.BoneList.Count; i++)
                    {
                        xxMesh.BoneList.Add(frame.Mesh.BoneList[i].Clone());
                    }
                }
            }

            xxSubmesh[] replaceSubmeshes = (frame.Mesh == null) ? null : new xxSubmesh[frame.Mesh.SubmeshList.Count];
            List<xxSubmesh> addSubmeshes = new List<xxSubmesh>(xxMesh.SubmeshList.Count);
            for (int i = 0; i < xxMesh.SubmeshList.Count; i++)
            {
                for (int j = 0; j < parser.MaterialList.Count; j++)
                {
                    if (parser.MaterialList[j].Name == materialNames[i])
                    {
                        xxMesh.SubmeshList[i].MaterialIndex = j;
                        break;
                    }
                }

                xxSubmesh xxSubmesh = xxMesh.SubmeshList[i];
                List<xxVertex> xxVertexList = xxSubmesh.VertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < xxVertexList.Count; j++)
                    {
                        xxVertexList[j].Position = Vector3.TransformCoordinate(xxVertexList[j].Position, transform);
                    }
                }

                xxSubmesh baseSubmesh = null;
                int idx = indices[i];
                if ((frame.Mesh != null) && (idx >= 0) && (idx < frame.Mesh.SubmeshList.Count))
                {
                    baseSubmesh = frame.Mesh.SubmeshList[idx];
                    CopyUnknowns(baseSubmesh, xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }
                else
                {
                    CreateUnknowns(xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyNormalsOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyNormalsNear(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }

                    if (bonesMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyBonesOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (bonesMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyBonesNear(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                }

                if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i])
                {
                    replaceSubmeshes[idx] = xxSubmesh;
                }
                else
                {
                    addSubmeshes.Add(xxSubmesh);
                }
            }

            if ((frame.Mesh != null) && merge)
            {
                xxMesh.SubmeshList = new List<xxSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List<xxSubmesh> copiedSubmeshes = new List<xxSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        xxSubmesh xxSubmesh = frame.Mesh.SubmeshList[i].Clone();
                        copiedSubmeshes.Add(xxSubmesh);
                        xxMesh.SubmeshList.Add(xxSubmesh);
                    }
                    else
                    {
                        xxMesh.SubmeshList.Add(replaceSubmeshes[i]);
                    }
                }
                xxMesh.SubmeshList.AddRange(addSubmeshes);

                if ((frame.Mesh.BoneList.Count == 0) && (xxMesh.BoneList.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = copiedSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count == 0))
                {
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List<xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count > 0))
                {
                    byte[] boneIdxMap;
                    xxMesh.BoneList = MergeBoneList(frame.Mesh.BoneList, xxMesh.BoneList, out boneIdxMap);
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List<xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                byte[] boneIndices = vertexList[j].BoneIndices;
                                vertexList[j].BoneIndices = new byte[4];
                                for (int k = 0; k < 4; k++)
                                {
                                    vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                                }
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            byte[] boneIndices = vertexList[j].BoneIndices;
                            vertexList[j].BoneIndices = new byte[4];
                            for (int k = 0; k < 4; k++)
                            {
                                vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                            }
                        }
                    }
                }
            }

            if ((xxMesh.NumVector2PerVertex > 0) || ((frame.Mesh != null) && merge))
            {
                xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList);
            }

            frame.Mesh = xxMesh;
            SetBoundingBox(frame);
        }
Ejemplo n.º 24
0
 public static void ExportDae([DefaultVar]string path, xxParser xxparser, List<xxFrame> meshFrames, List<xaParser> xaparsers, bool allFrames)
 {
     Collada.Exporter.Export(path, xxparser, meshFrames, xaparsers, allFrames);
 }
Ejemplo n.º 25
0
            private void SetJoints(xxParser parser, XmlElement visualScene)
            {
                HashSet<string> boneNames = new HashSet<string>();
                List<xxFrame> meshFrames = xx.FindMeshFrames(parser.Frame);
                foreach (xxFrame frame in meshFrames)
                {
                    xxMesh meshList = frame.Mesh;
                    List<xxBone> boneList = meshList.BoneList;
                    for (int j = 0; j < boneList.Count; j++)
                    {
                        xxBone bone = boneList[j];
                        string boneName = EncodeName(bone.Name);
                        boneNames.Add(boneName);
                    }
                }

                SetJointsNode(visualScene, boneNames);
            }
Ejemplo n.º 26
0
            private void ExportTexture(xxTexture texture, xxParser parser)
            {
                string texName = EncodeName(texture.Name);

                // image
                XmlElement image = doc.CreateElement("image", uri);
                image.SetAttribute("id", texName + "-image");
                image.SetAttribute("name", texName);
                libraries[(int)LibraryIdx.Images].AppendChild(image);

                XmlElement imageInit = doc.CreateElement("init_from", uri);
                imageInit.InnerText = "file://" + texture.Name;
                image.AppendChild(imageInit);
            }
Ejemplo n.º 27
0
            private void Export(string path, xxParser xxParser, List<xaParser> xaSubfileList)
            {
                try
                {
                    XmlElement collada = doc.CreateElement("COLLADA", uri);
                    collada.SetAttribute("version", "1.4.1");
                    doc.AppendChild(collada);

                    XmlElement asset = doc.CreateElement("asset", uri);
                    collada.AppendChild(asset);

                    DateTime creationTime = DateTime.Now.ToUniversalTime();
                    string creationTimeString = creationTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");
                    asset.AppendChild(doc.CreateElement("created", uri)).InnerText = creationTimeString;
                    asset.AppendChild(doc.CreateElement("modified", uri)).InnerText = creationTimeString;
                    asset.AppendChild(doc.CreateElement("up_axis", uri)).InnerText = "Y_UP";

                    if (xaSubfileList.Count > 0)
                    {
                        libraries[(int)LibraryIdx.Animations] = doc.CreateElement("library_animations", uri);
                        Dictionary<string, int> animationNameDic = new Dictionary<string, int>();
                        foreach (var subfile in xaSubfileList)
                        {
                            xaParser parser = subfile;
                            var animationNodeList = parser.AnimationSection.TrackList;

                            int animationNameIdx;
                            if (animationNameDic.TryGetValue(subfile.Name, out animationNameIdx))
                            {
                                animationNameIdx++;
                                animationNameDic[subfile.Name] = animationNameIdx;
                                ExportAnimation(animationNodeList, subfile.Name + animationNameIdx);
                            }
                            else
                            {
                                animationNameDic.Add(subfile.Name, 1);
                                ExportAnimation(animationNodeList, subfile.Name);
                            }
                        }
                    }

                    if (xxParser != null)
                    {
                        libraries[(int)LibraryIdx.Images] = doc.CreateElement("library_images", uri);
                        libraries[(int)LibraryIdx.Materials] = doc.CreateElement("library_materials", uri);
                        libraries[(int)LibraryIdx.Effects] = doc.CreateElement("library_effects", uri);
                        libraries[(int)LibraryIdx.Geometries] = doc.CreateElement("library_geometries", uri);
                        libraries[(int)LibraryIdx.Controllers] = doc.CreateElement("library_controllers", uri);
                        libraries[(int)LibraryIdx.Visual_Scenes] = doc.CreateElement("library_visual_scenes", uri);

                        XmlElement visualScene = doc.CreateElement("visual_scene", uri);
                        visualScene.SetAttribute("id", "RootNode");
                        visualScene.SetAttribute("name", "RootNode");
                        libraries[(int)LibraryIdx.Visual_Scenes].AppendChild(visualScene);

                        List<xxFrame> meshFrames = new List<xxFrame>();
                        List<XmlElement> meshNodes = new List<XmlElement>();
                        ExportFrame(visualScene, xxParser.Frame, meshFrames, meshNodes);

                        SetJoints(xxParser, visualScene);

                        List<xxMaterial> usedMaterials = new List<xxMaterial>(xxParser.MaterialList.Count);
                        for (int i = 0; i < meshFrames.Count; i++)
                        {
                            ExportMesh(meshFrames[i], meshNodes[i], xxParser, usedMaterials);
                        }
                        List<xxTexture> usedTextures = new List<xxTexture>(xxParser.TextureList.Count);
                        for (int i = 0; i < usedMaterials.Count; i++)
                        {
                            ExportMaterial(usedMaterials[i], xxParser, usedTextures);
                        }
                        foreach (xxTexture tex in usedTextures)
                        {
                            ExportTexture(tex, xxParser);
                            xx.ExportTexture(tex, Path.GetDirectoryName(path) + @"\" + tex.Name);
                        }
                    }
                    for (int i = 0; i < libraries.Length; i++)
                    {
                        if ((libraries[i] != null) && (libraries[i].ChildNodes.Count > 0))
                        {
                            collada.AppendChild(libraries[i]);
                        }
                    }
                    if (xxParser != null)
                    {
                        XmlElement scene = doc.CreateElement("scene", uri);
                        collada.AppendChild(scene);
                        XmlElement sceneInstance = doc.CreateElement("instance_visual_scene", uri);
                        sceneInstance.SetAttribute("url", "#RootNode");
                        scene.AppendChild(sceneInstance);
                    }

                    DirectoryInfo destDir = new DirectoryInfo(Path.GetDirectoryName(path));
                    if (!destDir.Exists)
                    {
                        destDir.Create();
                    }
                    using (XmlTextWriter xmlWriter = new XmlTextWriter(path, Encoding.UTF8))
                    {
                        xmlWriter.Formatting = Formatting.Indented;
                        doc.Save(xmlWriter);
                    }
                    Report.ReportLog("Finished exporting to " + path);
                }
                catch (Exception e)
                {
                    Report.ReportLog("Error exporting to Collada: " + e.Message);
                }
            }
Ejemplo n.º 28
0
 public static void Export(string path, xxParser xxParser, List<xxFrame> meshParents, List<xaParser> xaSubfileList, bool allFrames)
 {
     Exporter exporter = new Exporter(xxParser, meshParents, allFrames);
     exporter.Export(path, xxParser, xaSubfileList);
 }
Ejemplo n.º 29
0
            private void ExportMesh(xxFrame frame, XmlElement parent, xxParser parser, List<xxMaterial> usedMaterials)
            {
                string frameName = EncodeName(frame.Name);
                xxMesh meshList = frame.Mesh;
                List<xxBone> boneList = meshList.BoneList;

                for (int i = 0; i < meshList.SubmeshList.Count; i++)
                {
                    string meshName = frameName + "_" + i;
                    xxSubmesh meshObj = meshList.SubmeshList[i];
                    List<xxFace> faceList = meshObj.FaceList;
                    List<xxVertex> vertexList = meshObj.VertexList;

                    XmlElement geometry = doc.CreateElement("geometry", uri);
                    geometry.SetAttribute("id", meshName + "-lib");
                    geometry.SetAttribute("name", meshName + "Mesh");
                    libraries[(int)LibraryIdx.Geometries].AppendChild(geometry);

                    XmlElement mesh = doc.CreateElement("mesh", uri);
                    geometry.AppendChild(mesh);

                    // positions
                    XmlElement positions = doc.CreateElement("source", uri);
                    positions.SetAttribute("id", meshName + "-lib-Position");
                    mesh.AppendChild(positions);

                    XmlElement positionsArray = doc.CreateElement("float_array", uri);
                    positionsArray.SetAttribute("id", meshName + "-lib-Position-array");
                    positionsArray.SetAttribute("count", (vertexList.Count * 3).ToString());
                    positions.AppendChild(positionsArray);

                    XmlElement positionsTechnique = doc.CreateElement("technique_common", uri);
                    positions.AppendChild(positionsTechnique);
                    XmlElement positionsAccessor = doc.CreateElement("accessor", uri);
                    positionsAccessor.SetAttribute("source", "#" + meshName + "-lib-Position-array");
                    positionsAccessor.SetAttribute("count", vertexList.Count.ToString());
                    positionsAccessor.SetAttribute("stride", "3");
                    positionsTechnique.AppendChild(positionsAccessor);
                    XmlElement positionsAccessorParamX = doc.CreateElement("param", uri);
                    positionsAccessorParamX.SetAttribute("name", "X");
                    positionsAccessorParamX.SetAttribute("type", "float");
                    positionsAccessor.AppendChild(positionsAccessorParamX);
                    XmlElement positionsAccessorParamY = doc.CreateElement("param", uri);
                    positionsAccessorParamY.SetAttribute("name", "Y");
                    positionsAccessorParamY.SetAttribute("type", "float");
                    positionsAccessor.AppendChild(positionsAccessorParamY);
                    XmlElement positionsAccessorParamZ = doc.CreateElement("param", uri);
                    positionsAccessorParamZ.SetAttribute("name", "Z");
                    positionsAccessorParamZ.SetAttribute("type", "float");
                    positionsAccessor.AppendChild(positionsAccessorParamZ);

                    // normals
                    XmlElement normals = doc.CreateElement("source", uri);
                    normals.SetAttribute("id", meshName + "-lib-Normal0");
                    mesh.AppendChild(normals);

                    XmlElement normalsArray = doc.CreateElement("float_array", uri);
                    normalsArray.SetAttribute("id", meshName + "-lib-Normal0-array");
                    normalsArray.SetAttribute("count", (vertexList.Count * 3).ToString());
                    normals.AppendChild(normalsArray);

                    XmlElement normalsTechnique = doc.CreateElement("technique_common", uri);
                    normals.AppendChild(normalsTechnique);
                    XmlElement normalsAccessor = doc.CreateElement("accessor", uri);
                    normalsAccessor.SetAttribute("source", "#" + meshName + "-lib-Normal0-array");
                    normalsAccessor.SetAttribute("count", vertexList.Count.ToString());
                    normalsAccessor.SetAttribute("stride", "3");
                    normalsTechnique.AppendChild(normalsAccessor);
                    XmlElement normalsAccessorParamX = doc.CreateElement("param", uri);
                    normalsAccessorParamX.SetAttribute("name", "X");
                    normalsAccessorParamX.SetAttribute("type", "float");
                    normalsAccessor.AppendChild(normalsAccessorParamX);
                    XmlElement normalsAccessorParamY = doc.CreateElement("param", uri);
                    normalsAccessorParamY.SetAttribute("name", "Y");
                    normalsAccessorParamY.SetAttribute("type", "float");
                    normalsAccessor.AppendChild(normalsAccessorParamY);
                    XmlElement normalsAccessorParamZ = doc.CreateElement("param", uri);
                    normalsAccessorParamZ.SetAttribute("name", "Z");
                    normalsAccessorParamZ.SetAttribute("type", "float");
                    normalsAccessor.AppendChild(normalsAccessorParamZ);

                    // uvs
                    XmlElement uvs = doc.CreateElement("source", uri);
                    uvs.SetAttribute("id", meshName + "-lib-UV0");
                    mesh.AppendChild(uvs);

                    XmlElement uvsArray = doc.CreateElement("float_array", uri);
                    uvsArray.SetAttribute("id", meshName + "-lib-UV0-array");
                    uvsArray.SetAttribute("count", (vertexList.Count * 2).ToString());
                    uvs.AppendChild(uvsArray);

                    XmlElement uvsTechnique = doc.CreateElement("technique_common", uri);
                    uvs.AppendChild(uvsTechnique);
                    XmlElement uvsAccessor = doc.CreateElement("accessor", uri);
                    uvsAccessor.SetAttribute("source", "#" + meshName + "-lib-UV0-array");
                    uvsAccessor.SetAttribute("count", vertexList.Count.ToString());
                    uvsAccessor.SetAttribute("stride", "2");
                    uvsTechnique.AppendChild(uvsAccessor);
                    XmlElement uvsAccessorParamS = doc.CreateElement("param", uri);
                    uvsAccessorParamS.SetAttribute("name", "S");
                    uvsAccessorParamS.SetAttribute("type", "float");
                    uvsAccessor.AppendChild(uvsAccessorParamS);
                    XmlElement uvsAccessorParamT = doc.CreateElement("param", uri);
                    uvsAccessorParamT.SetAttribute("name", "T");
                    uvsAccessorParamT.SetAttribute("type", "float");
                    uvsAccessor.AppendChild(uvsAccessorParamT);

                    // faces
                    XmlElement vertices = doc.CreateElement("vertices", uri);
                    vertices.SetAttribute("id", meshName + "-lib-Vertex");
                    mesh.AppendChild(vertices);

                    XmlElement verticesInput = doc.CreateElement("input", uri);
                    verticesInput.SetAttribute("semantic", "POSITION");
                    verticesInput.SetAttribute("source", "#" + meshName + "-lib-Position");
                    vertices.AppendChild(verticesInput);

                    XmlElement polygons = doc.CreateElement("polygons", uri);
                    mesh.AppendChild(polygons);
                    XmlElement polygonsVertex = doc.CreateElement("input", uri);
                    polygonsVertex.SetAttribute("semantic", "VERTEX");
                    polygonsVertex.SetAttribute("offset", "0");
                    polygonsVertex.SetAttribute("source", "#" + meshName + "-lib-Vertex");
                    polygons.AppendChild(polygonsVertex);
                    XmlElement polygonsNormal = doc.CreateElement("input", uri);
                    polygonsNormal.SetAttribute("semantic", "NORMAL");
                    polygonsNormal.SetAttribute("offset", "1");
                    polygonsNormal.SetAttribute("source", "#" + meshName + "-lib-Normal0");
                    polygons.AppendChild(polygonsNormal);
                    XmlElement polygonsUV = doc.CreateElement("input", uri);
                    polygonsUV.SetAttribute("semantic", "TEXCOORD");
                    polygonsUV.SetAttribute("offset", "2");
                    polygonsUV.SetAttribute("set", "0");
                    polygonsUV.SetAttribute("source", "#" + meshName + "-lib-UV0");
                    polygons.AppendChild(polygonsUV);

                    // text
                    StringBuilder positionsString = new StringBuilder(12 * 3 * vertexList.Count);
                    StringBuilder normalsString = new StringBuilder(12 * 3 * vertexList.Count);
                    StringBuilder uvsString = new StringBuilder(12 * 2 * vertexList.Count);
                    positionsString.AppendLine();
                    normalsString.AppendLine();
                    uvsString.AppendLine();
                    for (int j = 0; j < vertexList.Count; j++)
                    {
                        xxVertex vert = vertexList[j];
                        positionsString.Append(vert.Position[0].ToFloatString() + " ");
                        positionsString.Append(vert.Position[1].ToFloatString() + " ");
                        positionsString.AppendLine(vert.Position[2].ToFloatString());
                        normalsString.Append(vert.Normal[0].ToFloatString() + " ");
                        normalsString.Append(vert.Normal[1].ToFloatString() + " ");
                        normalsString.AppendLine(vert.Normal[2].ToFloatString());
                        uvsString.Append(vert.UV[0].ToFloatString() + " ");
                        uvsString.AppendLine((1f - vert.UV[1]).ToFloatString());
                    }
                    positionsArray.InnerText = positionsString.ToString();
                    normalsArray.InnerText = normalsString.ToString();
                    uvsArray.InnerText = uvsString.ToString();

                    for (int j = 0; j < faceList.Count; j++)
                    {
                        XmlElement polygonsP = doc.CreateElement("p", uri);
                        polygons.AppendChild(polygonsP);

                        xxFace face = faceList[j];
                        StringBuilder polygonsPText = new StringBuilder(12 * 3 * 3);
                        for (int m = 0; m < face.VertexIndices.Length; m++)
                        {
                            polygonsPText.Append(face.VertexIndices[m] + " " + face.VertexIndices[m] + " " + face.VertexIndices[m] + " ");
                        }
                        polygonsP.InnerText = polygonsPText.ToString(0, polygonsPText.Length - 1);
                    }

                    // mesh binding
                    XmlElement meshNode = doc.CreateElement("node", uri);
                    meshNode.SetAttribute("id", meshName);
                    meshNode.SetAttribute("name", meshName);
                    parent.AppendChild(meshNode);

                    XmlElement nodeGeometry;
                    bool skinned = (boneList.Count > 0);
                    if (skinned)
                    {
                        nodeGeometry = doc.CreateElement("instance_controller", uri);
                        nodeGeometry.SetAttribute("url", "#" + meshName + "Controller");
                        meshNode.AppendChild(nodeGeometry);

                        XmlElement controller = doc.CreateElement("controller", uri);
                        controller.SetAttribute("id", meshName + "Controller");
                        libraries[(int)LibraryIdx.Controllers].AppendChild(controller);

                        XmlElement skin = doc.CreateElement("skin", uri);
                        skin.SetAttribute("source", "#" + meshName + "-lib");
                        controller.AppendChild(skin);

                        Matrix combined = Matrix.Identity;
                        /*ObjInfo parentFrame = frame;
                        while (parentFrame is xxFrame)
                        {
                            combined = ((xxFrame)parentFrame).matrix.ToMatrix4() * combined;
                            parentFrame = parentFrame.parent;
                        }*/
                        XmlElement bindMatrix = doc.CreateElement("bind_shape_matrix", uri);
                        string bindMatrixStr = String.Empty;
                        for (int j = 0; j < 4; j++)
                        {
                            for (int k = 0; k < 4; k++)
                            {
                                bindMatrixStr += combined[j, k].ToFloatString() + " ";
                            }
                        }
                        bindMatrix.InnerText = bindMatrixStr.Substring(0, bindMatrixStr.Length - 1);
                        skin.AppendChild(bindMatrix);

                        XmlElement joints = doc.CreateElement("source", uri);
                        joints.SetAttribute("id", meshName + "Controller-Joints");
                        skin.AppendChild(joints);
                        XmlElement jointsArray = doc.CreateElement("Name_array", uri);
                        jointsArray.SetAttribute("id", meshName + "Controller-Joints-array");
                        joints.AppendChild(jointsArray);
                        XmlElement jointsTechnique = doc.CreateElement("technique_common", uri);
                        joints.AppendChild(jointsTechnique);
                        XmlElement jointsAccessor = doc.CreateElement("accessor", uri);
                        jointsAccessor.SetAttribute("source", "#" + meshName + "Controller-Joints-array");
                        jointsTechnique.AppendChild(jointsAccessor);
                        XmlElement jointsParam = doc.CreateElement("param", uri);
                        jointsParam.SetAttribute("type", "name");
                        jointsAccessor.AppendChild(jointsParam);

                        XmlElement bindPoses = doc.CreateElement("source", uri);
                        bindPoses.SetAttribute("id", meshName + "Controller-Matrices");
                        skin.AppendChild(bindPoses);
                        XmlElement bindPosesArray = doc.CreateElement("float_array", uri);
                        bindPosesArray.SetAttribute("id", meshName + "Controller-Matrices-array");
                        bindPoses.AppendChild(bindPosesArray);
                        XmlElement bindPosesTechnique = doc.CreateElement("technique_common", uri);
                        bindPoses.AppendChild(bindPosesTechnique);
                        XmlElement bindPosesAccessor = doc.CreateElement("accessor", uri);
                        bindPosesAccessor.SetAttribute("source", "#" + meshName + "Controller-Matrices-array");
                        bindPosesTechnique.AppendChild(bindPosesAccessor);
                        XmlElement bindPosesParam = doc.CreateElement("param", uri);
                        bindPosesParam.SetAttribute("type", "float4x4");
                        bindPosesAccessor.AppendChild(bindPosesParam);

                        XmlElement weights = doc.CreateElement("source", uri);
                        weights.SetAttribute("id", meshName + "Controller-Weights");
                        skin.AppendChild(weights);
                        XmlElement weightsArray = doc.CreateElement("float_array", uri);
                        weightsArray.SetAttribute("id", meshName + "Controller-Weights-array");
                        weights.AppendChild(weightsArray);
                        XmlElement weightsTechnique = doc.CreateElement("technique_common", uri);
                        weights.AppendChild(weightsTechnique);
                        XmlElement weightsAccessor = doc.CreateElement("accessor", uri);
                        weightsAccessor.SetAttribute("source", "#" + meshName + "Controller-Weights-array");
                        weightsTechnique.AppendChild(weightsAccessor);
                        XmlElement weightsParam = doc.CreateElement("param", uri);
                        weightsParam.SetAttribute("type", "float");
                        weightsAccessor.AppendChild(weightsParam);

                        XmlElement jointSemantic = doc.CreateElement("joints", uri);
                        skin.AppendChild(jointSemantic);
                        XmlElement jointSemanticJoint = doc.CreateElement("input", uri);
                        jointSemanticJoint.SetAttribute("semantic", "JOINT");
                        jointSemanticJoint.SetAttribute("source", "#" + meshName + "Controller-Joints");
                        jointSemantic.AppendChild(jointSemanticJoint);
                        XmlElement jointSemanticMatrix = doc.CreateElement("input", uri);
                        jointSemanticMatrix.SetAttribute("semantic", "INV_BIND_MATRIX");
                        jointSemanticMatrix.SetAttribute("source", "#" + meshName + "Controller-Matrices");
                        jointSemantic.AppendChild(jointSemanticMatrix);

                        XmlElement verticesArray = doc.CreateElement("vertex_weights", uri);
                        verticesArray.SetAttribute("count", vertexList.Count.ToString());
                        skin.AppendChild(verticesArray);
                        XmlElement verticesJoint = doc.CreateElement("input", uri);
                        verticesJoint.SetAttribute("semantic", "JOINT");
                        verticesJoint.SetAttribute("offset", "0");
                        verticesJoint.SetAttribute("source", "#" + meshName + "Controller-Joints");
                        verticesArray.AppendChild(verticesJoint);
                        XmlElement verticesWeight = doc.CreateElement("input", uri);
                        verticesWeight.SetAttribute("semantic", "WEIGHT");
                        verticesWeight.SetAttribute("offset", "1");
                        verticesWeight.SetAttribute("source", "#" + meshName + "Controller-Weights");
                        verticesArray.AppendChild(verticesWeight);
                        XmlElement verticesVCount = doc.CreateElement("vcount", uri);
                        verticesArray.AppendChild(verticesVCount);
                        XmlElement verticesV = doc.CreateElement("v", uri);
                        verticesArray.AppendChild(verticesV);

                        StringBuilder weightsArrayText = new StringBuilder(12 * 4 * vertexList.Count);
                        StringBuilder vCountText = new StringBuilder(12 * 4 * vertexList.Count);
                        weightsArrayText.AppendLine();
                        int[] numVertexWeights = new int[vertexList.Count];
                        int totalWeights = 0;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            xxVertex vert = vertexList[j];
                            byte[] indices = vert.BoneIndices;
                            float[] weights4 = vert.Weights4(skinned);
                            for (int k = 0; k < indices.Length; k++)
                            {
                                if ((indices[k] < boneList.Count) && (weights4[k] > 0))
                                {
                                    weightsArrayText.Append(weights4[k].ToFloatString() + " ");
                                    numVertexWeights[j]++;
                                }
                            }
                            if (numVertexWeights[j] > 0)
                            {
                                weightsArrayText.Remove(weightsArrayText.Length - 1, 1);
                                weightsArrayText.AppendLine();
                            }
                            vCountText.Append(numVertexWeights[j] + " ");
                            totalWeights += numVertexWeights[j];
                        }
                        weightsArray.SetAttribute("count", totalWeights.ToString());
                        weightsArray.InnerText = weightsArrayText.ToString();
                        weightsAccessor.SetAttribute("count", totalWeights.ToString());
                        verticesVCount.InnerText = vCountText.ToString(0, vCountText.Length - 1);

                        StringBuilder jointsArrayText = new StringBuilder(32 * boneList.Count);
                        StringBuilder bindPosesArrayText = new StringBuilder(12 * 16 * boneList.Count);
                        jointsArrayText.AppendLine();
                        bindPosesArrayText.AppendLine();
                        int totalBones = 0;
                        int[] boneIdx = new int[boneList.Count];
                        List<string> usedBoneNames = new List<string>(boneList.Count);
                        for (int j = 0; j < boneList.Count; j++)
                        {
                            xxBone bone = boneList[j];
                            string boneName = EncodeName(bone.Name);
                            Matrix boneMatrix = bone.Matrix;
                            for (int m = 0; m < 4; m++)
                            {
                                for (int n = 0; n < 4; n++)
                                {
                                    bindPosesArrayText.Append(boneMatrix[m, n].ToFloatString() + " ");
                                }
                            }
                            usedBoneNames.Add(boneName);
                            bindPosesArrayText.Remove(bindPosesArrayText.Length - 1, 1);
                            bindPosesArrayText.AppendLine();
                            jointsArrayText.Append(boneName + " ");
                            boneIdx[j] = totalBones;
                            totalBones++;
                        }

                        jointsArray.SetAttribute("count", totalBones.ToString());
                        if (jointsArrayText.Length > 0)
                        {
                            jointsArrayText.Remove(jointsArrayText.Length - 1, 1);
                            jointsArrayText.AppendLine();
                        }
                        jointsArray.InnerText = jointsArrayText.ToString();
                        jointsAccessor.SetAttribute("count", totalBones.ToString());
                        bindPosesArray.SetAttribute("count", (totalBones * 16).ToString());
                        bindPosesArray.InnerText = bindPosesArrayText.ToString();
                        bindPosesAccessor.SetAttribute("count", totalBones.ToString());
                        bindPosesAccessor.SetAttribute("stride", "16");

                        StringBuilder vText = new StringBuilder(12 * 2 * vertexList.Count);
                        int weightsIdx = 0;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            xxVertex vert = vertexList[j];
                            byte[] indices = vert.BoneIndices;
                            float[] weights4 = vert.Weights4(skinned);
                            for (int k = 0; k < indices.Length; k++)
                            {
                                if ((indices[k] < boneList.Count) && (weights4[k] > 0))
                                {
                                    vText.Append(boneIdx[indices[k]] + " " + weightsIdx + " ");
                                    weightsIdx++;
                                }
                            }
                        }
                        verticesV.InnerText = vText.ToString(0, vText.Length - 1);
                    }
                    else
                    {
                        nodeGeometry = doc.CreateElement("instance_geometry", uri);
                        nodeGeometry.SetAttribute("url", "#" + meshName + "-lib");
                        meshNode.AppendChild(nodeGeometry);
                    }

                    int matIdx = meshObj.MaterialIndex;
                    if ((matIdx >= 0) && (matIdx < parser.MaterialList.Count))
                    {
                        xxMaterial mat = parser.MaterialList[matIdx];
                        if (!usedMaterials.Contains(mat))
                        {
                            usedMaterials.Add(mat);
                        }

                        string matName = EncodeName(mat.Name);
                        polygons.SetAttribute("material", matName);

                        XmlElement nodeMaterial = doc.CreateElement("bind_material", uri);
                        nodeGeometry.AppendChild(nodeMaterial);

                        XmlElement nodeMaterialTechnique = doc.CreateElement("technique_common", uri);
                        nodeMaterial.AppendChild(nodeMaterialTechnique);

                        XmlElement nodeMaterialInstance = doc.CreateElement("instance_material", uri);
                        nodeMaterialInstance.SetAttribute("symbol", matName);
                        nodeMaterialInstance.SetAttribute("target", "#" + matName);
                        nodeMaterialTechnique.AppendChild(nodeMaterialInstance);
                    }

                    polygons.SetAttribute("count", faceList.Count.ToString());
                }
            }
Ejemplo n.º 30
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);
        }
Ejemplo n.º 31
0
        public static void ExportDae([DefaultVar]string path, xxParser xxParser, object[] meshNames, object[] xaParsers, bool allFrames)
        {
            List<xaParser> xaParserList = null;
            if (xaParsers != null)
            {
                xaParserList = new List<xaParser>(Utility.Convert<xaParser>(xaParsers));
            }

            List<xxFrame> meshFrames = xx.FindMeshFrames(xxParser.Frame, new List<string>(Utility.Convert<string>(meshNames)));
            Collada.Exporter.Export(path, xxParser, meshFrames, xaParserList, allFrames);
        }
Ejemplo n.º 32
0
 public static void ExportMorphMqo([DefaultVar] string dirPath, xxParser xxparser, xxFrame meshFrame, xaParser xaparser, xaMorphClip clip)
 {
     Mqo.ExporterMorph.Export(dirPath, xxparser, meshFrame, xaparser, clip);
 }
Ejemplo n.º 33
0
        public static void ExportDirectX(string path, xxParser xxParser, object[] meshNames, object[] xaParsers, int ticksPerSecond, int keyframeLength)
        {
            List<xaParser> xaParserList = null;
            if (xaParsers != null)
            {
                xaParserList = new List<xaParser>(Utility.Convert<xaParser>(xaParsers));
            }

            List<xxFrame> meshParents = xx.FindMeshFrames(xxParser.Frame, new List<string>(Utility.Convert<string>(meshNames)));
            DirectX.Exporter.Export(path, xxParser, meshParents.ToArray(), xaParserList, ticksPerSecond, keyframeLength);
        }
Ejemplo n.º 34
0
 public void Dispose()
 {
     Frames.Clear();
     Meshes.Clear();
     Parser = null;
 }
Ejemplo n.º 35
0
        public RenderObjectXX(xxParser parser, HashSet<string> meshNames)
        {
            HighlightSubmesh = new HashSet<int>();
            highlightMaterial = new Material();
            highlightMaterial.Ambient = new Color4(1, 1, 1, 1);
            highlightMaterial.Diffuse = new Color4(1, 0, 1, 0);

            this.device = Gui.Renderer.Device;
            this.tweeningVertDec = new VertexDeclaration(this.device, TweeningMeshesVertexBufferFormat.ThreeStreams);

            Textures = new Texture[parser.TextureList.Count];
            Materials = new Material[parser.MaterialList.Count];

            rootFrame = CreateHierarchy(parser, meshNames, device, out meshFrames);

            AnimationController = new AnimationController(numFrames, 30, 30, 1);
            Frame.RegisterNamedMatrices(rootFrame, AnimationController);

            Bounds = meshFrames[0].Bounds;
            for (int i = 1; i < meshFrames.Count; i++)
            {
                Bounds = BoundingBox.Merge(Bounds, meshFrames[i].Bounds);
            }
        }
Ejemplo n.º 36
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);
            }
Ejemplo n.º 37
0
 private AnimationFrame CreateHierarchy(xxParser parser, HashSet<string> meshNames, Device device, out List<AnimationFrame> meshFrames)
 {
     meshFrames = new List<AnimationFrame>(meshNames.Count);
     HashSet<string> extractFrames = xx.SearchHierarchy(parser.Frame, meshNames);
     AnimationFrame rootFrame = CreateFrame(parser.Frame, parser, extractFrames, meshNames, device, Matrix.Identity, meshFrames);
     SetupBoneMatrices(rootFrame, rootFrame);
     return rootFrame;
 }
Ejemplo n.º 38
0
 public void Dispose()
 {
     Frames.Clear();
     Meshes.Clear();
     Parser = null;
 }
Ejemplo n.º 39
0
        public static void ReplaceMesh(xxFrame frame, xxParser parser, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool targetFullMesh)
        {
            Matrix  transform      = Matrix.Identity;
            xxFrame transformFrame = frame;

            while (transformFrame != null)
            {
                transform      = transformFrame.Matrix * transform;
                transformFrame = (xxFrame)transformFrame.Parent;
            }
            transform.Invert();

            string[] materialNames;
            int[]    indices;
            bool[]   worldCoords;
            bool[]   replaceSubmeshesOption;
            xxMesh   xxMesh = CreateMesh(mesh, parser.Format, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            List <xxVertex> allVertices = null;

            if (frame.Mesh == null)
            {
                CreateUnknowns(xxMesh);
            }
            else
            {
                CopyUnknowns(frame.Mesh, xxMesh);

                if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                {
                    xxMesh.BoneList = new List <xxBone>(frame.Mesh.BoneList.Count);
                    for (int i = 0; i < frame.Mesh.BoneList.Count; i++)
                    {
                        xxMesh.BoneList.Add(frame.Mesh.BoneList[i].Clone());
                    }
                }

                if (targetFullMesh && (normalsMethod == CopyMeshMethod.CopyNear || bonesMethod == CopyMeshMethod.CopyNear))
                {
                    allVertices = new List <xxVertex>();
                    HashSet <Vector3> posSet = new HashSet <Vector3>(new VertexPositionComparer());
                    foreach (xxSubmesh submesh in frame.Mesh.SubmeshList)
                    {
                        allVertices.Capacity = allVertices.Count + submesh.VertexList.Count;
                        foreach (xxVertex vertex in submesh.VertexList)
                        {
                            if (!posSet.Contains(vertex.Position))
                            {
                                posSet.Add(vertex.Position);
                                allVertices.Add(vertex);
                            }
                        }
                    }
                }
            }

            xxSubmesh[]      replaceSubmeshes = (frame.Mesh == null) ? null : new xxSubmesh[frame.Mesh.SubmeshList.Count];
            List <xxSubmesh> addSubmeshes     = new List <xxSubmesh>(xxMesh.SubmeshList.Count);

            for (int i = 0; i < xxMesh.SubmeshList.Count; i++)
            {
                for (int j = 0; j < parser.MaterialList.Count; j++)
                {
                    if (parser.MaterialList[j].Name == materialNames[i])
                    {
                        xxMesh.SubmeshList[i].MaterialIndex = j;
                        break;
                    }
                }

                xxSubmesh       xxSubmesh    = xxMesh.SubmeshList[i];
                List <xxVertex> xxVertexList = xxSubmesh.VertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < xxVertexList.Count; j++)
                    {
                        xxVertexList[j].Position = Vector3.TransformCoordinate(xxVertexList[j].Position, transform);
                    }
                }

                xxSubmesh baseSubmesh = null;
                int       idx         = indices[i];
                if ((frame.Mesh != null) && (idx >= 0) && (idx < frame.Mesh.SubmeshList.Count))
                {
                    baseSubmesh = frame.Mesh.SubmeshList[idx];
                    CopyUnknowns(baseSubmesh, xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }
                else
                {
                    CreateUnknowns(xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyNormalsOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyNormalsNear(targetFullMesh ? allVertices : baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }

                    if (bonesMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyBonesOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (bonesMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyBonesNear(targetFullMesh ? allVertices : baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                }

                if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i])
                {
                    replaceSubmeshes[idx] = xxSubmesh;
                }
                else
                {
                    addSubmeshes.Add(xxSubmesh);
                }
            }

            if ((frame.Mesh != null) && merge)
            {
                xxMesh.SubmeshList = new List <xxSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List <xxSubmesh> copiedSubmeshes = new List <xxSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        xxSubmesh xxSubmesh = frame.Mesh.SubmeshList[i].Clone();
                        copiedSubmeshes.Add(xxSubmesh);
                        xxMesh.SubmeshList.Add(xxSubmesh);
                    }
                    else
                    {
                        xxMesh.SubmeshList.Add(replaceSubmeshes[i]);
                    }
                }
                xxMesh.SubmeshList.AddRange(addSubmeshes);

                if ((frame.Mesh.BoneList.Count == 0) && (xxMesh.BoneList.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = copiedSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] {
                                0xFF, 0xFF, 0xFF, 0xFF
                            };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count == 0))
                {
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List <xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                vertexList[j].BoneIndices = new byte[4] {
                                    0xFF, 0xFF, 0xFF, 0xFF
                                };
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] {
                                0xFF, 0xFF, 0xFF, 0xFF
                            };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count > 0))
                {
                    byte[] boneIdxMap;
                    xxMesh.BoneList = MergeBoneList(frame.Mesh.BoneList, xxMesh.BoneList, out boneIdxMap);
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List <xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                byte[] boneIndices = vertexList[j].BoneIndices;
                                vertexList[j].BoneIndices = new byte[4];
                                for (int k = 0; k < 4; k++)
                                {
                                    vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                                }
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            byte[] boneIndices = vertexList[j].BoneIndices;
                            vertexList[j].BoneIndices = new byte[4];
                            for (int k = 0; k < 4; k++)
                            {
                                vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                            }
                        }
                    }
                }
            }

            if ((xxMesh.NumVector2PerVertex > 0) || ((frame.Mesh != null) && merge))
            {
                xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList);
            }

            frame.Mesh = xxMesh;
            SetBoundingBox(frame);
        }
Ejemplo n.º 40
0
            private void ExportMaterial(xxMaterial mat, xxParser parser, List<xxTexture> usedTextures)
            {
                string matName = EncodeName(mat.Name);

                // material
                XmlElement material = doc.CreateElement("material", uri);
                material.SetAttribute("id", matName);
                material.SetAttribute("name", matName);
                libraries[(int)LibraryIdx.Materials].AppendChild(material);

                XmlElement materialEffect = doc.CreateElement("instance_effect", uri);
                materialEffect.SetAttribute("url", "#" + matName + "-fx");
                material.AppendChild(materialEffect);

                // effect
                XmlElement effect = doc.CreateElement("effect", uri);
                effect.SetAttribute("id", matName + "-fx");
                effect.SetAttribute("name", matName);
                libraries[(int)LibraryIdx.Effects].AppendChild(effect);

                XmlElement effectProfile = doc.CreateElement("profile_COMMON", uri);
                effect.AppendChild(effectProfile);

                XmlElement effectTechnique = doc.CreateElement("technique", uri);
                effectTechnique.SetAttribute("sid", "standard");
                // added at the end

                XmlElement effectPhong = doc.CreateElement("phong", uri);
                effectTechnique.AppendChild(effectPhong);

                XmlElement effectEmission = doc.CreateElement("emission", uri);
                effectPhong.AppendChild(effectEmission);
                XmlElement effectEmissionColor = doc.CreateElement("color", uri);
                effectEmissionColor.SetAttribute("sid", "emission");
                effectEmissionColor.InnerText = mat.Emissive.Red.ToFloatString() + " " + mat.Emissive.Green.ToFloatString() + " " + mat.Emissive.Blue.ToFloatString() + " " + mat.Emissive.Alpha.ToFloatString();
                effectEmission.AppendChild(effectEmissionColor);

                XmlElement effectAmbient = doc.CreateElement("ambient", uri);
                effectPhong.AppendChild(effectAmbient);
                XmlElement effectAmbientColor = doc.CreateElement("color", uri);
                effectAmbientColor.SetAttribute("sid", "ambient");
                effectAmbientColor.InnerText = mat.Ambient.Red.ToFloatString() + " " + mat.Ambient.Green.ToFloatString() + " " + mat.Ambient.Blue.ToFloatString() + " " + mat.Ambient.Alpha.ToFloatString();
                effectAmbient.AppendChild(effectAmbientColor);

                XmlElement effectDiffuse = doc.CreateElement("diffuse", uri);
                effectPhong.AppendChild(effectDiffuse);

                XmlElement effectSpecular = doc.CreateElement("specular", uri);
                effectPhong.AppendChild(effectSpecular);
                XmlElement effectSpecularColor = doc.CreateElement("color", uri);
                effectSpecularColor.SetAttribute("sid", "ambient");
                effectSpecularColor.InnerText = mat.Specular.Red.ToFloatString() + " " + mat.Specular.Green.ToFloatString() + " " + mat.Specular.Blue.ToFloatString() + " " + mat.Specular.Alpha.ToFloatString();
                effectSpecular.AppendChild(effectSpecularColor);

                XmlElement effectShininess = doc.CreateElement("shininess", uri);
                effectPhong.AppendChild(effectShininess);
                XmlElement effectShininessFloat = doc.CreateElement("float", uri);
                effectShininessFloat.SetAttribute("sid", "shininess");
                effectShininessFloat.InnerText = mat.Power.ToFloatString();
                effectShininess.AppendChild(effectShininessFloat);

                XmlElement effectReflective = doc.CreateElement("reflective", uri);
                effectPhong.AppendChild(effectReflective);
                XmlElement effectReflectiveColor = doc.CreateElement("color", uri);
                effectReflectiveColor.SetAttribute("sid", "reflective");
                effectReflectiveColor.InnerText = "0 0 0 1";
                effectReflective.AppendChild(effectReflectiveColor);

                XmlElement effectReflectivity = doc.CreateElement("reflectivity", uri);
                effectPhong.AppendChild(effectReflectivity);
                XmlElement effectReflectivityFloat = doc.CreateElement("float", uri);
                effectReflectivityFloat.SetAttribute("sid", "reflectivity");
                effectReflectivityFloat.InnerText = "0.5";
                effectReflectivity.AppendChild(effectReflectivityFloat);

                XmlElement effectTransparent = doc.CreateElement("transparent", uri);
                effectPhong.AppendChild(effectTransparent);
                XmlElement effectTransparentColor = doc.CreateElement("color", uri);
                effectTransparentColor.SetAttribute("sid", "transparent");
                effectTransparentColor.InnerText = "0 0 0 1";
                effectTransparent.AppendChild(effectTransparentColor);

                XmlElement effectTransparency = doc.CreateElement("transparency", uri);
                effectPhong.AppendChild(effectTransparency);
                XmlElement effectTransparencyFloat = doc.CreateElement("float", uri);
                effectTransparencyFloat.SetAttribute("sid", "transparency");
                effectTransparencyFloat.InnerText = "1";
                effectTransparency.AppendChild(effectTransparencyFloat);

                xxMaterialTexture matTex = mat.Textures[0];
                string matTexName = matTex.Name;
                if ((matTexName == null) || (matTexName == String.Empty))
                {
                    XmlElement effectDiffuseColor = doc.CreateElement("color", uri);
                    effectDiffuseColor.InnerText = mat.Diffuse.Red.ToFloatString() + " " + mat.Diffuse.Green.ToFloatString() + " " + mat.Diffuse.Blue.ToFloatString() + " " + mat.Diffuse.Alpha.ToFloatString();
                    effectDiffuse.AppendChild(effectDiffuseColor);
                }
                else
                {
                    xxTexture usedTex = null;
                    foreach (xxTexture tex in parser.TextureList)
                    {
                        if (matTexName == tex.Name)
                        {
                            usedTex = tex;
                            break;
                        }
                    }

                    if (usedTex == null)
                    {
                        XmlElement effectDiffuseColor = doc.CreateElement("color", uri);
                        effectDiffuseColor.InnerText = mat.Diffuse.Red.ToFloatString() + " " + mat.Diffuse.Green.ToFloatString() + " " + mat.Diffuse.Blue.ToFloatString() + " " + mat.Diffuse.Alpha.ToFloatString();
                        effectDiffuse.AppendChild(effectDiffuseColor);
                    }
                    else
                    {
                        if (!usedTextures.Contains(usedTex))
                        {
                            usedTextures.Add(usedTex);
                        }
                        string texName = EncodeName(usedTex.Name);

                        XmlElement effectDiffuseTexture = doc.CreateElement("texture", uri);
                        effectDiffuseTexture.SetAttribute("texture", texName + "-image");
                        effectDiffuseTexture.SetAttribute("texcoord", "CHANNEL0");
                        effectDiffuse.AppendChild(effectDiffuseTexture);

                        XmlElement effectDiffuseTextureExtra = doc.CreateElement("extra", uri);
                        effectDiffuseTexture.AppendChild(effectDiffuseTextureExtra);

                        XmlElement effectDiffuseTextureTechnique = doc.CreateElement("technique", uri);
                        effectDiffuseTextureTechnique.SetAttribute("profile", "MAYA");
                        effectDiffuseTextureExtra.AppendChild(effectDiffuseTextureTechnique);

                        XmlElement effectDiffuseTextureWrapU = doc.CreateElement("wrapU", uri);
                        effectDiffuseTextureWrapU.SetAttribute("sid", "wrapU0");
                        effectDiffuseTextureWrapU.InnerText = "TRUE";
                        effectDiffuseTextureTechnique.AppendChild(effectDiffuseTextureWrapU);

                        XmlElement effectDiffuseTextureWrapV = doc.CreateElement("wrapV", uri);
                        effectDiffuseTextureWrapV.SetAttribute("sid", "wrapV0");
                        effectDiffuseTextureWrapV.InnerText = "TRUE";
                        effectDiffuseTextureTechnique.AppendChild(effectDiffuseTextureWrapV);

                        XmlElement effectDiffuseTextureBlend = doc.CreateElement("blend_mode", uri);
                        effectDiffuseTextureBlend.InnerText = "NONE";
                        effectDiffuseTextureTechnique.AppendChild(effectDiffuseTextureBlend);
                    }
                }

                effectProfile.AppendChild(effectTechnique);
            }
Ejemplo n.º 41
0
 public static void ExportMorphFbx([DefaultVar] xxParser xxparser, string path, xxFrame meshFrame, xaParser xaparser, xaMorphClip morphClip, string exportFormat, bool oneBlendShape)
 {
     Fbx.Exporter.ExportMorph(path, xxparser, meshFrame, morphClip, xaparser, exportFormat, oneBlendShape);
 }
Ejemplo n.º 42
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);
            }
        }