How to change TextureFilterMode & TextureAddressMode in game?

Hi, (maybe I’m asking a silly question)
I use these code to draw a texture(2D):

        basicEffect.TextureEnabled = true;
        basicEffect.Texture = testTexture;
        //in Draw()
        foreach (var pass in basicEffect.CurrentTechnique.Passes)
            switch (filterMode)
                case 0:
                    device.SamplerStates[0].Filter = TextureFilter.Point;
                case 1:
                    device.SamplerStates[0].Filter = TextureFilter.Linear;
            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

I want to do this: When I press F1, the TextureFilterMode change to Point, and when I press F2 it change to Linear. But I found if I set filterMode at first, it works correctly.But seems nothing happens when I press the key.
I’m sure after I press the key, the filterMode is changed and

device.SamplerStates[0].Filter = TextureFilter.Linear;

was runned. But I can’t see any changes in graphics.Just like the Filter setting is one-off.
Same thing happens when I want to change TextureAddressMode like this:

device.SamplerStates[0].AddressU = TextureAddressMode.Clamp;

The code was copied from a book teaching XNA, and when in XNA it runs correctly.(although there are some syntax differences)
What’s wrong with my code? Thanks.

I had a similar problem yesterday. My problem was that for some reason I don’t know, my shader was using SamplerStates[1] instead of SamplerStates[0], but I don’t know if this can be your case.

You need to set the whole SamplerState object at once. Like this:

device.SamplerStates[0] = new SamplerState { Filter = TextureFilter.Point, ... };

(Although I would suggest caching whatever SamplerState objects you need, in LoadContent, for example.)

The fact that MonoGame doesn’t throw an exception when you set an individual SamplerState property after it has been bound to a graphics device was a bug, which has now been fixed (

1 Like

Thank you, problem solved !

And thank you KakCAT for sharing your solution! (though we are not in the same case :P)