Page 1 of 1

Dynamic lighting and shadows

Posted: Tue Jul 15, 2014 11:19 am
by fruitcup
Hi, I wanted to share my lights.

Image
Image
Image
Image
Image
Image
Image
Image
Image
It's pretty simple:

Code: Select all

For every light
  For every block
     Iterate over edges of block, calculating the shadow for each edge
     Draw the shadows to the inverted stencil
  Turn on shader and additive blending
  Draw a fullscreen rectangle (shader renders the light with some simple math)
Change blending mode to multiply
Render every block with color
It's all runs smoothly, even with many lights and many Box2D bodies.

I just wanted to share and see what you guys think :D

Well I just saw this: viewtopic.php?f=5&t=77418

Code on GitHub in case anyone wants to see:
https://github.com/jacobbrunson/Love-Lights

Here's a standalone .love if you want it:
https://github.com/jacobbrunson/Love-Li ... ights.love

Controls:
  • Left-Click: Place light
  • Right-Click: Drag block
  • A/D: Cycle light hue
  • W/S: Change light sizes
  • Z: Remove light
  • F: Spawn blocks
  • R: Remove blocks
  • G: Toggle gravity
  • Arrow keys: Move blocks
And shameless plug, I also made this in Java and WebGL:
https://github.com/jacobbrunson/BasicLighting
http://jacobbrunson.me/lights.html

Re: Dynamic lighting and shadows

Posted: Tue Jul 15, 2014 2:30 pm
by Ranguna259
Really nice, what fps do you get ?

Re: Dynamic lighting and shadows

Posted: Tue Jul 15, 2014 8:04 pm
by fruitcup
Ranguna259 wrote:Really nice, what fps do you get ?
Hmm I just tested it. I get a solid 60 w/ 10 lights and 100 bodies, and also with 25 lights and 8o bodies

All of the code is currently optimized for readability so there's likely performance loss there. Also most of this is computed on the CPU. I might try moving all of it over to a shader to make it more robust.

Would you normally do math (like calculating the position of shadows) inside a shader? Or should that stay on the CPU?

Edit:
Well I just saw this: viewtopic.php?f=5&t=77418

So I am now quitting and putting my code on GitHub in case anyone wants to see:
https://github.com/jacobbrunson/Love-Lights

Here's a standalone .love if you want it:
https://github.com/jacobbrunson/Love-Li ... ights.love

And shameless plug, I also made this in Java and WebGL:
https://github.com/jacobbrunson/BasicLighting
http://jacobbrunson.me/lights.html

Re: Dynamic lighting and shadows

Posted: Tue Jul 15, 2014 10:04 pm
by Ranguna259
Disable vsync and check the FPS, lights on the CPU are very resource intensive so I want to know how much is shader based lightning better than CPU lightning.

Re: Dynamic lighting and shadows

Posted: Tue Jul 15, 2014 11:02 pm
by rmcode
fruitcup wrote: So I am now quitting and putting my code on GitHub in case anyone wants to see:
https://github.com/jacobbrunson/Love-Lights
No need to quit. There is enough LÖVE for everyone two lighting engines.