在表面着色器中使用属性-iso_17356

时间:2021-06-08 17:54:10
【文件属性】:
文件名称:在表面着色器中使用属性-iso_17356
文件大小:6.09MB
文件格式:PDF
更新时间:2021-06-08 17:54:10
Unity 1.4 在表面着色器中使用属性 现在我们已经创建了一些属性,我们可以将它们与着色器关联起来,以便使用属性来 调整着色器并让材质处理获得更多的交互性。 我们可以在材质 Inspector 面板上调整属性,因为已经为属性赋予了一个变量名,如果 你想在着色器代码里通过变量名来获得它的属性值,则必须创建与之对应的另一个变量。 1.4.1 如何操作 接下来将演示如何在表面着色器中使用属性: 1. 首先,要删除以下代码,因为在 1.2 节删除了 MainTex 属性。 Chapter 1 15 How to do it… The following steps show you how to use the properties in a Surface Shader: 1. To begin, let's remove the following lines of code, as we deleted the property called MainTex in the Creating a basic Surface Shader recipe of this chapter: sampler2D _MainTex; half4 c = tex2D (_MainTex, IN.uv_MainTex); 2. Next, add the following lines of code to the Shader, below the CGPROGRAM line: float4 _EmissiveColor; float4 _AmbientColor; float _MySliderValue; 3. With step 2 complete, we can now use the values from the properties in our Shader. Let's do this by adding the value from the _EmissiveColor property to the _AmbientColor property, and giving the result of that to the o.Albedo line of code. So, let's add the following code to the Shader inside the surf function: void surf (Input IN, inout SurfaceOutput o) { float4 c; c = pow((_EmissiveColor + _AmbientColor), _MySliderValue); o.Albedo = c.rgb; o.Alpha = c.a; } 4. Finally, your Shader should look like the following Shader code. If you save your Shader in MonoDevelop and re-enter Unity, your Shader will compile. If there were no errors, you will now have the ability to change the ambient and emissive colors of the Material, as well as increase the saturation of the  nal color by using the slider value. Pretty neat, huh! Shader "CookbookShaders/BasicDiffuse" { //We define Properties in the properties block Properties { _EmissiveColor ("Emissive Color", Color) = (1,1,1,1) _AmbientColor ("Ambient Color", Color) = (1,1,1,1) _MySliderValue ("This is a Slider", Range(0,10)) = 2.5 } SubShader 2. 然后,在 CGPROGRAM 行下面添加以下几行代码: Chapter 1 15 How to do it… The following steps show you how to use the properties in a Surface Shader: 1. To begin, let's remove the following lines of code, as we deleted the property called MainTex in the Creating a basic Surface Shader recipe of this chapter: sampler2D _MainTex; half4 c = tex2D (_MainTex, IN.uv_MainTex); 2. Next, add the following lines of code to the Shader, below the CGPROGRAM line: float4 _EmissiveColor; float4 _AmbientColor; float _MySliderValue; 3. With step 2 complete, we can now use the values from the properties in our Shader. Let's do this by adding the value from the _EmissiveColor property to the _AmbientColor property, and giving the result of that to the o.Albedo line of code. So, let's add the following code to the Shader inside the surf function: void surf (Input IN, inout SurfaceOutput o) { float4 c; c = pow((_EmissiveColor + _AmbientColor), _MySliderValue); o.Albedo = c.rgb; o.Alpha = c.a; } 4. Finally, your Shader should look like the following Shader code. If you save your Shader in MonoDevelop and re-enter Unity, your Shader will compile. If there were no errors, you will now have the ability to change the ambient and emissive colors of the Material, as well as increase the saturation of the  nal color by using the slider value. Pretty neat, huh! Shader "CookbookShaders/BasicDiffuse" { //We define Properties in the properties block Properties { _EmissiveColor ("Emissive Color", Color) = (1,1,1,1) _AmbientColor ("Ambient Color", Color) = (1,1,1,1) _MySliderValue ("This is a Slider", Range(0,10)) = 2.5 } SubShader 3. 当你完成步骤 2 之后,你就可以在着色器里使用属性值了。让我们将 _EmissiveColor 的属性值加到 AmbientColor 属性值上,然后把计算结果赋值给 o.Albedo。将以下代码加入 到 surf 函数。 Chapter 1 15 How to do it… The following steps show you how to use the properties in a Surface Shader: 1. To begin, let's remove the following lines of code, as we deleted the property called MainTex in the Creating a basic Surface Shader recipe of this chapter: sampler2D _MainTex; half4 c = tex2D (_MainTex, IN.uv_MainTex); 2. Next, add the following lines of code to the Shader, below the CGPROGRAM line: float4 _EmissiveColor; float4 _AmbientColor; float _MySliderValue; 3. With step 2 complete, we can now use the values from the properties in our Shader. Let's do this by adding the value from the _EmissiveColor property to the _AmbientColor property, and giving the result of that to the o.Albedo line of code. So, let's add the following code to the Shader inside the surf function: void surf (Input IN, inout SurfaceOutput o) { float4 c; c = pow((_EmissiveColor + _AmbientColor), _MySliderValue); o.Albedo = c.rgb; o.Alpha = c.a; } 4. Finally, your Shader should look like the following Shader code. If you save your Shader in MonoDevelop and re-enter Unity, your Shader will compile. If there were no errors, you will now have the ability to change the ambient and emissive colors of the Material, as well as increase the saturation of the  nal color by using the slider value. Pretty neat, huh! Shader "CookbookShaders/BasicDiffuse" { //We define Properties in the properties block Properties { _EmissiveColor ("Emissive Color", Color) = (1,1,1,1) _AmbientColor ("Ambient Color", Color) = (1,1,1,1) _MySliderValue ("This is a Slider", Range(0,10)) = 2.5 } SubShader 4. 最后,着色器的代码应该和下面一样,在 MonoDevelop 中保存着色器代码并返回

网友评论