I'm making a game using XNA. I have some simple objects like cube and sphere, and I would like to change the texture of one face of these objects based on picking. That is, when the cursor is over one face, it turns red. The only way I've seen to do this is to overload the content processor as here: http://xbox.create.msdn.com/en-US/education/catalog/sample/picking_triangle

but it seems like it shouldn't be this complicated. I'm using .x models, and would like to be able to implement this for more complex models in the future beyond cubes/spheres/etc. Is this the best/only way to go about it? I'll figure that out if that's what is necessary, but it seems that there would be a simpler solution to load a different texture to a face than I've seen, I just don't know what it is.


Triangle picking aside, generally you can only have 1 texture per draw call. (Technically, I think it's effectively one shader effect per vertex buffer. If multi-texturing occurs here then it would be done by having multiple texture samplers in the shader, or by using a texture atlas) What this means is you need to split models up into parts when you want these parts to have differing textures.

| improve this answer | |
  • \$\begingroup\$ Ok, I think that makes sense. That's stuff I'll have to look up because I've never used a texture atlas or tried to implement multiple texture samplers in the shader, but thank you for the information \$\endgroup\$ – Thomas Carmichael Dec 13 '12 at 18:15

If you know the name of the texture that's highlighted, you can simply do this to first get the Texture2D:

public void ChangeTexture(Effect effect, string name, Texture2D newTexture, out Texture2D oldTexture)
    oldTexture = effect.Parameters[@name].GetValueTexture2D();

then store the old texture somewhere and when the area that's highlighted is changed, you simply restore the texture the same way.

| improve this answer | |
  • \$\begingroup\$ Won't that set the texture for the entire object, though? \$\endgroup\$ – Thomas Carmichael Dec 13 '12 at 17:59
  • \$\begingroup\$ But you wanted the entire object to be highlighted in red? And you store the old texture, so when it's not highlighted you just reset the original texture. \$\endgroup\$ – Deukalion Dec 14 '12 at 7:58
  • \$\begingroup\$ Well, if you know the name of the texture say it's not "Texture" on the model. Say it's called "Face/Arm/Legs" and such for a Model, I believe you use "effect.Parameters[NAME_OF_TEXTURE].SetValue()" or something similiar. \$\endgroup\$ – Deukalion Dec 14 '12 at 7:59
  • \$\begingroup\$ well the idea is to get a single face to be highlighted red. Getting the whole object to change isn't too bad, but changing one face on the fly seems to be difficult \$\endgroup\$ – Thomas Carmichael Dec 15 '12 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.