"Unity Shaders" Learning note--surfaceshader (iii) Basicdiffuse and Halflambert

Source: Internet
Author: User
Tags pow

"Unity Shaders" Learning note--surfaceshader (iii) Basicdiffuse and Halflambert

Reprint Please specify source: http://www.cnblogs.com/-867259206/p/5598185.html

This series of articles was written using Unity5.3.
Before writing the code:

  1. Of course, if unity wasn't installed, you wouldn't learn unity shaders, would it?

  2. Before reading this series of articles you need to have some programming concepts.

  3. In VS, Unity shaders does not have syntax highlighting and smart hints, and the VS party can refer to this article to make the code highlighted, shaderlabvs or NShader a plugin such as download or the like to highlight the code.

  4. This is the basic knowledge of the unity shaders for small white, and if you have a foundation or you are a great God, then these articles are not for you.

  5. Due to the limitation of the author's level, there may be some fallacy in the text, and I implore to point out.

Remember what you said earlier about how to create surface shader? Create a new shader, named Basicdiffuse, according to the previous method.

    1. The content in the properties will be modified first:

Properties {_emissivecolor ("Emissive Color",Color) = (1,1,1,1) _ambientcolor ("Ambient Color",Color) = (1,1,1,1) _myslidervalue ("This is a Slider", Range (0,Ten)) =2.5}
    1. Will #pragma surface surf Standard fullforwardshadows read:

#pragma surface surf BasicDiffuse

This shows that we are going to use the Basicdiffuse illumination model, which is one of our custom lighting models.

    1. Define variables declared in the properties:

float4 _EmissiveColor;float4 _AmbientColor;float _MySliderValue;
    1. P style= "margin-top:0; margin-right:0; Margin-bottom:1.1em; margin-left:0; line-height:1.6; " > Modify your surf function:

void Surf (input  in , inout  surfaceoutput  o) {float4 c ; c  = Pow ((_emissivecolor+_ambientcolor), _myslidervalue    );    O.albedo  = c . RGB; O.alpha  = c . A;}  

Note the type of the output structure and make the modification.

    1. Add the following function, which is our custom lighting model:

inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten){    float difLight = max(0, dot (s.Normal, lightDir));    float4 col;    col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);    col.a = s.Alpha;    return col;}
Explain
  1. In the surf function, we will add the self-luminous color and diffuse color and as the base, and then the value of _myslidervalue as a power, the power operation to get four yuan floating point vector c, the output of the reflection value is equal to the RGB color of C, the output of the transparent channel is equal to the transparent channel C. This is in fact the self-luminous color and diffuse color of a certain mix.

  2. The naming rules for custom lighting models are "Lighting" + "Custom light names". This defines a lighting model.
    There are 5 prototypes of the Illumination model:

    Half4 Lightingname(surfaceoutput S, fixed3 lightdir, half atten);Half4 Lightingname(surfaceoutput S, fixed3 Lightdir, Fixed3 viewdir, half atten);Half4 Lightingname_prepass(surfaceoutput S, Half4 Light);Half4 Lightingname_dirlightmap(surfaceoutput S, fixed4 color, fixed4 scale, bool surffuncwritesnormal);Half4 Lightingname_dirlightmap(surfaceoutput S, fixed4 color, fixed4 scale, fixed3 viewdir, bool surffuncwritesnormal,out half3 Speccolor) ;

    Here we are using the first prototype. The first parameter is the output structure, the second parameter is the light direction, that is, the light source to the object surface of the point of the vector, is the unit vector, the third parameter attenuation, attenuation abbreviation, because the light is reflected by the object, there will be energy loss, so there will be attenuation. We see that this parameter is not assigned, so it is a unity built-in parameter, and I don't know how it works (if the great God knows please tell me), we know it means decay.
    The first sentence in the function calculates the light intensity of the diffuse reflection. Dot is the dot multiplication. By multiplying the normal and light angles of the surface, the larger the angle between the light and the normal vector, the smaller the light's component in the normal vector direction, the weaker the reflected light. Because the angle is more than 90 degrees, it is negative, so use the Max function to make the result greater than 0. Max (x, y) is the maximum value in X, Y.
    The third sentence means the color value = reflection value x The color of the parallel light x diffuse light intensity x attenuation degree x2.
    _lightcolor0 is also a variable built into unity that has different meanings in different render path and pass, where the color of the parallel light is.
    The back of this 2 should be an empirical value, can be modified according to their desired effect.
    This is how the basic diffuse reflection is calculated.
    Let's take a look at its effect:
    Create a new sphere and assign the basicdiffuse material to it. Adjust the slider's value to 0. No surprises, you see the effect is this:

    Changing the color of the self-illumination, and then adjusting the slider, we find that the larger the slider value, the more obvious the self-luminous color is:

    Now turn the self-illumination back to white, change the diffuse color, and adjust the slider:
    No accident, see the effect and spontaneous light, slider=0, when the shadow is gray, spontaneous light and diffuse color does not work, the greater the value, the more spontaneous light and diffuse reflection of the color more obvious.
    Next, while changing the colors of both spontaneous and diffuse, such as red and green, we see the ball become yellow, which is the effect of red and green blending:

    You can also try to adjust the other colors yourself and see what the effect will be.
    And the value in the function 2, you can also change to try Yo ~

Halflambert

Next, learn a classic light model--halflambert (half Lambert).
Very simply, we first renamed the lighting model to Halflambert, which is to modify the illumination function and #pragma.
First, modify one sentence:

float difLight = dot (s.Normal, lightDir);

Then add a sentence:

float0.50.5;

To change one more sentence:

 col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);

The part of the original diflight interval [ -1,0] becomes [0,0.5], if the result is greater than 0 directly with the max function, the portion less than 0 will be equal to 0, that is, the back surface will be black. If you use the half-Lambert formula, the part that is less than 0 will fade from 0 to 0.5, and the grayscale of the color will change. At the same time, the brightness of the bright part is also improved.
Halflambert is a technology developed by valve, a technique used to illuminate objects in low-light areas. It is used to prevent the back surface of an object from losing shape and appearing too flat. This technique is not of any physical principle, it is a perceptual visual enhancement.

Attached: Code Listings

Shader"Custom/basicdiffuse"{Properties {_emissivecolor ("Emissive Color", Color) = (1,1,1,1) _ambientcolor ("Ambient Color", Color) = (1,1,1,1) _myslidervalue ("This is a Slider", Range (0,Ten)) =2.5} subshader {Tags {"Rendertype"="Opaque"} LOD $Cgprogram//Physically based standard lighting model, and enable Shadows on all light types        #pragma surface surf basicdiffuse        //Use Shader Model 3.0 target, to get nicer looking lighting        #pragma target 3.0FLOAT4 _emissivecolor; FLOAT4 _ambientcolor;float_myslidervalue;structinput{FLOAT2 Uv_maintex; }; void surf (Input in, InOut surfaceoutput o) {FLOAT4 C; c =POW((_emissivecolor+_ambientcolor), _myslidervalue);            O.albedo = C.rgb;        O.alpha = C.A; } inline float4 lightingbasicdiffuse (surfaceoutput s, fixed3 lightdir, fixed atten) {floatDiflight = Max (0, Dot (S.normal, lightdir));        Float4 Col; Col.rgb = S.albedo * _lightcolor0.rgb * (diflight * atten *2); COL.A = S.alpha;returnCol } ENDCG} FallBack"Diffuse"}

Halflambert Illumination model function:

inline float4 Lightinghalflambert (surfaceoutput s, fixed3 lightdir, fixed atten) {float Diflight = dot (s.    Normal , Lightdir);    float Hlambert = 0.5 * diflight + 0.5;    Float4 Col; Col.rgb  = S .    Albedo  * _lightcolor0.rgb  * (Hlambert * atten * 2); Col.a  = S .    Alpha ; return col;}   

Unity Shaders Learning Note--surfaceshader (iii) Basicdiffuse and Halflambert

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.