如何在 glsl 中的texture2D 中围绕textureCoordinate 正确采样
我想要对特定像素周围的 8 个像素进行采样,并从中计算出一堆信息以在阈值处理例程中使用。
这是在 iOS 中使用 glsl 顶点和片段着色器。
这是我来自片段着色器的采样代码:
p00 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, -1.0*extent));
p01 = texture2D(videoFrame, textureCoordinate+vec2( 0.0, -1.0*extent));
p02 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, -1.0*extent));
p10 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, 0.0));
p11 = texture2D(videoFrame, textureCoordinate);
p12 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, 0.0));
p20 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, +1.0*extent));
p21 = texture2D(videoFrame, textureCoordinate+vec2( 0.0, +1.0*extent));
p22 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, +1.0*extent));
范围只是一个数字,当前设置为 20.0。我希望采样能够在本地合理地进行。
编译正常,但是根据我的例程的结果,我怀疑这不是对当前像素周围的像素进行采样,而是似乎对沿行延伸的像素进行采样。
我有什么想法可以解决这个问题吗?
我不确定这是否有效的另一个原因是在 iOS 文档中,Apple 声明这应该打包在图像单元中,并使用区域感兴趣函数。如果可能的话,我宁愿将其全部保留在 glsl 着色器中。
I am wanting to sample 8 pixels around a specific pixel and calculate a bunch of information from this to use in a thresholding routine.
This is in iOS using a glsl vertex and fragment shaders.
Here is my sampling code from the fragment shader:
p00 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, -1.0*extent));
p01 = texture2D(videoFrame, textureCoordinate+vec2( 0.0, -1.0*extent));
p02 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, -1.0*extent));
p10 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, 0.0));
p11 = texture2D(videoFrame, textureCoordinate);
p12 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, 0.0));
p20 = texture2D(videoFrame, textureCoordinate+vec2(-1.0*extent, +1.0*extent));
p21 = texture2D(videoFrame, textureCoordinate+vec2( 0.0, +1.0*extent));
p22 = texture2D(videoFrame, textureCoordinate+vec2(+1.0*extent, +1.0*extent));
extent is just a number, currently set to 20.0. I want the sampling to happen reasonably locally.
This compiles ok, however based on the results of my routines, I suspect that this is not sampling pixels around the current pixel, rather it seems to be sampling pixels extending along a row.
Any ideas how I can resolve this?
The other reason I am not sure if this works is in the iOS documentation Apple states that this should be packaged in an image unit and that a regionofinterest function be used. I would prefer to keep it all in the glsl shader if all possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果范围为 20,您将对当前像素周围 8 个基本方向上的 8 个像素进行采样,但相距 20 个像素。 (所以 20 个像素“北”,20 个像素“东北”,等等)如果它是 1.0,那么您编写的内容应该对当前像素周围的 8 个像素进行采样。 (我还假设纹理坐标是当前像素的坐标。请确保情况确实如此。)
If extent is 20, you'll be sampling the 8 pixels that are in the 8 cardinal directions around the current one, but 20 pixels away. (So 20 pixels "north", 20 pixels "north east", etc.) If it's 1.0, then what you've written should sample the 8 pixels around the current pixel. (I'm also assuming that textureCoordinate is the current pixel's coordinate. Make sure that's the case.)