[Unity Shaders] Use CgInclude to modularize your Shader-use the # define command to create a Shader,

Source: Internet
Author: User

[Unity Shaders] Use CgInclude to modularize your Shader-use the # define command to create a Shader,

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


We have seen how to use the built-in CgInclude file of Unity, and how to create our own CgInclude to save illumination models, variables, and helper functions. In this article, we will focus on how to use the CgInclude file more dynamically and effectively, making our Shader more modular and switch different States as needed.


Therefore, in this article, we will modify the Half Lambert illumination model created in the previous section and add a definition for it ). In this way, if Half Lambert is defined in our Shader, the illumination model will use Half Lambert diffuse reflection; otherwise, the standard NdotL illumination model will be used.


This code is just a reference.



Implementation


First, let's take a look at our CgInclude file. We want to make the illumination model in two states:

Now we can see that Unity does not use Half Lambert, but uses the standard NdotL illumination model. At this time, because HalfLambert is not defined, Unity will skip that code during compilation. This makes our Shader more flexible and effective, and we can no longer write or delete a large number of repeated code.


Explanation

As you can see, although this code has only three lines, we can use these simple technologies to make the illumination model more variable. By using the # ifdef command, we tell Unity to find the definition of this name. Here this name refers to HalfLambert.
Using CgInclude to write Shader not only helps us save a lot of code, but also stores a lot of illumination models. This makes it easier for us to call the illumination model or modify it using multiple States.
The skills mentioned here are actually similar to the related knowledge in C ++, and these are the knowledge of CG. The more you learn, the more important it is to learn the original Shader though Unity encapsulates a lot of details.






Robocraft: \ Steam \ SteamApps \ common \ Robocraft \ assetbundles \ allshadersunity3d why

Allshaders. unity3d
Delete this file and update the game again.


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.