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

Custom shader not working

Discussion in 'Shaders' started by HelloJinxie, Dec 5, 2019.

  1. HelloJinxie

    HelloJinxie

    Joined:
    Nov 7, 2019
    Posts:
    20
    Hey! I'm trying to make a shader that swaps each RGBs to different colours (e.g. R to pink, G to blue, B to Purple).
    I don't have that much experience with shaders but all its doing is making everything look plain white.

    Was following this tutorial

    Code (CSharp):
    1. Shader "Custom/ColorTint"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Particle Texture", 2D) = "white" {}
    6.         _TintColorRed("Tint Color Red", Color) = (0.5,0.5,0.5,0.5)
    7.         _TintColorGreen("Tint Color Green", Color) = (0.5,0.5,0.5,0.5)
    8.         _TintColorBlue("Tint Color Blue", Color) = (0.5,0.5,0.5,0.5)
    9.     }
    10.     Category
    11.     {
    12.         Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    13.             Blend SrcAlpha OneMinusSrcAlpha
    14.             AlphaTest Greater .01
    15.             ColorMask RGB
    16.             Cull Off Lighting Off ZWrite Off
    17.             BindChannels
    18.             {
    19.               Bind "Color", color
    20.               Bind "Vertex", vertex
    21.               Bind "TexCoord", texcoord
    22.             }
    23.     }
    24.     SubShader
    25.     {
    26.         Pass
    27.         {
    28.             CGPROGRAM
    29.             #pragma vertex vert
    30.             #pragma fragment frag
    31.             #pragma fragmentation ARB_presision_hunt_fastest
    32.             #pragma multi_compile_particles
    33.  
    34.             #include "UnityCG.cginc"
    35.  
    36.             sampler2D _MainTex;
    37.             fixed4 _TintColorRed;
    38.             fixed4 _TintColorGreen;
    39.             fixed4 _TintColorBlue;
    40.  
    41.             struct appdata_t
    42.             {
    43.                 float4 vertex : POSITION;
    44.                 float2 texcoord : TEXCOORD0;
    45.             };
    46.  
    47.             struct v2f
    48.             {
    49.                 float4 vertex : POSITION;
    50.                 float2 texcoord : TEXCOORD0;
    51.             };
    52.  
    53.             float4 _MainTex_ST;
    54.  
    55.             v2f vert(appdata_t v)
    56.             {
    57.                 v2f o;
    58.                 o.vertex = UnityObjectToClipPos(v.vertex);
    59.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    60.                 return o;
    61.             }
    62.  
    63.             sampler2D _CameraDepthTexture;
    64.             float _InvFade;
    65.  
    66.             fixed4 frag(v2f i) : COLOR
    67.             {
    68.                 float4 baseColor = tex2D(_MainTex, i.texcoord);
    69.                 float alpha = baseColor.a;
    70.                 baseColor = alpha * (baseColor.r * _TintColorRed + baseColor.g * _TintColorGreen + baseColor.b * _TintColorBlue);
    71.                 baseColor.a = 1.0f - step(alpha, 0.1);
    72.                 return baseColor;
    73.             }
    74.             ENDCG
    75.         }
    76.     }
    77. }
     
  2. DHein

    DHein

    Joined:
    Jan 26, 2016
    Posts:
    38
    For me the shader works, are you sure that you set a texture as the Particle Texture in the Inspector?
    The default fallback texture is plain white as you defined in this line:
    Code (CSharp):
    1. _MainTex("Particle Texture", 2D) = "white" {}
     
  3. HelloJinxie

    HelloJinxie

    Joined:
    Nov 7, 2019
    Posts:
    20
    Its not for a particle, its for a character sprite
    When i commented out the _MainTex it does the same thing
     
  4. DHein

    DHein

    Joined:
    Jan 26, 2016
    Posts:
    38
    It's really hard to tell what kind of setup you are using, can you post a a screenshot of your inspector with a material using this shader and showing the behaviour you are describing?

    EDIT: if you are using a sprite renderer you should also make sure to set a sprite, so the texture is forwarded to the shader instead of the fallback white texture
     
    Last edited: Dec 5, 2019
  5. HelloJinxie

    HelloJinxie

    Joined:
    Nov 7, 2019
    Posts:
    20
    I just wanted to mention I fixed one issue but now i have another. The solution was the dropdown menu at the top was set to the wrong thing. But the newest problem is the alpha, its creating squares around the sprite. (Same code)

    upload_2019-12-6_1-2-3.png
     
  6. HelloJinxie

    HelloJinxie

    Joined:
    Nov 7, 2019
    Posts:
    20
    Also heres the viewport
    upload_2019-12-6_1-9-9.png