The best way of adding vegetation to the terrain?

Hey monkeys,
currently I am designing a small test map. I generated some trees on the terrain with a noise algorithm. After that I batched all trees into one geometry and added this to the node which the terrain is attached to. So it looks like this:

My question: Is there are way to add my “Trees” to the single TerrainQuads? In the end I would like to have several batch nodes but they should be added to the single TerrainQuads. Is it okay if I add the batched nodes like in the Picture or should I really add the single nodes inside those TerrainQuads?

Thanks, Domenic

Hello again, monkeys. I know, I asked my question not that clearly. To be honest, I did not really know what I really wanted :grin: I was struggling with several problems. Sorry for that!

Anyway, in order to make my vegetation I did the following.

  1. To get the single TerrainQuads I use this method of the TerrainQuad class:

     getAllTerrainPatches(holder);
    
  2. After that I went through all of the terrain quads and generated the vegetation (palms in this case). I created one node per quad and added to palm trees to that node in order to have serveral chunks and not one big geometry. I created a seperate control for that:

     @Override
     public void setSpatial(Spatial spatial) {
         if (spatial == null) {
             return;
         }
         
         for (Spatial s : ((Node) spatial).getChildren()) {
    
             if (s instanceof TerrainQuad) {
    
                 this.rootTerrainQuad = (TerrainQuad) s; // root terrain quad                
                 ArrayList<TerrainQuad> subTerrains = getTerrainQuads(rootTerrainQuad);
    
                 for (TerrainQuad quad : subTerrains) {
                     if (quad == null) {
                         continue; // should never be the case !!!
                     }
                     
                     int counter = 0;
                     
                     Node treeNode = new Node("TreeNode#" + quad.getQuadrant());
                     
                     int size = (int) (quad.getPatchSize() * 2); 
                     
                     for (int x = -size; x < size; x++) {
                         for (int y = -size; y < size; y++) {
                             
                             float value = fractalSum.value(x, 0, y);
                             float x1 = quad.getWorldTranslation().x;
                             float y1 = quad.getWorldTranslation().z;
                             float terrainHeight = rootTerrainQuad.getHeight(new Vector2f(x + x1, y + y1));
                             
                             if (counter < 500 && value < -0.6f && terrainHeight < treeLimit) {
                                 Spatial treeClone = treeModel.clone();
                                 Vector3f location = new Vector3f(x, terrainHeight, y);
                                 location.addLocal(quad.getWorldTranslation());
                                 treeClone.setLocalTranslation(location);
                                 treeNode.attachChild(treeClone);
                                 counter++;
                             }
                         }
                     }                    
                     GeometryBatchFactory.optimize(treeNode);
                     ((Node)spatial).attachChild(treeNode);
                 }
             }
         }
     }
    

With GeometryBatchFactory.optimize(treeNode); I optimized the node.
Then I attached this node to the node where the terrain is attached to.

My scene graph looks as follows:

The scene looks like this:

Sorry again for my bad post in the beginning!

Create a node. Add the terrain to it. Add your vegation to it. Add the node to your scene. So now, because of the parent child relationship. When you remove that node, the terrain and vegetation will leave together.

Think is-a and has-a relationship. Is a node. Has a terrain. Has some vegetation.

Edit: sorry for the mega bump. Was on the front page :o

jay, that’s what he has done, he’s wondering how to batch them

but anyways it looks like he has done it.

Just wondering, anyone knows what would be the performance difference between having the trees part of the terrrain geometry and using separate trees, but use the new InstancedNodes?

Instancing is not as efficient as batching, performance-wise. It’s a lot more efficient memory-wise.

Even though it’s only one draw call (a win), instancing still has to do per object setup inside the driver and that’s not free.

Still, for objects of a certain size the overall gain (from reduced memory) makes up for the small loss in performance over batching. And either way it’s better than separately managed objects.

2 Likes