"Unity Shaders" uses Cginclude to make your shader modular-create shader using the # define directive

Source: Internet
Author: User

This series is the main reference to Unity Shaders and Effects Cookbook (thanks to the original book author), at the same time will add a little personal understanding or expansion.

Here are all the illustrations of this book.

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

========================================== Cutting Line ==========================================



write in front


We've seen how to use Unity's built-in cginclude files. And how to create our own cginclude to preserve lighting models, variables, and auxiliary functions. In this article, we focus on how to use cginclude files more dynamically and effectively, making our shader more modular and able to switch between different states depending on the need.


Therefore, in this article, we will change the half Lambert lighting model created in the previous section and add a definition to it.

Thus, assuming that the half Lambert is defined in our shader, the light model will use half Lambert diffuse. Otherwise, a standard NDOTL illumination model will be used.


This section of code is 3 lines, just a primer.



Implement


Let's take a look at our Cginclude file first.

We want the lighting model to have two states:

  1. First state. is a normal ndotl (NORMAL*LIGHTDIR) diffuse light model. And the second state, is a half Lambert illumination model. Change our Cginclude file to include the following two lines of code:

    watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2fuzhljyxqxotky/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

  2. Then, in our shader, update the instructions in the cgprogram block:


  3. Save your shader and return to unity compilation. At this point you will not see any changes whatsoever. This is because we tell unity to define a directive called Halflambert, assuming it finds this definition in whatever file is included. It will use the code in the first step.

  4. Returns shader. And stare at the new instructions we just added.

    Return to unity after saving view:

    watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2fuzhljyxqxotky/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Then you'll find out. Unity does not use the half Lambert, but instead uses the standard NDOTL lighting model.

At this point, because we do not define Halflambert, unity will skip that code at compile time. This makes our shader more flexible and effective, and we can no longer write or delete large amounts of repetitive code.




Explain

As you can see, although this code only has 3 lines. But we can use these simple techniques to make the lighting model more changeable. By using the #ifdef directive, we tell unity to find the definition of this name, which refers to Halflambert.
Use Cginclude to write shader. Not only can we save a lot of code, but we can also store a lot of light models.

This makes it easier to invoke the lighting model, or use multiple states to change it.


Here are the tips. In fact, the knowledge in C + + is very similar. And these are the knowledge content of CG. The more you learn, the more you feel. While unity encapsulates a lot of detail, the more important it is to learn the original shader.





"Unity Shaders" uses Cginclude to make your shader modular-create shader using the # define directive

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.