Unity Shade HalfLambert 半兰伯特光照模型

Unity Shade HalfLambert 光照模型

picture

Unity Shade HalfLambert 半兰伯特光照模型_第1张图片

code

Shader "DC/LightModel/HalfLambert"
{
    Properties
    {
        _Color("Color",Color)=(0.5,0.5,0.5,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal: NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 worldNormal: NORMAL;
                float3 worldPos : TEXCOORD0;
            };

            fixed4 _Color;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldNormal = mul(unity_ObjectToWorld, v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                /*
                该在半条命游戏中被开发使用,用于解决模型背面形状丢失和过于扁平的问题
                formular:
                halfLambertCol = (dot(worldNormal * lightDir) * 0.5 + 0.5) * surfaceCol * lightCol
                */
                float3 lDir = UnityWorldSpaceLightDir(i.worldPos);
                float lambertDot = saturate(dot(i.worldNormal, normalize(lDir)));
                float halfLambert = lambertDot * 0.5 + 0.5;
                // float halfLambert = lambertDot;
                // fixed4 finalRgb = (halfLambert * _LightColor0 * _Color) + unity_AmbientSky;
                fixed4 finalRgb = (halfLambert * _LightColor0 * _Color);
                return fixed4(finalRgb.rgb, 1);
            }
            ENDCG
        }
    }
}

你可能感兴趣的:(unity,shader)