Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Become a Unity Shaders Guru

You're reading from  Become a Unity Shaders Guru

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781837636747
Pages 492 pages
Edition 1st Edition
Languages
Author (1):
Mina Pêcheux Mina Pêcheux
Profile icon Mina Pêcheux

Table of Contents (23) Chapters

Preface 1. Part 1: Creating Shaders in Unity
2. Chapter 1: Re-Coding a Basic Blinn-Phong Shader with Unity/CG 3. Part 2: Stepping Up to URP and the Shader Graph
4. Chapter 2: The Three Unity Render Pipelines 5. Chapter 3: Writing Your First URP Shader 6. Chapter 4: Transforming Your Shader into a Lit PBS Shader 7. Chapter 5: Discovering the Shader Graph with a Toon Shader 8. Part 3: Advanced Game Shaders
9. Chapter 6: Simulating Geometry Efficiently 10. Chapter 7: Exploring the Unity Compute Shaders and Procedural Drawing 11. Chapter 8: The Power of Ray Marching 12. Part 4: Optimizing Your Unity Shaders
13. Chapter 9: Shader Compilation, Branching, and Variants 14. Chapter 10: Optimizing Your Code, or Making Your Own Pipeline? 15. Part 5: The Toolbox
16. Chapter 11: A Little Suite of 2D Shaders 17. Chapter 12: Vertex Displacement Shaders 18. Chapter 13: Wireframes and Geometry Shaders 19. Chapter 14: Screen Effect Shaders 20. Index 21. Other Books You May Enjoy Appendix: Some Quick Refreshers on Shaders in Unity

Shader Compilation, Branching, and Variants

In the first half of this book, we discussed various shading techniques that can help you create interesting visual effects in your game projects. We saw how to use Unity’s new URP pipeline and, in particular, its node-based shader editor – the Shader Graph.

Coding or branching up a shader to render a specific visual on screen is, of course, an essential skill for any technical artist, but there is something else that you should take into consideration if you want to master shaders: the art of optimization!

Indeed, knowing the inner workings of the tools you use is crucial to getting the most out of them, and this is especially the case with shaders, because of how compute-intensive they are. Moreover, nowadays, with the growing trend of mobile games, ensuring the best performance for your visuals is becoming paramount to your product actually being cross-platform.

So, in this chapter, we will highlight some of the...

Technical requirements

To familiarize yourself with the settings panels and tools presented here, you will need to have Unity installed, with a version from 2022 or later. For good measure, you should create either of the following:

  • A project with the common 3D template, which you will then upgrade to use the URP or HDRP render pipelines (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)

This chapter does not rely on any specific scripts or assets.

Learning some Unity shader compilation tricks

As a seasoned shader artist, you probably know that when it comes down to it, rendering is no piece of cake. If you’ve ever tried to make your own rendering system from scratch instead of relying on a game engine such as Unity and its pre-made rendering pipelines, then you know that writing shader code is just one step among many to actually display something on the screen. Out in the wild, you also have to worry about graphics backends, platform-specific compilation, cross-platform API compatibility, and much more!

And with the ever-growing variety of possible target devices, and in particular, the growing usage of mobiles and tablets for gamers, saying that you are going to produce a fully cross-platform game means you’ll need to think about dozens of graphics APIs and backends that can be very different from each other.

Now, of course, this is where 3D and game engines shine: by abstracting away this low-level rendering...

Taking advantage of shader branching and shader variants

When you first started your journey in the world of shaders and you took your first steps writing vertex and fragment functions, life was easy: you wrote a single piece of code that always executed the same (see the examples shown in the Appendix: Some Quick Refreshers on Shaders in Unity, or in Chapter 1).

But now that you are getting into more advanced rendering schemes, you might want to introduce some conditional behavior into your shader code so that it executes differently under different circumstances – for example, because of one of the following reasons:

  • You want to distinguish between two target platforms and their respective graphics backends
  • You don’t want to execute expensive code such as vertex inputs or large loops
  • You want your shader to sample a different texture depending on some instance-specific data

For all those cases, you might need to add some branching logic to...

Exploring shader macros and platform-dependent compilation

Now that we have discussed various tricks to optimizing the organization of our project shader assets and adding conditional behavior with branching or variants, let’s focus on another key aspect of cross-platform development for technical artists: handling the rendering specificities of each platform.

In this last section, we will explore various built-in macros that Unity provides us with for checking the type of platform we are building for, looking up the current Unity version, normalizing UV direction, and more.

Identifying the target platform

When working with shaders, it is fundamental to know what the target platform for your project is. Because although this obviously impacts many other areas of game development (such as UI/UX for controls, or responsive interfaces), the rendering tools you have at your disposal are inherently linked to the kind of device the game will run on.

Game engines such as...

Summary

In this chapter, we discussed some inner workings of Unity’s shader compilation system, and how advanced technical artists can leverage a few settings and clever tricks to further improve the performance of their shaders.

We began by presenting some important points about shader management in Unity, such as caching, asynchronous compilation, and AssetBundles organization.

We then explored the various forms of branching we can use in our Unity shaders, and what should be used in which case, between static branching, dynamic branching, and shader variants.

At the end, we listed some commonly used #pragma directives, and we saw how built-in macros and preprocessor directives can help us with platform-dependent compilation and cross-platform distribution.

In the next chapter, we will continue this talk on shader optimization and dive more into practical tips for improving the performance of your shaders through coding decisions. We will also quickly introduce...

Going further

If you’re curious about URP shader optimization (in particular, using shader compilation tricks, shader branching, and shader variants), here are a few interesting resources to check out or continue your journey from:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Become a Unity Shaders Guru
Published in: Jul 2023 Publisher: Packt ISBN-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.
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 $15.99/month. Cancel anytime}