0
\$\begingroup\$

So I wipped up this shader that disolves an object. The problem is that the shadow of the object is unaffected from the alpha value. How do I fix this. Full code :

Shader "Custom/SwordDistortion" {
    Properties {
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _DissolveScale("Dissolve Progression", Range(0.0, 1.0)) = 0.0
        _DissolveTex("Dissolve Texture", 2D) = "white" {}
        _GlowIntensity("Glow Intensity", Range(0.0, 5.0)) = 0.05
        _GlowScale("Glow Size", Range(0.0, 5.0)) = 1.0
        _Glow("Glow Color", Color) = (1, 1, 1, 1)
        _GlowEnd("Glow End Color", Color) = (1, 1, 1, 1)
        _GlowColFac("Glow Colorshift", Range(0.01, 2.0)) = 0.75
        _DissolveStart("Dissolve Start Point", Vector) = (1, 1, 1, 1)
        _DissolveEnd("Dissolve End Point", Vector) = (0, 0, 0, 1)
        _DissolveBand("Dissolve Band Size", Float) = 0.25
    }
    SubShader {
        Tags { 
        "Queue" = "Transparent"
        "RenderType" = "TransparentCutout"
    }


        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard addshadow alpha:fade
        #pragma vertex vert 
        // using shader model 4.6 for even nicer lighting
        #pragma target 4.6

        sampler2D _MainTex ;
        sampler2D _DissolveTex ;
        float _DissolveScale;
        float3 _DissolveStart;
        float3 _DissolveEnd;
        float _DissolveBand;
        float _GlowIntensity;
        float _GlowScale;
        float _GlowColFac;
        fixed4 _Color;
        fixed4 _Glow;
        fixed4 _GlowEnd;
        struct Input {
            float2 uv_MainTex;
            float3 dGeometry;
        };

        //Precompute dissolve direction.
            static float3 dDir = normalize(_DissolveEnd - _DissolveStart);

        //Precompute gradient start position.
        static float3 dissolveStartConverted = _DissolveStart - _DissolveBand * dDir;

        //Precompute reciprocal of band size.
        static float dBandFactor = 1.0f / _DissolveBand;

        void vert(inout appdata_full v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);

            //Calculate geometry-based dissolve coefficient.
            //Compute top of dissolution gradient according to dissolve progression.
            float3 dPoint = lerp(dissolveStartConverted, _DissolveEnd, _DissolveScale);

            //Project vector between current vertex and top of gradient onto dissolve direction.
            //Scale coefficient by band (gradient) size.
            o.dGeometry = dot(v.vertex - dPoint, dDir) * dBandFactor;
        }


        void surf (Input IN, inout SurfaceOutputStandard o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            half dBase = -2.0f * _DissolveScale;
            fixed4 dTex = tex2D(_DissolveTex, IN.uv_MainTex);
            half dTexRead = dTex.r + dBase;
            half dFinal = dTexRead + IN.dGeometry;
            half alpha = clamp(dFinal, 0.0f, 1.0f);
            //Shift the computed raw alpha value based on the scale factor of the glow.
            //Scale the shifted value based on effect intensity.
            half dPredict = (_GlowScale - dFinal) * _GlowIntensity;
            //Change colour interpolation by adding in another factor controlling the gradient.
            half dPredictCol = (_GlowScale * _GlowColFac - dFinal) * _GlowIntensity;

            //Calculate and clamp glow colour.
            fixed4 glowCol = dPredict * lerp(_Glow, _GlowEnd, clamp(dPredictCol, 0.0f, 1.0f));
            glowCol = clamp(glowCol, 0.0f, 1.0f);
            o.Albedo = c.rgb;
            o.Alpha = alpha;

            o.Emission = glowCol;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
\$\endgroup\$
  • \$\begingroup\$ Typical shadow mapping doesn't handle partial transparency. The light at a particular pixel is either occluded, or it isn't. So you'll need to write a shadow pass that uses alpha testing to dissolve by clipping out a whole pixel at a time, rather than fading it. \$\endgroup\$ – DMGregory Apr 13 '18 at 11:58
  • \$\begingroup\$ So you mean I should chage how I do the entire dissolve effect or add a custom shadow pass to this shader ? \$\endgroup\$ – Uri Popov Apr 15 '18 at 5:26
  • \$\begingroup\$ "you'll need to write a shadow pass" \$\endgroup\$ – DMGregory Apr 15 '18 at 11:17

Your Answer

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

Browse other questions tagged or ask your own question.