XNA not loading model textures automatically
I have a very n00b like question here .. I have this small XNA game in which I tried importing a 3D model already made and provided by Microsoft. The problem is XNA isn't loading the textures associated with this model, even though it makes sure that the associated with the model are present in the project directory (compiler gives an error if it can't find these textures), .. So basically I just see a big clay model :P ..
This picture shows the result I'm getting:
This is what it should be like:
Is t开发者_Go百科here anything which I'm missing here ? Shouldn't XNA automatically apply all textures associated with a specific model ?
When you are rendering your dude.fbx
model, make sure that in the loop for drawing the model that you have something like the following:
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
}
}
This is off the top of my head, so the method calls might not be correct.
Xna does not automatically load the textures for the model. The exception you're getting when the texture is not present is thrown by the Xna's resource compiler and is not related to actually importing anything.
The way you would solve this is by loading the texture manually in LoadContent()
or anywhere else where you have a ContentManager
using:
modelTexture = Content.Load<Texture2D>("mytexture");
and then add the texture either as a parameter if you have a custom effect or put it in the Texture
property on your BasicEffect
instance when you draw it:
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect eff in mesh.Effects)
{
eff.TextureEnabled = true;
eff.Texture = modelTexture;
}
mesh.Draw();
}
(drawing should obviously not be done in LoadContent()
though)
Notice the eff.TextureEnabled = true;
which is required to activate textures when using BasicEffect
.
精彩评论