Is it safe to create mesh and geometry in an arbitrary thread before you add it?

Suppose you have this in the “make UFO mesh” thread:

Mesh mesh = new Mesh(); //all this is safe
mesh.setBuffer(Type.Index, 3, …);
Geometry geom = new Geometry(…);
geom.setMesh(mesh);
mesh.setScale(0.1);
mesh.updateBound();

app.enqueue(…); //This is safe: Use an enqueue to add it to the scene’s root node.

mesh.setScale(0.5); //This is NOT safe.

Am I right about whats safe and what could generate an error?

Yes… unless you enqueued the scale call, you would eventually get unlucky and be attempting to modify the mesh at a time where the scene state is locked.

1 Like

Ooops… sorry… this is only the case, IF the mesh is currently attached to the scene. Othewise, you can do whatever you like.

Bottom line, you can generally do whatever you want to a Mesh, Node, Geometry, whatever. from another thread… UNTIL it is attached to the scene. Then you must enqueue() changes or update it in some other thread safe way.

1 Like

Ok thanks. Question answered.

Someone correct me if I’m wrong about this but I believe that if you are generating a mesh in a way that’s resource intensive you can create the mesh in an arbitrary thread (or using a Future) and then set the generated mesh to a geometry in the main render thread without any problems. Even if the geometry is already attached to the scenegraph. That way you can avoid the overhead of having to detach / reattach the geometry if the mesh needs to change.

You might need to call geometry.updateGeometricState() afterwards to get it to work properly. (Not sure about this

@Vektor said: Someone correct me if I'm wrong about this but I believe that if you are generating a mesh in a way that's resource intensive you can create the mesh in an arbitrary thread (or using a Future) and then set the generated mesh to a geometry without any problems. Even if the geometry is already attached to the scenegraph. That way you can avoid the overhead of having to detach / reattach the geometry if the mesh needs to change.

You might need to call geometry.updateGeometricState() afterwards to get it to work properly. (Not sure about this)

As already answered twice in this thread, yes, but you have to set the mesh on the render thread or you will have bad problems.

To repeat: you can do anything you want UNTIL you attach it to the scene graph.

To clarify: you cannot attach it to the scene graph from a separate thread… this includes calling setMesh()… because that would be modifying the already attached objects from another thread.