[Shaders] Changing Text Color with a Quad

Hey there,

Im trying to sample behind a quad and change any pixel that is white to a different color.

Right now its kind of working but it seems as if the IF statement in the frag function is always true.

Is there anything you guys think I am doing wrong or any way I can replace the white pixels.

Im pretty new to shaders and it doesnt seem like many people use shaders for text so its hard to get a good
answer from the internets.

Thanks in advance,
CG


Shader "Custom/Unlit/ColorChangeShader"{
    Properties
    {
        _Color("Tint Color", Color) = (1,1,1,1)
        _Color2("Tint 2 Color", Color) = (0,0,0,0)
    }

        SubShader
    {
        Tags{ "Queue" = "Transparent" }

        Pass
    {
        ZWrite On
        ColorMask 0

    }
        //Blend OneMinusDstColor OneMinusSrcAlpha //invert blending, so long as FG color is 1,1,1,
        //Blend OneMinusDstColor OneMinusSrcAlpha
        BlendOp Max

        Pass
    {

        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
        uniform float4 _Color;
        uniform float4 _Color2;

    struct vertexInput
    {
        float4 vertex: POSITION;
        float4 color : COLOR;
    };

    struct fragmentInput
    {
        float4 pos : SV_POSITION;
        float4 color : COLOR0;
    };

    fragmentInput vert(vertexInput i)
    {
        fragmentInput o;
        o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
        o.color = i.color;
        //o.color = _Color2;

       
        return o;
    }

    half4 frag(fragmentInput i) : COLOR
    {
       
        if (any(i.color == _Color))
        {
            i.color = _Color2;
        }
       

        return i.color;
    }

        ENDCG
    }
    }
}

It looks like you’re misunderstanding what “i.color” is. In that shader, i.color is the vertex color of the quad you’re rendering being passed from the vertex shader to the fragment shader. By default all meshes will return “white” as the vertex color if no other color has been assigned.

I would suggest you read this to get a better overview of what shaders are and do:

As for “using shaders for text”, every bit of text you see in a Unity project is using a shader. In fact basically everything anything ever displays using a modern GPU is being rendered with some kind of shader at some point, just most of the time that shader is pretty simple, like “draw this texture on screen”. Unity’s built in font rendering is like this, it has a texture with all of the characters in it and the engine is drawing a rectangle of just one character at a time.

For what you’re currently trying to do, as described, you would need to use a grab pass. However I suspect the results you get once you get a grabpass shader working properly isn’t going to be something you like.