Unity Surface Shader Sample Analysis

Source: Internet
Author: User

Unity Surface Shader Sample Analysis
for a surface shader in unity (surface Shader), the overall structure of its code is as follows:
Shader "name" {Properties {//First part         }
Subshader {//Part II         }
Fallback "Diffuse"//Part III     }
Part One Properties data block       its role is to act as an interface to the data, bringing in external data (resources) for use within the shader. Here, we can define the data types as shown below:
(1) _maintex ("Base (RGB)", 2D) = "White" {}2D type, mainly refers to the texture map we use. _maintex is a custom variable name that we will use when we write the shader code, which will be displayed in the Properties panel of the shader.Base (RGB) is also a name that is displayed on the material properties panel that binds to the shader.
(2) _cubetex ("3D Map", Cube) = "White" {}cube Type, which represents a three-dimensional texture. Three-dimensional textures have a width, height, and depth of three dimensions of information.
(3) _color ("Main Color", color) = (1,1,1,1)Color type, which represents a color that contains the components of (R, G, B, A) 4 color values. _color is a custom variable name that is used when writing shader code.
(4) _float ("Float Value", float) = 0.0a float type that represents a floating-point value. _float is a custom variable name that is used when writing shader code.
(5) _rimpower ("Rim Power", Range (0.5, 1.0)) = 1.0float type, values are taken from the range [min, Max]. _rimpower is a custom variable name that is used when writing shader code.
(6) _vector ("Vector4", Vector) = (1, 1, 1, 1)The vector type, which represents the 4 elements, contains 4 values (X, Y, Z, W). _vector is a custom variable name that is used when writing shader code.
Part II Subshader child Shader code block
This part is the code body of the shader, the main position where we write the code, which is highlighted in the following example analysis.
Part III Fallback "diffuse" rollback block
Rollback As an alternative, when our graphics card does not find the appropriate subshader will be called, with a relatively simple coloring effect.
Next, let's start with an example to analyze the shader code. Our sample program is the normal mapping in Unity Surface Examples, which implements the base shader for the normal map (link address http://docs.unity3d.com/Manual/ sl-surfaceshaderexamples.html), the complete code is as follows:          Shader "Example/diffuse Bump" {
Properties {
_maintex ("Texture", 2D) = "White" {}
_bumpmap ("BumpMap", 2D) = "Bump" {}
          } 
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD

cgprogram#pragma Surface Surf Lambert
sampler2d _maintex;
sampler2d _bumpmap;
struct Input {
float2 Uv_maintex;
float2 Uv_bumpmap;
               };
void Surf (Input in, InOut surfaceoutput o) {
O.albedo = tex2d (_maintex, In.uv_maintex). RGB;
o.normal = Unpacknormal (tex2d (_bumpmap, In.uv_bumpmap));
               } 
ENDCG
          }
Fallback "Diffuse"
      }
First, we define two variables of type 2D in the properties block: _maintex and _bumpmap. _maintex as texture map data, _bumpmap as normal map data. into our subshader, the first thing to see is a tags (tags). Tags are used to decorate the surface shader, and our hardware determines when the child shader should be called by judging these tags. Some of the commonly used tags are as follows:
"Rendertype" = "Opaque"//is called when drawing an opaque drawing"Rendertype" = "Transparent"//is called when drawing a transparent drawing"Ignoreprojector" = "True"//not affected by the projection"Queue" = "XXX"//Specify render queue
LODLOD is the abbreviation of level of Detail, and the size of this value determines whether our subshader can be called. When the maximum LOD set by the system is less than the LOD specified by the Subshader, the Subshader will not be available.
in the next section, CGPROGRAM-ENDCG says this is a CG program. #pragmasurface surf Lambert is a compilation instruction, surface shows that this is a surface shader, surf is the method name that the shader calls, and Lambert is the lighting model used, This is also a lighting model that comes with unity. It is important to note that to access variables defined in the properties in a CG program, the variables must be declared again using the same name in the CG program. Only then, the type of the variable is used in the CG program corresponding to the type. Input is a struct that needs our customization, and its object is used as an input (in) parameter for us to use in the surf method. In the above example, we define Uv_maintex and Uv_bumpmap, which represent the UV coordinates of the texture map and the normal map, respectively. The last part of the surf function is the method body of the shader. Here we have finished coloring and normal processing. The final effect is as follows:
     

Unity Surface Shader Sample Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.