Shader基于片段编程的Lambert光照,Phong光照和Blinn-Phong光照

Lambert光照模型(环境光+漫反射):

Shader "Custom/ShaderLambert_Fragment" {
    SubShader {
        Pass{

        CGPROGRAM

        #include "unitycg.cginc"
        #include "lighting.cginc"
        #pragma vertex vert
        #pragma fragment frag

        struct params{
            float4 Pos:POSITION;
            float3 Nor:TEXCOORD0;
            float4 Ver:COLOR;
        };

        params vert(appdata_base ab)
        {
            params pms;
            pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
            pms.Nor = ab.normal;
            pms.Ver = ab.vertex;
            return pms;
        }

        float4 frag(params INPUT):COLOR
        {
            float4 Col = UNITY_LIGHTMODEL_AMBIENT;

            float3 N = normalize(UnityObjectToWorldNormal(INPUT.Nor)).xyz;
            float3 L = normalize(WorldSpaceLightDir(INPUT.Ver)).xyz;
            float DotNL = saturate(dot(N,L));

            Col = Col + DotNL * _LightColor0;

            return Col;
        }

        ENDCG
        }

    }
}

Phong光照模型(环境光+漫反射+镜面高光):

Shader "Custom/ShaderPhong_Fragment" {
    Properties{
        _SpecularColor("SpecularColor",color) = (1,1,1,1)
    }

    SubShader {
            Pass{
                CGPROGRAM

                #include "unitycg.cginc"
                #include "lighting.cginc"
                #pragma vertex vert 
                #pragma fragment frag

                float4 _SpecularColor;

                struct params{
                    float4 Pos:POSITION;
                    float3 Nor:TEXCOORD0;
                    float4 Ver:COLOR;
                };

                params vert(appdata_base ab)
                {
                    params pms;

                    pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
                    pms.Nor = ab.normal;                        //由于片段程序中无法获取到 顶点的法向量和光照,所以传入到片段程序中
                    pms.Ver = ab.vertex;

                    return pms;
                }

                float4 frag(params INPUT):COLOR
                {
                    float4 Col = UNITY_LIGHTMODEL_AMBIENT;  //环境光

                    float3 N = normalize(UnityObjectToWorldNormal(INPUT.Nor)).xyz;
                    float3 L = normalize(WorldSpaceLightDir(INPUT.Ver)).xyz;
                    float DotNL = saturate(dot(N,L));

                    Col = Col + _LightColor0 * DotNL;   //环境光 + 漫反射

                    float3 R = normalize(reflect(-L,N));  //求反射向量
                    float3 V = WorldSpaceViewDir(INPUT.Ver);

                    float DotRv = saturate(dot(R,V));
                    Col = Col*0.8 + _SpecularColor*DotRv*0.1;

                    return Col;
                }

                ENDCG
            }
    }
}

Blinn-Phong镜面模型(环境光+漫反射+半角镜面高光):

Shader "Custom/ShaderBlinnPhong_Fragment" {
    Properties{
        _SpecularColor("SpecularColor",color) = (1,1,1,1)
    }
    SubShader{
        Pass{
            CGPROGRAM

            #include "unitycg.cginc"
            #include "lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag

            float4 _SpecularColor;
            struct params{
                float4 Pos:POSITION;
                float3 Nor:TEXCOORD0;
                float4 Ver:TEXCOORD1;
            };

            params vert(appdata_base ab)
            {
                params pms;

                pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
                pms.Nor = ab.normal;
                pms.Ver = ab.vertex; 

                return pms;
            }

            float4 frag(params INPUT):COLOR
            {
                float4 Col = UNITY_LIGHTMODEL_AMBIENT;

                float3 N = normalize(UnityObjectToWorldNormal(INPUT.Nor));
                float3 L = normalize(WorldSpaceLightDir(INPUT.Ver));
                float DotNL = saturate(dot(N,L));

                Col = Col + DotNL*_LightColor0;

                float3 BJ = normalize(L + normalize(WorldSpaceViewDir(INPUT.Ver)));

                float DotNBJ = saturate(dot(BJ,N));

                Col = Col*0.75 + pow(DotNBJ * _SpecularColor*0.6,3);

                return Col;
            }
            ENDCG
        }
    }
}

你可能感兴趣的:(Unity3D,Shader)