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. Dismiss Notice

Resolved Replace color with selected

Discussion in 'Shader Graph' started by demozbox, Jul 25, 2021.

  1. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Hello. I have texture that presents outline for the other texture.
    It has blue color and I need shader that allows change color with any selected.
    The shader I wrote just add (multiply) selected color to blue (i need replace).
    Also it doesnt follow alfa.
    Help me to write the shader/
    Code (CSharp):
    1. Shader "Custom/Recolor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue"="Transparent" "RenderType"="Transparent"
    13.         }
    14.         CGPROGRAM
    15.         #pragma surface surf Lambert// alpha
    16.  
    17.         struct Input
    18.         {
    19.             half2 uv_MainTex;
    20.         };
    21.  
    22.         sampler2D _MainTex;
    23.  
    24.         float4 _Color;
    25.         // float4 _White = half4(1, 1, 1, 1);
    26.  
    27.         void surf(Input IN, inout SurfaceOutput o)
    28.         {
    29.             fixed3 clr = tex2D(_MainTex, IN.uv_MainTex) * _Color;          
    30.             o.Albedo = clr;
    31.         }
    32.         ENDCG
    33.     }
    34.     FallBack "Diffuse"
    35. }
     

    Attached Files:

  2. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Some success!
    With this shader I am able to set any color I want, but got also black color. I believe that is some transparent pixels become black. How to get rid of it?

    Code (CSharp):
    1. Shader "Custom/Recolor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {    
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert //alpha
    12.  
    13.         struct Input
    14.         {
    15.             half2 uv_MainTex;
    16.         };
    17.  
    18.         sampler2D _MainTex;
    19.         float4 _Color;
    20.  
    21.         void surf(Input IN, inout SurfaceOutput o)
    22.         {
    23.             float4 mask = tex2D(_MainTex, IN.uv_MainTex);
    24.             float4 clr = _Color * mask.a;
    25.             o.Albedo = clr;
    26.         }
    27.         ENDCG
    28.     }
    29.     FallBack "Diffuse"
    30. }
     

    Attached Files:

  3. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Made some changes.
    Now I see the some gray color added and affect texture by surface.
    You can see on the attached picture left red stroke with gray outline. I need it to be clear as right blue one(original).
    Code (CSharp):
    1. Shader "Custom/Recolor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert alpha
    12.  
    13.         struct Input
    14.         {
    15.             half2 uv_MainTex;
    16.         };
    17.  
    18.         sampler2D _MainTex;
    19.         fixed4 _Color;
    20.  
    21.         void surf(Input IN, inout SurfaceOutput o)
    22.         {
    23.             fixed4 mask = tex2D(_MainTex, IN.uv_MainTex);
    24.             fixed4 clr = _Color * mask.a;        
    25.  
    26.             o.Albedo = clr;
    27.             o.Alpha = clr.a;
    28.         }
    29.         ENDCG
    30.     }
    31.     FallBack "Diffuse"
    32. }
     

    Attached Files:

  4. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    I removed gray "Outline" around texture.
    But if I select white color (and want to see my texture pure white) I see gray anyway.
    Code (CSharp):
    1. Shader "Custom/Recolor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert alpha
    12.  
    13.         struct Input
    14.         {
    15.             half2 uv_MainTex;
    16.         };
    17.  
    18.         sampler2D _MainTex;
    19.         fixed4 _Color;
    20.  
    21.         void surf(Input IN, inout SurfaceOutput o)
    22.         {
    23.             fixed4 mask = tex2D(_MainTex, IN.uv_MainTex);
    24.             fixed4 clr = _Color;        
    25.  
    26.             o.Albedo = clr;
    27.             o.Alpha = mask.a;
    28.         }
    29.         ENDCG
    30.     }
    31.     FallBack "Diffuse"
    32. }
    33.  
     

    Attached Files:

    Ruchir likes this.
  5. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Help me to set color to the texture.
    Now it is affected by gray color.
     
    Last edited: Jul 26, 2021
  6. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    So, I added MinusColor and devide selected color by MinusColor.
    Then I select gray color for MinusColor.
    This kind of fix the issue.
    Code (CSharp):
    1. Shader "Custom/StrokeRecolor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         _MinusColor ("Minus Color", Color)=(1,1,1,1)
    8.     }
    9.     SubShader
    10.     {
    11.         CGPROGRAM
    12.         #pragma surface surf Lambert alpha
    13.  
    14.         struct Input
    15.         {
    16.             half2 uv_MainTex;
    17.         };
    18.  
    19.         sampler2D _MainTex;
    20.         fixed4 _Color;
    21.         fixed4 _MinusColor;
    22.  
    23.         void surf(Input IN, inout SurfaceOutput o)
    24.         {            
    25.             fixed4 clr = _Color;
    26.             clr /= _MinusColor;
    27.             o.Albedo = clr;
    28.             o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a;
    29.         }
    30.         ENDCG
    31.     }
    32.     FallBack "Diffuse"
    33. }
     
  7. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    well, the line
    #pragma surface surf Lambert alpha
    adds lighting to the shader. I dont actually need it and I also need it to be more like 2d texture shader.
    What do I need to change?
     
  8. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    today I found out that all the time I was trying to get help for HLSL shader in ShaderGraph subforum.
    My bed.
    That is why I've got so many views but no one help.
    By the way.
    I took original unity sprite shader and make couple very simple changes. Now it works as I need
    Code (CSharp):
    1. Shader "Custom/StrokeRecolor"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend One OneMinusSrcAlpha
    25.  
    26.         Pass
    27.         {
    28.             CGPROGRAM
    29.             #pragma vertex vert
    30.             #pragma fragment frag
    31.             #pragma multi_compile _ PIXELSNAP_ON
    32.             #include "UnityCG.cginc"
    33.  
    34.             struct appdata_t
    35.             {
    36.                 float4 vertex : POSITION;
    37.                 float4 color : COLOR;
    38.                 float2 texcoord : TEXCOORD0;
    39.             };
    40.  
    41.             struct v2f
    42.             {
    43.                 float4 vertex : SV_POSITION;
    44.                 fixed4 color : COLOR;
    45.                 float2 texcoord : TEXCOORD0;
    46.             };
    47.  
    48.             fixed4 _Color;
    49.  
    50.             v2f vert(appdata_t IN)
    51.             {
    52.                 v2f OUT;
    53.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    54.                 OUT.texcoord = IN.texcoord;
    55.                 // OUT.color = IN.color * _Color;
    56.                 OUT.color = _Color;
    57.                 #ifdef PIXELSNAP_ON
    58.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    59.                 #endif
    60.  
    61.                 return OUT;
    62.             }
    63.  
    64.             sampler2D _MainTex;
    65.             sampler2D _AlphaTex;
    66.             float _AlphaSplitEnabled;
    67.  
    68.             fixed4 SampleSpriteTexture(float2 uv)
    69.             {
    70.                 fixed4 color = tex2D(_MainTex, uv);
    71.                 color.rgb = _Color.rgb;
    72.                 #if UNITY_TE//XTURE_ALPHASPLIT_ALLOWED
    73.                 if (_AlphaSplitEnabled)
    74.                     color.a = tex2D (_AlphaTex, uv).r;
    75.                 #endif //UNI//TY_TEXTURE_ALPHASPLIT_ALLOWED
    76.  
    77.                 return color;
    78.             }
    79.  
    80.             fixed4 frag(v2f IN) : SV_Target
    81.             {
    82.                 fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
    83.                 c.rgb *= c.a;
    84.                 return c;
    85.             }
    86.             ENDCG
    87.         }
    88.     }
    89. }
    .