Is It Possible To Clip a Sprite With Another Sprite?

Hi, Pals,

I am not familiar with ShaderLab or CG Hardware, so I am not sure if this is possible?

I wanna to clip a Sprite (or particles, something else, too) with another Sprite, such as:

alt text
for [Portal]

and this

alt text

for hide.

I want the result to be like this:

alt text

I have referred these 2 articles to do some test:

http://forum.unity3d.com/threads/clip-texture-based-on-depth-buffer.148379/​​

and

http://answers.unity3d.com/questions/590800/how-to-cullrender-to-through-a-window.html

​​BUT, actually, the results are something like this:
alt text

(It drew on the POLYGON shape, NOT PER PIXEL TEST.)

Or if I try the BLEND op, it will be like that:
alt text

(Actually, if there are something else drew before these two things, the result will be the same with the last picture.)

I have tried to google Stencil Buffer, Alpha Blending things, but NO good luck, seems the hardware do NOT allow the Stencil Buffer writing base on Alpha Channel?

I will be deeply appreciate your help if some Shader expert can tell me how to achieve my original goal with Shader — Clip a Sprite (or Particles) with Another Sprite? Or please just let me know the CG hardware do not allow this kind of work in Unity3D.

Thanks,

The easiest way to is to have two textures on a material/shader and then just combine them in any way in that shader. But it might be not a perfect solution depending on what you’re trying to build.

Another solution is to write very specific values into alpha buffer during rendering of first sprite (the one that does culling). And when you draw the second sprite you can do special blending which uses values in alpha channel of screen buffer. Note that Blend can take 4 arguments (blend of color and blend of alpha)

This will work from the forum here:
http://forum.unity3d.com/threads/is-it-possible-to-clip-a-sprite-with-another-sprite.254144/#post-1679776


WOOOOOW…That’s brilliant !!! It’s work !!! (on Mac or iOS devices, “Discard” works, but “Alpha Test” is not working for my codes.)

Many many many deep appreciation !!! :slight_smile:

In my situation, I am using 2D system of Unity3D, the codes are modified from the “Sprite-Default” offered by Unity3D 4.5.1 here:
http://unity3d.com/unity/download/archive

I set them as “Sprite/Stencil Mask” and “Sprite/Stencil Draw In Mask”.

Mask:

Shader "Sprites/Stencil Mask"
{
    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 Off
        ZWrite Off
        Fog { Mode Off }
        Blend One OneMinusSrcAlpha

        Pass
        {
            Stencil
            {
                Ref 1
                Comp always
                Pass replace
            }
      
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile DUMMY PIXELSNAP_ON
            #include "UnityCG.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;
            };
      
            fixed4 _Color;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            sampler2D _MainTex;

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
                if (c.a<0.1) discard;            //Most IMPORTANT working Code
                c.rgb *= c.a;
                return c;
            }
        ENDCG
        }
    }
}

Draw In Stencil Mask:

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

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

        Cull Off
        Lighting Off
        ZWrite Off
        Fog { Mode Off }
        Blend One OneMinusSrcAlpha

        Pass
        {
            Stencil
            {
                Ref 1
                Comp Equal
            }
  
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile DUMMY PIXELSNAP_ON
            #include "UnityCG.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;
            };
      
            fixed4 _Color;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            sampler2D _MainTex;

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
                c.rgb *= c.a;
                return c;
            }
        ENDCG
        }
    }
}

I am trying to set a changeable “Ref” value for different type of objects.

Anyway, the codes above work for the same stencil “Ref” value, for other guys who need it. :wink: