Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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().

share|improve this answer
    
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? –  user1542 May 19 '12 at 9:26
    
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. –  Roy T. May 19 '12 at 9:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.