Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active October 18, 2019 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdesl/cbbed09cde4700f04e683cd2d0bd36b3 to your computer and use it in GitHub Desktop.
Save mattdesl/cbbed09cde4700f04e683cd2d0bd36b3 to your computer and use it in GitHub Desktop.
float2 background(float2 uv, float2 resolution, float2 texResolution) {
float tAspect = texResolution.x / texResolution.y;
float pAspect = resolution.x / resolution.y;
float pwidth = resolution.x;
float pheight = resolution.y;
float width = 0.0;
float height = 0.0;
if (tAspect > pAspect) {
height = pheight;
width = height * tAspect;
}
else {
width = pwidth;
height = width / tAspect;
}
float x = (pwidth - width) / 2.0;
float y = (pheight - height) / 2.0;
float2 nUv = uv;
nUv -= float2(x, y) / resolution;
nUv /= float2(width, height) / resolution;
return nUv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment