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

2 textures, 2 colors: how to assign a color to each texture?

Discussion in 'Shaders' started by ramonfr, Aug 3, 2014.

  1. ramonfr

    ramonfr

    Joined:
    May 28, 2011
    Posts:
    23
    Hi, I'm trying to apply a different color (_Color2) to the _Tex2 attribute. So far, only the _MainTex receives a color ("_Color"). How can I make the _Tex2 receive the _Color2 attribute as its color?

    The code can be found here: http://pastebin.com/8tCcBihn


    The first layer can be opaque, the second layer must have a texture with alpha channel. I must be able to change the color of each layer. Remember how you can customize your car on Need For Speed? That's what I'm trying to do, but with team shirts. You can select the pattern, which is a .png picture and change its color; plus you can change the color of the first layer, wich doesn't need to be transparent. Sounds simple, but I couldn't find a single shader so far that lets me do that.

    Thanks in advance.
     
  2. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    Hi, ill be sending an advanced version of a colorchanger today to the asset store, for the moment, see the following post.
     
    Last edited: Aug 4, 2014
  3. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    changecolorsss.jpg Try this. the colors should change prior to applying the shadows etc, although with hsv, shadows are part of teh saturation value variable, so it can be done after, but i didnt check:

    there is a slider in the shader so you can change to many colors from one texture.

    Keep in mind its just 5 lines added to your code with rgb, which more often combines orange purple and green together yum, for amazing results use HSV function and more randomness. i will do a new version for assetstore because i already coded it lol.



    Code (csharp):
    1.  
    2. Shader "Transparent/colorchange" {
    3. Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.  
    6.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.         _Tex2 ("B", 2D) = "white" {}
    8.         _Randy("randomize", Range(-1, 1000)) = 0.7
    9. }
    10.  
    11. SubShader {
    12.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.         LOD 200
    14.  
    15. Pass {
    16.                 Material {
    17.                 Diffuse [_Tex2]
    18.                 Diffuse [_MainTex]
    19.          
    20.             }
    21.      
    22.      
    23.             // Apply base texture
    24.             SetTexture [_MainTex] {
    25.                 combine texture
    26.             }
    27.             // Blend in the alpha texture using the lerp operator
    28.             SetTexture [_Tex2] {
    29.                 //constantColor [_Color2]
    30.                 combine texture lerp (texture) previous
    31.             }
    32.         }
    33. CGPROGRAM
    34. #pragma surface surf Lambert alpha
    35.  
    36. sampler2D _MainTex;
    37. sampler2D _Tex2;
    38. fixed4 _Color;
    39. float _Randy;
    40.  
    41. half tri ( float xx ){  //triangle wave -1,1 period 4
    42.     half xd = abs((fmod(abs(xx),4.0)) - 2.0)*.5;
    43.     return xd;
    44. }
    45.  
    46.  
    47. struct Input {
    48.         float2 uv_MainTex;
    49.         float2 uv_Tex2;
    50. };
    51.  
    52. void surf (Input IN, inout SurfaceOutput o) {
    53.  
    54.  
    55.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    56.  
    57.  
    58.         c.r = tri ( c.r*4 +_Randy*1.3532);
    59.         c.g = tri ( c.g*4 +_Randy);
    60.         c.b = tri ( c.b*4 +_Randy*2.432);
    61.         o.Albedo = c.rgb;
    62.         o.Alpha = c.a;
    63. }
    64. ENDCG
    65. }
    66.  
    67. Fallback "Transparent/VertexLit"
    68. }
    69.  
    70.  
     

    Attached Files:

    Last edited: Aug 4, 2014
  4. ramonfr

    ramonfr

    Joined:
    May 28, 2011
    Posts:
    23
    Thanks, but this shader won't let me change the color of the opaque texture. And I wish I could pick the color of the second texture, instead of randomizing it.
     
  5. ramonfr

    ramonfr

    Joined:
    May 28, 2011
    Posts:
    23
    Thanks, but this shader won't let me me change the color of the opaque _Tex2. Also, I should be able to pick colors instead of randomizing them.
     
  6. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
  7. ramonfr

    ramonfr

    Joined:
    May 28, 2011
    Posts:
    23
    Lol, I don't wanna sound rude, but did you really read my first post? I've seen these shaders before, and as you can see they have 2 textures, but not 2 colors. I can't assign a color to each texture; plus it doesn't support alpha, I guess.

    I'm gonna try to explain again:

    You have 1 material with 1 shader. The shader must have two textures: the first one represents the first layer, the second represents the second layer. You must be able to assign a color to each texture, and the upper layer must support .png pictures with alpha channel.

    I managed to do it 2 years ago, now I can't remember how I did it.
     
    Last edited: Aug 5, 2014