---
title: "Draw navmesh debug geometry in C#"
url: https://recursiveai.org/onepath/cookbook/visualize-navmesh-polygons-and-links/
description: "Draw navmesh debug geometry in C# with OnePath. Save a top-down SVG of polygon outlines, tile portals, and off-mesh links from a tiled navmesh."
version: "1.2.0"
---

# Draw navmesh debug geometry in C#

**OnePath** is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/visualize-navmesh-polygons-and-links/visualize-navmesh-polygons-and-links.zip)

Draw walkable polygon edges, tile crossings, and jumps as an SVG. The zip includes `sample.onnm`.

```shell
dotnet add package RecursiveAI.OnePath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System.Xml.Linq;
using RecursiveAI.OnePath.Debug;
using RecursiveAI.OnePath.Mesh;

var data = File.ReadAllBytes("sample.onnm");
var mesh = NavMeshSerializer.ReadMesh(data);
var debug = NavMeshDebug.Collect(mesh);
// Use the origin for an empty mesh.
var vertices = debug.Polygons
    .SelectMany(poly => poly.Vertices)
    .DefaultIfEmpty().ToArray();
const double margin = 1;
var x = vertices.Min(v => v.X) - margin;
var z = vertices.Min(v => v.Z) - margin;
var width = vertices.Max(v => v.X) - x + margin;
var height = vertices.Max(v => v.Z) - z + margin;
var viewBox = FormattableString.Invariant(
    $"{x} {z} {width} {height}");

XNamespace ns = "http://www.w3.org/2000/svg";
var svg = new XElement(ns + "svg",
    new XAttribute("viewBox", viewBox),
    new XAttribute("fill", "none"));

foreach (var poly in debug.Polygons)
{
    var points = poly.Vertices
        .Select(v => (v.X, v.Z));
    Draw("polygon", "#334155", 0.12, points);
}

foreach (var link in debug.Links)
{
    bool portal = link.Kind == NavLinkKind.Portal;
    var color = portal ? "#0369a1" : "#b45309";
    var (a, b) = (link.A, link.B);
    Draw("polyline", color, 0.3,
        [(a.X, a.Z), (b.X, b.Z)]);
}

svg.Save("navmesh.svg");
Console.WriteLine("Wrote navmesh.svg");

void Draw(string shape, string color,
    double strokeWidth,
    IEnumerable<(double X, double Z)> points)
{
    var coords = points.Select(p =>
        FormattableString.Invariant(
            $"{p.X},{p.Z}"));
    var pairs = string.Join(" ", coords);
    var element = new XElement(ns + shape,
        new XAttribute("points", pairs),
        new XAttribute("stroke", color),
        new XAttribute("stroke-width", strokeWidth));
    svg.Add(element);
}
```

```text
Wrote navmesh.svg
```

The sample’s three squares span X = 0–8, 8–16, and 24–32; each spans Z = 0–8. The jump runs at Z = 4. `ReadMesh` returns a `TiledNavMesh`. The SVG’s view box fits the polygon bounds in X and Z, with one world unit of margin on each side.

If the input is not a valid OnePath mesh, `ReadMesh` throws `NavMeshFormatException` or a subclass before any SVG is written.

![Three outlined squares seen from above: a blue shared edge joins the first two, and an orange line crosses the gap to the third.](https://recursiveai.org/assets/onepath/visualize-navmesh-polygons-and-links.svg)

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onepath/cookbook/visualize-navmesh-polygons-and-links/visualize-navmesh-polygons-and-links.zip) and `dotnet run`.

Open `navmesh.svg` in a browser. Gray lines outline walkable polygons, blue marks a tile portal (a shared edge you can cross), and orange marks an off-mesh link (a connection such as a jump).

## Variations

### Draw the mesh you already have in memory

If you built a mesh with `NavMesh.Build2D`, pass its `Tiles` to the collector instead of loading a file. `NavMesh` is a type in the `RecursiveAI.OnePath` namespace; the builder is included in the `RecursiveAI.OnePath` NuGet package. Here `builtMesh` is the result of your build:

```csharp
var debug = NavMeshDebug.Collect(builtMesh.Tiles);
```

### Show only tile portals

Keep the polygon loop. In the link loop, draw only the links whose kind is `Portal`:

```csharp
if (link.Kind != NavLinkKind.Portal)
    continue;
```

## Related recipes

- [Build a 2D navigation mesh](https://recursiveai.org/onepath/cookbook/build-2d-navmesh/)
- [Traverse off-mesh links](https://recursiveai.org/onepath/cookbook/traverse-off-mesh-links/)

## The short answer

Call `NavMeshDebug.Collect` on a `TiledNavMesh`. It returns one ordered outline per polygon and world-space endpoints for each stitched link. Close each outline by joining the last vertex back to the first. Draw `Portal` links in one color and `OffMesh` or `OffMeshReverse` links in another. Use X and Z for a top-down picture or keep all three coordinates for a 3D overlay. OnePath supplies the geometry, not a graphics renderer.

## How do you show a navmesh in 3D?

For each polygon, draw a closed line loop from its `Vertices`. Each vertex is already in world space: X and Z locate it on the ground plane and Y gives its height. Don’t add the tile origin again. Then draw a line between `A` and `B` for each link. Drawing in 3D matters when two floors overlap in X and Z. A top-down SVG projects them onto the same plane and cannot tell you which floor an edge belongs to by height alone.

Use the outlines to see where the mesh ends, not which route a character will take. A route depends on the start and destination and on which polygons and links your query allows. The collected geometry also carries `Area` and `Flags` for every polygon if you want to color walkable surfaces by type instead of using a single outline color.

## Why is an off-mesh link missing from the debug view?

The drawing shows **stitched** links: the ones that resolved to polygons at both ends. An authored jump can still be in the tile data but absent from the drawing. Check that both tiles are loaded, that the departure point is within the link’s `Radius` of a polygon in the owning tile, and that the landing point can attach in the tile column beneath it. At both ends the height must be within `WalkableClimb` of the polygon’s vertex height range. If either end cannot attach, OnePath leaves the link unconnected without throwing an error.

Tile portals have a different rule: neighboring edges must overlap along the boundary and their heights must be close enough to cross. The blue segment marks only the part of the edge covered by the stitched portal. Portals come in pairs, so the sample draws two directed segments on the same visible blue edge. A bidirectional jump likewise has a forward and reverse segment; add an arrow at `B` when you need to show direction rather than just connectivity. See the [tiled mesh stitching rules](https://recursiveai.org/onepath/docs/navmesh-format/#stitching-links) for the attachment details.
