A UE 5.8 plugin that vendors zeux/meshoptimizer (MIT)
unchanged and wraps its C API in a modern, idiomatic C++ facade — plus a worked example of
hooking a step into the Interchange import pipeline.
Engine: UE 5.8 (developed and tested against 5.7 and 5.8) · Platforms: Win64 / Mac / Linux · License: MIT · meshoptimizer: v1.2
⚠️ Read this first — honest findings below. The import-pipeline pass in this repo is included as a reference implementation, but for standardUStaticMeshassets the engine's own build step re-does the same work, so the lossless passes do not reach the GPU. See Why the import pass doesn't help (and what does). The value here is the C++ facade, theFMeshDescriptionbridge, and the documented engine analysis.
| Module | Type | Purpose |
|---|---|---|
MeshOptimizerLib |
ThirdParty | Vendored meshoptimizer v1.2 (meshoptimizer.h + *.cpp), unchanged. |
MeshOptimizerRuntime |
Runtime | namespace MeshOpt — the modern C++ facade + FMeshDescription bridge. |
MeshOptimizerEditor |
Editor | UInterchangePipelineBase post-import step + UDeveloperSettings. |
- The library is never rewritten. Vendoring it as-is keeps upstream compatibility (thousands of commits, new algorithms, bug fixes), its zero-dependency C-compatible header, ABI stability, and its correctness-critical SIMD code. We wrap, we don't fork.
- The facade is idiomatic UE:
TArrayView/TConstArrayViewinstead ofptr + count,enum class+ENUM_CLASS_FLAGSinstead of bitmask ints,TArray/RAII instead of manual buffer budgets, result structs instead of out-parameters. - Operations are split by safety: reorder/reindex/weld are lossless;
simplifychanges geometry and is gated behind explicit LOD generation — never applied silently to LOD0.
Thin, allocation-safe wrappers over the stable meshopt_* C API.
#include "MeshOpt.h"
TArray<uint32> Indices = /* triangle list */;
TArray<FVector3f> Positions = /* vertex positions */;
// Lossless index reordering
TArray<uint32> Cached = MeshOpt::OptimizeVertexCache(Indices, Positions.Num());
TArray<uint32> Ordered = MeshOpt::OptimizeOverdraw(Cached, Positions, /*threshold*/ 1.05f);
// Geometry-changing simplification (this is what survives a UE rebuild — see findings below)
MeshOpt::FSimplifyResult Lod = MeshOpt::Simplify(
Indices, Positions, /*TargetIndexCount*/ Indices.Num() / 2, /*TargetError*/ 0.01f);
// Objective, GPU-model-independent quality metrics
MeshOpt::FVertexCacheStats Vcs = MeshOpt::AnalyzeVertexCache(Indices, Positions.Num());
// Vcs.ACMR, Vcs.ATVR, ...Templated helpers (OptimizeVertexFetch<T>, GenerateVertexRemap<T>, RemapVertexBuffer<T>) work
on any user vertex struct; multi-stream variants (GenerateVertexRemapMulti) handle SoA layouts.
The bulk of the real work — converting between UE's FMeshDescription and flat SoA buffers.
#include "MeshOptFlatMesh.h"
MeshOpt::FFlatMesh Flat = MeshOpt::ExtractFlatMesh(MeshDescription);
MeshOpt::FOptimizeStats Stats = MeshOpt::OptimizeFlatMesh(
Flat, MeshOpt::EOptimizePasses::AllLossless); // Weld -> VertexCache -> Overdraw -> VertexFetch
MeshOpt::BuildMeshDescription(Flat, MeshDescription);
// Stats.Before / Stats.After hold ACMR / ATVR / Overdraw / Overfetch for a measurable delta.FFlatMesh is a per-vertex-instance SoA snapshot (positions, normals, tangents, colors, UV channels)
with triangles bucketed into contiguous per-polygon-group sections. The bridge is validated as
lossless when fed a fully built mesh (see findings).
UMeshOptimizerInterchangePipeline is a UInterchangePipelineBase that runs on import and applies
the configured passes to imported static meshes. This is the part worth reading as an example of
how to add an Interchange step, even though the lossless result is redundant on standard meshes.
Wiring it up:
- Build the plugin (see Building).
- Content Browser → right-click → Interchange → Interchange Pipeline, pick
MeshOptimizerInterchangePipeline, name it e.g.IP_MeshOptimizer. (The stack takes a pipeline asset; a bare C++UClasscannot be referenced directly.) - Project Settings → Interchange → Pipeline Stacks → the
Assetsstack → addIP_MeshOptimizerat the end of the pipeline list. - Import a static mesh (glTF/OBJ go through Interchange; FBX if Interchange-for-FBX is enabled) and
watch the Output Log filtered by
LogMeshOptimizerfor the before/after metrics.
Settings live under Project Settings → Plugins → Mesh Optimizer (UMeshOptimizerSettings):
per-pass toggles, overdraw threshold, and a "skip Nanite meshes" option.
This is the interesting part, and the reason the plugin ships as a reference rather than a shipping optimizer. It is documented honestly because understanding why is more valuable than the pass itself.
FStaticMeshBuilder (verified in StaticMeshBuilder.cpp, UE 5.8) on every Build():
- re-welds vertex instances into render vertices, and
- for meshes with
< 300kvertex instances, re-runs its own Forsyth vertex-cache and vertex-fetch optimizer (BuildOptimizationHelper::CacheOptimizeVertexAndIndexBuffer).
The algorithm is selected by the CVar r.TriangleOrderOptimization
(0 = NvTriStrip, 1 = Forsyth [default], 2 = none, "debugging purposes only"). There is no
per-mesh toggle to keep an externally-supplied vertex/index order.
Consequence: any weld or index reordering we write into the FMeshDescription is thrown away and
redone by the engine's builder before the mesh ever reaches the GPU.
| Hook | When | Result |
|---|---|---|
ExecutePostFactoryPipeline |
before PostEditChange (pre-build) |
normals/tangents not computed yet → our attribute-aware weld over-merges vertices → fewer verts and corrupted shading. |
ExecutePostImportPipeline |
after the asset is built | the asset is already built; the roundtrip is lossless but simply reproduces the engine's own result. |
| Scenario | Render vertices |
|---|---|
| Imported without the plugin | 3199 (UE's weld + Forsyth) |
Pass at ExecutePostFactoryPipeline |
3006 — not a win; an over-merge artifact from unready normals |
Pass at ExecutePostImportPipeline |
no change — confirms the roundtrip is lossless and matches UE |
The earlier "ACMR 2.009 → 0.763" style numbers were measured against the raw, un-built
FMeshDescription (10064 verts) — a baseline the engine never ships. Against the engine's actual
build output the lossless passes add ~nothing.
- Geometry-changing simplification (
meshopt_simplify/simplifyWithAttributes): the builder can't undo fewer triangles, so simplification-based LOD generation survives — best delivered as an explicit editor action or anIMeshReductionbackend, not an import hook. - Meshes ≥ 300k vertex instances, where UE skips its own optimization (though these are usually Nanite candidates).
- Non-
FStaticMeshBuilderpaths: procedural/runtime geometry uploaded directly, custom vertex factories, export pipelines, or vertex/index buffer compression for streaming.
The repository is the plugin (its root contains MeshOptimizer.uplugin).
- Clone/copy it into your project's
Plugins/MeshOptimizer/folder. - Regenerate Visual Studio project files (right-click the
.uproject→ Generate Visual Studio project files). Required after adding the plugin — otherwise the linker won't see the modules. - Build the editor target and enable Mesh Optimizer under Edit → Plugins.
- Unreal Engine 5.8 (should build on 5.7).
- A C++ project (the plugin has C++ modules).
- meshoptimizer © Arseny Kapoulkine — MIT, vendored unchanged under
Source/ThirdParty/MeshOptimizerLib/(itsLICENSE.mdis retained). - Plugin wrapper, bridge, and Interchange example by Aleksey Karpov (IchiQ).
- Released under the MIT License.
