绿幕抠图shader备忘

"#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" +
            "}";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值