0

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 :)

chaotive
  • 182
  • 14
  • How did you determine its the same matrix? It shouldnt be. – pailhead Feb 20 '17 at 22:18
  • Call `object.updateMatrix()` after resetting the scale. – WestLangley Feb 20 '17 at 22:39
  • @pailhead I determine it because I do other operations based on CSG (https://github.com/chandlerprall/ThreeCSG) that don't pick up the updated scale I want to use. – chaotive Feb 21 '17 at 01:08
  • Please do not change the question after it has been answered. The previous comments and answers now make no sense. – WestLangley Feb 21 '17 at 15:47
  • @WestLangley I thought I was just adding more information to help understand the problem better. Your previous answer actually helped me get to a solution, but it turned out in the end that the problem was more specific. – chaotive Feb 21 '17 at 16:11
  • I answered your original question. – WestLangley Feb 21 '17 at 16:18

2 Answers2

4

object.matrix is updated for you by the renderer whenever you call renderer.render().

If you need to update the object matrix manually, call

object.updateMatrix();

and it will update the matrix from the current values of object.position, object.quaternion, and object.scale.

(Note that object.rotation and object.quaternion remain synchronized. When you update one, the other updates automatically.)

three.js r.84

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thanks. In the end it seems that CSG ThreeBSP actually used the geometries of the meshes to do its stuff and not the mesh itself, that's why it wasn't working. I explain this on my reply to the post. – chaotive Feb 21 '17 at 13:52
0

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
chaotive
  • 182
  • 14