[Xiao Ming learns shader] 9. Vertex method (Vertex Function) and vertex color

Source: Internet
Author: User
Tags ming

During rendering, shader always takes precedence over each vertex (if we use the vertex method, then our vertex method is called), and the vertex is computed and output to the following process in a certain format, one of which is the Surfa method we placed, The input it receives can be controlled (we will output it with the vertex method).

The entire shader related process is as follows:
1, shader is ready to calculate a vertex. Because we customize the vertex method, so the vert is tuned?
2, vert the first parameter determines which attributes the vertex needs to bring in, we get the vertex color of this attribute, put the vertex color into the Input structure, for the subsequent surf use;
  

Shader Code:

Shader"custom/11.30/0"{properties{_maintex ("Base (RGB)", 2D) =" White"{}} subshader{tags{"Rendertype"="Opaque"} LOD $Cgprogram#pragmaSurface Surf Lambert Vertex:vetxsampler2d _maintex; structinput{FLOAT2 Uv_maintex;        FLOAT4 Vertcolor;                }; voidSurf (Input in,inout surfaceoutput o) {half4 c=tex2d (_maintex,in.uv_maintex); O.albedo=c.rgb*In.vertcolor; }                voidVetx (inout Appdata_full V, outInput o)                        {unity_initialize_output (input,o); O.vertcolor=V.color; } ENDCG}}

In the code above, we have used this line

#pragma surface surf Lambert Vertex:vert

After the illumination model method we add Vertex:vert, where vertex is the keyword, followed by a colon, followed by a name we define, corresponding to the following method. When this is used to tell unity to compile our shader, we need to do something about the vertex, so it compiles the vertex method that we specified so that the unity engine can use our vertex method when it executes the shader program.
And then look at the contents of the specified method.

void  out  = v.color;}

The type of the first parameter is Appdata_full, which is the structure of the data that is fed from the application to shader, such as position (vertex), normal (normal), plus the vertex color (color) We are introducing here.

struct  appdata_full{float4 vertex:position;  FLOAT4 tangent:tangent;  FLOAT3 Normal:normal;  FLOAT4 texcoord:texcoord0;  FLOAT4 Texcoord1:texcoord1; Fixed4 Color:color;  #if defined (shader_api_xbox360) Half4 texcoord2:texcoord2;  Half4 Texcoord3:texcoord3;  Half4 Texcoord4:texcoord4; Half4 Texcoord5:texcoord5; #endif };

Although with shader, but also to the model into the vertex color, we use a C # code implementation.

Program code:

usingUnityengine;usingSystem.Collections; Public classClass11_30:monobehaviour {//Use this for initialization    voidStart () {Mesh mesh= getcomponent<meshfilter>(). Mesh; Vector3[] vertices=mesh.vertices; color[] Colors=Newcolor[vertices.        Length]; colors [0] =color.red; Mesh.colors=colors; }}

:

Because there are too many vertices, and only the color of the first vertex is assigned, it appears that only the first vertex is interpolated from the other vertices.

[Xiao Ming learns shader] 9. Vertex method (Vertex Function) and vertex color

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.