Unity3d Tutorial Shader: The 16th lecture on custom lighting models

Source: Internet
Author: User

reprinted from the Wind Yu Chong Unity3d Tutorial College      14 We've achieved basic surface Shader, and we've talked about the basics of lighting models. This is a lecture on how to write a light model. The custom lighting model is divided into 4 main steps:(0) erecting the frame, fill in the required parameters(1) Calculation of diffuse reflectance intensity(2) Calculate specular reflectionStrength (3) combined with diffuse reflection light and specular reflection light code with Chinese annotation, with the above-said lighting formula, step by step implementation can be.
Author: Wind Yu Chong Shader "Custom/t_customlightmodel" {Properties {_maintex ("Texture", 2D) = "white" {}} Subshader {//alpha test, with O in surf.        Alpha = C.A; Alphatest Greater 0.4 Cgprogram #pragma surface surf Lsylightmodel//naming rules: Lighting Connect The name of the suface after #pragma//lightdir: point to the unit vector of the light source Viewdir: Point to camera unit vector atten: Attenuation factor float4 Lightinglsylightmodel (Su             Rfaceoutput s, float3 lightdir,half3 viewdir, half atten) {FLOAT4 C;             (1) Diffuse reflectance strength float Diffusef = max (0,dot (S.normal,lightdir));            (2) Specular reflection strength float specf;            FLOAT3 H = normalize (Lightdir+viewdir);            float specbase = max (0,dot (s.normal,h));             Shininess specular strength factor, set here to 8 specf = POW (specbase,8);             (3) Combining diffuse reflection light with specular reflection light C.rgb = S.albedo * _lightcolor0 * Diffusef *atten + _lightcolor0*specf;            C.A = S.alpha;        return C;       }      struct Input {float2 Uv_maintex;             };             void Vert (InOut appdata_full v) {//This can be done in special position processing} sampler2d _maintex;            void Surf (Input in,inout surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex);            O.albedo = C.rgb;        O.alpha = C.A; } ENDCG}}

  

Unity3d Tutorial Shader: The 16th lecture on custom lighting models

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.