[Unity Shaders] Vertex Magic -- access Vertex color, unityshaders

Source: Internet
Author: User

[Unity Shaders] Vertex Magic -- access Vertex color, unityshaders

This series mainly references the book "Unity Shaders and Effects Cookbook" (thanks to the author of the original book) and adds a bit of personal understanding or expansion.

Here are all the illustrations in this book. Here is the code and resources required for this book (you can also download them from the official website ).

========================================================== ===================================================== =====



Preface


La la ~ I came back to read books. The article begins to talk about other things. Because I write blogs and are more active in the group (why QQ gave me the title of "spam "... I am so cool ...), Recently, some friends have sent me emails or private messages. Many people have encouraged me. I am very happy and some people have given me good learning suggestions, these experiences make me feel that it is the right way to stick to blogging ~ Thank you for your encouragement, support, and attention! I will stick to it.


Okay, let's get down to the truth ~ At the beginning of this article, I decided to start a new chapter, Vertex Magic. As the name suggests, it is the content related to learning vertices. Since reading a figure in Unity Gems below, I have a better understanding of the entire Surface Shader workflow. I will repeat it here.




We can see that there are four stages that we can participate in. Most of our previous studies were in the second and third stages, namely writing the surf and LightingXXX functions to influence the surface coloring and illumination functions. In the cartoon style Shader, we also made a preliminary attempt on the finalcolor command, the last chance to change the pixel color. Now, we will use a chapter to learn how to use the first stage-vertex function to influence the pixel color.


Vertex functions are called once before each vertex is transferred to the GPU. It is used to obtain three-dimensional coordinates from the model coordinate system, and then convert them to the two-dimensional position in the screen coordinate system when rendering them to the screen. Therefore, through the vertex function, we can modify the vertex position, color, and UV coordinates. Once the vertex is modified, the surf function is executed. Unlike vertex functions, the surf function is executed pixel by pixel.


Through the vertex function, we can create dynamic effects like waves at sea, banners floating, or use Shader to color the vertex. In this article, we will learn how to create the simplest vertex function in a Surface Shader!



Preparations


Before learning about vertex functions, you must first understand how to obtain and store vertex-related information through vertex functions.


Your scenario should look like this:





Implementation


Next, let's start writing Shader.


The complete code is as follows:

Shader "Custom/SimpleVertexColor" {Properties {_MainTint("Global Color Tint", Color) = (1,1,1,1)}SubShader {Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM#pragma surface surf Lambert vertex:vertfloat4 _MainTint;struct Input {float2 uv_MainTex;float4 vertColor;};void vert(inout appdata_full v, out Input o){o.vertColor = v.color;}void surf (Input IN, inout SurfaceOutput o) {o.Albedo = IN.vertColor.rgb * _MainTint.rgb;}ENDCG} FallBack "Diffuse"}

The effect is as follows:




Explanation

Through the vertex function, we can modify the vertex location, color, UV coordinate equivalent. In this section, we use a model that has colored vertices imported from Maya, but we can find that these colors are not displayed in Unity when the default material is used. We need to write a Shader to extract these colors and then display them on the model.
We first add the vertex: vert statement in the # pragma declaration. This actually tells Unity, hey, don't use your own built-in vertex function to access the model vertex information. Go to the Shader I wrote and find a guy named vert to use it to process the information! If Unity cannot be found, a compilation error is reported.
In the vert function, apart from the Input struct we are familiar with, there is also a very special parameter-appdata_full. This parameter is also a built-in variable of Unity. It contains all information about the model vertex, including position, tangent, normal, two texture coordinates, and color information. Others include appdata_base and appdata_tan. For details, see the official website.
You can also find that vertColor is a float4 variable, which means we can also access its transparent channel. As shown below:
void surf (Input IN, inout SurfaceOutput o) {o.Albedo = IN.vertColor.rgb * _MainTint.rgb;o.Alpha = IN.vertColor.a;}






Related Article

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.