How to Use Cookies to Create Realistic Lighting in Unity

Chris Hilton
Nov 15, 2021 · 3 min read

Objective: To implement a Cookie mask onto a light source to create more realistic lighting.

Adding a greyscale mask layer to point light source

We don’t live in a World where our light illuminations are always perfect, so why should our light sources always look perfect in Unity to? Let’s be a bit more reasonable or realistic and understand that our light source might have a pattern, shape or even a colour that is currently being illuminated somewhere in the form of a fragmented shadow. For example, you might have a lamp sitting at home with some scratches or imperfections on the bulb and this is illuminating on your desk with some patchy shadows.

Let’s see how we can recreate this complex light effect whilst having very little performance impact at runtime!

Besides being a very delicious snack, it is also a mask that is placed onto a light source, creating a shadow for a specific pattern, shape or colour.

unity.com

Instead of a plain light source, we could look to add this mask to possibly bring in some irregularities to the illumination to help make it look more realistic or to apply a pattern (as shown above).

One of the best features of a cookie is that it is a very efficient method of performing complex lighting effects whilst simultaneously having very little or no performance impact at runtime.

To start with let’s go ahead and bring in a greyscale texture and let’s change some it’s properties in the Inspector.

Texture Type: Leave as ‘Default’
Fixup Edge Seams: Tick
Light Type: Point
Alpha Source: From Grey Scale
Alpha is Transparency: Tick
Apply’ button

Next we are going to drag this texture into the ‘Cookiefield on the point light:

If you have changed your Texture Type to ‘Cookie’, you may have an error that pops up at this stage letting you know that ‘HDRP does not support the Cookie Texture type, only Default is supported’.

You can hit the ‘Fix’ button next to this to tidy up the issue (by converting it back to ‘Default’).

Please refer to this forum article for further information regarding this error:
Unity — [2020.1] New Feature — Baked Light Cookies — Unity Forum

Your texture should now come through onto the scene!

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.