-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavMeshGen.py
297 lines (244 loc) · 11.1 KB
/
NavMeshGen.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import direct.directbase.DirectStart
from panda3d.core import *
from panda3d.egg import EggData, EggPolygon, EggGroupNode
from GridNode import *
from math import sqrt
import sys
class MyApp():
def __init__(self):
count = 0
for arg in sys.argv:
count = count + 1
if count < 3:
print("Incorrect number of arguments...")
print("Program needs a fullmesh.egg and a collmesh.egg")
sys.exit()
# initialize lists
self.oldList = []
self.oldCollList = []
self.newList = []
self.newCollList = []
self.finalList = []
self.nodeCount = 0
self.collNodeCount = 0
self.firstNode = None
self.lowestVertex = -1
# Process the egg file and iterate
# through it
self.egg = EggData()
self.fname = Filename(sys.argv[1])
self.egg.resolveEggFilename(self.fname)
self.egg.read(self.fname, "full")
self.eggColl = EggData()
self.cname = Filename(sys.argv[2])
self.eggColl.resolveEggFilename(self.cname)
self.eggColl.read(self.cname, "coll")
print("Creating full node list...")
self.iterateEggPoly(self.egg, "Full")
print("Correcting full node list...")
self.createNewFullList()
print("Creating coll node list...")
self.iterateEggPoly(self.eggColl, "Coll")
print("Creating a proper grid with collisions...")
self.createCombinedGrid()
print("Creating neighbors for the grid...")
self.createNeighbors()
print("Write to csv file...")
self.writeToCSV()
# Iterates through the Egg file and
# extract all the quads and store them
# as nodes
def iterateEggPoly(self, egg, type):
# Create a node for each quad
if isinstance(egg, EggPolygon):
node = GridNode(self.nodeCount, egg.getVertex(0), egg.getVertex(1),
egg.getVertex(2), egg.getVertex(3))
if type == "Full":
self.oldList.append(node)
# Find the correct vertex number to use
if (self.lowestVertex == -1):
self.analyzeVertex(node)
# Store the bottom left node
if (self.firstNode == None):
self.firstNode = node
else:
if node.vertex[self.lowestVertex].getX() < self.firstNode.vertex[self.lowestVertex].getX() and \
node.vertex[self.lowestVertex].getZ() < self.firstNode.vertex[self.lowestVertex].getZ():
self.firstNode = node
self.nodeCount = self.nodeCount + 1
else:
self.oldCollList.append(node)
self.collNodeCount = self.collNodeCount + 1
if isinstance(egg, EggGroupNode):
child = egg.getFirstChild()
while child != None:
self.iterateEggPoly(child, type)
child = egg.getNextChild()
# Creates a new List which has the correctly
# ordered nodes for the Full nodes
def createNewFullList(self):
# Find the next node based on edge 0-3
currentRowNode = self.firstNode
currentColNode = self.firstNode
self.newList.append(currentRowNode)
for r in range(int(sqrt(self.nodeCount))): # sqrt
nextColNode = None
for c in range(int(sqrt(self.nodeCount)) - 1): # sqrt
# Processing next col
for i in range(self.nodeCount):
if currentColNode.vertex[self.rightVertex] == self.oldList[i].vertex[self.lowestVertex] and \
currentColNode.vertex[self.toprightVertex] == self.oldList[i].vertex[self.topVertex]:
nextColNode = self.oldList[i]
self.newList.append(nextColNode)
currentColNode = nextColNode
# Processing next row
if r == sqrt(self.nodeCount):
break
nextRowNode = None
for i in range(self.nodeCount):
if currentRowNode.vertex[self.toprightVertex] == self.oldList[i].vertex[self.rightVertex] and \
currentRowNode.vertex[self.topVertex] == self.oldList[i].vertex[self.lowestVertex]:
nextRowNode = self.oldList[i]
self.newList.append(nextRowNode)
currentRowNode = nextRowNode
currentColNode = nextRowNode
# Creates a combined List which has the correctly
# ordered nodes with collisions as None
def createCombinedGrid(self):
for r in range(int(sqrt(self.nodeCount))):
temp = []
self.finalList.append(temp)
for c in range(int(sqrt(self.nodeCount))):
tempNode = self.newList[r * int(sqrt(self.nodeCount)) + c]
if (self.CollContains(tempNode)):
self.finalList[r].append(tempNode)
else:
self.finalList[r].append(None)
# Creates neighbor lists for each node
def createNeighbors(self):
for r in range(int(sqrt(self.nodeCount))):
for c in range(int(sqrt(self.nodeCount))):
self.setNeighbors(self.finalList[r][c], r, c)
# This writes the grid in the correct format to the csv
def writeToCSV(self):
file = open('navmesh.csv', 'w')
# Grid Size
file.write('Grid Size,')
file.write(str(int(sqrt(self.nodeCount))))
file.write('\nNULL,NodeType,GridX,GridY,Length,Width,Height,PosX,PosY,PosZ')
file.write('\n')
for r in range(int(sqrt(self.nodeCount))):
for c in range(int(sqrt(self.nodeCount))):
node = self.finalList[r][c]
if node == None:
file.write('1,1,0,0,0,0,0,0,0,0')
file.write('\n')
else:
## For the nodes
# NULL
file.write('0' + ',')
# Node Type (Main)
file.write('0' + ',')
# Grid X
file.write(str(node.r) + ',')
# Grid Y
file.write(str(node.c) + ',')
# Length
file.write(str(abs(node.vertex[0].getX() - node.vertex[1].getX())) + ',')
# Width
file.write(str(abs(node.vertex[0].getZ() - node.vertex[3].getZ())) + ',')
# Height
file.write('0' + ',')
# PosX
file.write(str((node.vertex[0].getX() + node.vertex[1].getX()) / 2) + ',')
# PosY
file.write(str((node.vertex[0].getZ() + node.vertex[3].getZ()) / 2) + ',')
# PosZ
file.write('0')
file.write('\n')
## For the nodes neighbors
for i in range(8):
nnode = node.neighbors[i]
if nnode == None:
file.write('1,1,0,0,0,0,0,0,0,0')
else:
# NULL
file.write('0' + ',')
# Node Type (Neighbor)
file.write('1' + ',')
# Grid X
file.write(str(nnode.r) + ',')
# Grid Y
file.write(str(nnode.c) + ',')
# Length
file.write(str(abs(nnode.vertex[0].getX() - nnode.vertex[1].getX())) + ',')
# Width
file.write(str(abs(nnode.vertex[0].getZ() - nnode.vertex[3].getZ())) + ',')
# Height
file.write('0' + ',')
# PosX
file.write(str((nnode.vertex[0].getX() + nnode.vertex[1].getX()) / 2) + ',')
# PosY
file.write(str((nnode.vertex[0].getZ() + nnode.vertex[3].getZ()) / 2) + ',')
# PosZ
file.write('0')
file.write('\n')
## HELPER FUNCTIONS
# Helper function which finds collisions
def CollContains(self, chkNode):
for node in self.oldCollList:
if node.vertex == chkNode.vertex:
return True
return False
# Helper function which finds collisions
def setNeighbors(self, node, row, col):
if not (node == None):
# Setting the row and col parameters
node.setRC(row, col)
# left top
if col > 0 and (row + 1) < sqrt(self.nodeCount):
node.neighbors[0] = self.finalList[row + 1][col - 1]
# left mid
if col > 0:
node.neighbors[1] = self.finalList[row][col - 1]
# left bot
if col > 0 and (row - 1) > 0:
node.neighbors[2] = self.finalList[row - 1][col - 1]
# bot mid
if (row - 1) > 0:
node.neighbors[3] = self.finalList[row - 1][col]
# bot right
if (row - 1) > 0 and (col + 1) < sqrt(self.nodeCount):
node.neighbors[4] = self.finalList[row - 1][col + 1]
# right mid
if (col + 1) < sqrt(self.nodeCount):
node.neighbors[5] = self.finalList[row][col + 1]
# right top
if (row + 1) < sqrt(self.nodeCount) and (col + 1) < sqrt(self.nodeCount):
node.neighbors[6] = self.finalList[row + 1][col + 1]
# top mid
if (row + 1) < sqrt(self.nodeCount):
node.neighbors[7] = self.finalList[row + 1][col]
# Helper function to calculate the correct vertex
def analyzeVertex(self, node):
precision = 100.0
self.lowestVertex = 0
for i in range(4):
if node.vertex[i].getX() < node.vertex[self.lowestVertex].getX() and node.vertex[i].getZ() < node.vertex[
self.lowestVertex].getZ():
self.lowestVertex = i
# top, left, right
for i in range(4):
if not (i == self.lowestVertex):
x1 = float(int(node.vertex[self.lowestVertex].getX() * precision) / precision)
x2 = float(int(node.vertex[i].getX() * precision) / precision)
z1 = float(int(node.vertex[self.lowestVertex].getZ() * precision) / precision)
z2 = float(int(node.vertex[i].getZ() * precision) / precision)
if x1 == x2 and z1 < z2:
self.topVertex = i
if x1 < x2 and z1 == z2:
self.rightVertex = i
if x1 < x2 and z1 < z2:
self.toprightVertex = i
app = MyApp()
run()