Particles around a Sprite Renderer (3D depth like a Quad)

I’m making a game with 2D sprites. I want to use particle effects that exist in 3D space.

Here I’m using a particle system to create rings of particles. The quad on the right exists in 3D space and correctly blocks the particles behind it. The Sprite Renderer on the left only goes behind or in front of all the particles.

I need a way to give my sprites 3D depth like a quad. Apparently this can be achieved with custom shaders, but I haven’t been able to get this to work. Can someone please show me how to do this, or point me in the right direction?

Link to the example project:
drive(dot)google(dot)com/open?id=1imyjxSMsxDaIFA-YSJTYxP8HQnRSzrES

Modify your Sprite shader to write Depth, then it behaves like the quad.

Using AlphaTest or Cutout shaders no longer let you smoothly alpha-fade though. A pixel is either visible or not, there are no shades of transparency. Games sometimes use a dither-pattern for alpha-fading in this case instead.

1 Like

Thanks for your reply.

I’ve tried modifying a shader like this, but I must be doing something wrong. If I edit the default sprite shader and turn ZWrite to “On”, it behaves strangely as the camera moves. (Plus there’s no transparency at all)

4775096--455090--upload_2019-7-23_17-49-7.png
4775096--455093--upload_2019-7-23_17-49-26.png

Am I missing something here?

Below is an example of a cutout sprite shader and I also attached your example project that is using the shader below.

// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

// This is a modified Unity Sprite-Default shader to help in this forum thread:
// https://discussions.unity.com/t/751591

Shader "Sprites/Cutout"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    }

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

        Cull Off
        Lighting Off
        ZWrite On
        ZTest LEqual
       
        Pass
        {
        CGPROGRAM
            #pragma vertex SpriteVert
            #pragma fragment CutoutSpriteFrag
            #pragma target 2.0
            #pragma multi_compile_instancing
            #pragma multi_compile_local _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            #include "UnitySprites.cginc"
           
            float _Cutoff;
           
            fixed4 CutoutSpriteFrag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
                c.rgb *= c.a;
                clip(c.a - _Cutoff);
               
                return c;
            }       
        ENDCG
        }
    }
}

4775132–455144–sprite-cutout.zip (26.4 KB)

1 Like

Thank you so much! That’s exactly what I was looking for.

I see what you mean about the transparency. I suppose that’s a limit I have to put up with.

1 Like