Skip to content

Instantly share code, notes, and snippets.

@moonraker22
Last active April 16, 2023 01:25
Show Gist options
  • Save moonraker22/b739aed7f836b156ec1f124f959618df to your computer and use it in GitHub Desktop.
Save moonraker22/b739aed7f836b156ec1f124f959618df to your computer and use it in GitHub Desktop.
An image cover glsl implementation
vec2 Cover(vec2 uv, vec2 screenSize, vec2 imageSize) {
vec2 s = screenSize;
vec2 i = imageSize;
float rs = s.x / s.y;
float ri = i.x / i.y;
vec2 new = rs < ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x);
vec2 offset = (rs < ri ? vec2((new.x - s.x) / 2.0, 0.0) : vec2(0.0, (new.y - s.y) / 2.0)) / new;
vec2 st = uv * s / new + offset;
return st;
}
// Use this new set of UVs to create the texture.
//
// Notice that we're using the original vUv variable here.
// That's because the 'Cover' function expect a set of UVs that
// go from 0 to 1.
// vec2 coverUV = Cover(vUv, uResolution, uTexture0Size);
// vec4 tex0 = texture2D(uTexture0, coverUV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment