Search code examples
javascriptthree.jscsgthreecsg

Three.js: how to force update matrix after cloning? (to use with CSG ThreeBSP)


I'm trying to clone and then scale a mesh, but scaling does not seem to be working immediately on the cloned object, for programming purposes using CSG ThreeBSP. I think I should call a function after the scaling to force the matrix or other internal variables to recalculate immediately and not to wait for the full update loop on render side.

My code looks something like this:

var someMesh2 = someMesh1.clone();
someMesh2.scale.set(2,2,2);
someProgrammingOperation(someMesh2);
//It turns out that internally, someMesh2 still has the same properties (matrix?) as someMesh1 :(

What am I missing? Suggestions are also welcomed :)


Solution

  • In the end, my problem was that the CSG ThreeBSP object needed to work based on the Geometry of the object, not in the Mesh itself. I applied the scaling on the Geometry and it worked as expected.

    There is a caveat though, that one should be careful as with the meshes and geometries instances, therefore is needed to do some cloning in order to keep the original objects as they were, as in the following example:

    var clonedMesh = original.mesh.clone()
    var clonedGeometry = clonedMesh.geometry.clone()
    clonedMesh.geometry = clonedGeometry
    clonedMesh.geometry.scale(2,2,2)
    
    var someBsp = new ThreeBSP( clonedMesh )
    
    var newMesh = someBspBsp.toMesh()
    someScene.add newMesh