Search Unity

Decal shader with RGB for decal texture?

Discussion in 'Shaders' started by ZolnierGames, Aug 24, 2019.

  1. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    Hey all, I'm trying to figure out how to modify the decal shader to allow me to change the RGB color of 1) the main texture and then 2) the decal texture. I need to be able to modify these two colors separately. Any advice? The default decal shader has the ability to modify the RGB for everything, but is there any way to control the RGB of the decal layer separately? I'm very new to shaders! Has anyone created anything like this or have any guidance? Thanks, all!
     
  2. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    Or happy to hear other suggestions if the decal shader is not the way to go. For context, I'm trying to figure out the best way to have say a shirt and then have a stripe layer on top of that gives me the flexibility to programmatically alter the color of each item separately. The model I'm using isn't giving me much flexibility so I assume I have to solve this via shaders or texture manipulation. Any ideas? Thanks, friends!
     
  3. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    Yes. In the shader code, there will be a texture read, something like this
    Code (CSharp):
    1. fixed4 tex= tex2D(_MainTex, i.uv);
    After that you can modify the colour by adding or multiplying with a colour property defined earlier in the shader. Something like this.

    Code (CSharp):
    1.    Properties
    2.     {
    3.     _Color("Color", Color) = (1,1,1,1)
    4.      }
    5.   fixed4 frag (v2f i) : SV_Target
    6.       {
    7.              fixed4 tex = tex2D(_MainTex, i.uv);
    8.              tex *= _Color;
    9.       }
     
    Rs likes this.
  4. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    Hmm... Having a little trouble modifying that shader with the code you suggested. Any way to post what it would look like?
     
  5. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    can you post the shader here?
     
  6. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    It's the Legacy/Decal shader that I was working with and it's pretty big... Does your code work with that shader's code as a base because it's throwing all sorts of errors when I try to copy it and modify it.
     
  7. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    It should work. All you're doing is multiplying one value by another. It's about the simplest thing you can do in a shader.
    Forget the color property, did you find the texture read? the line that looks something like this...
    Code (CSharp):
    1. fixed4 tex= tex2D(_MainTex, i.uv);
     
  8. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    I don't see that line in the Legacy Decal shader at all...
     
  9. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    I'm really trying to just modify that shader to take an additional RGB color and then modify the decal color only.
     
  10. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    Sorry mate, I can't help then. I guess legacy shaders must be written differently.
     
  11. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    How might I create a new shader then with 1) a base texture, 2) a color value for that, 3) a decal texture and 4) a color value for that? Any guidance?
     
  12. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    Do you want it to receive lighting from the scene? Or is it an unlit shader?
     
  13. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    unlit is fine since it's for mobile
     
  14. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    This sort of thing?

    Code (CSharp):
    1. Shader "Unlit/Decal"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Decal("decal", 2D) = "white" {}
    7.  
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.                 float2 decalUV : TEXCOORD1;
    27.     };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 float4 vertex : SV_POSITION;
    33.                 float2 decalUV : TEXCOORD1;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             sampler2D _Decal;
    38.             float4 _Decal_ST;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.decalUV = TRANSFORM_TEX(v.uv, _Decal);
    45.                 o.uv = v.uv;
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag (v2f i) : SV_Target
    50.             {
    51.                 // sample the texture
    52.                 fixed4 col = tex2D(_MainTex, i.uv);
    53.                 fixed4 dec = tex2D(_Decal, i.decalUV);
    54.                 col = lerp(col, dec, dec.a);
    55.             return col;
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
    61.  
     
  15. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    Make sure the decal texture is set to "clamp" in it's import settings
     
  16. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    I didn't include the colour tinting. You should be able to figure it out from what I said above.
    if you get stuck, let me know
     
  17. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    So, I added the two color variables to the top:
    _Color("Color", Color) = (1,1,1,1)
    _DecalColor("Decal Color", Color) = (1,1,1,1)

    But when I try to modify fixed4 as follows, the whole model just turns pink:

    fixed4 frag(v2f i) : SV_Target
    {
    // sample the texture
    fixed4 col = tex2D(_MainTex, i.uv);
    fixed4 dec = tex2D(_Decal, i.decalUV);
    dec *= _DecalColor;
    col = lerp(col, dec, dec.a);
    return col;
    }

    What did I do wrong? Thanks for your patience - this whole custom shader thing is very new to me!
     
  18. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    oh yeah, sorry I forgot you need to declare properties in the body of cgprogram
    Code (CSharp):
    1. Shader "Unlit/Decal"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _MainColor("Main Color", Color) = (1,1,1,1)
    7.         _Decal("decal", 2D) = "white" {}
    8.         _DecalColor("Decal Color", Color) = (1,1,1,1)
    9.  
    10.     }
    11.         SubShader
    12.     {
    13.         Tags { "RenderType" = "Opaque" }
    14.         LOD 100
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.                 float2 decalUV : TEXCOORD1;
    29.     };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 float4 vertex : SV_POSITION;
    35.                 float2 decalUV : TEXCOORD1;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             sampler2D _Decal;
    40.             float4 _Decal_ST;
    41.             fixed4 _MainColor;
    42.             fixed4 _DecalColor;
    43.  
    44.             v2f vert(appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.decalUV = TRANSFORM_TEX(v.uv, _Decal);
    49.                 o.uv = v.uv;
    50.                 return o;
    51.             }
    52.  
    53.             fixed4 frag(v2f i) : SV_Target
    54.             {
    55.                 // sample the texture
    56.                 fixed4 col = tex2D(_MainTex, i.uv);
    57.                 col *= _MainColor;
    58.                 fixed4 dec = tex2D(_Decal, i.decalUV);
    59.                 dec *= _DecalColor;
    60.                 col = lerp(col, dec, dec.a);
    61.             return col;
    62.             }
    63.             ENDCG
    64.         }
    65.     }
    66. }
    67.  
    68.  
     
  19. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
    I'd watch this video to give you a primer on the basics


    Then the "makin stuff look good in unity" youtube channel to introduce you to some more advanced topics.
     
  20. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
  21. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    That's amazing! Thanks for taking the time to help and instruct! I've learned a lot and this shader will be useful to MANY people! I'm going to play around with it some more but it's so perfect!!!

    Just as one last piece of advice, what's the easy way to programmatically change the shader in my code? I'm going to start playing around with it, but figured you would know much more immediately than I would, as I continue to learn how to manipulate these custom shaders! Cheers!!
     
  22. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    56
  23. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    YES! Awesome! This is so useful and is one of the best "tutorials" I've seen when getting started on shaders. Kudos on the SUPERB assistance! I owe you a cup of coffee (or a beer, if you're of age - haha!)!!
     
    tmcthee likes this.
  24. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    Hey, I've been playing around with this and love it and I am trying to take it even further. If I wanted to add additional decals and RGB color options to this shader, how would I do that. What if I wanted to stack 4 more decals with color options?
     
  25. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    15
    Anyone have any guidance here? If I want to add multiple decals/textures on my shader, how do I implement. I am LOVING the sample above, but need to add an additional decal/texture layer to this shader.