Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomizeLoadStaticMeshAsync #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/glTFRuntime/Private/glTFRuntimeAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,11 @@ bool UglTFRuntimeAsset::LoadEmitterIntoAudioComponent(const FglTFRuntimeAudioEmi
return Parser->LoadEmitterIntoAudioComponent(Emitter, AudioComponent);
}

void UglTFRuntimeAsset::LoadStaticMeshAsync(const int32 MeshIndex, const FglTFRuntimeStaticMeshAsync& AsyncCallback, const FglTFRuntimeStaticMeshConfig& StaticMeshConfig)
void UglTFRuntimeAsset::LoadStaticMeshAsync(const int32 MeshIndex, const FglTFRuntimeStaticMeshAsyncCustom& AsyncCallback, const FglTFRuntimeStaticMeshConfig& StaticMeshConfig, UStaticMeshComponent* StaticMeshComponent)
{
GLTF_CHECK_PARSER_VOID();

Parser->LoadStaticMeshAsync(MeshIndex, AsyncCallback, StaticMeshConfig);
Parser->LoadStaticMeshAsync(MeshIndex, AsyncCallback, StaticMeshConfig, StaticMeshComponent);
}

void UglTFRuntimeAsset::LoadMeshAsRuntimeLODAsync(const int32 MeshIndex, const FglTFRuntimeMeshLODAsync& AsyncCallback, const FglTFRuntimeMaterialsConfig& MaterialsConfig)
Expand Down
102 changes: 61 additions & 41 deletions Source/glTFRuntime/Private/glTFRuntimeAssetActorAsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ AglTFRuntimeAssetActorAsync::AglTFRuntimeAssetActorAsync()

bShowWhileLoading = true;
bStaticMeshesAsSkeletal = false;
bSkeletalMeshesAsStatic = false;
LoadedMeshNum = 0;
TotalVerticesNum = 0;
}

// Called when the game starts or when spawned
Expand Down Expand Up @@ -49,6 +52,7 @@ void AglTFRuntimeAssetActorAsync::BeginPlay()
}
}

TotalMeshNum = MeshesToLoad.Num();
LoadNextMeshAsync();
}

Expand Down Expand Up @@ -80,7 +84,7 @@ void AglTFRuntimeAssetActorAsync::ProcessNode(USceneComponent* NodeParentCompone
}
else
{
if (Node.SkinIndex < 0 && !bStaticMeshesAsSkeletal)
if (Node.SkinIndex < 0 && !bStaticMeshesAsSkeletal || bSkeletalMeshesAsStatic)
{
UStaticMeshComponent* StaticMeshComponent = NewObject<UStaticMeshComponent>(this, GetSafeNodeName<UStaticMeshComponent>(Node));
StaticMeshComponent->SetupAttachment(NodeParentComponent);
Expand Down Expand Up @@ -135,61 +139,67 @@ void AglTFRuntimeAssetActorAsync::LoadNextMeshAsync()
return;
}

auto It = MeshesToLoad.CreateIterator();
if (UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(It->Key))
auto begin = MeshesToLoad.begin();
auto end = MeshesToLoad.end();

for (auto It = begin; It != end; ++It)
{
CurrentPrimitiveComponent = StaticMeshComponent;
if (StaticMeshConfig.Outer == nullptr)
if (UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(It->Key))
{
CurrentPrimitiveComponent = StaticMeshComponent;

if (StaticMeshConfig.Outer == nullptr)
{
StaticMeshConfig.Outer = StaticMeshComponent;
}
FglTFRuntimeStaticMeshAsyncCustom Delegate;
Delegate.BindDynamic(this, &AglTFRuntimeAssetActorAsync::LoadStaticMeshAsync);
Asset->LoadStaticMeshAsync(It->Value.MeshIndex, Delegate, StaticMeshConfig, StaticMeshComponent);
}
else if (USkeletalMeshComponent* SkeletalMeshComponent = Cast<USkeletalMeshComponent>(It->Key))
{
StaticMeshConfig.Outer = StaticMeshComponent;
CurrentPrimitiveComponent = SkeletalMeshComponent;
FglTFRuntimeSkeletalMeshAsync Delegate;
Delegate.BindDynamic(this, &AglTFRuntimeAssetActorAsync::LoadSkeletalMeshAsync);
Asset->LoadSkeletalMeshAsync(It->Value.MeshIndex, It->Value.SkinIndex, Delegate, SkeletalMeshConfig);
}
FglTFRuntimeStaticMeshAsync Delegate;
Delegate.BindDynamic(this, &AglTFRuntimeAssetActorAsync::LoadStaticMeshAsync);
Asset->LoadStaticMeshAsync(It->Value.MeshIndex, Delegate, StaticMeshConfig);
}
else if (USkeletalMeshComponent* SkeletalMeshComponent = Cast<USkeletalMeshComponent>(It->Key))
{
CurrentPrimitiveComponent = SkeletalMeshComponent;
FglTFRuntimeSkeletalMeshAsync Delegate;
Delegate.BindDynamic(this, &AglTFRuntimeAssetActorAsync::LoadSkeletalMeshAsync);
Asset->LoadSkeletalMeshAsync(It->Value.MeshIndex, It->Value.SkinIndex, Delegate, SkeletalMeshConfig);
}
}

void AglTFRuntimeAssetActorAsync::LoadStaticMeshAsync(UStaticMesh* StaticMesh)
void AglTFRuntimeAssetActorAsync::LoadStaticMeshAsync(UStaticMesh* StaticMesh, UStaticMeshComponent* StaticMeshComponent)
{
if (UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(CurrentPrimitiveComponent))
Mutex.Lock();
StaticMeshComponent->SetCastShadow(bIsCastShadowEnabled);
TotalVerticesNum += StaticMesh->GetNumVertices(0);
DiscoveredStaticMeshComponents.Add(StaticMeshComponent, StaticMesh);
Mutex.Unlock();

if (bShowWhileLoading)
{
DiscoveredStaticMeshComponents.Add(StaticMeshComponent, StaticMesh);
if (bShowWhileLoading)
{
StaticMeshComponent->SetStaticMesh(StaticMesh);
}
StaticMeshComponent->SetStaticMesh(StaticMesh);
}

if (StaticMesh && !StaticMeshConfig.ExportOriginalPivotToSocket.IsEmpty())
if (StaticMesh && !StaticMeshConfig.ExportOriginalPivotToSocket.IsEmpty())
{
UStaticMeshSocket* DeltaSocket = StaticMesh->FindSocket(FName(StaticMeshConfig.ExportOriginalPivotToSocket));
if (DeltaSocket)
{
UStaticMeshSocket* DeltaSocket = StaticMesh->FindSocket(FName(StaticMeshConfig.ExportOriginalPivotToSocket));
if (DeltaSocket)
{
FTransform NewTransform = StaticMeshComponent->GetRelativeTransform();
FVector DeltaLocation = -DeltaSocket->RelativeLocation * NewTransform.GetScale3D();
DeltaLocation = NewTransform.GetRotation().RotateVector(DeltaLocation);
NewTransform.AddToTranslation(DeltaLocation);
StaticMeshComponent->SetRelativeTransform(NewTransform);
}
FTransform NewTransform = StaticMeshComponent->GetRelativeTransform();
FVector DeltaLocation = -DeltaSocket->RelativeLocation * NewTransform.GetScale3D();
DeltaLocation = NewTransform.GetRotation().RotateVector(DeltaLocation);
NewTransform.AddToTranslation(DeltaLocation);
StaticMeshComponent->SetRelativeTransform(NewTransform);
}

}

MeshesToLoad.Remove(CurrentPrimitiveComponent);
if (MeshesToLoad.Num() > 0)
{
LoadNextMeshAsync();
}
// trigger event
else
LoadedMeshNum++;
OnProgress.Broadcast(LoadedMeshNum / TotalMeshNum);

if (TotalMeshNum - LoadedMeshNum == 0)
{
Mutex.Lock();
ScenesLoaded();
Mutex.Unlock();
}
}

Expand Down Expand Up @@ -262,3 +272,13 @@ void AglTFRuntimeAssetActorAsync::PostUnregisterAllComponents()
}
Super::PostUnregisterAllComponents();
}

int AglTFRuntimeAssetActorAsync::GetTotalMeshNum() const
{
return (int)TotalMeshNum;
}

int AglTFRuntimeAssetActorAsync::GetTotalVerticesNum() const
{
return TotalVerticesNum;
}
Loading