Unity Shaders and Effects Cookbook (1-1) Basic Surface Shaders (Surface Shader)

1. Create a basic surface shader

Create a folder in Assets and name it Materials. Create a Shader in Materials. Create another Material. Both are named BasicDiffuse.

Open BasicDiffuse.shader. The basic diffuse shader code has been added by default, and this Shader accepts a texture information.

Rename the automatically created Shader as follows:

Shader "CookBookShaders/BasicDiffuse" {
Properties {
_MainTex (" Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"RenderType"="Opaque" }
LOD 200
< br /> CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;

struct Input {
float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o .Alpha = ca;
}
ENDCG
}
FallBack "Diffuse"
}

Select the Material (BasicDiffuse.mat) created above , In the Inspector, click the drop-down box and select the Shader (CookBookShaders/BasicDiffuse) created above, so that the shader is assigned to the Material.

Returned from http://blog.csdn.net/huutu http://www.thisisgame .com.cn

Then create a Cube and drag BasisDiffuse.mat to the Cube.

< /p>

This completes the setting of the shader development environment.

For surface shaders, many elements are completed in the background. Unity uses CG as the shading language. CG makes code writing more efficient.

Surface shader languages ​​are mostly based on components to write shaders.

Unity has a lot of built-in CG functions. You can see several CG files in the Editor/Data/CGIncludes directory.

The basic surface shader we just created uses the three CG files UnityCG.cginc, Lighting.cginc, and UnityShaderVariables.cginc.

Returned from http://blog.csdn.net/huutu http:/ /www.thisisgame.com.cn

2. Add attributes to the surface shader

The attributes of the shader allow us to Adjust the value of the shader in the editor or in the code.

2.1 、Properties Area Properties in the shader

Properties in Shader corresponds to the variable with the same name, this It is similar to the name string of the variable in Properties. Unity creates the corresponding editor control by reading the properties in Properties, and displays it on the Inspector window, allowing us to edit the value of this property easily.

The format of properties in Properties is as follows:

_EmissiveColor: variable name

“Emissive Color”: the name displayed on the Inspector panel in the editor

Color: Variable type

(1,1,1, 1) :default value

returned from http: //blog.csdn.net/huutu http://www.thisisgame.com.cn

Let’s add a shader attribute

< p>Modify the default Shader code and delete the default _MainTex properties

Modify the Properties block as follows:

Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) //Set the default value
}

Go back to Unity view, now you can see Emissive Color in the editor Settings

Add another Range property

Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) //Set the default value
_EmissivePowValue("EmissivePow Value",Range(0,10)) = 1
}

< br> Range is a range, a slider in the editor

Okay, the attributes have been added, let’s use these two attributes below.

2.2. Use attributes in the shader

First delete the original code. This code declares a variable _MainTex, which corresponds to The _MainTex attribute that we deleted earlier. We delete it and add our own variables

sampler2D _MainTex;

Then the code that uses _MainTex in the following surf function is also deleted

half4 c = tex2D (_MainTex, IN.uv_MainTex);

Corresponding to the two new attributes above, add our variables with the same name.

float4 _EmissiveColor
float _EmissivePowValue

然后在 surf 函数中使用这两个新增的变量

void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c=pow(_EmissiveColor,_EmissivePowValue);
o.Albedo = c.rgb ;
o.Alpha = ca;
}

Reproduced from http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Complete BasicDiffuse.shader

Shader "CookBookShaders/BasicDiffuse" {Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) / /Set the default value
_EmissivePowValue("EmissivePow Value",Range(0,10)) = 1
} SubShader {Tags {"RenderType"="Opaque"} LOD 200 CGPROGRAM #pragma surface surf Lambert float4 _EmissiveColor; float _EmissivePowValue; struct Input {float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c=pow(_EmissiveColor,_Em issivePowValue);
o.Albedo = c.rgb;
o.Alpha = ca;
} ENDCG} FallBack "Diffuse"}

Pow: power function, For example, Pow(2,3) = 8

After modifying the code, go to Unity to view it. Adjust the slider to see different effects.

< /p>

In Shader, the specified _EmissiveColor is exponentiated with _EmissivePowValue as the exponent.

The value specified for EmissiveColor in the editor is in the range of 0-255, and this value is normalized to 0-1 in Shader.

So as the value of EmissivePowValue gets larger and larger, c=pow(_EmissiveColor,_EmissivePowValue); The RGBA in this calculated value is getting smaller and smaller, approaching 0, so slowly, the square is It became black.

Sample project download:

http://pan.baidu.com/s/1skjNqNz

pre>

1. Create a basic surface shader

Create a folder in Assets and name it Materials. Create a Shader in Materials. Create another Material. Both are named BasicDiffuse.

Open BasicDiffuse.shader. The basic diffuse shader code has been added by default, and this Shader accepts a texture information.

Rename the automatically created Shader as follows:

Shader "CookBookShaders/BasicDiffuse" {
Properties {
_MainTex (" Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"RenderType"="Opaque" }
LOD 200
< br /> CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;

struct Input {
float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o .Alpha = ca;
}
ENDCG
}
FallBack "Diffuse"
}

Select the Material (BasicDiffuse.mat) created above , In the Inspector, click the drop-down box and select the Shader (CookBookShaders/BasicDiffuse) created above, so that the shader is assigned to the Material.

Returned from http://blog.csdn.net/huutu http://www.thisisgame .com.cn

Then create a Cube and drag BasisDiffuse.mat to the Cube.

< /p>

This completes the setting of the shader development environment.

For surface shaders, many elements are completed in the background. Unity uses CG as the shading language. CG makes code writing more efficient.

Surface shader languages ​​are mostly based on components to write shaders.

Unity has a lot of built-in CG functions. You can see several CG files in the Editor/Data/CGIncludes directory.

The basic surface shader we just created uses the three CG files UnityCG.cginc, Lighting.cginc, and UnityShaderVariables.cginc.

Returned from http://blog.csdn.net/huutu http:/ /www.thisisgame.com.cn

2. Add attributes to the surface shader

The attributes of the shader allow us to Adjust the value of the shader in the editor or in the code.

2.1 、Properties Area Properties in the shader

Properties in Shader corresponds to the variable with the same name, this It is similar to the name string of the variable in Properties. Unity creates the corresponding editor control by reading the properties in Properties, and displays it on the Inspector window, allowing us to edit the value of this property easily.

The format of properties in Properties is as follows:

_EmissiveColor: variable name

"Emissive Color": the name displayed on the Inspector panel in the editor

Color: Variable type

(1,1,1, 1) :default value

returned from http: //blog.csdn.net/huutu http://www.thisisgame.com.cn

Let’s add a shader attribute

< p>Modify the default Shader code and delete the default _MainTex properties

Modify the Properties block as follows:

Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) //Set the default value
}

Go back to Unity view, now you can see Emissive Color in the editor Settings

Add another Range property

Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) / /Set the default value
_EmissivePowValue("EmissivePow Value",Range(0,10)) = 1
}

Range is a range, which is a slider in the editor< p>

Okay, the properties have been added, let's use these two properties.

2.2. Use attributes in the shader

First delete the original code. This code declares a variable _MainTex, which corresponds to The _MainTex attribute that we deleted earlier. We delete it and add our own variables

sampler2D _MainTex;

Then the code that uses _MainTex in the following surf function is also deleted

half4 c = tex2D (_MainTex, IN.uv_MainTex);

Corresponding to the two new attributes above, add our variables with the same name.

float4 _EmissiveColor
float _EmissivePowValue

然后在 surf 函数中使用这两个新增的变量

void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c=pow(_EmissiveColor,_EmissivePowValue);
o.Albedo = c.rgb ;
o.Alpha = ca;
}

Reproduced from http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Complete BasicDiffuse.shader

Shader "CookBookShaders/BasicDiffuse" {Properties {
_EmissiveColor("Emissive Color",Color) = (1,1,1,1) / /Set the default value
_EmissivePowValue("EmissivePow Value",Range(0,10)) = 1
} SubShader {Tags {"RenderType"="Opaque"} LOD 200 CGPROGRAM #pragma surface surf Lambert float4 _EmissiveColor; float _EmissivePowValue; struct Input {float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c=pow(_EmissiveColor,_EmissivePowVal ue);
o.Albedo = c.rgb;
o.Alpha = ca;
} ENDCG} FallBack "Diffuse"}

Pow: exponentiation function, For example, Pow(2,3) = 8

After modifying the code, go to Unity to view it. Adjust the slider to see different effects.

< /p>

In Shader, the specified _EmissiveColor is exponentiated with _EmissivePowValue as the exponent.

The value specified for EmissiveColor in the editor is in the range of 0-255, and this value is normalized to 0-1 in Shader.

So as the value of EmissivePowValue gets larger and larger, c=pow(_EmissiveColor,_EmissivePowValue); The RGBA in this calculated value is getting smaller and smaller, approaching 0, so slowly, the square is It became black.

Sample project download:

http://pan.baidu.com/s/1skjNqNz

pre>

Leave a Comment

Your email address will not be published.