Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x Shaders and Effects Cookbook

You're reading from   Unity 5.x Shaders and Effects Cookbook Master the art of Shader programming to bring life to your Unity projects

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781785285240
Length 240 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Alan Zucconi Alan Zucconi
Author Profile Icon Alan Zucconi
Alan Zucconi
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Creating Your First Shader 2. Surface Shaders and Texture Mapping FREE CHAPTER 3. Understanding Lighting Models 4. Physically Based Rendering in Unity 5 5. Vertex Functions 6. Fragment Shaders and Grab Passes 7. Mobile Shader Adjustment 8. Screen Effects with Unity Render Textures 9. Gameplay and Screen Effects 10. Advanced Shading Techniques Index

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:

  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:
    How to do it…

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:

How it works…

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

Color

This creates a color swatch in the Inspector tab that opens up a color picker = (float,float,float,float)

2D

This creates a texture swatch that allows a user to drag a texture in the shader

Rect

This creates a non-power-of-2 texture swatch and functions the same as the 2D GUI element

Cube

This creates a cube map swatch in the Inspector tab and allows a user to drag and drop a cube map in the shader

Float

This creates a float value in the Inspector tab but without a slider

Vector

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.

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image