Jump to content
  • Advertisement
Sign in to follow this  
anhoppe

Changing textures of a model at runtime (XNA)

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I loaded a height map as terrain model in an XNA game project via a custom content pipeline, everything works fine. Now I want to change textures in the model at runtime. The background: the player should be able to place roads on the terrain map. I figured the best way would be to change the terrain itself by altering the textures on the map rather then adding road models. Can anybody tell me how to access the textures at runtime (or an alternative way how to integrate user-defined roads) ? -- Thank you

Share this post


Link to post
Share on other sites
Advertisement
Are you using your own custom Effect or BasicEffect? With BasicEffect, you just assign a new Texture2D to the Texture property of the BasicEffect being used for each ModelMeshPart. For you own custom Effect, you need to get the EffectParameter that corresponds the the texture you've declared in your HLSL and then use SetValue to set the Texture2D you want.

Share this post


Link to post
Share on other sites
Thanks for your answer MJP,

currently I'm using BasicEffects. I haven't tried it yet, but doesn't the texture property of the ModelMeshPart alter the texture for the whole model part? My terrain is one ModelMeshPart, the triangles in the mesh have different textures. How do I change the texture of a single triangle?

Share this post


Link to post
Share on other sites
Quote:
Original post by anhoppe
How do I change the texture of a single triangle?


You don't. Each triangle that you want to have a separate texture for will need to be in its own ModelMeshPart. This means that every triangle that can have a custom texture needs to be a separate material. If you have 10,000 triangles in the mesh that can be drawn on, you'll need 10,000 materials which would result in 10,000 separate draw calls per frame. This will kill performance.

The other alternative is to draw the model manually by accessing the IndexBuffer and VertexBuffer of the ModelMesh, skipping over the triangles that have a special texture, then drawing each triangle skipped with its own texture. Depending on how many triangles have roads, this could also kill performance, if you end up with too many draw calls.

Yet another alternative is to render your map in a shader by using texture splatting. First, the base texture is rendered for ground. A second gray scale texture is used as a mask, indicating where roads will be placed. This texture is stretched across all triangles in the mesh, but is not rendered. Instead, the shader will look at the mask, and depending on the brightness, it will draw with a third texture, your road texture. This method results in one draw call (assuming the base texture is the same for the entire terrain), and the mask texture only needs to be updated when the user adds or removes a road.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!