将强度参数添加到GPUImageFalseColorFilter

时间:2015-10-05 22:48:33

标签: ios objective-c opengl-es gpuimage

请参阅底部的更新

看起来GPUImageFalseColorFilter是从另一个过滤器中复制的 - 我可以在着色器中看到intensity属性,尽管它实际上并没有做任何事情。我为它添加了setter和Obj-c包装器,但现在我似乎无法获得gl_fragColor返回值以将强度作为参数。

以下是使用100%强度的着色器:

 precision lowp float;

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;
 uniform lowp float intensity;
 uniform vec4 shadowTintColor;
 uniform vec4 highlightTintColor;

 const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);

 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float luminance = dot(textureColor.rgb, luminanceWeighting);

     gl_FragColor = vec4( mix(shadowTintColor.rgb, highlightTintColor.rgb, luminance), textureColor.a);
 }
 );

我试过了:

gl_FragColor = vec4( mix(shadowTintColor.rgb, highlightTintColor.rgb, luminance), intensity);
gl_FragColor = mix(textureColor, mix(shadowTintColor.rgb, highlightTintColor.rgb, luminance), intensity);

和其他一些人,但过滤器崩溃时出现了大量错误(No matching function for call to mix(vec4, vec3, float)Too many arguments to constructor of 'vec4')。

如何获得强度属性?

对于奖励积分,编写原始滤镜以将vec4色调颜色作为输入,但仅在滤镜中使用vec3色调颜色。除了全球"强度"我喜欢在每种颜色上支持alpha通道。

更新

在这方面取得了一些重大进展 - 它现在正在接受像这样的个别强度参数:

 precision lowp float;

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;
 uniform lowp float shadowTintIntensity;
 uniform lowp float highlightTintIntensity;
 uniform vec4 shadowTintColor;
 uniform vec4 highlightTintColor;

 const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
 {
    lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
    highp float luminance = dot(textureColor.rgb, luminanceWeighting);
    highp vec4 shadowResult = mix(textureColor, max(textureColor, vec4( mix(shadowTintColor.rgb, textureColor.rgb, luminance), textureColor.a)), shadowTintIntensity);
    highp vec4 mixedResult = mix(textureColor, min(shadowResult, vec4( mix(shadowResult.rgb, highlightTintColor.rgb, luminance), textureColor.a)), highlightTintIntensity / 3.0);

    gl_FragColor = mixedResult;
 }
 );

我现在可以用强度正确地影响图像,但亮度加权看起来有点偏差?我在高亮计算器上手动添加了/ 3.0乘数,它似乎平衡了阴影并突出强度效果,但这并不是正确的方法。 编辑:这甚至根本没有正确地将它们混合在一起 - 如果未指定highlightTint,则效果不起作用。

如何将最终图像正确混合在一起?

1 个答案:

答案 0 :(得分:3)

最后通过使用mix并将两个结果与亮度常数混合来实现这一点:

vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
float luminance = dot(textureColor.rgb, luminanceWeighting);

vec4 shadowResult = mix(textureColor, max(textureColor, vec4( mix(shadowTintColor.rgb, textureColor.rgb, luminance), textureColor.a)), shadowTintIntensity);
vec4 highlightResult = mix(textureColor, min(shadowResult, vec4( mix(shadowResult.rgb, highlightTintColor.rgb, luminance), textureColor.a)), highlightTintIntensity);

gl_FragColor = vec4( mix(shadowResult.rgb, highlightResult.rgb, luminance), textureColor.a);
//                            ^                   ^