From 96f9fcb00966fe9784f9a8eebaf208277aa6a889 Mon Sep 17 00:00:00 2001 From: Murry Lancashire Date: Thu, 1 Oct 2026 12:18:59 +1000 Subject: [PATCH] vert weld fix. --- Source/Engine/Graphics/Models/ModelData.Tool.cpp | 13 +++++++++++-- Source/Engine/Tools/ModelTool/SpatialSort.cpp | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Graphics/Models/ModelData.Tool.cpp b/Source/Engine/Graphics/Models/ModelData.Tool.cpp index f1c84cfb7..86e67ec0d 100644 --- a/Source/Engine/Graphics/Models/ModelData.Tool.cpp +++ b/Source/Engine/Graphics/Models/ModelData.Tool.cpp @@ -193,6 +193,7 @@ int32 FindVertex(const MeshData& mesh, int32 vertexIndex, int32 startIndex, int3 #if USE_SPATIAL_SORT , const Assimp::SpatialSort& spatialSort , std::vector& spatialSortCache + , float posEpsilon #endif ) { @@ -204,7 +205,7 @@ int32 FindVertex(const MeshData& mesh, int32 vertexIndex, int32 startIndex, int3 #if USE_SPATIAL_SORT const Float3 vPosition = mesh.Positions[vertexIndex]; - spatialSort.FindPositions(*(aiVector3D*)&vPosition, 1e-5f, spatialSortCache); + spatialSort.FindPositions(*(aiVector3D*)&vPosition, posEpsilon, spatialSortCache); if (spatialSortCache.empty()) return INVALID_INDEX; @@ -298,6 +299,14 @@ void MeshData::BuildIndexBuffer() Assimp::SpatialSort vertexFinder; vertexFinder.Fill((const aiVector3D*)Positions.Get(), vertexCount, sizeof(Float3)); std::vector spatialSortCache; + + // Ensure that the weld radius stays above float precision based on the mesh's largest coordinate + float maxAbs = 0.0f; + for (int32 i = 0; i < vertexCount; i++) + { + maxAbs = Math::Max(maxAbs, Positions[i].GetAbsolute().MaxValue()); + } + const float posEpsilon = Math::Max(1e-5f, maxAbs * 1e-6f); #endif // Build index buffer @@ -306,7 +315,7 @@ void MeshData::BuildIndexBuffer() // Find duplicated vertex before the current one const int32 reuseVertexIndex = FindVertex(*this, vertexIndex, 0, vertexIndex, mapping #if USE_SPATIAL_SORT - , vertexFinder, spatialSortCache + , vertexFinder, spatialSortCache, posEpsilon #endif ); if (reuseVertexIndex == INVALID_INDEX) diff --git a/Source/Engine/Tools/ModelTool/SpatialSort.cpp b/Source/Engine/Tools/ModelTool/SpatialSort.cpp index de13084dc..2cc64a7d6 100644 --- a/Source/Engine/Tools/ModelTool/SpatialSort.cpp +++ b/Source/Engine/Tools/ModelTool/SpatialSort.cpp @@ -164,7 +164,7 @@ void SpatialSort::FindPositions(const aiVector3D& pPosition, // Add all positions inside the distance range within the given radius to the result aray std::vector::const_iterator it = mPositions.begin() + index; const ai_real pSquared = pRadius * pRadius; - while (it->mDistance < maxDist) + while (it->mDistance <= maxDist) { if ((it->mPosition - pPosition).SquareLength() < pSquared) poResults.push_back(it->mIndex);