Sign in to follow this  
Syranide

Blending colored light values, etc?

Recommended Posts

Syranide    375
I'm currently developing a 2D game, the engine itself is meant to be rather simple in nature.

All terrain blocks and such are constrained to a simple fixed 2D grid, I've just implemented a crude form of real-time ambient occlusion, only white lights allowed, a light is assigned to a square in the grid, and the light propagates outwards to other squares from there, decreasing in intensity as it spreads... it works really well and was really rather simple to implement.

So I'm currently looking into supporting colored lights, and even pondering about trying my luck with color bleeding (having the color of the terrain influence the final light values slightly)... and an extension of that, have materials that can color light going through it (a white light source that shines through water will have a blue tint). Keep in mind that this is for a 2D-game, so correctness is not important, a pleasing end-result is the primary goal, in that context, I also assume that it is favorable to be able to maintain a bright environment (that is, one light should be able to fully light the immediate area around it)... perhaps I'll even add HDR just for the fun of it, could improve the feeling of the different environments and areas... although it could perhaps also make lighting a lot harder to work with.


But I'm quite literally rather lost when it comes to theory of color math and colorspaces, although I don't think I'll have any issues with the technical sides of it all.


[b]How do you correctly blend light values?[/b]
Adding the RGB-values seems like it would exaggerate the final light too much.
- Two lights of equal brightness shouldn't produce a twice as "visually" bright light if put in the same spot? (use a logarithmic scale?)
Taking the maximum of the R/G/B-channels seems like a reasonable approach from a brightness-perspective, but from my experience, this messes up the color instead.
- 50% R + 50% G will yield 50% RG, whereas 50% RG + 50% G will still yield 50% RG.

It seems like one should treat luminance and color separately? Use some other color space?
Take the maximum brightness of the light values, and then do a weighted blend on the color values separately? What colorspace would you recommend for blending colors?


Generally I'm just looking for information on how to deal with colors and such, links, tutorials, anything that could provide some in-sight. If you have any ideas or hints I'd love to hear them.

Share this post


Link to post
Share on other sites
Ashaman73    13715
A basic light setup is quite simple. Light is always added in linear space. Your texture is a color filter, so your simple light algo would look like:

[code]
final_red = ambient_red + texture_red * sum_of(light_red)
...
[/code]
That's really all and it should work.... in HDR. In LDR you would get oversaturated (clamped) values which would look wrong, but using HDR and a tonemapper would get rid of such artifacts.

Share this post


Link to post
Share on other sites
Syranide    375
[quote name='Ashaman73' timestamp='1306387933' post='4815910']
A basic light setup is quite simple. Light is always added in linear space. Your texture is a color filter, so your simple light algo would look like:

[code]
final_red = ambient_red + texture_red * sum_of(light_red)
...
[/code]
That's really all and it should work.... in HDR. In LDR you would get oversaturated (clamped) values which would look wrong, but using HDR and a tonemapper would get rid of such artifacts.
[/quote]

Yeah, I'm quite familiar with that equation, but as you say, it doesn't really work well in LDR.
Forgive me if this is a stupid question, but is that equation really a good approximation? ...or am I perhaps missing something, to me it would seem like luminance shouldn't be linear, e.g., a 10 times brighter light (I think) shouldn't produce a 10 times linear visual increase in brightness..., it would seem like there needs to be a log() somewhere... is this perhaps done in the tonemapper? (I should note that absolute simplicity is not required, the computations will not be done per-pixel so there is probably quite a lot of room for heavy math)

And on that subject, I'm not 100% sure if HDR will actually turn out well (visually) for my purposes (it might create really weirdly distributed lighting, remember, 2D "side-scroller") although I will definately have a try at it... but what are the options for LDR? Store as HDR anyway but just work with a static range? Other "better" ways of combining light values?


Also, when it comes to blending between/to colors (other purposes), is there any particular color space that generally performs better, visually? RGB is pretty poor, HSV likes to do rainbow colors... is there any particular colorspace that is usually preferred? CIELAB, CIELUV, YCbCr? Would it perhaps be beneficial to do HDR in such a colorspace too?

Share this post


Link to post
Share on other sites
Waterlimon    4398
You propably need something so 2x brigter light doesnt look 2x brighter, if i remember right in real world the light outside can be a lot brighter than inside even if you dont really realize it... :P

Also if you have 2 light "rays" going through each other, make them not affect each others color. Only blend the colors for the light that 'hit the surface'

Share this post


Link to post
Share on other sites

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