Sign in to follow this  

2D lighting through blend states

This topic is 2050 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

Hello there,

I want to create a lighting system for my 2D game using D3D11
Blend states seem like the way to go
The lighting one that I would expect to work would look like this:
SrcBlend = SRC_COLOR (output from pixel shader that shaded the light map, so this would be the light map)
DestBlend = DEST_COLOR (the existing game scene that was rendered and looks completely lit up and normal without this lighting process)
BlendOp = SUBTRACT (subtracts the light map from the existing scene)

Here's my theoretical process:
Render game scene normally
Set the OM's blend state
Draw the lighting map

The theory for the blend and lighting map is that the blend takes the existing rendered scene and subtracts the inverse of the lighting map from it
Lighting would be white on the map, and darkness black
This should result in the RGB values of the already rendered scene (s) going through this equation with the light map (l): s.R - 1 - l.R, s.G - 1 - l.G, s.B - 1 - l.B. A lit pixel would have 0 subtracted from it, leaving it its natural color and a dark pixel would have 1 subtracted from it, leaving it black

My issue is that no matter what I've tried, I cannot configure a blend state that achieves this effect
Is there something I don't understand about the blend state itself or when it is incurred?

Thanks for any help

Share this post


Link to post
Share on other sites
Wouldn't you just want to multiply the light map value with the existing color in the render target? To do this, you want to use D3D11_BLEND_DEST for SrcBlend and D3D11_BLEND_ZERO for DestBlend (with ADD as the blend op). Edited by MJP

Share this post


Link to post
Share on other sites
I think the problem is that you are subtracting a light value, but you are subtracting that from what you are assuming to be a fully lit texture. What you are subtracting is only the light portion, without any consideration of the material being lit. So I think with those blend states, you would have to sample the fully lit texture from your render target, then apply the light map to that value in your shader, then output the value to be blended with the fully lit render target. This should produce what you are trying to do.

Of course, I think what Matt said is the easier way to do this - just use a multiplication to zero to get the lighting effect.

Share this post


Link to post
Share on other sites
That confirms that I lack a fundamental understanding of exactly how blend states work
Does the ADD operation just add the corresponding RGB values of each pixel in both the blend source and blend destination together?
A comprehensive explanation of what exactly the blend state operations and blend source values do would be excellent, thank you in advance

Share this post


Link to post
Share on other sites
It works like this:
[code]
FinalColor = SrcColor * SrcBlend (BlendOp) DstColor * DstBlend
[/code]

So you what you originally specified would give you this:
[code]
FinalColor = SrcColor * SrcColor - DstColor * DstColor
[/code]

The values I gave you will do this:
[code]
FinalColor = SrcColor * DstColor + DstColor * 0
[/code] Edited by MJP

Share this post


Link to post
Share on other sites
Microsoft did not document certain aspects of DirectX very well, apparently this was one of them
That is an absolutely brilliant explanation, it made a world of difference
And no one else offered it, not the DX documentation or even a Google search for "D3D11 blend state"

I am good to go now, knowing that tiny piece that the blend state [i]multiplies [/i]those things by their corresponding assignments
I was under the impression that those BlendSrc and BlendDest values [i]were the data themselves [/i]XD

Thank you so much for you help, I really appreciate it

Share this post


Link to post
Share on other sites

This topic is 2050 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this