Search Unity

Shader is black on UI text

Discussion in 'Shaders' started by id0, Feb 25, 2021.

  1. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    Hello. I'm using this shader for simulate screen glitches for UI. And it's working fine on images, but the text are always black. Text have a font, but no color. I'm really bad about shaders, please tell me what is wrong with it?

    Shader code:

    Code (CSharp):
    1. Shader "Sprites/Glitch"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.      
    7.         _Color ("Tint", Color) = (1,1,1,1)
    8.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    9.         _GlitchInterval ("Glitch interval time [seconds]", Float) = 0.16
    10.         _DispProbability ("Displacement Glitch Probability", Float) = 0.022
    11.         _DispIntensity ("Displacement Glitch Intensity", Float) = 0.09
    12.         _ColorProbability("Color Glitch Probability", Float) = 0.02
    13.         _ColorIntensity("Color Glitch Intensity", Float) = 0.07
    14.         [MaterialToggle] _WrapDispCoords ("Wrap disp glitch (off = clamp)", Float) = 1
    15.         [MaterialToggle] _DispGlitchOn ("Displacement Glitch On", Float) = 1
    16.         [MaterialToggle] _ColorGlitchOn ("Color Glitch On", Float) = 1
    17.     }
    18.  
    19.     SubShader
    20.     {
    21.         Tags
    22.         {
    23.             "Queue"="Transparent"
    24.             "IgnoreProjector"="True"
    25.             "RenderType"="Transparent"
    26.             "PreviewType"="Plane"
    27.             "CanUseSpriteAtlas"="True"
    28.         }
    29.  
    30.         Cull Off
    31.         Lighting Off
    32.         ZWrite Off
    33.         Fog { Mode Off }
    34.         Blend One OneMinusSrcAlpha
    35.  
    36.         Pass
    37.         {
    38.         CGPROGRAM
    39.             // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members pos)
    40.             #pragma exclude_renderers xbox360
    41.             #pragma vertex vert
    42.             #pragma fragment frag
    43.             #pragma target 3.0
    44.             #pragma multi_compile DUMMY PIXELSNAP_ON
    45.             #include "UnityCG.cginc"
    46.          
    47.             struct appdata_t
    48.             {
    49.                 float4 vertex   : POSITION;
    50.                 float4 color    : COLOR;
    51.                 float2 texcoord : TEXCOORD0;
    52.             };
    53.  
    54.             struct v2f
    55.             {
    56.                 float4 vertex   : SV_POSITION;
    57.                 fixed4 color    : COLOR;
    58.                 half2 texcoord  : TEXCOORD0;
    59.             };
    60.          
    61.             fixed4 _Color;
    62.          
    63.             v2f vert(appdata_t IN)
    64.             {
    65.                 v2f OUT;
    66.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    67.                 OUT.texcoord = IN.texcoord;
    68.              
    69.                 OUT.color = IN.color * _Color;
    70.                 #ifdef PIXELSNAP_ON
    71.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    72.                 #endif
    73.                 return OUT;
    74.             }
    75.  
    76.             sampler2D _MainTex;
    77.          
    78.             //Takes two values and returns a pseudo-random number between 0 (included) and 1 (excluded)
    79.             //It samples the sin function, scales it up (presumably to increase floating point error) and then takes it's fraction part (to get value between 0 and 1)
    80.             float rand(float x, float y){
    81.                 return frac(sin(x*12.9898 + y*78.233)*43758.5453);
    82.             }
    83.          
    84.             float _GlitchInterval;
    85.             float _DispIntensity;
    86.             float _DispProbability;
    87.             float _ColorIntensity;
    88.             float _ColorProbability;
    89.             float _DispGlitchOn;
    90.             float _ColorGlitchOn;
    91.             float _WrapDispCoords;
    92.             fixed4 frag(v2f IN) : SV_Target
    93.             {
    94.                 //This ensures that the shader only generates new random variables every [_GlitchInterval] seconds, e.g. every 0.5 seconds
    95.                 //During each interval the value wether the glitch occurs and how much the sprites glitches stays the same
    96.                 float intervalTime = floor(_Time.y / _GlitchInterval) * _GlitchInterval;
    97.  
    98.                 //Second value increased by arbitrary number just to get more possible different random values
    99.                 float intervalTime2 = intervalTime + 2.793;
    100.  
    101.                 //These values depend on time and the x/y translation of that sprite (top right and middle right value in the transformation matrix are translation)
    102.                 //The transformation matrix values are included so sprites with differen x/y values don't glitch at the same time
    103.                 float timePositionVal = intervalTime + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3];
    104.                 float timePositionVal2 = intervalTime2 + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3];
    105.  
    106.                 //Random chance that the displacement glich or color glitch occur
    107.                 float dispGlitchRandom = rand(timePositionVal, -timePositionVal);
    108.                 float colorGlitchRandom = rand(timePositionVal, timePositionVal);
    109.  
    110.                 //Precalculate color channel shift
    111.                 float rShiftRandom = (rand(-timePositionVal, timePositionVal) - 0.5) * _ColorIntensity;
    112.                 float gShiftRandom = (rand(-timePositionVal, -timePositionVal) - 0.5) * _ColorIntensity;
    113.                 float bShiftRandom = (rand(-timePositionVal2, -timePositionVal2) - 0.5) * _ColorIntensity;
    114.  
    115.                 //For the displacement glitch, the sprite is divided into strips of 0.2 * sprite height (5 stripes)
    116.                 //This value is the random offset each of the strip boundries get either up or down
    117.                 //Without this, each strip would be exactly a 5th of the sprite height, with this their height is slightly randomised
    118.                 float shiftLineOffset = float((rand(timePositionVal2, timePositionVal2) - 0.5) / 50);
    119.  
    120.                 //If the randomly rolled value is below the probability boundry and the displacement effect is turned on, apply the displacement effect
    121.                 if(dispGlitchRandom < _DispProbability && _DispGlitchOn == 1){
    122.                     IN.texcoord.x += (rand(floor(IN.texcoord.y / (0.2 + shiftLineOffset)) - timePositionVal, floor(IN.texcoord.y / (0.2 + shiftLineOffset)) + timePositionVal) - 0.5) * _DispIntensity;
    123.                     //Prevent the texture coordinate from going into other parts of the texture, especially when using texture atlases
    124.                     //Instead, loop the coordinate between 0 and 1
    125.                     if(_WrapDispCoords == 1){
    126.                         IN.texcoord.x = fmod(IN.texcoord.x, 1);
    127.                     }
    128.                     else{
    129.                         IN.texcoord.x = clamp(IN.texcoord.x, 0, 1);
    130.                     }
    131.                 }
    132.  
    133.                 //Sample the texture at the normal position and at the shifted color channel positions
    134.                 fixed4 normalC = tex2D(_MainTex, IN.texcoord);
    135.                 fixed4 rShifted = tex2D(_MainTex, float2(IN.texcoord.x + rShiftRandom, IN.texcoord.y + rShiftRandom));
    136.                 fixed4 gShifted = tex2D(_MainTex, float2(IN.texcoord.x + gShiftRandom, IN.texcoord.y + gShiftRandom));
    137.                 fixed4 bShifted = tex2D(_MainTex, float2(IN.texcoord.x + bShiftRandom, IN.texcoord.y + bShiftRandom));
    138.              
    139.                 fixed4 c = fixed4(0.0,0.0,0.0,0.0);
    140.  
    141.                 //If the randomly rolled value is below the probability boundry and the color effect is turned on, apply the color glitch effect
    142.                 //Sets the output color to the shifted r,g,b channels and averages their alpha
    143.                 if(colorGlitchRandom < _ColorProbability && _ColorGlitchOn== 1){
    144.                     c.r = rShifted.r;
    145.                     c.g = gShifted.g;
    146.                     c.b = bShifted.b;
    147.                     c.a = (rShifted.a + gShifted.a + bShifted.a) / 3;
    148.                 }
    149.                 else{
    150.                     c = normalC;
    151.                 }
    152.                 //Apply tint and tint color alpha
    153.                 c.rgb *= IN.color;
    154.                 c.a *= IN.color.a;
    155.                 c.rgb *= c.a;
    156.                 return c;
    157.             }
    158.         ENDCG
    159.         }
    160.     }
    161.     SubShader
    162.     {
    163.         Tags
    164.         {
    165.             "Queue"="Transparent"
    166.             "IgnoreProjector"="True"
    167.             "RenderType"="Transparent"
    168.             "PreviewType"="Plane"
    169.             "CanUseSpriteAtlas"="True"
    170.         }
    171.  
    172.         Cull Off
    173.         Lighting Off
    174.         ZWrite Off
    175.         Fog { Mode Off }
    176.         Blend One OneMinusSrcAlpha
    177.  
    178.         Pass
    179.         {
    180.         CGPROGRAM
    181.             #pragma vertex vert
    182.             #pragma fragment frag
    183.             #pragma multi_compile DUMMY PIXELSNAP_ON
    184.             #include "UnityCG.cginc"
    185.          
    186.             struct appdata_t
    187.             {
    188.                 float4 vertex   : POSITION;
    189.                 float4 color    : COLOR;
    190.                 float2 texcoord : TEXCOORD0;
    191.             };
    192.  
    193.             struct v2f
    194.             {
    195.                 float4 vertex   : SV_POSITION;
    196.                 fixed4 color    : COLOR;
    197.                 half2 texcoord  : TEXCOORD0;
    198.             };
    199.          
    200.             fixed4 _Color;
    201.          
    202.             v2f vert(appdata_t IN)
    203.             {
    204.                 v2f OUT;
    205.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    206.                 OUT.texcoord = IN.texcoord;
    207.              
    208.                 OUT.color = IN.color * _Color;
    209.                 #ifdef PIXELSNAP_ON
    210.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    211.                 #endif
    212.                 return OUT;
    213.             }
    214.  
    215.             sampler2D _MainTex;
    216.          
    217.             float rand(float x, float y){
    218.                 return frac(sin(x*12.9898 + y*78.233)*43758.5453);
    219.             }
    220.          
    221.             float _DispIntensity;
    222.             float _DispProbability;
    223.             float _GlitchInterval;
    224.             float _DispGlitchOn;
    225.             float _WrapDispCoords;
    226.  
    227.             fixed4 frag(v2f IN) : SV_Target
    228.             {
    229.                 float intervalTime = floor(_Time.y / _GlitchInterval) * _GlitchInterval;
    230.                 float timePositionVal = float(intervalTime + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]);
    231.                 float timeRandom = rand(timePositionVal, -timePositionVal);
    232.                 if(timeRandom < _DispProbability && _DispGlitchOn == 1){
    233.                     IN.texcoord.x += (rand(floor(IN.texcoord.y / 0.2) - intervalTime, floor(IN.texcoord.y / 0.2) + intervalTime) - 0.5) * _DispIntensity;
    234.                     if(_WrapDispCoords == 1){
    235.                         IN.texcoord.x = fmod(IN.texcoord.x, 1);
    236.                     }
    237.                     else{
    238.                         IN.texcoord.x = clamp(IN.texcoord.x, 0, 1);
    239.                     }
    240.                 }
    241.                 fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
    242.              
    243.                 c.a *= IN.color.a;
    244.                 c.rgb *= c.a;
    245.                 return c;
    246.             }
    247.         ENDCG
    248.         }
    249.     }
    250. }
     
    Last edited: Feb 25, 2021