Skip to content

About

A plugin that wraps zeux/meshoptimizer C API in a modern, idiomatic C++ facade — plus a worked example of hooking a step into the Interchange import pipeline.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Streaming Tuner

Unreal Mesh Optimization Pipeline

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 standard UStaticMesh assets 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, the FMeshDescription bridge, and the documented engine analysis.


What's inside

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.

Design decisions

  • 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/TConstArrayView instead of ptr + count, enum class + ENUM_CLASS_FLAGS instead 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; simplify changes geometry and is gated behind explicit LOD generation — never applied silently to LOD0.

The C++ facade (MeshOpt)

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 FMeshDescription bridge

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).


Interchange pipeline integration (reference)

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:

  1. Build the plugin (see Building).
  2. Content Browser → right-click → Interchange → Interchange Pipeline, pick MeshOptimizerInterchangePipeline, name it e.g. IP_MeshOptimizer. (The stack takes a pipeline asset; a bare C++ UClass cannot be referenced directly.)
  3. Project Settings → Interchange → Pipeline Stacks → the Assets stack → add IP_MeshOptimizer at the end of the pipeline list.
  4. Import a static mesh (glTF/OBJ go through Interchange; FBX if Interchange-for-FBX is enabled) and watch the Output Log filtered by LogMeshOptimizer for the before/after metrics.

Settings live under Project Settings → Plugins → Mesh Optimizer (UMeshOptimizerSettings): per-pass toggles, overdraw threshold, and a "skip Nanite meshes" option.


Why the import pass doesn't help (and what does)

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.

The engine already does the lossless work — unconditionally

FStaticMeshBuilder (verified in StaticMeshBuilder.cpp, UE 5.8) on every Build():

  • re-welds vertex instances into render vertices, and
  • for meshes with < 300k vertex 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.

The timing trap

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.

Measured on the pirate test mesh

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.

Where meshoptimizer does add value in UE

  • 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 an IMeshReduction backend, not an import hook.
  • Meshes ≥ 300k vertex instances, where UE skips its own optimization (though these are usually Nanite candidates).
  • Non-FStaticMeshBuilder paths: procedural/runtime geometry uploaded directly, custom vertex factories, export pipelines, or vertex/index buffer compression for streaming.

Building

The repository is the plugin (its root contains MeshOptimizer.uplugin).

  1. Clone/copy it into your project's Plugins/MeshOptimizer/ folder.
  2. 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.
  3. Build the editor target and enable Mesh Optimizer under Edit → Plugins.

Requirements

  • Unreal Engine 5.8 (should build on 5.7).
  • A C++ project (the plugin has C++ modules).

Credits & License

  • meshoptimizer © Arseny Kapoulkine — MIT, vendored unchanged under Source/ThirdParty/MeshOptimizerLib/ (its LICENSE.md is retained).
  • Plugin wrapper, bridge, and Interchange example by Aleksey Karpov (IchiQ).
  • Released under the MIT License.

About

A plugin that wraps zeux/meshoptimizer C API in a modern, idiomatic C++ facade — plus a worked example of hooking a step into the Interchange import pipeline.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages