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

Wireframes and Geometry Shaders

In the two previous chapters, we studied various examples of shaders and focused on two specific types – 2D sprite effects and vertex displacement-based effects. When we worked on the vertex displacement technique, we saw that it was a neat way of creating procedural animations and environment decor efficiently, just by deforming the pre-existing geometry.

However, we also saw that there was a limitation to using vertex displacement, since it can only modify the vertices already present in the mesh – it cannot introduce new points or subdivisions, and it doesn’t have access to the mesh’s topology.

To go further and allow technical artists to manipulate geometry even more, there is another category of shaders worth diving into, called geometry shaders. So, in this chapter, we’ll learn more about this other tool, as well as its limitations, and we’ll discuss one of its most common applications. To do so,...

Technical requirements

To try out the shader examples shown in this chapter, you should install Unity with a version from 2021 or later, and make a project using the URP 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%2013.

As detailed in the Understanding the principles and limitations of geometry shaders section, the geometry shaders we will make here are not compatible with recent macOS X architectures, so as a Mac user, you probably won’t be able to try them out.

Understanding the principles and limitations of geometry shaders

First things first, we’re going to start this chapter by having a quick look at the foundational principles of geometry shaders and their most important limitations. In particular, we’ll focus on the specific cases of the macOS X and iOS platforms, which don’t deal well with this type of code and have to be taken into consideration for cross-platform compatibility.

What are geometry shaders?

Geometry shaders are a special type of shaders that, in addition to the usual vertex and fragment stages, also contain an additional step in the render process – the geometry stage.

Contrary to vertex and fragment shader functions, the geometry shader function is totally optional. As we saw throughout this book, there are indeed many effects and renders that can be achieved without using this stage in the shader code. However, if you want to change the geometry of your meshes in depth and you need...

Creating a URP wireframe shader

To better understand the ins and outs of these geometry shaders, let’s take a very common use case – the creation of a runtime wireframe shader. Our goal in this section will be to make the geometry of our mesh apparent live, outside of edit mode. This type of effect can be very useful for debugging, sci-fi games, or even retro-futuristic visuals à la Tron:

Figure 13.5 – An example of a wireframe shader partially applied to a spaceship model

Figure 13.5 – An example of a wireframe shader partially applied to a spaceship model

The shader will be unlit, and we will make the size of the strokes tweakable so that users can easily pick the thickness they want and modify the render to their liking. Therefore, let’s go through this shader step by step by first setting up our URP shader, then adding the geometry stage, and finally, fine-tuning a few details to get smoother visuals.

Preparing our URP wireframe shader

To begin, we’re going to create our shader as a new...

Summary

In this chapter, we’ve discussed geometry shaders.

First, we talked about the base principles behind geometry shaders – we saw what they are, how they work, and also why they can be limited or risky to use. In particular, we focused on the case of Apple devices, which are now discontinuing the support for this kind of shader, thus requiring us to think twice before using them if we need full cross-platform compatibility.

We then studied a classical geometry shader application, which is rendering a wireframe version of our meshes. In just a few dozen lines, we were able to code a URP unlit shader script that highlights the geometry of our 3D models by drawing the edges of each triangle, which even works in-game, at runtime.

We are now nearing the end of this book, and we have seen a lot of examples that have taught us various tricks about modern Unity shaders and the new URP pipeline. To wrap all of this up, our next and final chapter will examine one...

Going further

If you’re curious about geometry shaders and their limitations, here are a few interesting resources to check out to continue your journey:

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}