Skip to content

Instantly share code, notes, and snippets.

@Longor1996
Last active April 18, 2025 12:56
Show Gist options
  • Save Longor1996/3a89c2b32311f5704daf939972671adf to your computer and use it in GitHub Desktop.
Save Longor1996/3a89c2b32311f5704daf939972671adf to your computer and use it in GitHub Desktop.
I keep loosing this function, somehow, so here it is.
// vec2 uv: XY=[0..1]
// vec4 size: XY=1.0/TEXSIZE, ZW=TEXSIZE.
// sampler2D samplr: MIN,MAG = LINEAR
vec4 pixel_perfect_sample(vec2 uv, vec4 size, in sampler2D samplr) {
vec2 boxSize = clamp(fwidth(uv) * size.zw, 1e-5, 1.0);
vec2 tx = uv * size.zw - 0.5 * boxSize;
vec2 txOffset = smoothstep(1.0 - boxSize, vec2(1.0), fract(tx));
vec2 finaluv = (floor(tx) + 0.5 + txOffset) * size.xy;
return textureGrad(samplr, finaluv, dFdx(uv), dFdy(uv));
}
@Longor1996
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment