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

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 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 $15.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