Skip to content

Commit

Permalink
Fix Chaos epsilon silliness by scaling meshes up and then down.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Jun 30, 2024
1 parent 0c619da commit d7f5f48
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
49 changes: 35 additions & 14 deletions Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace {
using TMeshVector2 = FVector2f;
using TMeshVector3 = FVector3f;
using TMeshVector4 = FVector4f;
constexpr double scaleFactor = 1024.0;
} // namespace

static uint32_t nextMaterialId = 0;
Expand Down Expand Up @@ -1309,6 +1310,9 @@ static void loadPrimitive(
maxPosition = glm::dvec3(max[0], max[1], max[2]);
}

minPosition *= scaleFactor;
maxPosition *= scaleFactor;

primitiveResult.dimensions =
glm::vec3(transform * glm::dvec4(maxPosition - minPosition, 0));

Expand Down Expand Up @@ -1370,9 +1374,9 @@ static void loadPrimitive(
FStaticMeshBuildVertex& vertex = StaticMeshBuildVertices[i];
uint32 vertexIndex = indices[i];
const TMeshVector3& pos = positionView[vertexIndex];
vertex.Position.X = pos.X;
vertex.Position.Y = -pos.Y;
vertex.Position.Z = pos.Z;
vertex.Position.X = pos.X * scaleFactor;
vertex.Position.Y = -pos.Y * scaleFactor;
vertex.Position.Z = pos.Z * scaleFactor;
vertex.UVs[0] = TMeshVector2(0.0f, 0.0f);
vertex.UVs[2] = TMeshVector2(0.0f, 0.0f);
RenderData->Bounds.SphereRadius = FMath::Max(
Expand All @@ -1384,9 +1388,9 @@ static void loadPrimitive(
for (int i = 0; i < StaticMeshBuildVertices.Num(); ++i) {
FStaticMeshBuildVertex& vertex = StaticMeshBuildVertices[i];
const TMeshVector3& pos = positionView[i];
vertex.Position.X = pos.X;
vertex.Position.Y = -pos.Y;
vertex.Position.Z = pos.Z;
vertex.Position.X = pos.X * scaleFactor;
vertex.Position.Y = -pos.Y * scaleFactor;
vertex.Position.Z = pos.Z * scaleFactor;
vertex.UVs[0] = TMeshVector2(0.0f, 0.0f);
vertex.UVs[2] = TMeshVector2(0.0f, 0.0f);
RenderData->Bounds.SphereRadius = FMath::Max(
Expand Down Expand Up @@ -1669,7 +1673,14 @@ static void loadPrimitive(
primitiveResult.pMaterial = &material;
primitiveResult.pCollisionMesh = nullptr;

primitiveResult.transform = transform * yInvertMatrix;
double scale = 1.0 / scaleFactor;
glm::dmat4 scaleMatrix = glm::dmat4(
glm::dvec4(scale, 0.0, 0.0, 0.0),
glm::dvec4(0.0, scale, 0.0, 0.0),
glm::dvec4(0.0, 0.0, scale, 0.0),
glm::dvec4(0.0, 0.0, 0.0, 1.0));

primitiveResult.transform = transform * yInvertMatrix * scaleMatrix;

if (primitive.mode != MeshPrimitive::Mode::POINTS &&
options.pMeshOptions->pNodeOptions->pModelOptions->createPhysicsMeshes) {
Expand Down Expand Up @@ -3834,6 +3845,14 @@ BuildChaosTriangleMeshes(
Chaos::TParticles<Chaos::FRealSingle, 3> vertices;
vertices.AddParticles(vertexCount);
for (int32 i = 0; i < vertexCount; ++i) {
// Scale up the collision meshes because Unreal has a degenerate triangle
// epsilon test in `TriangleMeshImplicitObject.cpp` that is almost laughably
// too eager. Perhaps it would be fine if our meshes actually used units of
// centimeters like UE, but they usually use meters instead. UE considering
// a triangle that is ~10cm on each side to be degenerate is... infuriating.
//
// We scale up the collision meshes by a power-of-two factor so that only
// the exponent portion of the floating point number is affected.
vertices.X(i) = vertexData[i].Position;
}

Expand All @@ -3849,13 +3868,15 @@ BuildChaosTriangleMeshes(
int32 vIndex1 = indices[index0];
int32 vIndex2 = indices[index0 + 2];

if (!isTriangleDegenerate(
vertices.X(vIndex0),
vertices.X(vIndex1),
vertices.X(vIndex2))) {
triangles.Add(Chaos::TVector<int32, 3>(vIndex0, vIndex1, vIndex2));
faceRemap.Add(i);
}
// if (!isTriangleDegenerate(
// vertices.X(vIndex0),
// vertices.X(vIndex1),
// vertices.X(vIndex2))) {
triangles.Add(Chaos::TVector<int32, 3>(vIndex0, vIndex1, vIndex2));
faceRemap.Add(i);
//} else {
// UE_LOG(LogCesium, Warning, TEXT("Degenerate"));
//}
}

TUniquePtr<TArray<int32>> pFaceRemap = MakeUnique<TArray<int32>>(faceRemap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ bool UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(
std::array<FVector, 3> Positions;
for (size_t i = 0; i < Positions.size(); i++) {
auto& Position = primData.PositionAccessor[VertexIndices[i]];
// The Y-component of glTF positions must be inverted
Positions[i] = FVector(Position[0], -Position[1], Position[2]);
// The Y-component of glTF positions must be inverted\
// TODO: don't hardcode this 1024.0 here.
Positions[i] = FVector(Position[0], -Position[1], Position[2]) * 1024.0;
}

const FVector Location =
Expand Down

0 comments on commit d7f5f48

Please sign in to comment.