如何使片段着色器用alpha,opengl-es替换白色

时间:2014-09-17 23:43:47

标签: opengl-es fragment-shader

我试图想出一个用alpha替换白色像素的opengl-es片段着色器。具有方格背景的图像是我想要的。方格背景表示alpha转换后的图像。有小费吗?通常我讨厌在这里问这个问题,但我找不到任何东西。

Before fragment shader

After fragment shader

2 个答案:

答案 0 :(得分:1)

获取"白色像素"就像你发布的图像似乎是一个灰度组件。这就是总结RGB值除以3.然后输出RGB在你的情况下都是.0而alpha等于灰度像素......

    vec4 textureSample = texture2D(uniformTexture, textureCoordinate);
    lowp float grayscaleComponent = textureSample.x*(1.0/3.0) + textureSample.y*(1.0/3.0) + textureSample.z*(1.0/3.0);
    gl_FragColor =  lowp vec4(.0, .0, .0, grayscaleComponent);

答案 1 :(得分:1)

正确地说,灰度值是0.2126 * R + 0.7152 * G + 0.0722 * B http://en.wikipedia.org/wiki/Grayscale

相关问题