Open ghost opened 10 years ago
Hi truongnguyenptit, do you want to share your code for TiltShiftFilter?
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.
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