Sprite Outline Shader

Hello there,

im looking for a sprite outline diffuse shader. When a player is behind a wall then you only see the outline shader.

can someone help me? i have no experience in shaders.

Thank you, but i need something where the outline is not always showing only there where a bodypart or a sprite is behind an other specific sprite

i think it might be your best shot. not many good free open source outline shaders for unity that i could find. from page 4 of that thread:

thank you for your help. its not exactly what i need. i hope i will find a solution

I managed to use this shader as a base for the effect you wanted using stencils. It’s not perfect (I made it in a rush), and could be fixed up, but it should help you get started. Let me know if you need any more help.

First Shader

Shader "Outlined/Blocked Silhouette Only ZTest Fail" {
    Properties{
        _OutlineColor("Outline Color", Color) = (0,0,0,1)
        _Outline("Outline width", Range(0.0, 0.5)) = .005
    }

        CGINCLUDE
        #include "UnityCG.cginc"

        struct appdata {
        float4 vertex : POSITION;
        float3 normal : NORMAL;
    };

    struct v2f {
        float4 pos : POSITION;
        float4 color : COLOR;
    };

    uniform float _Outline;
    uniform float4 _OutlineColor;

    v2f vert(appdata v) {
        // just make a copy of incoming vertex data but scaled according to normal direction
        v2f o;
        o.pos = UnityObjectToClipPos(v.vertex);

        float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
        float2 offset = TransformViewToProjection(norm.xy);

        o.pos.xy += offset * o.pos.z * _Outline;
        o.color = _OutlineColor;
        return o;
    }
    ENDCG

    SubShader{
        Name "FULL"
        Tags { "Queue" = "Transparent" }
        ztest equal

        Pass {
            Name "BASE"
            Cull Back
            Blend Zero One
            ztest always
            Stencil {
Ref 1
Comp always
Pass replace
}
         

        // uncomment this to hide inner details:
        //Offset -8, -8

            SetTexture[_OutlineColor] {
                ConstantColor(0,0,0,0)
                Combine constant
            }
        }

        // note that a vertex shader is specified here but its using the one above
        Pass {
            Name "BLOCK"
            Tags { "LightMode" = "Always" "Queue" = "Transparent"}
            Cull Front
            Ztest greater
            Stencil {
Ref 1
Comp notEqual
}

        // you can choose what kind of blending mode you want for the outline
        //Blend SrcAlpha OneMinusSrcAlpha // Normal
        //Blend One One // Additive
        Blend One OneMinusDstColor // Soft Additive
        //Blend DstColor Zero // Multiplicative
        //Blend DstColor SrcColor // 2x Multiplicative

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        half4 frag(v2f i) :COLOR {
            return i.color;
        }
        ENDCG
        }

        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" "Queue" = "Transparent"}
            Cull Front
            Ztest greater
            Stencil {
Ref 1
Comp notEqual
}

        // you can choose what kind of blending mode you want for the outline
        //Blend SrcAlpha OneMinusSrcAlpha // Normal
        //Blend One One // Additive
        Blend One OneMinusDstColor // Soft Additive
        //Blend DstColor Zero // Multiplicative
        //Blend DstColor SrcColor // 2x Multiplicative

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        half4 frag(v2f i) :COLOR {
            return i.color;
        }
        ENDCG
        }
    }

        Fallback "Diffuse"
}

Second Shader

Shader "Unlit/UnlitBasic"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        //ztest always

        Pass
        {
            Name "BASIC"
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog
         
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
         
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
         
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

Third shader combining the two (this is the one you’ll use on a material)

Shader "Outline/Full" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}

        _OutlineColor("Outline Color", Color) = (0,0,0,1)
        _Outline("Outline width", Range(0.0, 0.5)) = .005
    }

    SubShader {
        Tags { "Queue" = "Transparent" }
        UsePass "Unlit/UnlitBasic/BASIC"
        UsePass "Outlined/Blocked Silhouette Only ZTest Fail/BASE"
        UsePass "Outlined/Blocked Silhouette Only ZTest Fail/BLOCK"
    }
 
    Fallback "Diffuse"
}
1 Like