Has anyone figured out how to change the color of bitmaps when rendering with Direct2D? With Direct3D it's a simple matter of specifying different vertex colors or, when using the sprite batch, providing a color. I would like to do this using Direct2D. Thanks in advance for any help.
Color attenuation/tint in Direct2D
1.7k Views Asked by Tom At
2
There are 2 best solutions below
0

I have tried using the CLSID_D2D1ColorMatrix effect to tint the bitmap and it does work but the performance is pretty terrible (if you need to do it every frame).
I could cache the output if the color is going to change a lot but I like to do animations over the tint on my sprites to do flashing/damage effects and so caching wouldn't help.
My next course of action is to switch my renderer to D3D and just implement the SpriteBatch shader including tinting (or base it off of DirectX Tool Kit implementation):
http://blogs.msdn.com/b/shawnhar/archive/2012/06/18/directx-tool-kit-is-now-on-codeplex.aspx
Using the current API, drawing a semitransparent geometry over the drawn image is the easiest method.
If you are willing to use the preview APIs:
It isn't as convenient as XNA's SpriteBatch, but using ID2D1DeviceContext::SetPrimitiveBlend() to set the type of blending you want (additive or source over alpha blend) then you can draw on top or below the image to simulate the tint effect you are looking for.
Alternatively you can use the DImage's ID2D1Effects (new component to D2D) to use a built in effect or custom effect to draw an image.
DImage effects can be chained together to from an "effect graph", think node based shader editors.
Finally you have available the ID2D1GpuTransform. It allows pixel shader to be set in the ID2D1GpuTransform. This transformer can be plugged into the effect graph.