我假设您已将纹理环绕参数设置为GL_CLAMP_TO_EDGE。见glTexParameter。
当使用纹理查找函数texture2D 访问纹理时,这会导致拉伸像素超出范围 [0.0, 1.0]。
您可以使用包裹参数GL_REPEAT 创建“平铺”纹理。
如果你愿意
“如何将其替换为任何颜色或透明?”
,那么你必须进行范围检查。
如果在 x 或 y 坐标处超过 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);