Basic

November 10 2020

0 comments

The Bright Side of Ray-Traced Global Illumination in Unity

By Rubén Torres Bonet

November 10, 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__

Let's get back to ray tracing with global illumination in Unity. Discover what global illumination is and choose the better fit for your game:

Baked GI

Screen-space Raster GI

Screen-Space Ray-traced GI

Can You Guess What's Missing in This Scene?

Take a second to have a look at the screenshot below.

Can you guess what is this picture missing to be... visually "appealing"?

It's looking either too bright or too dark. That's not an easy thing to see in real life.

Why?

Because light bounces between surfaces.

These bounces soften the "transitions" between bright and dark areas. And you don't see that happening in the screenshot above. 

Now, let's see something more realistic.

Have a look below. Use the slider to transition to a more "visually correct" scene:

Is Global Illumination Overrated? (Answer: NO)

  • Global Illumination OFF
  • Global Illumination ON

What's the difference?

Global illumination (GI). To be more specific, indirect illumination.

Let's quickly explain how GI works.

What's Unity Global Illumination?

Like I said, light rays bounce off surfaces and lose energy in the process (e.g. brightness, a change in color, etc.). Eventually, light will reach your eyes from different surfaces.

In graphics, we distinguish between:

  • Direct lighting: the sun light hits the floor and reflects into your eyes. That lets you see that specific fragment on the floor.
  • Indirect lighting: the sun light hits the floor, bounces to your hand watch and then bounces towards someone's eyes. That person becomes temporarily stunned.

The difference is that GI takes more bounces into account. Not just one.

Direct lighting is easy and cheap to calculate. Indirect lighting, not so much.

So, how do you do indirect lighting* in Unity?

Here are your options.

* I use the words indirect lighting and global illumination interchangeably. In theory, they're not the same. But in practice, we treat them as if they were.

Your Options for Global Illumination in Unity

Let's build an ultra-simple scene to see and compare the different methods you have in Unity to do global illumination.

Here's the scene we are building:

Light Bounces Global Illumination Diagram

First, take a deep breath and enjoy my drawing skills.

Now, let's explain it.

A wall is covering/occluding another wall, which in real-life would prevent light from reaching it.

However, there's a small gap below.

This bottom gap will allow some sun rays to "escape" the big wall and reach the wall behind after these rays bounce off the floor.

The diagram is clear, right?

Here's how our baseline (just direct lighting + shadows) looks in Unity.

Global Illumination Scenario

We won't use that angle. It's hard to see the effect of indirect lighting later on.

Here's a better angle.

Global Illumination Baseline (No GI)

The wall behind is quite dark.

No global illumination means light is not bouncing. Our black shadow on that wall remains untouched with no secondary light rays coming in.

Not what you want in your game. Nope.

So let's improve this scenario and add some GI with our first method: baked global illumination (Unity Lightmapper).

Unity Baked Global Illumination

Baking global illumination is the cheapest way to do global illumination.

We do all these expensive calculations offline (in the editor). That process produces textures that we later "paint on top" of our scene.

It takes some effort to get the results you want with this method, as there are many settings to play with. And baking takes time.

But once you get it right, it looks great.

In fact, it's probably the best-looking method out there to do (static) global illumination.

The downsides?

Well, it only works with static geometry.

I mean, you can use light probes to add some indirect illumination to dynamic (moving) elements. But that also goes with many downsides.

Anyway, let's see how it looks.

Use the slider to see the evolution from no GI to baked GI.

Baked Global Illumination

  • Global Illumination OFF
  • Baked Global Illumination

Just like I promised you, it looks more... correct*.

* You know what I mean. Not the simplified scene per se, but the indirect lighting.

However, did you ever play an interactive game with just static elements?

No, you say?

That's why we need more options.

One is to use light probes together with baked GI. The issue with light probes is that you need many of them to have smooth indirect lighting transitions. And guess what? That's going to break batching for good.

Let's consider instead something else: Screen-space (Raster) Global Illumination.

Unity Screen-Space Global Illumination (SSGI)

The idea with SSGI is simple. It's execution is not.

We analyze the rendered frame data and guesstimate the way light should bounce between surfaces that are within that frame.

What frame data?

  • The normal buffer.
  • The depth buffer.
  • The color buffer of the previous frame.

Again, we don't do that for the whole scene. Just for the current rendered image.

Doing this effect in screen-space makes this option affordable in terms of performance.

Use the slider below to find out what SSGI paints on top of our baseline.

Screen-Space Global Illumination (SSGI)

  • Global Illumination OFF
  • Screen-Space Global Illumination

A small tint of red.

Not much else.

Here is in detail the indirect lighting that we are adding to our baseline:

Unity Global Illumination: Screen-Space Effect

That's a very shy effect. Quite different from baked GI, isn't it?

That's one of the limitations of SSGI: accuracy. Two reasons for this:

  • We have incomplete information (the G Buffer, information about our lights and that's about it).
  • We have a low performance budget: we need to keep this effect cheap.

But hey, it works with dynamic elements. And in real-time 🙂

Can we do better?

Yes!

What if we used ray-tracing for global illumination?

After all, with ray-tracing we have access to off-screen data. And that removes a limitation of this method.

Let's check out ray-traced SSGI.

Ray-Traced Global Illumination in Unity

This is getting exciting.

If your project is ray-tracing-ready, you just need to click a checkbox in your screen-space global illumination effect: enable ray-tracing.

The result?

Use the slider below to discover it.

Ray-Traced Global Illumination

  • Global Illumination OFF
  • Ray-Traced Global Illumination

Juicy.

Here's the singled-out effect that ray-tracing GI is adding to our baseline:

Unity Global Illumination: Ray Traced Effect

Indeed, ray-tracing gets you good-looking indirect lighting. And it works with dynamic elements... in real-time.

Huge deal.

The question is, can you really afford ray-traced global illumination in Unity?

Let's analyze the performance cost of these effects.

The Performance Cost of Ray-Traced Global Illumination in Unity

Here are some quick tests I ran with:

  • CPU: AMD Ryzen 2700X
  • GPU: TITAN RTX beefy edition
  • Dataset: 3000 frames taken on the editor
  • Scene: HDRP template
  • Versions: Unity 2020.2.0b7 + HDRP 10.0.1

Here are the average frame times at full-HD resolution:

  • Baked GI: 27.64ms
  • Baked GI + SSGI (low quality): 28.19 ms
  • Baked GI + SSGI (medium quality): 30.36 ms
  • Baked GI + SSGI (high quality): 32.96 ms
  • Baked GI + Ray-Traced SSGI (performance mode — low quality): 32.07 ms
  • Baked GI + Ray-Traced SSGI (performance mode — medium quality): 32.88 ms
  • Baked GI + Ray-Traced SSGI (performance mode — high quality): 34.81 ms
  • Baked GI + Ray-Traced SSGI (quality mode): 107.91 ms (ouch)

And here are some of these numbers at 4K resolution:

  • Baked GI: 106.21ms
  • Baked GI + SSGI (medium quality): 114.56
  • Baked GI + Ray-Traced SSGI (performance mode — medium quality): 119.77 ms
  • Baked GI + Ray-Traced SSGI (quality mode): 421 ms (OUCH)

Why did I take them all with baked GI, you might ask? Well, I removed the baked lightmaps and the entire scene broke for good. Like visually, all was pure white.

Don't take these times as absolute figures.

After all, it's a specific scene, a specific software/hardware setup and within the editor.

But you can compare the relative frame times and see how performance scales on this high-fidelity scene.

You get the idea.

Two interesting questions arise (and two subjective answers):

  • Are we ready for 4K real-time ray-traced GI? Nope.
  • Ok, and are we ready for 1080p real-time ray-traced GI? Yeah, maybe.

It depends on your game.

But if you put enough optimization effort into it, you can get real-time ray-tracing global illumination in. It's feasible.

Just run it on performance mode at 1080p and you'll be fine on most NVIDIA 2000+ cards (and maybe even on the NVIDIA 1000 series).

And the importance of performance brings me to...

Next Steps

How much did you like real-time ray-traced global illumination?

Well, you can get RTGI in your game if you make enough performance room (and all other Unity ray-tracing effects as well).

And nothing better* than my Unity Performance Optimization Checklist for that.

* Unity performance consulting is better (but not many can't afford it).

Till next time,

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
>