"Unity Shaders" Vertex magic--access vertex color

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/candycat1992/article/details/38147767

This series focuses on Unity Shaders and Effects Cookbook (thanks to the author of the book), plus a bit of personal understanding or outreach.

Here are all the illustrations of this book. Here is the code and resources required for this book (you can also download it from the website, of course).

========================================== Split Line ==========================================

write in front

Cheer up ~ I came back to read again. The article begins by saying some digression. Because blogging and in the group is more active (why QQ to my name is "spit Groove" ...) I'm such a good girl ... Recently, some friends sent me e-mail or private messages, a lot of people gave me encouragement, I am very happy, also someone gave me a good study advice, these experiences let me feel that insist on blogging is the right way ~ Also thank all the encouragement, support and attention to my people! I'll keep it going.

Well, to the beginning, I decided to start a new chapter--vertex Magic. As the name implies, it is learning vertex-related content. Since I saw a picture of unity gems, I have a better understanding of the entire surface shader workflow, and I'll revisit it here.

We can see that there are four stages we can participate in. Much of what we learned before was in the second and third stages of writing surf and lightingxxx functions to influence surface shading and illumination functions. In the cartoon-style shader, we also have a tentative attempt at the last chance to change the color of the pixel--finalcolor command. Now, we'll use a chapter to learn how to use the first phase-vertex function to influence pixel color.

Vertex functions are called once before each vertex is routed to the GPU. Its function is to obtain three-dimensional coordinates from the model coordinate system, and then convert it to a two-dimensional position in the screen coordinate system when it is rendered to the screen. So, with vertex functions, we can modify the position, color, and UV coordinates of the vertices. Once we have finished modifying the vertex, we go to the execution of the surf function. Unlike vertex functions, which are performed on a per-vertex basis, the surf function is performed on a per-pixel basis.

With vertex functions, we can create dynamic effects like waves on the sea, waving banners, or using shader to color vertices. In this article, let's learn how to create one of the simplest vertex functions in a surface shader!

preparatory work

Before we dive into the vertex function, we need to first understand how to get and store vertex-related information through vertex functions.

    1. First, we want to prepare a model that has been shaded for vertices so that we can see the vertex color in the vertex function. For convenience, we use the model resource--VERTEXCOLOROBJECT.FBX in the seventh chapter of this book, which comes with resources (see the beginning of the article). We'll import the VERTEXCOLOROBJECT.FBX into unity and drag it into a new scene. Finally, add a parallel light.
    2. Create a new shader and material that can be named Simplevertexcolor, assign shader to material, and assign material to the model.

Your scene should look something like this:

Implement

Below, we start writing shader.

  1. Add a new properties to the properties block: [Plain] view plaincopyprint?
    1. Properties
    2. {
    3. _maintint ("Global color Tint", color) = (1,1,1,1)
    4. }
    Properties {_maintint ("Global color Tint", color) = (1,1,1,1)}
  2. Next, tell unity that we will use our own vertex functions: [Plain] view plaincopyprint?
    1. Cgprogram
    2. #pragma surface surf Lambert vertex:vert
    Cgprogram#pragma Surface Surf Lambert Vertex:vert
  3. properties: [plain] view plaincopyprint?
    1. FLOAT4 _MAINTINT;  
    float4 _maintint; 
  4. vertcolor so that the surf function can access the data passed in the Vert function: [plain] view plaincopyprint?
    1. STRUCT INPUT   
    2. {  
    3.      float2 uv_maintex;  
    4.     float4 vertColor;   
    5. };  
    struct Input {float2 Uv_maintex ; Float4 Vertcolor;}; 
  5. Below is a very simple vert function. We access the vertex color of the model and then store it in the input structure: [plain] view plaincopyprint?
    1. void vert (Inout appdata_full v, out input o)   
    2. {  
    3.     o.vertcolor = v.color;   
    4. }  
    void Vert (inout appdata_full V, out Input o) {O. Vertcolor = V.color;} 
  6. Finally, we populate the albedo parameter of the surfaceoutput struct with the data obtained from input: [Plain] view plaincopyprint?
    1. void Surf (Input in, InOut surfaceoutput o)
    2. {
    3. O.albedo = IN.vertColor.rgb * _MAINTINT.RGB;
    4. }
    void Surf (Input in, InOut surfaceoutput o) {O.albedo = IN.vertColor.rgb * _MAINTINT.RGB;}
The complete code is as follows: [Plain]View Plaincopyprint?
  1. Shader "Custom/simplevertexcolor" {
  2. Properties
  3. {
  4. _maintint ("Global color Tint", color) = (1,1,1,1)
  5. }
  6. Subshader
  7. {
  8. Tags {"Rendertype" = "Opaque"}
  9. LOD 200
  10. Cgprogram
  11. #pragma surface surf Lambert vertex:vert
  12. FLOAT4 _maintint;
  13. struct Input
  14. {
  15. FLOAT2 Uv_maintex;
  16. FLOAT4 Vertcolor;
  17. };
  18. void Vert (InOut appdata_full V, out Input O)
  19. {
  20. O.vertcolor = V.color;
  21. }
  22. void Surf (Input in, InOut surfaceoutput o)
  23. {
  24. O.albedo = IN.vertColor.rgb * _MAINTINT.RGB;
  25. }
  26. Endcg
  27. }
  28. FallBack "Diffuse"
  29. }
Shader "Custom/simplevertexcolor" {Properties {_maintint ("Global Color Tint", Color) = (1,1,1,1)}subshader {Tags {"Rende Rtype "=" 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: explainwith vertex functions, we can modify the position, color, and UV coordinates of vertices. In this section we have used a model that has been shaded from Maya for a given vertex, but we can see that these colors are not displayed in unity with the default material. We need to write shader, extract these colors and then show them on the model. we begin by adding 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 my shader to find a guy named Vert, use it to process information! If unity is not found, it will report a compilation error. vert function, in addition to our familiar input structure, there is a very special parameter--appdata_full . This parameter is also a variable built into unity that contains all the information about the vertex of the model, including position, tangent, normals, two texture coordinates, and color information. Other appdata_base and Appdata_tan, can see the official website. you can also see that vertcolor is a variable of type FLOAT4, which means that we can also access its transparent channel. Like this: [Plain]View Plaincopyprint?
    1. void Surf (Input in, InOut surfaceoutput o)
    2. {
    3. O.albedo = IN.vertColor.rgb * _MAINTINT.RGB;
    4. O.alpha = IN.VERTCOLOR.A;
    5. }

(GO) "Unity Shaders" Vertex magic--access 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.