Last active
April 18, 2025 12:56
-
-
Save Longor1996/3a89c2b32311f5704daf939972671adf to your computer and use it in GitHub Desktop.
I keep loosing this function, somehow, so here it is.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note:
CopiedInspired by https://www.youtube.com/watch?v=d6tp43wZqps