tomasslavicek

Výpočet BoundingBoxu 3D modelu v XNA 4.0

May 10th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. /// <summary>
  2. /// Precalculate model's bounding box (by its vertices)
  3. /// </summary>
  4. public static BoundingBox GetBoundingBox(Model model)
  5. {
  6.     Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  7.     Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  8.  
  9.     foreach (ModelMesh mesh in model.Meshes)
  10.         foreach (ModelMeshPart meshPart in mesh.MeshParts)
  11.         {
  12.             int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
  13.             int vertexBufferSize = meshPart.NumVertices * vertexStride;
  14.  
  15.             float[] vertexData = new float[vertexBufferSize / sizeof(float)];
  16.             meshPart.VertexBuffer.GetData<float>(vertexData);
  17.  
  18.             for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
  19.             {
  20.                 Vector3 position = new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]);
  21.                 min = Vector3.Min(min, position);
  22.                 max = Vector3.Max(max, position);
  23.             }
  24.         }
  25.  
  26.     return new BoundingBox(min, max);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment