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

Appendix: Some Quick Refreshers on Shaders in Unity

No matter how many tutorials you watch, and how many books you read, chances are that you won’t be able to get to the real stuff if you don’t have the basics. So, if you want to have a quick run through the fundamentals of shaders and how to create them in Unity with the built-in legacy system before diving deeper into Unity’s newest render pipelines and shader tools, go ahead and take a peek at these core notions on shaders.

In this Appendix, we will cover the following topics:

  • Diving into what shaders are exactly
  • Rendering 3D scenes on a 2D screen
  • Exploring SubShader and Pass tags
  • Recalling the basics of z-buffering and blending modes
  • Reviewing textures and UV mapping

If you feel you are a bit shaky on some aforementioned points or you would like to remember all the neat words technical artists use to talk about their masterpieces, stick with me for this rapid review of the...

Rendering 3D scenes on a 2D screen

We’ve seen that a 3D shape cannot be blitted on a screen as-is. This is because the computer just doesn’t have the required information to fill the entire set of pixels from the virtual positions of the shape’s vertices. So, in practice, how do shaders achieve this? In the following sections, we will discuss the working of shaders, both in general and in Unity.

A typical 3D rendering pipeline

Shaders are able to bridge the gap between the scarce 3D data and the denser 2D pixel information, thanks to the following three-step process:

  1. Vertex shader: First of all, the shader has to gather all the data about the shape that is necessary for its render. This is done in the vertex shader: this piece of code is run in parallel on all vertices of the 3D mesh and transforms the initial raw per-vertex data into processed per-vertex data. Usually, this process implies converting the vertex’s position from object space...

Exploring SubShader and Pass tags

First of all, the shader tags are a list of one or more key-value pairs that allow you to custom-configure your SubShader or your Pass blocks. There are several built-in ones, and you can also define your own to then access them in your C# script using the Material.GetTag API. Most of the time, however, the predefined ones are enough.

At the time of writing this book, Unity documents list a total of eight SubShader tags. Some are very specific and rarely used, but there is one that is worth addressing: the Queue tag. It defines in which render queue a geometry with this shader will be. This is one of the key options that determines in which order this geometry will be rendered compared to the others.

To have precise control over the render order of your shaders, Unity lets you choose between two types of queue values:

  • Either you use one of the built-in queues, among Background, Geometry, Alpha Test, Transparent, and Overlay (from first...

Recalling the basics of z-buffering and blending modes

Another essential component of a shader is how it interacts with the rest of the environment, or in other words, what kind of behavior you expect from an object using your shader. Should it always be rendered in front of the rest? Should it be visible both from the front and the back? Should it be partially transparent and mix with the background color?

This scene interaction can be separated into two parts:

  • On the one hand, it is crucial to decide whether your camera should render your shader in the front or in the back – to optimize the rendering process, a clever thing to do is to check whether some opaque objects are masking others, and thus we can decide whether we can ignore the ones further away or discard all the faces that are not visible from our point of view. To do this, we can rely on the technique of z-buffering.
  • On the other hand, there are still cases where you will have one or more objects...

Reviewing textures and UV mapping

To finish this quick review of the shader fundamentals, let’s have a short discussion on textures and UV mapping. Although we often think of the albedo (which means, the main color) when we hear the word texture, the truth is that shaders use a wide range of textures for diverse reasons, and they can be a valuable tool for optimizing your rendering. Of course, using 2D textures on 3D objects naturally implies converting between these two worlds, thus the need for the UV mapping process.

In the following sections, we are going to quickly refresh our knowledge of UVs and textures before discussing special types of textures and studying how to declare and use textures in a Unity shader code.

Wrapping a 2D image on a 3D shape

Textures are, as you know, 2D images. Therefore, the first step to using them in a 3D scene, and on 3D objects, is to find a technique to project this 2D plane onto the 3D shape. This is done via the process of UV...

Summary

In this Appendix, we recalled the fundamentals of shader theory and various useful tips for writing shaders in Unity.

We recalled the three steps of a common rendering pipeline (the vertex shader, the rasterization, and the fragment shader), before showing how they are implemented in a basic Unity ShaderLab shader code. We also discussed Unity shader configuration with tags, z-buffering options, and blend modes. We followed that up with some reminders on UV mapping and how to handle textures in the game engine.

With this review of all the essential notions of shaders done, you should now be clear on what shaders are, how they are implemented in Unity with the built-in render pipeline, and what a ShaderLab script usually looks like.

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 €14.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