Skip to content

Commit

Permalink
Merge pull request #1515 from CesiumGS/texture-coordinate-memory
Browse files Browse the repository at this point in the history
Manually copy vertices to work around UE 5.3/5.4 bug.
  • Loading branch information
j9liu authored Sep 10, 2024
2 parents aa6c949 + d6ae651 commit 5dbc37f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

### Next version (not released yet)

##### Fixes :wrench:

- Drastically reduced tile mesh memory usage in UE 5.3 and 5.4 by working around a bug that causes those engine versions to add more texture coordinate sets than necessary.

### v2.8.0 - 2024-09-02

##### Additions :tada:
Expand Down
37 changes: 33 additions & 4 deletions Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,11 +1628,40 @@ static void loadPrimitive(
ColorVertexBuffer.Init(StaticMeshBuildVertices, false);
}

LODResources.VertexBuffers.StaticMeshVertexBuffer.Init(
StaticMeshBuildVertices,
gltfToUnrealTexCoordMap.size() == 0 ? 1
: gltfToUnrealTexCoordMap.size(),
uint32 numberOfTextureCoordinates =
gltfToUnrealTexCoordMap.size() == 0
? 1
: uint32(gltfToUnrealTexCoordMap.size());

FStaticMeshVertexBuffer& vertexBuffer =
LODResources.VertexBuffers.StaticMeshVertexBuffer;
vertexBuffer.Init(
StaticMeshBuildVertices.Num(),
numberOfTextureCoordinates,
false);

// Manually copy the vertices into the buffer. We do this because UE 5.3
// and 5.4 have a bug where the overload of `FStaticMeshVertexBuffer::Init`
// taking an array of `FStaticMeshBuildVertex` will create a mesh with all 8
// sets of texture coordinates, even when we usually only need one or two.
// See https://github.com/CesiumGS/cesium-unreal/issues/1513
for (uint32 vertexIndex = 0;
vertexIndex < uint32(StaticMeshBuildVertices.Num());
++vertexIndex) {
const FStaticMeshBuildVertex& source =
StaticMeshBuildVertices[vertexIndex];

vertexBuffer.SetVertexTangents(
vertexIndex,
source.TangentX,
source.TangentY,
source.TangentZ);
for (uint32 uvIndex = 0; uvIndex < numberOfTextureCoordinates;
uvIndex++) {
vertexBuffer
.SetVertexUV(vertexIndex, uvIndex, source.UVs[uvIndex], false);
}
}
}

FStaticMeshSectionArray& Sections = LODResources.Sections;
Expand Down

0 comments on commit 5dbc37f

Please sign in to comment.