• Create Account

## 2D lighting through blend states

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1TheResolute  Members

187
Like
0Likes
Like

Posted 01 May 2012 - 02:39 PM

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

### #2MJP  Moderators

18215
Like
0Likes
Like

Posted 01 May 2012 - 02:48 PM

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, 01 May 2012 - 08:34 PM.

### #3Jason Z  Members

6417
Like
0Likes
Like

Posted 01 May 2012 - 04:18 PM

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.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #4TheResolute  Members

187
Like
0Likes
Like

Posted 01 May 2012 - 04:22 PM

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

### #5MJP  Moderators

18215
Like
1Likes
Like

Posted 01 May 2012 - 08:33 PM

It works like this:
FinalColor = SrcColor * SrcBlend (BlendOp) DstColor * DstBlend


So you what you originally specified would give you this:
FinalColor = SrcColor * SrcColor - DstColor * DstColor


The values I gave you will do this:
FinalColor = SrcColor * DstColor + DstColor * 0


Edited by MJP, 01 May 2012 - 08:33 PM.

### #6TheResolute  Members

187
Like
0Likes
Like

Posted 01 May 2012 - 09:33 PM

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 multiplies those things by their corresponding assignments
I was under the impression that those BlendSrc and BlendDest values were the data themselves XD

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.