片段着色器模糊......这是如何工作的?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它将模糊内核应用于图像。
tc_offset
需要由应用程序正确初始化,以在实际纹理坐标周围形成 3x3 的采样点区域:(假设 x 是原始坐标)。左上角采样点的偏移量为
-1/width,-1/height
。中心点的偏移需要小心地与纹素中心对齐(偏离 0.5 的问题)。此外,硬件双线性滤波器可用于以廉价的方式增加模糊量(通过在纹素之间采样)。着色器的其余部分根据样本的距离来缩放样本。通常,这也是预先计算的:
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:(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:
一种方法是生成原始图像以渲染到纹理,而不是渲染到屏幕。
然后,您使用此着色器和纹理作为输入来绘制全屏四边形以对图像进行后处理。
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.
正如您所注意到的,为了制作模糊图像,您首先需要制作图像,然后对其进行模糊处理。该着色器(仅)执行第二步,获取之前生成的图像并对其进行模糊处理。其他地方需要额外的代码来生成原始的非模糊图像。
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.