private static ModelMeshData LoadGeometry(ModelData data, ModelBoneData parentBoneData, Collada.ColladaGeometry geometry) { ModelMeshData modelMeshData = new ModelMeshData(); data.Meshes.Add(modelMeshData); modelMeshData.Name = parentBoneData.Name; modelMeshData.ParentBoneIndex = data.Bones.IndexOf(parentBoneData); bool flag = false; foreach (Collada.ColladaPolygons polygons in geometry.Mesh.Polygons) { ModelMeshPartData modelMeshPartData = Collada.LoadPolygons(data, polygons); modelMeshData.MeshParts.Add(modelMeshPartData); modelMeshData.BoundingBox = (flag ? BoundingBox.Union(modelMeshData.BoundingBox, modelMeshPartData.BoundingBox) : modelMeshPartData.BoundingBox); flag = true; } return(modelMeshData); }
public ModelData(Stream stream) { BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8); binaryReader.ReadBoolean(); this.Bones.Capacity = binaryReader.ReadInt32(); for (int i = 0; i < this.Bones.Capacity; i++) { ModelBoneData modelBoneData = new ModelBoneData(); this.Bones.Add(modelBoneData); modelBoneData.ParentBoneIndex = binaryReader.ReadInt32(); modelBoneData.Name = binaryReader.ReadString(); modelBoneData.Transform = new Matrix( binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()); } this.Meshes.Capacity = binaryReader.ReadInt32(); for (int j = 0; j < this.Meshes.Capacity; j++) { ModelMeshData modelMeshData = new ModelMeshData(); this.Meshes.Add(modelMeshData); modelMeshData.ParentBoneIndex = binaryReader.ReadInt32(); modelMeshData.Name = binaryReader.ReadString(); modelMeshData.MeshParts.Capacity = binaryReader.ReadInt32(); modelMeshData.BoundingBox = new BoundingBox( new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()), new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle())); for (int k = 0; k < modelMeshData.MeshParts.Capacity; k++) { ModelMeshPartData modelMeshPartData = new ModelMeshPartData(); modelMeshData.MeshParts.Add(modelMeshPartData); modelMeshPartData.BuffersDataIndex = binaryReader.ReadInt32(); modelMeshPartData.StartIndex = binaryReader.ReadInt32(); modelMeshPartData.IndicesCount = binaryReader.ReadInt32(); modelMeshPartData.BoundingBox = new BoundingBox( new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()), new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle())); } } this.Buffers.Capacity = binaryReader.ReadInt32(); for (int l = 0; l < this.Buffers.Capacity; l++) { ModelBuffersData modelBuffersData = new ModelBuffersData(); this.Buffers.Add(modelBuffersData); VertexElement[] array = new VertexElement[binaryReader.ReadInt32()]; for (int m = 0; m < array.Length; m++) { array[m] = new VertexElement(binaryReader.ReadInt32(), (VertexElementFormat)binaryReader.ReadInt32(), binaryReader.ReadString()); } modelBuffersData.VertexDeclaration = new VertexDeclaration(array); modelBuffersData.Vertices = binaryReader.ReadBytes(binaryReader.ReadInt32()); modelBuffersData.Indices = binaryReader.ReadBytes(binaryReader.ReadInt32()); } }
private XElement GetGeometry(ModelData model, ModelMeshData data) { ModelMeshPartData modelMeshPartData = data.MeshParts[0]; ModelBoneData modelBoneData = model.Bones[data.ParentBoneIndex]; string name = data.Name; ModelBuffersData modelBuffersData = model.Buffers[modelMeshPartData.BuffersDataIndex]; int indicesCount = modelMeshPartData.IndicesCount; VertexDeclaration vertexDeclaration = modelBuffersData.VertexDeclaration; byte[] array = new byte[indicesCount * 32]; using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(modelBuffersData.Indices))) { binaryReader.BaseStream.Position = (long)(modelMeshPartData.StartIndex * 2); for (int i = 0; i < indicesCount; i++) { short num = binaryReader.ReadInt16(); Buffer.BlockCopy(modelBuffersData.Vertices, (int)(num * 32), array, i * 32, 32); } } List <Vector3> list = new List <Vector3>(); List <Vector3> list2 = new List <Vector3>(); float[] array2 = new float[indicesCount * 2]; int[] array3 = new int[indicesCount * 3]; using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(array))) { foreach (VertexElement vertexElement in vertexDeclaration.VertexElements) { if (vertexElement.Semantic.StartsWith("POSITION")) { Vector3[] array4 = new Vector3[3]; for (int j = 0; j < indicesCount; j++) { binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * j + vertexElement.Offset); Vector3 vector = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()); if (!list.Contains(vector)) { list.Add(vector); } int num2 = j % 3; array4[num2] = vector; if (num2 == 2) { array3[j * 3 - 6] = list.IndexOf(array4[0]); array3[j * 3 - 3] = list.IndexOf(array4[2]); array3[j * 3] = list.IndexOf(array4[1]); } } } else if (vertexElement.Semantic.StartsWith("NORMAL")) { Vector3[] array5 = new Vector3[3]; for (int k = 0; k < indicesCount; k++) { binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * k + vertexElement.Offset); Vector3 vector2 = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()); if (!list2.Contains(vector2)) { list2.Add(vector2); } int num3 = k % 3; array5[num3] = vector2; if (num3 == 2) { array3[k * 3 - 5] = list2.IndexOf(array5[0]); array3[k * 3 - 2] = list2.IndexOf(array5[2]); array3[k * 3 + 1] = list2.IndexOf(array5[1]); } } } else if (vertexElement.Semantic.StartsWith("TEXCOORD")) { for (int l = 0; l < indicesCount; l++) { binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * l + vertexElement.Offset); array2[l * 2] = binaryReader.ReadSingle(); array2[l * 2 + 1] = 1f - binaryReader.ReadSingle(); if (l % 3 == 2) { array3[l * 3 - 4] = l - 2; array3[l * 3 - 1] = l; array3[l * 3 + 2] = l - 1; } } } } } XName name2 = this.colladaNS + "mesh"; object[] array6 = new object[5]; XElement source = this.GetSourceArray(name, "-mesh-positions", string.Join(" ", list.ConvertAll <string>((Vector3 v) => string.Format("{0} {1} {2}", v.X.ToString("R"), v.Y.ToString("R"), v.Z.ToString("R")))), list.Count * 3, 3, this.XYZParam()); array6[0] = source; XElement source2 = this.GetSourceArray(name, "-mesh-normals", string.Join(" ", list2.ConvertAll <string>((Vector3 v) => string.Format("{0} {1} {2}", v.X.ToString("R"), v.Y.ToString("R"), v.Z.ToString("R")))), list2.Count * 3, 3, this.XYZParam()); array6[1] = source2; XElement source3 = this.GetSourceArray(name, "-mesh-map", string.Join(" ", from f in array2 select f.ToString("R")), array2.Length, 2, this.STParam()); array6[2] = source3; XElement source4 = new XElement(this.colladaNS + "vertices", new object[] { new XAttribute("id", name + "-mesh-vertices"), this.GetInput("POSITION", source) }); array6[3] = source4; array6[4] = new XElement(this.colladaNS + "triangles", new object[] { new XAttribute("count", indicesCount / 3), this.GetInput("VERTEX", source4, 0), this.GetInput("NORMAL", source2, 1), this.GetInput("TEXCOORD", source3, 2), new XElement(this.colladaNS + "p", string.Join <int>(" ", array3)) }); XElement xelement = new XElement(name2, array6); XElement result; this.root.Element(this.colladaNS + "library_geometries").Add(result = new XElement(this.colladaNS + "geometry", new object[] { new XAttribute("id", name + "-mesh"), new XAttribute("name", name), xelement })); return(result); }