Validate and Optimize Meshes
Contents
[
Hide
]
Check If a Mesh Is Manifold
A mesh is manifold when every edge is shared by exactly two polygons — the mesh forms a closed, watertight surface. Several operations like boolean operations require manifold meshes as their operands. The Mesh.IsManifold() method checks this, the result is not cached so each call recalculates it:
// Check if a mesh is manifold
var box = new Box(1, 1, 1).ToMesh();
Console.WriteLine("The box mesh is manifold: {0}", box.IsManifold());
Optimize the Mesh’s Memory Usage
Real-world models exported from CAD or DCC tools often contain duplicated control points — identical vertex positions stored multiple times. The Mesh.Optimize() method eliminates duplicated control points to reduce the mesh’s memory usage:
var box = new Box(1, 1, 1).ToMesh();
// Eliminate duplicated control points to reduce memory usage
var optimized = box.Optimize(true);
Console.WriteLine("The optimized mesh is manifold: {0}", optimized.IsManifold());
// Fine-tune the tolerances used when comparing control points
var fineTuned = box.Optimize(true, 1e-8f, 1e-9f, 1e-8f);
Console.WriteLine("Control points: {0}", fineTuned.ControlPoints.Count);
The overload with four parameters lets you specify the tolerances used when comparing control points, normals and UV coordinates — vertices closer together than the tolerance are considered identical and merged.