Last active
January 20, 2021 02:26
-
-
Save marioecg/f98799593484d23a0cee3c17e4ec6857 to your computer and use it in GitHub Desktop.
Make texture image in a plane behave like background cover in CSS
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
vec2 backgroundCoverUv(vec2 screenSize, vec2 imageSize, vec2 uv) { | |
float screenRatio = screenSize.x / screenSize.y; | |
float imageRatio = imageSize.x / imageSize.y; | |
vec2 newSize = screenRatio < imageRatio | |
? vec2(imageSize.x * screenSize.y / imageSize.y, screenSize.y) | |
: vec2(screenSize.x, imageSize.y * screenSize.x / imageSize.x); | |
vec2 newOffset = (screenRatio < imageRatio | |
? vec2((newSize.x - screenSize.x) / 2.0, 0.0) | |
: vec2(0.0, (newSize.y - screenSize.y) / 2.0)) / newSize; | |
return uv * screenSize / newSize + newOffset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment