Last active
August 18, 2022 17:03
-
-
Save TheDreamsWind/d724a0f7a56bf95894cfa4d46b9d4347 to your computer and use it in GitHub Desktop.
[SO-a/53772151/5690248] `GPUImageFilter` with subtract blending function implemented
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package the.dreams.wind.blendingfilter; | |
import android.opengl.GLES20; | |
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter; | |
class ColorMatrixSubtractFilter extends GPUImageFilter { | |
private static final String FRAGMENT_SHADER = "precision mediump float;" + | |
"struct ColorFilter {" + | |
" mat4 factor;" + | |
" vec4 shift;" + | |
"};" + | |
"uniform sampler2D inputImageTexture;" + | |
"uniform ColorFilter uColorFilter;" + | |
"varying vec2 textureCoordinate;" + | |
"void main() {" + | |
" vec4 originalColor = texture2D(inputImageTexture, textureCoordinate);" + | |
" originalColor.rgb *= originalColor.a;" + | |
" vec4 filteredColor = (originalColor * uColorFilter.factor) + uColorFilter.shift;" + | |
" filteredColor.rgb *= filteredColor.a;" + | |
" gl_FragColor = vec4(originalColor.rgb - filteredColor.rgb, originalColor.a);" + | |
"}"; | |
private final float[] mColorFilter; | |
// ========================================== // | |
// Lifecycle | |
// ========================================== // | |
ColorMatrixSubtractFilter(float[] mColorFilter) { | |
this(NO_FILTER_VERTEX_SHADER, mColorFilter); | |
} | |
@SuppressWarnings("WeakerAccess") | |
ColorMatrixSubtractFilter(String vertexShader, float[] colorFilter) { | |
super(vertexShader, FRAGMENT_SHADER); | |
mColorFilter = colorFilter; | |
} | |
// ========================================== // | |
// GPUImageFilter | |
// ========================================== // | |
@Override | |
public void onInitialized() { | |
super.onInitialized(); | |
attachColorFilter(getProgram()); | |
} | |
// ========================================== // | |
// Private | |
// ========================================== // | |
private void attachColorFilter(int program) { | |
final float[] colorFilterFactor = new float[4 * 4]; | |
final float[] colorFilterShift = new float[4]; | |
for (int i = 0; i < mColorFilter.length; i++) { | |
final float value = mColorFilter[i]; | |
final int calculateIndex = i + 1; | |
if (calculateIndex % 5 == 0) { | |
colorFilterShift[calculateIndex / 5 - 1] = value / 255; | |
} else { | |
colorFilterFactor[i - calculateIndex / 5] = value; | |
} | |
} | |
final int colorFactorLocation = GLES20.glGetUniformLocation(program, | |
"uColorFilter.factor"); | |
setUniformMatrix4f(colorFactorLocation, colorFilterFactor); | |
final int colorShiftLocation = GLES20.glGetUniformLocation(program, | |
"uColorFilter.shift"); | |
setFloatVec4(colorShiftLocation, colorFilterShift); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment