October 28 2020
By Rubén Torres Bonet
October 28, 2020
In this post, you will learn how to render colored shadows in Unity to give a unique tint to the style of your game. Indeed, real-time ray-tracing will do this for you.
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.
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.
A stained glass is all we need, right?
In other words, this:
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?
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.
So this is the plan:
It sounds complex.
But it isn’t, because Unity takes well care of us.
This is what you need to do:
Let me explain 🙂
I won’t go in detail on this one. It’s already covered in other tutorials.
Note: there are GPU requirements for ray-tracing to work. Citing Unity documentation:
Full ray tracing hardware acceleration is available on following GPUs:
NVIDIA also provides a ray tracing fallback for some previous generation graphics cards:
Basically, import the HDRP package and go through the wizard to set up the render pipeline with DXR support.
After restarting your editor, you are good to go.
Ok, let’s start with the fun.
Inspect your directional light and under shadows, enable:
The lighting setup is now ready.
Next step? Setting up your window-like material.
Make sure you are using the HDRP/Lit shader.
Here’s what you need to set in your window material:
Here’s an example:
Output: Check ✓
That’s solid progress.
But such shot isn’t really satisfying… is it?
After all, it’s just a plane, a skybox and the window.
We can do better than that.
Let’s step up the ray-tracing game with a real scenario.
Curious to see how colored shadows can look in an interior?
Well, I’m not an artist, but…
I know how to buy assets.
So I took my hungry credit card and bought this dragon interior asset.
Then, I upgraded the included scene to HDRP and set up colored shadows just like I described.
I like it.
(But don’t judge my art style. I’m a programmer)
Use the slider below to see the visual impact colored shadows make.
I know you had this question all along…
Yeah sure, it’s pretty, but how much will it cost me?
I heard it. Now I have the time to answer it.
Here are some rudimentary tests I ran with my TITAN RTX beast and Ryzen 2700X.
To my surprise, in my tests I saw no increase in performance cost of colored shadows over the uncolored version.
That is, with ray-traced screen-space shadows enabled as my baseline test.
So if you are already using ray-traced screen-space shadows, colored shadows won’t cost you extra.
However, enabling ray-traced shadows will cost you considerable performance juice. Remember, that’s a requirement for colored shadows.
Here’s the difference between ray-traced shadows ON and OFF.
In upcoming blog posts I’ll discuss the performance implications in ray-tracing in more detail.
But for now, one more thing about performance…
If you plan to enable ray-tracing in your game, you need to make extra performance room in your game. Like, considerable room.
To help you improve the performance of your game, I prepared this Unity Performance Checklist for you.
For each performance checklist item:
If you make enough performance room for ray-tracing, then you’re good to go to craft and publish a handsome experience.
Alright, see you in the next post.
(The Gamedev Guru)