Unity Shader Tutorial-fourth lesson: Customizing the lighting model (equation)

Source: Internet
Author: User
Tags diff

This article starting address: HTTP://98JY.NET/ARTICLE/22

For more articles, please enter the portal

----------------------------------------------


In our previous lesson, one line of code is as follows


#pragma surface surf Lambert
This line of code instructs Unity to use the "Lambert" illumination model when compiling our shader code. It's formatted as

#pragma surface surf Lightmodel


One of the Lightmodel is the name of the lighting model that unity comes with or we write it ourselves according to Unity's requirements. So what is a light model? A light model is a mathematical abstraction that reflects how a material of an object reacts to light, for example, you can imagine a photon hitting a part of an object's surface from a distance as an incident beam and then being reflected, refracted, or absorbed. If we give the incident light to a certain value, given the material to a certain value, the final emission or refraction of the change after the light-related values (direction, direction of energy) and the original relationship is our light model, some places also called this light equation.


The illumination model or the illumination equation is finally expressed by the relevant mathematical formula, and then the engine is used for the code to become present. In unity, we can see the code of this equation by opening the data\cgincludes Lighting.cginc in the Unity installation directory, for convenience, I copy it as follows:


Inline Fixed4 Lightinglambert (surfaceoutput s, fixed3 lightdir, fixed atten) {fixed diff = max (0, Dot (s.normal, Lightdir )); Fixed4 C;c.rgb = S.albedo * _lightcolor0.rgb * (diff * Atten * 2); c.a = S.alpha;return c;}

Method name, more than we see in the #pragma "Lambert", more out of the "Lighting" the few words. This is Unity's rule: The lighting model does not have to carry the word "Lighting" in the #pragma code, and unity adds this keyword to the search method when it is compiled. Therefore, our above #pragma surface surf Lambert This sentence, the corresponding method is Lightinglambert ().


The code for the lighting model is run by unity after the surface code runs, and the surface code outputs the "surface" value of a series of objects that unity defines (determining the material information of the object), and then the light model calculates the output of the light and the result of the material's interaction as the last color we see.


So can we write a light equation ourselves and let unity use our custom equations? The answer is yes. In the following chapters we begin to try to write a method of our own lighting model.


Steps:


    • Use our original shader code to do the basics
    • Change #pragma as in code Lambert to Basicdiffuse
    • Add the following code to the subshader{} block


Inline Float4 Lightingbasicdiffuse (surfaceoutput s, fixed3lightdir, fixed atten) {float diflight = max (0, Dot (s.normal, l Ightdir) Float4 Col;col.rgb = S.albedo * _lightcolor0.rgb * (diflight * atten * 2); col.a = S.alpha;return col;}
    • Save the shader and return to unity.


Unity will start compiling our shader, and if there is no error, then the new shader will be applied to the corresponding object. There is no difference from the performance of the object in the scene (since Basicdiffuse's code logic is exactly the same as Lambert). But behind this, unity does use our lighting model "basicdiffuse" instead of using the "Lambert" it provides.


Unity uses #pragma this precompiled directive (indeed, this is its official name) to let us choose the lighting model. There are 3 types of care model prototypes we can choose from:


    • Half4 Lightingname (surfaceoutput s, Half3 lightdir, half atten) {}
    • Half4 Lightingname (surfaceoutput s, half3 Lightdir, Half3 viewdir, half atten) {}
    • Half4 Lightingname_presspass (surfaceoutput s, Half4 light) {}

The first one is used in ordinary rendering without the observer's direction. The second is used when the normal rendering requires the direction of the observer. The third is when unity is configured to delay rendering.


The above method uses a dot () method, which is also a library method of CG language. Used to calculate an angle-related value between two directions. In the Lambert illumination model, the angle between the direction of the incident light of each point and its own normal position determines the intensity of the value of the reflected light. The smaller the angle of the incident light and the normal direction, the greater the value in the result.

Unity Shader Tutorial-fourth lesson: Customizing the lighting model (equation)

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.