[libgdx] Shaders, Light system.

Hi, new here!

So me and my friend are working hard on our 2D game engine based on LibGDX,
and it can do a lot but there is no support for Lights, So we need to implement a good lighting system,

I am relatively new to shaders though, while I’ve read lot’s of cool tutorials on how it’s done, the question that is left for me is an architecture of that all.
There are tons of tutorials on how to cast a light on one particular texture, but we have an entire scene, build of many many textures.

So what is the best way to do that? Render all scene to FBO, then apply light to it? if so, is it optimal to render entire scene to FBO or maybe just parts that potentially will be light up? What are the common practices/mistakes? Basically I am not seeing entire picture of how is this usually done :slight_smile: Anyone did this before? would really appreciate any help or advice, and I can provide a screenshot of a level for example to make it really clear what I need to be done.

Thank!

I dont think sendng all lightsources to the shader is an good idea.
If its just 2d, you could just calculate an light level with each block?

I am not sure I understand what do you mean by “Block”

Let me show you a picture with example.

And here is an awesome tutorial I read before:

There’s another tutorial you should take a look at that isn’t exclusively java, is this the effect you’re looking for? http://www.gamedev.net/page/reference/index.html/_/technical/graphics-programming-and-theory/dynamic-2d-soft-shadows-r2032

Also, this wiki post has loads of goodies for this effect. http://www.java-gaming.org/topics/2d-dynamic-lighting/27012/view.html

Haha, i hoped for an much more simple design (square tiles) :).
I guess the simplest solution is using an texture with all the light sources (in small scale).
Hopefully someone with more knowledge could answer this, im curious about it to :slight_smile:

Thanks a lot guys, I’ll read through some links! :slight_smile:

But it’s worth mentioning that I do not need any complex type of lights like directional ones, just point light that ill illuminate it’s surroundings in a round shape.
I can basically do this with a small fragment shader code for one texture, but if used on entire screen I guess a Frame Buffer Object needs to be used, and here I am not sure about architecture and best practices.

I think I will use a better wording if I say I need to implement “Ambient light” or “Spot Light”, not sure but this seems like the right terminology do describe it :slight_smile:

A spot(or point) light is usually light that starts from one specific point and continues outward. Is that sort of what you’re looking for?

I am not sure about the naming, I am looking for effect that looks like a round circle. (you can see it on a screenshot)
Basically it’s a simple effect of a gradient fading circle that overlays the scene. It gives the near surroundings a color. It does not have direction or anything.

And most importantly it does not need any fancy things like stop on walls or whatever.

What I need, looks like this :slight_smile:

https://a248.e.akamai.net/camo.github.com/14613c209bbc2d11881d02beb35068699b4c9a30/687474703a2f2f692e696d6775722e636f6d2f5336456c572e676966

That is a point light. The first link I posted should help you understand them.

Do the lights need to be dynamic? If not, just bake them into your textures.

If the lights need to be dynamic, you can “fake” it by overlaying them as a second draw pass. Basically this involves rendering a transparent feathered circle atop your scene, using different vertex colors and blend modes to achieve something like this (Slick code here). The problem, of course, is that it is not truly dynamic – it won’t cast any shadows if you place the light next to an occluder (wall or player).

For something more dynamic you’ll want to use a solution like in box2dlights. If you want to learn how it works, read here.

You can also use a normal mapping technique like I discussed in the tutorials, but generally that’s not going to be very efficient if you have many lights (unless you apply some sort of deferred lighting technique). Further, these shader-based techniques may perform poorly on Android/iOS.

Wow, thanks! I think I am more interested in the one where it is pseudo dynamic, I am afraid bo2xlight might be using too much resources, so I’ll take a look at your example code, looks pretty good so far! This is nor a primarily Android game, it is PC, but with a possibility of future Android port.
I will look at the example code more detailed tomorrow, but just to clarify, is this the right method?

  1. Draw entire screen to buffer
  2. Overlay Light to buffer using shaders
  3. Render buffer to actual screen
  4. Render elements that are not affected to light on screen.

If so, is there a room for optimization like:

  1. render only parts that are expected to be affected by light to buffer so buffer will take less memory? (Does this make sense?)
  2. Any caching techniques maybe?

Thanks a lot again!

box2dlights will generally be less intensive since it is not dependent on fill-rate and multiple draw passes.

The steps you outlined will work but involve more fill rate. Really the main performance hit here is the extra fill and state changes involved with rendering to an FBO with different shaders before rendering to screen.

So, that means that even if I do not need “more dynamic” system, box2dlight is still better in performance then the fake dynamic overlaying to buffer approach?

As author of box2dLights I can say that performance with box2dLights is way better than any other multiple light solution that I have tested. If you don’t need shadows then algorithm is cheap as hell. Its designed to work with mobile devices so performance with pc is top notch.

Oh well, that’s great to hear then! we just go ahead and try to use it then! Thanks a lot!

First i read through the code of box2light I was a bit worried that it uses a physics engine to do a calculations, and I thought it might be slow, but yeah if you say it’s not then it’s great news.

I hope it is possible to use it with Stage class right?

Should be usable with Stage.
Box2d is only used for raycasting what is needed for shadows. Box2d have really good and efficient acceleration structure for collisions so that plus native code make generic raycasting with it really fast.

Thanks!

I’ll show you some screenshots when integrated, the project we are working on is hopefully very beautiful, and I hope you will like to see how it will all look like with box2dlight integrated :slight_smile:

Here is the website if curious.
http://underwaterapps.com/project_moon/

Thanks again!

Hi, a quick question:

The diffuse mode seems a lot more eye appealing, but it has some awkward effect when lights overlap. (green+green = red)
Is that a normal behavior? Or did I do something wrong?

sounds like a byte overflow bug with the blending