• Advertisement
Sign in to follow this  

Loading content efficiently in XNA?

This topic is 1840 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

I'm having a couple of problems (or potential problems) that I'll try to describe with examples:

 

1) I have a couple of different models that all use the same texture map. If I load each model by the standard method,

I assume the texture map is loaded automatically several times. Assuming this is true (is it?), what's the best way to make sure

the texture is loaded only once? Have the models uvw mapped but with no texture and then load the texture separately and

pass it to a custom shader?

 

2) A similar problem. Since the texture associated with a model seems to be loaded automatically along with it,

what's the best way to load a model that you intend to render with a custom shader? Is there a way to grab the model's

texture from within the shader so it doesn't need to be loaded separately and then passed to the effect? (as that seems

to suggest you're loading the texture twice???)

 

Hope someone can shed light in this corner of XNA.

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

For the issue with  loading multiple textures, I would just have a TextureManager class of some sort and have all textures loaded in there once, that class can then be accessed for all textures.

Share this post


Link to post
Share on other sites

1. If you use ContentManager it will make sure you only an asset once. So if multiple models reference the same texture, the texture will only be loaded once and shared among your models. You don't need to do anything special on your end to make that work.

 

2. It's been a long time since I worked with XNA, but from what I recall it was possible to "assign" a custom effect to a model. When you did that, it would automatically load the effect (along with any textures used by that effect) when you load the model. Getting this to work requires authoring the model file in a particular way such that the XNA model importer knows that an effect is "assigned", and also what textures to bind for any texture parameters exposed by the effect. Unfortunately I can't really help you with the specifics...like I said it's been a long time.

Share this post


Link to post
Share on other sites
if 2 models point to the same texture, it should only be loaded once.

to access textures loaded with a model you have to iterate through meshes and meshparts. this way you can also grab diffusecolors etc.
to handle multiple textures/colors in one model, you can write a simple MeshTag-class, that contains all the data you need from the BasicEffect and store it in part.Tag.

something like this should do the job:

foreach (ModelMesh mesh in model.Meshes)
foreach (ModelMeshPart part in mesh.MeshParts)
{
MeshTag tag = new MeshTag();
tag.Texture = ((BasicEffect)part.Effect).Texture;
tag.Color = ((BasicEffect)part.Effect).DiffuseColor;
part.Tag = (Object)tag; //not sure if a cast to Object is needed
//additionally you can set a customeffect here
}

when trying to access MeshTag-Data, dont forget the typecast.

hope that helps,
major

Share this post


Link to post
Share on other sites

[quote name='MJP' timestamp='1357284358' post='5017390']
1. If you use ContentManager it will make sure you only an asset once. So if multiple models reference the same texture, the texture will only be loaded once and shared among your models. You don't need to do anything special on your end to make that work.
[/quote]

 

Glad to hear it. I'd hoped XNA would've been intelligently written!

 

[quote name='majorbrot' timestamp='1357312548' post='5017438']
something like this should do the job:

foreach (ModelMesh mesh in model.Meshes)
foreach (ModelMeshPart part in mesh.MeshParts)
{
MeshTag tag = new MeshTag();
tag.Texture = ((BasicEffect)part.Effect).Texture;
tag.Color = ((BasicEffect)part.Effect).DiffuseColor;
part.Tag = (Object)tag; //not sure if a cast to Object is needed
//additionally you can set a customeffect here
}

[/quote]

 

Thanks, looks easy enough. I'll give it a try.

Share this post


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

  • Advertisement