I want to dynamically move glb model to the center position where i have created box

I want to import a glb model into my scene, however by default the model is not in the scene’s centre. Is there a way to automatically centre the imported model without manually entering its position?
just like creating a box is positioned in the center?

Here is the playgroun

Thank you.

Hi,
Yes. It’s about getting the hierarchy bounding box and make a new center from there.
Something like this:

Edit: to reposition the center of mesh on the center of cube on the Y axis, use the commented ‘Ybottom’ scale and apply this to your ‘y.position’ (uncomment lines 34 and 38 in this PG)

Edit1: Forgot to mention a thing that might be important to you. If you want to further interact with this object such as i.e. rotate it from the new center, you will need to parent it to a transform node and use this transform node for translation/rotation. If you click on the mesh node (parent) you can see that the values of position are not a zero world origin. That’s because the object position has been recalculated from the bounding box BUT the pivot has not changed. Hope this all helps. Else, let me know and meanwhile, have a great day :sunglasses:

1 Like

Hello @mawa Im really thank full for your support

I have one more question.
I want to load two seperate models with the different sizes how i can equal both model in terms of size dynamically. so that they look same size in 3D view?
I want to reduce the large model size equal to small model

This is also commented in the code of this PG (lucky you :smiley:)
The function is called ‘normalizeToUnitCube’. What it does is that it scales the bounding box hierarchy of an imported mesh to fit inside a ‘unitcube’ (a cube of 1,1,1 units).
Next you can use ‘scaleInPlace’ to scale up the model again from this unitcube (uncomment lines 25 and 27 in PG). What it does in the end is that all your objects will have the same size/scale, upscaled from the normalized 1,1,1 unitcube.

EDIT: I again forgot an important note here. May be you realized that ‘computeWorldMatrix(true)’ is called twice in this scenario. The thing to remember here is that getting the bounding box does not call on computeWorldMatrix first. So if you upscale your model without computing, the new bounding box will be incorrect or better said, the new scale of bounding box will not be accounted. Same goes with ‘normalize’.

2 Likes

Yes im really happy.
loadedmesh.normalizeToUnitCube();
Is this only function to uncomment?

Yes, as I said, unless you want to scale it up again. In which case you can scale it on the fly (with ‘scaleInPlace’ - uncomment line 27) or you can still change the scale at anytime afterwards.

2 Likes

Thank you so much for great explanation. :smiley:

1 Like