EquirectangularSeamCorrection icon indicating copy to clipboard operation
EquirectangularSeamCorrection copied to clipboard

Consider less generic solutions

Open rdoeffinger opened this issue 4 years ago • 11 comments

The proposed solutions all are based on explicitly smoothing out the derivatives. However I would suggest a different interpretation of the issue: the function to calculate the coordinates is discontinuous, and the solution to switch between 2 equivalent variants that have the discontinuity in different locations. Instead of selecting whichever outputs that minimize the delta (where btw you could imagine cases where it would be wrong), it would also be possible to simply select the correct function from the start, leaving the remaining challenge to agree on which across the quad (assuming it's not possible to stitch the functions up in such a way to completely remove the discontinuity). To give an example, in ShaderToy the approach looks like this:

        // Calculate if we are near a bad part of atan and should switch,
        // then distribute that information.
        // Where possible, it would be better to calculate "bad" in the vertex
        // shader and store it into a flat attribute
        float bad = (2.0*pos.y < pos.x) ? 1.0 : 0.0;
        float badx1 = dFdx(bad);
        float bady1 = dFdy(bad);
        bad = (badx1 >= bad || bady1 >= bad) ? 1.0 : 0.0;
        badx1 = dFdx(bad);
        bady1 = dFdy(bad);
        if (badx1 > 0.0 || bady1 > 0.0) bad = 1.0;
        uv = vec2(bad > 0.0 ? phi_frac : phi, theta);

Note that this is particularly cheap in cases like the sphere with a simple texture mapped onto it since you can a priori know which triangles need to use which function and set things up either in a vertex shader or even as fixed vertex attributes (in which case it can work even without flat attributes, though requires duplicating a few vertices). I call this less generic because you need to be able to figure out if a quad (or triangle) would be affected by a discontinuity, so needs adjustment to your specific coordinate generation function.

rdoeffinger avatar Apr 13 '21 18:04 rdoeffinger