Unity3d Shaderlab Custom Lighting model

Source: Internet
Author: User

Then the previous article Basicmydiffuse, this time to illustrate is a custom lighting model.

1.>//#pragma surface surf Lambert:: This is the default lighting model

2.> #pragma surface surf cusdiffuse//self-defined lighting model cusdiffuse;

3.> adds a light model function to the Subshader:

Inline Float4 lightingcusdiffuse (surfaceoutput s,fixed3 lightdir,fixed atten) {

float diflight = max (0,dot (S.normal,lightdir));

Float4 Col;

Use (diflight*atten*2) to try the difference;;

Col.rgb = S.albedo * _lightcolor0.rgb* (diflight*atten*1.5);

Col.a=s.alpha;

return col;

}

The second step is to tell the shader to use the Cusdiffuse illumination model to calculate. The second article uses the default Lambert lighting model.

The third step is to write your own lighting model functions. The function name format lighting+ the illumination model name, namely our lightingcusdiffuse;

To accomplish the diffuse effect, we multiply the data provided to us by the surfaceoutput struct, S.albedo from the surf function, _lightcolor0.rgb from unity, and the RGB values of col are obtained by multiplication.

By switching shader in unity, we can see the difference between our own lighting model and the previous basicmydiffuse use of the Lambert illumination model. If the parameter is adjusted to 2, then the effect difference is difficult to see.

Read:1 the dot multiplier function dot (arg1,arg2) is a mathematical function built into the CG language that compares the direction of two vectors in space, and the DOT function checks whether two vectors are parallel or perpendicular.

Any 2 vectors can get the angle range of the -1~1 through the dot function, 1 means that the parallel vector is in the direction of the viewing angle, 1 is parallel but toward your direction, and 0 is the vector of your vertical direction.

The Read:2 max function is a function of the CG standard library, and we can use the result of his limiting point multiplication, max (ARG1,ARG2), and the Max function to ensure that the diffuse computation demerit is always between 0 and the point multiplier maximum.

If the value is less than 0, the shader may generate an extremely black area and affect subsequent computed values.

The following is the entire content of this modification.

Code Start---------------------------------------------------------------

Shader "91ygame/basicmycusdiffuse" {
Properties {
_emissivecolor ("emissive color", color) = (1,1,1,1)
_ambientcolor ("Ambient color", color) = (1,1,1,1)
_myslidervalue ("Slider Value", Range (0,10)) =1.3
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
#pragma surface surf Lambert
#pragma surface surf Cusdiffuse

FLOAT4 _emissivecolor;
FLOAT4 _ambientcolor;
float _myslidervalue;

struct Input {
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 lightingcusdiffuse (surfaceoutput s,fixed3 lightdir,fixed atten) {
float diflight = max (0,dot (S.normal,lightdir));
Float4 Col;
Col.rgb = S.albedo * _lightcolor0.rgb* (diflight*atten*1.5);
Col.a=s.alpha;
return col;
}
Endcg
}
FallBack "Diffuse"
}
Code End---------------------------------------------------------------

Unity3d Shaderlab Custom Lighting model

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.