3
\$\begingroup\$

I have a set of sprite drawn in FrontToBack sortmode. In XNA 4.0, the effect must be as an input argument of Begin() function of SpriteBatch. However, If I reinvoke the function, then the sprite sorting is lost. Therefore, I have to find a way to implement the pixel shader within the Begin() and End() for specific texture drawn. I tried to use effect.CurrentTechnique.Passes[0].Apply(), but no pixel was shaded when I invoke this. On the other hand, if I put effect as an argument, then all of the sprites would be effected. Therefore, is there any convenient way to approach this?

By the way, what I am trying to do is to provide an overlay on an isometric tile using some kind of masking. Therefore, I want to draw such masking and programmatically change it to optional color.

Something like this (from final fantasy tactics):

enter image description here

I am not sure if pixel shader is the right way to approach it, but then I have to tackle the fact that I want to be able to specify optional color dynamically. Other than that, I really want it to animate, which I might approach this by creating several grayscale animated masking and saturate it using pixel shader.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Changing effects and effect parameters in a single batch doesn't work unless if you're using the SpriteSortMode.Immediate. This happens because the sprite batch waits until you call spriteBatch.End() before starting the drawing.

It seems like you would like to use the spritebatch because you want to sort the sprites before drawing. If you manually sort the sprites and then draw them all using SortMode.Immediate you can use effect.CurrentTechnique.Passes[0].Apply() anywhere between .Begin() and .End().

\$\endgroup\$
2
  • \$\begingroup\$ So you suggest me to manually sort all the sprite without relying on spritebatch functionality. I want to know if there is any performance penalty by implementing my own sorting code. Wouldn't that means that I have to invoke several hundred of Begin()/End()? Will that affect the performance? \$\endgroup\$
    – user1542
    May 19, 2012 at 9:26
  • \$\begingroup\$ Sorting is 'cheap' and will always be O(n log n) wether you do it or the SB does it. Also you will only need one Begin()/End() since in immediate mode you can change the shaders and shader args anywhere between Begin() and Ed(). There will be a performance penalty for using immediate mode though, since it doesn't batch. \$\endgroup\$
    – Roy T.
    May 19, 2012 at 9:44
1
\$\begingroup\$

This link might help you out with the issue:

http://msdn.microsoft.com/en-us/library/bb313868(v=xnagamestudio.31).aspx

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .