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

Screen Effect Shaders

Our journey through the world of modern Unity shaders is almost over, and we’ve looked at quite a number of examples. In the past few chapters, we explored a gallery of 2D and 3D shaders, and we learned several handy tricks for boosting our visuals and creating easy-to-tweak effects on our sprites and meshes.

In this final chapter, we’ll discuss one last common type of shader – the fullscreen effect shader. These are global filters applied to the entire render image that make it possible to make our own custom postprocessing effects and improve the overall atmosphere of the scene.

Still, if you’ve ever lurked around the Unity forums and the shader-related threads, you’ve probably seen how the arrival of URP has sadly complicated things a little for screen effects... so we’ll definitely have to take a moment to talk about the required setup for using fullscreen shaders in URP.

In this last chapter, we’ll...

Technical requirements

To try out the shader examples shown in this chapter, you will need to install Unity with a version from 2022.2 or later and make a project using the URP pipeline.

All the shaders and assets for this chapter are also available in the GitHub repository of this book at https://github.com/PacktPublishing/Become-a-Unity-Shaders-Guru/tree/main/Assets/Chapter%2014

Using fullscreen shaders in URP

Before we get to actually creating fullscreen shaders, let’s take a second to see how this kind of shader works in URP, and in particular how this render pipeline evolved to gradually make this feature easier and easier to use.

In this first section, we’re going to have a quick look at the old way of doing things with the built-in render pipeline, and then we’ll see how the URP pipeline changed over the years to eventually incorporate fullscreen shaders as a readily available tool.

The case of the built-in render pipeline

In the past, any shader creator who worked with the built-in render pipeline was fairly pleased to create a fullscreen effect shader, because it was simple. You would just need to do three things:

  1. Prepare your screen shader.
  2. Create a new material asset using this shader.
  3. Add a MonoBehaviour script to your scene camera object containing the OnRenderImage function, to essentially apply this...

Creating a security camera effect

To continue our study of fullscreen shaders, let’s work on a classic security camera-like render, with a single-colored image, some glitchy scanlines, a bit of lens distortion, and a grainy filter (see Figure 14.10).

Figure 14.10 – Demo of our soon-to-be fullscreen Security Camera shader

Figure 14.10 – Demo of our soon-to-be fullscreen Security Camera shader

To do this, we’ll rely on both a fullscreen shader and a couple of built-in postprocessing effects. We’ll start by implementing all the base features of our security camera effect, before adding in the postprocessing filters to boost the result even further.

Implementing the color tinting and scanlines

Our security camera effect will be handled by a SecurityCamera Shader Graph asset that is, once again, based on the Fullscreen Shader Graph URP template.

We will start by using the URP Sample Buffer node to get our initial render image, and multiply it by a single green color to tint it globally, as follows...

Making a fullscreen box blur

Up until now, we’ve seen how to implement some screen effect shaders thanks to the URP Sample Buffer node, and we’ve played around with the original render image in various ways. In this last section, we’re going to discuss another method for creating fullscreen shaders with the Shader Graph tool and apply it to a box blur filter effect.

We will start by getting a quick reminder of how the box blur effect works; then, we’ll see how to implement it thanks to a Sub Graph asset; and finally, we’ll re-integrate this Sub Graph into a fullscreen URP shader to apply our box blur filter to the whole render.

What is a box blur?

The box blur is a very simple low-pass filter that lets us easily blur an image thanks to a few mathematical operations. It relies on a basic idea, which is to replace the initial value of each pixel in the image with the average of their neighbor pixel values.

This technique is far from perfect...

Summary

In this final chapter, we’ve studied fullscreen effect shaders.

We first examined how the built-in and URP pipelines compare in terms of tooling for fullscreen effects, and we set up our first URP screen effect to turn the scene render black and white. Then, we worked on a common screen effect – a security camera-like filter that turns the image green, adds scanlines, and uses a few built-in postprocessing effects to set up lens distortion or vignetting. Finally, we talked about another method for accessing the screen data and discussed how to implement a basic 2D box blur filter effect with Shader Graph in Unity.

This marks the end of our adventure!

Throughout this book, we’ve studied the art of shaders and discussed a lot of techniques for using the new URP render pipeline in Unity. We’ve explored optimization tricks and plenty of examples, dived into writing modern Unity shaders using HLSL code, and learned how to use the node-based Shader...

Going further

If you’re curious about screen effect shaders, 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 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 €14.99/month. Cancel anytime}