forked from denetii/io_thps_scene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollision.py
351 lines (304 loc) · 14.1 KB
/
collision.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#############################################
# THUG1/2 COLLISION SETTINGS
#############################################
import bpy
import struct
import mathutils
import math
import bmesh
import os, sys
import statistics, random
import collections
from bpy.props import *
from . constants import *
from . helpers import *
from . autorail import *
# PROPERTIES
#############################################
update_triggered_by_ui_updater = False
BSPNode = collections.namedtuple("BSPNode", "split_point split_axis left right")
BSPLeaf = collections.namedtuple("BSPLeaf", "faces")
# METHODS
#############################################
def _resolve_face_terrain_type(ob, bm, face):
ttl = bm.faces.layers.int.get("terrain_type")
tt = 0
if not ttl or face[ttl] == AUTORAIL_AUTO:
if face.material_index >= 0 and len(ob.material_slots):
face_material = ob.material_slots[face.material_index].material
if face_material:
tt = TERRAIN_TYPES.index(face_material.thug_material_props.terrain_type)
else:
tt = face[ttl]
return tt
def make_bsp_tree(ob, faces, matrix):
def vv(vert, matrix):
return to_thug_coords(matrix * vert.co)
def inner(faces, split_axis, level, cant_split=set()):
# print(level)
# split_axis = 0
if len(faces) <= 50: # or split_axis in cant_split:
return BSPLeaf(faces)
best_duplis = float("inf")
best_point = None
best_left = None
best_right = None
best_axis = None
for split_axis in range(3):
if split_axis in cant_split: continue
split_point = statistics.median(
vv(vert, matrix)[split_axis] for face in random.sample(faces, min(20, len(faces))) for vert in face.verts)
"""
split_point = statistics.median(
vv(vert)[split_axis] for face in faces for vert in face.verts)
"""
split_point = int(split_point * 16.0) * 0.0625
left_faces = []
right_faces = []
duplis = 0
for face in faces:
assert len(face.verts) == 3
left = False
right = False
for vert in face.verts:
if vv(vert, matrix)[split_axis] < split_point:
left = True
if vv(vert, matrix)[split_axis] >= split_point:
right = True
if left:
left_faces.append(face)
if right:
right_faces.append(face)
if left and right:
duplis += 1
if duplis < best_duplis:
best_left = left_faces
best_right = right_faces
best_axis = split_axis
best_duplis = duplis
best_point = split_point
left_faces = best_left
right_faces = best_right
split_axis = best_axis
duplis = best_duplis
split_point = best_point
if duplis >= (len(faces) // 2):
return BSPLeaf(faces)
# print(len(faces), len(left_faces), len(right_faces), duplis, split_axis, level)
return BSPNode(
split_point,
split_axis,
inner(left_faces,
(split_axis + 1) % 3,
level + 1,
(cant_split | set([split_axis]) if len(left_faces) == len(faces) else cant_split)),
inner(right_faces,
(split_axis + 1) % 3,
level + 1,
(cant_split | set([split_axis]) if len(right_faces) == len(faces) else cant_split))
#,
)
return inner(faces, 0, 0)
def iter_tree(tree):
yield tree
if isinstance(tree, BSPLeaf):
return
yield from iter_tree(tree.left)
yield from iter_tree(tree.right)
def tree_to_list(tree):
index = 0
indices = {id(tree): index}
l = [tree]
stack = [tree]
while stack:
tree = stack.pop(0)
if isinstance(tree, BSPNode):
index += 1
indices[id(tree.left)] = index
l.append(tree.left)
stack.append(tree.left)
index += 1
indices[id(tree.right)] = index
l.append(tree.right)
stack.append(tree.right)
return l, indices
def update_collision_flag_mesh(wm, context, flag):
global update_triggered_by_ui_updater
if update_triggered_by_ui_updater:
return
if not context.edit_object:
return
bm = bmesh.from_edit_mesh(context.edit_object.data)
cfl = bm.faces.layers.int.get("collision_flags")
if not cfl:
cfl = bm.faces.layers.int.new("collision_flags")
flag_set = getattr(wm, "thug_face_" + flag)
for face in bm.faces:
if not face.select:
continue
flags = face[cfl]
#for ff in SETTABLE_FACE_FLAGS:
if flag_set:
flags |= FACE_FLAGS[flag]
else:
flags &= ~FACE_FLAGS[flag]
face[cfl] = flags
bmesh.update_edit_mesh(context.edit_object.data)
#----------------------------------------------------------------------------------
def update_terrain_type_mesh(wm, context):
global update_triggered_by_ui_updater
if update_triggered_by_ui_updater:
return
if not context.edit_object:
return
bm = bmesh.from_edit_mesh(context.edit_object.data)
ttl = bm.faces.layers.int.get("terrain_type")
if not ttl:
ttl = bm.faces.layers.int.new("terrain_type")
for face in bm.faces:
if not face.select:
continue
if wm.thug_face_terrain_type == "Auto":
face[ttl] = AUTORAIL_AUTO
else:
face[ttl] = TERRAIN_TYPES.index(wm.thug_face_terrain_type)
bmesh.update_edit_mesh(context.edit_object.data)
#----------------------------------------------------------------------------------
@bpy.app.handlers.persistent
def update_collision_flag_ui_properties(scene):
global update_triggered_by_ui_updater
update_triggered_by_ui_updater = True
try:
ob = scene.objects.active
if not ob or ob.mode != "EDIT" or ob.type != "MESH":
return
bm = bmesh.from_edit_mesh(ob.data)
wm = bpy.context.window_manager
arl = bm.edges.layers.int.get("thug_autorail")
edge = bm.select_history.active
if arl and edge and isinstance(edge, bmesh.types.BMEdge):
new_value = "Auto" if edge[arl] == AUTORAIL_AUTO else \
"None" if edge[arl] == AUTORAIL_NONE else \
TERRAIN_TYPES[edge[arl]]
if wm.thug_autorail_terrain_type != new_value:
try:
wm.thug_autorail_terrain_type = new_value
except TypeError:
wm.thug_autorail_terrain_type = "Auto"
face = None
if (("FACE" in bm.select_mode)
and bm.select_history
and isinstance(bm.select_history[-1], bmesh.types.BMFace)):
face = bm.select_history[-1]
if not face:
face = next((face for face in bm.faces if face.select), None)
if not face:
return
cfl = bm.faces.layers.int.get("collision_flags")
for ff in SETTABLE_FACE_FLAGS:
new_value = bool(cfl and (face[cfl] & FACE_FLAGS[ff]))
if getattr(wm, "thug_face_" + ff) != new_value:
setattr(wm, "thug_face_" + ff, new_value)
ttl = bm.faces.layers.int.get("terrain_type")
if ttl:
if face[ttl] == AUTORAIL_AUTO:
new_value = "Auto"
else:
new_value = TERRAIN_TYPES[face[ttl]]
else:
new_value = "Auto"
if wm.thug_face_terrain_type != new_value:
wm.thug_face_terrain_type = new_value
finally:
update_triggered_by_ui_updater = False
# PROPERTIES
#############################################
class THUGCollisionMeshTools(bpy.types.Panel):
bl_label = "TH Collision Mesh Tools"
bl_region_type = "TOOLS"
bl_space_type = "VIEW_3D"
bl_category = "THUG Tools"
"""
@classmethod
def poll(cls, context):
# Only allow in edit mode for a selected mesh.
return context.mode == "EDIT_MESH" and context.object is not None and context.object.type == "MESH"
"""
def draw(self, context):
self.layout.prop(context.window_manager, "thug_show_face_collision_colors")
if not context.object: return
#print(context.mode + " " + context.object.type)
if context.mode == "EDIT_MESH" and context.object.type == "MESH":
obj = context.object
bm = bmesh.from_edit_mesh(obj.data)
any_face_selected = any(face for face in bm.faces if face.select)
collision_flag_layer = bm.faces.layers.int.get("collision_flags")
terrain_type_layer = bm.faces.layers.int.get("terrain_type")
if any_face_selected:
box = self.layout.box().column(True)
tmp_row = box.split()
col = tmp_row.column()
for idx, ff in enumerate(SETTABLE_FACE_FLAGS):
if idx == 5:
col = tmp_row.column()
col.prop(context.window_manager, "thug_face_" + ff, toggle=True)
self.layout.prop(context.window_manager, "thug_face_terrain_type")
else:
self.layout.label("No faces selected.")
if any_face_selected or any(edge for edge in bm.edges if edge.select):
box = self.layout.box().column(True)
tmp_row = box.split()
col = tmp_row.column()
col.operator(MarkAutorail.bl_idname)
col = tmp_row.column()
col.operator(ClearAutorail.bl_idname)
box.row().prop(context.window_manager, "thug_autorail_terrain_type")
box.row().operator(ExtractRail.bl_idname)
else:
self.layout.label("No edges selected.")
elif False and context.mode == "OBJECT":
self.layout.row().label("Object: {}".format(context.object.type))
self.layout.row().label("Object flags: {}".format(context.object.thug_col_obj_flags))
elif context.mode == "EDIT_CURVE" and context.object.type == "CURVE":
if (context.object.type == "CURVE" and
context.object.data.splines and
context.object.data.splines[0].points):
self.layout.prop(context.window_manager.thug_pathnode_props, "name")
self.layout.prop_search(
context.window_manager.thug_pathnode_props, "script_name",
context.window_manager.thug_all_nodes, "scripts", icon='SCRIPT')
#self.layout.prop(context.window_manager.thug_pathnode_props, "script_name")
if context.object.thug_path_type == "Rail":
self.layout.prop(context.window_manager.thug_pathnode_props, "terrain")
if context.object.thug_path_type == "Waypoint":
#self.layout.prop(context.window_manager.thug_pathnode_props, "waypt_type")
self.layout.prop_search(
context.window_manager.thug_pathnode_props, "spawnobjscript",
context.window_manager.thug_all_nodes, "scripts", icon='SCRIPTPLUGINS')
if context.object.thug_waypoint_props.waypt_type == "PedAI":
#self.layout.prop(context.window_manager.thug_pathnode_props, "PedType")
if context.object.thug_waypoint_props.PedType == "Skate":
#self.layout.prop(context.window_manager.thug_pathnode_props, "do_continue")
#self.layout.prop(context.window_manager.thug_pathnode_props, "ContinueWeight")
# Once I implement branching paths, Priority will be important!
#self.layout.label(text="Priority")
#self.layout.prop(context.window_manager.thug_pathnode_props, "Priority", expand=True)
self.layout.prop(context.window_manager.thug_pathnode_props, "SkateAction")
self.layout.prop(context.window_manager.thug_pathnode_props, "JumpToNextNode")
if context.window_manager.thug_pathnode_props.SkateAction == "Jump" or \
context.window_manager.thug_pathnode_props.JumpToNextNode:
self.layout.prop(context.window_manager.thug_pathnode_props, "JumpHeight")
self.layout.prop(context.window_manager.thug_pathnode_props, "SpinAngle")
self.layout.prop(context.window_manager.thug_pathnode_props, "SpinDirection", expand=True)
if context.window_manager.thug_pathnode_props.SkateAction == "Grind":
self.layout.prop(context.window_manager.thug_pathnode_props, "terrain")
#if context.window_manager.thug_pathnode_props.SkateAction == "Manual":
#self.layout.prop(context.window_manager.thug_pathnode_props, "ManualType")
#if context.window_manager.thug_pathnode_props.SkateAction == "Stop":
#self.layout.prop(context.window_manager.thug_pathnode_props, "Deceleration")
#self.layout.prop(context.window_manager.thug_pathnode_props, "StopTime")
elif context.object.thug_waypoint_props.PedType == "Walk":
#self.layout.prop(context.window_manager.thug_pathnode_props, "do_continue")
#self.layout.prop(context.window_manager.thug_pathnode_props, "ContinueWeight")
self.layout.prop(context.window_manager.thug_pathnode_props, "Priority")
#self.layout.prop(context.window_manager.thug_pathnode_props, "SkateAction")