CodePfo

Is it possible to edit the vertices contained in ModelMesh Like could I get the vertices, alter their position and set the data back in the collection I ask this because for the terrain object in my game, I want it to be deformable. I've been playing with the content pipeline and my custom importer / processor / writer writes the height data as a byte array. When the object is read back, an index buffer and vertex buffer are generated in the terrain class. The vertex buffer is specified with ResourceUsage.Dynamic which to my knowledge is the only way to make the buffer read / write enabled. I'm considering altering my custom content pipeline objects to create ModelContent objects instead of my current approach, but I will abandon that idea pending whether or not I can edit a Model's vertices at runtime.


Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

leclerc9

Yes, you can, but you have to create your own processor that extends (is a subclass of) the ModelProcessor.  One way is to pass the vertex data from the content pipeline to your game, usually by using the Tag object attached to each mesh.  For example, you could override the Process method and use this code as the method body:

ModelContent model = base.Process(input,context);
foreach (ModelMeshContent mesh in model.Meshes)
    mesh.Tag=mesh.VertexBuffer.VertexData;
return model;

Once you load the model from the content pipeline, you'll have to convert the bytes into whatever vertex format each mesh uses.  Here is a generic, but inefficient, method that will convert to any vertex type:

public static T[] Convert<T>(byte[] data, int vertexSize, GraphicsDevice device) where T : struct
{
    T[] verts =
new T[data.Length / vertexSize];
   
using (VertexBuffer vb = new VertexBuffer(device, data.Length, ResourceUsage.None))
    {
        vb.SetData<
byte>(data);
        vb.GetData<T>(verts);
    }
   
return verts;
}





Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

CodePfo

I don't want to extend ModelProcessor, I want to write a custom processor that reads an XML file describing terrain attributes and uses a heightmap to generates a Model using ModelBuilder myself (shouldn't be too difficult). In my reader class, I want to call input.ReadExternalReference<Model>(path) to get that model back. The only problem is whether or not I can get the ModelMesh's verex buffer, edit the vertex positions and write them back into the model so I can deform them (for things like craters) at runtime in my game. So ideally for this to work, it should be possible to do this for any Model I loaded through the ContentManager. When I played around with MDX 1.0, I could not do this with the mesh object because of the way it was setup and so I had to implement a custom object that used a VertexBuffer and IndexBuffer (which is how I have it setup now). Rather than go through the hassle of doing all of that and testing it, I'll ask in the forum for a thumbs up or thumbs down from someone who has tried it (or someone form MS that knows off the top of their head).




Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

leclerc9

 CodePfo wrote:
I don't want to extend ModelProcessor, I want to write a custom processor that reads an XML file describing terrain attributes and uses a heightmap to generates a Model using ModelBuilder myself (shouldn't be too difficult).  In my reader class, I want to call input.ReadExternalReference<Model>(path) to get that model back.  The only problem is whether or not I can get the ModelMesh's verex buffer, edit the vertex positions and write them back into the model so I can deform them (for things like craters) at runtime in my game.  So ideally for this to work, it should be possible to do this for any Model I loaded through the ContentManager.  When I played around with MDX 1.0, I could not do this with the mesh object because of the way it was setup and so I had to implement a custom object that used a VertexBuffer and IndexBuffer (which is how I have it setup now).  Rather than go through the hassle of doing all of that and testing it, I'll ask in the forum for a thumbs up or thumbs down from someone who has tried it (or someone form MS that knows off the top of their head).


By ModelBuilder, do you mean MeshBuilder (I don't think there is a ModelBuilder)   If so, MeshBuilder creates a MeshContent object, which is an unprocessed subclass of NodeContent that, when processed, is converted to an instance of ModelMeshContent.  At that point, you already have access to the mesh's vertex data (stored in ModelMeshContent.VertexBuffer.VertexData), so if you dont want to use the tag, you can pass that to your writer and then to your reader (if you dont want to create a new object to manipulate vertex data, then you'll have to put the data in the tag at this point).  Perhaps I am misunderstanding you   This seems like grabbing the vertex buffer data is far less of a hassle than anything else you are doing.




Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

CodePfo

Right, I do mean MeshBuilder. And yes, you are correct that grabbing the vertex data at runtime is a small hassle compared to all the other work I have to implement. Basically I want to do this:

class Terrain
{
// instance constructor from content type reader
public static Terrain Read(ContentTypeReader input)
{
Terrain T = new Terrain();

// read back model and texture data from input...

return T;
}

// this is the function I am wondering about
public AddCrater(Vector2 position, float radius)
{
// get model vertices, apply crater at position, and re-write vertices to model
}

Model terrainModel;
}

My question is after I call AddCrater and mess with the Model's vertex buffer will the changes be visible And the only reason that I ask this is because in MDX 1.0, this was not possible with the Mesh object. Correct me if I am wrong, but I think the VertexBuffers need to be created with the Dynamic usage flag, and I don't think they are with the Model class. When using MeshBuilder, you don't have the option of specifying VertexBuffer creation attributes (or do you ) in the ContentPipeline (and if you can't, this would be a handy feature to add). I hope this is a bit clearer, thanks for trying to answer my questions.




Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

Leaf.

ModelMesh is not designed to have the vertex buffer manipulated by the CPU, the VertexBuffers are created with the WriteOnly flag set. You would probably be better off with your own runtime data structure - perhaps have a data structure that you can easily modify and then generate new vertex buffers from instead of reading data in and out of VertexBuffers.

Cheers,
Leaf.





Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

CodePfo

That's what I thought, thanks Leaf. I guess I'll just keep my current implementation as is. I only wanted to generate a Model in the hopes I could offload as much content prep time in the ContentPipeline / building phase as possible, but the time it takes to build a terrain mesh from an image is negligible.




Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

Fluxtah

Somebody was discussing terrain systems in another thread and mentioned methods of using heighmaps in shaders to transform a peice of flat geometry into a terrain section, it sounded very interesting, not sure if something like that is worth considering.





Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

r2d2-proton

I need to bump this. . .I realize it's a little old.

It seems that the framework is taking about 155ms for each frame, or 6 frames a second. Obviously I need to speed this up.

I would like to state that the same geometry runs much faster under DirectX 9 and OpenGL. If I had a lot of my own code to work with, then I could look for performance areas. However, given that there are few lines of my own it leads me to believe that something in the framework is causing the problem.

Of course you might be asking what does this have to do with accessing vertices Well, I thought I'd continue to load the model through the normal ContentManager, and augment the data later. As pointed out already, I'm stumbling into the WriteOnly problem.

I'll keep reading as I don't understand the framework well enough. . . .but from what I've experienced over this weekend. . .it feels like I'm programming with TWO THUMBS!

For performance reasons, I will scale an object at the vertex level once and render it ad nauseam. Likewise, morphing geometry is normal.

So, I guess, what I'd like to know is should I code the way I do with C++ and D3D, and ignore the framework (if possible)

If so, will there be any performance penalty

Thanx for the help.






Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

cronholio

I ran into the same problem with this method, just this weekend. My new solution, which I've not yet implemented, is going to to be to shove the positions into the vertexColor Channel (which I am not using) and then send them through a vertex shader to do the morph.



Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

leclerc9

I believe there was a post recently about this, but I can't seem to find it. You can also alter the vertices by storing the transformation data in a texture and do it inside the shader. I can't find the link, but I would suggest un-marking the post as an answer so someone can help post it or confirm its non-existence.



Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

Fluxtah

I would have thought that using vertex color in a shader, the components would be clamped in the range of 0-1. Using multiple streams could be a way to accomplish vertex morphing.





Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

Leaf.

The texture coordinates are usually a good place to store extra (non standard semantics) information for vertices but Fluxtah's suggestion of using multiple vertex streams would work well for tweening.

Cheers,
Leaf.






Re: XNA Game Studio Express Can you edit a Model's vertices at runtime?

cronholio

They may be, I don't know, I'm going to find out though. It's a trick we used to use in DCC apps before they started supporting arbitrary vertex data, just shove whatever you want into a color. If it doesn't work I'm going to try making a secondary UVW set, or FVFData or something (I'm using a custom .x exporter for my app).

 

I had read about mutli-streaming, I'm going to try it eventually, if this doesn't work. I'm just looking for the fastest way to get it done.

 

That recent thread, btw, was me.