Nico

Dispose mesh and keep position rotation and scaling

Recommended Posts

Hi guys !

I was absent for a long time, but I am still playing sometimes with Babylon.js. There is so much new awesome and cool feature !

I came back here because I got a problem, I am trying to play with Mesh.dispose(), but in my case I want to remove a parent mesh, and keep children meshes, it work, using the parameter doNotRecurse, but it would like to do more than just keeping children meshes. I want to apply parents transformations to children meshes, in order to keep the children at same position, rotation, and scaling as they were before deleting the parent...

I know it is possible to do that by applying parent transformation to children meshes, but I don't have enough 3D / math skills to do that alone...

I know that some operation with matrix could do this, but I really don't know what to do...

If someone can explain me how to proceed it would be awesome ! :)

 

I attached an example, you can see the full scene on first screenshot, and on second screenshot I have disposed the plane.

Here is the hierarchy :

Plane
---- Green cube
---- Blue cube
---- Red cube
-------- Black cube
-------- White cube

 

Complete scene.jpg

After removing plane.jpg

SimpleSceneWithHierarchy.babylon

Share this post


Link to post
Share on other sites

Hiya @Nico, welcome back.  I have a crazy idea.  (oh no)

child.bakeCurrentTransformIntoVertices();

Do that to each child mesh... before parent.dispose(noRecurse, whatever).

No promises.  :)  Keep your fingers crossed.  Wear safety gear, could blow-up.  heh

Share this post


Link to post
Share on other sites

Hi @Wingnut !

Thanks for the answer, I tried it and here is the result.

It is better, but there is still something missing... Black and white cube (which are red cube's children) got some positionning problems...

I will look at the bakeCurrentTransformIntoVertices code to try to understand how it works, thank you for the hint ! :)

baketransform and dispose before.jpg

BakeTransform and dispose after.jpg

Share this post


Link to post
Share on other sites

Huum I think I got why it does not work for second level children, this method reset all transforms and move vertices to get the same final visual, so it work well for direct children, but for second and deeper level children, it does not work since transform have been deleted.

I have to find how to apply parent transform to a child mesh, and then dispose the parent, so I think it could work (tell me if I am wrong :huh:)

Share this post


Link to post
Share on other sites

Yeah, it was a half-"baked" Wingnut idea.  Good testing and reports, Nico... sorry it didn't work. 

I think... what you need... is a .transformCoordinates of some kind, using parent.getWorldMatrix() as your matrix.

Math.ts is full of such stuff, though I barely understand any of it.  Take a look at line 1481 in the Vector3 section

It MIGHT BE called like...  child.rotation = BABYLON.Vector3.TransformCoordinates(child.rotation, parent.getWorldMatrix()).

Then you might have to do the same for child.position, and child.scaling... both ALSO using parent.getWorldMatrix() as the needed matrix.

Again, no promises.  I'm not even sure how to apply it to your issue.  But, I believe "transform" is the secret word, here, and PG searches and github source searches... might pay-off. 

I saw some similar goods in the spotLight.ts file (search inside that file for parent), but when they do the rotation transform (if spotlight has a parent)... it is used to set light.direction.  YOU need to deal with mesh.rotation, instead.

If you need JS versions of the source, you can search in the raw JS source.

All in all, I THINK... matrices contain the big-3 info (rotation, position, and scaling), all inside that single matrix.  SO... I believe the act of parenting... is a "transform" of the parent matrix... onto the child's matrix.  Possibly, a matrix multiplication is involved in that.  (Wingnut feels-around in the dark) :)

There is a possibility that... you can avoid parenting completely... if you build some kind of system that SIMULATES parenting.  For example, if you rotated the parent, then you would iterate down thru the non-children, and multiply each of their .getWorldMatrix()... by the parent's .getWorldMatrix().  It might "act" like you have used .parent, when you didn't use it at all.  *shrug*  Not sure.

Perhaps something in my aimless yapping... will help, here.  Sorry for my incompetency on this.  I hope others reply.  Issue remains open, forum helpers.

Share this post


Link to post
Share on other sites

HI !

Thanks for the answers ! :)

15 hours ago, adam said:

Try to remove the parent from the child using:

child.setParent(null, true);

https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.abstractMesh.ts#L1828

I have tried it, and the result is the same as just call dispose(true). (I replaced the line child.parent = null;, by your code). Thank you for showing me this method, I will check the code inside to check if I can take some interesting parts ^_^

@Wingnut : I will try this as soon as possible, and let you know the result, thanks again for the hints ;)

Share this post


Link to post
Share on other sites

Another aproach could be to also make the plane a child,

e.g;
Invisible parent (box or a plane.. whatever :) )
-- Plane child
-- Green cube child
-- Blue cube child
-- Red cube child
-- Black cube child
-- White cube child

so the plane and all cubes are children of an invisible parent, dispose one and nothing will happen to any of the others :) 

Share this post


Link to post
Share on other sites

It is sound more like a hack than a solution in my case :unsure: (Because I can't edit the source file hierarchy)

For more information, I want to be able to generate a new .babylon file from an existing file with only a part of the 3D.

So I am providing a UI to show/hide meshes, and when I serialize the scene, I dispose all invisible meshes (to get a lighter file).

Normally hiding a parent but not his descendant should not happen, but I don't want to be stuck if it happens one day B)

Thanks anyway for the help ;)

Share this post


Link to post
Share on other sites
22 hours ago, Wingnut said:

It MIGHT BE called like...  child.rotation = BABYLON.Vector3.TransformCoordinates(child.rotation, parent.getWorldMatrix()).

It tried to do this for rotation, scaling and position it works well when I delete the plane (here is the PG), but it does not work when I delete the red cube... (another PG here)

If my plane position is different than 0,0,0 it does not work :wacko: (third PG...)

Delete red cube result.jpg

Delete plane with position 0,1,0.jpg

Delete plane working.jpg

Share this post


Link to post
Share on other sites
28 minutes ago, adam said:

After looking at your example and playing around with it a little I remembered that AbstractMesh.setParent only preserves rotation and position - not scale.

Great ! I tried to use the setParent method, and multiply child scaling by parent scaling and then dispose the parent and it works great ! :)

Here is the code I have to use to dispose a parent and keep the child position, scaling, and rotation in the world :

childMesh.setParent((parentMesh.parent) ? parentMesh.parent : null, true);
childMesh.scaling.multiplyInPlace(parentMesh.scaling);
parentMesh.dispose(true);

Thank you guys for helping ;)

Edit : Can any math guy can confirm if it is correct or not ?

Share this post


Link to post
Share on other sites

The solution you came up with to fix the scaling will work as long as the parent is scaled the same on all axes or if the child rotation is limited to the axis that is scaled.  Something like that. :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.