Exemple #1
0
        public XDocument ConvertToXml()
        {
            var doc  = new XDocument();
            var root = new XElement("LddGeometry");

            root.AddNumberAttribute("VertexCount", VertexCount);
            root.AddNumberAttribute("IndexCount", IndexCount);
            root.AddBooleanAttribute("IsTextured", IsTextured);
            root.AddBooleanAttribute("IsFlexible", IsFlexible);
            doc.Add(root);


            //var vertices = root.AddElement("Vertices");
            root.Add(new XComment("Format: X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3 ..."));
            var positions = root.AddElement("Positions");

            positions.Value = string.Join(" ", Vertices.Select(v => FormatVector3(v.Position)));

            root.Add(new XComment("Format: X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3 ..."));
            var normals = root.AddElement("Normals");

            normals.Value = string.Join(" ", Vertices.Select(v => FormatVector3(v.Normal)));

            if (IsTextured)
            {
                root.Add(new XComment("Format: X1 Y1 X2 Y2 X3 Y3 ..."));
                var uvs = root.AddElement("UVs");
                uvs.Value = string.Join(" ", Vertices.Select(v => FormatVector2(v.TexCoord)));
            }
            RebuildIndices();
            var indices = root.AddElement("Indices");

            indices.Value = string.Join(" ", Indices.Select(x => x.VIndex));

            if (IsFlexible)
            {
                root.Add(new XComment("Format: VertexIndex BoneID Weight ..."));
                var bones = root.AddElement("BoneWeights");
                for (int i = 0; i < VertexCount; i++)
                {
                    var vert = Vertices[i];
                    foreach (var bw in vert.BoneWeights)
                    {
                        bones.Value += string.Format(CultureInfo.InvariantCulture,
                                                     "{0} {1} {2} ", i, bw.BoneID, bw.Weight);
                    }
                }
                bones.Value = bones.Value.TrimEnd();
            }

            if (CheckHasRoundEdgeData())
            {
                //root.Add(new XComment("Format: Index{n}Coord{1,6}.X Index{n}Coord{1,6}.Y"));
                var outlines = root.AddElement("Outlines");
                var coords   = Indices.SelectMany(x => x.RoundEdgeData.Coords.Take(6)).ToList();
                outlines.Value = string.Join(" ", coords.Select(v => FormatVector2(v)));
            }

            return(doc);
        }