官术网_书友最值得收藏!

How to do it...

Once the StandardColor shader is ready, we can start changing its properties:

  1. In the first line of the script, update the name to the following:
Shader "CookbookShaders/Chapter 02/StandardColor" {

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

  1. In the Properties block of our shader, remove the current property by deleting the following code from our current shader:
_MainTex ("Albedo (RGB)", 2D) = "white" {} 
  1. 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 inside of the SubShader section:
sampler2D _MainTex; 
  1. 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; 

Just as you may be used to the float type being used for floating point values when writing code in C# and other programming languages, fixed is used for fixed-point values and is the type used when writing shaders. You may also see the half type used as well, which is like the float type but takes up half the space. This is useful for saving memory but is less precise in how it is presented. We will be discussing this in much greater detail in the Techniques to make shaders more efficient recipe in Chapter 8, Mobile Shader Adjustment.

For more information on fixed-point values, check out https://en.wikipedia.org/wiki/Fixed-point_arithmetic.

The 4  in fixed4 stands for the fact that the color is a single variable that contains four fixed values: red, green, blue, and alpha. You will learn more about how this works and how to modify these values in more detail in the next chapter, Chapter 3Surface Shaders and Texture Mapping.

  1. 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 any more. To complete the refit of this shader, let's add one more property to the Properties block and see what happens. Enter the following code:
_AmbientColor ("Ambient Color", Color) = (1,1,1,1) 
  1. We have added another color swatch to the material's Inspector tab. Now let's add one more in order 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 
  1. 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:
  1. 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.

主站蜘蛛池模板: 荆门市| 旬阳县| 凤阳县| 额敏县| 寿光市| 越西县| 常德市| 东源县| 冕宁县| 阆中市| 潞西市| 香格里拉县| 定南县| 神池县| 萝北县| 山西省| 乌拉特后旗| 新泰市| 长海县| 二手房| 吐鲁番市| 天峻县| 福泉市| 梁山县| 海兴县| 理塘县| 乳山市| 正定县| 绍兴县| 手游| 韶山市| 尉犁县| 嘉义县| 获嘉县| 定襄县| 厦门市| 玛纳斯县| 偃师市| 贵德县| 新安县| 龙川县|