/
loadNavMesh.ts
32 lines (31 loc) · 1.12 KB
/
loadNavMesh.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import fs from 'fs-extra'
import wavefrontObjParser from 'wavefront-obj-parser'
import NavMesh from 'navmesh';
export async function loadNavMesh(fileNameNavMesh: string) {
const fileNavMesh = await fs.readFile(`./data/${fileNameNavMesh}.obj`, 'utf8')
var jsonNavMesh = wavefrontObjParser(fileNavMesh)
const meshPolygonPoints = []
const vertexPositions = jsonNavMesh.vertexPositions
const array = jsonNavMesh.vertexPositionIndices
for (let index = 0; index < array.length; index += 4) {
const aIndex = array[index];
const bIndex = array[index + 1]
const cIndex = array[index + 2]
meshPolygonPoints.push([
{
x: vertexPositions[aIndex * 3],
y: vertexPositions[(aIndex * 3) + 2]
},
{
x: vertexPositions[bIndex * 3],
y: vertexPositions[(bIndex * 3) + 2]
},
{
x: vertexPositions[cIndex * 3],
y: vertexPositions[(cIndex * 3) + 2]
}
])
}
const navMesh = new NavMesh(meshPolygonPoints);
return navMesh
}