Adding mesh to rootNode lags the game

I haven’t been able to find anything on this (but have sworn there WAS a topic on it).
When I go to attach a mesh to the rootNode on runtime it causes the game to lag for a moment (3 or 4 seconds). I think this has something to do with a lack of thread implementation, but I wanted to make sure that was it (as I’ve seen countless posts claiming that people might use threads for something it doesn’t need to be used for). I don’t have access to any example code, but I’ll try to recall exactly how I went about it.

If certain conditions were met in the simpleUpdate method I would have it put a mesh on the rootNode. It was a decently large mesh but it would lag only while it was being placed on the rootNode.

Any idea on what’s happening and how to remedy the problem?

Are you loading the asset at the same time?

If so, this is what is causing the lag.

anything you do in the update loop will “lag” the game. none of the update() calls happen simultaneously. the loop goes through and calls update() one at a time on each appstate and control within the same thread.

to get around this use the thread pool executor to do everything along the lines of asset.loadModel() outside of the game loop, then once its loaded use app.enque() to attach the model to the rootnode. (make sure you dont attach anything to the rootnode from your loading thread)

You can also preload the mesh when game starts… (Note preload on the gpu not only into ram)

When you folks are referring to the “game loop”, you’re talking about the simpleUpdate, right? Should I use simpleRender instead for attaching meshes?

The mesh is a custom model made with vertices, etc. all through the code; it is not an asset.

@mjbmitch said: When you folks are referring to the "game loop", you're talking about the simpleUpdate, right? Should I use simpleRender instead for attaching meshes?

The mesh is a custom model made with vertices, etc. all through the code; it is not an asset.

It’s all the render loop, game loop, etc… all the same.

How long does it take to make your mesh? You may want to do that on another thread.

@pspeed said: It's all the render loop, game loop, etc... all the same.

How long does it take to make your mesh? You may want to do that on another thread.

I thought I couldn’t alter the rootNode from a different thread?

You can, but you need to wrap the actual update into a code snippet and submit that to the update loop.
See the very first section of https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading .

Well, that’s misleading. He’s right that you can’t alter the rootNode - you are telling him how to move the alteration onto the correct thread.

You can load and modify a model from any (single) thread you like - so long as the final attach goes onto the render thread.