alex_mahone
New Member
Is there something wrong with my code?
main.cc:
https://github.com/shithappens2016/temp/blob/master/main.cc
help.h:
https://github.com/shithappens2016/temp/blob/master/help.h
chroma_key_filter.effect(1.fx):
main.cc:
https://github.com/shithappens2016/temp/blob/master/main.cc
help.h:
https://github.com/shithappens2016/temp/blob/master/help.h
chroma_key_filter.effect(1.fx):
Code:
uniform float4x4 ViewProj;
uniform texture image;
uniform float4x4 yuv_mat = { 0.182586, 0.614231, 0.062007, 0.062745,
-0.100644, -0.338572, 0.439216, 0.501961,
0.439216, -0.398942, -0.040274, 0.501961,
0.000000, 0.000000, 0.000000, 1.000000};
uniform float4 color;
uniform float contrast;
uniform float brightness;
uniform float gamma;
uniform float2 chroma_key;
uniform float2 pixel_size;
uniform float similarity;
uniform float smoothness;
uniform float spill;
sampler textureSampler =
sampler_state
{
Texture = <image>;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
struct VertData {
float4 pos : POSITION;
float2 uv : TEXCOORD;
};
VertData VSDefault(VertData v_in)
{
VertData vert_out;
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
vert_out.uv = v_in.uv;
return vert_out;
}
float4 CalcColor(float4 rgba)
{
return float4(pow(rgba.rgb, float3(gamma, gamma, gamma)) * contrast + brightness, rgba.a);
}
float GetChromaDist(float3 rgb)
{
float4 yuvx = mul(float4(rgb.rgb, 1.0), yuv_mat);
return distance(chroma_key, yuvx.yz);
}
float4 SampleTexture(float2 uv)
{
return tex2D(textureSampler, uv);
}
float GetBoxFilteredChromaDist(float3 rgb, float2 texCoord)
{
float distVal = GetChromaDist(rgb);
distVal += GetChromaDist(SampleTexture(texCoord-pixel_size).rgb);
distVal += GetChromaDist(SampleTexture(texCoord-float2(pixel_size.x, 0.0)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord-float2(pixel_size.x, -pixel_size.y)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord-float2(0.0, pixel_size.y)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord+float2(0.0, pixel_size.y)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord+float2(pixel_size.x, -pixel_size.y)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord+float2(pixel_size.x, 0.0)).rgb);
distVal += GetChromaDist(SampleTexture(texCoord+pixel_size).rgb);
return distVal / 9.0;
}
float4 ProcessChromaKey(float4 rgba, VertData v_in)
{
float chromaDist = GetBoxFilteredChromaDist(rgba.rgb, v_in.uv);
float baseMask = chromaDist - similarity;
float fullMask = pow(saturate(baseMask / smoothness), 1.5);
float spillVal = pow(saturate(baseMask / spill), 1.5);
rgba.a *= fullMask;
float desat = (rgba.r * 0.2126 + rgba.g * 0.7152 + rgba.b * 0.0722);
rgba.rgb = saturate(float3(desat, desat, desat)) * (1.0 - spillVal) + rgba.rgb * spillVal;
return CalcColor(rgba);
}
float4 PSChromaKeyRGBA(VertData v_in) : SV_TARGET
{
float4 rgba = tex2D(textureSampler, v_in.uv) * color;
return ProcessChromaKey(rgba, v_in);
//return float4( 1.0f, 0.0f, 0.0f, 0.5f );
//return rgba;
}
technique Draw
{
pass p0
{
SetVertexShader(CompileShader(vs_3_0,VSDefault()));
SetPixelShader(CompileShader(ps_3_0,PSChromaKeyRGBA()));
}
}