void ParseObjectList(XmlReader xml, ParsedXmlModel data) { int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { int id = int.Parse(xml.GetAttribute("ID")); XmlModelObject obj = new XmlModelObject(); obj.Name = xml.ReadString(); data.ObjectTable.Add(id, obj); } } } }
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); } } } }
void ParseBody(XmlReader xml, ParsedXmlModel data) { int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { switch (xml.Name) { case "Info": ParseInfo(xml, data); break; case "Material": ParseMaterials(xml, data); break; case "Object": int id = int.Parse(xml.GetAttribute("ID")); string name = xml.GetAttribute("Name"); string cls = xml.GetAttribute("Class"); XmlModelObject obj = data.ObjectTable[id]; obj.Name = name; obj.ID = id; switch (cls) { case "Dummy": obj.Type = XmlModelObjectType.Dummy; break; case "Editable_mesh": obj.Type = XmlModelObjectType.Mesh; break; case "Biped_Object": obj.Type = XmlModelObjectType.Bone; break; } ParseObject(xml, data, ref obj); break; } for (int i = 0; i < data.ObjectCount; i++) { Dictionary <int, Material> matTable = data.MaterialTable; bool[] useState = new bool[matTable.Count]; XmlModelObject obj = data.ObjectTable[i]; if (obj.Type == XmlModelObjectType.Mesh) { MeshData meshData = obj.Mesh; for (int j = 0; j < meshData.Faces.Length; j++) { int mId = meshData.Faces[j].MaterialIndex; if (mId == -1) { mId = matTable.Count - 1; meshData.Faces[j].MaterialIndex = mId; } useState[mId] = true; } int[] matIdxShift = new int[matTable.Count]; int shifts = 0; List <Material> entMats = new List <Material>(); for (int j = 0; j < matTable.Count; j++) { if (useState[j]) { entMats.Add(matTable[j]); matIdxShift[j] = shifts; } else { shifts++; } } meshData.Materials = new Material[entMats.Count][]; //entMats.ToArray(); meshData.MaterialAnimation = new MaterialAnimationInstance[entMats.Count]; for (int j = 0; j < entMats.Count; j++) { meshData.Materials[j] = new Material[] { entMats[j] }; meshData.MaterialAnimation[j] = new MaterialAnimationInstance(new MaterialAnimation(1, 1)); } for (int j = 0; j < meshData.Faces.Length; j++) { meshData.Faces[j].MaterialIndex -= matIdxShift[meshData.Faces[j].MaterialIndex]; } } } } } }
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); } } } }