Godot Engine Tutorial Part 18 – 2D Lighting

 

Welcome back to our ongoing Godot tutorial series.  You may notice a pretty major change in the way Godot looks from previous tutorials.  This is because this tutorial jumps forward to Godot 2.1 which has an updated look and feel.  Don’t worry, the process is virtually identical regardless to which version of Godot you were running.  Today we are going to look at implementing 2D lights in your Godot game.  This will involve creating Godot light nodes, as well as occluders to prevent light.  We will also look at light masking, which is useful for implementing fog of war type effects. 

 

As always there is a video version of this tutorial available here.

 

First let’s set up a simple scene we can test our lighting on.  I simple create the following hierarchy using the default Godot icon sprite.

image

 

Then scale it up like so:

image

 

Now let’s add a Light2D as a Child to our Root node:

image

 

Notice the exclamation mark?

image

 

This is because we need to specify a texture to use as the light source.  I created the following image in Paint.net (process is shown in the video if you want to see how it was made):

LightMask

 

Notice that this image is a grayscale PNG file with an alpha channel.  Where there are no pixels at all, there will be no light shown.  Where it is whitest, the light will be most intense, while the darker it gets the less intense the light.  Save this image or create your own and add it to your project.  Now we set it as the texture source for our light.

image

 

Now if you move your Light2D node around the scene, you will see how it interacts with other objects in the scene:

GIF

 

There are a number of properties you can set for the light, such as the Energy (intensity) Color (tint) and Scale (size):

image

 

Notice also Mode.  We will come back to that in a minute.  Now sometimes you aren’t going to want all lights affecting all objects in the scene.  Thankfully there is a setting for that.  Notice in the range category there is a setting called Item Mask:

image

 

This controls what layer of items this light will interact with (there is also a setting for shadows as well).  Now if you switch over to your Sprite, under canvas item you will notice a Light Mask setting:

image

 

So long as the light and CanvasItem use the same masking channel, the light will apply to that node.  If they don’t use the same channel, the light will have no effect.  This can lower processing costs and enables you to exempt parts of your seen from being lit if so desired.

 

Now sometimes you want to have light actively blocked in your scene.  For example you might have a wall that should cast a shadow.  This is easy to implement in Godot using Occuluders.  This is simply a polygon area where the proverbial light don’t shine.  Let’s set one up around the nose of our Godot sprite.  Add a LightOccluder2D to your Sprite.

image

 

Your scene hierarchy should now look like:

image

 

With the LightOcculder2D node selected in the Scene the following icon should be available in the 2D view:

image

 

Click the pencil.  Now we want to draw our occlusion polygon, like so:

Gif2

 

Now notice the way the light interacts with the scene…

gif4

 

Now remember earlier on the Mode value of our Light2D node?

image

 

Now we are going to switch it over to “Mask” and show a powerful capability.  This can be used to create localized lights or to implement fog of war effects.  Just like before we need a texture to represent our light source, but this time we want the pixel values reversed, so I created the following image:

LightMask2

 

In this case the black area will receive no light, while the whitest area will contain the most light.  Here is the end result:

gif5

 

By scaling the light out to the full size of the screen, you can easily attach such a light to the character sprite to easily implement a fog of war type approach.

 

The Video

Programming


Scroll to Top