导航网格 API
createNavMesh
Section titled “createNavMesh”function createNavMesh(): NavMesh构建导航网格
Section titled “构建导航网格”const navmesh = createNavMesh();
// 添加凸多边形const id1 = navmesh.addPolygon([ { x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }]);
const id2 = navmesh.addPolygon([ { x: 10, y: 0 }, { x: 20, y: 0 }, { x: 20, y: 10 }, { x: 10, y: 10 }]);
// 方式1:自动检测共享边并建立连接navmesh.build();
// 方式2:手动设置连接navmesh.setConnection(id1, id2, { left: { x: 10, y: 0 }, right: { x: 10, y: 10 }});// 查找包含点的多边形const polygon = navmesh.findPolygonAt(5, 5);
// 检查位置是否可通行navmesh.isWalkable(5, 5);
// 寻路(内部使用漏斗算法优化路径)const result = navmesh.findPath(1, 1, 18, 8);导航网格适合:
- 复杂不规则地形
- 需要精确路径的场景
- 多边形数量远少于网格单元格的大地图
// 从编辑器导出的导航网格数据const navData = await loadNavMeshData('level1.navmesh');
const navmesh = createNavMesh();for (const poly of navData.polygons) { navmesh.addPolygon(poly.vertices);}navmesh.build();