4
\$\begingroup\$

I've been trying to add shadow to my flag that animated In my vertex shader but after adding shadow , my shadow is fixed and does not follow the animation of the flag.

for adding shadow I use below link:

Diffuse has a fallback of vertex lit, which contains within it two passes; Shadow Caster and Shadow Receiver. These are the two passes that allow the shader to cast and receive shadows. http://polycount.com/discussion/131260/how-to-get-custom-shaders-to-recieve-shadows

flag animating but shadow is fixed

my shader is here:

Shader "Custom/Flag" {
    Properties {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Speed ("Speed", Range(0, 5.0)) = 1
        _Frequency ("Frequency", Range(0, 1.3)) = 1
        _Amplitude ("Amplitude", Range(0, 5.0)) = 1
    }
    SubShader {
        Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"  }
        Cull off

        Pass {

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase 

            #include "UnityCG.cginc"
            #include "AutoLight.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;

            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                LIGHTING_COORDS(1, 2)
            };

            float _Speed;
            float _Frequency;
            float _Amplitude;

            v2f vert(appdata_base v)
            {
                v2f o;
                v.vertex.y +=  cos((v.vertex.x + _Time.y * _Speed) * _Frequency) * _Amplitude * (v.vertex.x - 5);
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                TRANSFER_VERTEX_TO_FRAGMENT(o);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return tex2D(_MainTex, i.uv);
            }

            ENDCG

        }
    }
    FallBack "VertexLit"
}

Finally I use "LightMode"="ShadowCaster" but this Tag prevents rendering my flag like this problem.A solution came to my mind that works correctly "using two pass for implementing shadows: 1.normal pass 2.ShadowCaster pass" but I think It's not optimize.

enter image description here

this is my solution as Pseudo Code:

   SubShader {
        Tags { "RenderType"="Opaque" }
        Cull off
        Pass {
            CGPROGRAM
            Shader "Custom/Flag" // normal Pass for animating flag
            ENDCG
        }

        Pass {
            Tags { "LightMode"="ShadowCaster" }

            CGPROGRAM
            Shader "Custom/Flag" //ShadowCaster Pass for recieving shadow
            ENDCG
        }
    }
    FallBack "VertexLit"
}

Flag shader.unitypackage

In short I finding a way for adding shadows for vertex animated objects In optimized way.

\$\endgroup\$
1
\$\begingroup\$

The 2 pass system is the only way. It's how the engine handles shadows.

Here's an example of a shadow pass from unity's manual. You'd need to modify the shadow pass' vertex shader.

    // shadow caster rendering pass, implemented manually
    // using macros from UnityCG.cginc
    Pass
    {
        Tags {"LightMode"="ShadowCaster"}

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma multi_compile_shadowcaster
        #include "UnityCG.cginc"

        struct v2f { 
            V2F_SHADOW_CASTER;
        };

        v2f vert(appdata_base v)
        {
            v2f o;
            TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
            return o;
        }

        float4 frag(v2f i) : SV_Target
        {
            SHADOW_CASTER_FRAGMENT(i)
        }
        ENDCG
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.