Sea otter in a circleSea Otter Games

#8 - 2D Dynamic lighting in Unity

By Hugo on 2024-03-23
Project milestone
Pixel Art
Tutorial
Game Design

While working on The Neptune Records -the game project that concluded my studies- I came across a video tutorial to implement dynamic lighting for 2D sprites in Unity. The video is awesome!! In a single minute, the video creator manages to take you through the whole process. It is efficient, funny, and gives you an excellent overview of the subject and how it could be used in your project. Go check it out!

Now that I am working on my personal -yet to be named- game project, it is the occasion to implement this myself. And since the start of the project, I knew I wanted to try this out. Had I entirely followed the video I would not have written about it, but I changed some things so I think the topic is interesting.

What is 2D Dynamic Lighting?

To understand what dynamic lighting is, let’s split up the term.

Lighting, just like in all other visual art forms, describes the effect of a light source on an object, be it a drawing, a painting, a photograph etc. In our field, it is mostly applied to 2D drawings/renders or 3D models.

Dynamic means that the lighting dynamically evolves according to the nearby light sources. Light source moves, shadows move as well. Basic. Indeed, you could simply draw your sprites already lit by the light. But that is lame and we do not want lame. So what you could also do, with some more steps, is adding some logic to your sprites to make them reactive to light. This is a bit vague but you will get the hang of it later on.

And here we mention 2D because it describes flat drawings rather than 3D models. When it comes to 3D models, these are often meant to be dynamically lit by light sources: these are digital sculptures so they have shapes and forms, and shadows, and edges. Light can naturally behave. On flat pictures, however, if you shine a light on it, everything is just flat. It is a picture! As such, the whole frame will be somewhat brighter and it will not light the drawing according to the shapes represented.

How to achieve it?

⚠️ Disclaimer: I use a paid software at some point in the process. I sadly could not find an easy and free way to do it.

If you have seen the aforementioned video, you know that first, we need our sprites.

A picture with multiple sprites of buildings
Behooold the buildings from my game! I feel like the sawmill is a bit weird, no? What do you think?

Technically speaking, I guess I should have drawn my buildings with absolutely no lights. I tried my best to do so, but it just felt too weird. For example, for all the wooden beams in my buildings, the top and front sides should be the same color (because the darker shade of wood-brown is technically due to an absence of light) and that is just too. damn. weird.

Then, for each sprite, you will need to draw the lighting in black and white. Black represents the absence of light and white the full exposure to the light source.

A picture of one of the previous sprites with the lighting in black and white
The lighting sprites you will have to draw from multiple angles. Note that I'm not completely sure I did that properly but it works well enough for now.

💡 I did not draw the lighting coming from under because it does not suit the perspective I chose, but keep in mind that for a flat 2D perspective you will need the four sides.

Export the main building sprite as SpriteName_Diffuse.png and the black and white lightings as SpriteName_Top, SpriteName_Bottom, SpriteName_Left and SpriteName_Right (and the .png obviously). Watch out, uppercase matters!

Open SpriteLamp and import your *_Diffuse.png file, the others should follow if named properly. From there you can generate and export the normal map for your sprite. This is the png file that holds the virtual “height” of your drawing. We will not need anything else other than your sprite and its height map, you can store your lighting sprites somewhere and forget about them.

Also, do not forget to play around with your lit sprite because it is hella cool.

A gif showing the preview of a building sprite with dynamic lighting
A gif showing the preview of a building sprite with dynamic lighting.
The first dynamic lighting preview I created with previous versions of my buildings.

Now start up your Unity project, grab a coffee, fold your clothes, watch your children grow or whatever you usually do in the meantime. Things are about to get interesting.

In Unity, set your usual settings for your sprite and then open the sprite editor.

A screenshot of unity's inspector while selection a sprite. It shows the button opening the sprite editor.

Open the dropdown menu in the top-left corner of the sprite editor and select Secondary Texture.

A screenshot of unity's inspector while selection a sprite. It shows the button opening the sprite editor.

It should open a small window within the sprite editor. In this window, drag and drop the corresponding normal map and name it _NormalMap. Again uppercase matters. Once that is done, your sprite is ready to be lit. It now holds all the information it needs to be properly lit according to its topography.

A screenshot of the window that allows you to place and name your normal map files in Unity
Here add your properly named normal map.

Now, each if you want your light sources to dynamically light your sprites, go over to the Light2D component and in the folding menu named “Normal Maps” switch the quality from disabled to something else. Now your sprites should go from this…

A screenshot of the light 2D component in unity. It shows a section in charge of using normal maps
The last section controls the use of normal maps on this particular light.

Here is the result

A gif of buildings being lit without dynamic lighting.
Notice that all buildings are lit the same way while the light travels over them.

...to this!

A gif of buildings being lit with dynamic lighting.
Now you can clearly understand how the sun moves over the buildings and how it creates bright and darker faces

Next step is to add shadows on the groud under the buildings, but I will keep this for next time 😌
Thank you for reading to the end! I hope to see you in the next post. 🦦

© Copyright 2024 - 🦦 Sea Otter Games