XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

SpriteBatch - strange behavior

Last post 10/14/2007 10:13 AM by Zanza. 2 replies.
  • 10/13/2007 3:14 PM

    SpriteBatch - strange behavior

    I am completely new to XNA, so please bear with me asking what is probably a very simple question.

    I am rendering a very simple GUI element as a 9-quad with the spritebatch. As it should be resizeable, I am rendering it so that the sides and the top and bottom quads are resized from the texture. I am getting very strange results doing that. It's a somewhat transparent texture on a red background. The corners and the middle seem to render correctly, the sides look strange (artifacts in the lower left corner and that strange gradient to transparent on the right and top)

    Here is a screenshot of the output that XNA renders and below is how the texture looks in Photoshop.




    The code I am using is so simple that I doubt it can be the problem:

     	graphics.GraphicsDevice.Clear(Color.Red);

    spriteBatch.Begin();

    // top left
    spriteBatch.Draw(t2, new Rectangle(10, 10, 10, 10), new Rectangle(0, 0, 10, 10), Color.White);
    // top right
    spriteBatch.Draw(t2, new Rectangle(200, 10, 10, 10), new Rectangle(10, 0, 10, 10), Color.White);
    // bottom right
    spriteBatch.Draw(t2, new Rectangle(200, 200, 10, 10), new Rectangle(20, 0, 10, 10), Color.White);
    // bottom left
    spriteBatch.Draw(t2, new Rectangle(10, 200, 10, 10), new Rectangle(30, 0, 10, 10), Color.White);

    // top
    spriteBatch.Draw(t2, new Rectangle(20, 10, 180, 10), new Rectangle(50, 0, 1, 10), Color.White);
    // left
    spriteBatch.Draw(t2, new Rectangle(10, 20, 10, 180), new Rectangle(40, 0, 10, 1), Color.White);
    // bottom
    spriteBatch.Draw(t2, new Rectangle(20, 200, 180, 10), new Rectangle(55, 0, 1, 10), Color.White);
    // right
    spriteBatch.Draw(t2, new Rectangle(200, 20, 10, 180), new Rectangle(40, 1, 10, 1), Color.White);

    // fill
    spriteBatch.Draw(t2, new Rectangle(20, 20, 180, 180), new Rectangle(42, 5, 3, 3), Color.White);

    spriteBatch.End();

    So, what's it that I am doing wrong?
  • 10/14/2007 6:31 AM In reply to

    Re: SpriteBatch - strange behavior

    It looks rather like the SpriteBatch's texture filtering is causing the issue.  Take for example the bit where you draw the top - you're taking a 1x10 area of the sprite and drawing it across a 180x10 area of screen.  By default the texture unit will bilinearly filter the texture which will interpolate the texel at (50, 0) with the texel at (51, 0) and so on as the screen coordinates move from 20 to 200.

    This is usually what you would want to smooth out texture aliasing, but in your case you're being very exact and packing multiple textures into one image.  You don't want any texels from one part of the image bleeding into the next.

    To disable this behaviour, you'll need to go "behind the back" of the SpriteBatch a little, with something like:

    spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); // tells the spritebatch to draw each sprite immediately, has the side effect of preventing it from changing state between Draw() calls.

    // Set the texture sampler's filter settings to all be "point" filters
    renderContext.GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Point;
    renderContext.GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Point;
    renderContext.GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Point;

    // ...drawing code here

  • 10/14/2007 10:13 AM In reply to

    Re: SpriteBatch - strange behavior

    Thanks, that worked.
Page 1 of 1 (3 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG