[XNA] Using custom textures with models

Started by
7 comments, last by janta 14 years, 6 months ago
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.
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)
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...
Have you set up the lighting properties?
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.
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.
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?
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.


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 ;)

This topic is closed to new replies.

Advertisement