How to fit the texture to the plane

I’m trying to fit the texture to the plane, someone knows how to do it, I want to do the same result as background-size: cover in CSS

Try it with this code:

texture.matrixAutoUpdate = false;

var aspect = window.innerWidth / window.innerHeight;
var imageAspect = texture.image.width / texture.image.height;

if ( aspect < imageAspect ) {

    texture.matrix.setUvTransform( 0, 0, aspect / imageAspect, 1, 0, 0.5, 0.5 );

} else {

    texture.matrix.setUvTransform( 0, 0, 1, imageAspect / aspect, 0, 0.5, 0.5 );

}

Live demo: https://jsfiddle.net/3k8ah1py/

1 Like

Thank you! you are a genius :slight_smile:

2 Likes