Jump to content
  • Advertisement
Sign in to follow this  
Gage64

[XNA] Using custom textures with models

This topic is 3262 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 trying to apply a custom texture to a model. If I do this:
BasicEffect effect = new BasicEffect(Device, null);
effect.TextureEnabled = true;
effect.Texture = texture;
model.Meshes[0].MeshParts[0].Effect = effect;
it works, but if I do this:
BasicEffect effect = (BasicEffect)model.Meshes[0].MeshParts[0].Effect;
effect.TextureEnabled = true;
effect.Texture = texture;
it doesn't - the model shows up as black. Why is that? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
What does your model look like if you don't do either?
(i.e. are there other differences between you model's effect and the new effect you create, besides TextureEnabled and Texture)

Share this post


Link to post
Share on other sites
If I do neither, the model is black, which is strange because the model file does reference a texture (it's an ascii .X file).

I just tried the second snippet with a different model and it worked. So I guess the problem is just with this particular model.

Thanks for your help, I don't know why I didn't try using a different model before...

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Have you set up the lighting properties?


For this model, if I disable lighting, it works with the first snippet (but not the second). If I enable lighting, the model is black with both snippets.

For the other model I tried, both snippets work with and without lighting enabled.

Share this post


Link to post
Share on other sites
Quote:

BasicEffect effect = (BasicEffect)model.Meshes[0].MeshParts[0].Effect;


you are casting to a BasicEffect, but the collection may contain a different class of effect. you should instance a basic effect and reference it by the mesh, not alter the actual one, this way you need to look for type casting. That's why I think that following BasicEffect:: instructions do not work,actualy they would couse bugs.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnnyCode
the collection may contain a different class of effect.


But if it did, wouldn't the cast cause an exception?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Quote:
Original post by JohnnyCode
the collection may contain a different class of effect.


But if it did, wouldn't the cast cause an exception?


in C# it passes but you should enable throwing such exceptions or use try() {}catch {} in yout program to see a succeed. Also you can enable vstudio automatic throwing when such things occur.It is in Debug->Exceptions->Common Language..->System->SystemInvalisCastException.


Share this post


Link to post
Share on other sites
you tried clearing the back buffer whith something else that black and see if your model is really black or just out of the frustum? (I often make these kind of silly little mistake myself...)

You're processing both models with the same processor are you?

You could also try to put both effects in the watch windows and compare them. (and check the original effect is actually a BasicEffect, but if it weren't you'd have seen a exception during casting, or the statement right after that for using a null pointer)

The diffuse color of your vertices, or the BasicEffect.DiffuseColor property could also be black which would lead to a black model. (also check VertexColorEnabled)

Good luck ;)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!