如何将抗脉化应用于着色器中的等距网格线?

发布于 2025-01-29 07:24:07 字数 1172 浏览 4 评论 0原文

我有一个等距网格着色器,当缩放

“在此处输入图像说明”

这些间隙不应存在,线条应该看起来平滑。我如何对此进行抗抗衡?

代码

const float tileSize = 0.04;
const float borderSize = 0.98;

vec2 aGrid(vec2 uv) {
  return vec2(mod(uv.x, 1.0), mod(uv.y * 2.0, 1.0));
}

vec2 bGrid(vec2 uv) {
  return vec2(mod(uv.x - 0.5, 1.0), mod((uv.y * 2.0) - 0.5, 1.0));
}

float los(vec2 pos, float border) {
  vec2 abspos = abs(pos - vec2(0.5));
  return sign(abspos.x * border + abspos.y * border - border * border);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = fragCoord / iResolution.xy;
  float border = 0.5 * borderSize;
  vec2 size = (uv / tileSize) / 2.0;
  float alos = los(aGrid(size), border);
  float blos = los(bGrid(size), border);
  float color = min(alos, blos);
  fragColor = vec4(color);
}

这是我必须在此处使此等距网格在ShaderToy https:///www.shadertoy中运行的 。 com/view/slbbz3

我找到了另一个用于抗矩形网格的着色器。

I have an isometric grid shader that looks like this when zoomed out

enter image description here

Those gaps should not be there and lines should look smooth. How I can apply antialising to this?

This is the code I have to make this isometric grid

const float tileSize = 0.04;
const float borderSize = 0.98;

vec2 aGrid(vec2 uv) {
  return vec2(mod(uv.x, 1.0), mod(uv.y * 2.0, 1.0));
}

vec2 bGrid(vec2 uv) {
  return vec2(mod(uv.x - 0.5, 1.0), mod((uv.y * 2.0) - 0.5, 1.0));
}

float los(vec2 pos, float border) {
  vec2 abspos = abs(pos - vec2(0.5));
  return sign(abspos.x * border + abspos.y * border - border * border);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = fragCoord / iResolution.xy;
  float border = 0.5 * borderSize;
  vec2 size = (uv / tileSize) / 2.0;
  float alos = los(aGrid(size), border);
  float blos = los(bGrid(size), border);
  float color = min(alos, blos);
  fragColor = vec4(color);
}

Here is running in shadertoy https://www.shadertoy.com/view/slBBz3

I found this other shader for an antialiased rectangle grid https://www.shadertoy.com/view/7lBBz3 I tried to apply the same technique but I can't find how

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

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

发布评论

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

评论(1

朕就是辣么酷 2025-02-05 07:24:07

我建议简化您的代码。使用 lineare equation 根据片段的x坐标计算一条

float y = uv.x * 0.5;

线距离片段的y坐标的距离,并获得瓷砖大小的其余部分:

return mod(uv.y - y, tileSize);

计算到线中心的绝对距离:

float d_abs = abs(d - tileSize*0.5);

使用 smoothStep 用于抗sialiasing:

return smoothstep(0.0, borderSize, borderSize*2.0-d_abs);

最小示例:

“

const float tileSize = 0.1;
const float borderSize = 0.001;

float aGrid(vec2 uv) {
    float y = uv.x * -0.5;
    return mod(uv.y - y, tileSize);
}

float bGrid(vec2 uv) {
    float y = uv.x * 0.5;
    return mod(uv.y - y, tileSize);
}

float los(float d) {
    float d_abs = abs(d - tileSize*0.5);
    return smoothstep(0.0, borderSize, borderSize*2.0-d_abs);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    float alos = los(aGrid(uv));
    float blos = los(bGrid(uv));
    float color = max(alos, blos);
    fragColor = vec4(color);
}

I suggest to simplify your code. Use the Linear equation to calculate a line depending on the x-coordinate of the fragment:

float y = uv.x * 0.5;

Calculate the distance to the y-coordinate of the fragment and get the rest of the division with the tile size:

return mod(uv.y - y, tileSize);

Compute the absolute distance to the center of the line:

float d_abs = abs(d - tileSize*0.5);

Use smoothstep for antialiasing:

return smoothstep(0.0, borderSize, borderSize*2.0-d_abs);

Minimal example:

const float tileSize = 0.1;
const float borderSize = 0.001;

float aGrid(vec2 uv) {
    float y = uv.x * -0.5;
    return mod(uv.y - y, tileSize);
}

float bGrid(vec2 uv) {
    float y = uv.x * 0.5;
    return mod(uv.y - y, tileSize);
}

float los(float d) {
    float d_abs = abs(d - tileSize*0.5);
    return smoothstep(0.0, borderSize, borderSize*2.0-d_abs);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    float alos = los(aGrid(uv));
    float blos = los(bGrid(uv));
    float color = max(alos, blos);
    fragColor = vec4(color);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文