Adding properties to a shader
Properties of a shader are very important for the shader pipeline as they are the method that you use to let the artist or user of the shader assign textures and tweak your shader values. Properties allow you to expose GUI elements in a material's Inspector tab without you having to use a separate editor, which provides visual ways to tweak a shader.
With your shader opened in MonoDevelop, look at the block of lines 2 through 7. This is called the Properties
block. Currently, it will have one property in it called _MainTex
. If you look at your material that has this shader applied to it, you will notice that there is one texture GUI element in the Inspector tab. These lines of code in our shader are creating this GUI element for us.
Again, Unity has made this process very efficient in terms of coding and the amount of time it takes to iterate through changing your properties.
Getting ready
Let's see how this works in our current shader called StandardDiffuse
, by creating our own properties and learning more about the syntax involved. For this example, we will refit the shader previously created. Instead of using a texture, it will only use its color and some other properties that we will be able to change directly from the Inspector tab. Start by duplicating the StandardDiffuse
shader. You can do this by selecting it in the Inspector tab and pressing Ctrl + D. This will create a copy called StandardDiffuse2
.
Note
You can give a friendlier name to your shader in its first line. For instance, Shader "CookbookShaders/StandardDiffuse"
tells Unity to call this StandardDiffuse
shader and move it to a group called CookbookShaders
. If you duplicate a shader using Ctrl + D, your new file will share the same name. To avoid confusion, make sure to change the first line of each new shader so that it uses a unique alias.
How to do it…
Once the StandardDiffuse2
shader is ready, we can start changing its properties:
- In our
Properties
block of our shader, remove the current property by deleting the following code from our current shader:_MainTex ("Albedo (RGB)", 2D) = "white" {}
- As we have removed an essential property, this shader will not compile until the other references to
_MainTex
are removed. Let's remove this other line:sampler2D _MainTex;
- The original shader used
_MainTex
to color the model. Let's change this by replacing the first line of code of thesurf()
function with this:fixed4 c = _Color;
- When you save and return to Unity, the shader will compile, and you will see that now our material's Inspector tab doesn't have a texture swatch anymore. To complete the refit of this shader, let's add one more property and see what happens. Enter the following code:
_AmbientColor ("Ambient Color", Color) = (1,1,1,1)
- We have added another color swatch to the material's Inspector tab. Now let's add one more to get a feel for other kinds of properties that we can create. Add the following code to the
Properties
block:_MySliderValue ("This is a Slider", Range(0,10)) = 2.5
- We have now created another GUI element that allows us to visually interact with our shader. This time, we created a slider with the name This is a Slider, as shown in the following screenshot:
Properties allow you to create a visual way to tweak shaders without having to change values in the shader code itself. The next recipe will show you how these properties can actually be used to create a more interesting shader.
Note
While properties belong to shaders, the values associated with them are stored in materials. The same shader can be safely shared between many different materials. On the other hand, changing the property of a material will affect the look of all the objects that are currently using it.
How it works…
Every Unity shader has a built-in structure that it is looking for in its code. The Properties
block is one of those functions that are expected by Unity. The reason behind this is to give you, the shader programmer, a means of quickly creating GUI elements that tie directly into your shader code. These properties that you declare in the Properties
block can then be used in your shader code to change values, colors, and textures. The syntax to define a property is as follows:
Let's take a look at what is going on under the hood here. When you first start writing a new property, you will need to give it a Variable Name. The variable name is going to be the name that your shader code is going to use in order to get the value from the GUI element. This saves us a lot of time because we don't have to set up this system ourselves.
The next elements of a property are the Inspector GUI Name and Type of the property, which is contained within parentheses. The Inspector GUI Name is the name that is going to appear in the material's Inspector tab when the user is interacting with and tweaking the shader. The Type is the type of data that this property is going to control. There are many types that we can define for properties inside of Unity shaders. The following table describes the types of variables that we can have in our shaders:
Surface Shader property types | |
---|---|
|
This creates a float property as a slider from the minimum value to the maximum value |
|
This creates a color swatch in the Inspector tab that opens up a |
|
This creates a texture swatch that allows a user to drag a texture in the shader |
|
This creates a non-power-of-2 texture swatch and functions the same as the 2D GUI element |
|
This creates a cube map swatch in the Inspector tab and allows a user to drag and drop a cube map in the shader |
|
This creates a float value in the Inspector tab but without a slider |
|
This creates a four-float property that allows you to create directions or colors |
Finally, there is the Default Value. This simply sets the value of this property to the value that you place in the code. So, in the previous example image, the default value for the property named _AmbientColor
, which is of the Color
type, is set to a value of 1,1,1,1
. As this is a color property expecting a color that is RGBA
or float4
or r, g, b, a = x, y, z, w
, this color property, when first created, is set to white.
See also
The properties are documented in the Unity manual at http://docs.unity3d.com/Documentation/Components/SL-Properties.html.