1
我正在使用OpenGL-ES混合两种纹理的应用程序。对于叠加图像,我想指定某种颜色作为alpha通道,例如)绿色。这怎么可能完成?我尝试使用glBlendFunc没有太多成功。任何帮助将不胜感激!有没有办法为sampler2D纹理设置alpha通道颜色?
我正在使用OpenGL-ES混合两种纹理的应用程序。对于叠加图像,我想指定某种颜色作为alpha通道,例如)绿色。这怎么可能完成?我尝试使用glBlendFunc没有太多成功。任何帮助将不胜感激!有没有办法为sampler2D纹理设置alpha通道颜色?
有在OpenGL本身没有这样的功能,但也许你可以实现,在着色器:
uniform vec3 colorMask; // eg. green
varying vec2 texCoord;
uniform sampler2D overlay;
void main()
{
vec4 texture = texture2D(overlay, texCoord);
// get RGB of the texture
vec3 absdiff = abs(texture.rgb - colorMask);
float alpha = all(lessThan(absdiff, vec3(0.1)))? 1.0 : 0.0;
// calculate alpha using vector relational functions
texture.a = alpha; // use alpha
gl_FragColor = texture;
// write final color, can use blending
}
这是通过计算纹理颜色的绝对差值和掩蔽颜色,并将其与0.1。这很简单,但它可能很慢(我从内存中写入它,你必须测试它)。
或者你可以使用计算阿尔法以不同的方式:
uniform vec3 colorMask; // eg. green
varying vec2 texCoord;
uniform sampler2D overlay;
void main()
{
vec4 texture = texture2D(overlay, texCoord);
// get RGB of the texture
vec3 diff = texture.rgb - colorMask;
float alpha = step(dot(diff, diff), 0.1);
// calculate alpha using difference squared and a step function
texture.a = alpha; // use alpha
gl_FragColor = texture;
// write final color, can use blending
}
这是用方差度量计算阿尔法。它计算RGB空间中的颜色距离,计算其平方,并将其与0.1进行比较。调整阈值(0.1)可能会稍微困难一点,但可以使用软阈值。如果你有颜色渐变,并希望某些颜色比其他颜色更透明,则可以丢弃step功能并改用smoothstep。