سؤال

I am just getting into XNA programming and have been unable to figure out how can I access the texture from a ".x" model. I am using a custom shader to display my model (just a cube with a texture mapped on it) with the filters set to point. To do this I needed to pass the effect my texture file which needed to be imported separately from my model or else it would complain since it is included in my model as well. This works perfectly how I want it, however this isn't really an agreeable method when I have many different models with their own textures.

My question is:

How am I able to access the texture included in my model directly from it and send that to my shader? Or am I able to access it directly with HLSL?

What I have tried:

I have found posts saying that it can assigned to a texture variable with:

Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture

When I tried this the game runs but the cubes are just black. I can see that the texture variable is holding info and has the right dimensions but I can't tell if it is correctly holding the actual image. When I used just the BasicEffect they rendered just fine with their texture.

Update:

I have managed to get this to work after a little bit of fiddling. My game loads in a few hundred of the same cube and upon creation of each it would try save the texture of the model using the code above and then go through the mesh parts and change the effects to my custom effect. I discovered that the first cube created would save the texture okay but any subsequent cubes created would complain that they can't be cast as a BasicEffect. This resulted in one textured cube and then a lot of black ones. I am guessing that when it reuses the same model over and over like that it will just use the one that was modified to use my custom effect which was done on the first instance of the cube. Is this normal? I have got them all to render as textured by changing the texture variable to static.

هل كانت مفيدة؟

المحلول 2

If anyone else is wondering about this as I was then saving the texture using:

Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture

This does work but there is one thing to watch for which is what was causing me problems. If you change the effect of the model from the default BasicEffect for one instance it will be changed for every instance of the model created thereafter. So you will only be able to use the above code before you change the effect for the first time on a particular model.

I later found this book which describes exactly how to extract the texture and other information from a model: 3D Graphics with XNA Game Studio 4.0 by Sean James - Specifically chapter 2

نصائح أخرى

Please observe that you are assigning the texture of your model to a temporary Texture2D variable, and not setting the Texture present in the Effect currently tied to your mesh. If you do the following:

Texture2D textureToSet = Content.Load<Texture2D>("MyTex");

//Keep in mind that this method requires a basic effect type and that only one 
//effect is present on each mesh to work properly.
foreach(Mesh mesh in model.Meshes)
{
    ((BasicEffect)(mesh.Effects[0])).Texture = textureToSet;
}

The quirky stuff going on inside the foreach is simply that you are grabbing the effect, then casting it to a BasicEffect and using its Texture property to give it a new texture to draw when used. Please see the documentation and Shawn's blog for a more detailed introduction.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top