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

1 Like

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: Edit fiddle - JSFiddle - Code Playground

4 Likes

Thank you! you are a genius :slight_smile:

2 Likes

4 years later and this just worked for me :pray: