【问题标题】:How to replace vacant pixels of texture2D after offsetting from original vec4?从原始vec4偏移后如何替换texture2D的空白像素?
【发布时间】:2018-10-20 05:43:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: opengl-es glsl textures opengl-es-2.0 fragment-shader


    【解决方案1】:

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

    您可以使用包裹参数GL_REPEAT 创建“平铺”纹理。

    如果你愿意

    “如何将其替换为任何颜色或透明?”

    ,那么你必须进行范围检查。

    如果在 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);
    

    【讨论】:

    • 太棒了,这行得通,虽然透明部分仍然留下一些混合颜色,但它仍然可以做它应该做的事情。谢谢人
    猜你喜欢
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 2012-01-01
    • 2022-01-07
    相关资源
    最近更新 更多