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

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:

主站蜘蛛池模板: 临朐县| 通渭县| 鄂州市| 微山县| 吉首市| 漳浦县| 海南省| 泉州市| 海安县| 和林格尔县| 定陶县| 澎湖县| 通山县| 泰州市| 双柏县| 岱山县| 修文县| 嵊州市| 邛崃市| 台东市| 孝义市| 苍南县| 镇原县| 英超| 新干县| 琼结县| 隆林| 呼和浩特市| 博野县| 太和县| 商丘市| 荣昌县| 开江县| 澜沧| 凉城县| 武安市| 务川| 灵宝市| 阳江市| 曲阜市| 兰考县|