Reader small image

You're reading from  Become a Unity Shaders Guru

Product typeBook
Published inJul 2023
Reading LevelN/a
PublisherPackt
ISBN-139781837636747
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Mina Pêcheux
Mina Pêcheux
author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux

Right arrow

The Power of Ray Marching

In Chapter 6, we saw a few techniques to boost basic geometry and add details to cubes or planes using textures and clever shading tricks. We explored how parallax mapping and interior mapping are nice and easy-to-understand techniques because they make you manipulate common asset types, namely meshes and images.

The issue with these methods, however, is that they require your team to produce the aforementioned assets. If the members of your game development team don’t have the time or skills to create the right textures or the right base geometries, you won’t be able to apply your shader to anything!

To counteract this issue, a growing trend is procedural generation, which basically is about using controlled randomness to auto-create viable parts of your game based on a set of rules. This idea of scripted generation is particularly interesting if you are more into development than art, you want to produce assets quickly and in a modular...

Technical requirements

To try out the samples yourself, you will need to have Unity installed, with a version from 2019 or later. You will then need to create either of the following:

  • A project with the common 3D template, which you will then upgrade to use the URP or HDRP render pipeline (see the Stepping up with the URP render pipeline section of Chapter 2)
  • A project with the new 3D URP or 3D HDRP template (see the Stepping up with the URP render pipeline section of Chapter 2 for guidance on how to download and pick this template)

We will also rely on the custom scriptable render feature we coded in Chapter 7, so make sure you either implement or download the URPComputeAsset.cs, URPComputePass.cs, and URPComputeFeature.cs files from the GitHub repository of this book, at https://github.com/PacktPublishing/Become-a-Unity-Shaders-Guru/tree/main/Assets/Chapter%2008.

Understanding the fundamentals of ray marching

Before diving into the practical applications and implementation tricks of ray marching, let’s first have a quick run through the concepts it relies on.

In the upcoming sections, we will first introduce the concept of signed distance functions (SDFs) for representing arbitrarily complex shapes, then understand how this notion can be used to actually compute 3D renders, and finally, have a look at how to add shading to ray marching visualizations.

Describing a shape with SDFs

At the core of the ray marching process is a concept called the signed distance function, or SDF. To put it simply, SDFs are mathematical functions that take a point in space (represented by its X, Y, and Z coordinates) and return how far it is from a surface.

It may seem like a strange idea to compute surfaces at first, but the power of SDFs is that they can describe quite complex shapes in just a few lines, and they allow us to render surprising...

Entering a world of primitives...

We saw in Chapter 7 that compute shaders are an excellent way to process large data quickly – which is typically what we need for ray marching, since in every frame, we have to step through many rays (one per pixel) to render the scene!

Our ray marching logic will therefore be written in a compute shader file, and then called from a C# script at the time of the render as a full-screen effect. So, the process will reuse the custom scriptable render feature (URPComputeFeature) we prepared in Chapter 7, and we will go through the same steps as in the Applying a compute shader-based screen effect in URP section of Chapter 7.

In the upcoming sections, we will therefore start by preparing a basic compute shader to render a single sphere using ray marching, and then improve it to handle multiple shapes of different types.

Rendering a sphere with ray marching

To begin with, let’s set up a first basic ray marching compute shader to...

Studying a common use case – the rendering of volumetric clouds

So far, we have seen how ray marching allows us to create arbitrarily complex shapes based on SDFs and 3D surfaces. This makes it easy to create procedural scenes with solid shapes, like the ones shown in Figure 8.12.

But ray marching can also be taken one step further, to actually render 3D volumes. For example, thanks to this technique, we can create fairly realistic clouds like these ones:

Figure 8.13 – Volumetric clouds rendered on the scene skybox using ray marching

Figure 8.13 – Volumetric clouds rendered on the scene skybox using ray marching

So, our goal here will be to create a material shader for the skybox of the scene, to have it display volumetric clouds similar to the ones shown in Figure 8.13.

In the following sections, we are going to implement our shader step by step and discuss how we can adapt our ray marching principles to the case of volume rendering. Then, we will see how we can use mathematical noise to create interesting shapes...

Summary

In this chapter, we explored the technique of ray marching and discovered how to apply it to the rendering of simple shapes or more advanced effects, such as clouds.

We first talked about the theory behind ray marching and focused on the importance of SDFs for defining arbitrarily complex surfaces.

Then, we saw how to implement a basic ray marching algorithm in Unity to render either a sphere or several random shapes. We also took this opportunity to reuse the compute shaders we introduced in Chapter 7, and we discussed how this technique allows us to create an infinity of procedural scenes quickly and easily.

Finally, we dived into a famous application of ray marching: the rendering of volumetrics such as clouds. We made our own skybox shader that creates a blue sky filled with clouds and used ray marching to give the clouds more volume. We then simulated light absorption and transmittance using color gradients and clever visual tricks.

This concludes our overview...

Going further

If you’re curious about ray marching and volumetric cloud rendering, here are a few interesting resources to check out or continue your journey with:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Become a Unity Shaders Guru
Published in: Jul 2023Publisher: PacktISBN-13: 9781837636747
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux