ShadowMap-Example

Shader "QStudyShader/ShadowMap"{
    Properties{
        _MainTex("Main Texture",2D) = "white"{}
        _BaseColor("Base Color",Color) = (1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(8.0,200)) = 20.0
    }
        SubShader{
            pass {
            // 这个Pass专门为了生成ShadowMap
            Tags{"LightMode" = "ShadowCaster"}
            CGPROGRAM
            #pragma vertex VertexShadow
            #pragma fragment FragmentShadow
            #pragma multi_compile_shadowcaster
            #include "UnityCG.cginc"

            struct v2f_shadow {

                V2F_SHADOW_CASTER;
            };

            v2f_shadow VertexShadow(appdata_base v) {

                v2f_shadow o;
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                return o;
            }

            float4 FragmentShadow(v2f_shadow i) :SV_TARGET{

                SHADOW_CASTER_FRAGMENT(i)
            }

            ENDCG
        }
        pass {
            //这个Pass让物体接受阴影并且计算光照
            //让物体接受阴影,其实也就是让物体表面的颜色和阴影进行混合
            Tags{"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex Vertex
            #pragma fragment Fragment
            #pragma multi_compile_fwdbase
            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
                float2 uv : TEXCOORD2;
                SHADOW_COORDS(3)
                    //Shadow_Coords声明一个uv坐标,只不过是对ShadowMap采样。
             };

                sampler2D _MainTex;
                fixed4 _BaseColor;
                float _Gloss;

                v2f Vertex(a2v v) {

                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    o.worldNormal = UnityObjectToWorldNormal(v.normal);
                    o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                    o.uv = v.texcoord;
                    TRANSFER_SHADOW(o)    //转换阴影的uv坐标
                    return o;
                }
                fixed4 Fragment(v2f i) :SV_TARGET{

                    fixed3 worldNormal = normalize(i.worldNormal);
                    fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
                    fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

                    fixed3 albedo = tex2D(_MainTex,i.uv).xyz * _BaseColor.xyz;
                    fixed3 diffuse = _LightColor0.xyz * albedo * saturate(dot(lightDir,worldNormal));
                    fixed3 halfDir = normalize(viewDir + lightDir);
                    fixed3 specular = _LightColor0.xyz * pow(saturate(dot(halfDir,worldNormal)),_Gloss);

                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
                    UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);

                    return fixed4(ambient + (specular + diffuse) * atten,1.0);
                }

                ENDCG
            }
            pass {
                Blend One One
                Tags{"LightMode" = "ForwardAdd"}
                CGPROGRAM
                #pragma vertex Vertex
                #pragma fragment Fragment
                #pragma multi_compile_fwdadd_fullshadows
                #include "Lighting.cginc"
                #include "AutoLight.cginc"

                struct a2v {
                    float4 vertex : POSITION;
                    float3 normal : NORMAL;
                    float4 texcoord : TEXCOORD0;
                };

                struct v2f {
                    float4 pos : SV_POSITION;
                    float3 worldNormal : TEXCOORD0;
                    float3 worldPos : TEXCOORD1;
                    float2 uv : TEXCOORD2;
                    SHADOW_COORDS(3)
                        //Shadow_Coords声明一个uv坐标,只不过是对ShadowMap采样。
                    };

                    sampler2D _MainTex;
                    fixed4 _BaseColor;
                    float _Gloss;

                    v2f Vertex(a2v v) {

                        v2f o;
                        o.pos = UnityObjectToClipPos(v.vertex);
                        o.worldNormal = UnityObjectToWorldNormal(v.normal);
                        o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                        o.uv = v.texcoord;
                        TRANSFER_SHADOW(o)    //转换阴影的uv坐标
                        return o;
                    }
                    fixed4 Fragment(v2f i) :SV_TARGET{

                        fixed3 worldNormal = normalize(i.worldNormal);
                        fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
                        fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

                        fixed3 albedo = tex2D(_MainTex,i.uv).xyz * _BaseColor.xyz;
                        fixed3 diffuse = _LightColor0.xyz * albedo * saturate(dot(lightDir,worldNormal));
                        fixed3 halfDir = normalize(viewDir + lightDir);
                        fixed3 specular = _LightColor0.xyz * pow(saturate(dot(halfDir,worldNormal)),_Gloss);

                        UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos)


                        return fixed4((specular + diffuse) * atten,1.0);
                    }
                    ENDCG
                }

        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值