Created
September 10, 2024 21:51
-
-
Save sortofsleepy/579eb4ec95da823484ee4fe9b0409d7a to your computer and use it in GitHub Desktop.
glsl spritesheet helper
This file contains 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
// A helper to sample a section of a texture. useful for things like spritesheets. | |
// texCoord - uvs of the surface you're rendering to | |
// imageSize - the size of the patch you want to sample | |
// fulLTextureSize - the full dimensions of the texture you're sampling from | |
// imageCoord - the x/y value of the section to sample from. | |
// Sourced from @Gabor on Disccord in the @creativecode server. | |
vec2 sample_section( | |
vec2 texCoord, | |
vec2 imageSize, | |
vec2 fullTextureSize, | |
vec2 imageCoord | |
){ | |
return imageCoord / fullTextureSize + imageSize * texCoord / fullTextureSize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another user named Simon suggested the following as well
color = texture( uSampler, uTextureParams[texId].xy + uTextureParams[texId].zw * normalizedUvs )
where in uTextureParams, xy is the translation and zw is the scale, though I haven't gotten this to work yet as I didn't ask for clarification on what he meant by normalizedUvs(as uvs are normally already normalized)