Search Unity

Add Single-Color Swap Feature to Existing Shader

Discussion in 'Shaders' started by MVR1988, Oct 26, 2020.

  1. MVR1988

    MVR1988

    Joined:
    May 23, 2018
    Posts:
    2
    I know next to nothing about shaders but have been able to get this one to do most of what I need:

    Code (CSharp):
    1. Shader "Custom/SpriteShadowCutout" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    6.     }
    7.  
    8.         SubShader{
    9.             Tags {
    10.             "Queue" = "AlphaTest"
    11.             "IgnoreProjector" = "True"
    12.             "RenderType" = "TransparentCutout"
    13.         }
    14.             Cull off
    15.  
    16.             CGPROGRAM
    17.  
    18.             #pragma surface surf Lambert vertex:vert alphatest:_Cutoff addshadow
    19.  
    20.             sampler2D _MainTex;
    21.             fixed4 _Color;
    22.  
    23.             void vert(inout appdata_full v) {
    24.                 if (dot(normalize(ObjSpaceViewDir(v.vertex)), v.normal) < 0)
    25.                 {
    26.                     v.normal = -v.normal;
    27.                 }
    28.             }
    29.  
    30.             struct Input {
    31.                 float2 uv_MainTex;
    32.             };
    33.  
    34.             void surf(Input IN, inout SurfaceOutput o) {
    35.                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    36.                 o.Albedo = c.rgb;
    37.                 o.Alpha = c.a;
    38.             }
    39.             ENDCG
    40.         }
    41.  
    42.             Fallback "Transparent/Cutout/VertexLit"
    43. }
    I need to add a "color swap" that will take anything grayscale and swap it with a custom color. I've found working "color swap" shaders but cannot seem to cobble the two together (shaders confuse the hell out of me).

    I am working on a character customization feature whereby most of the assets are pre-drawn and colored, but the skin tone is variable (represented by grayscale).
    Example.png
    I would appreciate anyone who could help me take this shader and add this feature to it. I am shader illiterate, so my apologies in advance if this is silly request.
     

    Attached Files:

    Last edited: Oct 26, 2020
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Only way I can think of is have a specific color for sections as like a normal map. Then use the 'normal' graphics color positions to find what parts of the body you want to change color. So for instance you make the hair white in the unrendered graphic then when sampled white you can use a conditional to change the color on your rendered graphic.

    Something like this.

    Code (CSharp):
    1. float3 color = tex2D(_MainTex, IN.uv_MainTex);
    2. float3 conditionalColor  = tex2D(_ConditionalImage, IN.uv_MainTex);
    3. float3 hairColor = float3(255,255,255);
    4. float3 newColor = float3(0,0,0);
    5.  
    6. if(conditionalColor == hairColor)
    7. {
    8.     color = newColor ;
    9. }
     
  3. MVR1988

    MVR1988

    Joined:
    May 23, 2018
    Posts:
    2
    Thank you for your response. I was able to solve this problem using the Colorify Asset and taking bits and pieces of their sprite-based color swap and merging it with mine (literally by trial and error, because shaders are some kind of black magic).