|
22 | 22 | * Selectively replaces a color in the first image with the second image
|
23 | 23 | */
|
24 | 24 | public class GPUImageChromaKeyBlendFilter extends GPUImageTwoInputFilter {
|
25 |
| - public static final String CHROMA_KEY_BLEND_FRAGMENT_SHADER = "varying highp vec2 textureCoordinate;\n" + |
26 |
| - " varying highp vec2 textureCoordinate2;\n" + |
| 25 | + public static final String CHROMA_KEY_BLEND_FRAGMENT_SHADER = " precision highp float;\n" + |
27 | 26 | " \n" +
|
| 27 | + " varying highp vec2 textureCoordinate;\n" + |
| 28 | + " varying highp vec2 textureCoordinate2;\n" + |
| 29 | + "\n" + |
| 30 | + " uniform float thresholdSensitivity;\n" + |
| 31 | + " uniform float smoothing;\n" + |
| 32 | + " uniform vec3 colorToReplace;\n" + |
28 | 33 | " uniform sampler2D inputImageTexture;\n" +
|
29 | 34 | " uniform sampler2D inputImageTexture2;\n" +
|
30 | 35 | " \n" +
|
31 |
| - " highp float lum(lowp vec3 c) {\n" + |
32 |
| - " return dot(c, vec3(0.3, 0.59, 0.11));\n" + |
33 |
| - " }\n" + |
34 |
| - " \n" + |
35 |
| - " lowp vec3 clipcolor(lowp vec3 c) {\n" + |
36 |
| - " highp float l = lum(c);\n" + |
37 |
| - " lowp float n = min(min(c.r, c.g), c.b);\n" + |
38 |
| - " lowp float x = max(max(c.r, c.g), c.b);\n" + |
39 |
| - " \n" + |
40 |
| - " if (n < 0.0) {\n" + |
41 |
| - " c.r = l + ((c.r - l) * l) / (l - n);\n" + |
42 |
| - " c.g = l + ((c.g - l) * l) / (l - n);\n" + |
43 |
| - " c.b = l + ((c.b - l) * l) / (l - n);\n" + |
44 |
| - " }\n" + |
45 |
| - " if (x > 1.0) {\n" + |
46 |
| - " c.r = l + ((c.r - l) * (1.0 - l)) / (x - l);\n" + |
47 |
| - " c.g = l + ((c.g - l) * (1.0 - l)) / (x - l);\n" + |
48 |
| - " c.b = l + ((c.b - l) * (1.0 - l)) / (x - l);\n" + |
49 |
| - " }\n" + |
50 |
| - " \n" + |
51 |
| - " return c;\n" + |
52 |
| - " }\n" + |
53 |
| - "\n" + |
54 |
| - " lowp vec3 setlum(lowp vec3 c, highp float l) {\n" + |
55 |
| - " highp float d = l - lum(c);\n" + |
56 |
| - " c = c + vec3(d);\n" + |
57 |
| - " return clipcolor(c);\n" + |
58 |
| - " }\n" + |
59 |
| - " \n" + |
60 | 36 | " void main()\n" +
|
61 | 37 | " {\n" +
|
62 |
| - " highp vec4 baseColor = texture2D(inputImageTexture, textureCoordinate);\n" + |
63 |
| - " highp vec4 overlayColor = texture2D(inputImageTexture2, textureCoordinate2);\n" + |
64 |
| - "\n" + |
65 |
| - " gl_FragColor = vec4(baseColor.rgb * (1.0 - overlayColor.a) + setlum(overlayColor.rgb, lum(baseColor.rgb)) * overlayColor.a, baseColor.a);\n" + |
| 38 | + " vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n" + |
| 39 | + " vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);\n" + |
| 40 | + " \n" + |
| 41 | + " float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;\n" + |
| 42 | + " float maskCr = 0.7132 * (colorToReplace.r - maskY);\n" + |
| 43 | + " float maskCb = 0.5647 * (colorToReplace.b - maskY);\n" + |
| 44 | + " \n" + |
| 45 | + " float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;\n" + |
| 46 | + " float Cr = 0.7132 * (textureColor.r - Y);\n" + |
| 47 | + " float Cb = 0.5647 * (textureColor.b - Y);\n" + |
| 48 | + " \n" + |
| 49 | + " float blendValue = 1.0 - smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));\n" + |
| 50 | + " gl_FragColor = mix(textureColor, textureColor2, blendValue);\n" + |
66 | 51 | " }";
|
67 | 52 |
|
68 | 53 | private int mThresholdSensitivityLocation;
|
|
0 commit comments