gpt4 book ai didi

graphics - 是否有使用深度缓冲区的假抗锯齿算法?

转载 作者:行者123 更新时间:2023-12-02 00:13:04 30 4
gpt4 key购买 nike

最近我在我的 OpenGL 应用程序中实现了 FXAA 算法。我现在还没有完全理解这个算法,但我知道它使用最终图像的对比度数据来有选择地应用模糊。作为有意义的后处理效果。 B 因为我在我的应用程序中使用了延迟着色,所以我已经有了场景的深度纹理。使用它可能更容易和更精确地找到边缘以在那里应用模糊。

那么是否有一种已知的抗锯齿算法使用深度纹理而不是最终图像来找到边缘?我所说的假货是指基于像素而不是顶点的抗锯齿算法。

最佳答案

经过一些研究,我发现我的想法已经在延迟渲染器中广泛使用。我决定发布这个答案,因为我想出了我自己的实现,我想与社区分享。

根据深度的梯度变化和法线的角度变化,对像素应用模糊。

// GLSL fragment shader

#version 330

in vec2 coord;
out vec4 image;

uniform sampler2D image_tex;
uniform sampler2D position_tex;
uniform sampler2D normal_tex;
uniform vec2 frameBufSize;

void depth(out float value, in vec2 offset)
{
value = texture2D(position_tex, coord + offset / frameBufSize).z / 1000.0f;
}

void normal(out vec3 value, in vec2 offset)
{
value = texture2D(normal_tex, coord + offset / frameBufSize).xyz;
}

void main()
{
// depth

float dc, dn, ds, de, dw;
depth(dc, vec2( 0, 0));
depth(dn, vec2( 0, +1));
depth(ds, vec2( 0, -1));
depth(de, vec2(+1, 0));
depth(dw, vec2(-1, 0));

float dvertical = abs(dc - ((dn + ds) / 2));
float dhorizontal = abs(dc - ((de + dw) / 2));
float damount = 1000 * (dvertical + dhorizontal);

// normals

vec3 nc, nn, ns, ne, nw;
normal(nc, vec2( 0, 0));
normal(nn, vec2( 0, +1));
normal(ns, vec2( 0, -1));
normal(ne, vec2(+1, 0));
normal(nw, vec2(-1, 0));

float nvertical = dot(vec3(1), abs(nc - ((nn + ns) / 2.0)));
float nhorizontal = dot(vec3(1), abs(nc - ((ne + nw) / 2.0)));
float namount = 50 * (nvertical + nhorizontal);

// blur

const int radius = 1;
vec3 blur = vec3(0);
int n = 0;
for(float u = -radius; u <= +radius; ++u)
for(float v = -radius; v <= +radius; ++v)
{
blur += texture2D(image_tex, coord + vec2(u, v) / frameBufSize).rgb;
n++;
}
blur /= n;

// result

float amount = mix(damount, namount, 0.5);
vec3 color = texture2D(image_tex, coord).rgb;
image = vec4(mix(color, blur, min(amount, 0.75)), 1.0);
}

为了比较,这是没有任何抗锯齿的场景。

scene without anti-aliasing

这是应用抗锯齿后的结果。

scene with anti-aliasing

您可能需要以全分辨率查看图像以判断效果。在我看来,结果对于简单的实现来说已经足够了。最好的一点是,当相机移动时几乎没有锯齿状伪影。

关于graphics - 是否有使用深度缓冲区的假抗锯齿算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14566483/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com