简体   繁体   中英

how to clone three.js geometry?

I'm trying to calculate object volume based on a scale value passed from a form.
I want to calculate the scale from the start geometry every time so my problem is that when i update my geometry then at the next value change the scale is done on the updated geometry.
i was thinking that could be ok to clone the geometry and apply the scale only on the cloned one, so every time i change the scale value i'm able to start from original geometry but i don't know how to do it.

That's my code, any suggestion?

$(document).on('change', '.scaleBox', function (e) {
    if (idfile == $(this).attr('idgl')) {
        var val = $(this).val();
        if (val <= 0)
            val = 0.1;

        myObj.scale.set(val, val, val);            
        myObj.updateMatrix();
        myObj.geometry.applyMatrix(myObj.matrix);
        myObj.matrix.identity();
        myObj.geometry.verticesNeedUpdate = true;

        var volume = myVolume(myObj.geometry);
        $("#specs-" + idfile).find('.volumeVal').html(myRound(volume, 3));     

        var box = new THREE.Box3().setFromObject(myObj);
        xyzSizes = box.size();
        $("#specs-" + idfile).find('.sizesVal').html('x: ' + myRound(xyzSizes.x, 3) + ' y: ' + myRound(xyzSizes.y, 3) + ' z: ' + myRound(xyzSizes.z, 3));
    }
});

I found a solution by reversing the scale factor and re-updating the geometry. Hope this could help someone else
This is the result:

 $(document).on('change', '.scaleBox', function (e) {
    if (idfile == $(this).attr('idgl')) {
        var val = $(this).val();
        if (val <= 0)
            val = 0.1;

        myObj.scale.set(val, val, val);
        var box = new THREE.Box3().setFromObject(myObj);
        xyzSizes = box.size();
        $("#specs-" + idfile).find('.sizesVal').html('x: ' + myRound(xyzSizes.x, 3) + ' y: ' + myRound(xyzSizes.y, 3) + ' z: ' + myRound(xyzSizes.z, 3));
        myObj.scale.set(val,val,val);
        myObj.updateMatrix();
        myObj.geometry.applyMatrix(myObj.matrix);


        var volume = myVolume(myObj.geometry);
        $("#specs-" + idfile).find('.volumeVal').html(myRound(volume, 3));
        myObj.scale.set(1/val,1/val,1/val);
        myObj.updateMatrix();
        myObj.geometry.applyMatrix(myObj.matrix);


    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM