Пример #1
0
        void ParseObject(XmlReader xml, ParsedXmlModel data, ref XmlModelObject obj)
        {
            int depth = xml.Depth;

            Vector3[]  positions = null;
            Vector3[]  normals   = null;
            MeshFace[] faces     = null;

            Vector2[] texVtx = null;
            Index3i[] texIdx = null;

            VertexWeight[] vtxWeights = null;

            #region 从Xml中读取数据
            while (xml.Read() && xml.Depth > depth)
            {
                if (xml.IsStartElement() && !xml.IsEmptyElement)
                {
                    switch (xml.Name)
                    {
                    case "Parent":
                        obj.ID = int.Parse(xml.GetAttribute("ID"));
                        break;

                    case "LocalTM":
                        obj.LocalTM = ParseMatrix(xml);
                        break;

                    case "WorldTM":
                        obj.WorldTM = ParseMatrix(xml);
                        break;

                    case "BoundingBox":
                        obj.AABB = ParseBoundingBox(xml);
                        break;

                    case "Vertex":
                        positions = ParseMeshVector3Array(xml);
                        break;

                    case "VertexNormal":
                        normals = ParseMeshVector3Array(xml);
                        for (int i = 0; i < normals.Length; i++)
                        {
                            normals[i].Normalize();
                        }
                        break;

                    case "TexVertex":
                        texVtx = ParseMeshVector2Array(xml);
                        break;

                    case "TriIndex":
                        faces = ParseMeshFaces(xml);
                        break;

                    case "TexIndex":
                        texIdx = ParseTexIndex(xml, faces.Length);
                        break;

                    case "VertexWeight":
                        vtxWeights = ParseVertexWeightArray(xml, positions.Length);
                        break;

                    case "Key":
                        obj.BoneData = ParseBoneData(xml);
                        break;
                    }
                }
            }

            #endregion

            if (obj.Type == XmlModelObjectType.Mesh)
            {
                FastList <VertexElement> elementsList = new FastList <VertexElement>();
                int ofs = 0;

                if (positions != null)
                {
                    VertexElement e = new VertexElement(ofs, VertexElementFormat.Vector3, VertexElementUsage.Position);
                    elementsList.Add(e);
                    ofs += e.Size;
                }
                if (normals != null)
                {
                    VertexElement e = new VertexElement(ofs, VertexElementFormat.Vector3, VertexElementUsage.Normal);
                    elementsList.Add(e);
                    ofs += e.Size;
                }
                if (texVtx != null)
                {
                    VertexElement e = new VertexElement(ofs, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0);
                    elementsList.Add(e);
                    ofs += e.Size;
                }
                if (vtxWeights != null)
                {
                    VertexElement e = new VertexElement(ofs, VertexElementFormat.Byte4, VertexElementUsage.BlendWeight, 0);
                    elementsList.Add(e);
                    ofs += e.Size;
                }
                VertexElement[] elements = new VertexElement[elementsList.Count];
                Array.Copy(elementsList.Elements, elements, elementsList.Count);

                obj.Mesh = new MeshData((RenderSystem)null);

                MeshData mesh = obj.Mesh;

                mesh.Faces = faces;
                mesh.Name  = obj.Name;

                byte[] buffer = null;
                if (VertexElement.Compare(elements, VertexPBNT1.Elements))
                {
                    buffer              = BuildVertexPBNT1Data(positions, normals, texVtx, texIdx, vtxWeights, faces);
                    mesh.VertexSize     = sizeof(VertexPBNT1);
                    mesh.VertexElements = VertexPBNT1.Elements;
                }
                else if (VertexElement.Compare(elements, VertexPNT1.Elements))
                {
                    buffer              = BuildVertexPNT1Data(positions, normals, texVtx, texIdx, faces);
                    mesh.VertexSize     = sizeof(VertexPNT1);
                    mesh.VertexElements = VertexPNT1.Elements;
                }
                else if (VertexElement.Compare(elements, VertexPT1.Elements))
                {
                    buffer              = BuildVertexPT1Data(positions, texVtx, texIdx, faces);
                    mesh.VertexSize     = sizeof(VertexPT1);
                    mesh.VertexElements = VertexPT1.Elements;
                }
                else if (VertexElement.Compare(elements, VertexPN.Elements))
                {
                    buffer              = BuildVertexPNData(positions, normals, faces);
                    mesh.VertexSize     = sizeof(VertexPN);
                    mesh.VertexElements = VertexPN.Elements;
                }
                else if (VertexElement.Compare(elements, VertexP.Elements))
                {
                    buffer              = BuildVertexPData(positions, faces);
                    mesh.VertexSize     = sizeof(VertexP);
                    mesh.VertexElements = VertexP.Elements;
                }

                if (buffer != null)
                {
                    if (mesh.VertexSize != 0)
                    {
                        mesh.VertexCount = buffer.Length / mesh.VertexSize;
                    }

                    fixed(byte *src = &buffer[0])
                    {
                        mesh.SetData(src, buffer.Length);
                    }
                }
            }
        }