美文网首页Unity_Shader
同时处理阴影和各种光源的光照衰减

同时处理阴影和各种光源的光照衰减

作者: _Arturia | 来源:发表于2018-08-24 11:47 被阅读18次

    Shader "_MyShader/6_Light/2_ShadowAndAttenuation"
    {
    Properties
    {
    _DiffuseColor ("DiffuseColor",COLOR) =(1,1,1,1)
    _SpecularColor ("SpecularColor",COLOR) =(1,1,1,1)
    _SpecularRange ("SpecularRange",Range(5,100)) = 20
    }
    SubShader
    {
    Pass
    {
    Tags{"LightMode" = "ForwardBase"}
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_fwdbase

            #include "Lighting.cginc"
            #include "AutoLight.cginc"
    
    
            fixed4 _DiffuseColor;
            fixed4 _SpecularColor;
            float _SpecularRange;
    
            struct a2v {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };
    
            struct v2f{
                float4 pos:SV_POSITION;
                fixed3 worldNormalDir:POSITION1;
                fixed3 worldLightDir:POSITION2;
                half3 reflectDir:POSITION3;
                fixed3 worldViewDir:POSITION4;
                //Blinn-Phong
                fixed3 blinn_Phong_Dir:POSITION5;
                //声明阴影坐标
                float4 worldPos:POSITION6;
                SHADOW_COORDS(2)
            };
    
            v2f vert(a2v v){
                v2f o;
                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
    
                o.worldPos = mul(_Object2World,v.vertex);
    
                o.worldLightDir = normalize(_WorldSpaceLightPos0);
                o.worldNormalDir = normalize(mul(v.normal,(float3x3)_World2Object));
    
                //计算反射向量
                o.reflectDir = normalize(reflect(-o.worldLightDir,o.worldNormalDir));
                //计算视角向量
                o.worldViewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));
    
                //Blinn-Phong
                o.blinn_Phong_Dir = normalize(o.worldLightDir + o.worldViewDir);
    
                //计算并输出阴影坐标
                TRANSFER_SHADOW(o);
    
                return o;
            }
    
            fixed4 frag(v2f i):SV_Target{
                UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
    
                //漫反射阶段
                fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;
    
                fixed3 diffuseColor = _LightColor0 * _DiffuseColor*saturate(dot(i.worldNormalDir,i.worldLightDir));
    
                //高光阶段
                fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.reflectDir,i.worldViewDir)),_SpecularRange);
                //Blinn-Phong光照
                //fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.worldNormalDir,i.blinn_Phong_Dir)),_SpecularRange);
                
                fixed4 col = fixed4(ambientColor + (diffuseColor + specularColor) * atten,1);
                return col;
            }
    
            
            ENDCG
        }
    
        //-----------------------------------------------------------------------------------------------------
    
        Pass
        {
            Tags{"LightMode" = "ForwardAdd"}
            Blend One One
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdadd_fullshadows
            
            #include "Lighting.cginc"
            #include "AutoLight.cginc"
    
    
            fixed4 _DiffuseColor;
            fixed4 _SpecularColor;
            float _SpecularRange;
    
            struct a2v {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };
    
            struct v2f{
                float4 pos:SV_POSITION;
                fixed3 worldNormalDir:POSITION1;
                fixed3 worldLightDir:POSITION2;
                half3 reflectDir:POSITION3;
                fixed3 worldViewDir:POSITION4;
                //Blinn-Phong
                fixed3 blinn_Phong_Dir:POSITION5;
                float4 worldPos:POSITION6;
                SHADOW_COORDS(2)
            };
    
            v2f vert(a2v v){
                v2f o;
                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
    
                o.worldPos = mul(_Object2World,v.vertex);
                
            #ifdef USING_DIRECTIONAL_LIGHT
                o.worldLightDir = normalize(_WorldSpaceLightPos0);
            #else
                o.worldLightDir = normalize(_WorldSpaceLightPos0 - o.worldPos);
            #endif
    
                o.worldNormalDir = normalize(mul(v.normal,(float3x3)_World2Object));
    
                //计算反射向量
                o.reflectDir = normalize(reflect(-o.worldLightDir,o.worldNormalDir));
                //计算视角向量
                o.worldViewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));
    
                //Blinn-Phong
                o.blinn_Phong_Dir = normalize(o.worldLightDir + o.worldViewDir);
    
                TRANSFER_SHADOW(o);
    
                return o;
            }
    
            fixed4 frag(v2f i):SV_Target{
                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos.xyz)
    
                //漫反射阶段
                //fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;
    
                fixed3 diffuseColor = _LightColor0 * _DiffuseColor*saturate(dot(i.worldNormalDir,i.worldLightDir));
    
                //高光阶段
                fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.reflectDir,i.worldViewDir)),_SpecularRange);
                //Blinn-Phong光照
                //fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.worldNormalDir,i.blinn_Phong_Dir)),_SpecularRange);
    
    
                fixed4 col = fixed4((diffuseColor + specularColor) * atten,1);
                return col;
            }
    
            
            ENDCG
        }
    
    }
    FallBack "VertexLit"
    

    }

    相关文章

      网友评论

        本文标题:同时处理阴影和各种光源的光照衰减

        本文链接:https://www.haomeiwen.com/subject/eisbiftx.html