Learning notes--simple implementation of basic illumination model

Source: Internet
Author: User
Tags mul

Lambert illumination model, the illumination intensity is determined according to the angle of illumination vector and vertex discovery:

Shader"JAMES/VP Shader/lightmodel-lambert"{Properties {_maintex ("Maintex", 2D) =" White"{}} subshader {Pass {Tags {"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"                        float_lightcolor0;            Sampler2d _maintex;            FLOAT4 _maintex_st; structv2f {float4 pos:sv_position;                FLOAT2 uv:texcoord0;                FLOAT3 Lightdir:texcoord1;            FLOAT3 Normal:texcoord2;                        };                v2f Vert (Appdata_full v) {v2f o; O.pos=Mul (UNITY_MATRIX_MVP, V.vertex); O.uv=Transform_tex (V.texcoord, _maintex); //local vectors from vertex position to lightO.lightdir =Objspacelightdir (V.vertex); O.normal=V.normal; returno; } float4 Frag (v2f i): COLOR {i.lightdir=normalize (I.lightdir); I.normal=normalize (i.normal); //Texture SamplingFLOAT4 C =tex2d (_maintex, I.UV); //cos values for light intensity, normal direction, and illumination orientation                floatDiffuse = max (0, Dot (i.normal, i.lightdir)); //Texture Color * source color * Strength parameterc = c * _LIGHTCOLOR0 *Diffuse; returnC2; } ENDCG}} FallBack"Diffuse"}

The Phong illumination model calculates the intensity of specular highlights based on the angle of the reflected vector of the light vector and the line of sight, plus the composition of the diffuse light:

Shader"JAMES/VP Shader/lightmodel-phong"{Properties {_maintex ("Maintex", 2D) =" White"{} _gloss ("Gloss", Float) =1} subshader {Pass {Tags {"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"                        float_lightcolor0;            Sampler2d _maintex;            FLOAT4 _maintex_st; float_gloss; structv2f {float4 pos:sv_position;                FLOAT2 uv:texcoord0;                FLOAT3 Normal:texcoord1;            FLOAT3 Lightdir:texcoord2; //Illumination DirectionFLOAT3 Reflectlightdir:texcoord3;//Light Reflection DirectionFLOAT3 Viewdir:texcoord4;//Sight Direction            };                v2f Vert (Appdata_full v) {v2f o; O.pos=Mul (UNITY_MATRIX_MVP, V.vertex); O.uv=Transform_tex (V.texcoord, _maintex); O.normal=V.normal; //local vectors from vertex position to lightO.lightdir =Objspacelightdir (V.vertex); //reflection vectors, note the need to use-o.lightdirO.reflectlightdir = Reflect (-O.lightdir, V.normal); //Line of sight, vertex to camera local variableO.viewdir =Objspaceviewdir (V.vertex); returno; } float4 Frag (v2f i): COLOR {i.normal=normalize (i.normal); I.reflectlightdir=normalize (I.reflectlightdir); I.viewdir=normalize (I.viewdir); //Texture SamplingFLOAT4 C =tex2d (_maintex, I.UV); //Diffuse light intensity                floatDiffuse = max (0, Dot (i.normal, i.lightdir)); //Specular light intensity                floatSpecular = max (0, Dot (I.reflectlightdir, i.viewdir)); Specular= Pow (Specular, +) *_gloss; //Texture Color * source color * Strength parameterc = c * _lightcolor0 * (diffuse +specular); returnC2; } ENDCG}} FallBack"Diffuse"}

The Binnphong illumination model calculates the intensity of specular highlights based on the light vector and the half-width vector of the line of sight, along with the angle of the vertex normals, plus the composition of the diffuse light:

Shader"JAMES/VP Shader/lightmodel-binnphong"{Properties {_maintex ("Maintex", 2D) =" White"{} _gloss ("Gloss", Float) =5} subshader {Pass {Tags {"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"                        float_lightcolor0;            Sampler2d _maintex;            FLOAT4 _maintex_st; float_gloss; structv2f {float4 pos:sv_position;                FLOAT2 uv:texcoord0;                        FLOAT3 Normal:texcoord1;        FLOAT3 Lightdir:texcoord2; //Illumination Direction            };                v2f Vert (Appdata_full v) {v2f o; O.pos=Mul (UNITY_MATRIX_MVP, V.vertex); O.uv=Transform_tex (V.texcoord, _maintex); O.normal=V.normal; //local vectors of vertex->lightO.lightdir =Objspacelightdir (V.vertex); //local vectors of vertex->cameraFLOAT3 Viewdir =Objspaceviewdir (V.vertex); returno; } float4 Frag (v2f i): COLOR {i.lightdir=normalize (I.lightdir); I.normal=normalize (i.normal); //half-width vector, light direction and middle value of line of sightFLOAT3 Halfdir = (i.lightdir + i.normal) *0.5; //Texture SamplingFLOAT4 C =tex2d (_maintex, I.UV); //Diffuse light intensity                floatDiffuse = max (0, Dot (i.normal, i.lightdir)); //Specular light intensity                floatSpecular = max (0, Dot (i.normal, halfdir)); Specular= Pow (Specular, +) *_gloss; //Texture Color * source color * Strength parameterc = c * _lightcolor0 * (diffuse +specular); returnC2; } ENDCG}} FallBack"Diffuse"}

Binnphong less than Phong to calculate a single reflection vector, it will be more concise and efficient.

Learning notes--simple implementation of basic illumination 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.