Replace Texture with Solid Color

Is there any fast way (without using GetData/SetData) that I can any pixel where the alpha is non-zero with a solid color?
For example:


I will need to be doing this to many small (32 x 32) textures, filling the entire screen.
I wrote a shader to do this, but it still seems too slow (drops to 15~20 fps).

Or, even better, if there is some way to determine if the mouse is over any opaque portion of a texture that has been drawn to the screen (while ignoring textures underneath it)?

Can you show your shader’s code. It should not be so slow.
I use a similar technique with thousands of 3d models to render them in a rendertarget with solid color and it runs blazing fast.
Another question is do you apply the shader to each drawn sprite or draw them in a rendertaget, and then apply it on the latter (if they have all the same solid color ?

Your shader code looks fine, except that you’re missing the blue-channel of the new_color (color = new_color; color.a = 0).
Like alkher said that should be blazingly fast.
Be sure that you use SpriteBatch and batch all your sprites you need to draw using that effect into one draw call (don’t use SpriteSortMode.Immediate). The draw call will actually happen when you call SpriteBatch.End() and the effect will be applied once then.
So that would batch your sprites effectively, but you’d only be able to use a single replacement-color (since there will be only a single draw call).

Don’t you use the bklue channel ?

This shaders seems good. Maybe faster by starting with

if (color.a == 0.0f)
 return color;
return new_color;

after the texture sampling, and making the changes afterwards.

If you only have 0 or 1 in alpha, and no gradient, it can be done in a simple line:

color = color + new_color *color.a;

may do the trick (I did not test it thought).

If not, how do you loop on your sprites to draw them ?

I’ve never used update() to draw something in a rendertarget.


should give you a rendertarget result.

Ok. Obviously you have some draw-order issues here.
The only way to speed things up is to batch at least some of the draw calls, since the number of draw calls by using SpriteSortMode.Immediate seems to be the limiting factor here.
But you may only batch sprites that would result in the same output color since you can only pass a single color to your shader for a single draw call.

Hummm… interesting. Did I scare him ?

Nonetheless, @throbax your proposition is the only solution I can think of for the moment, as the latest optimization.

I hope it has nothing to do with us… He can’t see me, right?