If you need to repost an article, please note: Reprinted from Fengyu Chong unity3d tutorial Institute
Lecture on shader 14thSurfaceShader
It is complicated to use shader to implement illumination. There are different light types, different shadow options, and different render paths (forward and deferred ). Unity only encapsulates the illumination model, and the shader code is written by CG/HLSL.
Example 1: Simplest surface shader
- Shader "Custom/t_3_0 "{
- Properties {
- _ Maintex ("base (RGB)", 2d) = "white "{}
- }
- Subshader {
- 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 = C.;
- }
- Endcg
- }
- }
The surface processing function receives the input parameter from the vertex processing function (automatically vertex processing here) and outputs surfaceoutput O to the illumination model (Lambert here ). Here we have done three things: (1) We have obtained the color C corresponding to the UV through UV. (2) Assign C. RGB to O. Albedo (3) Assign C. A to O. AlphaThis is done. Then, if any object uses the shader, the shader can be correctly displayed no matter what light is added. It's really easy!
Input and Output of the surface processing function surf
(1) Input
- StructInput {
- Float2 uv_maintex;
- Float3 viewdir;
- Float4 anyname: color;
- Float4 screenpos;
- Float3 worldpos;
- Float3 worldrefl;
- };
Uv_maintex: uvviewdir: In-view screenpos: Crop Space Location worldpos: World Space Location
(2) Output
- Struct surfaceoutput {
- Half3 albedo;
- Half3 normal;
- Half3 emission;
- Half specular;
- Half gloss;
- Half Alpha;
- };
Albedo: diffuse color normal: normal emission: Self-emitting color specular: mirror reflection coefficient gloss: Alpha: Transparency value for more parameters, see the documentation
Surface shader ParametersSurface shader can implement additional control by adding parameters, such as adding vertex processing functions and Alpha mixing. Note: If you use the Lambert, blinnphong: Alpha, Alpha, and other features of unity, you must use the surface shader parameter. Previously, Code such as alphatest greater 0.3 cannot be added. If it is added, the light becomes invalid and the object becomes dark. If the illumination model used is a custom illumination model, you only need to give O. if Alpha is assigned a normal value (such as a value of texture color), the previous Code such as alphatest greater 0.3 can still be used as usual. For specific code, refer to the Code in the next lecture.
(1) Alpha MixingDirectly add the Alpha parameter.
# Pragma surface surf blinnphong alpha
(2) alphatest
Add a variable named _ cutoff.Add at the end of the surfaceAlphatest: _ cutoff.
The color is output only when the Alpha value is greater than _ cutoff.
- Shader "Custom/t_3_0 "{
- Properties {
- _ Maintex ("base (RGB)", 2d) = "white "{}
- _ Cutoff ("Alpha cutoff", range (0, 1) =
0.5
- }
- Subshader {
- Cgprogram
- # Pragma surface surf Lambert alphatest: _ cutoff
- 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 = C.;
- }
- Endcg
- }
- }
(3) vertex functions
Example: bloated Model
Implementation Method: extend the vertex to the normal direction for a distance
- Shader "example/normal extrusion "{
- Properties {
- _ Maintex ("texture", 2d) = "white "{}
- _ Amount ("extrusion amount", range (-1, 1) = 0.5
- }
- Subshader {
- Tags {"rendertype" = "Opaque "}
- Cgprogram
- # Pragma surface surf Lambert vertex: vert
- Struct input {
- Float2 uv_maintex;
- };
- Float _ amount;
- Void Vert (inout appdata_full v ){
- V. vertex. xyz + = V. Normal * _ amount;
- }
- Sampler2d _ maintex;
- Void SURF (input in, inout surfaceoutput O ){
- O. albedo = tex2d (_ maintex, In. uv_maintex). RGB;
- }
- Endcg
- }
- Fallback "diffuse"
- }
(4) finalcolor
Finalcolor: colorfunction
Make the final change to the color output
- Shader
"Example/tint final color "{
- Properties
{
- _ Maintex
("Texture ",
2d) =
"White"
{}
- _ Colortint
("Tint ",
Color) = (1.0,
0.6,
0.6,
1.0)
- }
- Subshader
{
- Cgprogram
- # Pragma
Surface surf Lambert
Finalcolor: mycolor
- Struct
Input {
- Float2
Uv_maintex;
- };
- Fixed4
_ Colortint;
-
Void
Mycolor (input in, surfaceoutput o, inout fixed4 color)
- {
- // Modify the color of the final output here
- Color
* = _ Colortint;
- }
- Sampler2d
_ Maintex;
- Void
Surf (input in, inout surfaceoutput O ){
- O. Albedo
= Tex2d (_ maintex, In. uv_maintex). RGB;
- }
- Endcg
- }
- }
For more parameters, see