From c2fe82118b10cc6323e188cf25648386f498b7cb Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Thu, 28 Nov 2024 18:14:33 +0100 Subject: [PATCH] reworked getInstancePropertyTableValues() Changed the function and its helper getInstancePropertyTableValues() to have less duplicated code. Also, it is now possible for instances to fall back to feature IDs that might be stored in the primitives of an instance. --- .../CesiumMetadataPickingBlueprintLibrary.cpp | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/Source/CesiumRuntime/Private/CesiumMetadataPickingBlueprintLibrary.cpp b/Source/CesiumRuntime/Private/CesiumMetadataPickingBlueprintLibrary.cpp index cbd1bc422..054148e72 100644 --- a/Source/CesiumRuntime/Private/CesiumMetadataPickingBlueprintLibrary.cpp +++ b/Source/CesiumRuntime/Private/CesiumMetadataPickingBlueprintLibrary.cpp @@ -151,8 +151,17 @@ bool UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit( } namespace { +/* + * Returns std:nullopt if the component isnt an instanced static mesh or doesn't + * have instance feature ID data, which will allow + * GetPropertyTableValuesFromHit() to search for feature IDs in primitive + * attributes. + */ std::optional> -getInstancePropertyTableValues(const FHitResult& Hit, int64 FeatureIDSetIndex) { +getInstancePropertyTableValues( + const FHitResult& Hit, + const UCesiumGltfComponent* pModel, + int64 FeatureIDSetIndex) { const auto* pInstancedComponent = Cast(Hit.Component); if (!IsValid(pInstancedComponent)) { @@ -161,20 +170,12 @@ getInstancePropertyTableValues(const FHitResult& Hit, int64 FeatureIDSetIndex) { const TSharedPtr pInstanceFeatures = pInstancedComponent->pInstanceFeatures; if (!pInstanceFeatures) { - return TMap(); + return std::nullopt; } - const UCesiumGltfComponent* pModel = - Cast(pInstancedComponent->GetOuter()); - if (!IsValid(pModel)) { - return TMap(); - } - const FCesiumInstanceFeatures& instanceFeatures = - UCesiumInstanceFeaturesBlueprintLibrary::GetInstanceFeatures( - pInstancedComponent); const TArray& featureIDSets = UCesiumInstanceFeaturesBlueprintLibrary::GetFeatureIDSets( - instanceFeatures); + *pInstanceFeatures); if (FeatureIDSetIndex < 0 || FeatureIDSetIndex >= featureIDSets.Num()) { return TMap(); } @@ -192,7 +193,7 @@ getInstancePropertyTableValues(const FHitResult& Hit, int64 FeatureIDSetIndex) { propertyTables[propertyTableIndex]; int64 featureID = UCesiumInstanceFeaturesBlueprintLibrary::GetFeatureIDFromInstance( - instanceFeatures, + *pInstanceFeatures, Hit.Item, FeatureIDSetIndex); if (featureID < 0) { @@ -208,24 +209,27 @@ TMap UCesiumMetadataPickingBlueprintLibrary::GetPropertyTableValuesFromHit( const FHitResult& Hit, int64 FeatureIDSetIndex) { + const UCesiumGltfComponent* pModel = nullptr; + if (const auto* pPrimComponent = Cast(Hit.Component); + !IsValid(pPrimComponent)) { + return TMap(); + } else { + pModel = Cast(pPrimComponent->GetOuter()); + if (!IsValid(pModel)) { + return TMap(); + } + } std::optional> instanceProperties = - getInstancePropertyTableValues(Hit, FeatureIDSetIndex); + getInstancePropertyTableValues(Hit, pModel, FeatureIDSetIndex); if (instanceProperties) { return *instanceProperties; } - const UCesiumGltfPrimitiveComponent* pGltfComponent = - Cast(Hit.Component); - if (!IsValid(pGltfComponent)) { - return TMap(); - } - - const UCesiumGltfComponent* pModel = - Cast(pGltfComponent->GetOuter()); - if (!IsValid(pModel)) { + const auto* pCesiumPrimitive = Cast(Hit.Component); + if (!pCesiumPrimitive) { return TMap(); } - const CesiumPrimitiveData& primData = pGltfComponent->getPrimitiveData(); + const CesiumPrimitiveData& primData = pCesiumPrimitive->getPrimitiveData(); const FCesiumPrimitiveFeatures& features = primData.Features; const TArray& featureIDSets = UCesiumPrimitiveFeaturesBlueprintLibrary::GetFeatureIDSets(features);