- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 653字
- 2021-06-18 19:04:15
How to do it...
The following steps show you how to use the properties in a Surface Shader:
- 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;
- 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
}
- Next, add the following lines of code to the shader, below the CGPROGRAM line:
float4 _AmbientColor; float _MySliderValue;
- 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
- 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:

- Learn Type:Driven Development
- 算法精粹:經典計算機科學問題的Java實現
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- Spring Boot進階:原理、實戰與面試題分析
- Visual Basic程序設計實踐教程
- Python機器學習算法: 原理、實現與案例
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- SQL Server 2008 R2數據庫技術及應用(第3版)
- 區塊鏈項目開發指南
- 軟件工程基礎與實訓教程
- Java并發編程之美
- 程序員的成長課
- 一步一步學Spring Boot:微服務項目實戰(第2版)
- 劍指大數據:企業級電商數據倉庫項目實戰(精華版)
- Server Side development with Node.js and Koa.js Quick Start Guide