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

Shader Background Color instread of color masking

Discussion in 'Shaders' started by moayadrayyan99, May 18, 2021.

  1. moayadrayyan99

    moayadrayyan99

    Joined:
    Mar 1, 2018
    Posts:
    1
    I have a text and part of it will be highlighted with green background but the text color should remain the same.
    so i need this result.
    upload_2021-5-18_11-28-5.png

    but am getting this result.
    upload_2021-5-18_11-27-47.png


    My Shader is :
    Note that the shader should be masked to fit within the page scrolls.

    Code (JavaScript):
    1. Shader "Shader Forge/test1" {
    2.  
    3. Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (0.682353,0.9294118,0.7803922,1)
    7.         _StencilComp ("Stencil Comparison", Float) = 8
    8.         _Stencil ("Stencil ID", Float) = 0
    9.         _StencilOp ("Stencil Operation", Float) = 0
    10.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    11.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    12.         _ColorMask ("Color Mask", Float) = 15
    13.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    14.     }
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19.             "Queue"="Transparent"
    20.             "IgnoreProjector"="True"
    21.             "RenderType"="Transparent"
    22.             "PreviewType"="Plane"
    23.             "CanUseSpriteAtlas"="True"
    24.         }
    25.    
    26.         Stencil
    27.         {
    28.             Ref [_Stencil]
    29.             Comp [_StencilComp]
    30.             Pass [_StencilOp]
    31.             ReadMask [_StencilReadMask]
    32.             WriteMask [_StencilWriteMask]
    33.         }
    34.         Cull Off
    35.         Lighting Off
    36.         ZWrite Off
    37.         ZTest [unity_GUIZTestMode]
    38.         Blend SrcAlpha One
    39.  
    40.         ColorMask [_ColorMask]
    41.         Pass
    42.         {
    43.             Name "Default"
    44.         CGPROGRAM
    45.             #pragma vertex vert
    46.             #pragma fragment frag
    47.             #pragma target 2.0
    48.             #include "UnityCG.cginc"
    49.             #include "UnityUI.cginc"
    50.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
    51.        
    52.             struct appdata_t
    53.             {
    54.                 float4 vertex   : POSITION;
    55.                 float4 color    : COLOR;
    56.                 float2 texcoord : TEXCOORD0;
    57.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    58.             };
    59.             struct v2f
    60.             {
    61.                 float4 vertex   : SV_POSITION;
    62.                 fixed4 color    : COLOR;
    63.                 float2 texcoord  : TEXCOORD0;
    64.                 float4 worldPosition : TEXCOORD1;
    65.                 UNITY_VERTEX_OUTPUT_STEREO
    66.             };
    67.        
    68.             fixed4 _Color;
    69.             fixed4 _TextureSampleAdd;
    70.             float4 _ClipRect;
    71.          
    72.             v2f vert(appdata_t IN)
    73.             {
    74.                 v2f OUT;
    75.                 UNITY_SETUP_INSTANCE_ID(IN);
    76.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    77.                 OUT.worldPosition = IN.vertex;
    78.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
    79.                 OUT.texcoord = IN.texcoord;
    80.            
    81.                 OUT.color = IN.color * _Color;
    82.                 return OUT;
    83.             }
    84.  
    85.  
    86.  
    87.             sampler2D _MainTex;
    88.             fixed4 frag(v2f IN) : SV_Target
    89.             {
    90.                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    91.            
    92.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    93.            
    94.                 #ifdef UNITY_UI_ALPHACLIP
    95.                 clip (color.a - 0.001);
    96.                 #endif
    97.                 return color;
    98.             }
    99.  
    100.        
    101.  
    102.         ENDCG
    103.         }
    104.     }
    105. }