Making Instanced Geometry based on Imported 3D Environment

Hey guys :smiley:
So after some fighting, I figured out how to make foliage in Blender with it’s particle hair system. I have an exemplary tree:


All it’s leaves are the same triangle, with varying rotation and scale. Currently I got them merged for export, and they are only 300 tris so it’s not too bad. However it might be troublesome if I had hundreds or even thousands of those trees with 300 leaves each. That’s why I’d like to optimize it further and make them (the leaves) an InstancedBufferGeometry mesh. As far as I know, its not possible to export that to GLTF (with blender at least), so they would probably need to be constructed during the loading phase in the app.
Most primitive way of acchieving that would probably be to export them as single objects, loop through each of them and copy each of their offset, rotation and scale to a new InstancedBufferGeometry, making a new better mesh. All that sounds extremely sketchy to me though and I suspect there should be a better way.

Is there some recommended workflow for this?
Thanks in advance :blush:

1 Like

This is basically the way you do it, though you will have to disable culling (frustumCulled to false on the mesh) then since instanced geometries aren’t culled anymore. To save memory you could place null-objects in blender as placeholder and give them a name for when you traverse through the scene on the import.

If you’re interested, im very soon releasing a IndexedScene with auto-instancing that also keeps culling, you find some information here. It helps to render very large and rich scenes you couldn’t render with a regular scene.

3 Likes

As far as I know, its not possible to export that to GLTF (with blender at least), so they would probably need to be constructed during the loading phase in the app.

Nope, glTF can reuse the leaf geometry so there’s only one copy of the mesh, but it’d still be multiple draw calls unless you manually set up the InstancedBufferGeometry during the loading phase.

I might suggest keeping your trees simple (1 non-instanced mesh) and doing instancing at the per-tree level, rather than per-leaf. Probably it’s easier to place trees dynamically than leaves.

Here’s a bit of code I’ve used for creating an instanced mesh from a loaded glTF mesh:

… this assumes you’ve only got one mesh in the model, which will be simpler. I’m using node materials to manage instance positions in that case, but a custom shader would work just as well. (demo)

4 Likes

Thank you both for great suggestions :hugs: