In this lesson, you will learn how to import and apply custom light cookie textures to simulate complex, non-uniform light patterns such as natural foliage shadows.
Understanding Light Cookies
While a standard light source emits light in a uniform cone or sphere, a Light Cookie acts like a stencil or "gobo" placed in front of a light. It filters the light intensity based on a grayscale texture, allowing you to project intricate patterns onto your environment. This is essential for achieving organic looks, such as sunlight filtering through dense tree canopies or a flickering candle casting shaped shadows.
Importing and Configuring the Texture
To create your own cookie, design a high-contrast black-and-white image in software like Photoshop. Pure white represents full light transmission, while black represents total occlusion.
- Importing: Drag your texture into your project. In the Import Settings, set the Texture Shape to
2Dand ensure the Alpha Source is set toFrom Gray Scaleif you are using a standard image. - Settings: Most importantly, set the Wrap Mode to
Clampto prevent the texture from repeating at the edges of the light projection. - Application: Select your light source (e.g., a
Spot LightorDirectional Light). In the Cookie field of the Light component, drag your texture asset.
Adjusting the Projection
Once applied, adjust the Cookie Size (for directional lights) or the light’s Angle (for spot lights) to scale the texture projection. If your shadows look blurry, check the Shadow Resolution in your light settings or ensure your texture dimensions are power-of-two (e.g., 512x512 or 1024x1024) for optimal sampling.
You can also animate the texture offset via script to simulate moving foliage:
public Light targetLight;
public float speed = 0.1f;
void Update() {
// Modify the cookie offset if using a custom shader or
// simply rotate the light transform to simulate wind movement.
targetLight.transform.Rotate(Vector3.up, speed * Time.deltaTime);
}
By layering these cookies, you break the artificial feel of standard Unity lights, grounding your environments in realistic, textured lighting.
Key Takeaway: Light cookies transform flat, geometric light into rich, textured shadows by using grayscale textures as physical occlusion masks for your light sources.