Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

8 Textures + 1 Color Key Mask in 1 Pass (Conceptual Shader)

Discussion in 'Shaders' started by apple_motion, Nov 2, 2009.

  1. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    After reading one of the post in this forum, suddenly just want to make something similar to test my idea. It's work :)

    But, the question is how many textures allowed in shader model 3.0? Is it safe to use more than 4 textures always??



    Code (csharp):
    1. Shader "Multi_Texture_ColorKey"
    2. {
    3.     Properties
    4.     {              
    5.         key ("key   " , 2D) = "" {}
    6.         tx0 ("yellow" , 2D) = "" {}
    7.         tx1 ("red   " , 2D) = "" {}
    8.         tx2 ("green"  , 2D) = "" {}
    9.         tx3 ("blue  ", 2D) = "" {}
    10.         tx4 ("magenta", 2D) = "" {}
    11.         tx5 ("cyan"   , 2D) = "" {}
    12.         tx6 ("white"  , 2D) = "" {}
    13.         tx7 ("black"  , 2D) = "" {}
    14.     }  
    15.     SubShader
    16.     {
    17.         Pass
    18.         {          
    19.            
    20. CGPROGRAM //--------------
    21. #pragma target 3.0 
    22. #pragma vertex   vertex_shader
    23. #pragma fragment fragment_shader
    24.  
    25. struct src
    26. {
    27.     float4 vertex   : POSITION;
    28.     float4 color    : COLOR;
    29.     float2 texcoord : TEXCOORD;
    30. };
    31.                
    32. void vertex_shader( src src1,
    33.                     out float4 position : POSITION,
    34.                     out float2 texcoord : TEXCOORD)
    35. {                                                  
    36.     position = mul(glstate.matrix.mvp, src1.vertex);
    37.     texcoord = src1.texcoord;
    38. }
    39.  
    40. void fragment_shader( float2 t:TEXCOORD,
    41.                       uniform sampler2D key,
    42.                       uniform sampler2D tx0,
    43.                       uniform sampler2D tx1,
    44.                       uniform sampler2D tx2,
    45.                       uniform sampler2D tx3,
    46.                       uniform sampler2D tx4,
    47.                       uniform sampler2D tx5,
    48.                       uniform sampler2D tx6,
    49.                       uniform sampler2D tx7,
    50.                       out float4 c : COLOR )
    51. {
    52.    
    53.     float4 color = tex2D(key,t);
    54.                    
    55.     float r = (color.r >= 0.5)? 1:0;
    56.     float g = (color.g >= 0.5)? 1:0;
    57.     float b = (color.b >= 0.5)? 1:0;
    58.                                                            
    59.     c  = float4(0,0,0,0);
    60.     c += tex2D(tx0,t)*( r   g  !b); // yellow
    61.     c += tex2D(tx1,t)*( r  !g  !b); // red
    62.     c += tex2D(tx2,t)*(!r   g  !b); // green
    63.     c += tex2D(tx3,t)*(!r  !g   b); // blue
    64.     c += tex2D(tx4,t)*( r  !g   b); // magenta
    65.     c += tex2D(tx5,t)*(!r   g   b); // cyan
    66.     c += tex2D(tx6,t)*( r   g   b); // white
    67.     c += tex2D(tx7,t)*(!r  !g  !b); // black
    68. }
    69.  
    70. ENDCG //--------------
    71.         } // Pass
    72.     } // SubShader
    73. } // Shader
    (Sorry that, no lights and bump mapping at the moment :p)
     
  2. dock

    dock

    Joined:
    Jan 2, 2008
    Posts:
    603
    Wow, that's really interesting! Thanks for sharing, I can imagine that being useful for some things.
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Just to make sure I'm not missing anything: all this shader does is use those 3-bit colours as a mixing mask for the other textures, right?
     
  4. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    especially make the mask animated :)

    YES !

    Originally, I used this method with vertex color change as key mask on the animated map textures.

    (use red and green channel to mask out 4 textures)

    Code (csharp):
    1. void fragment_shader( float4 color:COLOR,
    2.                       float2 t:TEXCOORD,
    3.                       uniform sampler2D _MainTex,
    4.                       uniform sampler2D tx0,
    5.                       uniform sampler2D tx1,
    6.                       uniform sampler2D tx2,
    7.                       uniform sampler2D tx3,                                 
    8.                       uniform float fade,
    9.                       out float4 c : COLOR )
    10. {
    11.     float2 t0, t1, t2, t3, t4;                 
    12.     t0 = t1 = t2 = t3 = t4 = t;                
    13.     t1.x--;
    14.     t2.y--;
    15.     t3.x--;     t3.y--;                
    16.     t4.x/=2;    t4.y/=2;
    17.                    
    18.     float r = (color.r >= 0.5)? 1:0;
    19.     float g = (color.g >= 0.5)? 1:0;
    20.    
    21.     c = tex2D(_MainTex,t4);            
    22.     matte(c, c, tex2D(tx0,t0)*( r   g), fade);
    23.     matte(c, c, tex2D(tx1,t1)*( r  !g), fade);
    24.     matte(c, c, tex2D(tx2,t2)*(!r   g), fade);
    25.     matte(c, c, tex2D(tx3,t3)*(!r  !g), fade);                                 
    26.     c.w = 1-(c.b - c.g);
    27. }
    28.  
    Code (csharp):
    1. void matte(out float4 dest, in float4 B, in float4 A, in float m)
    2. {
    3.     dest = A*A.w*m + B* (1-A.w*m);
    4. }
    5.