Display model in the center

yeah by mistake i gave the wrong link this is the propper link of that implmentation

1 Like

@mawa were you able to rectify the error that i make in the pg

No. I was just waiting on @nogalo. Itā€™s his solution and, as I said, not really my cup of tea, sorry.
If you have no answer or solution by tomorrow, I can try take some time to have a closer look at it but right now I canā€™t (itā€™s already nearly time for me to cook dinner and I have just too many other things to attend to). Sorry. Hope it wonā€™t be too long before you get your solution.

Why not using this ? Camera Behaviors | Babylon.js Documentation

1 Like

Oh yes, why not? Completely forgot about this.
By any chance, You wouldnā€™t be able to quickly attempt implementing this in the PG above for a trial? @Ashish_Abhiwan is a dedicated user but not the best at digging into code and doc (hope Iā€™m not offending you when saying this, and then actually, Iā€™m also a designer and mostly like things straight forward and ez :grinning:

well i am not offended, i will try to implement it

1 Like

@sebavan @mawa i implemented it but recived error how can i do that on impoted model

Thanks well i did it i made some error i fixed that

2 Likes

The error comes from that you are targeting ā€˜newMeshesā€™ but the camera can only target a single mesh.
Here (below PG) Iā€™m targeting mesh[0] (the first mesh in the hierarchy). However, Iā€™m afraid that this will just not entirely solve your issue. Itā€™s still a good start I supposeā€¦

Yes. As I said, a good start. It will however not change the fact that if you want to center and focus on different parts of the model, you will still need to implement this part. Hope youā€™ll figure it out from the above. Else, let us know and meanwhile, have a great day :sunglasses:

well thanks for the help. have great day :sunglasses:

Here is the working playground (to be honest, I just copy pasted everything from the existing Playground I posted in the above mentioned thread). The reason why your playground is not working is because you didnā€™t implement anything that I mentioned in the comment and in the existing playground (fully commented on each step). :smiley:

Basically the logic is, you use the provided method totalBoundingInfo, which basically loops through every mesh in the scene, looks at the boundingBox values and defining the most minimum and most maximum point of whole scene (meshes only).

Once you have that info, you set that bounding box to any of the meshes in the scene (meshes[0] lets say). And now you have bounding box for your object.

Next step is to create a parentBox that will act as parent of every mesh in the scene, the size of the parentBox is the size of the biggest dimension of the newly created boundingBox.

You can position the box at (0,0,0), meaning that no matter what object you load the position will always be at (0, 0, 0), where you camera already looks at.

After that you call normalizeToUnitCube which will resize your object to 1x1x1 space.

Thats it.

What you achieve with this is. Your object will always be at (0,0,0) and your object will always have same size relative to camera. So no matter how big your object is or where in space it is positioned, it will always behave the same, so you dont need to worry about the incoming mesh at all.

4 Likes

This is so helpful! I had tried to do import and display with three.js, and got completely stuck. With this example you saved me potentially days of work. Itā€™s just what I needed to get started with a project of mine. I have models of various sizes that all have one bound aligned with the origin, and this will help make things consistent with them all. Thank you so much!

3 Likes