Created
May 18, 2019 15:57
-
-
Save deccer/49f430179d66190b1085f7522b99e29e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void GetVerticesAndIndicesFromModel(Model model, out Vector3[] vertices, out int[] indices) | |
{ | |
var verticesList = new List<Vector3>(); | |
var indicesList = new List<int>(); | |
var transforms = new Matrix[model.Bones.Count]; | |
model.CopyAbsoluteBoneTransformsTo(transforms); | |
Matrix transform; | |
foreach (ModelMesh mesh in model.Meshes) | |
{ | |
transform = Matrix.Identity; | |
AddMesh(mesh, transform, verticesList, indicesList); | |
} | |
vertices = verticesList.ToArray(); | |
indices = indicesList.ToArray(); | |
} | |
static void AddMesh(ModelMesh modelMesh, Matrix transform, List<Vector3> vertices, IList<int> indices) | |
{ | |
foreach (var meshPart in modelMesh.MeshParts) | |
{ | |
var startIndex = vertices.Count; | |
var meshPartVertices = new Vector3[meshPart.NumVertices]; | |
var stride = meshPart.VertexBuffer.VertexDeclaration.VertexStride; | |
meshPart.VertexBuffer.GetData(meshPart.VertexOffset * stride, meshPartVertices, 0, meshPart.NumVertices, stride); | |
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 i = 0; k < meshIndices.Length; i++) | |
{ | |
indices.Add(startIndex + meshIndices[i]); | |
} | |
} | |
else | |
{ | |
var meshIndices = new ushort[meshPart.PrimitiveCount * 3]; | |
meshPart.IndexBuffer.GetData(meshPart.StartIndex * 2, meshIndices, 0, meshPart.PrimitiveCount * 3); | |
for (int i = 0; i < meshIndices.Length; i++) | |
{ | |
indices.Add(startIndex + meshIndices[i]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment