|
| 1 | +package com.chillingvan.canvasgl.textureFilter; |
| 2 | + |
| 3 | +import android.opengl.GLES20; |
| 4 | + |
| 5 | +import com.chillingvan.canvasgl.ICanvasGL; |
| 6 | +import com.chillingvan.canvasgl.OpenGLUtil; |
| 7 | +import com.chillingvan.canvasgl.glcanvas.BasicTexture; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | + |
| 11 | +public class GaussianBlurFilter extends FilterGroup { |
| 12 | + public static class GaussianFilter extends BasicTextureFilter { |
| 13 | + private float blurSize; |
| 14 | + private int dir; |
| 15 | + public static final String TEXEL_WIDTH_OFFSET = "texelWidthOffset"; |
| 16 | + public static final String TEXEL_HEIGHT_OFFSET = "texelHeightOffset"; |
| 17 | + public static final String BLUR_COORDINATES = "blurCoordinates"; |
| 18 | + |
| 19 | + |
| 20 | + public GaussianFilter(int dir, float blurSize) { |
| 21 | + this.dir = dir; |
| 22 | + this.blurSize = blurSize; |
| 23 | + |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + public GaussianFilter(int dir) { |
| 28 | + this(dir, 1f); |
| 29 | + } |
| 30 | + |
| 31 | + public static final String VERTEX_SHADER = |
| 32 | + "precision mediump float;\n" + |
| 33 | + "uniform mat4 " + MATRIX_UNIFORM + ";\n" + |
| 34 | + "uniform mat4 " + TEXTURE_MATRIX_UNIFORM + ";\n" + |
| 35 | + "attribute vec2 " + POSITION_ATTRIBUTE + ";\n" + |
| 36 | + "const int GAUSSIAN_SAMPLES = 9;\n" + |
| 37 | + "\n" + |
| 38 | + "uniform float " + TEXEL_WIDTH_OFFSET + ";\n" + |
| 39 | + "uniform float " + TEXEL_HEIGHT_OFFSET + ";\n" + |
| 40 | + "\n" + |
| 41 | + "varying vec2 " + VARYING_TEXTURE_COORD + ";\n" + |
| 42 | + "varying vec2 " + BLUR_COORDINATES + "[GAUSSIAN_SAMPLES];\n" + |
| 43 | + "\n" + |
| 44 | + "void main()\n" + |
| 45 | + "{\n" + |
| 46 | + " vec4 pos = vec4(" + POSITION_ATTRIBUTE + ", 0.0, 1.0);\n" + |
| 47 | + " gl_Position = " + MATRIX_UNIFORM + "* pos;\n" + |
| 48 | + VARYING_TEXTURE_COORD + " = (" + TEXTURE_MATRIX_UNIFORM + " * pos).xy;\n" + |
| 49 | + " \n" + |
| 50 | + " int multiplier = 0;\n" + |
| 51 | + " vec2 blurStep;\n" + |
| 52 | + " vec2 singleStepOffset = vec2(" + TEXEL_HEIGHT_OFFSET + ", " + TEXEL_WIDTH_OFFSET + ");\n" + |
| 53 | + " \n" + |
| 54 | + " for (int i = 0; i < GAUSSIAN_SAMPLES; i++)\n" + |
| 55 | + " {\n" + |
| 56 | + " multiplier = (i - ((GAUSSIAN_SAMPLES - 1) / 2));\n" + |
| 57 | + " // Blur in x (horizontal)\n" + |
| 58 | + " blurStep = float(multiplier) * singleStepOffset;\n" + |
| 59 | + " " + BLUR_COORDINATES + "[i] = " + VARYING_TEXTURE_COORD + ".xy + blurStep;\n" + |
| 60 | + " }\n" + |
| 61 | + "}\n"; |
| 62 | + |
| 63 | + public static final String FRAGMENT_SHADER = |
| 64 | + "uniform sampler2D " + TEXTURE_SAMPLER_UNIFORM + ";\n" + |
| 65 | + "\n" + |
| 66 | + "const lowp int GAUSSIAN_SAMPLES = 9;\n" + |
| 67 | + "\n" + |
| 68 | + "varying highp vec2 " + VARYING_TEXTURE_COORD + ";\n" + |
| 69 | + "varying highp vec2 " + BLUR_COORDINATES + "[GAUSSIAN_SAMPLES];\n" + |
| 70 | + "\n" + |
| 71 | + "void main()\n" + |
| 72 | + "{\n" + |
| 73 | + " lowp vec3 sum = vec3(0.0);\n" + |
| 74 | + " lowp vec4 fragColor=texture2D(" + TEXTURE_SAMPLER_UNIFORM + "," + VARYING_TEXTURE_COORD + ");\n" + |
| 75 | + " \n" + |
| 76 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[0]).rgb * 0.05;\n" + |
| 77 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[1]).rgb * 0.09;\n" + |
| 78 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[2]).rgb * 0.12;\n" + |
| 79 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[3]).rgb * 0.15;\n" + |
| 80 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[4]).rgb * 0.18;\n" + |
| 81 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[5]).rgb * 0.15;\n" + |
| 82 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[6]).rgb * 0.12;\n" + |
| 83 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[7]).rgb * 0.09;\n" + |
| 84 | + " sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[8]).rgb * 0.05;\n" + |
| 85 | + "\n" + |
| 86 | + " gl_FragColor = vec4(sum,fragColor.a);\n" + |
| 87 | + "}"; |
| 88 | + |
| 89 | + |
| 90 | + @Override |
| 91 | + public String getVertexShader() { |
| 92 | + return VERTEX_SHADER; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String getFragmentShader() { |
| 97 | + return FRAGMENT_SHADER; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onPreDraw(int program, BasicTexture texture, ICanvasGL canvas) { |
| 103 | + super.onPreDraw(program, texture, canvas); |
| 104 | + int widthOffsetLocation = GLES20.glGetUniformLocation(program, TEXEL_WIDTH_OFFSET); |
| 105 | + int heightOffsetLocation = GLES20.glGetUniformLocation(program, TEXEL_HEIGHT_OFFSET); |
| 106 | + if (dir == 0) { |
| 107 | + OpenGLUtil.setFloat(widthOffsetLocation, blurSize / texture.getWidth()); |
| 108 | + OpenGLUtil.setFloat(heightOffsetLocation, 0); |
| 109 | + |
| 110 | + } else { |
| 111 | + OpenGLUtil.setFloat(widthOffsetLocation, 0); |
| 112 | + OpenGLUtil.setFloat(heightOffsetLocation, blurSize / texture.getHeight()); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public void setBlurSize(float blurSize) { |
| 119 | + this.blurSize = blurSize; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + public GaussianBlurFilter(float blurSize) { |
| 127 | + super(new ArrayList<TextureFilter>()); |
| 128 | + mFilters.add(new GaussianFilter(0, blurSize)); |
| 129 | + mFilters.add(new GaussianFilter(1, blurSize)); |
| 130 | + updateMergedFilters(); |
| 131 | + } |
| 132 | + |
| 133 | + public void setBlurSize(float blurSize) { |
| 134 | + for (TextureFilter textureFilter : mFilters) { |
| 135 | + if (textureFilter instanceof GaussianFilter) { |
| 136 | + GaussianFilter gaussianFilter = (GaussianFilter) textureFilter; |
| 137 | + gaussianFilter.setBlurSize(blurSize); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments