Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Color Ramp Replacement Maintaining Alpha

Discussion in 'Shaders' started by Fanttum, Jan 11, 2016.

  1. Fanttum

    Fanttum

    Joined:
    May 18, 2014
    Posts:
    39
    So I'm new to shaders, and not quite sure what I am doing.

    I'm trying to take a sprite in grayscale, and then with a color ramp, pallet swap those colors for new colors.

    I've taken a few solutions from around the forms and made them into this.

    Code (CSharp):
    1.  
    2. Shader "ColourReplacement" {
    3.     Properties {
    4.         _MainTex ("Greyscale (R) Alpha (A)", 2D) = "white" {}
    5.         _ColorRamp ("Colour Palette", 2D) = "gray" {}
    6.         _Clear ("NoAlpha", 2D) = "clear" {}
    7.     }
    8.     SubShader {
    9.                     Tags
    10.         {
    11.             "Queue"="Transparent"
    12.             "IgnoreProjector"="True"
    13.             "RenderType"="Transparent"
    14.             "PreviewType"="Plane"
    15.             "CanUseSpriteAtlas"="False"  
    16.         }
    17.         Pass {
    18.             Name "ColorReplacement"
    19.          
    20.  
    21.             CGPROGRAM
    22.                 #pragma vertex vert
    23.                 #pragma fragment frag
    24.                 #pragma fragmentoption ARB_precision_hint_fastest
    25.                 #include "UnityCG.cginc"
    26.              
    27.                 struct v2f
    28.                 {
    29.                     float4  pos : SV_POSITION;
    30.                     float2  uv : TEXCOORD0;
    31.                 };
    32.                 v2f vert (appdata_tan v)
    33.                 {
    34.                     v2f o;
    35.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    36.                     o.uv = v.texcoord.xy;
    37.                     return o;
    38.                 }
    39.              
    40.                 sampler2D _MainTex;
    41.                 sampler2D _ColorRamp;
    42.                 sampler2D _Clear;
    43.                 float4 frag(v2f i) : COLOR
    44.                 {
    45.                 // SURFACE COLOUR
    46.                     float greyscale = tex2D(_MainTex, i.uv).r;
    47.              
    48.         // RESULT
    49.                     float4 result;
    50.                     result.a = tex2D(_MainTex, i.uv).a;
    51.                     if (result.a < 0.5)
    52.                          result.rgb = tex2D(_Clear, i.uv).rgb;
    53.                    else
    54.                          result.rgb = tex2D(_ColorRamp, float2(greyscale, 0.5)).rgb;
    55.                     return result;
    56.                 }
    57.             ENDCG
    58.         }
    59.     }
    60.     FallBack "Diffuse"
    61. }
    62.  
    Now in all honesty I'm not quite sure what is going on here, but it is mostly working, but when it should be replacing areas with small alpha with a transparent texture, it is making it black. Basically not having the alpha be transparent.

    I know there are some cheep solutions on the asset store, but just looking to learn what's going on here.
    Thanks for any help.
     
  2. flumbatron

    flumbatron

    Joined:
    May 6, 2015
    Posts:
    5
    Hey! In order for a shader to render things as transparent, you need to tell it which blend mode to use. For a basic transparent shader, you'll want to put a row with "Blend SrcAlpha OneMinusSrcAlpha" somewhere between "Pass { " and "CGPROGRAM". More on blend modes here http://docs.unity3d.com/Manual/SL-Blend.html. It's also usually a good idea to also tell the shader not to write to the depth buffer when it's transparent, that would be a row with "ZWrite Off" in the same area.
     
  3. Fanttum

    Fanttum

    Joined:
    May 18, 2014
    Posts:
    39
    Nice, perfect. Working how I want right now. I thought it was something around there, but shaders don't make sense to me yet.
    Thanks for the help.