Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Created September 10, 2024 21:51
Show Gist options
  • Save sortofsleepy/579eb4ec95da823484ee4fe9b0409d7a to your computer and use it in GitHub Desktop.
Save sortofsleepy/579eb4ec95da823484ee4fe9b0409d7a to your computer and use it in GitHub Desktop.
glsl spritesheet helper
// 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;
}
@sortofsleepy
Copy link
Author

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)

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