ranagraw

Parenting a loaded mesh rotates it

Recommended Posts

I am using the `AssetsManager` to load a glb file to my scene. I am basically replicating this: https://playground.babylonjs.com/#Y7XMAR#0

Now, after the glb is loaded, I give it a parent so that I can manage the overall mesh's position/rotation. Here's what I have done: https://playground.babylonjs.com/#Y7XMAR#1

This technique works well for most meshes, except some where it rotates the mesh in a weird way or moves the individual components around. I wasn't sure how to upload a custom mesh on to the playground, so I have the glb file on this url: table_tennis.glb

Any idea why this could be happening? 

Share this post


Link to post
Share on other sites

Hiya ranagraw!  Umm... first... foreach needs to be forEach... I think.  It seems so.

https://playground.babylonjs.com/#Y7XMAR#2

There, I've done some fiddling-around in lines 18-28.

SOME meshes will have values on their .rotationQuaternion or their .rotation properties... when they arrive.  IF you parent them to something, they WILL rotate to match the (new) parent... because parents hand-down their .rotation/.rotatationQuaternion values... to children.

In SOME models, such as this skull, there is only one mesh.  In other models, there are many mesh... and often one "root" mesh at the "top".  But "top" is not easy to find, when you load multiple models in a single scene.

In complex models, with many "sub-mesh"... those sub-mesh are VERY OFTEN parented to other sub-mesh within the model.  SO, when you forEach thru all the loaded mesh, and parent them to a master parent, that will totally mess-up a complex model.

What you CAN do... is somehow "find" the root mesh of each model... and ONLY parent THAT to your master parent.  scene.getMeshByName() might be a great way to ONLY get the "root" mesh of any model.  THAT'S what you want to parent... but IF that root mesh has a .rotation or .rotationQuaternion value when it arrives in the BJS scene, then parenting it to something COULD rotate it.

You COULD make the parent.rotation or .rotationQuaternion... an exact copy of the modelroot.rotation or .rotationQuaternion.  THEN setParent, and you should see no rotational change.

Kind of complicated, eh?  It gets easier with practice, I promise.  :)

Notice lines 26-27.  It refuses to set a new parent... on any meshes that already have a parent.  This may help SOME, but... think about ALWAYS somehow finding the "root" to any model... and only parenting THAT to your master parent. 

In some cases, you may find that you don't need a master parent in a scene... because ALL complex multi-submesh models... ALWAYS have a "root"... and it is often named, so... it is easy to "look-up" in the scene, using scene.getMeshByName(somename).

I hope I have been helpful.  Stay tuned... others might comment soon.

Share this post


Link to post
Share on other sites
12 minutes ago, Wingnut said:

Hiya ranagraw!  Umm... first... foreach needs to be forEach... I think.  It seems so.'

Yep, you're right! My bad.

12 minutes ago, Wingnut said:

https://playground.babylonjs.com/#Y7XMAR#2

...

Kind of complicated, eh?  Notice lines 26-27.  It refuses to set a new parent... on any meshes that already have a parent.  This may help SOME, but... think about ALWAYS somehow finding the "root" to any mesh... and only parenting THAT to your master parent.  In MANY cases, you may find that you don't need a master parent in a scene... because ALL complex multi-submesh models... ALWAYS have a "root"... and it is often named, so... it is easy to "look-up" in the scene, using scene.getMeshByName(somename).

Perfect! This is exactly what it was. 

This fixed all the issues I was having. Also, this is a great piece of information and it would be great if this can go on the official documentation somewhere (that is, if it's not already there and I skipped past it 😐).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.