ChromaKey_RGB.pShader
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/********************************************************************************
Copyright (C) 2012 Hugh Bailey <obs.jim@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
uniform Texture2D diffuseTexture;
uniform float4 outputColor;
uniform float4 keyBaseColor;
uniform float4 chromaKey;
uniform float2 pixelSize;
uniform float keySimilarity;
uniform float keyBlend;
uniform float keySpill;
uniform float gamma;
SamplerState textureSampler
{
AddressU = Clamp;
AddressV = Clamp;
Filter = Linear;
};
struct VertData
{
float4 pos : SV_Position;
float2 texCoord : TexCoord0;
};
float getSampleDist(float3 chromaKey, float3 rgb)
{
const float4x4 yuvMat = {0.182586, -0.100644, 0.439216, 0.0,
0.614231, -0.338572, -0.398942, 0.0,
0.062007, 0.439216, -0.040274, 0.0,
0.062745, 0.501961, 0.501961, 1.0};
//a nice quick colorspace conversion
float4 yuvx = mul(float4(rgb.rgb, 1.0), yuvMat);
return distance(chromaKey.yz, yuvx.yz*2.0);
}
float4 main(VertData input) : SV_Target
{
float4 rgbx = diffuseTexture.Sample(textureSampler, input.texCoord);
//-----------------------------
float distVal = getSampleDist(chromaKey, rgbx.rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord-pixelSize).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord-float2(pixelSize.x, 0.0)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord-float2(pixelSize.x, -pixelSize.y)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord-float2(0.0, pixelSize.y)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord+float2(0.0, pixelSize.y)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord+float2(pixelSize.x, -pixelSize.y)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord+float2(pixelSize.x, 0.0)).rgb);
distVal += getSampleDist(chromaKey, diffuseTexture.Sample(textureSampler, input.texCoord+pixelSize).rgb);
distVal /= 9.0;
//-----------------------------
float baseMask = distVal - keySimilarity;
float fullMask = pow(saturate(baseMask/keyBlend), 1.5);
float spillVal = pow(saturate(baseMask/keySpill), 1.5);
//-----------------------------
rgbx.a = fullMask;
float3 spillDesaturation = rgbx.rgb-keyBaseColor.rgb;
spillDesaturation.rgb += (keyBaseColor.r+keyBaseColor.g+keyBaseColor.b)*0.333333333;
rgbx.rgb = saturate(spillDesaturation)*(1.0-spillVal) + rgbx.rgb*spillVal;
rgbx.rgb = pow(rgbx.rgb, gamma);
return rgbx * outputColor;
}