Recently, I delved into learning threejs and attempted to create a function based on the threejs.org/editor platform. The goal was to develop a function that could accurately center a selected model within the scene. However, I encountered a significant problem:
The calculation of the center of the model was incorrect when the model had been rotated, resulting in an inability to place it correctly.
Below is the code for my alignCenter function:
editor.selected.geometry.center();
var box = new THREE.Box3().setFromObject(editor.selected);
var newPosition = new THREE.Vector3(0, box.size().y/2, 0);
editor.execute(new SetPositionCommand(editor.selected, newPosition));
You can see the function in action here - - (tab Alignment - AlignCenter)
However, when the model is rotated as shown here - (with random angles, box.size().y = 105.89974660644481, position.y = 52.95), due to the inaccuracies in center positioning, it does not work correctly.
What steps can be taken to resolve this issue?
PS: Apologies for any language mistakes as English is not my native language ;(