Custom LOD method massive FPS drop?

I have created a custom LOD method that checks each item inside of a Node for its distance relative to the camera, and if the Distance between the camera Position and The object position Exceeds the LOD distance, the mesh should be changed into the LOD version. It removes the current child form teh node, and replaces it with the new geometry at that location. (That was the only way i knew how to change the mesh).

Now when i run this, it causes my framerate to drop down to 8fps, and the LOD mesh does not appear…

[java] public void checkLods() {
//LOD CONTROL
for (int i = 0; i < treeNode.getQuantity(); ++i) {
if (treesAr.get(i).getLocalTranslation().distance(cam.getLocation()) > LOD_dist && !treeNode.getChild(i).getName().contains(“Lod”)) {
treeNode.getChild(i).removeFromParent();
treeNode.attachChildAt(Trees.tree_Lod.clone(), i);
treeNode.getChild(i).setShadowMode(RenderQueue.ShadowMode.Off);
} else {
treeNode.getChild(i).setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
treeNode.getChild(i).removeFromParent();
treeNode.attachChildAt(Trees.tree.clone(), i);
}
}
}

Yeah, that’s a pretty inefficient loop and probably not at all doing what you want. It’s hard to say what it’s supposed to be doing though since I read it and it’s full of contradictions even just reading it logically:

“go through every index of treeNode’s children”
“but actually look at treesAr’s children for an expensive distance check to camera… but only if the tree node’s same child doesn’t contain Lod”
if true : remove from parent and attach some other node
if false : remove from parent and attach some other node

So every update() you go through all of treesAr and treeNode’s children and remove and readd them. Never mind the sqrt and string look-up every time… the remove/attach is going to be slow enough as is.

Why not just attach everything and turn things on/off with cull hints?

Also, how many trees are we talking? Is each child a single tree?

That will be fine for 100-200 trees maybe. Anything more than that and you should really use a different approach.

If the trees are a custom mesh (or a model saved with multiple LoDs), you could swap out the index buffer for a simplied/more complex version based on distance and save yourself the .removeFromParent() and node.attachChild© calls, which can tend to stutter the app depending on the size of the model being added.

I have around 800 Trees in my game. SO this approach is not ideal?

Doing this, with simply the Change in the shadow mode, works beautifully. So i’m not sure why the remove and add new Object isn’t working. But as you have stated, its very inefficient for too many trees, however i have 800+

HOWEVER: The trees in question have around 10 Triangles each, so they are already very low poly.

The Lod reduces them to 4.

@TastyLemons said: I have around 800 Trees in my game. SO this approach is not ideal?

Doing this, with simply the Change in the shadow mode, works beautifully. So i’m not sure why the remove and add new Object isn’t working. But as you have stated, its very inefficient for too many trees, however i have 800+

HOWEVER: The trees in question have around 10 Triangles each, so they are already very low poly.

The Lod reduces them to 4.

There is little reason to do any LOD at all in that case. ALL of your performance issues are related to 800 objects.

Just batch them instead (no LOD) and you should see your performance improve dramatically. Even better if you organize and batch them spatially.