Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Loading content efficiently 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.

  • You cannot reply to this topic
4 replies to this topic

#1 gchewood   Members   -  Reputation: 236

Like
0Likes
Like

Posted 03 January 2013 - 08:03 PM

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.



Sponsor:

#2 Xanather   Members   -  Reputation: 712

Like
0Likes
Like

Posted 04 January 2013 - 12:14 AM

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.



#3 MJP   Moderators   -  Reputation: 11736

Like
0Likes
Like

Posted 04 January 2013 - 01:25 AM

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.



#4 majorbrot   Members   -  Reputation: 621

Like
0Likes
Like

Posted 04 January 2013 - 09:15 AM

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

#5 gchewood   Members   -  Reputation: 236

Like
0Likes
Like

Posted 04 January 2013 - 10:03 AM

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.

 

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

 

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
}

 

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






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.



PARTNERS