# 2D Coloured Lights

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to add "coloured lights" to my game, and I want to do this by rendering an oval gradient of colour (blue for example) onto my current scene, and let blending states take care of it. Here is the gradient texture:

When I do this in photoshop and set the blend mode to "Color", I get the desired effect, where all colour gets a blue tint and black remains black:

However, when I do this in game, I can't achieve it no matter what render states I set. I get lot's of different things but the closest I got was this, which is wrong because the black turns blue, I want it to stay black!

That's with DirectX9 and
SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTALPHA);

I tried all combinations of render states... maybe.. Any help? :/

##### Share on other sites

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
Although I am rusty on these states, this should produce the result:
(SrcRGB * SrcA) + (DstRBG).

Photoshop is most likely doing this:
(SrcRGB * SrcA) + (DstRBG * (1 - SrcA)).
Or this:

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

Photoshop is most likely doing this:
DstRBG + ((SrcRGB * SrcA) * (DstRBG)).
Which can’t be done with render states.

L. Spiro Edited by L. Spiro

##### Share on other sites

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

And I got the following result

I then tried all different combinations of texture stage settings for color/alpha whilst keeping those blending operations constant, but nothing looked any better that the above picture. I really can't get my head around this for some reason.

Just to point something out which may be important, the original blue gradiant texture is blue on a transparant background, not a white background. Should I try blue on a white background?

Edit

Just tried the following

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

And I got this result:

I also tried a blue graident on a white background for my source image but that just really didn't work so I'm back to a blue gradient on a transparent background

Final Edit:
I think I'm starting to give up on blending states for this, It'd be more practical to use a shader for this right? I think I could probably get the effect I want using some shader code... right? :|

Edited by rukiruki

##### Share on other sites

Wouldn't the shader code be to divide the total r+b+g value into the desired r : g : b ratio?

##### Share on other sites

I think I'm starting to give up on blending states for this, It'd be more practical to use a shader for this right? I think I could probably get the effect I want using some shader code... right? :|

The only other thing I would try would be:

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
But that still will not get the result as in Photoshop.
You can’t get that result via render states, so yes you will need to do it via shaders.
The formula is as I mentioned above for Photoshop.

With shaders, you have the ground texture as an input and the light texture as one, and the formula then becomes:
OutRGB = GroundRGB + ((LightRGB * LightA) * (GroundRBG)).

L. Spiro

##### Share on other sites

Just tried this and I get a similar result to before

SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

• 13
• 18
• 29
• 11
• 27