I used the quick tree generator for creating random trees. But when I tried to create an instance of the tree (in order to clone them), I get the error: Instances should only be created for meshes with geometry. Is there any way to give the mesh a geometry? Or are Instances used only for simple shapes like plane, spheres, etc ?
Hiya S… sorry for the slow replies. Here ya go:
https://www.babylonjs-playground.com/#LG3GS#204 (see lines 117-120)
Want to know how/why error was happening? If so… please load https://www.babylonjs-playground.com/#LG3GS#107 into a separate browser window… so we can easily compare playground #107 and #204.
Ready? Here we go:
-
Notice line 28… in PG107.
var tree = new BABYLON.Mesh("tree", scene);
That line… is creating a “blank mesh”… no geometry. It is being used as a “tree parent”… a “thing” to parent the trunk and leaves… onto. That’s the line that gives the error. Also, these trees are each 3-pieces, which makes them more difficult to “instance”. We will fix that problem… with a MergeMeshes… soon. -
Notice line 27 of pg-204. This time, we use an invisible plane… instead of a blank mesh. No more errors about geometry-less mesh. I also re-named to treeparent… in pg-204.
-
Now look at lines 108-110 of pg-204. I ADDED line 110… which is a multi-multi-material… mesh-merger. What a name, eh? You can read all about mesh-merging… here. Most important part… is that we merged the treeparent and the leaves and the trunk… into a SINGLE multi-material mesh. It is returned from the tree generator… as a single mesh called mTree.
-
Now that we have a SINGLE-mesh tree (cuz we merged the 3 tree-pieces - treeparent, trunk, and leaves)… we can do easier and error-free createInstance()… as seen in pg 204… lines 116 area.
That MergeMeshes line is important and powerful. MAYBE… that MergeMeshes would still work ok… if treeparent remained a blank mesh. I didn’t test it. When I changed the blank mesh… into being a plane instead… I added SOME vertices to the tree (4? 8? 12?). That COULD affect scene performance a small amount.
So… there may be wiser ways of doing this tree-merge. Let’s listen for more comments from smarter people than I… see what others can teach us.
Meantime, you can move your project forward… using the QuickTreeGenerator function from pg-204. If future helpers teach us how to “trim fat” from 204’s QuickTreeGenerator(), then later you can make changes to your project, too.
I hope I have been helpful. Let’s keep listening. Somebody might know how to make a treeparent that has NO added vertices at all, yet still doesn’t cause that error. Party on.
PS: By using instances… you cause each tree to have the same shape. So, you may want to randomly rotate each new tree around its Y-axis… to keep trees looking somewhat different from one-another. Check PG-206 where I built a badly-coded func called rotval()… and see me call it on the new instances… lines 125 and 129. It helps make the trees look different from one-another. Randomly scaling them (adjusting their sizes)… might be useful, too.
Thanks. This really cleared a big problem I was facing. Excellent post. I’ll look up more about MergeMeshes.
I have one more question regarding creating instances for imported meshes. I tried using the method shown here. But I’m getting the same error (Instances should only be created for meshes with geometry). Is there any way to solve this?? How to use the MergeMeshes method here??
I’m not real sure about that. As your imported models become more complex… they might start being difficult to multi-material mesh-merge. I don’t have a lot of experience with that, but I suspect that the same issues (same as the tree merge/instancing) are true, for other models.
Many models have a root node. Maybe its a transform-type node, maybe a blank mesh, but it rarely has geometry, I suspect. Same thing as before… you might need to replace that “root” node (often a master parent)… with an invisible plane or similar.
Can you install the BabylonJS Scene Inspector into your project? That will let you EASILY examine the hierarchy of the imported mesh. There you will be able to see… which/how-many sub-mesh are involved, and you can try to replace the root node… with a plane… getting it ready for a MergeMesh() call.
I might be wrong, about this. I have almost ZERO experience with importing models and getting them ready for createInstance(). Let’s keep listening for other comments. I’m scared that I’m telling you stuff that is incorrect.
Also, many models are in ascii files… sometimes json format (.babylon)… and can be examined with basic editor or with an online JSON file-viewer. You can sometimes see the names of the subMeshes… and see if they have a geometry-less, central-parent-gizmo root node… and other interesting things.
Try to get the BJS Scene Inspector activated… on home project, or try to import your models into a playground scene (our playground has the inspector built-in). Bring your model into a scene that has an inspector, examine the nodes. Learn the structure of your models… find their root nodes, gather the names of all the children of the root, make a plane, and start re-parenting the children of root… to be the children of YOUR plane.
Get that done right, where you can position, rotate, or scale your plane, and the entire model does the same thing… perfectly. THEN you are ready for a mesh-merge. If that is successful, then you are ready for createInstance attempts.
I hope I’m not telling you wrong things. Other helpers… please… comment at will… yell at me, as needed.
Another user is having a similar problem: Here
I tried something similar. Selected the Transform Nodes and then made them the children to my invisible plane. But it isn’t working.
Yeah, I saw that… good catch and link. I almost pasted a link to this thread… over in that/those threads… a little while ago.
Sorry to hear you are having problems… but don’t be down on yourself about it. Such a task is not an easy thing to do… and I sure didn’t give you much help/info. (sorry)
I sort of like what @sebavan said over there… maybe insinuating that we need a “special” mesh-merger tool… that “processes” imported models… attempting to get them merged-up and ready for mergedMesh.createInstance() calls.
I’m not experienced enough to see the far-impacts/problems in building such a tool… but others nearby, are. Let’s hope they are planning and plotting some fresh ideas.
Thanks for looking it up. I’m downloading my models from Sketchfab. Is there any other website that provides free gltf models? Maybe they have already merged nodes.
As I mentioned in the other post, you cannot instantiate nodes with no geometry because an instance MUST have a source with a geometry to work. It is a GPU feature that will duplicate an original geometry
So you cannot instantiate something with no geometry. For them I recommend using clone().
What I also recommend is to use mesh.instantiateHierarchy()
. It will deal automatically with createInstance / clone problem for you (beware though I just found that I did a typo and this function will REALLY be available with the next nightly)