/
GlMonochromeFilter.java
58 lines (39 loc) · 1.75 KB
/
GlMonochromeFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.daasuu.mp4compose.filter;
import android.opengl.GLES20;
/**
* Created by sudamasayuki on 2018/01/06.
*/
public class GlMonochromeFilter extends GlFilter {
private static final String FRAGMENT_SHADER =
"precision lowp float;" +
"varying highp vec2 vTextureCoord;" +
"uniform lowp sampler2D sTexture;" +
"uniform float intensity;" +
"const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);" +
"void main() {" +
"lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);" +
"float luminance = dot(textureColor.rgb, luminanceWeighting);" +
"lowp vec4 desat = vec4(vec3(luminance), 1.0);" +
"lowp vec4 outputColor = vec4(" +
"(desat.r < 0.5 ? (2.0 * desat.r * 0.6) : (1.0 - 2.0 * (1.0 - desat.r) * (1.0 - 0.6)))," +
"(desat.g < 0.5 ? (2.0 * desat.g * 0.45) : (1.0 - 2.0 * (1.0 - desat.g) * (1.0 - 0.45)))," +
"(desat.b < 0.5 ? (2.0 * desat.b * 0.3) : (1.0 - 2.0 * (1.0 - desat.b) * (1.0 - 0.3)))," +
"1.0" +
");" +
"gl_FragColor = vec4(mix(textureColor.rgb, outputColor.rgb, intensity), textureColor.a);" +
"}";
private float intensity = 1.0f;
public GlMonochromeFilter() {
super(DEFAULT_VERTEX_SHADER, FRAGMENT_SHADER);
}
public float getIntensity() {
return intensity;
}
public void setIntensity(float intensity) {
this.intensity = intensity;
}
@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("intensity"), intensity);
}
}