Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Shaders] Changing Text Color with a Quad

Discussion in 'Shaders' started by CG522, Mar 8, 2017.

  1. CG522

    CG522

    Joined:
    Dec 6, 2016
    Posts:
    3
    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

    ---------

    Code (CSharp):
    1. Shader "Custom/Unlit/ColorChangeShader"{
    2.     Properties
    3.     {
    4.         _Color("Tint Color", Color) = (1,1,1,1)
    5.         _Color2("Tint 2 Color", Color) = (0,0,0,0)
    6.     }
    7.  
    8.         SubShader
    9.     {
    10.         Tags{ "Queue" = "Transparent" }
    11.  
    12.         Pass
    13.     {
    14.         ZWrite On
    15.         ColorMask 0
    16.  
    17.     }
    18.         //Blend OneMinusDstColor OneMinusSrcAlpha //invert blending, so long as FG color is 1,1,1,
    19.         //Blend OneMinusDstColor OneMinusSrcAlpha
    20.         BlendOp Max
    21.  
    22.         Pass
    23.     {
    24.  
    25.         CGPROGRAM
    26. #pragma vertex vert
    27. #pragma fragment frag
    28.         uniform float4 _Color;
    29.         uniform float4 _Color2;
    30.  
    31.     struct vertexInput
    32.     {
    33.         float4 vertex: POSITION;
    34.         float4 color : COLOR;
    35.     };
    36.  
    37.     struct fragmentInput
    38.     {
    39.         float4 pos : SV_POSITION;
    40.         float4 color : COLOR0;
    41.     };
    42.  
    43.     fragmentInput vert(vertexInput i)
    44.     {
    45.         fragmentInput o;
    46.         o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
    47.         o.color = i.color;
    48.         //o.color = _Color2;
    49.  
    50.        
    51.         return o;
    52.     }
    53.  
    54.     half4 frag(fragmentInput i) : COLOR
    55.     {
    56.        
    57.         if (any(i.color == _Color))
    58.         {
    59.             i.color = _Color2;
    60.         }
    61.        
    62.  
    63.         return i.color;
    64.     }
    65.  
    66.         ENDCG
    67.     }
    68.     }
    69. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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:
    http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/

    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.