Skip to content

Instantly share code, notes, and snippets.

@atil
Created May 6, 2016 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atil/573d87696e6a4e7c0791a2cfed4de111 to your computer and use it in GitHub Desktop.
Save atil/573d87696e6a4e7c0791a2cfed4de111 to your computer and use it in GitHub Desktop.
Extracting mesh from a recast graph
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;
public class MeshExtractor
{
public MeshExtractor()
{
// Assuming a valid recast graph exists...
var tiles = (AstarPath.active.graphs[0] as RecastGraph).GetTiles();
var tileMeshGos = new List<GameObject>();
foreach (var navmeshTile in tiles)
{
var mesh = new Mesh();
mesh.name = "tmpNavMesh";
var verts = new Vector3[navmeshTile.verts.Length];
for (var j = 0; j < navmeshTile.verts.Length; j++)
{
verts[j] = (Vector3)navmeshTile.verts[j];
}
mesh.vertices = verts;
mesh.triangles = navmeshTile.tris;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
var tempGo = new GameObject("TempMeshGo");
tempGo.AddComponent<MeshFilter>().mesh = mesh;
tileMeshGos.Add(tempGo);
}
var combineInstances = new List<CombineInstance>();
foreach (var tileMeshGo in tileMeshGos)
{
combineInstances.Add(new CombineInstance()
{
mesh = tileMeshGo.GetComponent<MeshFilter>().mesh,
transform = tileMeshGo.transform.localToWorldMatrix
});
}
var go = new GameObject("RecastMesh");
go.AddComponent<MeshFilter>().mesh = new Mesh();
go.GetComponent<MeshFilter>().mesh.CombineMeshes(combineInstances.ToArray());
go.AddComponent<MeshCollider>().sharedMesh = go.GetComponent<MeshFilter>().mesh;
foreach (var tileMeshGo in tileMeshGos)
{
Object.Destroy(tileMeshGo);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment