在OpenGL中将遮罩应用于视频

时间:2018-09-15 08:10:48

标签: android opengl-es


我是OpenGL的新生。我试图找到解决我问题的方法。我录制了带有Alpha蒙版的视频。视频RGB的顶部和底部是alpha。据我了解,需要更改我的着色器,但是我不知道如何执行此操作。

顶点着色器

let strokeTextAttributes: [NSAttributedStringKey: Any] = [
    NSAttributedStringKey.strokeColor: UIColor.black,
    NSAttributedStringKey.foregroundColor : UIColor.white,
    NSAttributedStringKey.strokeWidth : -3.0,
    NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
]

片段着色器

    private static final String vertexShaderCode =
        "attribute vec4 vPosition;" +
                "attribute vec4 vTexCoordinate;" +
                "uniform mat4 textureTransform;" +
                "varying vec2 v_TexCoordinate;" +
                "void main() {" +
                "   v_TexCoordinate = (textureTransform * vTexCoordinate).xy;" +
                "   gl_Position = vPosition;" +
                "}";

enter image description here

1 个答案:

答案 0 :(得分:1)

要跳过片段,可以在片段着色器中使用discard关键字。

您必须对纹理进行两次查找。上半部分的第一个获取RGB颜色通道,下半部分的第二个获取红色通道的遮罩:

vec2 c_uv  = vec2(texture.x, texture.y*0.5); 
vec4 color = texture2D(c_uv, v_TexCoordinate);

vec2  m_uv = vec2(texture.x, 0.5 + texture.y*0.5); 
float mask = texture2D(m_uv, v_TexCoordinate).r;

片段着色器可能看起来像这样:

#extension GL_OES_EGL_image_external : require
precision mediump float;

uniform samplerExternalOES texture;
varying vec2 v_TexCoordinate;

void main () {

    vec2  m_uv = vec2(v_TexCoordinate.x, 0.5 + v_TexCoordinate.y*0.5); 
    float mask = texture2D(texture, m_uv).r;
    if ( mask < 0.5 )
        discard;

    vec2 c_uv  = vec2(v_TexCoordinate.x, v_TexCoordinate.y*0.5); 
    vec4 color = texture2D(texture, c_uv);  
    gl_FragColor = color;
}