Vertex/Fragment Shader in Unity Shader

The basic structure of a vertex/fragment shader

insert image description here

Where does the model data come from

Struct a2v
{
    
    
	float4 vertex:POSITION;
	float4 NORMAL:NORMAL;
	float4 texcoord:TEXCOORD0;
}
  • The semantics supported by Unity are: POSITION, TANGENT, NORMAL, TEXCOORD0, TEXCOORD1, TEXCOOD2, TEXCOORD3, COLOR, etc.
  • Contains the data of the vertex shader
  • a stands for application, v stands for vertex shader, and a2v means to pass data from the application stage to the vertex shader.
  • A model usually includes a set of triangles, each triangle is composed of vertices, and each vertex contains some data, such as vertex position, location, tangent, texture coordinates, vertex color, etc.

How to communicate between vertex shader and fragment shader

struct v2f
{
    
    
	float4 pos:SV_POSITION;
	float4 color:COLOR0;
}

  • v2f is used to pass information between the vertex shader and the fragment shader
  • Similarly, the semantics of each variable also need to be specified in v2f
  • In this example we used SV_POSITION and COLOR0 semantics

How to use attributes

Properties
{
    
    
	_Color("Color Tint", Color) = (1, 1, 1, 1)
}

Batch relationship between 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
unitform fixed4_Color;

The unitform keyword is a modifier for Cg to modify variables and parameters. It is only used to provide some modifiers about the variables and parameters. It is only used to provide some information about how the initial value of the variable is specified and stored. Related information, in the Unity Shader, the unitform keyword can be omitted

Guess you like

Origin blog.csdn.net/weixin_50617270/article/details/123737121