Jump to content

  • Log In with Google      Sign In   
  • Create Account


Translucent Sprites, Depth and Effects in XNA


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 bonus.2113   Members   -  Reputation: 630

Like
0Likes
Like

Posted 11 January 2013 - 03:24 PM

I am trying to implement a flexible solution for applying shaders to sprites in my XNA game. I have troubles coming

up with an efficient way to maintain back-to-front draw order (or any other way to draw translucent sprites correctly)

while having the possibility to draw each sprite with a different shader.

 

The only thing I can think of right now is sorting the sprites by depth and then starting a new SpriteBatch Begin/End block

everytime the current sprite has a different shader than the previous one, like that:

 

List sprites; //Depth-sorted List of sprites
Sprite prevSprite = sprites[0];
spriteBatch.Begin(..., sprites[0].shader);     //begin with the parameters for the first sprite
foreach( sprite in sprites)
{
    if(prevSprite.Shader != sprite.Shader)     //if we have to switch the shader
    {
        spriteBatch.End();                    //end old, begin new block
        spriteBatch.Begin(...,sprite.Shader)
    }
    spriteBatch.Draw(sprite);                //draw the sprite
}
spriteBatch.End();

This could potentially end in a lot of draw calls, though in a 2D game it might be probable for sprites with the same shader to be

drawn at the same depth. Has anyone a better idea on how to do this?

bonus.2113

 



Sponsor:

#2 Adam_42   Crossbones+   -  Reputation: 2361

Like
0Likes
Like

Posted 11 January 2013 - 04:07 PM

My suggestion would be to write the simple code which does lots of draw calls first and if your profiler tells you it's a problem then come back to the code later to optimize it.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS