How to Implement GPUImageMaskFilter using GPUImage

后端 未结 2 1322
难免孤独
难免孤独 2021-02-08 15:31

I need to Cut from the Full Image using the mask and created the masked Image.

\"Full +

2条回答
  •  别跟我提以往
    2021-02-08 16:26

    You don't need a mask filter but an alpha mask blend.

    I implemented one here like this:

    // GPUImage shader strings
    NSString * const kNBUAlphaMaskShaderString = SHADER_STRING
    (
     varying highp vec2 textureCoordinate;
     varying highp vec2 textureCoordinate2;
    
     uniform sampler2D inputImageTexture;
     uniform sampler2D inputImageTexture2;
    
     void main()
     {
         lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
         lowp vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);
    
         gl_FragColor = vec4(textureColor.xyz, textureColor2.a);
     }
     );
    

    Simply keeps the colors from one image and the alpha channel of the second one.

    Then create a GPUImageTwoInputFilter:

    GPUImageTwoInputFilter * alphaMask = [[GPUImageTwoInputFilter alloc] initWithFragmentShaderFromString:kNBUAlphaMaskShaderString];
    

    I'm not sure if a similar blend filter has been added to GPUImage since.


    Just checked again to see if there's a built-in blend that does it, and there isn't. But the blend filter I used as inspiration is still there (GPUImageAlphaBlendFilter). It merges two images using the alpha mask to mix them. The filter mentioned above doesn't require a second "empty" image.

提交回复
热议问题