djm-web

Hi,

I'm trying to save out and reload a list of vector3's that contain the positions of each triangle vertex in a mesh.

I've followed the 'saving out data' threads and I have the following content processor:

[ContentProcessor]

public class MyProcessor : ModelProcessor

{

public override ModelContent Process(NodeContent input, ContentProcessorContext context)

{

ModelContent model = base.Process(input, context);

foreach (ModelMeshContent mesh in model.Meshes)

{

int nNumIndices = mesh.IndexBuffer.Count;

Vector3[] vertices = new Vector3[nNumIndices];

for (int i = 0; i < nNumIndices; i++)

{

int index = (int)mesh.IndexBuffer[i\];

VertexPositionNormalTexture vertex = (VertexPositionNormalTexture).VertexBuffer.VertexData.GetValue(index);

vertices[i\] = vertex.Position;

}

mesh.Tag = vertices;

}

return model;

}

}

 

This gives a casting exception when compiling a mesh. Also if they are stored as strips and not lists this will be incorrect. What would be the correct way to do this and also to access the list of vector3's in game

 

 



Re: XNA Game Studio Express Saving out Vector3's

Shawn Hargreaves - MSFT

The content pipeline always works with indexed triangle lists, so you don't need to worry about other formats.

I think your problem is that VertexBuffer.VertexData is an array of bytes holding the processed vertex information, so you can't just look up a byte and cast this to a VertexPositionNormalTexture.

I think the easiest way to do this, rather than scanning the processed output from the ModelProcessor, would be to look at the incoming NodeContent information. That is in a much higher level format that will be a lot easier to extract useful information from. Specifically you need to:

- Recurse down the tree of NodeContent objects, using their Children properties
- Examine each NodeContent to see if it is a MeshContent subclass
- If so, loop over it's Geometry collection (which holds GeometryContent instances)
- Inside each GeometryContent examine the Indices property, and use that to index into the Vertices.Positions collection.

That will give you direct access to Vector3 position data without any nasty casting to or from byte arrays.





Re: XNA Game Studio Express Saving out Vector3's

Derek Morris

Thanks, that's just what I need. However, I'm still having a bit of trouble getting the loop right as the CLR debugger is saying 'Cannot evaluate expression because the code of the current method is optimized.' when I attempt to look into most of the fields.

Could you show me an example loop to get from NodeContent input through to the index and vertex position lists for each mesh





Re: XNA Game Studio Express Saving out Vector3's

Shawn Hargreaves - MSFT

Untested:

void ScanNode(NodeContent node)
{
MeshContent mesh = node as MeshContent;

if (mesh != null)
{
foreach (GeometryContent geometry in mesh.Geometry)
{
for (int i = 0; i < geometry.Indices.Count; i += 3)
{
int index1 = geometry.Indices[i + 0];
int index2 = geometry.Indices[i + 1];
int index3 = geometry.Indices[i + 2];

Vector3 vertex1 = geometry.Vertices.Positions[index1];
Vector3 vertex2 = geometry.Vertices.Positions[index2];
Vector3 vertex3 = geometry.Vertices.Positions[index3];

// these three vertices make up one triangle
}
}
}

foreach (NodeContent child in node.Children)
ScanNode(child);
}






Re: XNA Game Studio Express Saving out Vector3's

Derek Morris

This worked great btw, thanks Shawn :)