fenX Posted October 26, 2016 Share Posted October 26, 2016 Hi, Is there a simple way to create instances of a mesh with its children (and grandchildren) meshes ? I have a tree made of a hierarchy of custom meshes : treeGround (juste a square ground for texture like tree roots, dirt and grass), a trunk wich has treeGround as parent, a branch wich has trunk as parent, and other branches wich are instances of the first branch and also have trunk as parent (and are moved and rotated around the trunk). When i try to create instance of treeGround i get only the ground, not the tree, same if i try to instanciate the trunk i dont get the branches. So is there a way to create an instance of the whole hierarchy ? Would cloning instead of instancing be better ? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted October 28, 2016 Share Posted October 28, 2016 Quote Link to comment Share on other sites More sharing options...
Temechon Posted October 28, 2016 Share Posted October 28, 2016 This can help hopefully : public createInstanceAsset(name:string, newname?:string) : BABYLON.AbstractMesh { var model : BABYLON.Mesh = this.assets[name]; var childrens = model.getDescendants(); if (!newname) { newname = name+'_instance'; } var mesh = model.createInstance(newname); for (let c of childrens) { var child = <BABYLON.Mesh> c; var inst = child.createInstance(''); inst.parent = mesh; } return mesh; } Quote Link to comment Share on other sites More sharing options...
fenX Posted October 28, 2016 Author Share Posted October 28, 2016 Thanks Temechon, that's the idea, I just wanted to know i there a was already a buld-in Mesh method or a parameter of createInstance to do this. It might be better to use getChildren instead of getDescendants and make a recursive call to the function to keep the same hirearchy and pass the mesh as parameter instead of the name. Now i'm trying to make this work but in my case I get an empty array when I do tree.getChildren() or tree.getDescendants(), no idea why Quote Link to comment Share on other sites More sharing options...
dbawel Posted October 29, 2016 Share Posted October 29, 2016 @fenX Here's a playground scene I found which helped me both in instancing and cloning children. It doesn't matter that the example is cloning children in a skeleton hierarchy - just modify the code as needed. However, I highly recommend that you export a .babylon file from Blender in which the parent child relationship is already set up ( check the hierarchy in Blender), then using clone instead of instance will automatically import both parent and children without any additional code. This saves me LOADS of time. DB Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 29, 2016 Share Posted October 29, 2016 (edited) @dbawel Edited October 29, 2016 by JohnK missing pg 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.