0
\$\begingroup\$

I would like to be able to make a Unity Sprite flash 2 colors:

  1. The entire sprite should flash Red (or whatever color is defined in the Sprite Color setting).
  2. There should be a Black outline around the Sprite.

DMGregory wrote the following code which modified the default Sprite Shader to wonderfully achieve the first goal:

    Pass
    {
    CGPROGRAM
        #pragma vertex SpriteVert
        #pragma fragment ColorLerpFrag // Use our version instead of the default.
        #pragma target 2.0
        #pragma multi_compile_instancing
        #pragma multi_compile_local _ PIXELSNAP_ON
        #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
        #include "UnitySprites.cginc"


        fixed4 ColorLerpFrag(v2f IN) : SV_Target
        {
            // Sample sprite texture.
            fixed4 c = SampleSpriteTexture (IN.texcoord);
            // Blend toward sprite component color depending on its alpha.
            c.rgb = lerp(IN.color.rgb, c.rgb, IN.color.a);
            // Pre-multiply alpha to work with the blending function.
            c.rgb *= c.a;
            return c;
        }
    ENDCG
    }

I have tried modifying the shader to also add a black outline around the Sprite, but can't get it to work.

Another possibility is to clone the GameObject with the Sprite Component, center the Sprite, turn it Black and then scale it up slightly. But it would be much better if this effect could be achieved with a shader, as cloning would add to the memory footprint.

Is it possible to modify the shader further to also create a black outline around the Sprite?

\$\endgroup\$
1
  • \$\begingroup\$ It's cheaper to get high-quality outlines if you can sample a signed distance field — say stored in the sprite's alpha channel. If that's not practical, then you have to sample the sprite multiple times at nearby locations to detect how close you are to an edge, and that sampling gets more expensive the thicker the outline needs to be (or the higher fidelity you want at corners/fine features). \$\endgroup\$
    – DMGregory
    May 24 at 11:47

0

You must log in to answer this question.

Browse other questions tagged .