0
\$\begingroup\$

So Im trying to use this shader I had from a while ago to implement shadows in a 2D top down game. In order to show clouds on top of the game. At the moment, Sprites dont overlap which is good, but the shape of the sprite rendered by the sprite renderer is completely different to the one inserted in the sprite renderer, as the sprites in the sprite renderer are circles not squares. And it seems to cover any other sprite in the scenes which is not what a shade is supposed to be like.

How can I modify the shader so that it respects the sprite. Also I want it to react to lighting the same way as sprite diffuse does.

Shader "Custom/OverlappingDiffuse"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" { }
        _Color ("Tint", Color) = (1, 1, 1, 1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    }

    SubShader
    {
        Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True" }

        Cull Off
        Lighting On
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Stencil
        {
            Ref 1
            // Comp NotEqual
            Comp NotEqual
            Pass Replace
            Fail Keep
            ZFail Keep
            ReadMask 1
            WriteMask 1
        }

        CGPROGRAM

        #pragma surface surf Lambert vertex:vert nofog keepalpha
        #pragma multi_compile _ PIXELSNAP_ON
        #pragma multi_compile _ ETC1_EXTERNAL_ALPHA

        sampler2D _MainTex;
        fixed4 _Color;
        sampler2D _AlphaTex;

        struct Input
        {
            float2 uv_MainTex;
            fixed4 color;
        };


        void vert(inout appdata_full v, out Input o)
        {
            #if defined(PIXELSNAP_ON)
                v.vertex = UnityPixelSnap(v.vertex);
            #endif

            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.color = v.color * _Color;
        }

        fixed4 SampleSpriteTexture(float2 uv)
        {
            fixed4 color = tex2D(_MainTex, uv);

            #if ETC1_EXTERNAL_ALPHA
                color.a = tex2D(_AlphaTex, uv).r;
            #endif //ETC1_EXTERNAL_ALPHA

            return color;
        }

        void surf(Input IN, inout SurfaceOutput o)
        {
            fixed4 c = SampleSpriteTexture(IN.uv_MainTex) * IN.color;
            o.Albedo = c.rgb * c.a;
            o.Alpha = c.a;
        }
        ENDCG

    }

    Fallback "Transparent/VertexLit"
}

This is how is showing up in the editor:

here is what I want , and want I am getting at the moment:

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can you clarify what you want it to look like instead? \$\endgroup\$
    – DMGregory
    Commented Jun 1, 2020 at 10:41
  • \$\begingroup\$ They are two circles, but they show like two irregular octagons \$\endgroup\$
    – Kepol
    Commented Jun 1, 2020 at 10:48
  • \$\begingroup\$ Please see the extra picture I attached, I put two circles inside the sprite renderer, but they show up like different shapes and wont work as a shade when being put on top of other sprite renderers like the yellow square. The yellow square in this case will have a different material : sprite diffuse \$\endgroup\$
    – Kepol
    Commented Jun 1, 2020 at 11:12

1 Answer 1

0
\$\begingroup\$

Ok I found the solution here https://forum.unity.com/threads/2d-issue-with-overlapping-transparent-objects.355025/ :

Shader "multiply2D"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
        _Stencil ("Stencil ID", Float) = 0
        _ColorMask ("Color Mask", Float) = 15
        _ClipAlpha ("ClipAlpha", Float) = 0.2
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Stencil
        {
            Ref [_Stencil]
            Comp NotEqual
            Pass Replace
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                half2 texcoord  : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
            };

            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            fixed _ClipAlpha;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.worldPosition = IN.vertex;
                OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);

                OUT.texcoord = IN.texcoord;

                #ifdef UNITY_HALF_TEXEL_OFFSET
                OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
                #endif

                OUT.color = IN.color * _Color;
                return OUT;
            }

            sampler2D _MainTex;

            fixed4 frag(v2f IN) : SV_Target
            {
                half4 color = (tex2D(_MainTex, IN.texcoord) );
                color.rgb=_Color.rgb;
                color.a*=_Color.a;
                clip (color.a - _ClipAlpha);
                return color;
            }
        ENDCG
        }
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ This answer would be even better if it explained why this code fixes the issue. \$\endgroup\$
    – DMGregory
    Commented Jun 2, 2020 at 11:54
  • \$\begingroup\$ @DMGregory I wish I could... \$\endgroup\$
    – Kepol
    Commented Jun 2, 2020 at 14:21
  • \$\begingroup\$ I think the key is here. color.a*=_Color.a; clip (color.a - _ClipAlpha); \$\endgroup\$
    – Kepol
    Commented Jun 2, 2020 at 14:41
  • \$\begingroup\$ @DMGregory using this I have the problem that the shades completely hides the character sprite once the shade is on top of him, instead of being transparent, any idea how I could tweak the shader, I tried changing variables values in the material but couldnt get it to be behind the character \$\endgroup\$
    – Kepol
    Commented Sep 10, 2020 at 7:17
  • \$\begingroup\$ Why are you tagging me in this? I didn't write this shader. \$\endgroup\$
    – DMGregory
    Commented Sep 10, 2020 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .