First, the context: I’m trying to re-implement a 3D L-system generator that could eventually reproduce the constructs described in “The Algorithmic Beauty of Plants”. The underlying graphics implementation is a mix of turtle graphics with polygon and more complicated meshes spliced in. Second, I’m a JavaScript rookie, so bear with my code.
I have a working preliminary 3D turtle implementation and have run into performance problems with the number of meshes that it creates: tens of thousands of short, connected tubes are child’s play for an L-system to generate, before considering leaves and flowers. I’ve read many of the suggestions to use SPS to deal with this, but that is a bad fit for L-systems. Instances seem to be next in line, but also suffer from problems wrt to color and texture management.
So, my first attempt was to merge meshes as soon as they are generated and that is what the playground below demonstrates, but it is also painfully slow (5 or more minutes to complete). This creates 3600 tubes, immediately merging the newest with the previous mesh, ending with 1 mesh. Is there a way to optimize this? I’ve thought about creating an array of segments and merging in larger batches - will that help or is it just an issue of vertices?
If I can’t make this work, I’m thinking of trying to generate a path and create a single tube/extrusion per branch all at once. For a plant, this would still result in hundreds to tens of thousands of meshes. Again, less of a conceptual fit, but close.
the code snippet of interest is here, where 'segment is a newly created tube:
if (this.TurtleState.TrackMesh == null) {
this.TurtleState.TrackMesh = segment;
this.meshCount[1]++;
} else {
this.TurtleState.TrackMesh = BABYLON.Mesh.MergeMeshes([this.TurtleState.TrackMesh, segment],true, true);
}
this.TurtleState.TrackMesh.id = t;
BABYLON.Tags.AddTagsTo(this.TurtleState.TrackMesh, tag, scene);
https://playground.babylonjs.com/#WJD8Q7#2 - be prepared to wait several minutes