Skip to content

Instantly share code, notes, and snippets.

@smakhtin
Created July 26, 2012 21:08
Show Gist options
  • Save smakhtin/3184521 to your computer and use it in GitHub Desktop.
Save smakhtin/3184521 to your computer and use it in GitHub Desktop.
Simple masking shader for VVVV
//@author: alg (Vadim Smakhtin)
//@help: masking with binary image. Black - alpha, white - visible.
//@tags: mask, alpha
//@credits:
// --------------------------------------------------------------------------------------------------
// PARAMETERS:
// --------------------------------------------------------------------------------------------------
//transforms
float4x4 tW: WORLD;
float4x4 tV: VIEW;
float4x4 tP: PROJECTION;
float4x4 tWVP: WORLDVIEWPROJECTION;
//material properties
float4 cAmb : COLOR <String uiname="Color";> = {1, 1, 1, 1};
float Alpha = 1;
//texture
texture Tex <string uiname="Texture";>;
sampler Samp = sampler_state
{
Texture = (Tex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
texture MaskTex <string uiname="Mask";>;
sampler MaskSamp = sampler_state
{
Texture = (MaskTex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
float4x4 tTex: TEXTUREMATRIX <string uiname="Texture Transform";>;
struct vs2ps
{
float4 Pos : POSITION;
float4 TexCd : TEXCOORD0;
float4 MaskTexCd : TEXCOORD1;
};
// --------------------------------------------------------------------------------------------------
// VERTEXSHADERS
// --------------------------------------------------------------------------------------------------
vs2ps VS(
float4 Pos : POSITION,
float4 TexCd : TEXCOORD0)
{
vs2ps Out = (vs2ps)0;
Out.Pos = mul(Pos, tWVP);
Out.TexCd = mul(TexCd, tTex);
Out.MaskTexCd = TexCd;
return Out;
}
// --------------------------------------------------------------------------------------------------
// PIXELSHADERS:
// --------------------------------------------------------------------------------------------------
float4 PS(vs2ps In): COLOR
{
float4 col = tex2D(Samp, In.TexCd) * cAmb;
float maskCol = tex2D(MaskSamp, In.MaskTexCd);
col.a = maskCol > 0;
return col;
}
// --------------------------------------------------------------------------------------------------
// TECHNIQUES:
// --------------------------------------------------------------------------------------------------
technique TConstant
{
pass P0
{
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment