Chris_F
Chris_F

Reputation: 5567

Why am I getting these jagged edges in my shader?

I wrote a simple shadertoy example of ray-plane intersection. I am using a checker pattern on the plane and I noticed odd artifacts. I know that this lacks anti-aliasing, so I was fully expecting jagged lines. However, there are random pixels along the edges that seem to stand out as being incorrect. I tried my best to recreate the same thing in Blender using the Cycles renderer with 1 sample per pixel, and it produced a result closer to what I was expecting. Can anyone explain this?

enter image description here

float intersect(in vec3 ro, in vec3 rd)
{
    return -ro.y / rd.y;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    vec2 uv = ((fragCoord + vec2(0.5)) / iResolution.xy * 2.0 - 1.0) * vec2(16.0 / 9.0, 1.0);
    vec3 ro = vec3(0.0, 1.0, 0.0);
    vec3 rd = normalize(vec3(uv, 2.0));

    float t = intersect(ro, rd);

    vec3 col = vec3(0.0, 0.0, 0.0);

    if (t >= 0.0)
    { 
        vec3 pos = ro + t * rd;
        vec2 tex = floor(pos.xz);
        col = vec3(mod(tex.x + tex.y, 2.0));
    }

    fragColor = vec4(col, 1.0);
}

Upvotes: 2

Views: 1543

Answers (1)

Alex - GlassEditor.com
Alex - GlassEditor.com

Reputation: 15557

If the grid lines fall directly on a sampling point then floating point error will determine which grid square is chosen for that pixel. Cycles uses random sampling points, while you are using integers (fragCoord + vec2(0.5)). Changing your sampling points to fragCoord + vec2(0.001234, 0.004321) removes the artifacts.

Upvotes: 1

Related Questions