Wrap texels between desired values

by Maverick   Last Updated July 12, 2018 01:13 AM

I'm using a basic pixel shader here

uniform sampler2D texture;
uniform float pixel_threshold;

void main()
    float factor = 1.0 / (pixel_threshold + 0.001);
    vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
    gl_FragColor = texture2D(texture, pos) * gl_Color;

It works great in my game. But on a new enemy it doesn't. This enemy is different because it uses a texture atlas- one big texture holding more frames than just the character. The sampling gets messed up because factorapproaches 0 so does the xy coord therefore sampling some pixels outside of the character's sheet. Likewise when pixel_threshold gets small, the xy coord goes outside of the character's spot in the texture.

Clamping is not the solution because we want it to wrap around. I've tried many ways of wrapping but not seem to offer the desired effect of the original.

I tried using 4 uniforms: left,top,width,height and passing in the masking values used when drawing the sprite from the spritesheet.

vector2 size = mySprite.getSize();
float left = mySprite.getTextureRect().left / size.x;   
float top  = mySprite.getTextureRect().top / size.y;
float width= mySprite.getTextureRect().width/ size.x;
float height=mySprite.getTextureRect().height/size.y;

myShader.setUniformf("left", left);
myShader.setUniformf("height", height);

How can I have the pixel blur shader stay within the texel ranges provided by the uniform? Thanks.

Related Questions

GLSL - skewed parallax occlusion mapping

Updated September 10, 2017 01:13 AM

Toon/cel shading with variable line width?

Updated November 15, 2018 15:13 PM

2D day night mapping

Updated May 25, 2015 22:05 PM

Opengl 4.3 - GLSL Bump mapping problem (Normal mapping)

Updated September 08, 2016 08:05 AM

Raymarching tangents

Updated August 10, 2015 17:05 PM