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

How to do it...

The following steps show you how to use the properties in a Surface Shader:

  1. Continuing from the previous example, let's create another shader with the name ParameterExample. Just like before, remove the _MainTex property in the same manner as was done in the Adding properties to a shader recipe of this chapter:
// Inside the Properties block
_MainTex ("Albedo (RGB)", 2D) = "white" {}

// Below the CGPROGRAM line sampler2D _MainTex;

// Inside of the surf function fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  1. Afterward, update the Properties section to the following code:
Properties {
_Color ("Color", Color) = (1,1,1,1)
_AmbientColor ("Ambient Color", Color) = (1,1,1,1)
_MySliderValue ("This is a Slider", Range(0,10)) = 2.5

_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
  1. Next, add the following lines of code to the shader, below the CGPROGRAM line:
float4 _AmbientColor; 
float _MySliderValue; 
  1. With step 3 complete, we can now use the values from the properties in our shader. Let's do this by adding the value from the _Color property to the _AmbientColor property and giving the result of this to the o.Albedo line of code. So, let's add the following code to the shader in the surf() function:
void surf (Input IN, inout SurfaceOutputStandard o) {
// We can then use the properties values in our shader
fixed4 c = pow((_Color + _AmbientColor), _MySliderValue);

// Albedo comes from property values given from slider and colors
o.Albedo = c.rgb;

// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
  1. Finally, your shader should look like the following shader code. If you save your shader 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 final color using the slider value. Pretty neat:
Shader "CookbookShaders/Chapter02/ParameterExample" {
// We define Properties in the properties block
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200

// We need to declare the properties variable type inside of the
// CGPROGRAM so we can access its value from the properties block.

CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

float4 _AmbientColor;
float _MySliderValue;

struct Input {
float2 uv_MainTex;
};

half _Glossiness;
half _Metallic;
fixed4 _Color;

// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)

void surf (Input IN, inout SurfaceOutputStandard o) {
// We can then use the properties values in our shader
fixed4 c = pow((_Color + _AmbientColor), _MySliderValue);

// Albedo comes from property values given from slider and colors
o.Albedo = c.rgb;

// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}

The pow(arg1, arg2) function is a built-in function that will perform the equivalent math function of power. So, argument 1 is the value that we want to raise to a power and argument 2 is the power that we want to raise it to.
To find out more about the pow() function, look at the Cg tutorial. It is a great free resource that you can use to learn more about shading and there is a glossary of all the functions available to you in the Cg shading language at http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html.

The following screenshot demonstrates the result obtained using our properties to control our material's colors and saturation from within the material's Inspector tab:

主站蜘蛛池模板: 元朗区| 盐津县| 宁津县| 北辰区| 莲花县| 永康市| 陕西省| 宁陵县| 汨罗市| 鄂温| 穆棱市| 平江县| 遂川县| 徐闻县| 共和县| 上思县| 渑池县| 乌拉特前旗| 洛浦县| 礼泉县| 宜阳县| 瑞昌市| 沛县| 浪卡子县| 郁南县| 长春市| 邹平县| 保山市| 手游| 甘孜县| 吐鲁番市| 茶陵县| 门头沟区| 鹤峰县| 余江县| 沾化县| 郴州市| 崇明县| 澄城县| 武汉市| 南丹县|