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

Optimizing Your Code, or Making Your Own Pipeline?

In the previous chapter, we explored some of the tools Unity provides us with for creating efficient cross-platform shaders. We talked about built-in shader compilation tricks, shader branching, and useful macros.

But, of course, compiling shaders properly is not the only way you can optimize your rendering. If you want to get the most out of your shaders, you also need to make the right choices when actually coding them up.

Or, even better, if you know your renders require very specific processing, you might opt out of Unity’s default generalist pipelines and make your own!

So, in this chapter, we are going to explore a few tips and techniques for optimizing your shader code and gaining some precious extra milliseconds. To do so, we’ll talk about the following topics:

  • Picking the right shading model
  • Optimizing your runtime performance
  • Creating your own SRP

Technical requirements

To familiarize yourself with the settings panels and tools presented here, you will need to have Unity installed, with a version from 2021 or later.

For the first two sections, Picking the right shading model and Optimizing your runtime performance, you should set up a project using the URP pipeline. For the third section, Creating your own SRP, you should start a simple 3D Unity project with the legacy pipeline.

You can also find all the code files for this chapter on GitHub, at https://github.com/PacktPublishing/Become-a-Unity-Shaders-Guru/tree/main/Assets/Chapter%2010.

Picking the right shading model

To begin our exploration of scoped shader optimization techniques, let’s first discuss an important property of any shader: its shading model.

Put simply, the shading model determines how the color of your object’s surface will vary depending on its orientation, the position of the camera, or the lights in the scene. In other words, it is the set of mathematical computations that the engine will have to do in order to render your material for the current context.

Over the years, technical artists have developed a whole gallery of shading models to represent various types of surfaces and recreate various visual styles. While some models are dedicated to reproducing reality as accurately as possible (most notably, physically-based shading), others are simpler processes that either approximate realism or take a completely different route and apply their own look and feel to the render.

We’ve already touched upon this idea in...

Optimizing your runtime performance

There are various interesting low-level optimizations you can do to improve your shaders and keep your code fast.

In the following sections, we will see why the precision of your computations matters, which mathematical operations are considered “complex” and can slow down your shaders, and how some particular cases such as alpha testing may benefit from platform-specific implementations. Finally, we’ll discuss how picking the right esthetics for your project can sometimes help with technical limitations.

Float, half, or fixed?

If you’re a bit familiar with Unity shaders, then you’ve probably already seen that, depending on the tutorial you look at, fragment shader functions may return either float4 or half4. This might be surprising at first: how come the same function can use different types like this, and why choose one over the other?

Well, a key idea to keep in mind when doing intensive calculations...

Creating your own SRP

Developing performant code is never easy, and rendering is no exception. The famous programming mantra of “make it work, make it right, make it fast” typically applies very well to coding shaders. Usually, you’ll need to iterate multiple times over your scripts to really get the effects right, and then when the optimization phase comes, you’ll have to once again rework the whole thing to squeeze the performance out of your code.

We’ve seen, in the two first sections of this chapter, that this optimization step can be about choosing the right option for your shader lighting mode, or even doing some low-level data type conversions. But there might come a day when these fall short for you – if your project is really too special, and if it leverages really unique visual features, then perhaps Unity’s built-in options won’t be enough.

Because, despite our best efforts, we haven’t yet reached a point...

Summary

In this chapter, we explored various techniques and tools for improving the efficiency of our shader code.

We first talked about lighting models and discussed how Unity provides us with several built-in ones. We saw that they always do a trade-off between realism and power usage and that your target platform has a big impact on the kind of lighting you can use in your games.

Then, we dove into low-level optimization and studied different scoped improvements for shader code, from per-platform computation precision to mathematical operation complexity, alpha testing caveats, and even SRP Batcher compatibility.

Finally, we had a peek at another approach and introduced the basics of writing a custom SRP shader in Unity to completely tune the rendering process to our liking. We saw how the engine gives us easy-to-use and safe tools for customizing our visuals, and how we can debug the render steps in detail thanks to the Frame Debugger window.

As a final note, always...

Going further

If you’re curious about URP shader code and making your own SRP, the following are a few interesting resources to check out or continue your journey from.

Shader code optimization

Custom SRPs

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}