0
\$\begingroup\$

How would I go about using changing the texture filtering method being used within the same SpriteBatch Begin/End pairing?

I have this code so far.

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null);

spriteBatch.Draw(PixelArtTexture, new Vector2(x, y), null, Color.White, 0f, Vector2.Zero, new Vector2(8, 8), SpriteEffects.None, 0f);

spriteBatch.End ();

This draws a pixel art texture using point filtering, however I want to draw another texture using linear filtering, so how would I do that?

\$\endgroup\$
  • \$\begingroup\$ Create a second SpriteBatch Begin/End combo... \$\endgroup\$ – ClassicThunder Feb 25 '15 at 18:32
  • \$\begingroup\$ Ok ClassicThunder but is there a way of doing everything in the same Begin/End, Or ever change a textures filter mode? \$\endgroup\$ – user61929 Feb 25 '15 at 18:34
  • \$\begingroup\$ You can do something like this.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp, although I don't know if it can be done with an an .Begin()/.End() block. \$\endgroup\$ – William Mariager Feb 25 '15 at 18:39
  • \$\begingroup\$ No that dosent even work outside of the Begin/end for some reason :/ but thanks for the comment :) \$\endgroup\$ – user61929 Feb 25 '15 at 18:44
  • \$\begingroup\$ @William'MindWorX'Mariager That works only if the sort mode is SpriteSortMode.Immediate. \$\endgroup\$ – ClassicThunder Feb 25 '15 at 18:46
1
\$\begingroup\$

So long as the SpriteSortMode is SpriteSortMode.Immediate you can make alterations to graphics device in between draw calls which allows you to change the ScissorRectangle, RasterizerState, SamplerStates, ect...

Of course you lose the performance optimization from the batching; however, it is sometimes worth it if the alternative is creating many begin/end pairs.

For Example

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null);
spriteBatch.Draw(PixelArtTexture, new Vector2(x, y), null, Color.White, 0f, Vector2.Zero, new Vector2(8, 8), SpriteEffects.None, 0f);

//Changing the SamplerState
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
spriteBatch.Draw(PixelArtTexture, new Vector2(x, y), null, Color.White, 0f, Vector2.Zero, new Vector2(8, 8), SpriteEffects.None, 0f);

spriteBatch.End();

The first Draw call would use SamplerState.PointClamp and the second draw call would use SamplerState.LinearWrap.

\$\endgroup\$

Your Answer

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