"#extension GL_OES_EGL_image_external : require\n" +
"precision highp float;\n"+
"varying highp vec2 textureCoordinate;\n" +
"varying highp vec2 backgroundCoordinate;\n" +
"uniform samplerExternalOES inputImageTexture;\n" +
"uniform sampler2D inputBackgroundImageTexture;\n" +
"uniform mat4 yuv_mat;\n" +
"uniform vec4 color;\n" +
"uniform float contrast;\n" +
"uniform float brightness;\n" +
"uniform float gamma;\n" +
"uniform vec4 key_rgb;\n" +
"uniform vec2 chroma_key;\n" +
"uniform vec2 pixel_size;\n" +
"uniform float similarity;\n" +
"uniform float smoothness;\n" +
"uniform float spill;\n" +
"vec4 CalcColor(vec4 rgba) {\n" +
" return vec4(pow(rgba.rgb, vec3(gamma, gamma, gamma)) * contrast + brightness, rgba.a);\n" +
"}\n" +
"float GetChromaDist(vec3 rgb) {\n" +
" vec4 yuvx = yuv_mat * vec4(rgb.rgb, 1.0);\n" +
" return distance(chroma_key, yuvx.yz);\n" + //rgb在转成yuvx后,与设置的颜色计算距离,即相似度,越接近距离越小
"}\n" +
"vec4 SampleTexture(vec2 uv) {\n" +
" return texture2D(inputImageTexture, uv);\n" +
"}\n" +
"float GetBoxFilteredChromaDist(vec3 rgb, vec2 texCoord) {\n" +
" float distVal = GetChromaDist(rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord - pixel_size).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord - vec2(pixel_size.x, 0.0)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord - vec2(pixel_size.x, -pixel_size.y)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord - vec2(0.0, pixel_size.y)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord + vec2(0.0, pixel_size.y)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord + vec2(pixel_size.x, -pixel_size.y)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord + vec2(pixel_size.x, 0.0)).rgb);\n" +
" distVal += GetChromaDist(SampleTexture(texCoord + pixel_size).rgb);\n" +
" return distVal / 9.0;\n" + //计算9个点的距离均值
"}\n" +
"vec4 ProcessChromaKey(vec4 rgba, vec2 uv) {\n" +
" float chromaDist = GetBoxFilteredChromaDist(rgba.rgb, uv);\n" +
" float baseMask = chromaDist - similarity;\n" + //similarity相似度 计算差值
" float fullMask = pow(clamp(baseMask / smoothness, 0.0, 1.0), 1.5);\n" + //
" float spillVal = pow(clamp(baseMask / spill, 0.0, 1.0), 1.5);\n" + //
" rgba.a *= fullMask;\n" + ///绿色时alpah是透明,非绿色时为不透明
" float desat = (rgba.r * 0.2126 + rgba.g * 0.7152 + rgba.b * 0.0722);\n" + //BT709 Y= 0.2126R + 0.7154G + 0.072B Cb = 0.5388(B - Y ) Cr = 0.635(R - Y ) 计算灰度图
" rgba.rgb = clamp(vec3(desat, desat, desat), 0.0, 1.0) * (1.0 - spillVal) + rgba.rgb * spillVal;\n" + //抠图
" return CalcColor(rgba);\n" +
"}\n" +
"void main() {\n" +
" vec4 rgba = texture2D(inputImageTexture, textureCoordinate) * color;\n" +
" gl_FragColor = ProcessChromaKey(rgba, textureCoordinate);\n" +
"}";
绿幕抠图shader备忘
最新推荐文章于 2025-03-08 00:30:00 发布