Project to learn about shaders

hello all,
I would like to finally write my own shaders.
Little exercise for me:

I want to associate different textures to kinect users.

my plan is to tell vvvv: if the color = color user 1 then associate this color to texture 1, etc…

first step for me, must be so simple, but i can’t ;)
how to tell vvvv looks up at the texture and replace all x color pixels with white if they match color pin and the others with black if they don’t match.

here’s my code:

//replace pixels to white or black according to texture’s color
float4x4 tW: WORLD;
float4x4 tV: VIEW;
float4x4 tP: PROJECTION;
float4x4 tWVP: WORLDVIEWPROJECTION;

texture Tex <string uiname=“Texture”;>;
sampler Samp = sampler_state //sampler for doing the texture-lookup
{
Texture = (Tex); //apply a texture to the sampler
MipFilter = LINEAR; //sampler states
MinFilter = LINEAR;
MagFilter = LINEAR;
};

float4x4 tTex: TEXTUREMATRIX <string uiname=“Texture Transform”;>;

float4 usercolor : COLOR <string uiname=“user color”;> = { 0.0, 0.0, 0.0, 1.00000 };

struct vs2ps
{
float4 Pos : POSITION;
float4 TexCd : TEXCOORD0;
};

vs2ps VS(
float4 Pos : POSITION,
float4 TexCd : TEXCOORD0)
{
vs2ps Out = (vs2ps)0;

Out.Pos = mul(Pos, tWVP);


Out.TexCd = mul(TexCd, tTex);

return Out;

}

float4 user_color(vs2ps In): COLOR
//must be the heart of my silly process :)
{

float4 col = tex2D(Samp, In.TexCd);

if (col.r == usercolor.r && col.g ==usercolor.g && col.b == usercolor.b)
{
col = (1,1,1,1);
	
}
return col;

}

technique kinect
{
pass P0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 user_color();
}
}

thanks a lot in advance !

kinect-texture.v4p (11.3 kB)
equal1.fx (1.4 kB)

hi

try this formula:

col=all(abs(col.rgb-usercolor.rgb)<epsilon);

.rgb processes r,g,b values “in parallel”

abs(col.rgb-usercolor.rgb) is a difference between col and usercolor (rgb/vector)

abs(A-B)<epsilon is 1 if A is equal to B (within epsilon), and 0 otherwise (rgb/vector)

all(X) returns 1 (scalar) if all components of vector X are non-zero
(in this case, if rgb of both colors match)

yes really cool and thanks for explanations !!
next step is to deal with alpha mask, I’ll patch the result.
merci beaucoup

ok next problem :)
I want to make one texture per user;
the problem is that textures are blended together in each user, although I would like user1= texure1, user 2 = texture 2…and now I have user1= tex1+tex2, user 2 = tex1+tex2).

float4 user_color(vs2ps In): COLOR
{

float4 M1 = tex2D(Samp, In.TexCd);
M1=all(abs(M1.rgb-Ucolor1.rgb)<E1);
float4 col1 = tex2D(Samp1, In.TexCd);
col1.a *= M1;
 float4 M2 = tex2D(Samp, In.TexCd);
M2=all(abs(M2.rgb-Ucolor2.rgb)<E2);
float4 col2 = tex2D(Samp2, In.TexCd);
col2.a *= M2;
float4 col = col1 + col2;
	return col;

}

the whole fx file and patch are attached,
thank you in advance.

reves_mask6.fx (3.8 kB)
entre reve.v4p (17.4 kB)

you mean you want to render to two textures?

@unc
no I would like to put texture 1 into user 1, texture 2 into user 2, texture 3 into user 3… but together in the same image.
in my example you can see that user 1 is composed by texture 1+texture 2, i don’t want that… maybe I 'll do a schema to explain,

i hope i’m clear

thx

here’s simple picture describes what I want.

col1.a *= M1;

col2.a *= M2;

when you do that, you only multiply alpha channel by this “mask” - and later you do col1 + col2 which only adds all channels together, that means r=r1+r2, g=g1+g2, b=b1+b2, a=a1+a2 - and you have rgb of your textures mixed together, which you dont want

you could just multiply all components col1*=M1, instead of only alpha

or perform some kind of blending

result=backgroundcolor;
...
result=lerp(result,col1,col1.a);
...
result=lerp(result,col2,col2.a);
...

and so on, alphablending each next thing on top of everything before it

really cool !!
thx,
I’ll post the patch with the shader soon !
merci bien

here’s the patch, not the best one that’s for sure ! could be improved I guess !
thanks unc !

kinect user texturing.v4p (21.0 kB)
quinette-mask.fx (6.3 kB)