简体   繁体   English

在opengl es片段着色器中,如何将纹理移动一些像素值?

[英]In opengl es fragment shader, how to shift texture by some pixel values?

I am currently using multiple textures for a rendering job. 我当前正在使用多个纹理进行渲染。 All textures are passed to the same fragment shader for processing. 所有纹理都传递到同一片段着色器进行处理。 One of the textures need to be shifted by some pixel values. 纹理之一需要移动一些像素值。 How to achieve this in fragment shader code? 如何在片段着色器代码中实现这一点?

I have tried texture2D(tex, texCoord.xy + vec2(shiftx, 0.0)), where shiftx is a less than 1 float value. 我尝试了texture2D(tex,texCoord.xy + vec2(shiftx,0.0)),其中shiftx小于1浮点值。 This does not work properly. 这不能正常工作。 Ideally, the area where the texture shift away should be blank, but the shader uses the last pixel color to fill the area. 理想情况下,纹理移开的区域应为空白,但着色器使用最后的像素颜色填充该区域。 If this area can be cleared, this could be one method. 如果可以清除此区域,则可能是一种方法。 But is there any other solutions? 但是还有其他解决方案吗?

Thanks! 谢谢!

No. The primitive defines the drawing area so if you are using multiple textures in a single shader that is not possible (if only 1 texture is used then rather offset the primitive). 否。图元定义了绘图区域,因此,如果在单个着色器中使用多个纹理是不可能的(如果仅使用1个纹理,则偏移图元)。 So what you need to do is check the coordinate and if the values are out of bounds (less then zero or more then one) do a separate logic as in not including the texture there. 因此,您需要做的是检查坐标,如果值超出范围(小于零或大于一个,则大于一个),则执行单独的逻辑,如不包括那里的纹理。

Some source code might be helpful to suggest you a proper solution. 一些源代码可能有助于建议您适当的解决方案。

Just a note here it usually makes sense to do the coordinate translation in the vertex shader. 此处仅需注意,在顶点着色器中进行坐标平移通常是有意义的。 You need to use another varying coordinates like varying lowp vec2 offsetedTexCoord for it then. 然后,您需要使用其他变化的坐标,例如varying lowp vec2 offsetedTexCoord

There's more than one way to go about this. 有多种方法可以解决此问题。 The easiest is to check if one of the wrap types will do what you need. 最简单的方法是检查其中一种包装类型是否可以满足您的需要。 This one fills with a solid color instead of the last pixel which you said was causing trouble. 该像素用纯色填充,而不是您说引起麻烦的最后一个像素。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

If a wrap doesn't give the desired effect, you already know where the cutoff for the shift is. 如果自动换行没有达到预期的效果,您已经知道转换的截止点在哪里。 So discard the fragment or set it to any color you want. 因此,丢弃片段或将其设置为所需的任何颜色。

if (texCoord.x < shiftx) discard;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM