Using Cookies to Create Realistic Lighting in Unity
Objective: Learn what Cookies are in Unity, what they do, how they work, and how you can apply them.
A cookie is a mask you apply to a Light Source in your scene to cast a shadow of a particular form or color, altering the Light’s appearance and intensity. This mask is essentially a black and white texture that you can add to your light source where it will interpret the information for you, we’ll take a look at this in the example later. Cookies simulate sophisticated lighting effects effectively and have little to no runtime performance impact. Cookies can replicate a variety of effects, including caustics, gentle shadows, and light shapes.
Some of the effects you can replicate with this are water light reflections
Flashlight light shapes and intensity. In fact, the example we’ll be taking a look at today nearly replicates the image below.
Adding Cookies to a Light Source
In this example, I will be using a Flashlight Asset from Filebase to attach my Spotlight to it. I will also be using this cookie texture:
Note: This is completely optional, feel free to follow along with your own setup.
- Select your cookie texture and make sure the texture type is set accordingly. You will also need to set the Alpha Source to “from grayscale” since we’re using a black and white Image. Lastly, check the Alpha Transparency box, this is what enables the editor to recognize transparency in the image and not just project a square with black spaces like the regular texture shows.
2. Choose a light source to apply the cookie texture. I personally created a spotlight and positioned it as a flashlight pointed at a wall. For the look of texture that I’m using in this example, a spotlight is the best option to apply to.
3. With your light source selected, drag your cookie texture to the cookie section in the Inspector.
You may get a warning message once you add it, you can simply click on “Fix” and it will automatically adjust your texture.
The Result
As you can see, both the intensity and shape of my Spotlight changed. Now it looks like an organic flashlight rather than just some artificial lighting made in a program.
Thank you for your time! If you found this information valuable give me a follow! :) I document my learning journey into Unity Development and Software Engineering in the form of tutorials!