为表面着色器添加属性-iso_17356

时间:2024-06-28 11:40:50
【文件属性】:

文件名称:为表面着色器添加属性-iso_17356

文件大小:6.09MB

文件格式:PDF

更新时间:2024-06-28 11:40:50

Unity

1.3 为表面着色器添加属性 着色器的属性在渲染管道过程里是很重要的,因为它们是一个接口可以让使用着色器 的美工或用户指定纹理和调整着色器的值。属性可以作为 GUI 元素暴露在材质的 Inspector 面板上,而不需要单独的编辑器,它提供了可视化的方法供你调整着色器。 使用 MonoDevelop 打开你的着色器,查看第 3 行到第 6 行的代码。这些就是着色器的 Properties 块,目前,它只有一个名为 _MainTex 的属性。如果你查看对应的材质,你会注 意到 Inspector 面板上有个对应的纹理 GUI 元素,这就是使用我们的代码自动生成的。 需要再次说明的是,Unity 采用高效的编码方式实现这一过程,它花费一定的时间量通 过遍历来改变属性值。 1.3.1 如何操作 让我们通过创建自己的着色器属性,来了解 BasicDiffuse 着色器的语法以及它是如何工 作的。 1. 在着色器的 Properties 块中,删除下面的代码来移除当前属性: Diffuse Shading 12 See also For more information on where to  nd a large portion of the built-in Cg functions for Unity, go to your Unity install directory and navigate to Unity4\Editor\Data\CGIncludes. Within that folder there are three  les that are of note at this point, the UnityCG.cginc, Lighting.cginc, and UnityShaderVariables.cginc. Our current Shader is making use of all these  les at the moment. We will go more in-depth with CgInclude  les in Chapter 9, Making Your Shader World Modular with CgIncludes. Adding properties to a Surface Shader Properties of a Shader are very important to the Shader pipeline, as they are the method 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 3 through 6. 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, is creating this GUI element for us. Again, Unity has made this process very ef cient in terms of coding and the amount of time it takes to iterate through changing your properties. How to do it… Let's see how this works in our current Shader called BasicDiffuse, by creating our own properties and learning more about the syntax involved: 1. In our Properties block of our Shader, remove the current property by deleting the following code from our current Shader. _MainTex ("Base (RGB)", 2D) = "white" {} 2. Now enter the following code, save the Shader, and re-enter the Unity editor. _EmissiveColor ("Emissive Color", Color) = (1,1,1,1) 3. When you return to Unity, the Shader will compile and you will see that our Material's Inspector tab now has a color swatch, named Emissive Color, instead of a texture swatch. Let's add one more and see what happens. Enter the following code: _AmbientColor ("Ambient Color", Range(0,10)) = 2 2. 输入下面的代码,并保存着色器,然后再次打开 Unity 编辑器。 Diffuse Shading 12 See also For more information on where to  nd a large portion of the built-in Cg functions for Unity, go to your Unity install directory and navigate to Unity4\Editor\Data\CGIncludes. Within that folder there are three  les that are of note at this point, the UnityCG.cginc, Lighting.cginc, and UnityShaderVariables.cginc. Our current Shader is making use of all these  les at the moment. We will go more in-depth with CgInclude  les in Chapter 9, Making Your Shader World Modular with CgIncludes. Adding properties to a Surface Shader Properties of a Shader are very important to the Shader pipeline, as they are the method 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 3 through 6. 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, is creating this GUI element for us. Again, Unity has made this process very ef cient in terms of coding and the amount of time it takes to iterate through changing your properties. How to do it… Let's see how this works in our current Shader called BasicDiffuse, by creating our own properties and learning more about the syntax involved: 1. In our Properties block of our Shader, remove the current property by deleting the following code from our current Shader. _MainTex ("Base (RGB)", 2D) = "white" {} 2. Now enter the following code, save the Shader, and re-enter the Unity editor. _EmissiveColor ("Emissive Color", Color) = (1,1,1,1) 3. When you return to Unity, the Shader will compile and you will see that our Material's Inspector tab now has a color swatch, named Emissive Color, instead of a texture swatch. Let's add one more and see what happens. Enter the following code: _AmbientColor ("Ambient Color", Range(0,10)) = 2 3. 当你返回 Unity 时,着色器将完成编译,并且你在材质 Inspector 面板上可以看到一 个名为 Emisslve Color 的颜色样本,它替换掉了之前的纹理样本。接下来我们再添加一个看 看会发生什么。输入如下代码: _AmbientColor ("Ambient Color", Color) = (1,1,1,1) 4. 我们已经在材质 Inspector 面板上添加了另一个颜色样本,现在让我们再次添加并且 看看其他类型的属性,在 Properties 块中输入如下代码: Chapter 1 13 4. 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 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. How it works… Every Unity Shader has a built-in structure it is looking for in its code. The properties block is one of those functions that is 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. Let's take a look at what is going on underneath the hood here. When you  rst 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 to get the value from the GUI element. This saves us a lot of time because we don't have to set up that system ourselves.


网友评论