public static void AddMesh(ModelMesh collisionModelMesh, Matrix transform, List<Vector3> vertices, IList<int> indices)
{
foreach (ModelMeshPart meshPart in collisionModelMesh.MeshParts)
{
int startIndex = vertices.Count;
var meshPartVertices = new Vector3[meshPart.NumVertices];
//Grab position data from the mesh part.
int stride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
meshPart.VertexBuffer.GetData(
meshPart.VertexOffset * stride,
meshPartVertices,
0,
meshPart.NumVertices,
stride);
//Transform it so its vertices are located in the model's space as opposed to mesh part space.
Vector3.Transform(meshPartVertices, ref transform, meshPartVertices);
vertices.AddRange(meshPartVertices);
if (meshPart.IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits)
{
var meshIndices = new int[meshPart.PrimitiveCount * 3];
meshPart.IndexBuffer.GetData(meshPart.StartIndex * 4, meshIndices, 0, meshPart.PrimitiveCount * 3);
for (int k = 0; k < meshIndices.Length; k++)
{
indices.Add(startIndex + meshIndices[k]);
}
}
else
{
var meshIndices = new ushort[meshPart.PrimitiveCount * 3];
meshPart.IndexBuffer.GetData(meshPart.StartIndex * 2, meshIndices, 0, meshPart.PrimitiveCount * 3);
for (int k = 0; k < meshIndices.Length; k++)
{
indices.Add(startIndex + meshIndices[k]);
}
}
}
}