Search code examples
three.jstextures

How to make a not squared texture fit in a "background-size:cover" way to a geometry plane in Three.js?


I want my texture to have the same behaviour than the "background-size:cover" css property.

I'd like to work with uvs coordinates. I looked at this answer and start to work on a solution : Three.js Efficiently Mapping Uvs to Plane

I try to have the same dimension/position planes that some div of my DOM. This is what I want :

enter image description here

And this is the result I get with this code : the dimension and position are good, the ratio of my texture looks good too but it seems like there's a scale issue :

            let w = domElmt.clientWidth / window.innerHeight;
            let h = domElmt.clientHeight / window.innerHeight;
            geometry = new THREE.PlaneGeometry(w, h);

            var uvs = geometry.faceVertexUvs[ 0 ];
            uvs[ 0 ][ 0 ].set( 0, h );
            uvs[ 0 ][ 1 ].set( 0, 0 );
            uvs[ 0 ][ 2 ].set( w, h );
            uvs[ 1 ][ 0 ].set( 0, 0 );
            uvs[ 1 ][ 1 ].set( w, 0 );
            uvs[ 1 ][ 2 ].set( w, h );

            tex = new THREE.TextureLoader().load('image.jpg'));
            tex.wrapS = tex.wrapT = THREE.RepeatWrapping;

            material = new THREE.MeshBasicMaterial( { map: tex } );
            mesh = new THREE.Mesh( geometry, material );

enter image description here

Should I play with the repeat attribute of my texture or can I fully made this behaviour using uvs ? Thank you


Solution

  • https://en.wikipedia.org/wiki/UV_mapping

    UV mapping values range from 0 to 1, inclusive, and represent a percentage mapping across your texture image.

    You're using a ratio of the div size vs the window size, which is likely much smaller than 1, and would result in the "zoomed in" effect you're seeing.

    For example, if your w and h result in the value 0.5, then The furthest top-right corner of the mapped texture will be the exact center of the image.

    background-style: cover:

    Scales the image as large as possible without stretching the image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no empty space remains.

    In other words, it will scale the image based on the size of the short side, and crop the rest. So let's assume you have a nice 128x512 image, and a 64x64 space. cover would scale the width of 128 down to 64 (a scale factor of 0.5), so multiply 512 by 0.5 to get the new height (128). Now your w would still be 1, but your h will be 128 / 512 = 0.25. Your texture will now fit to the width, and crop the height.

    You'll need to perform this calculation for each image-to-container size relationship to find the proper UVs, keeping in mind that the scaling is always relevant to the short side.