From d32f688be2963e38b8af1814a0e17071e6a4c96c Mon Sep 17 00:00:00 2001 From: Project PLATEAU <79615787+Project-PLATEAU@users.noreply.github.com> Date: Fri, 24 Feb 2023 01:01:45 +0900 Subject: [PATCH 1/4] Optimize bounding box --- .../Private/CesiumGltfComponent.cpp | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index 41f534337..3a93d1e9d 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -834,19 +834,16 @@ static void loadPrimitive( const std::vector& max = positionAccessor.max; glm::dvec3 minPosition{std::numeric_limits::max()}; glm::dvec3 maxPosition{std::numeric_limits::lowest()}; - if (min.size() != 3 || max.size() != 3) { - for (int32_t i = 0; i < positionView.size(); ++i) { - minPosition.x = glm::min(minPosition.x, positionView[i].X); - minPosition.y = glm::min(minPosition.y, positionView[i].Y); - minPosition.z = glm::min(minPosition.z, positionView[i].Z); - - maxPosition.x = glm::max(maxPosition.x, positionView[i].X); - maxPosition.y = glm::max(maxPosition.y, positionView[i].Y); - maxPosition.z = glm::max(maxPosition.z, positionView[i].Z); - } - } else { - minPosition = glm::dvec3(min[0], min[1], min[2]); - maxPosition = glm::dvec3(max[0], max[1], max[2]); + for (int32_t i = 0; i < indicesView.size(); ++i) { + const uint32 index = indicesView[i]; + + minPosition.x = glm::min(minPosition.x, positionView[index].X); + minPosition.y = glm::min(minPosition.y, positionView[index].Y); + minPosition.z = glm::min(minPosition.z, positionView[index].Z); + + maxPosition.x = glm::max(maxPosition.x, positionView[index].X); + maxPosition.y = glm::max(maxPosition.y, positionView[index].Y); + maxPosition.z = glm::max(maxPosition.z, positionView[index].Z); } #if ENGINE_MAJOR_VERSION >= 5 @@ -1904,7 +1901,6 @@ static void loadPrimitiveGameThreadPart( RF_Transient | RF_DuplicateTransient | RF_TextExportTransient); pMesh->pModel = loadResult.pModel; pMesh->pMeshPrimitive = loadResult.pMeshPrimitive; - pMesh->boundingVolume = boundingVolume; pMesh->SetRenderCustomDepth(pGltf->CustomDepthParameters.RenderCustomDepth); pMesh->SetCustomDepthStencilWriteMask( pGltf->CustomDepthParameters.CustomDepthStencilWriteMask); From 2e49cb35d00032d205b976b6f3fa97b326c8a5c5 Mon Sep 17 00:00:00 2001 From: Project PLATEAU <79615787+Project-PLATEAU@users.noreply.github.com> Date: Fri, 7 Apr 2023 11:31:01 +0900 Subject: [PATCH 2/4] Optimize only when positions are shared --- .../Private/CesiumGltfComponent.cpp | 73 ++++++++++++++++--- .../CesiumRuntime/Private/CreateGltfOptions.h | 1 + 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index 3a93d1e9d..4478189cd 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -834,16 +834,33 @@ static void loadPrimitive( const std::vector& max = positionAccessor.max; glm::dvec3 minPosition{std::numeric_limits::max()}; glm::dvec3 maxPosition{std::numeric_limits::lowest()}; - for (int32_t i = 0; i < indicesView.size(); ++i) { - const uint32 index = indicesView[i]; - - minPosition.x = glm::min(minPosition.x, positionView[index].X); - minPosition.y = glm::min(minPosition.y, positionView[index].Y); - minPosition.z = glm::min(minPosition.z, positionView[index].Z); - - maxPosition.x = glm::max(maxPosition.x, positionView[index].X); - maxPosition.y = glm::max(maxPosition.y, positionView[index].Y); - maxPosition.z = glm::max(maxPosition.z, positionView[index].Z); + if (!options.sharedPositions) { + if (min.size() != 3 || max.size() != 3) { + for (int32_t i = 0; i < positionView.size(); ++i) { + minPosition.x = glm::min(minPosition.x, positionView[i].X); + minPosition.y = glm::min(minPosition.y, positionView[i].Y); + minPosition.z = glm::min(minPosition.z, positionView[i].Z); + + maxPosition.x = glm::max(maxPosition.x, positionView[i].X); + maxPosition.y = glm::max(maxPosition.y, positionView[i].Y); + maxPosition.z = glm::max(maxPosition.z, positionView[i].Z); + } + } else { + minPosition = glm::dvec3(min[0], min[1], min[2]); + maxPosition = glm::dvec3(max[0], max[1], max[2]); + } + } else { + for (int32_t i = 0; i < indicesView.size(); ++i) { + const uint32 index = indicesView[i]; + + minPosition.x = glm::min(minPosition.x, positionView[index].X); + minPosition.y = glm::min(minPosition.y, positionView[index].Y); + minPosition.z = glm::min(minPosition.z, positionView[index].Z); + + maxPosition.x = glm::max(maxPosition.x, positionView[index].X); + maxPosition.y = glm::max(maxPosition.y, positionView[index].Y); + maxPosition.z = glm::max(maxPosition.z, positionView[index].Z); + } } #if ENGINE_MAJOR_VERSION >= 5 @@ -1376,7 +1393,39 @@ static void loadMesh( result = LoadMeshResult(); result->primitiveResults.reserve(mesh.primitives.size()); for (const CesiumGltf::MeshPrimitive& primitive : mesh.primitives) { - CreatePrimitiveOptions primitiveOptions = {&options, &*result, &primitive}; + bool sharedPositions = false; + + if (primitive.indices >= 0) { + auto positionAccessorIt = primitive.attributes.find("POSITION"); + if (positionAccessorIt == primitive.attributes.end()) { + // This primitive doesn't have a POSITION semantic, ignore it. + continue; + } + + int positionAccessorID = positionAccessorIt->second; + + for (const CesiumGltf::MeshPrimitive& otherPrimitive : mesh.primitives) { + if (&primitive == &otherPrimitive) { + continue; + } + + auto otherPositionAccessorIt = + otherPrimitive.attributes.find("POSITION"); + if (otherPositionAccessorIt == otherPrimitive.attributes.end()) { + // This primitive doesn't have a POSITION semantic, ignore it. + continue; + } + + int otherPositionAccessorID = otherPositionAccessorIt->second; + + if (positionAccessorID == otherPositionAccessorID) { + sharedPositions = true; + break; + } + } + } + + CreatePrimitiveOptions primitiveOptions = {&options, &*result, &primitive, sharedPositions}; auto& primitiveResult = result->primitiveResults.emplace_back(); loadPrimitive(primitiveResult, transform, primitiveOptions); @@ -1901,7 +1950,7 @@ static void loadPrimitiveGameThreadPart( RF_Transient | RF_DuplicateTransient | RF_TextExportTransient); pMesh->pModel = loadResult.pModel; pMesh->pMeshPrimitive = loadResult.pMeshPrimitive; - pMesh->SetRenderCustomDepth(pGltf->CustomDepthParameters.RenderCustomDepth); + pMesh->boundingVolume = boundingVolume; pMesh->SetCustomDepthStencilWriteMask( pGltf->CustomDepthParameters.CustomDepthStencilWriteMask); pMesh->SetCustomDepthStencilValue( diff --git a/Source/CesiumRuntime/Private/CreateGltfOptions.h b/Source/CesiumRuntime/Private/CreateGltfOptions.h index 8a423fc21..05a3e237a 100644 --- a/Source/CesiumRuntime/Private/CreateGltfOptions.h +++ b/Source/CesiumRuntime/Private/CreateGltfOptions.h @@ -39,5 +39,6 @@ struct CreatePrimitiveOptions { const CreateMeshOptions* pMeshOptions = nullptr; const LoadGltfResult::LoadMeshResult* pHalfConstructedMeshResult = nullptr; const CesiumGltf::MeshPrimitive* pPrimitive = nullptr; + const bool sharedPositions = false; }; } // namespace CreateGltfOptions From 2f72b420be317c60d31e35211fb5dc0a2c87c251 Mon Sep 17 00:00:00 2001 From: Project PLATEAU <79615787+Project-PLATEAU@users.noreply.github.com> Date: Fri, 7 Apr 2023 11:36:23 +0900 Subject: [PATCH 3/4] Bounding volume for navigation --- Source/CesiumRuntime/Private/CesiumGltfComponent.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index 4478189cd..45c0c9e6f 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -1950,7 +1950,9 @@ static void loadPrimitiveGameThreadPart( RF_Transient | RF_DuplicateTransient | RF_TextExportTransient); pMesh->pModel = loadResult.pModel; pMesh->pMeshPrimitive = loadResult.pMeshPrimitive; - pMesh->boundingVolume = boundingVolume; + if (!createNavCollision) { + pMesh->boundingVolume = boundingVolume; + } pMesh->SetCustomDepthStencilWriteMask( pGltf->CustomDepthParameters.CustomDepthStencilWriteMask); pMesh->SetCustomDepthStencilValue( From 9b3406dca1f29c189d53f4169dc384520b5b9864 Mon Sep 17 00:00:00 2001 From: Project PLATEAU <79615787+Project-PLATEAU@users.noreply.github.com> Date: Mon, 10 Apr 2023 19:29:06 +0900 Subject: [PATCH 4/4] Undo accidentally deleted lines --- Source/CesiumRuntime/Private/CesiumGltfComponent.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index 45c0c9e6f..4bf6d1186 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -1950,6 +1950,7 @@ static void loadPrimitiveGameThreadPart( RF_Transient | RF_DuplicateTransient | RF_TextExportTransient); pMesh->pModel = loadResult.pModel; pMesh->pMeshPrimitive = loadResult.pMeshPrimitive; + pMesh->SetRenderCustomDepth(pGltf->CustomDepthParameters.RenderCustomDepth); if (!createNavCollision) { pMesh->boundingVolume = boundingVolume; }