RavenWorks

I'm trying to figure out how I can replace, at runtime, a texture that was automatically assigned to a model loaded from (for example) an FBX file. In the debugger's 'Watch' window, I can see the texture I want to change, however the path to get to it is a little strange. The 'texture' property is a member of SOME property of the Effects on the mesh; however, this property doesn't seem to have a name. All it says in the Watch window is "[Microsoft.Xna.Framework.Graphics.BasicEffect]", where it would normally say the property's name. (I have a screenshot here, highlighted to show the path I'm trying to take.) Does anyone know how to access that member and its properties

In short:
myModel.Meshes[0].Effects[1]._______.Texture = testTexture;
fill in the blank :)

Thanks!


Re: XNA Game Studio Express accessing the textures associated to a loaded model's BasicEffect

Jon Watte

From effects, you can get parameters by name, as ParameterHandle objects. When you have a ParameterHandle, you can call SetValue() on that to set the texture being used.

You can also use the Texture property on the basic effect -- cast the Effect returned from the model mesh into BasicEffect to get access to it.






Re: XNA Game Studio Express accessing the textures associated to a loaded model's BasicEffect

RavenWorks

Perfect! Just for the sake of anyone else having the same problem, here's the working code:

((BasicEffect)myModel.Meshes[0].Effects[1]).Texture = testTexture;




Re: XNA Game Studio Express accessing the textures associated to a loaded model's BasicEffect

LeeC22

That's excellent, thanks for posting that, this was something on my very soon to do list. I had been thinking about building textures and then applying them to a mesh. Building a football kit for example where you mix shirt types and short types.

Using this, you can premap your model with a template texture and then apply a runtime generated texture to the model... excellent, thank you.

Now if only the official docs contained simple lines of code like this... how much more useful would that be What's a little frustrating is Jon could have posted this line of code instead of leaving the answer floating around.

There's no harm in saying you can do this and that... etc... but give us an example too, like RavenWorks did.

I'm presuming you could also do... Texture2D testTexture = ((BasicEffect)myModel.Meshes[0].Effects[1]).Texture could you