I'm experiencing lag when loading Datasmith files at runtime in Unreal Engine using the DatasmithRuntime plugin. Specifically, when I use the Load File
or Load File from Explorer
nodes in the plugin, loading a scene of about 100 MB causes the program to freeze for a few seconds before the model is displayed and the program becomes responsive again.
What I've Tried:
Background Threading in C++:
I moved the file loading logic to a background threadAsyncTask
to avoid blocking the main thread. However, the lag persists. Upon further investigation, I found that while the file loading itself happens in the background, the construction of meshes, collision bodies, and scene updates still occur on the main thread.Blueprint Nodes:
I also tried using theLoad File
andLoad File from Explorer
nodes directly in Blueprints, but the lag issue remains.Here's the code I used:
void ABlueprintTest::StatrAsyncEvent(
ADatasmithRuntimeActor\* DatasmithRuntimeActor,
const FString& FileName)
{
AsyncTask(ENamedThreads::AnyBackgroundThreadNormalTask, [DatasmithRuntimeActor, FileName]()
{
bool bSuccess = LoadFile(DatasmithRuntimeActor, FileName);
AsyncTask(ENamedThreads::GameThread, [bSuccess]()
{
if (bSuccess)
{
UKismetSystemLibrary::PrintString(nullptr, TEXT("File loaded successfully!"));
}
else
{
UKismetSystemLibrary::PrintString(nullptr, TEXT("Filed to load file!"));
}
});
});
}
When the program runs the file loading function, it becomes unresponsive for a few seconds. After the model is displayed, the program returns to normal. It seems that the file loading itself doesn't block the main thread, but the construction and presentation of a large number of meshes and collision bodies do.
Questions:
How can I reduce or eliminate the lag caused by mesh construction and scene updates during Datasmith file loading?
Is it possible to achieve a "load and display simultaneously" effect, where the scene is rendered progressively as the file is loaded?
Are there any best practices or optimizations for handling large Datasmith files at runtime?
Screenshots:
Program Freeze:
Model Displayed After Freeze: