Nico Posted April 26, 2017 Share Posted April 26, 2017 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 SimpleSceneWithHierarchy.babylon Quote Link to comment Share on other sites More sharing options...
Wingnut Posted April 26, 2017 Share Posted April 26, 2017 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 Quote Link to comment Share on other sites More sharing options...
Nico Posted April 27, 2017 Author Share Posted April 27, 2017 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 ! Quote Link to comment Share on other sites More sharing options...
Nico Posted April 27, 2017 Author Share Posted April 27, 2017 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 ) Quote Link to comment Share on other sites More sharing options...
Wingnut Posted April 27, 2017 Share Posted April 27, 2017 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. Nico 1 Quote Link to comment Share on other sites More sharing options...
adam Posted April 27, 2017 Share Posted April 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 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 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted April 28, 2017 Share Posted April 28, 2017 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 Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 It is sound more like a hack than a solution in my case (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 Thanks anyway for the help Quote Link to comment Share on other sites More sharing options...
adam Posted April 28, 2017 Share Posted April 28, 2017 Can you create a PG of your issue? Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 8 minutes ago, adam said: Can you create a PG of your issue? Yep for sure, I will do this as soon as possible Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 Here is the PG : https://www.babylonjs-playground.com/index.html#MG4WD5 I had to put the .babylon file content inside in order to load the 3D (there are not classic BABYLON boxes). After 5s I dispose the plane mesh (parent of all mesh). Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 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 (third PG...) Quote Link to comment Share on other sites More sharing options...
adam Posted April 28, 2017 Share Posted April 28, 2017 After looking at your example and playing around with it a little I remembered that AbstractMesh.setParent only preserves rotation and position - not scale. Wingnut and Nico 2 Quote Link to comment Share on other sites More sharing options...
Nico Posted April 28, 2017 Author Share Posted April 28, 2017 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 ? Wingnut 1 Quote Link to comment Share on other sites More sharing options...
adam Posted April 28, 2017 Share Posted April 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.