I want to apply textures to a 3D Model in XNA 4.

Whatever I saw on the internet was to apply the textures in the modeling tool and then load the model (*.fbx) into XNA. Is it possible to load the Model into XNA game first and then apply the Texture into its Meshes somehow?


Sure, you have to set it to a shader which you draw your model with. If it is a hand-written shader then you have to set it tothe shader:

Texture2D texture;
Effect ef;

Specify a Texture in it and a sampler, for example:

Texture xTexture0;
sampler TextureSampler0 = sampler_state { 
                                        texture = <xTexture0>; 
                                        magfilter = anisotropic; 
                                        minfilter = anisotropic; 
                                        mipfilter = linear; 
                                        AddressU = wrap; 
                                        AddressV = wrap;

Then sample it:

float4 color = tex2D(TextureSampler0, coordinates);
//coordinates is a float2 for a 2D texture, but float3 for a cubemap

If you want to use it with an XNA effect (Basiceffect for example) then you just have to set it before drawing to that effect:

Texture2D texture;
BasicEffect ef;

Please note that normally you will have to have texture coordinates specified in your model file to use textures this way, except if you are using projective texturing.

  • \$\begingroup\$ Assigning texture properties in BasicEffect worked for me. I guess UV coordinates must me specified in the Model itself. Is it a good idea to apply texture in a plain model or have the texture in the model itself before loading it into XNA? \$\endgroup\$ – Shashwat Jun 1 '14 at 8:10
  • 1
    \$\begingroup\$ You don't have the texture in the model before loading but the link of the texture so when loading the model, XNA loads the texture with it like you would. \$\endgroup\$ – János Turánszki Jun 1 '14 at 10:18

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.