Android OpenGL ES 3.0 camera vibrato filter

1. Picture-in-picture filter

Picture-in-picture: The original texture is sampled into an area in the middle of the screen, and the texture coordinates of the area outside the screen are scaled before sampling.

#version 300 es
precision highp float;
varying vec2 v_texcoord;
uniform  sampler2D s_textureY;
uniform  sampler2D s_textureU;
uniform  sampler2D s_textureV;

const float OFFSET_LEVEL = 0.15;
const float SCALE_LEVEL = 4.0;

vec4 YuvToRgb(vec2 uv) {
    float y, u, v, r, g, b;
    y = texture(s_textureY, uv).r;
    u = texture(s_textureU, uv).r;
    v = texture(s_textureV, uv).r;
    u = u - 0.5;
    v = v - 0.5;
    r = y + 1.403 * v;
    g = y - 0.344 * u - 0.714 * v;
    b = y + 1.770 * u;
    return vec4(r, g, b, 1.0);
}

vec2 scale(vec2 uv, float level)
{
    vec2 center = vec2(0.5, 0.5);
    vec2 newTexCoord = uv.xy;
    newTexCoord -= center;
    newTexCoord = newTexCoord / level;
    newTexCoord += center;
    return newTexCoord;
}
void main()
{

    if(OFFSET_LEVEL < v_texcoord.x && v_texcoord.x < (1.0 - OFFSET_LEVEL)
       && OFFSET_LEVEL < v_texcoord.y && v_texcoord.y < (1.0 - OFFSET_LEVEL))
    {
        //将原图下采样到指定区域中
        vec2 newTexCoord = v_texcoord;
        newTexCoord -= OFFSET_LEVEL;
        newTexCoord = newTexCoord / (1.0 - 2.0 * OFFSET_LEVEL);
        gl_FragColor = YuvToRgb(newTexCoord);
    }
    else
    {
        //原纹理坐标缩放之后再进行采样
        gl_FragColor = YuvToRgb(scale(v_texcoord, SCALE_LEVEL));
    }
}

2. Out of body

The principle of the soul out of body filter: according to the offset, scale transforms the texture coordinates, samples them separately, and then performs weighted mixing according to the mixing coefficient.

#version 300 es
precision highp float;
varying vec2 v_texcoord;
uniform  sampler2D s_textureY;
uniform  sampler2D s_textureU;
uniform  sampler2D s_textureV;
uniform float u_offset;
uniform vec2 texSize;

const float MAX_ALPHA = 0.5;
const float MAX_SCALE = 0.8;

vec4 YuvToRgb(vec2 uv) {
    float y, u, v, r, g, b;
    y = texture(s_textureY, uv).r;
    u = texture(s_textureU, uv).r;
    v = texture(s_textureV, uv).r;
    u = u - 0.5;
    v = v - 0.5;
    r = y + 1.403 * v;
    g = y - 0.344 * u - 0.714 * v;
    b = y + 1.770 * u;
    return vec4(r, g, b, 1.0);
}

//灵魂出窍
void main()
{
    //根据偏移量计算混合系数 alpha
    float alpha = MAX_ALPHA * (1.0 - u_offset);
    //根据偏移量计算混合系数 scale
    float scale = 1.0 + u_offset * MAX_SCALE;

    //缩放操作
    float scale_x = 0.5 + (v_texcoord.x - 0.5) / scale;
    float scale_y = 0.5 + (v_texcoord.y - 0.5) / scale;

    vec2 scaleCoord = vec2(scale_x, scale_y);
    
    vec4 maskColor = YuvToRgb(scaleCoord);

    vec4 originColor = YuvToRgb(v_texcoord);
    //加权混合
    gl_FragColor = originColor * (1.0 - alpha) + maskColor * alpha;
}

3. Color separation shift

The principle of the color separation offset filter: offset based on the original texture coordinates, sample them separately and then synthesize them according to the RGBA channel to form a new color.

#version 300 es
precision highp float;
varying vec2 v_texcoord;
uniform  sampler2D s_textureY;
uniform  sampler2D s_textureU;
uniform  sampler2D s_textureV;
uniform float u_offset;
vec4 YuvToRgb(vec2 uv) {
    float y, u, v, r, g, b;
    y = texture(s_textureY, uv).r;
    u = texture(s_textureU, uv).r;
    v = texture(s_textureV, uv).r;
    u = u - 0.5;
    v = v - 0.5;
    r = y + 1.403 * v;
    g = y - 0.344 * u - 0.714 * v;
    b = y + 1.770 * u;
    return vec4(r, g, b, 1.0);
}

//分色偏移
void main()
{
    vec4 originColor = YuvToRgb(v_texcoord);

    //右下方偏移
    vec4 offsetColor0 = YuvToRgb(vec2(v_texcoord.x + u_offset, v_texcoord.y + u_offset));
    //左上方偏移
    vec4 offsetColor1 = YuvToRgb(vec2(v_texcoord.x - u_offset, v_texcoord.y - u_offset));

    //混合成一个颜色输出
    gl_FragColor = vec4(originColor.r, offsetColor1.g, offsetColor0.b, originColor.a);
}

4. Rotating circle

Rotated circle: All pixels within a certain radius are rotated according to the angle converted from the offset, and pixels outside the radius are rendered normally.

#version 300 es
precision highp float;
varying vec2 v_texcoord;
uniform  sampler2D s_textureY;
uniform  sampler2D s_textureU;
uniform  sampler2D s_textureV;
uniform float u_offset;
uniform vec2 texSize;

const float PI = 3.141592653;

vec4 YuvToRgb(vec2 uv) {
    float y, u, v, r, g, b;
    y = texture(s_textureY, uv).r;
    u = texture(s_textureU, uv).r;
    v = texture(s_textureV, uv).r;
    u = u - 0.5;
    v = v - 0.5;
    r = y + 1.403 * v;
    g = y - 0.344 * u - 0.714 * v;
    b = y + 1.770 * u;
    return vec4(r, g, b, 1.0);
}

//旋转的圆
void main()
{
    //纹理坐标转为图片坐标
    vec2 imgTex = v_texcoord * texSize;
    float r = 0.3 * texSize.x; //设置半径为图片宽度的 0.3 倍
    //取圆心为中心点
    if(distance(imgTex, vec2(texSize.x / 2.0, texSize.y / 2.0)) < r)
    {
        vec2 tranTex = v_texcoord - 0.5;
        vec2 imgTranTex = tranTex * texSize;
        float len = length(imgTranTex);
        float angle = 0.0;

        angle = acos(imgTranTex.x / len);

        if(tranTex.y < 0.0)
        {
            angle *= -1.0;
        }

        angle -= u_offset;

        imgTranTex.x = len * cos(angle);
        imgTranTex.y = len * sin(angle);

        vec2 newTexCoors = imgTranTex / texSize + 0.5;

        gl_FragColor = YuvToRgb(newTexCoors);
    }
    else
    {
        gl_FragColor = YuvToRgb(v_texcoord);
    }
}

Guess you like

Origin blog.csdn.net/u014078003/article/details/128010083