public static void EvaluateNormals(ModelMeshData mesh) { Vector3[] normals = new Vector3[mesh.Vertices.Count]; for (int i = 0; i != mesh.Vertices.Count; ++i) { normals[i] = Vector3.Zero; } Action <int, int, int, int> ProcessTriangle = delegate(int partIdx, int idx1, int idx2, int idx3) { List <short> indices = mesh.MeshParts[partIdx].Indices; int[] idx = { indices[idx1], indices[idx2], indices[idx3] }; Vector3 side1 = mesh.Vertices[idx[1]] - mesh.Vertices[idx[0]]; Vector3 side2 = mesh.Vertices[idx[2]] - mesh.Vertices[idx[0]]; Vector3 norm = Vector3.Cross(side1, side2); norm.Normalize(); for (int i = 0; i != 3; ++i) { normals[idx[i]] += norm; } }; if (mesh.TriangleStrip) { for (int j = 0; j != mesh.MeshParts.Count; ++j) { for (int i = 2; i < mesh.MeshParts[j].Indices.Count; ++i) { ProcessTriangle(j, i - 2, i - 1, i); } } } else { for (int j = 0; j != mesh.MeshParts.Count; ++j) { for (int i = 0; i < mesh.MeshParts[j].Indices.Count; i += 3) { ProcessTriangle(j, i, i + 1, i + 2); } } } for (int i = 0; i != normals.Length; ++i) { normals[i].Normalize(); normals[i] = normals[i]; } mesh.Normals = new List <Vector3>(normals); }
public static ModelData ReadModelData(Stream stream) { EngineBinaryReader engineBinaryReader = new EngineBinaryReader(stream); ModelData modelData = new ModelData(); modelData.Bones.Capacity = engineBinaryReader.ReadInt32(); for (int i = 0; i < modelData.Bones.Capacity; i++) { ModelBoneData modelBoneData = new ModelBoneData(); modelData.Bones.Add(modelBoneData); modelBoneData.ParentBoneIndex = engineBinaryReader.ReadInt32(); modelBoneData.Name = engineBinaryReader.ReadString(); modelBoneData.Transform = engineBinaryReader.ReadMatrix(); } modelData.Meshes.Capacity = engineBinaryReader.ReadInt32(); for (int j = 0; j < modelData.Meshes.Capacity; j++) { ModelMeshData modelMeshData = new ModelMeshData(); modelData.Meshes.Add(modelMeshData); modelMeshData.ParentBoneIndex = engineBinaryReader.ReadInt32(); modelMeshData.Name = engineBinaryReader.ReadString(); modelMeshData.MeshParts.Capacity = engineBinaryReader.ReadInt32(); modelMeshData.BoundingBox = engineBinaryReader.ReadBoundingBox(); for (int k = 0; k < modelMeshData.MeshParts.Capacity; k++) { ModelMeshPartData modelMeshPartData = new ModelMeshPartData(); modelMeshData.MeshParts.Add(modelMeshPartData); modelMeshPartData.BuffersDataIndex = engineBinaryReader.ReadInt32(); modelMeshPartData.StartIndex = engineBinaryReader.ReadInt32(); modelMeshPartData.IndicesCount = engineBinaryReader.ReadInt32(); modelMeshPartData.BoundingBox = engineBinaryReader.ReadBoundingBox(); } } modelData.Buffers.Capacity = engineBinaryReader.ReadInt32(); for (int l = 0; l < modelData.Buffers.Capacity; l++) { ModelBuffersData modelBuffersData = new ModelBuffersData(); modelData.Buffers.Add(modelBuffersData); VertexElement[] array = new VertexElement[engineBinaryReader.ReadInt32()]; for (int m = 0; m < array.Length; m++) { array[m] = new VertexElement(engineBinaryReader.ReadInt32(), (VertexElementFormat)engineBinaryReader.ReadInt32(), engineBinaryReader.ReadString()); } modelBuffersData.VertexDeclaration = new VertexDeclaration(array); modelBuffersData.Vertices = engineBinaryReader.ReadBytes(engineBinaryReader.ReadInt32()); modelBuffersData.Indices = engineBinaryReader.ReadBytes(engineBinaryReader.ReadInt32()); } return(modelData); }