[SOLVED] Custom Pipeline importer ModelContent Vertex info

I am trying to write a custom importer which will create and add a bounding box to a model at load time. I am having difficulty converting all the original Model BoundingBox code over to ModelContent.

Specifically, this part:

            foreach (ModelMeshPart meshPart in mesh.MeshParts)
                // Vertex buffer parameters
                int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
                int vertexBufferSize = meshPart.NumVertices * vertexStride;

                // Get vertex data as float
                float[] vertexData = new float[vertexBufferSize / sizeof(float)];

                // Iterate through vertices (possibly) growing bounding box, all calculations are done in world space
                for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
                    Vector3 vertexVector = new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]);

                    min = Vector3.Min(min, Vector3.Transform(vertexVector, _boneTransforms[mesh.ParentBone.Index]));

                    max = Vector3.Max(max, Vector3.Transform(vertexVector, _boneTransforms[mesh.ParentBone.Index]));

How do I convert the VertexData[byte] from a ModelMeshPartContent (from the pipeline) into a VertexData[float] which is what we normally get from a ModelMeshPart?

Do I assume every 4 bytes in the VertexData byte array is 1 float?


Looks like assuming every 4 bytes is a float works… got this part of my code working.

Take a look at the source in MonoGame.Framework.Content.Pipeline/Graphics/ModelProcessor.cs. The ProcessMesh() method in there shows how it calculates the bounding sphere. You can create your own custom processor that derives from ModelProcessor. Iterate over all nodes, and for all ModelMeshContent nodes, calculate your bounding box and place it in the ModelMeshContent.Tag property. This bounding box will then get written out with the rest of the model content in the XNB file.

At runtime, you can get the bounding box from the ModelMesh’s Tag property.

Thanks for the response!

I did manage to get this working. I was a little confused in thinking that I needed to create both an importer and a processor, so I did both. Once I saw that you actually set the importer separate from the processor in the pipeline tool, I realized that I could have gotten away with just creating the processor… but I guess it’s done now :slightly_smiling: