Anders Lassen

I'm trying to do something as simple as setting a sampler2D parameter in the effect to a Texture2D object loaded by the content manager, like this:

tex = content.Load<Texture2D>("assetname");

For importer and processor for 'assetname' i use Texture - XNA Framework and Texture (mipmapped) XNA Framework, respectively.

The loading works fine, but when i try to bind it to the sampler, like this:

effect.Parameters["tex"].SetValue(tex);

i get an InvalidCastException . This happens no matter what importer/processor i try in whatever combination, and no matter if i change the type and Load<> parameter to Texture instead of Texture2D. 'tex' is the correct name of the sampler2D in the shader, and i don't think i've messed up anything else in particular. Googling this issue gets me about zero relevant hits. Any ideas what might be wrong


Re: XNA Game Studio Express Problems binding a texture to a sampler (InvalidCastException)

Kyle_W

Please post the verbatim code block from your project as well as from your effect, and post the exact wording of the exception.



Re: XNA Game Studio Express Problems binding a texture to a sampler (InvalidCastException)

Kyle_W

I think maybe I know what the problem is. You said you are trying to assign your texture to the Sampler2D in your effect. You should not be doing this. Inside your effect, the Sampler2D will be referencing a texture. You should assign the texture in your XNA project to the referenced texture in the effect, not directly to the Sampler2D.