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

Writing Your First URP Shader

Thanks to the first two chapters, we are now familiar with both the old way of writing shaders using the built-in render pipeline and the idea that we could improve our workflow by switching to the new Universal Render Pipeline (URP). However, we still don’t know exactly what this implies for our shader scripts, and how to actually create our effects with this new setup.

Although Unity now puts the node-based shader editing in the foreground with its Shader Graph, it is still interesting to know what goes on behind the scenes and to have an idea of how to write shader code for this new render pipeline via code. This will give you more knowledge of the overall shading pipeline and help you decipher some obscure documentation or a discreet thread on a great but rare shader effect, and be useful if your shaders require some custom functions – for which you will need to write your own scripts.

So, throughout this chapter, we will learn...

Technical requirements

To try out the samples yourself, you will need to have Unity installed, with a version from 2019 or later. You will then need to create either of the following:

  • A project with the common 3D template, which you will then upgrade to use the URP or HDRP render pipelines (see the Stepping up with the URP render pipeline section of Chapter 2)
  • A project with the new 3D URP or 3D HDRP template (see the Stepping up with the URP render pipeline section of Chapter 2 for guidance on how to download and pick this template)

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

Structuring a Unity HLSL shader

When we worked on our simple Blinn-Phong shader in Chapter 1, we discussed the syntax of Unity shaders. We saw that all these files are composed of an overarching wrapper using the ShaderLab syntax and that inside it, we find blocks such as Properties, SubShader, and Pass. The real “meat” of the code, so to speak, is the low-level shader logic written in between the CGPROGRAM and ENDCG lines. This is the hardware shader code that performs the actual computation.

All of this can be summarized as follows:

Shader "Custom/MyLegacyShader" {
    Properties { ... }
    SubShader {
        // subshader tags
        Pass {
            // pass tags
            // (optional: blend mode setup)
 ...

Having a peek at shader includes and tags

After this quick overview of the new shader script structure for URP, we now know that there has been an important change with the includes.

Thus, to better understand why and how it has changed, let’s briefly discuss how to import the new core shader tools for URP/HLSL shader scripts and how to update some of our most common built-in variables and function calls provided via macros. We will then talk about the two important shader tags you need to be aware of when writing shaders for URP.

Including the URP core module

As we discussed in the Structuring a Unity HLSL shader section, the new HLSL-based shader scripts cannot use the classic .cginc shader includes, as their predecessors did. Because they are based on the HLSL language, they have to include libraries written in that language, and more precisely in our case, modules from the new URP shader library.

The most common is the following:

#include "Packages/com...

Writing our unlit URP shader

With all this theory out of the way, it’s time to write our first URP shader! For this first example, we will go for something simple – we will design a basic unlit shader that accepts a base color and a main texture.

Figure 3.3 shows what the shader will look like on a few primitive shapes with a simple checker texture and a red color:

Figure 3.3 – Example of primitive shapes with our unlit URP shader applied

Figure 3.3 – Example of primitive shapes with our unlit URP shader applied

Important note

The shader we will design in this chapter will not be production-ready. It was simplified to facilitate the exploration of HLSL-based programming but does not use some of the basic optimization tricks URP offers (in particular, it is not compatible with the SRP Batcher).

To get ready to write our unlit URP shader, let’s follow these steps:

  1. Create a new shader script file by right-clicking on our Project panel and going to the Create | Shader | Unlit Shader menu...

Summary

In this chapter, we discussed the fundamentals of programming shaders with Unity’s new URP.

We studied the evolutions of ShaderLab and low-level hardware shader logic syntax between the built-in render pipeline and our new setup, and in particular, we zoomed into some common macro translations that are worth mentioning. We also got an overview of the frequently used tags to help the Unity engine handle cross-platform shaders that contain multiple SubShader and/or Pass tags.

Finally, we applied the theory to a simple example and implemented an unlit URP shader with two properties: a base color and a main texture. We also took this opportunity to look at the material inspector and how, with the new SRPs, it now provides us with some interesting extra options that are material-specific.

This concludes our first look at how to program HLSL-based shaders for the Unity URP – we now have a good idea of how these scripts are structured and how this structure...

Going further

If you’re curious about the fundamentals of writing URP 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 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