Temporary freeze every time a new object comes on-screen (3.0)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By con_tone

In a 3D first-person scene, pointing the camera at something that hasn’t been on-screen before causes the game to freeze while it loads the resources.
This is reproducible in the materials tester example, if you turn the camera away from the testers before running the scene.
How can this be avoided?
A way to manually trigger all of these loading pauses at the start of a scene would be helpful.

:bust_in_silhouette: Reply From: endragor

The problem is that Godot compiles shader lazily - whenever something is rendered for the first time, its shader gets compiled and cached, which takes CPU time and results in the freezes you described. The plan for 3.1 is to compile shaders when the shader resource is loaded, as discussed here: https://github.com/godotengine/godot/issues/13954

In the meantime, you can workaround this by forcing compilation of each of the shaders that may be used in your scene(s) during game loading screen. To do that, you can iterate all materials in your scene, apply them to quad meshes, add them to the scene for one frame, then remove them. This will ensure the materials are compiled and cached in the engine.

Thank you! That clears things up a lot.
I had a feeling this kind of workaround might be necessary; hopefully a more efficient solution will be implemented soon.

con_tone | 2018-02-10 17:31