Skip to content

Commit

Permalink
Uncompilable WIP
Browse files Browse the repository at this point in the history
But scared straight by being locked out of my laptop!
  • Loading branch information
timoore committed Sep 17, 2024
1 parent 53a5394 commit a39e543
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
60 changes: 50 additions & 10 deletions Source/CesiumRuntime/Private/CesiumFeaturesMetadataComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "Materials/MaterialExpressionFunctionOutput.h"
#include "Materials/MaterialExpressionIf.h"
#include "Materials/MaterialExpressionMaterialFunctionCall.h"
#include "Materials/MaterialExpressionPerInstanceCustomData.h"
#include "Materials/MaterialExpressionRound.h"
#include "Materials/MaterialExpressionScalarParameter.h"
#include "Materials/MaterialExpressionSetMaterialAttributes.h"
#include "Materials/MaterialExpressionTextureCoordinate.h"
Expand Down Expand Up @@ -457,6 +459,7 @@ struct MaterialFunctionLibrary {
UMaterialFunction* TransformTexCoords = nullptr;
UMaterialFunction* GetFeatureIdsFromAttribute = nullptr;
UMaterialFunction* GetFeatureIdsFromTexture = nullptr;
UMaterialFunction* GetFeatureIdsFromInstance = nullptr;

MaterialFunctionLibrary()
: SelectTexCoords(LoadMaterialFunction(
Expand All @@ -466,13 +469,17 @@ struct MaterialFunctionLibrary {
GetFeatureIdsFromAttribute(LoadMaterialFunction(
"/CesiumForUnreal/Materials/MaterialFunctions/CesiumGetFeatureIdsFromAttribute.CesiumGetFeatureIdsFromAttribute")),
GetFeatureIdsFromTexture(LoadMaterialFunction(
"/CesiumForUnreal/Materials/MaterialFunctions/CesiumGetFeatureIdsFromTexture.CesiumGetFeatureIdsFromTexture")) {
"/CesiumForUnreal/Materials/MaterialFunctions/CesiumGetFeatureIdsFromTexture.CesiumGetFeatureIdsFromTexture")),
GetFeatureIdsFromInstance(LoadMaterialFunction(
"/CesiumForUnreal/Materials/MaterialFunctions/CesiumGetFeatureIdsFromInstance.CesiumGetFeatureIdsFromInstance"))
{
}

bool isValid() {
return SelectTexCoords != nullptr &&
GetFeatureIdsFromAttribute != nullptr &&
GetFeatureIdsFromTexture != nullptr;
GetFeatureIdsFromTexture != nullptr &&
GetFeatureIdsFromInstance != nullptr;
}
};
} // namespace
Expand All @@ -487,7 +494,8 @@ static void ClassifyNodes(
FunctionLibrary.GetFeatureIdsFromAttribute;
const UMaterialFunction* GetFeatureIdsFromTextureFunction =
FunctionLibrary.GetFeatureIdsFromTexture;

const UMaterialFunction* GetFeatureIdsFromInstanceFunction =
FunctionLibrary.GetFeatureIdsFromInstance;
for (const TObjectPtr<UMaterialExpression>& Node :
Layer->GetExpressionCollection().Expressions) {
// Check if this node is marked as autogenerated.
Expand Down Expand Up @@ -526,7 +534,8 @@ static void ClassifyNodes(

const FName& name = FunctionCallNode->MaterialFunction->GetFName();
if (name == GetFeatureIdsFromAttributeFunction->GetFName() ||
name == GetFeatureIdsFromTextureFunction->GetFName()) {
name == GetFeatureIdsFromTextureFunction->GetFName() ||
name == GetFeatureIdsFromInstanceFunction->GetFName()) {
Classification.GetFeatureIdNodes.Add(FunctionCallNode);
}
} else {
Expand Down Expand Up @@ -1840,7 +1849,7 @@ void GenerateNodesForPropertyTable(
UMaterialFunctionMaterialLayer* TargetMaterialLayer,
int32& NodeX,
int32& NodeY,
UMaterialExpressionMaterialFunctionCall* GetFeatureIdCall) {
UMaterialExpression* GetFeatureExpression) {
int32 BeginSectionX = NodeX;
// This value is used by parameters on the left side of the
// "GetPropertyValues" function...
Expand Down Expand Up @@ -1880,7 +1889,7 @@ void GenerateNodesForPropertyTable(

FCustomInput& FeatureIDInput = GetPropertyValuesFunction->Inputs[0];
FeatureIDInput.InputName = FName("FeatureID");
FeatureIDInput.Input.Expression = GetFeatureIdCall;
FeatureIDInput.Input.Expression = GetFeatureExpression;

GetPropertyValuesFunction->AdditionalOutputs.Reserve(
PropertyTable.Properties.Num());
Expand Down Expand Up @@ -2364,6 +2373,28 @@ void GenerateNodesForPropertyTexture(
NodeY = FMath::Max(PropertyDataSectionY, PropertyTransformsSectionY) + Incr;
}

UMaterialExpression* GenerateInstanceNodes(TArray<UMaterialExpression*>& AutoGeneratedNodes,
UMaterialFunctionMaterialLayer* TargetMaterialLayer,
int32& NodeX,
int32& NodeY) {
UMaterialExpressionPerInstanceCustomData *data =
NewObject<UMaterialExpressionPerInstanceCustomData>(TargetMaterialLayer);
UMaterialExpressionScalarParameter* TexCoordsIndex =
NewObject<UMaterialExpressionScalarParameter>(TargetMaterialLayer);
TexCoordsIndex->ParameterName = FName(SafeName + MaterialTexCoordIndexSuffix);
TexCoordsIndex->DefaultValue = 0.0f;
TexCoordsIndex->MaterialExpressionEditorX = NodeX;
TexCoordsIndex->MaterialExpressionEditorY = NodeY;
data->DefaultValue.Expression = TexCoordsIndex;
AutoGeneratedNodes.Add(TexCoordsIndex);
data->ConstDefaultValue = 0.0f;
data->DataIndex = 0;
UMaterialExpressionRound* round =
NewObject<UMaterialExpressionRound>(TargetMaterialLayer);

return data;
}

void GenerateMaterialNodes(
UCesiumFeaturesMetadataComponent* pComponent,
TArray<UMaterialExpression*>& AutoGeneratedNodes,
Expand All @@ -2385,17 +2416,27 @@ void GenerateMaterialNodes(
}

UMaterialExpressionMaterialFunctionCall* GetFeatureIdCall = nullptr;
UMaterialExpression* LastNode = nullptr;
if (featureIdSet.Type == ECesiumFeatureIdSetType::Texture) {
GetFeatureIdCall = GenerateNodesForFeatureIdTexture(
LastNode = GenerateNodesForFeatureIdTexture(
featureIdSet,
AutoGeneratedNodes,
pComponent->TargetMaterialLayer,
FunctionLibrary,
NodeX,
NodeY);
} else {
// Handle implicit feature IDs the same as feature ID attributes
} else if (featureIdSet.Type == ECesiumFeatureIdSetType::Instance) {
GetFeatureIdCall = GenerateNodesForFeatureIdAttribute(
featureIdSet,
AutoGeneratedNodes,
pComponent->TargetMaterialLayer,
FunctionLibrary.GetFeatureIdsFromInstance,
NodeX,
NodeY);
}
else {
// Handle implicit feature IDs the same as feature ID attributes
LastNode = GenerateNodesForFeatureIdAttribute(
featureIdSet,
AutoGeneratedNodes,
pComponent->TargetMaterialLayer,
Expand All @@ -2404,7 +2445,6 @@ void GenerateMaterialNodes(
NodeY);
}

UMaterialExpression* LastNode = GetFeatureIdCall;
int32 BeginSectionY = NodeY;

if (!featureIdSet.PropertyTableName.IsEmpty()) {
Expand Down
14 changes: 12 additions & 2 deletions Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3178,12 +3178,22 @@ void addInstanceFeatureIds(UCesiumGltfInstancedComponent* pInstancedComponent) {
const TArray<FCesiumFeatureIdSet>& featureIdSets =
UCesiumInstanceFeaturesBlueprintLibrary::GetFeatureIDSets(instanceFeatures);
int32 featureSetCount = featureIdSets.Num();
pInstancedComponent->NumCustomDataFloats = featureSetCount;
if (featureSetCount == 0) {
return;
}
for (const auto& idSet : featureIdSets) {
int32 numInstances = pInstancedComponent->GetInstanceCount();
pInstancedComponent->PerInstanceSMCustomData.SetNum(featureSetCount * numInstances);
for (int32 j = 0; j < featureSetCount; ++j) {
for (int32 i = 0; i < numInstances; ++i) {
int64 featureId = UCesiumInstanceFeaturesBlueprintLibrary::GetFeatureIDFromInstance(
instanceFeatures,
i,
j);
pInstancedComponent->PerInstanceSMCustomData[i * featureSetCount + j] =
static_cast<float>(featureId);
}
}

}
} // namespace

Expand Down
9 changes: 5 additions & 4 deletions Source/CesiumRuntime/Private/LoadGltfResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ struct LoadMeshResult {

LoadMeshResult(const LoadMeshResult&) = delete;

LoadMeshResult(LoadMeshResult&& other)
: primitiveResults(std::move(other.primitiveResults)) {}
LoadMeshResult(LoadMeshResult&& other) {
primitiveResults.swap(other.primitiveResults);
}

LoadMeshResult& operator=(LoadMeshResult&& other) {
primitiveResults.swap(other.primitiveResults);
Expand All @@ -226,8 +227,8 @@ struct LoadNodeResult {
LoadNodeResult(const LoadNodeResult&) = delete;

LoadNodeResult(LoadNodeResult&& other)
: InstanceTransforms(std::move(other.InstanceTransforms)),
pInstanceFeatures(std::move(other.pInstanceFeatures)) {
: pInstanceFeatures(std::move(other.pInstanceFeatures)) {
InstanceTransforms.swap(other.InstanceTransforms);
meshResult.swap(other.meshResult);
}

Expand Down

0 comments on commit a39e543

Please sign in to comment.