Learn how to render ray-traced colored shadows in Unity to give a unique tint to the style of your game. Real-time ray-tracing will do this for you.
[For better formating, read the original blog post here]
TABLE OF CONTENTS
The Goal: Level Up Your Visuals
Attempt #1: Semi-Transparent Colored Material
Why Wouldn't That Work? — Realigning Expectations
Attempt #2: Unity Ray-Traced Colored Shadows
1. Enable HDRP and Ray-Tracing
2. Enable Colored Shadows in Your Directional Light
3. Set Up Transparency and Refraction in Your Material
The Ultimate Result: Ray-Traced Colored Shadows in Unity
The Performance Cost of Ray-Traced Colored Shadows in Unity
The Goal: Level Up Your Visuals
Black, hard-shaped shadows are boring. No secret here.
That's why over time we upgraded from rendering hard shadows to the better looking soft shadows. A well-needed realism upgrade.
But in the game dev scene, we were still missing the next big step: to upgrade from monochrome shadows to colored shadows.
How is that, you might ask?
Go to your local church and see the nice colored shadows that the stained glasses project (if the church is not on a tight budget, that is). They look impressive.
Or just any fancy building in general, like this one.
Decoration Colored Shadows
How do you like them?
Imagine the unique scenery you could build for your game with well-polished, real-time colored shadows.
Ok, stop imagining. Because now you can in Unity.
So let's get to draw colored shadows.
What could be our first approach?
A semi-transparent colored material, of course.
Attempt #1: Semi-Transparent Colored Material
A stained glass is all we need, right?
Setup With HDRP/Lit Shader
That means:
A thin box-like mesh
With a lit / standard shader
Set to transparent rendering mode
With the color/texture and alpha you want
In other words, this:
Unity Lit: Semi-Transparent Material
Now we just need a directional light, enable shadow casting and play with the opacity aaaand...
Ooops... what happened there?
A grey shadow still?
That's not what we want.
But why did this happen?
Why Wouldn't That Work? — Realigning Expectations
In traditional rasterisation graphics, we use a technique called shadow mapping (any of its variants).
In short, we treat a light as if it was a camera. Then, we check what surfaces that "camera" can see... and which surfaces it can't see.
It is the later ones that are so important for rendering shadows.
Here's the simplified idea: if a surface can't be seen from that light, we say it is occluded and "paint" a shadow on that surface.
It's kind of a binary decision. That point on the surface is either on the shade or not.
(really simplified explanation)
So if you wanted to color your shadows, then you would need to know the (many) surfaces your light "rays" go through. And their properties and bla bla.
In other words, we would need to trace rays.
And that brings us to ray tracing. Our second attempt.
Surprised?
Attempt #2: Unity Ray-Traced Colored Shadows
So this is the plan:
We are going to "shoot" rays
that go through surfaces
while we accumulate information about surfaces we go through (e.g. the color of our stained windows)
so that when our light rays hit a surface that we need to shade
we know the kind of shadow to render on that surface
It sounds complex.
But it isn't, because Unity takes well care of us.
This is what you need to do:
Enable HDRP and ray-tracing
Enable colored shadows in our directional light
Make our material transparent