hi, i try using tree.js and objloader to display a 3d face but the face is off by 5 degree angle on z axis. i try to get the boundary box arround it and box is at origin but the obj inside the box is off (face is symmetrical but sitting at angle) how can i detect the coordinate of object relative to coordinate of origin thanks
Is the model at an angle by default?
This answer to this question is the object’s property .position
, however I definitely doubt that this is what you actually want to ask.
You need to figure out the orientation of the face normal, relative to the axis you want to align it to, and then construct a rotation matrix that undoes that.
I like to do these things in worldspace, so that I can just adjust the models matrix instead of transforming the geometry… since transforming geometry is often a lossy operation.
so… something like:
let worldNormal = object.localToWorld(
theVertex.clone().add( theFaceNormal) ).sub(object.localToWorld( theVertex.clone() )).normalize()
then:
let angle = worldNormal.angleTo( new THREE.Vector3(0,0,1) );
then once you have the angle, you could create a
let group = new THREE.Group()
group.rotation.z = -angle;
group.add( theMesh );
scene.add(group);
yes it is, the obj object is off by 5 degree angle, but the box around the object (new THREE.Box3().setFromObject()) is at origin
.position give angle 0, i think it calculate using the boundary box arround the object, not the object itself
object.localToWorld is object, theVertex.clone(), how do you get the theVertex variable?
You said a single face in the model?
You can use any vertex of the face. You can maybe pull it out with:
let attr = geometry.attributes.position;
let vertex = new THREE.Vector3(attr.getX(0),attr.getY(0),attr.getZ(0))
attr = geometry.attributes.normal;
let normal = new THREE.Vector3(attr.getX(0),attr.getY(0),attr.getZ(0))
edit: I see from your question in another thread that you probably didn’t mean a model with a single face… which is probably why in his infinite wisdom @PavelBoytchev definitely doubted that this is what you actually wanted to ask.
You either need singular value decomposition. (very hard math), or maybe some kind of raycast from a known location, if there is something about your meshes that is “known” like… it’s always a box or a building shape or something.
When asking complex questions, it’s good ettiquette to include all the context around why you are asking the question, rather than making people guess what you mean.
Like… “I’m trying to align drone captured models to a common reference frame” or “I have a collection of 3d generated models that I need to all face forward” or something.