片段着色器模糊......这是如何工作的?

发布于 2024-08-25 04:27:10 字数 500 浏览 4 评论 0原文

uniform sampler2D sampler0;
uniform vec2 tc_offset[9];
void blur()
{
  vec4 sample[9];
  for(int i = 0; i < 9; ++i)
    sample[i] = texture2D(sampler0, gl_TexCoord[0].st + tc_offset[i]);

  gl_FragColor = (sample[0] + (2.0 * sample[1]) + sample[2] +
      (2.0 * sample[3]) + sample[4] + 2.0 * sample[5] +
      sample[6] + 2.0 * sample[7] + sample[8] ) / 13.0;
}

Sample[i] =texture2D(sample0, ...) 行如何工作?

似乎模糊了图像,我必须首先生成图像,但在这里,我以某种方式尝试查询我正在生成的图像。这是如何运作的?

uniform sampler2D sampler0;
uniform vec2 tc_offset[9];
void blur()
{
  vec4 sample[9];
  for(int i = 0; i < 9; ++i)
    sample[i] = texture2D(sampler0, gl_TexCoord[0].st + tc_offset[i]);

  gl_FragColor = (sample[0] + (2.0 * sample[1]) + sample[2] +
      (2.0 * sample[3]) + sample[4] + 2.0 * sample[5] +
      sample[6] + 2.0 * sample[7] + sample[8] ) / 13.0;
}

How does the sample[i] = texture2D(sample0, ...) line work?

It seems like to blur an image, I have to first generate the image, yet here, I'm somehow trying to query the very iamge I'm generating. How does this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2024-09-01 04:27:10

它将模糊内核应用于图像。 tc_offset 需要由应用程序正确初始化,以在实际纹理坐标周围形成 3x3 的采样点区域:(

0   0   0
0   x   0
0   0   0

假设 x 是原始坐标)。左上角采样点的偏移量为-1/width,-1/height。中心点的偏移需要小心地与纹素中心对齐(偏离 0.5 的问题)。此外,硬件双线性滤波器可用于以廉价的方式增加模糊量(通过在纹素之间采样)。

着色器的其余部分根据样本的距离来缩放样本。通常,这也是预先计算的:

for(int i = 0; i < NUM_SAMPLES; ++i) {
    result += texture2D(sampler,texcoord+offsetscaling[i].xy)*offsetscaling[i].z;
}

It applies a blur kernel to the image. tc_offset needs to be properly initialized by the application to form a 3x3 area of sampling points around the actual texture coordinate:

0   0   0
0   x   0
0   0   0

(assuming x is the original coordinate). The offset for the upper-left sampling point would be -1/width,-1/height. The offset for the center point needs to be carefully aligned to texel center (the off-by-0.5 problem). Also, the hardware bilinear filter can be used to cheaply increase the amount of blur (by sampling between texels).

The rest of the shader scales the samples by their distance. Usually, this is precomputed as well:

for(int i = 0; i < NUM_SAMPLES; ++i) {
    result += texture2D(sampler,texcoord+offsetscaling[i].xy)*offsetscaling[i].z;
}
花开浅夏 2024-09-01 04:27:10

一种方法是生成原始图像以渲染到纹理,而不是渲染到屏幕。
然后,您使用此着色器和纹理作为输入来绘制全屏四边形以对图像进行后处理。

One way is to generate your original image to render to a texture, not to the screen.
And then you draw a full screen quad using this shader and the texture as it's input to post-process the image.

筱果果 2024-09-01 04:27:10

正如您所注意到的,为了制作模糊图像,您首先需要制作图像,然后对其进行模糊处理。该着色器(仅)执行第二步,获取之前生成的图像并对其进行模糊处理。其他地方需要额外的代码来生成原始的非模糊图像。

As you note, in order to make a blurred image, you first need to make an image, and then blur it. This shader does (just) the second step, taking an image that was generated previously and blurring it. There needs to be additional code elsewhere to generate the original non-blurred image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文