Unity Shader Learning notes: Simple shader

Source: Internet
Author: User
Shader "Own/chapter5-simpleshader" {properties{//declares a property of a color type _color ("Color Tint", color) = (1.0,1. 0,1.0,1.0)} subshader{pass{cgprogram #pragma vertex vert #p
                Ragma fragment Frag//In CG code, we need to define a variable fixed4 _color that matches both the property name and the type;
                    Use a struct to define the input struct a2v{//position semantics of the vertex shader tell unity to populate the vertex variable with the vertex coordinates of the model space
                    FLOAT4 vertex:position;
                    Normal semantics tells Unity to fill the normal variable float3 normal:normal with the tangent direction of the model space.
                TEXCOORD0 semantics tells Unity to fill the Texcoord variable float4 texcoord:texcoord0 with the first set of texture coordinates of the model;
                };
                    Use a struct to define the output of a vertex shader struct v2f{//sv_postion semantics tells Unity,pos to include the position of vertices in the clipping space
                    FLOAT4 pos:sv_position; COLOR0 semantics can be used to store color information fixed3 colors:COLOR0;
                };
                    v2f Vert (a2v v) {//Declaration output structure v2f o;
                    O.pos = Mul (Unity_matrix_mvp,v.vertex); The v.normal contains the normal direction of the vertex, whose component range is mapped to [0.0,1.0]//stored in the O.color in [ -1.0,1.0]///and passed to the slice in the
                    Shader O.color = v.normal * 0.5 + fixed3 (0.5,0.5,0.5);
                return o;
                    } fixed4 Frag (v2f i): sv_target{fixed3 c = i.color;
                    Use the _color property to control the output color C *= _color.rgb;
                Return Fixed4 (c,1.0); } ENDCG}}}

1. Surround CG Code Snippets with Cgprogram and ENDCG
2. #pragma vertex vert
#pragma fragment Frag
Declare the function corresponding to the vertex shader, declaring the corresponding function of the slice shader
The semantics of 3.Unity support are: Position,tangent,normal,texcoord0,texcoord1,texcoord2,texcoord3,color, etc.
Matching relationship of 4.ShaderLab attribute type and CG variable type

Shaderlab Property Type       CG variable type
color,vector            float4,half4,fixed4
range,float             float,half,fixed
2D                      sampler2d
Cube                    samplercube
3D                      Sampler3d

5.unity built-in include file download address: Unity Shader
Common include files in unity

File name                             description
unitycg.cginc contains the                       most commonly used help functions, macros, and structs, etc.
unityshadervariables.cginc          when compiling unity shader, will be automatically included in it. Contains a number of built-in global variables, such as UNITY_MATRIX_MVP, such as
Lighting.cginc                      contains a variety of built-in lighting models, if you are writing surface shader, will be automatically included in
Hlslsupport.cginc is                   automatically included when compiling unity shader. Declares a lot of macros and definitions for cross-platform compilation

Some common structures in the Unitycg.cginc

                The name Description                      contains the variable
appdata_base that      can be used for the input vertex position of the vertex shader      , the vertex normals, and the first set of texture coordinates
Appdata_tan       available for the input of the vertex shader      vertex position, vertex tangent, vertex normal, first set of texture coordinates
appdata_full      can be used for vertex shader input      vertex positions, vertex tangents, vertex normals, four sets (or more) of texture coordinates
APPDATA_IMG The       input vertex position that can be used for the vertex shader      , the first set of texture coordinates
v2f_img           can be used for the      position in the output clipping space of the vertex shader, texture coordinates

Some common helper functions in Unitycg.cginc

The function name                                              describes
float3 Worldspaceviewdir (float4 v)                   entering the vertex position in a model space, returning the viewing direction from that point to the camera in world space
FLOAT3 Objspaceviewdir (Float4 v)                     enters the vertex position in a model space, returning the viewing direction from that point to the camera in model space
FLOAT3 Worldspacelightdir (float4 v)                  Can only be used in forward rendering. Enter the vertex position in a model space and return the light direction from that point to the light source in world space. Not
                                                     normalized
float3 objspacelightdir (float4 v)                    can only be used in forward rendering. Enter the position of the vertex in a model space and return the light direction from that point to the light source in model space. Not
                                                     normalized
float3 unityobjecttoworldnormal (float3 norm)         converts the normal direction from model space to world space
FLOAT3 Unityobjecttoworlddir (float3 dir)             transforms the directional vector from model space into World space
float3 Unityworldtoobjectdir (float3 dir)             Change direction vectors from World space to model space

Common semantics supported by unity when passing model data to a vertex shader from the application phase

                                      The semantic description
is the                                vertex position in the POSITION model space, usually the FLOAT4 type
normal                                  vertex normal, usually the FLOAT3 type
TANGENT                                 vertex tangent. Typically a FLOAT4 type
Texcoordn, such as Texcoord0,texcoord1        the vertex's texture coordinates, TEXCOORD0 represents the first set of texture coordinates, and so on. Typically a float2 or FLOAT4 type
color                                   vertex color, usually fixed4 or FLOAT4 type

The number of n in Texcoordn is related to the shader model, for example, in shader Model2 (the shader model version that unity is compiled by default) and shader Model3, n equals 8, while in shader In Model4 and shader Model5, n equals 16.

Common semantics used by unity when passing data from a vertex shader to a slice shader

The semantic                                description
sv_position The                        vertex coordinates in the clipping space, and the struct must contain a variable that is decorated with that semantics. is equivalent to POSITION in DirectX9, but it is best
to use sv_position COLOR0                             typically used to output the first set of vertex colors, but not required
COLOR1 is                             typically used to output the second set of vertex colors. But not required
Texcoord0~texcoord7 are                typically used to output texture coordinates, but are not required

Common semantics supported by unity when the chip shader output

The semantic            description
sv_target     output value will be stored in the render target. is equivalent to the color semantics in DirectX9, but it is best to use Sv_target

numeric types of 3 precision in CG/HLSL

Type               Precision
float            value with the highest precision. Typically, 32-bit is used to store
half             medium-precision floating-point values. Typically 16 bits are used for storage, with a precision range of -60000 ~ +60000
fixed            minimum precision floating point value. Typically 11 bits to store, with a precision range of-2.0 ~ +2.0

Unity-supported Shader Target

Instruction                           Description
#pragma target 2.0           //default shader target level. Equivalent to shader Model2.0 on Direct3D 9, does not support sampling of vertex textures does not support explicit LOD texture sampling
#pragma target 3.0           //Direct3D Model on Shader 9 3.0, supports sampling of vertex textures
#pragma target 4.0           //equivalent to shader Model 4.0 on Direct3D 10, supports geometry shader
#pragma target 5.0           Equivalent to shader Model 5.0 on Direct3D 11

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.