【发布时间】:2021-10-07 12:43:33
【问题描述】:
我一直在尝试使用 Android 中的 OpenGL 绘制具有透明背景的图像的边框。我正在使用片段着色器和顶点着色器。 (From the GPUImage Library)
下面我添加了图A和图B。
图A。
图 B.
我已经实现了图 A。使用自定义的片段着色器。但是无法使边框更平滑,如图 B 所示。我附上了我使用过的着色器代码(以实现粗糙的边框)。这里有人可以帮助我如何使边框更平滑吗?
这是我的顶点着色器:
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
这是我的片段着色器:
我计算了当前像素周围的 8 个像素。如果这 8 个像素中的任何一个像素是不透明的(alpha 大于 0.4),它就会被绘制为边框颜色。
precision mediump float;
uniform sampler2D inputImageTexture;
varying vec2 textureCoordinate;
uniform lowp float thickness;
uniform lowp vec4 color;
void main() {
float x = textureCoordinate.x;
float y = textureCoordinate.y;
vec4 current = texture2D(inputImageTexture, vec2(x,y));
if ( current.a != 1.0 ) {
float offset = thickness * 0.5;
vec4 top = texture2D(inputImageTexture, vec2(x, y - offset));
vec4 topRight = texture2D(inputImageTexture, vec2(x + offset,y - offset));
vec4 topLeft = texture2D(inputImageTexture, vec2(x - offset, y - offset));
vec4 right = texture2D(inputImageTexture, vec2(x + offset, y ));
vec4 bottom = texture2D(inputImageTexture, vec2(x , y + offset));
vec4 bottomLeft = texture2D(inputImageTexture, vec2(x - offset, y + offset));
vec4 bottomRight = texture2D(inputImageTexture, vec2(x + offset, y + offset));
vec4 left = texture2D(inputImageTexture, vec2(x - offset, y ));
if ( top.a > 0.4 || bottom.a > 0.4 || left.a > 0.4 || right.a > 0.4 || topLeft.a > 0.4 || topRight.a > 0.4 || bottomLeft.a > 0.4 || bottomRight.a > 0.4 ) {
if (current.a != 0.0) {
current = mix(color , current , current.a);
} else {
current = color;
}
}
}
gl_FragColor = current;
}
【问题讨论】: