• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Problem drawing model with multiple textures in XNA

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1gchewood  Members

Posted 04 March 2013 - 03:44 PM

I have a model with multiple materials/textures in .X format. If I load it and draw with BasicEffect, it renders just fine.

However, I need do it with a custom effect and so far my efforts are in vain. The model is being rendered with just one of the textures being used for all parts (where it shouldn't be).

private void LoadModel(string assetName, out Model model)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
BasicEffect basicEffect = part.Effect as BasicEffect;
if (basicEffect != null)
modelTextures[mesh.GetHashCode()] = basicEffect.Texture;

part.Effect = myEffect;
}
}
}


modelTextures being an array of Texture2Ds.

And then here's my drawing the model:

public void DrawModel()
{
foreach (ModelMesh m in model.Meshes)
{
foreach (Effect e in m.Effects)
{
e.Parameters["world"].SetValue(Matrix.CreateTranslation(0, 100, 0));
e.Parameters["view"].SetValue(view);
e.Parameters["projection"].SetValue(projection);
e.Parameters["colorMap"].SetValue(modelTextures[m.GetHashCode()]);
}
m.Draw();
}
}


This actually works fine for a different model I've got (that someone else made). But not my model!

But since my model renders just fine with the basiceffect, I don't know where the problem is I should be trying to fix?

Worst case scenario, I can break my model up into separate models, each with a single texture. But I prefer a real solution,

any help would be very much appreciated!

Thanks

Edited by gchewood, 04 March 2013 - 03:45 PM.

### #2phil_t  Members

Posted 04 March 2013 - 05:00 PM

Look how you're storing your textures... indexed by ModelMesh's hash instead of ModelMeshPart's. So if you have a model that has two ModelMeshParts with different textures, that would explain the bug you're seeing.

### #3gchewood  Members

Posted 04 March 2013 - 07:53 PM

Aha. How would I adjust it to fix that then? Cos you draw a model by looping through the ModelMeshes don't you. So how can the shader set different values

for 2 different ModelMeshParts?

Edited by gchewood, 04 March 2013 - 08:09 PM.

### #4phil_t  Members

Posted 04 March 2013 - 08:18 PM

Yeah, I see what you mean. Of course, ModelMesh handles this ok with the default BasicEffect, so it can be done.

You have three options:

1) Clone your effect before assigning it to ModelMeshPart.Effect. That creates a unique instance of your effect (which is what I expect each ModelMeshPart has by default... a unique instance of BasicEffect). Assign each effect its appropriate texture. And then you only need to update the parameters that change each frame in your DrawModel method (e.g. view, world, etc...). This is probably the easiest solution; but Clone'ing an effect creates a new GPU resource, so you should remember to call Dispose() on it when you're completely finished with it - you have to manage its lifetime manually since it wasn't loaded through the ContentManager.

2) Avoid using ModelMesh.Draw (which submits a draw call for each ModelMeshPart). Instead loop through all the ModelMeshParts and use DrawIndexedPrimitives after setting the corresponding Effect parameters and Apply()'ing the changes. This way you can still keep a single Effect instance. ModelMeshPart and its ModelMesh parent should have all the necessary information needed for the parameters to DrawIndexedPrimitives, but if you're not familiar with DrawIndexedPrimitives it might take a while to figure it all out. (DrawIndexedPrimitives is essentially what ModelMesh.Draw uses internally).

3) Change your model so it has two ModelMeshes instead of one ModelMesh and two ModelMeshParts :-).

Edited by phil_t, 04 March 2013 - 08:23 PM.

### #5gchewood  Members

Posted 04 March 2013 - 08:42 PM

Thanks phil_t,  a nice encyclopedic answer!

I'll probably just go with #3 cos it's the only one I know how to do without further effort!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.