Book Image

Unity 5.x Shaders and Effects Cookbook

By : Alan Zucconi
Book Image

Unity 5.x Shaders and Effects Cookbook

By: Alan Zucconi

Overview of this book

Since their introduction to Unity, Shaders have been notoriously difficult to understand and implement in games: complex mathematics have always stood in the way of creating your own Shaders and attaining that level of realism you crave. With Shaders, you can transform your game into a highly polished, refined product with Unity’s post-processing effects. Unity Shaders and Effects Cookbook is the first of its kind to bring you the secrets of creating Shaders for Unity3D—guiding you through the process of understanding vectors, how lighting is constructed with them, and also how textures are used to create complex effects without the heavy math. We’ll start with essential lighting and finishing up by creating stunning screen Effects just like those in high quality 3D and mobile games. You’ll discover techniques including normal mapping, image-based lighting, and how to animate your models inside a Shader. We’ll explore the secrets behind some of the most powerful techniques, such as physically based rendering! With Unity Shaders and Effects Cookbook, what seems like a dark art today will be second nature by tomorrow.
Table of Contents (16 chapters)
Unity 5.x Shaders and Effects Cookbook
About the Authors

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.


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:

  1. 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" {}
  2. 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;
  3. The original shader used _MainTex to color the model. Let's change this by replacing the first line of code of the surf() function with this:

    fixed4 c = _Color;
  4. 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)
  5. 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
  6. 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.


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

Range (min, max)

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 color picker = (float,float,float,float)


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