Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Sorting bugs with custom UI shader

Discussion in 'Shaders' started by DimitriX89, Nov 11, 2021.

  1. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    I wanted to be able to recolor some of my UI images using color picker and a channel mask. So I created the new Unlit shader with this recolor by mask functionality. But after I applied said shader to UI Image component, it became occluded by background Sprite renderers (it is 2d game). I had to settle with no shaders and using pre-colored pictures. Anything that needs to be added to shader code to avoid it?

    Code (CSharp):
    1. Shader "Unlit/PowerupMeterColored"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("cell grayscale", 2D) = "white" {}
    6.         _CellColor ("cell color", Color) = (0,0,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             sampler2D _MainTex;
    34.             float4 _MainTex_ST;
    35.             fixed4 _CellColor;
    36.  
    37.             v2f vert (appdata v)
    38.             {
    39.                 v2f o;
    40.                 o.vertex = UnityObjectToClipPos(v.vertex);
    41.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    42.                 return o;
    43.             }
    44.  
    45.             fixed4 frag (v2f i) : SV_Target
    46.             {
    47.                 // sample the texture
    48.                 fixed4 col = tex2D(_MainTex, i.uv);
    49.                 return col + (1.0 - col.r)*_CellColor;
    50.             }
    51.             ENDCG
    52.         }
    53.     }
    54. }
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Bump: same situation is when sprite is assigned custom material created from Surface shader