Skip to content

Commit d7f5f48

Browse files
committed
Fix Chaos epsilon silliness by scaling meshes up and then down.
1 parent 0c619da commit d7f5f48

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

Source/CesiumRuntime/Private/CesiumGltfComponent.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ namespace {
8383
using TMeshVector2 = FVector2f;
8484
using TMeshVector3 = FVector3f;
8585
using TMeshVector4 = FVector4f;
86+
constexpr double scaleFactor = 1024.0;
8687
} // namespace
8788

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

1313+
minPosition *= scaleFactor;
1314+
maxPosition *= scaleFactor;
1315+
13121316
primitiveResult.dimensions =
13131317
glm::vec3(transform * glm::dvec4(maxPosition - minPosition, 0));
13141318

@@ -1370,9 +1374,9 @@ static void loadPrimitive(
13701374
FStaticMeshBuildVertex& vertex = StaticMeshBuildVertices[i];
13711375
uint32 vertexIndex = indices[i];
13721376
const TMeshVector3& pos = positionView[vertexIndex];
1373-
vertex.Position.X = pos.X;
1374-
vertex.Position.Y = -pos.Y;
1375-
vertex.Position.Z = pos.Z;
1377+
vertex.Position.X = pos.X * scaleFactor;
1378+
vertex.Position.Y = -pos.Y * scaleFactor;
1379+
vertex.Position.Z = pos.Z * scaleFactor;
13761380
vertex.UVs[0] = TMeshVector2(0.0f, 0.0f);
13771381
vertex.UVs[2] = TMeshVector2(0.0f, 0.0f);
13781382
RenderData->Bounds.SphereRadius = FMath::Max(
@@ -1384,9 +1388,9 @@ static void loadPrimitive(
13841388
for (int i = 0; i < StaticMeshBuildVertices.Num(); ++i) {
13851389
FStaticMeshBuildVertex& vertex = StaticMeshBuildVertices[i];
13861390
const TMeshVector3& pos = positionView[i];
1387-
vertex.Position.X = pos.X;
1388-
vertex.Position.Y = -pos.Y;
1389-
vertex.Position.Z = pos.Z;
1391+
vertex.Position.X = pos.X * scaleFactor;
1392+
vertex.Position.Y = -pos.Y * scaleFactor;
1393+
vertex.Position.Z = pos.Z * scaleFactor;
13901394
vertex.UVs[0] = TMeshVector2(0.0f, 0.0f);
13911395
vertex.UVs[2] = TMeshVector2(0.0f, 0.0f);
13921396
RenderData->Bounds.SphereRadius = FMath::Max(
@@ -1669,7 +1673,14 @@ static void loadPrimitive(
16691673
primitiveResult.pMaterial = &material;
16701674
primitiveResult.pCollisionMesh = nullptr;
16711675

1672-
primitiveResult.transform = transform * yInvertMatrix;
1676+
double scale = 1.0 / scaleFactor;
1677+
glm::dmat4 scaleMatrix = glm::dmat4(
1678+
glm::dvec4(scale, 0.0, 0.0, 0.0),
1679+
glm::dvec4(0.0, scale, 0.0, 0.0),
1680+
glm::dvec4(0.0, 0.0, scale, 0.0),
1681+
glm::dvec4(0.0, 0.0, 0.0, 1.0));
1682+
1683+
primitiveResult.transform = transform * yInvertMatrix * scaleMatrix;
16731684

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

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

3852-
if (!isTriangleDegenerate(
3853-
vertices.X(vIndex0),
3854-
vertices.X(vIndex1),
3855-
vertices.X(vIndex2))) {
3856-
triangles.Add(Chaos::TVector<int32, 3>(vIndex0, vIndex1, vIndex2));
3857-
faceRemap.Add(i);
3858-
}
3871+
// if (!isTriangleDegenerate(
3872+
// vertices.X(vIndex0),
3873+
// vertices.X(vIndex1),
3874+
// vertices.X(vIndex2))) {
3875+
triangles.Add(Chaos::TVector<int32, 3>(vIndex0, vIndex1, vIndex2));
3876+
faceRemap.Add(i);
3877+
//} else {
3878+
// UE_LOG(LogCesium, Warning, TEXT("Degenerate"));
3879+
//}
38593880
}
38603881

38613882
TUniquePtr<TArray<int32>> pFaceRemap = MakeUnique<TArray<int32>>(faceRemap);

Source/CesiumRuntime/Private/CesiumMetadataPickingBlueprintLibrary.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ bool UCesiumMetadataPickingBlueprintLibrary::FindUVFromHit(
127127
std::array<FVector, 3> Positions;
128128
for (size_t i = 0; i < Positions.size(); i++) {
129129
auto& Position = primData.PositionAccessor[VertexIndices[i]];
130-
// The Y-component of glTF positions must be inverted
131-
Positions[i] = FVector(Position[0], -Position[1], Position[2]);
130+
// The Y-component of glTF positions must be inverted\
131+
// TODO: don't hardcode this 1024.0 here.
132+
Positions[i] = FVector(Position[0], -Position[1], Position[2]) * 1024.0;
132133
}
133134

134135
const FVector Location =

0 commit comments

Comments
 (0)