Having just spent the better part of 2 hours trying to figure out how to generate a bounding box from a mesh, I thought that I’d give a quick how to here.
Note, a lot of the information was obtained from the XNA forums – I’ve just collated everything into one place.
First I would say that I would have expected XNA to have provided this functionality out of the box as its pretty much a basic requirement but hey ho.
Anyway, what you need to do is first create a new project seperate from your app. This project will just create a handler from the content pipeline.
You’ll then have to add a reference to the pipeline assembly:
Project => Add Reference => .Net (tab)
Scroll down and select the Microsoft.Xna.Framework.Content.Pipeline assembly and click
You want a class that looks like:
using System;
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.
Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.
Processors;
namespace XNAInvadersLoader
{
[ContentProcessor]
public class MyModelProcessor : ModelProcessor
{
float minX = float.MaxValue;
float minY = float.MaxValue;
float minZ = float.MaxValue;
float maxX = float.MinValue;
float maxY = float.MinValue;
float maxZ = float.MinValue;
public override ModelContent Process(
NodeContent input,
ContentProcessorContext context)
{
NodeContentCollection ncc = input.Children;
parseChildren(ncc);
ModelContent mc2 = base.Process(input,
context);
mc2.Tag = new BoundingBox(
new Vector3(
(float)minX,
(float)minY,
(float)minZ),
new Vector3(
(float)maxX,
(float)maxY,
(float)maxZ));
return mc2;
}
private void parseChildren(
NodeContentCollection http://www.xanaxlowprice.com ncc)
{
foreach (NodeContent nc in ncc)
{
if (nc is MeshContent)
{
MeshContent mc = (MeshContent)nc;
foreach (Vector3 basev in mc.Positions)
{
Vector3 v = basev;
if (v.X maxX)
maxX = v.X;
if (v.Y > maxY)
maxY = v.Y;
if (v.Z > maxZ)
maxZ = v.Z;
}
}
else
parseChildren(nc.Children);
}
}
}
}
This creates a pipeline loader for Models that stores a BoundingBox in the Tag property of the Model.
Build this (this will create a DLL that you will need to add into your app – below) and then switch to your app.
Double click on the Properties item in the Solution Explorer for your app.
Select the Content Pipeline tab Click the Add button and select the DLL that you built above it.
Now add your meshes/models to your project.
Then, for each mesh select it in the Solution Explorer and view its properties.
In the Property Window, select the Content Processor drop down and select the content processor you wish to use when loading this mesh (in this example you would select the MyModelProcessor).
Then, load your model in the standard way:
Model myModel = content.Load("ContentMeshesmodel");
and you can the get the BoundingBox from the model using:
BoundingBox BB = (BoundingBox)myModel.Tag;