Skip to content

Commit

Permalink
Temporary commit for debugging test bug
Browse files Browse the repository at this point in the history
  • Loading branch information
j9liu committed Oct 17, 2023
1 parent d465d82 commit f23f80a
Show file tree
Hide file tree
Showing 8 changed files with 769 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,8 @@ FString GetCodeForAssemblingPropertyFromTexture(
" uint channel = uint(f.Get(Channels, i));\n"
" uint sample = asuint(f.Get(sample, channel));\n"
"}\n";

return code;
}

UMaterialExpressionMaterialFunctionCall* GenerateNodesForFeatureIdTexture(
Expand Down Expand Up @@ -1769,7 +1771,7 @@ void GenerateMaterialNodes(
// NodeX = MetadataSectionLeft;

//// Generate nodes for any property textures that aren't linked to a
///primitive / texture coordinate set.
/// primitive / texture coordinate set.
// for (const FCesiumPropertyTextureDescription& propertyTexture :
// pComponent->PropertyTextures) {
// if (!GeneratedPropertyTextureNames.Find(propertyTexture.Name)) {
Expand Down
8 changes: 7 additions & 1 deletion Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,12 @@ static void loadIndexedPrimitive(
positionView,
indexAccessor);
primitiveResult.IndexAccessor = indexAccessor;
} else {
UE_LOG(
LogCesium,
VeryVerbose,
TEXT(
"Skip loading primitive due to invalid component type in its index accessor."));
}
}

Expand Down Expand Up @@ -1605,7 +1611,6 @@ static void loadPrimitive(
}

AccessorView<TMeshVector3> positionView(model, *pPositionAccessor);
result.PositionAccessor = positionView;

if (primitive.indices < 0 || primitive.indices >= model.accessors.size()) {
std::vector<uint32_t> syntheticIndexBuffer(positionView.size());
Expand All @@ -1628,6 +1633,7 @@ static void loadPrimitive(
*pPositionAccessor,
positionView);
}
result.PositionAccessor = std::move(positionView);
}

static void loadMesh(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ bool UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(
return false;
}

if (pGltfComponent->PositionAccessor.status() !=
CesiumGltf::AccessorViewStatus::Valid) {
return false;
}

auto accessorIt =
pGltfComponent->TexCoordAccessorMap.find(GltfTexCoordSetIndex);
if (accessorIt == pGltfComponent->TexCoordAccessorMap.end()) {
Expand All @@ -112,14 +117,13 @@ bool UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(
if (!maybeTexCoord) {
return false;
}

const glm::dvec2& texCoord = *maybeTexCoord;
UVs[i] = FVector2D(texCoord[0], texCoord[1]);
}

std::array<FVector, 3> Positions;
for (size_t i = 0; i < Positions.size(); i++) {
auto Position = pGltfComponent->PositionAccessor[VertexIndices[i]];
auto& Position = pGltfComponent->PositionAccessor[VertexIndices[i]];
// The Y-component of glTF positions must be inverted
Positions[i] = FVector(Position[0], -Position[1], Position[2]);
}
Expand Down
236 changes: 236 additions & 0 deletions Source/CesiumRuntime/Private/Tests/CesiumFeatureIdTexture.spec.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "CesiumFeatureIdTexture.h"
#include "CesiumGltf/ExtensionExtMeshFeatures.h"
#include "CesiumGltfPrimitiveComponent.h"
#include "CesiumGltfSpecUtility.h"
#include "Misc/AutomationTest.h"

Expand All @@ -17,6 +18,7 @@ const std::vector<glm::vec2> texCoords{
glm::vec2(0.5, 0),
glm::vec2(0, 0.5),
glm::vec2(0.5, 0.5)};
TObjectPtr<UCesiumGltfPrimitiveComponent> pPrimitiveComponent;
END_DEFINE_SPEC(FCesiumFeatureIdTextureSpec)

void FCesiumFeatureIdTextureSpec::Define() {
Expand Down Expand Up @@ -434,4 +436,238 @@ void FCesiumFeatureIdTextureSpec::Define() {
}
});
});

Describe("GetFeatureIDFromHit", [this]() {
BeforeEach([this]() {
model = Model();
Mesh& mesh = model.meshes.emplace_back();
pPrimitive = &mesh.primitives.emplace_back();
pPrimitiveComponent = NewObject<UCesiumGltfPrimitiveComponent>();

std::vector<glm::vec3> positions{
glm::vec3(-1, 0, 0),
glm::vec3(0, 1, 0),
glm::vec3(1, 0, 0),
glm::vec3(-1, 3, 0),
glm::vec3(0, 4, 0),
glm::vec3(1, 3, 0),
};

CreateAttributeForPrimitive(
model,
*pPrimitive,
"POSITION",
AccessorSpec::Type::VEC3,
AccessorSpec::ComponentType::FLOAT,
positions);

pPrimitiveComponent->PositionAccessor =
CesiumGltf::AccessorView<FVector3f>(
model,
static_cast<int32_t>(model.accessors.size() - 1));

// For convenience when testing, the UVs are the same as the positions
// they correspond to. This means that the interpolated UV value should be
// directly equal to the barycentric coordinates of the triangle.
std::vector<glm::vec2> texCoords{
glm::vec2(-1, 0),
glm::vec2(0, 1),
glm::vec2(1, 0),
glm::vec2(-1, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)};
CreateAttributeForPrimitive(
model,
*pPrimitive,
"TEXCOORD_0",
AccessorSpec::Type::VEC2,
AccessorSpec::ComponentType::FLOAT,
texCoords);

pPrimitiveComponent->TexCoordAccessorMap.emplace(
0,
AccessorView<CesiumGltf::AccessorTypes::VEC2<float>>(
model,
static_cast<int32_t>(model.accessors.size() - 1)));
});

It("returns -1 for invalid texture", [this]() {
FeatureIdTexture texture;
texture.index = -1;
texture.texCoord = 0;
texture.channels = {0};

FCesiumFeatureIdTexture featureIDTexture(
model,
*pPrimitive,
texture,
"PropertyTableName");

TestNotEqual(
"FeatureIDTextureStatus",
UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDTextureStatus(
featureIDTexture),
ECesiumFeatureIdTextureStatus::Valid);

FHitResult Hit;
Hit.Location = FVector_NetQuantize::Zero();
Hit.Component = pPrimitiveComponent;
Hit.FaceIndex = 0;

TestEqual(
"FeatureIDForVertex",
UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDFromHit(
featureIDTexture,
Hit),
-1);
});

// It("returns -1 if hit has no valid component", [this]() {
// FHitResult Hit;
// Hit.Location = FVector_NetQuantize(0, -1, 0);
// Hit.FaceIndex = 0;
// Hit.Component = nullptr;

// FVector2D UV;
// TestFalse(
// "found hit",
// UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(Hit, 0, UV));
//});

// It("returns -1 if specified texcoord set does not exist", [this]() {
// FHitResult Hit;
// Hit.Location = FVector_NetQuantize(0, -1, 0);
// Hit.FaceIndex = 0;
// Hit.Component = pPrimitiveComponent;

// FVector2D UV;
// TestFalse(
// "found hit",
// UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(Hit, 1, UV));
//});

// It("returns correct value for valid texture", [this]() {
// const std::vector<uint8_t> featureIDs{0, 3, 1, 2};

// FeatureId& featureId = AddFeatureIDsAsTextureToModel(
// model,
// *pPrimitive,
// featureIDs,
// 4,
// 2,
// 2,
// texCoords,
// 0);

// FCesiumFeatureIdTexture featureIDTexture(
// model,
// *pPrimitive,
// *featureId.texture,
// "PropertyTableName");

// TestEqual(
// "FeatureIDTextureStatus",
// UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDTextureStatus(
// featureIDTexture),
// ECesiumFeatureIdTextureStatus::Valid);

// for (size_t i = 0; i < featureIDs.size(); i++) {
// int64 featureID =
// UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDForVertex(
// featureIDTexture,
// static_cast<int64>(i));
// TestEqual("FeatureIDForVertex", featureID, featureIDs[i]);
// }
//});

// It("returns correct value for primitive with multiple texcoords",
// [this]() {
// const std::vector<uint8_t> featureIDs{0, 3, 1, 2};
// const std::vector<glm::vec2> texCoord0{
// glm::vec2(0, 0),
// glm::vec2(0.5, 0),
// glm::vec2(0, 0.5),
// glm::vec2(0.5, 0.5)};

// std::vector<std::byte> values(texCoord0.size());
// std::memcpy(values.data(), texCoord0.data(), values.size());

// CreateAttributeForPrimitive(
// model,
// *pPrimitive,
// "TEXCOORD_0",
// AccessorSpec::Type::VEC2,
// AccessorSpec::ComponentType::FLOAT,
// std::move(values));

// const std::vector<glm::vec2> texCoord1{
// glm::vec2(0.5, 0.5),
// glm::vec2(0, 0),
// glm::vec2(0.5, 0),
// glm::vec2(0.0, 0.5)};

// FeatureId& featureId = AddFeatureIDsAsTextureToModel(
// model,
// *pPrimitive,
// featureIDs,
// 4,
// 2,
// 2,
// texCoord1,
// 1);

// FCesiumFeatureIdTexture featureIDTexture(
// model,
// *pPrimitive,
// *featureId.texture,
// "PropertyTableName");

// TestEqual(
// "FeatureIDTextureStatus",
// UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDTextureStatus(
// featureIDTexture),
// ECesiumFeatureIdTextureStatus::Valid);

// const std::vector<uint8_t> expected{2, 0, 3, 1};
// for (size_t i = 0; i < featureIDs.size(); i++) {
// int64 featureID =
// UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDForVertex(
// featureIDTexture,
// static_cast<int64>(i));
// TestEqual("FeatureIDForVertex", featureID, expected[i]);
// }
//});

// It("gets hit for primitive without indices", [this]() {
// FHitResult Hit;
// Hit.Location = FVector_NetQuantize(0, -1, 0);
// Hit.FaceIndex = 0;
// Hit.Component = pPrimitiveComponent;

// FVector2D UV = FVector2D::Zero();

// TestTrue(
// "found hit",
// UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(Hit, 0, UV));
// TestEqual("UV at point", UV, FVector2D(0, 1));

// Hit.Location = FVector_NetQuantize(0, -0.5, 0);
// TestTrue(
// "found hit",
// UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(Hit, 0, UV));
// TestTrue(
// "UV at point inside triangle (X)",
// FMath::IsNearlyEqual(UV[0], 0.0));
// TestTrue(
// "UV at point inside triangle (Y)",
// FMath::IsNearlyEqual(UV[1], 1.0 / 3.0));

// Hit.FaceIndex = 1;
// Hit.Location = FVector_NetQuantize(0, -4, 0);
// TestTrue(
// "found hit",
// UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(Hit, 0, UV));
// TestEqual("UV at point", UV, FVector2D(0, 1));
//});
});
}
11 changes: 6 additions & 5 deletions Source/CesiumRuntime/Private/Tests/CesiumGltfSpecUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ template <typename T>
void CreateIndicesForPrimitive(
CesiumGltf::Model& model,
CesiumGltf::MeshPrimitive& primitive,
const std::string& type,
const int32_t componentType,
const std::vector<T>& indices) {
std::vector<std::byte> values = GetValuesAsBytes(indices);
const int32_t accessor =
AddBufferToModel(model, type, componentType, std::move(values));
const int32_t accessor = AddBufferToModel(
model,
AccessorSpec::Type::SCALAR,
componentType,
std::move(values));
primitive.indices = accessor;
}

Expand Down Expand Up @@ -215,7 +217,6 @@ CesiumGltf::PropertyTextureProperty& AddPropertyTexturePropertyToModel(
CesiumGltf::PropertyTextureProperty& property =
propertyTexture.properties[propertyName];
property.channels = channels;
property.index = static_cast<int32_t>(model.textures.size() - 1)
;
property.index = static_cast<int32_t>(model.textures.size() - 1);
return property;
}
Loading

0 comments on commit f23f80a

Please sign in to comment.