变量声明 (GLES 语法)
precision mediump float;
varying highp vec2 textureCoordinate;
uniform sampler2D inputTexture;
//亮度
uniform lowp float brightness;
//对比度
uniform lowp float contrast;
//饱和度
uniform lowp float saturation;
// Values from \\\"Graphics Shaders: Theory and Practice\\\" by Bailey and Cunningham
const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
1.亮度调整
lowp vec4 textureColor = texture2D(inputTexture, textureCoordinate);
gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);
获取handle
mBrightnessHandle = GLES20.glGetUniformLocation(mProgramId, "brightness");
赋值 (-1.0 to 1.0, 默认为0.0f)
GLES20.glUniform1f(mBrightnessHandle, 0.0f);
2.对比度
lowp vec4 textureColor = texture2D(inputTexture, textureCoordinate);
gl_FragColor = vec4((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5), textureColor.w);
获取handle
mContrastHandle = GLES20.glGetUniformLocation(mProgramId, "contrast");
赋值 ( 0.0 ~ 4.0, 默认1.0f)
GLES20.glUniform1f(mContrastHandle, 1.0f);
3.饱和度
lowp vec4 textureColor = texture2D(inputTexture, textureCoordinate);
lowp vec3 greyScaleColor = vec3(dot(contrastColor.rgb, luminanceWeighting));
gl_FragColor = vec4(mix(greyScaleColor, contrastColor.rgb, saturation), textureColor.w);
获取handle
mSaturationHandle = GLES30.glGetUniformLocation(mProgramId, "saturation");
赋值(0.0 ~ 2.0之间 默认1.0f)
GLES20.glUniform1f(mSaturationHandle, mSaturationValue);
如果想一起合并使用同一个fragment_shader,可以这样弄
void main()
{
//亮度
lowp vec3 brightnessColor = textureColor.rgb + vec3(brightness);
//对比度
lowp vec3 contrastColor = (brightnessColor.rgb - vec3(0.5)) * contrast + vec3(0.5);
//饱和度
lowp vec3 greyScaleColor = vec3(dot(contrastColor.rgb, luminanceWeighting));
gl_FragColor = vec4(mix(greyScaleColor, contrastColor.rgb, saturation), textureColor.w);
}
网友评论