Skip to content

Commit

Permalink
Only add instance custom data for feature IDs that are used.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Dec 24, 2024
1 parent c314291 commit d63f692
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
50 changes: 41 additions & 9 deletions Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2890,34 +2890,59 @@ PRAGMA_ENABLE_DEPRECATION_WARNINGS
#pragma endregion

namespace {
void addInstanceFeatureIds(UCesiumGltfInstancedComponent* pInstancedComponent) {
void addInstanceFeatureIds(
UCesiumGltfInstancedComponent* pInstancedComponent,
const FCesiumFeaturesMetadataDescription& featuresDescription) {
const TSharedPtr<FCesiumPrimitiveFeatures>& pInstanceFeatures =
pInstancedComponent->pInstanceFeatures;
if (!pInstanceFeatures) {
return;
}
const TArray<FCesiumFeatureIdSet>& featureIdSets =
const TArray<FCesiumFeatureIdSet>& allFeatureIdSets =
UCesiumPrimitiveFeaturesBlueprintLibrary::GetFeatureIDSets(
*pInstanceFeatures);
int32 featureSetCount = featureIdSets.Num();

const TArray<FCesiumFeatureIdSetDescription>& featureIDSetDescriptions =
featuresDescription.Features.FeatureIdSets;

int32_t featureIdTextureCounter = 0;

TArray<int32> activeFeatureIdSets;

for (int32 i = 0; i < allFeatureIdSets.Num(); ++i) {
FString name = CesiumEncodedFeaturesMetadata::getNameForFeatureIDSet(
allFeatureIdSets[i],
featureIdTextureCounter);

const FCesiumFeatureIdSetDescription* pDescription =
featureIDSetDescriptions.FindByPredicate(
[&name](
const FCesiumFeatureIdSetDescription& existingFeatureIDSet) {
return existingFeatureIDSet.Name == name;
});

if (pDescription) {
activeFeatureIdSets.Emplace(i);
}
}

int32 featureSetCount = activeFeatureIdSets.Num();
if (featureSetCount == 0) {
return;
}
#if ENGINE_VERSION_5_3_OR_HIGHER
pInstancedComponent->SetNumCustomDataFloats(featureSetCount);
#else
pInstancedComponent->NumCustomDataFloats = featureSetCount;
#endif
int32 numInstances = pInstancedComponent->GetInstanceCount();
pInstancedComponent->PerInstanceSMCustomData.SetNum(
featureSetCount * numInstances);
for (int32 j = 0; j < featureSetCount; ++j) {
int64_t setIndex = activeFeatureIdSets[j];

for (int32 i = 0; i < numInstances; ++i) {
int64 featureId =
UCesiumPrimitiveFeaturesBlueprintLibrary::GetFeatureIDFromInstance(
*pInstanceFeatures,
i,
j);
setIndex);
pInstancedComponent
->SetCustomDataValue(i, j, static_cast<float>(featureId), true);
}
Expand Down Expand Up @@ -2968,7 +2993,14 @@ static void loadPrimitiveGameThreadPart(
pInstancedComponent->AddInstance(transform, false);
}
pInstancedComponent->pInstanceFeatures = pInstanceFeatures;
addInstanceFeatureIds(pInstancedComponent);

const std::optional<FCesiumFeaturesMetadataDescription>&
maybeFeaturesDescription =
pTilesetActor->getFeaturesMetadataDescription();
if (maybeFeaturesDescription) {
addInstanceFeatureIds(pInstancedComponent, *maybeFeaturesDescription);
}

pCesiumPrimitive = pInstancedComponent;
} else {
auto* pComponent =
Expand Down
5 changes: 5 additions & 0 deletions Source/CesiumRuntime/Public/Cesium3DTileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,11 @@ class CESIUMRUNTIME_API ACesium3DTileset : public AActor {
return this->_pTileset.Get();
}

const std::optional<FCesiumFeaturesMetadataDescription>&
getFeaturesMetadataDescription() const {
return this->_featuresMetadataDescription;
}

// AActor overrides (some or most of them should be protected)
virtual bool ShouldTickIfViewportsOnly() const override;
virtual void Tick(float DeltaTime) override;
Expand Down

0 comments on commit d63f692

Please sign in to comment.