GLSL fragment shader: How do I prevent seams when sampling half of texture to repeat? (with mipmapping or linear filtering on)

29 views Asked by At

I have this texture base texture

One half is the base, the other half is the lit-up magma in the cracks of the rock, which fades in and out using a sine of time passed. The GLSL shader is as follows:

// Lava crack shader by Striker
uniform float timer;

vec4 getTexelWrapped(vec2 coord)
{   
    return texture2DGrad(tex, fract(coord), dFdx(coord), dFdy(coord));
}

float map(float value, float min1, float max1, float min2, float max2)
{
    return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}

vec4 Process(vec4 color)
{
    vec2 coord = gl_TexCoord[0].st;

    // Sample first half of texture and repeat it.
    coord.x = mod(coord.x, 0.5);
    
    // Sample second half of texture.
    vec2 coord2 = coord;
    coord2.x += 0.5;
    
    // Get result
    vec4 baseTexel = getTexelWrapped(coord);
    vec4 brightTexel = mix(vec4(0.0, 0.0, 0.0, 0.0), getTexelWrapped(coord2), map(sin(timer*2.0), -1.0, 1.0, 0.0, 1.0));
    
    vec4 finalTexel = (baseTexel*color)+brightTexel;
    finalTexel.a = baseTexel.a;
    
    return finalTexel;
}

But I end up with this nasty seam if mipmaps or linear filtering is on. texture with seam

How would I best avoid this situation? I am limited to GLSL version 120, by the way.

0

There are 0 answers