光照
Shader "chicai/LightMode"
{
    Properties
    {
        _DiffuseIntensity("Diffuse Intensity", float) = 1
        _SpecularIntensity("Specular Intensity", float) = 1
        _Gloss("Gloss", float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque"}
        LOD 100

        Pass
        {
            Tags { "LightMode"="ForwardBase" }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

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


            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                half3 normal:NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                half3 worldNormal:TEXCOORD1;
                float3 worldPos:TEXCOORD2;
            };

            half _DiffuseIntensity;
            float _SpecularIntensity;
            float _Gloss;

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

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = 0;
                fixed4 Ambient = unity_AmbientSky;
                half Kd = _DiffuseIntensity;
                fixed4 LightColor = _LightColor0;
                fixed3 N = normalize( i.worldNormal );
                fixed3 L = _WorldSpaceLightPos0;
                fixed4 diffuse = Ambient + Kd * LightColor * max(0,dot(N,L));
                col += diffuse;

                //Phong高光
                //float3 reflectDir = reflect(-L, N);
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);
                // fixed4 specularColor = _LightColor0 * _SpecularIntensity * (pow( max( dot(reflectDir,viewDir) ,0),_Gloss) );
                // col += specularColor;

               
                //Bilnn-Phong高光
                fixed4 specularColor = _LightColor0 * _SpecularIntensity * (pow( max(dot(i.worldNormal, normalize(L+viewDir)),0), _Gloss));
                col += specularColor;
                return col;
            }
            ENDCG
        }

        Pass
        {
            Tags { "LightMode"="ForwardAdd" }
            Blend One One
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdadd //Builtin keywords used: POINT DIRECTIONAL SPOT POINT_COOKIE DIRECTIONAL_COOKIE
            #pragma skip_variants DIRECTIONAL POINT_COOKIE DIRECTIONAL_COOKIE//剔除不要的变体

            #include "UnityCG.cginc"
            #include "Lighting.cginc"//_LightColor0 _WorldSpaceLightPos0
            #include "AutoLight.cginc"//unity_WorldToLight _LightTexture0 衰减贴图

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                half3 normal:NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                half3 worldNormal:TEXCOORD1;
                float3 worldPos:TEXCOORD2;
            };

            half _DiffuseIntensity;

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

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 LightColor = _LightColor0;
                fixed3 N = normalize( i.worldNormal );
                fixed3 L = _WorldSpaceLightPos0;

                //float3 lightCoord = mul(unity_WorldToLight, float4(i.worldPos.xyz,1)).xyz;//距离灯光位置 点光源的
                //float atten = tex2D(_LightTexture0, dot(lightCoord,lightCoord));
                UNITY_LIGHT_ATTENUATION(atten, 0, i.worldPos);//光照衰减 第二参数计算阴影的



                fixed4 diffuse = LightColor * max(0,dot(N,L)) * atten;

                return diffuse;
            }
            ENDCG
        }
    }
}



首页 我的博客
粤ICP备17103704号