Basic

October 28 2020

1 comments

The Colorful Side of Ray-Traced Colored Shadows in Unity

By Rubén Torres Bonet

October 28, 2020


__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"6c4de":{"name":"Main Accent","parent":-1},"67ed2":{"name":"Accent Dark","parent":"6c4de","lock":{"saturation":1}}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"6c4de":{"val":"var(--tcb-skin-color-0)"},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"l":-0.05,"s":0.04}}},"gradients":[]},"original":{"colors":{"6c4de":{"val":"rgb(51, 190, 127)","hsl":{"h":152,"s":0.57,"l":0.47}},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"s":0.04,"l":0.24}}},"gradients":[]}}]}__CONFIG_colors_palette__

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.

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

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:

  1. Enable HDRP and ray-tracing
  2. Enable colored shadows in our directional light
  3. Make our material transparent
  4. Give our material a refraction mode and a refraction texture/color

Let me explain 🙂

1. Enable HDRP and Ray-Tracing

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 GeForce RTX 2060, RTX 2060 Super, RTX 2070, RTX 2070 Super, RTX 2080, RTX 2080 Super, RTX 2080 Ti, and NVIDIA TITAN RTX
  • NVIDIA Quadro RTX 3000 (laptop only), RTX 4000, RTX 5000, RTX 6000, RTX 8000

NVIDIA also provides a ray tracing fallback for some previous generation graphics cards:

  • NVIDIA GeForce GTX
    • Turing generation: GTX 1660 Super, GTX 1660 Ti
    • Pascal generation: GTX 1060 6GB, GTX 1070, GTX 1080, GTX 1080 Ti
  • NVIDIA TITAN V
  • NVIDIA Quadro: P4000, P5000, P6000, V100

Basically, import the HDRP package and go through the wizard to set up the render pipeline with DXR support.

Like this:

Unity HDRP Wizard DXR Setup

After restarting your editor, you are good to go.

2. Enable Colored Shadows in Your Directional Light

Ok, let's start with the fun.

Inspect your directional light and under shadows, enable:

  • Screen-Space Shadows
  • Ray-Traced Shadows
  • Color Shadow

Like below.

Unity Directional Light Color Shadows

Unity Directional Light Color Shadows

The lighting setup is now ready.

Next step? Setting up your window-like material.

3. Set Up Transparency and Refraction in Your Material

Make sure you are using the HDRP/Lit shader.

Here's what you need to set in your window material:

  • Base Map: a texture/color with low alpha values.
  • Surface Type: transparent.
  • Refraction Mode: choose the best-fitting shape, e.g. box.
  • Transmittance Color: a texture/color for the shadow projection.

Here's an example:

Unity Colored Shadows Material Setup Refraction

Unity Colored Shadows Material Setup for Refraction

  • Lighting set up? Check ✓
  • Material set up? Check ✓

Output?

Unity Colored Shadows Test

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.

The Ultimate Result: Ray-Traced Colored Shadows in Unity

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.

The result?

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.

Unity Ray-Traced Colored Shadows

  • Monochrome Shadows
  • Colored Shadows

The Performance Cost of Ray-Traced Colored Shadows in Unity

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...

What's Next?

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:

  • Execute it
  • Tick it ✓
  • Next

And done.

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.

~Ruben

(The Gamedev Guru)

The Gamedev Guru Logo
  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Rubén Torres Bonet

    About the author

    Born Game Developer, now ready to help you develop better games. Primary programmer on Star Trek Bridge Crew (Oculus Quest), Diamond Dash. Programmer on Time Stall, Catan Universe, Anne Frank House VR, Jelly Splash, Blackguards Definitive Edition. I also worked in minor XR experiences for HoloLens and Vive for clients such as Audi and Volkswagen.

    You might also like

    The Bright Side of Ray-Traced Global Illumination in Unity
    >