Web Class: Create your First Shader in Unity Shader Graph

Web Class: Create your First Shader in Unity Shader Graph

You can access the full course here: Shader Graph for Beginners

Transcript

Hey, everyone, and welcome back to the course. In this lesson, we are going to go over how to create a new shader for Shader Graph, and then we’re gonna go over what is Shader Graph, how does it work and what all the various different components in that do, how to connect nodes, how to then create that into a material that we can apply to our model.

So, first of all, I’m going to create a new folder here called Shaders. And this is where we are going to store all of our various shaders we create with Shader Graph. So in here, I’m gonna right-click. To create a brand new shader, we can right-click here in the folder, go to create. Then we wanna hover over the Shader selection and choose PBR Graph. This creates us a Shader Graph. And I’m just gonna call this one our ExampleShader. And as you can see, it creates the shader folder, which is this S here.

And to open the Shader Graph editor, we can either double-click on this file or go up to this Open Shader Editor button in the Inspector. Click on that. Then we should open is a window something like this. It is a graph here that we can then edit and create our shaders in.

So how does it work? Well, Shader Graph is set up using nodes. You connect various nodes together, which can do various different things, like add two effects together, create new ones, allow for inputs. And the final result all plugs into the PBR Master node right here, which is something similar to how you would look at the standard asset, how you look at the standard shader. You have your Albedo, Normal, Metallic, Smoothness, Alpha, all that sorta stuff that, you would want to change the overall appearance of your model.

So, how do we actually navigate? First of all, well what we can do is to move around, we can hold down middle mouse button and start dragging around to just navigate around. You can also zoom in and out with the scroll wheel to have a much better view of your shader. And also, then, to create new nodes, we can right-click and go Create node. And here you’ll see that there are a bunch of various different categories, Artistic. It’s just a various number of selections.

We won’t, of course, be using all of these. We’ll probably be using only around maybe 20 or so different nodes throughout this course. We’ll go over in detail what they do and how they work with the other nodes, as well. But for now, let’s just have a look at some of the other aspects inside of the Shader Graph here.

First of all, you might see up here where it says example shader. This over here is the Blackboard. What the Blackboard is is basically like a variable list. If you’ve ever looked at a material, such as the standard shader or any other sort of materials you’ll see – you’ll notice that in the Inspector there are a bunch of different properties that you can edit, like the color, the smoothness, the metallic level, the mission, all these various different properties that you’ll also if you were to create a variable in a script. So that is basically what the Blackboard is.

To create a new variable or property, we can go and click on this plus icon here. And then we can choose between a bunch of different data types here. Now you might notice that you don’t have your traditional ints or floats here like you do in C#. Instead, if you wanna create just a number, you would select Vector1. And here we could name it something like MyNumber. And then we can enter in a default value for that we can choose if it is Exposed or not. This is pretty much similar to is it public or private does it appear in the inspector for us. This Reference here is if we want to access this property inside of a script, we will be accessing it by this reference, you can change that if you wish.

Some other variable types, you can have your colors, so you can see a default color here in the Color wheel. You can also choose that to be a HDR color, which stands for high dynamic range and allows you to add intensity. So if you do have things such as bloom, or post-processing effects like we currently do in our project, you’ll be able to see these colors more intensely, and they’ll appear to be very broad.

With this, then you can also, create other variables such as Texture2D to allow for inputs of textures. We will be using this in quite a few of our shaders. And that is pretty much all of the different examples will be using for us. You can of course guard for many more of these and have look at how they work. But for now, let’s go over how node work and what we can do with them.

Now a good starting point when working with shader node, when working in the shader, is to create an input node. Now one input node is it’s something that takes from the world or takes from the player’s input. This can be various things such as a number. An example here, for example, we could have one of the properties in the Blackboard, we could also have something like the position of the object, the position of the different vertices of the faces of the object or the normal direction of the object, or we could have something more procedural like some noise, or some randomly generated aspect like so to create a shader.

For example, I’m actually going to bring in the color here. So I can just actually drag this in. And as you can see, it will create a brand new node with our property. And what you can see here is actually a little dot, and what we can do is we can click and drag on that dot. And this will create a connection. Now what we can do is connect this to an input of another node. And a node has two things on it, a node has either and a node has birth one of either – an input and an output.

As you can see here, the property node only has an output because this in and of itself is an input node. And the PBR Master node here, only has inputs as this is pretty much the end, the end position for everything inside of the Shader Graph. There are other nodes that do have birth, and many nodes actually have birth, we could get a node here, we can go create node and we can actually create an add node.

So we can go Add. And what this add node does is it takes two inputs, and adds those values together into the output here. So what we could do is create two colors, and plug them into the add node. And what that would do is add those colors together and create a result and color. So let’s actually do that right now, by going it’s dragging out color here into the A1 input of the add node. As you can see it added like so. And what we could then do is create another color down here.

Now we could create a color here in the Blackboard. But we could also create what you could call hardcoded properties by just going – which is right-clicking here, go Create Node, we’ll search for Color. And it’ll create us a color input note here that we can just edit inside of the shader graph. So it – we’ll drag that also into the add node here.

Now let’s actually set a value for our default color appeal. Let’s set this to maybe a red. And then this one down here, we could set maybe to a green. And as you can see, if you have red plus green, that equals yellow. And then what we can do is actually take this output of the add node and plug this into albedo color, which is going to be what the actual final material looks like. So we can drag this out, plug it into Albedo. And as you can see down here in the main preview, we can actually see what the resulting shader will look like when applied to a model.

Also, in the main preview, you can increase the size, you can drag it around like so you can click on this little white rectangle down here to increase the size or decrease the size. I’m just going to keep this and drag it right down to the bottom here. You can do the same with the with the Blackboard – click on this little white rectangle here to increase the size and drag it around, it’s going to put it back up here as well.

And now what we can also do with the main preview is make it so that it can display a different type of model. We can right-click on the main preview, and we can choose between Sphere, you can have Capsule, we can have a Cube, we can also put a Custom Mesh. So you can click on that. Let’s choose a robot too. And then as you can see, this is what it looks like when applied to a robot model.

That is pretty much the basics of the Shader Graph.

Another thing there is that some nodes like the add here actually have a preview just showing you what so far in the process it looks like. So when we add those two colors together, it creates a preview of the color something all session is that the add node or isn’t just for colors. It’s pretty much for many different things. These aren’t like in C#, where you have very strict rules with what can go where what can be added together. Because in Shader Graph, you can pretty much add various different things together.

And you can get surprising results will be adding stuff like numbers together, colors together – we’ll even be adding colors and stuff like noise maps together to create a colored noise map and various other things as well.

Okay, but how do we actually set this as a material and apply it in game? Well, first of all, let’s click on the Save Asset button up here to save this asset. And then we can just click on the X up here to exit out of the shader graph. And we can then go to assets folder materials. And I’m going to right-click go create. I’m going to create a new material code, test material. And then to set this material to have a shader we can go up to the Shader here, click on this and we want to find Shader Graphs and ExampleShader and as you can see, it will pop up we also have that property’s here we have a number of property, we also have our color property.

And what we all want to do then is in our scene view, we can just simply drag and drop it onto a character. I can then change the color here. And as you can see, it will change the appearance adding this to the color red.

And in the next lesson we are going to be going over, a bunch of different nodes that will be using inside of our feature Shader Graphs, and I’ll just explain how they work and how they can even work together. So I’ll see y’all in the next lesson.

Interested in continuing? Check out the full Shader Graph for Beginners course, which is part of our Unity Game Development Mini-Degree.