Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Slow when I set 3 or 4 filter. #15

Open
ghost opened this issue Aug 4, 2014 · 2 comments
Open

Filter Slow when I set 3 or 4 filter. #15

ghost opened this issue Aug 4, 2014 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 4, 2014

I custome Gaussian blur like TiltShiftFilter Instagram. If only 1 filter Gaussian Blur it's ok but I using Filter Group add 3 or 4 filter. It's slow and lag

@gkakasevski
Copy link

Hi truongnguyenptit, do you want to share your code for TiltShiftFilter?

@ghost
Copy link
Author

ghost commented Sep 4, 2014

My English very poor.

Hi Mr gkakasevski,

Of course, You can use class TiltShiftFilterGroup for linear TiltShift and GaussianSelectiveBlurFilter for radial TiltShift.

Code:

Class TiltShiftFilterGroup:

public class TiltShiftFilterGroup extends GaussianBlurPositionFilter {

protected static final String UNIFORM_ROTATION_FOCUS = "u_RotationFocus";

private float rotation;
private int rotationHandle;
public TiltShiftFilterGroup(float blurSize, float aspectRatio, PointF excludedCirclePoint, float excludedCircleRadius, float excludedBlurSize, float rotation) {
    super(blurSize, aspectRatio, excludedCirclePoint, excludedCircleRadius, excludedBlurSize);
    this.rotation = rotation;
}
@Override
protected String getFragmentShader() {
    return
             "precision mediump float;\n" 
            +"uniform sampler2D "+UNIFORM_TEXTURE0+";\n" 
            +"uniform sampler2D "+UNIFORM_TEXTUREBASE+1+";\n"
            +"varying vec2 "+VARYING_TEXCOORD+";\n" 
            +"uniform float "+UNIFORM_BLUR_SIZE+";\n"
            +"uniform float "+UNIFORM_ASPECT_RATIO+";\n"
            +"uniform vec2 "+UNIFORM_EXCLUDE_CIRCLE_POINT+";\n"
            +"uniform float "+UNIFORM_EXCLUDE_CIRCLE_RADIUS+";\n"

            +"uniform float "+UNIFORM_ROTATION_FOCUS+";\n"

            +"void main(){\n"
            +"   vec4 sharpImageColor = texture2D("+UNIFORM_TEXTURE0+", "+VARYING_TEXCOORD+");\n"
            +"   vec4 blurredImageColor = texture2D("+UNIFORM_TEXTUREBASE+1+", "+VARYING_TEXCOORD+");\n"


            +"   float distanceFromCenter = abs(("+VARYING_TEXCOORD+".x - "+UNIFORM_EXCLUDE_CIRCLE_POINT+".x) * "+UNIFORM_ASPECT_RATIO+"*cos("+UNIFORM_ROTATION_FOCUS+") + ("+VARYING_TEXCOORD+".y-"+UNIFORM_EXCLUDE_CIRCLE_POINT+".y)*sin("+UNIFORM_ROTATION_FOCUS+"));\n"

            +"   gl_FragColor = mix(sharpImageColor, blurredImageColor, smoothstep("+UNIFORM_EXCLUDE_CIRCLE_RADIUS+" - "+UNIFORM_BLUR_SIZE+", "+UNIFORM_EXCLUDE_CIRCLE_RADIUS+", distanceFromCenter));\n"
            +"}\n";
}

@Override
protected void initShaderHandles() {
    super.initShaderHandles();
    rotationHandle = GLES20.glGetUniformLocation(programHandle, UNIFORM_ROTATION_FOCUS);
} 

@Override
protected void passShaderValues() {
    super.passShaderValues();
    GLES20.glUniform1f(rotationHandle, rotation);
}

Class GaussianSelectiveBlurFilter:

public class GaussianSelectiveBlurFilter extends GaussianBlurPositionFilter {

public GaussianSelectiveBlurFilter(float blurSize, float aspectRatio, PointF excludedCirclePoint, float excludedCircleRadius, float excludedBlurSize) {
    super(blurSize, aspectRatio, excludedCirclePoint, excludedCircleRadius, excludedBlurSize);
}

@Override
protected String getFragmentShader() {
    return
             "precision mediump float;\n" 
            +"uniform sampler2D "+UNIFORM_TEXTURE0+";\n" 
            +"uniform sampler2D "+UNIFORM_TEXTUREBASE+1+";\n"
            +"varying vec2 "+VARYING_TEXCOORD+";\n" 
            +"uniform float "+UNIFORM_BLUR_SIZE+";\n"
            +"uniform float "+UNIFORM_ASPECT_RATIO+";\n"
            +"uniform vec2 "+UNIFORM_EXCLUDE_CIRCLE_POINT+";\n"
            +"uniform float "+UNIFORM_EXCLUDE_CIRCLE_RADIUS+";\n"


            +"void main(){\n"
            +"   vec4 sharpImageColor = texture2D("+UNIFORM_TEXTURE0+", "+VARYING_TEXCOORD+");\n"
            +"   vec4 blurredImageColor = texture2D("+UNIFORM_TEXTUREBASE+1+", "+VARYING_TEXCOORD+");\n"
            +"   vec2 texCoordAfterAspect = vec2("+VARYING_TEXCOORD+".x, "+VARYING_TEXCOORD+".y * "+UNIFORM_ASPECT_RATIO+" + 0.5 - 0.5 * "+UNIFORM_ASPECT_RATIO+");\n"
            +"   float distanceFromCenter = distance("+UNIFORM_EXCLUDE_CIRCLE_POINT+", texCoordAfterAspect);\n"
            +"   gl_FragColor = mix(sharpImageColor, blurredImageColor, smoothstep("+UNIFORM_EXCLUDE_CIRCLE_RADIUS+" - "+UNIFORM_BLUR_SIZE+", "+UNIFORM_EXCLUDE_CIRCLE_RADIUS+", distanceFromCenter));\n"
            +"}\n";
}

}

How to use:
BasicFilter filter = new GaussianSelectiveBlurFilter(4f, 1f, new PointF(x,y), radius, radius);
BasicFilter filter = new TiltShiftFilterGroup(4f, 1f, new PointF(x,y), radius, radius, (float)(Math.PI*2-angle+Math.PI/2));

I hope help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant