forked from QuantumCoderQC/haxerecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildnavjs.js
137 lines (113 loc) · 4.05 KB
/
buildnavjs.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const fs = require('fs');
const Recast = require("../haxerecast/recastjs/recast");
// Get mesh path and NavMesh config
const recast_settings = process.argv[3];
const path_mesh = './' + process.argv[2] + '.obj'
// Read OBJ file and build NavMesh
readObjFile(path_mesh, (error, vertices, vertexLength, indices, indexLength) => {
if (error) {
console.error('Error:', error);
} else {
buildNavMesh(vertices, vertexLength, indices, indexLength);
}
});
// Parse OBJ file
function readObjFile(objFilePath, callback) {
try {
// Read the OBJ file
const objFileContent = fs.readFileSync(objFilePath, 'utf-8');
// Process the OBJ content
const vertices = [];
const indices = [];
vertices.length
const lines = objFileContent.split('\n');
lines.forEach((line) => {
const parts = line.trim().split(/\s+/);
if (parts[0] === 'v') {
// Vertex data
const x = parseFloat(parts[1]);
const y = parseFloat(parts[2]);
const z = parseFloat(parts[3]);
vertices.push(x, y, z);
} else if (parts[0] === 'f') {
// Face data
const faceIndices = parts.slice(1).map((vertex) => parseInt(vertex.split('/')[0]) - 1);
indices.push(...faceIndices);
}
});
// Call the callback with the result
callback(null, vertices, vertices.length, indices, indices.length);
} catch (error) {
callback(error);
}
}
function buildNavMesh (vertices, vertexLength, indices, indexLength) {
recastSettings = JSON.parse(recast_settings);
Recast().then(function(Recast) {
// Copy recast config settings
var recastConfig = new Recast.rcConfig();
recastConfig.width = recastSettings.width;
recastConfig.height = recastSettings.height;
if(recastSettings.tiledMesh) {
recastConfig.tileSize = recastSettings.tileSize;
}
else {
recastConfig.tileSize = 0;
}
recastConfig.borderSize = recastSettings.borderSize;
recastConfig.cs = recastSettings.cellSize;
recastConfig.ch = recastSettings.cellHeight;
recastConfig.walkableSlopeAngle = recastSettings.walkableSlopeAngle;
recastConfig.walkableHeight = recastSettings.walkableHeight;
recastConfig.walkableClimb = recastSettings.walkableClimb;
recastConfig.walkableRadius = recastSettings.walkableRadius;
recastConfig.maxEdgeLen = recastSettings.maxEdgeLen;
recastConfig.maxSimplificationError = recastSettings.maxSimplificationError;
recastConfig.minRegionArea = recastSettings.minRegionArea;
recastConfig.mergeRegionArea = recastSettings.mergeRegionArea;
recastConfig.maxVertsPerPoly = recastSettings.maxVertsPerPoly;
recastConfig.detailSampleDist = recastSettings.detailSampleDist;
recastConfig.detailSampleMaxError = recastSettings.detailSampleMaxError;
// Init NavMesh
var navNesh = new Recast.NavMesh();
// Build NavMesh
navNesh.build(vertices, vertexLength, indices, indexLength, recastConfig);
// Create debug NavMesh for visualization
var debugNavMesh = navNesh.getDebugNavMesh();
var trianglesCount = debugNavMesh.getTriangleCount();
triangelVertices = []
triangleFaces = []
// Loop through triangles
for(let runTriangle = 0; runTriangle < trianglesCount; runTriangle++) {
var triangle = debugNavMesh.getTriangle(runTriangle);
// Get triangle vertices
var points = [triangle.getPoint(0),
triangle.getPoint(1),
triangle.getPoint(2)];
// Get vertex positon
for(var i in points) {
// y, z interchanged
triangelVertices.push([points[i].x, points[i].z, points[i].y]);
}
// Get triangle indices
var face = [3 * runTriangle + 1,
3 * runTriangle + 2,
3 * runTriangle + 3];
triangleFaces.push(face);
}
// Get OBJ format of triangles
var navMeshObj = saveGeometryToObj(triangelVertices, triangleFaces);
// Write to OBJ file
fs.writeFile(path_mesh, navMeshObj, function (err) {
if (err) throw err;
});
});
}
function saveGeometryToObj (vertices, faces) {
var buffer = '';
//Write Vertices
for (var i in vertices) buffer += 'v ' + (vertices[i][0]) + ' ' + vertices[i][1] + ' ' + vertices[i][2] + '\n';
//Write Faces
for (var i in faces) buffer += 'f ' + (faces[i][0]) + ' ' + faces[i][1] + ' ' + faces[i][2] + '\n';
return buffer;
}