The Vertex&frag type of shader uses shader methods for vertex calculations and pixel calculations.
For example: first through the vertex shader, calculate the model's coordinates and textures, and then pass the pixel shader to calculate the color value.
* Note
_maintex_st and Transform_tex are correspondence, calculating the offset and spread of texture coordinates.
The range of clipping space is [ -1,1], that is, after the MVP matrix, the range of O.POS.X/O.POS.W and O.POS.Y/O.POS.W is [ -1,1] so the clipping space coordinates can be converted to relative screen position of the UV coordinates, as follows O.UV = FLOAT2 ((o.pos.x/o.pos.w+1) *0.5, (o.pos.y/o.pos.w+1) *0.5);
Source:
1Shader"Jqm/vertexandfrag" //file name and path2 {3Properties//Properties4 {5_maintex ("Texture", 2D) =" White" {}6 }7 Subshader8 {9 Ten Pass One { ACgprogram//Vertex&frag type of shader with CG language - #pragmaVertex vert - #pragmaFragment Frag the -#include"Unitycg.cginc" - -Sampler2d _maintex;//Property Declaration + float4 _maintex_st; - + structAppData A { at float4 vertex:position; - float2 uv:texcoord0; - }; - - structv2f - { in float4 pos:sv_position; - float2 uv:texcoord0; to }; + - thev2f Vert (AppData v)//Vertex program * { $ v2f o;Panax NotoginsengO.pos = Mul (UNITY_MATRIX_MVP, V.vertex);//model View projection matrix, which transfers the model coordinates to the camera coordinates, in the projection to the screen coordinates -O.UV = Transform_tex (V.uv, _maintex);//calculate the values between Uvs and maps the returno; + } A theFixed4 Frag (v2f i): COLOR//fragment program, Force return color value of color type + { - //Sample The texture $Fixed4 col = tex2d (_maintex, I.UV);//Texture Sampling $ returnCol; - } - ENDCG the } - }Wuyi}
View Code
Vertex&frag