从原始 vec4 偏移后如何替换 texture2D 的空像素?

How to replace vacant pixels of texture2D after offsetting from original vec4?

基本上我是用这段代码从它的原始 inputImageTexture 偏移一个 texture2D

highp vec2 toprightcoord = textureCoordinate + 0.25;
highp vec4 tr = texture2D(inputImageTexture, toprightcoord);

它做了它应该做的事情,但是它从偏移纹理的边缘留下了拉伸的像素颜色(就像拉出的披萨片上的奶酪)。 如何更换成任意颜色或透明?

我假设您已将纹理环绕参数设置为 GL_CLAMP_TO_EDGE。参见 glTexParameter。 当使用纹理查找函数 texture2D 访问纹理时,这会导致拉伸像素超出范围 [0.0, 1.0].

您可以使用 wrap 参数 GL_REPEAT 创建一个 "tiled" 纹理。

但是如果你想要

"How to replace it to any color or transparent?"

,那么你要做一个范围检查。

如果在 xy 坐标处超过 1.0 的限制,以下代码会将 alpha 通道设置为 0.0。如果纹理坐标在边界内,则变量 inBounds 设置为 1.0,否则设置为 0.0:

vec2 toprightcoord = textureCoordinate + 0.25;
vec4 tr = texture2D(inputImageTexture, toprightcoord);

vec2  boundsTest = step(toprightcoord, vec2(1.0));
flaot inBounds   = boundsTest.x * boundsTest.y;
tr.a *= inBounds;

您可以将其扩展到 [0.0, 1.0] 中的范围测试:

vec2 boundsTest = step(vec2(0.0), toprightcoord) * step(toprightcoord, vec2(1.0)); 

注意,glsl函数step

genType step( genType edge, genType x);

如果 x[i] < edge[i] 则返回 0.0,否则返回 1.0。

使用glsl函数mix,颜色可以换成不同的:

vec4 red = vec4(1.0, 0.0, 0.0, 1.0); 
tr = mix(red, tr, inBounds);