Hello and welcome to another episode in the Godot Basics tutorial series, Godot Tutorials is not affiliated with or sponsored by Godot Game Engine. In this episode, we take a look at the light occluder 2D node class. The Light Occluder 2D Node class inherits from the Node 2D class, which ultimately inherits from the node class, meaning we do have access to those life cycle scripts. The light occluder 2d Node, occludes or obstructs light cast by the light 2D node.
And this is where these shadows come from. The light occluder 2d node must be provided with occluder polygon 2d in order for these shadows to be computed, the light occluder 2d node comes with two properties. The first is the light mask property. By default, you are set to layer one. The second property is occluder, which is your occluder Polygon 2d data type and by default this is empty, meaning you have to set this up yourself. Now the occluder polygon 2d is a resource class and you're going to need to provide this resource to your light or occluder to denote in order to get your shadows working.
Basically the occluder polygon 2d facilitates the drawing of your 2D polygon. Your 2D polygon is use for the little occluder to denote the occluder. Polygon Tutee comes with three properties. The first is the closed property, which is a boolean data type, and by default this is set to true. The second is your commode and it needs to be provided a commode enum data type. In this case, the default value is zero, meaning that no Colling will be applied and the last property is your polygon and this holds all the points you provide, which ultimately is used when creating or casting your shadow.
Now the closed property is set to true. And all this means is that does the first index of the polygon array connect with the last index of the polygon or the commode, which again is set to zero by default, comes with three options. The first is disabled, the second is clockwise, and the third is counterclockwise. And just like the name implies, either Coaling is disabled. Kealing is performed in the clockwise direction or Colling is performed in the counterclockwise direction. And again, lastly, the polygon property holds your excluder polygon points, which is ultimately used when creating or casting your shadow by default.
Like two, D does not cast a shadow even when you provide a light occluder to download onto your game scene.
And this is because the Light 2d has a property called shadow enabled and by default this is set to force.
In order to cast shadows onto your game scene, you need to change the value of shadow enabled to true. Now, once you've set the shadow enabled value to true the light, the note comes with five properties that allow you to change the look and feel of your shadows or rather the shadows you cast onto your game. Seeing the first property is called shadow colour, and when you change the colour of the shadow colour property, you do in fact change the colour that your shadows cast in.
The second property is called shadow gradient link, and by changing this fellow you can change the gradient of your shadow when it's cast.
The third property is shadow filter and by default your shadows have no filter. However, you can change how many sample sizes you'd wish to apply onto your shadow. Basically, the more filters you apply, the more organic your shadow looks when being cast it the fourth property, a shadow filter smooth. When you set the shadow filter value, you most likely want to set the shadow filter smooth value as well. And again, the higher your number is for the shadow filter smooth value, the more organic your shadow will look.
And lastly, you do have a shadow item coal mask and so you can control which layers you want to cast shadows onto.
And by default, this is set to layer one. Now, let's go ahead and take a look at an example.
This one here, I have three different game objects.
The first is our light source.
And I put the picture of Godot just to show you visually where exactly the light source is emanating from. The second object will be our game object in which we will apply shadow to. And the last game object is the background. Without the background, we are unable to see where our light is touching or affecting or even c# where the shadows being cast it from. Now, first, let's go ahead and set up our little cluster to denote. So first you want to pick the game object in which you would like to apply your little cluster to the node.
In this case, I would like to apply it to the game object called Shadow Source, press the button and the search bar type of little cluster and select it by default. It will give a soft error. In order to fix that, all you need to do is set a. Holligan and so in this case, if we go to the inspector tab and select the excluder object, you will be given a drop down menu and you just go ahead and pick a clue to Publico and the software we're still going to show because we need to apply polygon points.
So let's go ahead and do that right now. So in this case, I'm going to mark the entire square. And I'm going to close it off.
And that's basically it now, if the error message is still showing, that's OK, sometimes it does that you can just move it, move it back in and will be fine. Now, as you can see here, there is no shadow being processed.
And that's because our light tudy known as the shadow set to FOSE. So in this case, all you need to do is click on late Tudi in the inspector tab. Go ahead, select the shadow dropdown options and in the enabled section, make sure you check box that into true. And now we can see the shadow being a.
So let me zoom out a little bit. So as you can see here, once we've applied the shadow enabled property to true, a shadow is being tested.
Now, if we move our light source around, you can see that shadow is being calculated based on the position of our light source.
Now, if we select late occlude or to date, we have several options available to us to open the options.
Or you have to do is click in the box that says Occlude or Polygon, and you can seal the default values close, set to false commo, set to disabled.
And of course, because we have to set up our polygon points, we've created an array of polygon points. If you click on that, you can see all the points and their individual locations. Now, in this case, we have four points one, two, three, four. And it is closed and all closed means is that our first element is connected to our last element. Now, if we uncheck the closed option, all that means is that the last element does not connect with the first element.
And so when we move into the opening, light is being applied to everything inside that game object. And if we move out, you can see that shadow is now being applied inside.
And so in this case, only the top border, the right portion of our border and our bottom borders are applying Chettle, whereas the left side of this game object is not applying shadow.
So of course, you have your clockwise and counterclockwise options.
They do different effects based on different things. And you just have to play around with these effects in order to get the look and feel of shadow effects you want.
Now, another important thing is setting up values in the light to denote so in this case, color. In this case, it set two zero zero zero zero by default or over.
You can also change the colors of your shuttle by changing the color value inside the shuttle color property. I'm going to set this back to default. On top of that, you have gradient length. So in this case, how many gradients or how transparent you want your shuttle to start from before hitting solid colors?
So you can see the higher the number, the more transparency your shadow has onto the background object. I'm going to set this back to zero next. Let's take a look at the filter. Allow me to zoom in onto the shuttle so you can see the filter effect being applied on to a shallow excuse me, shuttle. So in this case, filter set to none. However, if we set it to three, you can see that we use three samples, one to three. And the more samples we use, the more as added towards the edge. And so obviously, the more samples you have, the more organic your shadows can look like.
On top of that, we have the smooth filter, so let me zoom back in again. In this case, the more we smoothed the water bigot, but in this case, when we zoom out, you can see that our shuttle looks more organic. So let me reset everything to its default value. And you can see that our shuttle is very straight. In this case. I'm going to play 13.
We're going to smooth it out and you can sort of see that the shadow looks and feels smooth when you keep it within certain bounds. Now, Leslie, let's take a look at the item mask for our shuttle property. In this case, we are affecting everything in layer one and by default, everything except shuttle from layer one over.
Let's say we only want our shuttles to be affected by layer two.
In this case, I'm going to set it to layer to only now we need to head into our little clearer 2d and set the same thing to our lightness. Now there's something I want you to notice here in the mask when we set the light mask here inside of our light to A or 2d, we also affect the light mask in our canvas items. So something to note that the light mask and little occluder to reconnect with the canvas items.
Now let's go ahead and set the mask to only be affected by light in layer two and you'll notice that there's no shadow. And that's because our background also needs to be affected by things in layer two as well in order to have the shuttle apply it onto it. So in this case, we're only being affected by shadows in layer two. So if I were to turn off this Little Cluetrain layer to notice how it's on and off and at the same time, because this shadow is being applied on to our background object, we also need our background object to have light mask being affected by things, and they are one and layer two.
So in this case, when we do canvas item, we're dealing not only with shadows but also light.
And so we need to keep one and two on for a background, for a background to be affected by light and shadow.
Anyway, that's all I have for you in this episode. Thank you so much for joining me. If you have any questions or comments, please feel free to leave them in the comments section down below. I'm going to go ahead and upload this example to GitHub. No code, just the example. Thank you so much for joining me. And I look forward to seeing you in the next episode. Have an amazing day.