Unity Shader Getting Started (ii) Semantics, structure, vertex-wise illumination

Source: Internet
Author: User

A vertex-by-pixel diffuse light shader

Shader "Study/chapter6/vertexshader" {
properties{
_diffuse ("Diffuse", Color) = (1, 1, 1, 1)//diffuse property of material
Declares a color-type property} subshader{pass{tags{"Lightmode" = "forwardbase"}cgprogram#pragma vertex vert#pragma fragment Frag
Unity's built-in files, in order to be able to use some of the built-in variables of unity, such as--lightcolor0#include "Lighting.cginc"
A variable that matches a property, a match for a property and a variable, the following is a fixed4 _diffuse;//vertex shader input struct with struct A2V{FLOAT4 vertex:position;//storage model vertex FLOAT3 Normal : NORMAL; The storage model vertex normal};//vertex shader output structure is also the element shader input struct struct V2F{FLOAT4 pos:sv_position;fixed3 color:color;// Pass the light color computed by the vertex shader to the slice shader};//vertex shader v2f vert (a2v v) {v2f o;//convert vertices from model space to clipping space o.pos=unityobjecttoclippos (V.VERTEX);//Get External items , Unity_lightmodel_ambient gets the ambient light part fixed3 ambient=unity_lightmodel_ambient.xyz;//The normal from the model space to the world space through the UNITY built-in function. fixed3 Worldnormal=normalize (Mul (V.normal, (float3x3) unity_worldtoobject);//light source direction, _ WORLDSPACELIGHTPOS0 assumes that there is only one parallel light source in the scene to get the correct direction fixed3 worldlight=normalize (_WORLDSPACELIGHTPOS0.XYZ);//_ LightColor0 for unity built-in variables access color and strength information for the pass-processed light source
The diffuse reflection Calculation formula Fixed3 diffuse=_lightcolor0.rgb*_diffuse.rgb*saturate (dot (worldnormal,worldlight)) is given below; o.color=ambient +diffuse;return o;}
Chip shader Fixed4 Frag (v2f i): Sv_target{return fixed4 (i.color,1.0);} ENDCG}}//Set this unity shader callback to the built-in diffuse FallBack "diffuse"}

The bottom is the semantics supported by unity

Now let's take a look at the vertex input structure
struct A2V{FLOAT4 vertex:position;//storage model vertex FLOAT3 normal:normal;//Storage model vertex normals};
The structure called A2V contains the model data required by the vertex shader, a2v represents application to vertex shader, and passes the data from the application phase to the vertex shader
The format of a struct is as follows
struct structname{
Type (data type) name (variable name): Semantic (semantics);
Type (data type) name (variable name): Semantic (semantics);
Type (data type) name (variable name): Semantic (semantics);
......
};
We usually output some data from the vertex shader, such as the normal of the model, coordinate texture, etc. to the element shader, we need to use the V2F structure in the above code to realize the communication between the two.
Vertex shaders are called on a per-vertex basis, and the slice shader is called on a slice-by-element basis. The slice shader is actually the result of the difference in the output of the vertex shader.
What is done in the vertex shader is to convert the vertex shader input struct (a2v) into a v2f (that is, a function), and then drop the v2f into the slice shader and
One operation, then output a color

The following is the relationship between attributes and variables

For example, you declare a 2D attribute in the properties, in CG you declare a sample2d variable to match the 2D attribute, and the declared property name must be the same as the variable name.
For example, the above are _diffuse. Value range on the Fixed

The light we see = ambient light/spontaneous light + diffuse reflectance + specular reflection
Diffuse reflection Calculation formula
Diffuse = (diffuse color of the light source color * material) max (0, Surface normal * unit vector of the light source)

To prevent negative results for normal and light direction point multiplication, you need to use the Go Max function
Intercept it to 0 to prevent the object from being illuminated from the back light source.

The resulting material ball effect is as follows

Unity Shader Getting Started (ii) Semantics, structure, vertex-wise illumination

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.