Jump to content
  • Advertisement
Sign in to follow this  
rukiruki

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.

If you intended to correct an error in the post then please contact us.

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:

collight.png
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:
light2.png

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!

light1.png
 

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 this post


Link to post
Share on other sites
Advertisement
Lights are additive.
 
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 this post


Link to post
Share on other sites

Thank you for your reply L. Spiro I really appreciate your time.
 
I tried your suggested solution:

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

And I got the following result
blulight.png

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:
blulight2.jpg


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 this post


Link to post
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 this post


Link to post
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);

Thanks for the advice, I'll be going ahead with the shader route, thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!