-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First cut of a working origin shift component.
- Loading branch information
Showing
9 changed files
with
223 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Source/CesiumRuntime/Private/CesiumOriginShiftComponent.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2020-2023 CesiumGS, Inc. and Contributors | ||
|
||
#include "CesiumOriginShiftComponent.h" | ||
#include "CesiumGeoreference.h" | ||
#include "CesiumGlobeAnchorComponent.h" | ||
#include "CesiumSubLevelComponent.h" | ||
#include "CesiumSubLevelSwitcherComponent.h" | ||
#include "CesiumWgs84Ellipsoid.h" | ||
#include "LevelInstance/LevelInstanceActor.h" | ||
|
||
class ALevelInstance; | ||
|
||
UCesiumOriginShiftComponent::UCesiumOriginShiftComponent() : Super() { | ||
this->PrimaryComponentTick.bCanEverTick = true; | ||
this->PrimaryComponentTick.TickGroup = ETickingGroup::TG_PrePhysics; | ||
} | ||
|
||
void UCesiumOriginShiftComponent::BeginPlay() { | ||
Super::BeginPlay(); | ||
|
||
this->GlobeAnchor = nullptr; | ||
|
||
AActor* Owner = this->GetOwner(); | ||
if (!Owner) | ||
return; | ||
|
||
this->GlobeAnchor = | ||
Owner->FindComponentByClass<UCesiumGlobeAnchorComponent>(); | ||
if (!this->GlobeAnchor) { | ||
// A globe anchor is missing and required, so add one. | ||
this->GlobeAnchor = | ||
Cast<UCesiumGlobeAnchorComponent>(Owner->AddComponentByClass( | ||
UCesiumGlobeAnchorComponent::StaticClass(), | ||
false, | ||
FTransform::Identity, | ||
false)); | ||
Owner->AddInstanceComponent(this->GlobeAnchor); | ||
} | ||
} | ||
|
||
void UCesiumOriginShiftComponent::TickComponent( | ||
float DeltaTime, | ||
ELevelTick TickType, | ||
FActorComponentTickFunction* ThisTickFunction) { | ||
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); | ||
|
||
if (!this->GlobeAnchor) | ||
return; | ||
|
||
ACesiumGeoreference* Georeference = this->GlobeAnchor->ResolveGeoreference(); | ||
|
||
if (!Georeference) | ||
return; | ||
|
||
UCesiumSubLevelSwitcherComponent* Switcher = | ||
Georeference->GetSubLevelSwitcher(); | ||
if (!Switcher) | ||
return; | ||
|
||
const TArray<TWeakObjectPtr<ALevelInstance>>& Sublevels = | ||
Switcher->GetRegisteredSubLevelsWeak(); | ||
|
||
if (Sublevels.Num() == 0) { | ||
// If we don't have any known sub-levels, bail quickly to save ourselves a | ||
// little work. | ||
return; | ||
} | ||
|
||
FVector ActorEcef = this->GlobeAnchor->GetEarthCenteredEarthFixedPosition(); | ||
|
||
ALevelInstance* ClosestActiveLevel = nullptr; | ||
double ClosestLevelDistance = std::numeric_limits<double>::max(); | ||
|
||
for (int32 i = 0; i < Sublevels.Num(); ++i) { | ||
ALevelInstance* Current = Sublevels[i].Get(); | ||
if (!IsValid(Current)) | ||
continue; | ||
|
||
UCesiumSubLevelComponent* SubLevelComponent = | ||
Current->FindComponentByClass<UCesiumSubLevelComponent>(); | ||
if (!IsValid(SubLevelComponent)) | ||
continue; | ||
|
||
if (!SubLevelComponent->GetEnabled()) | ||
continue; | ||
|
||
FVector LevelEcef = | ||
UCesiumWgs84Ellipsoid::LongitudeLatitudeHeightToEarthCenteredEarthFixed( | ||
FVector( | ||
SubLevelComponent->GetOriginLongitude(), | ||
SubLevelComponent->GetOriginLatitude(), | ||
SubLevelComponent->GetOriginHeight())); | ||
|
||
double LevelDistance = FVector::Distance(LevelEcef, ActorEcef); | ||
if (LevelDistance < SubLevelComponent->GetLoadRadius() && | ||
LevelDistance < ClosestLevelDistance) { | ||
ClosestActiveLevel = Current; | ||
ClosestLevelDistance = LevelDistance; | ||
} | ||
} | ||
|
||
Switcher->SetTargetSubLevel(ClosestActiveLevel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2020-2023 CesiumGS, Inc. and Contributors | ||
|
||
#pragma once | ||
|
||
#include "Components/ActorComponent.h" | ||
#include "CoreMinimal.h" | ||
#include "CesiumOriginShiftComponent.generated.h" | ||
|
||
class UCesiumGlobeAnchorComponent; | ||
|
||
UCLASS(ClassGroup = "Cesium", Meta = (BlueprintSpawnableComponent)) | ||
class CESIUMRUNTIME_API UCesiumOriginShiftComponent : public UActorComponent { | ||
GENERATED_BODY() | ||
|
||
public: | ||
UCesiumOriginShiftComponent(); | ||
|
||
protected: | ||
virtual void BeginPlay() override; | ||
virtual void TickComponent( | ||
float DeltaTime, | ||
enum ELevelTick TickType, | ||
FActorComponentTickFunction* ThisTickFunction) override; | ||
|
||
private: | ||
// The globe anchor attached to the same Actor as this component. Don't | ||
// save/load or copy this. It is set in BeginPlay. | ||
UPROPERTY(Transient, DuplicateTransient, TextExportTransient) | ||
UCesiumGlobeAnchorComponent* GlobeAnchor; | ||
}; |
Oops, something went wrong.