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

After reading one of the post in this forum, suddenly just want to make something similar to test my idea. It’s work :slight_smile:

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

Shader "Multi_Texture_ColorKey"
{
	Properties 
	{				
		key ("key	" , 2D) = "" {} 
		tx0 ("yellow" , 2D) = "" {} 
		tx1 ("red	" , 2D) = "" {}
		tx2 ("green"  , 2D) = "" {} 
		tx3 ("blue	", 2D) = "" {}
		tx4 ("magenta", 2D) = "" {}
		tx5 ("cyan"   , 2D) = "" {}
		tx6 ("white"  , 2D) = "" {}
		tx7 ("black"  , 2D) = "" {}
	}	
	SubShader 
	{
		Pass 
		{			
			
CGPROGRAM //--------------
#pragma target 3.0	
#pragma vertex 	 vertex_shader
#pragma fragment fragment_shader

struct src 
{
	float4 vertex	: POSITION;
	float4 color	: COLOR;
	float2 texcoord : TEXCOORD;
};
				
void vertex_shader( src src1,
					out float4 position : POSITION,
					out float2 texcoord : TEXCOORD)
{													
	position = mul(glstate.matrix.mvp, src1.vertex);
	texcoord = src1.texcoord;
}

void fragment_shader( float2 t:TEXCOORD,
					  uniform sampler2D key,
					  uniform sampler2D tx0,
					  uniform sampler2D tx1,
					  uniform sampler2D tx2,
					  uniform sampler2D tx3,
					  uniform sampler2D tx4,
					  uniform sampler2D tx5,
					  uniform sampler2D tx6,
					  uniform sampler2D tx7,
					  out float4 c : COLOR )
{
	
	float4 color = tex2D(key,t);
					
	float r = (color.r >= 0.5)? 1:0;
	float g = (color.g >= 0.5)? 1:0;
	float b = (color.b >= 0.5)? 1:0;
															
	c  = float4(0,0,0,0);
	c += tex2D(tx0,t)*( r   g  !b);	// yellow
	c += tex2D(tx1,t)*( r  !g  !b);	// red
	c += tex2D(tx2,t)*(!r   g  !b);	// green
	c += tex2D(tx3,t)*(!r  !g   b);	// blue
	c += tex2D(tx4,t)*( r  !g   b);	// magenta
	c += tex2D(tx5,t)*(!r   g   b);	// cyan
	c += tex2D(tx6,t)*( r   g   b);	// white
	c += tex2D(tx7,t)*(!r  !g  !b);	// black
}

ENDCG //--------------
		} // Pass
	} // SubShader
} // Shader

(Sorry that, no lights and bump mapping at the moment :p)

Wow, that’s really interesting! Thanks for sharing, I can imagine that being useful for some things.

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?

especially make the mask animated :slight_smile:

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)

void fragment_shader( float4 color:COLOR,
					  float2 t:TEXCOORD,
					  uniform sampler2D _MainTex,
					  uniform sampler2D tx0,
					  uniform sampler2D tx1,
					  uniform sampler2D tx2,
					  uniform sampler2D tx3,								  
					  uniform float fade,
					  out float4 c : COLOR )
{
	float2 t0, t1, t2, t3, t4;					
	t0 = t1 = t2 = t3 = t4 = t;					
	t1.x--; 
	t2.y--; 
	t3.x--; 	t3.y--;					
	t4.x/=2; 	t4.y/=2;
					
	float r = (color.r >= 0.5)? 1:0;
	float g = (color.g >= 0.5)? 1:0;
	
	c = tex2D(_MainTex,t4);				
	matte(c, c, tex2D(tx0,t0)*( r   g), fade); 
	matte(c, c, tex2D(tx1,t1)*( r  !g), fade);
	matte(c, c, tex2D(tx2,t2)*(!r   g), fade);
	matte(c, c, tex2D(tx3,t3)*(!r  !g), fade);									
	c.w = 1-(c.b - c.g);
}
void matte(out float4 dest, in float4 B, in float4 A, in float m)
{
	dest = A*A.w*m + B* (1-A.w*m);
}