2

Hi I am developing an application using the viewer api. I have a master model. And I load other models on this model. I can move and resize these models that I have uploaded later. I achieved rotation using makeRotationX ,makeRotationY,makeRotationZ functions. But when I rotate the loaded model, it moves it to the starting point. What could be the reason for this? So for example, I add the cube to the left, but it moves to the main 0,0,0 point and does the rotation. first

I just change the rotation Y value and this is the result. second

Code :

cubeModel.getModelTransform().makeRotationY(inputValue * Math.PI / 180);
                viewer.impl.invalidate(true, true, true)
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
hamdi
  • 67
  • 2
  • 10

1 Answers1

3

This is because the model's THREE.Matrix4 transform that you retrieve using model.getModelTransform() may already include some transformation (in this particular case, the transformation is offsetting the red cube to the left). And the makeRotationY method doesn't append any transformation, it simply resets the matrix to a new transformation that only rotates around the Y axis.

Instead, what you'll want to do is something like this:

let xform = model.getModelTransform().clone();
// modify the xform instead of resetting it, for example
let rotate = new THREE.Matrix4().makeRotationY(0.1);
let scale = new THREE.Matrix4().makeScale(0.1, 0.5, 0.1);
xform.premultiply(rotate);
xform.multiply(scale);
// since we cloned the matrix earlier (good practice), apply it back to the model
model.setModelTransform(xform);
Petr Broz
  • 8,891
  • 2
  • 15
  • 24