平面阴影

2017-07-27  本文已影响82人  墨明_棋妙

前言:

阴影的实现方式之一是平面阴影,也就是假阴影,这种阴影适合应用到平面上,不适合不规则物体上(会穿帮)。但是这种阴影效率更高,因为根本不需要计算真实的光照与阴影信息,也是目前多数手游公司采用的一种阴影方案之一(王者荣耀等一大批)。

原理:

//点光源
                float4 pointLightGroundPos = mul(World2Ground,pointLightWorldPos);
                float3 lightDir;
                float4 vt;
                vt = mul(unity_ObjectToWorld,v.vertex);
                vt = mul(World2Ground,vt);
                lightDir = normalize((vt-pointLightGroundPos).xyz);
                vt.xz = vt.xz - (lightDir.xz * vt.y)/lightDir.y;
                vt.y = 0;
地面:
Stencil {
        Ref 1
            Comp always
        Pass replace
}
目标物体:
Stencil{
    Ref 1
    Comp equal
}
Shader "FFD/MyPlanerShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderQueue" = "Transparent"}
        LOD 100

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

            #include "unitycg.cginc"
            #include "lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
                o.normal = mul(v.normal,(float3x3)unity_WorldToObject);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex,i.uv);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                float3 normal = normalize(i.normal);
                float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.xyz * saturate(dot(normal,lightDir));

                return fixed4(col.xyz*(ambient+diffuse),1);
            }

            ENDCG
        }


        Pass
        {
            Tags{"LightMode" = "ForwardBase" }
            Blend SrcAlpha OneMinusSrcAlpha 
            Offset -1,0
            Stencil{
                Ref 1
                Comp equal
            }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
        
            
            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            uniform float4x4 World2Ground;
            uniform float4x4 Ground2World;
            uniform float4 MainColor;
            uniform float4 pointLightWorldPos;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                //平行光
//              float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
//              lightDir = mul((float3x3)World2Ground,lightDir);
//              lightDir = normalize(lightDir);

                //点光源
                float4 pointLightGroundPos = mul(World2Ground,pointLightWorldPos);
                float3 lightDir;
                float4 vt;
                vt = mul(unity_ObjectToWorld,v.vertex);
                vt = mul(World2Ground,vt);
                lightDir = normalize((vt-pointLightGroundPos).xyz);
                vt.xz = vt.xz - (lightDir.xz * vt.y)/lightDir.y;
                vt.y = 0;
                o.vertex = mul(Ground2World,vt);
                o.vertex = mul(unity_WorldToObject,o.vertex);
                o.vertex = UnityObjectToClipPos(o.vertex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col = fixed4(0.3,0.3,0.3,0.3);
                return col;
            }
            ENDCG
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读