Sprite with transparent pixels for specific color

Is it possible to have a Sprite which has all of its pixels of a specified color transparent?

Thanks!

This would likely be achieved through a custom shader. It is possible to have a knock-out shader that takes a specific color within a specific range, and causes that particular color of pixel to not be rendered. (transparent)

This WILL effect the performance of the shader. A single check, however, shouldn’t tax the game too heavily. Adding a lot of different conditionals usually hamstrings shaders. Having just one for knocking out a specific color is reasonably light.

Here, I cooked up a quickie Unlit shader that would do what you’re describing.

Shader "Unlit/PickColorCutout"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _PickColor ("Transparent Color", Color) = (1.0, 0.0, 1.0, 1.0)
        _AlphaFalloff ("Falloff", Float) = 0.01
    }
    SubShader
    {
        Tags { "Queue" = "AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector" = "True" }
        LOD 100

        Zwrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
           
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            fixed4 _PickColor;
            fixed _AlphaFalloff;
           
            fixed4 frag (v2f_img i) : COLOR
            {
                fixed4 col = tex2D(_MainTex, i.uv).rgba;
                if ((col.r <= _PickColor.r + _AlphaFalloff && col.r >= _PickColor.r - _AlphaFalloff) && (col.b <= _PickColor.b + _AlphaFalloff && col.b >= _PickColor.b - _AlphaFalloff) && (col.g <= _PickColor.g + _AlphaFalloff && col.g >= _PickColor.g - _AlphaFalloff)) { discard; }
                return col;
            }
            ENDCG
        }
    }
}
1 Like

Thanks Richard,

I just have to figure out how to plug this in and Im gold :smile: