-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathui_draw.py
296 lines (256 loc) · 12.2 KB
/
ui_draw.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
import bpy
import bgl
from bpy.props import *
from mathutils import Vector
from . import_thps4 import THPS4ScnToScene, THPS4ColToScene
from . import_thug1 import THUG1ScnToScene
from . import_thug2 import THUG2ScnToScene, THUG2ColToScene
from . import_park import ImportTHUGPrk
from . import_thps2 import THPS2PsxToScene
from . tex import THUGImgToImages
from . qb import THUGImportLevelQB
from . skeleton import THUGImportSkeleton
from . scene_props import *
from . constants import *
from . material import *
from . constants import *
from . collision import *
from . export_thug1 import *
from . export_thug2 import *
from . export_shared import *
from . import_nodes import *
from . presets import *
from . script_template import *
# PROPERTIES
#############################################
draw_stuff_objects = set()
draw_handle = None
draw_stuff_dirty = True
draw_batches = []
# METHODS
#############################################
@bpy.app.handlers.persistent
def draw_stuff_post_update(scene):
global draw_stuff_dirty, draw_stuff_objects
if draw_stuff_dirty: return
if not draw_stuff_objects:
draw_stuff_dirty = True
return
dep = bpy.context.evaluated_depsgraph_get()
for update in dep.updates:
if update.id.original not in draw_stuff_objects:
draw_stuff_dirty = True
return
#----------------------------------------------------------------------------------
@bpy.app.handlers.persistent
def draw_stuff_pre_load_cleanup(*args):
global draw_stuff_dirty, draw_stuff_objects, draw_batches
draw_stuff_dirty = True
draw_stuff_objects = set()
draw_batches = []
#----------------------------------------------------------------------------------
@bpy.app.handlers.persistent
def draw_stuff():
from . bglx import glColor4f, glVertex3f, glBegin, glEnd, GL_LINES, GL_TRIANGLES, GL_LINE_STRIP #, GL_POLYGON
global draw_stuff_dirty, draw_stuff_objects, draw_batches
ctx = bpy.context
if not len(ctx.selected_objects) and not ctx.object:
return
if not bpy.context.window_manager.thug_show_face_collision_colors:
return
VERT_FLAG = FACE_FLAGS["mFD_VERT"]
WALLRIDABLE_FLAG = FACE_FLAGS["mFD_WALL_RIDABLE"]
TRIGGER_FLAG = FACE_FLAGS["mFD_TRIGGER"]
NON_COLLIDABLE_FLAG = FACE_FLAGS["mFD_NON_COLLIDABLE"]
_tmp_buf = bgl.Buffer(bgl.GL_FLOAT, 1)
bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_FACTOR, _tmp_buf)
old_offset_factor = _tmp_buf[0]
bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_UNITS, _tmp_buf)
old_offset_units = _tmp_buf[0]
del _tmp_buf
objects = set([ob.name for ob in ctx.selected_objects] if ctx.mode == "OBJECT" else [ctx.object.name])
if draw_stuff_objects != objects:
draw_stuff_dirty = True
if not draw_stuff_dirty:
bgl.glCullFace(bgl.GL_BACK)
bgl.glEnable(bgl.GL_CULL_FACE)
bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
bgl.glPolygonOffset(-2, -2)
bgl.glEnable(bgl.GL_BLEND)
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA);
prefs = ctx.preferences.addons[ADDON_NAME].preferences
bgl.glLineWidth(prefs.line_width)
for bi in draw_batches:
if bi == None: continue
bi[1].bind()
bi[1].uniform_float("color", bi[2])
bi[0].draw(bi[1])
bgl.glPolygonOffset(old_offset_factor, old_offset_units)
bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
bgl.glDisable(bgl.GL_CULL_FACE)
return
draw_batches = []
bm = None
try:
bgl.glCullFace(bgl.GL_BACK)
bgl.glEnable(bgl.GL_CULL_FACE)
bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
bgl.glPolygonOffset(-2, -2)
bgl.glEnable(bgl.GL_BLEND)
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA);
prefs = ctx.preferences.addons[ADDON_NAME].preferences
depsgraph = bpy.context.evaluated_depsgraph_get()
bgl.glLineWidth(prefs.line_width)
draw_stuff_objects = objects
bdobs = {ob.name: ob for ob in bpy.data.objects}
for ob in objects:
ob = bdobs[ob]
if not bm: bm = bmesh.new()
if (ob and
ob.type == "CURVE" and
ob.data.splines and
ob.data.splines[-1].points and
ob.thug_path_type == "Rail" and
ob.thug_rail_connects_to):
connects_to = bdobs[ob.thug_rail_connects_to]
if (connects_to and
connects_to.type == "CURVE" and
connects_to.data.splines and
connects_to.data.splines[0].points):
glBegin(GL_LINES)
glColor4f(*prefs.rail_end_connection_color)
v = ob.matrix_world @ ob.data.splines[-1].points[-1].co.to_3d()
glVertex3f(v[0], v[1], v[2])
v = connects_to.matrix_world @ connects_to.data.splines[0].points[0].co.to_3d()
glVertex3f(v[0], v[1], v[2])
draw_batches.append(glEnd())
# Draw previews for area lights - tube/sphere lights and area lights
if (ob and ob.type == 'LAMP'):
if ob.data.thug_light_props.light_type == 'TUBE':
if ob.data.thug_light_props.light_end_pos != (0, 0, 0):
glBegin(GL_LINES)
glColor4f(1.0, 0.75, 0.25, 1.0)
glVertex3f(ob.location[0], ob.location[1], ob.location[2])
glVertex3f(ob.location[0] + ob.data.thug_light_props.light_end_pos[0], ob.location[1] + ob.data.thug_light_props.light_end_pos[1], ob.location[2] + ob.data.thug_light_props.light_end_pos[2])
draw_batches.append(glEnd())
continue
elif ob.data.thug_light_props.light_type == 'SPHERE':
continue
elif ob.data.thug_light_props.light_type == 'AREA':
continue
else:
continue
elif (ob and ob.type == 'EMPTY'):
if ob.thug_empty_props.empty_type == 'LightVolume' or ob.thug_empty_props.empty_type == 'CubemapProbe':
# Draw light volume bbox!
bbox, bbox_min, bbox_max, bbox_mid = get_bbox_from_node(ob)
# 50% alpha, 2 pixel width line
bgl.glEnable(bgl.GL_BLEND)
glColor4f(1.0, 0.0, 0.0, 0.5)
bgl.glLineWidth(4)
glBegin(GL_LINE_STRIP)
glVertex3f(*bbox[0])
glVertex3f(*bbox[1])
glVertex3f(*bbox[2])
glVertex3f(*bbox[3])
glVertex3f(*bbox[0])
glVertex3f(*bbox[4])
glVertex3f(*bbox[5])
glVertex3f(*bbox[6])
glVertex3f(*bbox[7])
glVertex3f(*bbox[4])
draw_batches.append(glEnd())
glBegin(bgl.GL_LINES)
glVertex3f(*bbox[1])
glVertex3f(*bbox[5])
glVertex3f(*bbox[2])
glVertex3f(*bbox[6])
glVertex3f(*bbox[3])
glVertex3f(*bbox[7])
draw_batches.append(glEnd())
if not ob or ob.type != "MESH":
continue
if ob.mode == "EDIT":
bm.free()
bm = bmesh.from_edit_mesh(ob.data).copy()
else:
bm.clear()
bm.from_object(ob, depsgraph)
arl = bm.edges.layers.int.get("thug_autorail")
if arl:
glColor4f(*prefs.autorail_edge_color)
glBegin(GL_LINES)
for edge in bm.edges:
if edge[arl] == AUTORAIL_NONE:
continue
for vert in edge.verts:
v = ob.matrix_world @ vert.co
glVertex3f(v[0], v[1], v[2])
draw_batches.append(glEnd())
cfl = bm.faces.layers.int.get("collision_flags")
flag_stuff = ((VERT_FLAG, prefs.vert_face_color),
(WALLRIDABLE_FLAG, prefs.wallridable_face_color),
(TRIGGER_FLAG, prefs.trigger_face_color),
(NON_COLLIDABLE_FLAG, prefs.non_collidable_face_color))
if cfl:
bmesh.ops.triangulate(bm, faces=bm.faces)
for face in bm.faces:
drawn_face = False
if prefs.show_bad_face_colors:
if (face[cfl] & (VERT_FLAG | WALLRIDABLE_FLAG | NON_COLLIDABLE_FLAG) not in
(VERT_FLAG, WALLRIDABLE_FLAG, NON_COLLIDABLE_FLAG, 0)):
glColor4f(*prefs.bad_face_color)
glBegin(GL_TRIANGLES)
for vert in face.verts:
v = ob.matrix_world @ vert.co
glVertex3f(v[0], v[1], v[2])
draw_batches.append(glEnd())
continue
for face_flag, face_color in flag_stuff:
if face[cfl] & face_flag and (not drawn_face or prefs.mix_face_colors):
glColor4f(*face_color)
drawn_face = True
glBegin(GL_TRIANGLES)
for vert in face.verts:
v = ob.matrix_world @ vert.co
glVertex3f(v[0], v[1], v[2])
draw_batches.append(glEnd())
finally:
draw_stuff_dirty = False
if bm: bm.free()
bgl.glPolygonOffset(old_offset_factor, old_offset_units)
bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
#----------------------------------------------------------------------------------
def import_menu_func(self, context):
self.layout.operator(THUG2ColToScene.bl_idname, text=THUG2ColToScene.bl_label, icon='PLUGIN')
self.layout.operator(THUG2ScnToScene.bl_idname, text=THUG2ScnToScene.bl_label, icon='PLUGIN')
self.layout.operator(THUG1ScnToScene.bl_idname, text=THUG1ScnToScene.bl_label, icon='PLUGIN')
self.layout.operator(THPS4ScnToScene.bl_idname, text=THPS4ScnToScene.bl_label, icon='PLUGIN')
self.layout.operator(THPS4ColToScene.bl_idname, text=THPS4ColToScene.bl_label, icon='PLUGIN')
self.layout.operator(THPS2PsxToScene.bl_idname, text=THPS2PsxToScene.bl_label, icon='PLUGIN')
self.layout.operator(THUGImportLevelQB.bl_idname, text=THUGImportLevelQB.bl_label, icon='PLUGIN')
self.layout.operator(THUGImportSkeleton.bl_idname, text=THUGImportSkeleton.bl_label, icon='PLUGIN')
self.layout.operator(ImportTHUGPrk.bl_idname, text=ImportTHUGPrk.bl_label, icon='PLUGIN')
self.layout.operator(THUGImgToImages.bl_idname, text=THUGImgToImages.bl_label, icon='PLUGIN')
#----------------------------------------------------------------------------------
def export_menu_func(self, context):
self.layout.operator(SceneToTHPSLevel.bl_idname, text=SceneToTHPSLevel.bl_label, icon='PLUGIN')
self.layout.operator(SceneToTHPSModel.bl_idname, text=SceneToTHPSModel.bl_label, icon='PLUGIN')
#----------------------------------------------------------------------------------
def add_menu_func(self, context):
self.layout.menu(THUG_MT_PresetsMenu.bl_idname, text="THUG", icon='PLUGIN')
#----------------------------------------------------------------------------------
def register_menus():
bpy.types.TOPBAR_MT_file_import.append(import_menu_func)
bpy.types.TOPBAR_MT_file_export.append(export_menu_func)
addPresetNodes()
addPresetMesh()
bpy.types.VIEW3D_MT_add.append(add_menu_func)
script_template.init_templates()
#----------------------------------------------------------------------------------
def unregister_menus():
bpy.types.TOPBAR_MT_file_import.remove(import_menu_func)
bpy.types.TOPBAR_MT_file_export.remove(export_menu_func)
bpy.types.VIEW3D_MT_add.remove(add_menu_func)
clearPresetNodes()
clearPresetMesh()