Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Precalculate model's bounding box (by its vertices)
- /// </summary>
- public static BoundingBox GetBoundingBox(Model model)
- {
- Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
- Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
- foreach (ModelMesh mesh in model.Meshes)
- foreach (ModelMeshPart meshPart in mesh.MeshParts)
- {
- int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
- int vertexBufferSize = meshPart.NumVertices * vertexStride;
- float[] vertexData = new float[vertexBufferSize / sizeof(float)];
- meshPart.VertexBuffer.GetData<float>(vertexData);
- for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
- {
- Vector3 position = new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]);
- min = Vector3.Min(min, position);
- max = Vector3.Max(max, position);
- }
- }
- return new BoundingBox(min, max);
- }
Advertisement
Add Comment
Please, Sign In to add comment