I'm building some custom content types with the XNA 4.0 content pipeline.
I have a custom ContentTypeWriter and ContentTypeReader for my classes TerrainModelSetContent and TerrainModelSet respectively, which are each the build time and run time classes.
A terrain model set of course includes/is a collection of models used for the parts of a level's terrain, and as such have one or more models serialized sequentially into a single .xnb content file.
Anyway, pretty much any XNA documentation or tutorials out there that I can find (from Microsoft or otherwise) make it pretty clear that XNA already comes with an out-of-the-box Writer and Reader for Models.
So my question is, why is it not preserving any of the actual model data between writing and reading? It only preserves the frivolous extra things like the BoundingSpheres and PrimitiveCounts--none of the actual geometry in the Vertex or Index buffers that it would have to preserve in order to be even remotely useful.
At build time, the TerrainModelSet gets serialized into a .xnb file via the Write(...) function of this class:
[ContentTypeWriter]
public class TerrainModelSetWriter : ContentTypeWriter<TerrainModelSetContent>
{
protected override void Write(ContentWriter output, TerrainModelSetContent value) {
//Write starting TerrainModelSet data...
output.Write(value.GraphicsMeshes.Count); //value.GraphicsMeshes is a dictionary of string-keys and ModelContent-values.
foreach (KeyValuePair<string, ModelContent> item in value.GraphicsMeshes) {
output.Write(item.Key);
//At this point, all geometry data is present in the Vertex and
//Index buffers of the Model item.Value's ModelMesh's
//ModelMeshParts, nice and neat how we would expect it. I made
//sure if this with the debugger.
output.WriteObject<ModelContent>(item.Value);
}
}
//GetRuntimeReader(...) and GetRuntimeType(...) functions are overridden here as well.
}
And the TerrainModelSet is of course deserialized at run-time in the Read(...) method of this corresponding class:
public class TerrainModelSetReader : ContentTypeReader<TerrainModelSet>
{
protected override TerrainModelSet Read(ContentReader input, TerrainModelSet existingInstance) {
if (existingInstance == null)
existingInstance = new TerrainModelSet();
//Read starting TerrainModelSet data...
int numItems = input.ReadInt32();
for (int i = 0; i < numItems; i++) {
string itemName = input.ReadString();
Model m = input.ReadObject<Model>();
//Here, we use the debugger again to check the state of m, and
//find that the XNA Framework Content Pipeline has UTTERLY
//FAILED to preserve ANY of the geomentry data.
//All Vertex and Index buffers in any ModelMeshParts of any
//ModelMeshes of m are null. Not even empty, just null. WTF?
existingInstance.GraphicsMeshes.Add(itemName, m.Meshes[0]);
existingInstance.CollisionMeshes.Add(itemName, CollisionMesh.FromModelMesh(m.Meshes[0]));
}
}
}
The geometry is all there before the Write call--nice and neat in its appropriate buffers. I've checked with the debugger. However, after the Read call, all the buffers in the meshes of m are null. They aren't even empty--just null. What is going on here? Can anyone enlighten me?