e4ba4d99d69eb65e91fb36f4f453dccf20e887c9
[carveJwlIkooP6JGAAIwe30JlM.git] / skaterift_blender / sr_main.py
1 print( "sr_main" )
2
3 import bpy, blf, math, gpu, os, time
4 import cProfile
5 from ctypes import *
6 import numpy as np
7 from mathutils import *
8 from gpu_extras.batch import batch_for_shader
9 from bpy_extras import mesh_utils
10 from bpy_extras import view3d_utils
11
12 sr_entity_list = [
13 ('none', 'None', '', 0 ),
14 ('ent_gate', 'Gate', '', 1 ),
15 ('ent_spawn', 'Spawn Point', '', 2 ),
16 ('ent_route_node', 'Routing Path', '', 3 ),
17 ('ent_route', 'Skate Course', '', 4 ),
18 ('ent_water', 'Water Surface', '', 5 ),
19 ('ent_volume', 'Volume/Trigger', '', 6 ),
20 ('ent_audio', 'Audio', '', 7 ),
21 ('ent_marker', 'Marker', '', 8 ),
22 ('ent_font', 'Font', '', 9 ),
23 ('ent_font_variant', 'Font:Variant', '', 10 ),
24 ('ent_traffic', 'Traffic Model', '', 11 ),
25 ('ent_skateshop', 'Skate Shop', '', 12 ),
26 ('ent_camera', 'Camera', '', 13 ),
27 ('ent_swspreview', 'Workshop Preview', '', 14 ),
28 ('ent_menuitem', 'Menu Item', '', 15 ),
29 ('ent_worldinfo', 'World Info', '', 16 ),
30 ('ent_ccmd', 'CCmd', '', 17 ),
31 ('ent_objective', 'Objective', '', 18 ),
32 ('ent_challenge', 'Challenge', '', 19 ),
33 ('ent_relay', 'Relay', '', 20 ),
34 ('ent_miniworld', 'Mini World', '', 22 ),
35 ('ent_prop', 'Prop', '', 23 ),
36 ('ent_list', 'Entity List', '', 24 ),
37 ('ent_region', 'Region', '', 25 ),
38 ('ent_glider', 'Glider', '', 26 ),
39 ('ent_npc', 'npc', '', 27 )
40 ]
41
42 MDL_VERSION_NR = 106
43 SR_TRIGGERABLE = [ 'ent_audio', 'ent_ccmd', 'ent_gate', 'ent_challenge', \
44 'ent_relay', 'ent_skateshop', 'ent_objective', 'ent_route',\
45 'ent_miniworld', 'ent_region', 'ent_glider', 'ent_list',\
46 'ent_npc', 'ent_water' ]
47
48 def get_entity_enum_id( alias ):
49 #{
50 for et in sr_entity_list:#{
51 if et[0] == alias:#{
52 return et[3]
53 #}
54 #}
55
56 if alias == 'ent_cubemap': return 21
57
58 return 0
59 #}
60
61 class mdl_vert(Structure): # 48 bytes. Quite large. Could compress
62 #{ # the normals and uvs to i16s. Not an
63 _pack_ = 1 # real issue, yet.
64 _fields_ = [("co",c_float*3),
65 ("norm",c_float*3),
66 ("uv",c_float*2),
67 ("colour",c_uint8*4),
68 ("weights",c_uint16*4),
69 ("groups",c_uint8*4)]
70 #}
71
72 class mdl_transform(Structure):
73 #{
74 _fields_ = [("co",c_float*3),
75 ( "s",c_float*3),
76 ( "q",c_float*4)]
77 #}
78
79 class mdl_submesh(Structure):
80 #{
81 _fields_ = [("indice_start",c_uint32),
82 ("indice_count",c_uint32),
83 ("vertex_start",c_uint32),
84 ("vertex_count",c_uint32),
85 ("bbx",(c_float*3)*2),
86 ("material_id",c_uint16), # index into the material array
87 ("flags",c_uint16)]
88 #}
89
90 # shader data
91 # =================================================
92
93 class mdl_file(Structure):
94 #{
95 _fields_ = [("path",c_uint32),
96 ("pack_offset",c_uint32),
97 ("pack_size",c_uint32)]
98 #}
99
100 class mdl_material_kvs(Structure):
101 #{
102 _fields_ = [("offset",c_uint32),
103 ("size",c_uint32)]
104 #}
105
106 class mdl_material_properties_union(Union):
107 #{
108 _fields_ = [("kvs",mdl_material_kvs),
109 ("compiled",c_void_p)]
110 #}
111
112 class mdl_material(Structure):
113 #{
114 _fields_ = [("pstr_name",c_uint32),
115 ("shader",c_uint32),
116 ("flags",c_uint32),
117 ("surface_prop",c_uint32),
118 ("props",mdl_material_properties_union)]
119
120 # ("colour",c_float*4), (old) v105
121 # ("colour1",c_float*4),
122 # ("tex_diffuse",c_uint32),
123 # ("tex_none0",c_uint32),
124 # ("tex_none1",c_uint32)]
125 #}
126
127 class mdl_bone(Structure):
128 #{
129 _fields_ = [("co",c_float*3),("end",c_float*3),
130 ("parent",c_uint32),
131 ("collider",c_uint32),
132 ("ik_target",c_uint32),
133 ("ik_pole",c_uint32),
134 ("flags",c_uint32),
135 ("pstr_name",c_uint32),
136 ("hitbox",(c_float*3)*2),
137 ("conevx",c_float*3),("conevy",c_float*3),("coneva",c_float*3),
138 ("conet",c_float)]
139 #}
140
141 class mdl_armature(Structure):
142 #{
143 _fields_ = [("transform",mdl_transform),
144 ("bone_start",c_uint32),
145 ("bone_count",c_uint32),
146 ("anim_start",c_uint32),
147 ("anim_count",c_uint32)]
148 #}
149
150 class mdl_animation(Structure):
151 #{
152 _fields_ = [("pstr_name",c_uint32),
153 ("length",c_uint32),
154 ("rate",c_float),
155 ("keyframe_start",c_uint32)]
156 #}
157
158 class mdl_mesh(Structure):
159 #{
160 _fields_ = [("transform",mdl_transform),
161 ("submesh_start",c_uint32),
162 ("submesh_count",c_uint32),
163 ("pstr_name",c_uint32),
164 ("entity_id",c_uint32),
165 ("armature_id",c_uint32)]
166 #}
167
168 class mdl_texture(Structure):
169 #{
170 _fields_ = [("file",mdl_file),
171 ("glname",c_uint32)]
172 #}
173
174 class mdl_array(Structure):
175 #{
176 _fields_ = [("file_offset",c_uint32),
177 ("item_count",c_uint32),
178 ("item_size",c_uint32),
179 ("name",c_byte*16)]
180 #}
181
182 class mdl_header(Structure):
183 #{
184 _fields_ = [("version",c_uint32),
185 ("arrays",mdl_array)]
186 #}
187
188 class ent_spawn(Structure):
189 #{
190 _fields_ = [("transform",mdl_transform),
191 ("pstr_name",c_uint32)]
192 #}
193
194 class ent_light(Structure):
195 #{
196 _fields_ = [("transform",mdl_transform),
197 ("daytime",c_uint32),
198 ("type",c_uint32),
199 ("colour",c_float*4),
200 ("angle",c_float),
201 ("range",c_float),
202 ("inverse_world",(c_float*3)*4), # Runtime
203 ("angle_sin_cos",(c_float*2))] # Runtime
204 #}
205
206 class version_refcount_union(Union):
207 #{
208 _fields_ = [("timing_version",c_uint32),
209 ("ref_count",c_uint8)]
210 #}
211
212 class ent_gate(Structure):
213 #{
214 _fields_ = [("flags",c_uint32),
215 ("target", c_uint32),
216 ("key",c_uint32),
217 ("dimensions", c_float*3),
218 ("co", (c_float*3)*2),
219 ("q", (c_float*4)*2),
220 ("to_world",(c_float*3)*4),
221 ("transport",(c_float*3)*4),
222 ("_anonymous_union",version_refcount_union),
223 ("timing_time",c_double),
224 ("routes",c_uint16*4),
225 ("route_count",c_uint8),
226 ("submesh_start",c_uint32), # v102+
227 ("submesh_count",c_uint32), # v102+ (can be 0)
228 ]
229 sr_functions = { 0: 'unlock' }
230 #}
231
232 class ent_route_node(Structure):
233 #{
234 _fields_ = [("co",c_float*3),
235 ("ref_count",c_uint8),
236 ("ref_total",c_uint8)]
237 #}
238
239 class ent_path_index(Structure):
240 #{
241 _fields_ = [("index",c_uint16)]
242 #}
243
244 class vg_audio_clip(Structure):
245 #{
246 _fields_ = [("path",c_uint64),
247 ("flags",c_uint32),
248 ("size",c_uint32),
249 ("data",c_uint64)]
250 #}
251
252 class union_file_audio_clip(Union):
253 #{
254 _fields_ = [("file",mdl_file),
255 ("reserved",vg_audio_clip)]
256 #}
257
258 # NOTE: not really an entity. no reason for ent_ -- makes more sense as file_,
259 # but then again, too late to change because compat.
260 class ent_audio_clip(Structure):
261 #{
262 _fields_ = [("_anon",union_file_audio_clip),
263 ("probability",c_float)]
264 #}
265
266 class ent_list(Structure):
267 #{
268 _fields_ = [("entity_ref_start",c_uint32),
269 ("entity_ref_count",c_uint32)]
270 #}
271
272 # used in ent_list
273 class file_entity_ref(Structure):
274 #{
275 _fields_ = [("index",c_uint32)]
276 #}
277
278 class ent_checkpoint(Structure):
279 #{
280 _fields_ = [("gate_index",c_uint16),
281 ("path_start",c_uint16),
282 ("path_count",c_uint16)]
283 #}
284
285 class ent_route(Structure):
286 #{
287 _fields_ = [("transform",mdl_transform),
288 ("pstr_name",c_uint32),
289 ("checkpoints_start",c_uint16),
290 ("checkpoints_count",c_uint16),
291 ("colour",c_float*4),
292 ("active",c_uint32), #runtime
293 ("factive",c_float),
294 ("board_transform",(c_float*3)*4),
295 ("sm",mdl_submesh),
296 ("latest_pass",c_double),
297 ("id_camera",c_uint32), # v103+
298 ]
299
300 sr_functions = { 0: 'view' }
301 #}
302
303 class ent_list(Structure):#{
304 _fields_ = [("start",c_uint16),("count",c_uint16)]
305 #}
306
307 class ent_glider(Structure):#{
308 _fields_ = [("transform",mdl_transform),
309 ("flags",c_uint32),
310 ("cooldown",c_float)]
311 sr_functions = { 0: 'unlock',
312 1: 'equip' }
313 #}
314
315 class ent_npc(Structure):#{
316 _fields_ = [("transform",mdl_transform),
317 ("id",c_uint32),
318 ("context",c_uint32),
319 ("camera",c_uint32)]
320 sr_functions = { 0: 'proximity', -1: 'leave' }
321 #}
322
323 class ent_water(Structure):
324 #{
325 _fields_ = [("transform",mdl_transform),
326 ("max_dist",c_float),
327 ("reserved0",c_uint32),
328 ("reserved1",c_uint32)]
329 sr_functions = { 0: "drown" }
330 #}
331
332 class volume_trigger(Structure):
333 #{
334 _fields_ = [("event",c_uint32),
335 ("event_leave",c_int32)]
336 #}
337
338 class volume_particles(Structure):
339 #{
340 _fields_ = [("blank",c_uint32),
341 ("blank2",c_uint32)]
342 #}
343
344 class volume_union(Union):
345 #{
346 _fields_ = [("trigger",volume_trigger),
347 ("particles",volume_particles)]
348 #}
349
350 class ent_volume(Structure):
351 #{
352 _fields_ = [("transform",mdl_transform),
353 ("to_world",(c_float*3)*4),
354 ("to_local",(c_float*3)*4),
355 ("type",c_uint32),
356 ("target",c_uint32),
357 ("_anon",volume_union)]
358 #}
359
360 class ent_audio(Structure):
361 #{
362 _fields_ = [("transform",mdl_transform),
363 ("flags",c_uint32),
364 ("clip_start",c_uint32),
365 ("clip_count",c_uint32),
366 ("volume",c_float),
367 ("crossfade",c_float),
368 ("channel_behaviour",c_uint32),
369 ("group",c_uint32),
370 ("probability_curve",c_uint32),
371 ("max_channels",c_uint32)]
372 #}
373
374 class ent_marker(Structure):
375 #{
376 _fields_ = [("transform",mdl_transform),
377 ("name",c_uint32)]
378 #}
379
380 class ent_glyph(Structure):
381 #{
382 _fields_ = [("size",c_float*2),
383 ("indice_start",c_uint32),
384 ("indice_count",c_uint32)]
385 #}
386
387 class ent_font_variant(Structure):
388 #{
389 _fields_ = [("name",c_uint32),
390 ("material_id",c_uint32)]
391 #}
392
393 class ent_font(Structure):
394 #{
395 _fields_ = [("alias",c_uint32),
396 ("variant_start",c_uint32),
397 ("variant_count",c_uint32),
398 ("glyph_start",c_uint32),
399 ("glyph_count",c_uint32),
400 ("glyph_utf32_base",c_uint32)]
401 #}
402
403 class ent_traffic(Structure):
404 #{
405 _fields_ = [("transform",mdl_transform),
406 ("submesh_start",c_uint32),
407 ("submesh_count",c_uint32),
408 ("start_node",c_uint32),
409 ("node_count",c_uint32),
410 ("speed",c_float),
411 ("t",c_float),
412 ("index",c_uint32)]
413 #}
414
415 # Skateshop
416 # ---------------------------------------------------------------
417 class ent_skateshop_characters(Structure):
418 #{
419 _fields_ = [("id_display",c_uint32),
420 ("id_info",c_uint32)]
421 #}
422 class ent_skateshop_boards(Structure):
423 #{
424 _fields_ = [("id_display",c_uint32),
425 ("id_info",c_uint32),
426 ("id_rack",c_uint32)]
427 #}
428 class ent_skateshop_worlds(Structure):
429 #{
430 _fields_ = [("id_display",c_uint32),
431 ("id_info",c_uint32)]
432 #}
433 class ent_skateshop_server(Structure):
434 #{
435 _fields_ = [("id_lever",c_uint32)]
436 #}
437 class ent_skateshop_anon_union(Union):
438 #{
439 _fields_ = [("boards",ent_skateshop_boards),
440 ("character",ent_skateshop_characters),
441 ("worlds",ent_skateshop_worlds),
442 ("server",ent_skateshop_server)]
443 #}
444 class ent_skateshop(Structure):
445 #{
446 _fields_ = [("transform",mdl_transform), ("type",c_uint32),
447 ("id_camera",c_uint32),
448 ("_anonymous_union",ent_skateshop_anon_union)]
449
450 sr_functions = { 0: 'trigger' }
451 #}
452
453 class ent_swspreview(Structure):
454 #{
455 _fields_ = [("id_camera",c_uint32),
456 ("id_display",c_uint32),
457 ("id_display1",c_uint32)]
458 #}
459
460 # Menu
461 # -----------------------------------------------------------------
462 class ent_menuitem_visual(Structure):
463 #{
464 _fields_ = [("pstr_name",c_uint32)]
465 #}
466 class ent_menuitem_slider(Structure):
467 #{
468 _fields_ = [("id_min",c_uint32),
469 ("id_max",c_uint32),
470 ("id_handle",c_uint32),
471 ("pstr_data",c_uint32)]
472 #}
473 class ent_menuitem_button(Structure):
474 #{
475 _fields_ = [("pstr",c_uint32),
476 ("stack_behaviour",c_uint32)]
477 #}
478 class ent_menuitem_checkmark(Structure):
479 #{
480 _fields_ = [("id_check",c_uint32),
481 ("pstr_data",c_uint32),
482 ("offset",c_float*3)]
483 #}
484 class ent_menuitem_page(Structure):
485 #{
486 _fields_ = [("pstr_name",c_uint32),
487 ("id_entrypoint",c_uint32),
488 ("id_viewpoint",c_uint32)]
489 #}
490 class ent_menuitem_binding(Structure):
491 #{
492 _fields_ = [("pstr_bind",c_uint32),
493 ("font_variant",c_uint32)]
494 #}
495 class ent_menuitem_anon_union(Union):
496 #{
497 _fields_ = [("slider",ent_menuitem_slider),
498 ("button",ent_menuitem_button),
499 ("checkmark",ent_menuitem_checkmark),
500 ("page",ent_menuitem_page),
501 ("visual",ent_menuitem_visual),
502 ("binding",ent_menuitem_binding)]
503 #}
504 class ent_menuitem(Structure):
505 #{
506 _fields_ = [("type",c_uint32), ("groups",c_uint32),
507 ("id_links",c_uint32*4),
508 ("factive",c_float), ("fvisible",c_float),
509 #-- TODO: Refactor this into a simple mesh structure
510 ("transform",mdl_transform),
511 ("submesh_start",c_uint32),("submesh_count",c_uint32),
512 ("_u64",c_uint64),
513 #-- end
514 ("_anonymous_union", ent_menuitem_anon_union)]
515 #}
516
517 class ent_camera(Structure):
518 #{
519 _fields_ = [("transform",mdl_transform),
520 ("fov",c_float)]
521 #}
522
523 class ent_worldinfo(Structure):
524 #{
525 _fields_ = [("pstr_name",c_uint32),
526 ("pstr_author",c_uint32), # unused
527 ("pstr_desc",c_uint32), # unused
528 ("timezone",c_float),
529 ("pstr_skybox",c_uint32),
530 ("flags",c_uint32)]
531 #}
532
533 class ent_ccmd(Structure):
534 #{
535 _fields_ = [("pstr_command",c_uint32)]
536 #}
537
538 class ent_objective(Structure):#{
539 _fields_ = [("transform",mdl_transform),
540 ("submesh_start",c_uint32), ("submesh_count",c_uint32),
541 ("flags",c_uint32),
542 ("id_next",c_uint32),
543 ("filter",c_uint32),("filter2",c_uint32),
544 ("id_win",c_uint32),
545 ("win_event",c_int32),
546 ("time_limit",c_float)]
547
548 sr_functions = { 0: 'trigger',
549 2: 'show',
550 3: 'hide' }
551 #}
552
553 class ent_challenge(Structure):#{
554 _fields_ = [("transform",mdl_transform),
555 ("pstr_alias",c_uint32),
556 ("flags",c_uint32),
557 ("target",c_uint32),
558 ("target_event",c_int32),
559 ("reset",c_uint32),
560 ("reset_event",c_int32),
561 ("first",c_uint32),
562 ("camera",c_uint32),
563 ("status",c_uint32)] #runtime
564 sr_functions = { 0: 'unlock',
565 1: 'view/reset' }
566 #}
567
568 class ent_region(Structure):#{
569 _fields_ = [("transform",mdl_transform),
570 ("submesh_start",c_uint32), ("submesh_count",c_uint32),
571 ("pstr_title",c_uint32),
572 ("flags",c_uint32),
573 ("zone_volume",c_uint32),
574 #105+
575 ("target0",c_uint32*2)]
576 sr_functions = { 0: 'enter', 1: 'leave' }
577 #}
578
579 class ent_relay(Structure):#{
580 _fields_ = [("targets",(c_uint32*2)*4),
581 ("targets_events",c_int32*4)]
582 sr_functions = { 0: 'trigger' }
583 #}
584
585 class ent_cubemap(Structure):#{
586 _fields_ = [("co",c_float*3),
587 ("resolution",c_uint32), #placeholder
588 ("live",c_uint32), #placeholder
589 ("texture_id",c_uint32), #engine
590 ("framebuffer_id",c_uint32),#engine
591 ("renderbuffer_id",c_uint32),#engine
592 ("placeholder",c_uint32*2)]
593 #}
594
595 class ent_miniworld(Structure):#{
596 _fields_ = [("transform",mdl_transform),
597 ("pstr_world",c_uint32),
598 ("camera",c_uint32),
599 ("proxy",c_uint32)]
600
601 sr_functions = { 0: 'zone', 1: 'leave' }
602 #}
603
604 class ent_prop(Structure):#{
605 _fields_ = [("transform",mdl_transform),
606 ("submesh_start",c_uint32),
607 ("submesh_count",c_uint32),
608 ("flags",c_uint32),
609 ("pstr_alias",c_uint32)]
610 #}
611
612 def obj_ent_type( obj ):
613 #{
614 if obj.type == 'ARMATURE': return 'mdl_armature'
615 elif obj.type == 'LIGHT': return 'ent_light'
616 elif obj.type == 'CAMERA': return 'ent_camera'
617 elif obj.type == 'LIGHT_PROBE' and obj.data.type == 'CUBEMAP':
618 return 'ent_cubemap'
619 else: return obj.SR_data.ent_type
620 #}
621
622 def sr_filter_ent_type( obj, ent_types ):
623 #{
624 if obj == bpy.context.active_object: return False
625
626 for c0 in obj.users_collection:#{
627 for c1 in bpy.context.active_object.users_collection:#{
628 if c0 == c1:#{
629 return obj_ent_type( obj ) in ent_types
630 #}
631 #}
632 #}
633
634 return False
635 #}
636
637 def v4_dot( a, b ):#{
638 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]
639 #}
640
641 def q_identity( q ):#{
642 q[0] = 0.0
643 q[1] = 0.0
644 q[2] = 0.0
645 q[3] = 1.0
646 #}
647
648 def q_normalize( q ):#{
649 l2 = v4_dot(q,q)
650 if( l2 < 0.00001 ):#{
651 q_identity( q )
652 #}
653 else:#{
654 s = 1.0/math.sqrt(l2)
655 q[0] *= s
656 q[1] *= s
657 q[2] *= s
658 q[3] *= s
659 #}
660 #}
661
662 def compile_obj_transform( obj, transform ):
663 #{
664 co = obj.matrix_world @ Vector((0,0,0))
665
666 # This was changed from matrix_local on 09.05.23
667 q = obj.matrix_world.to_quaternion()
668 s = obj.scale
669 q_normalize( q )
670
671 # Setup transform
672 #
673 transform.co[0] = co[0]
674 transform.co[1] = co[2]
675 transform.co[2] = -co[1]
676 transform.q[0] = q[1]
677 transform.q[1] = q[3]
678 transform.q[2] = -q[2]
679 transform.q[3] = q[0]
680 transform.s[0] = s[0]
681 transform.s[1] = s[2]
682 transform.s[2] = s[1]
683 #}
684
685 def int_align_to( v, align ):
686 #{
687 while(v%align)!=0: v += 1
688 return v
689 #}
690
691 def bytearray_align_to( buffer, align, w=b'\xaa' ):
692 #{
693 while (len(buffer) % align) != 0: buffer.extend(w)
694 return buffer
695 #}
696
697 def bytearray_print_hex( s, w=16 ):
698 #{
699 for r in range((len(s)+(w-1))//w):#{
700 i0=(r+0)*w
701 i1=min((r+1)*w,len(s))
702 print( F'{r*w:06x}| \x1B[31m', end='')
703 print( F"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end='' )
704 print( "\x1B[0m", end='')
705 print( ''.join(chr(x) if (x>=33 and x<=126) else '.' for x in s[i0:i1] ) )
706 #}
707 #}
708
709 def sr_compile_string( s ):
710 #{
711 if s in sr_compile.string_cache: return sr_compile.string_cache[s]
712
713 index = len( sr_compile.string_data )
714 sr_compile.string_cache[s] = index
715 sr_compile.string_data.extend( c_uint32(hash_djb2(s)) )
716 sr_compile.string_data.extend( s.encode('utf-8') )
717 sr_compile.string_data.extend( b'\0' )
718
719 bytearray_align_to( sr_compile.string_data, 4 )
720 return index
721 #}
722
723 def vg_str_bin( s ):
724 #{
725 decoded = bytearray()
726 for i in range(len(s)//2):#{
727 c = (ord(s[i*2+0])-0x41)
728 c |= (ord(s[i*2+1])-0x41)<<4
729 decoded.extend(bytearray(c_uint8(c))) #??
730 #}
731 return decoded
732 #}
733
734 def sr_pack_file( file, path, data ):
735 #{
736 file.path = sr_compile_string( path )
737 file.pack_offset = len( sr_compile.pack_data )
738 file.pack_size = len( data )
739
740 sr_compile.pack_data.extend( data )
741 bytearray_align_to( sr_compile.pack_data, 16 )
742 #}
743
744 def sr_compile_texture( img ):
745 #{
746 if img == None:
747 return 0
748
749 name = os.path.splitext( img.name )[0]
750
751 if name in sr_compile.texture_cache:
752 return sr_compile.texture_cache[name]
753
754 texture_index = (len(sr_compile.texture_data)//sizeof(mdl_texture)) +1
755
756 tex = mdl_texture()
757 tex.glname = 0
758
759 if sr_compile.pack_textures:#{
760 filedata = qoi_encode( img )
761 sr_pack_file( tex.file, name, filedata )
762 #}
763
764 sr_compile.texture_cache[name] = texture_index
765 sr_compile.texture_data.extend( bytearray(tex) )
766 return texture_index
767 #}
768
769 def sr_armature_bones( armature ):
770 #{
771 def _recurse_bone( b ):
772 #{
773 yield b
774 for c in b.children: yield from _recurse_bone( c )
775 #}
776
777 for b in armature.data.bones:
778 if not b.parent:
779 yield from _recurse_bone( b )
780 #}
781
782 def sr_entity_id( obj ):#{
783 if not obj: return 0
784
785 tipo = get_entity_enum_id( obj_ent_type(obj) )
786 index = sr_compile.entity_ids[ obj.name ]
787
788 return (tipo&0xffff)<<16 | (index&0xffff)
789 #}
790
791 # Returns submesh_start,count and armature_id
792 def sr_compile_mesh_internal( obj ):
793 #{
794 can_use_cache = True
795 armature = None
796
797 submesh_start = 0
798 submesh_count = 0
799 armature_id = 0
800
801 for mod in obj.modifiers:#{
802 if mod.type == 'DATA_TRANSFER' or mod.type == 'SHRINKWRAP' or \
803 mod.type == 'BOOLEAN' or mod.type == 'CURVE' or \
804 mod.type == 'ARRAY':
805 #{
806 can_use_cache = False
807 #}
808
809 if mod.type == 'ARMATURE': #{
810 armature = mod.object
811 rig_weight_groups = \
812 ['0 [ROOT]']+[_.name for _ in sr_armature_bones(mod.object)]
813 armature_id = sr_compile.entity_ids[armature.name]
814
815 POSE_OR_REST_CACHE = armature.data.pose_position
816 armature.data.pose_position = 'REST'
817 #}
818 #}
819
820 # Check the cache first
821 #
822 if can_use_cache and (obj.data.name in sr_compile.mesh_cache):#{
823 ref = sr_compile.mesh_cache[obj.data.name]
824 submesh_start = ref[0]
825 submesh_count = ref[1]
826 return (submesh_start,submesh_count,armature_id)
827 #}
828
829 # Compile a whole new mesh
830 #
831 submesh_start = len(sr_compile.submesh_data)//sizeof(mdl_submesh)
832 submesh_count = 0
833
834 dgraph = bpy.context.evaluated_depsgraph_get()
835 data = obj.evaluated_get(dgraph).data
836 data.calc_loop_triangles()
837 data.calc_normals_split()
838
839 # Mesh is split into submeshes based on their material
840 #
841 mat_list = data.materials if len(data.materials) > 0 else [None]
842 for material_id, mat in enumerate(mat_list): #{
843 mref = {}
844
845 sm = mdl_submesh()
846 sm.indice_start = len(sr_compile.indice_data)//sizeof(c_uint32)
847 sm.vertex_start = len(sr_compile.vertex_data)//sizeof(mdl_vert)
848 sm.vertex_count = 0
849 sm.indice_count = 0
850 sm.material_id = sr_compile_material( mat )
851
852 INF=99999999.99999999
853 for i in range(3):#{
854 sm.bbx[0][i] = INF
855 sm.bbx[1][i] = -INF
856 #}
857
858 # Keep a reference to very very very similar vertices
859 # i have no idea how to speed it up.
860 #
861 vertex_reference = {}
862
863 # Write the vertex / indice data
864 #
865 for tri_index, tri in enumerate(data.loop_triangles):#{
866 if tri.material_index != material_id: continue
867
868 for j in range(3):#{
869 vert = data.vertices[tri.vertices[j]]
870 li = tri.loops[j]
871 vi = data.loops[li].vertex_index
872
873 # Gather vertex information
874 #
875 co = vert.co
876 norm = data.loops[li].normal
877 uv = (0,0)
878 colour = (255,255,255,255)
879 groups = [0,0,0,0]
880 weights = [0,0,0,0]
881
882 # Uvs
883 #
884 if data.uv_layers:
885 uv = data.uv_layers.active.data[li].uv
886
887 # Vertex Colours
888 #
889 if data.vertex_colors:#{
890 colour = data.vertex_colors.active.data[li].color
891 colour = (int(colour[0]*255.0),\
892 int(colour[1]*255.0),\
893 int(colour[2]*255.0),\
894 int(colour[3]*255.0))
895 #}
896
897 # Weight groups: truncates to the 3 with the most influence. The
898 # fourth bone ID is never used by the shader so it
899 # is always 0
900 #
901 if armature:#{
902 src_groups = [_ for _ in data.vertices[vi].groups \
903 if obj.vertex_groups[_.group].name in \
904 rig_weight_groups ]
905
906 weight_groups = sorted( src_groups, key = \
907 lambda a: a.weight, reverse=True )
908 tot = 0.0
909 for ml in range(3):#{
910 if len(weight_groups) > ml:#{
911 g = weight_groups[ml]
912 name = obj.vertex_groups[g.group].name
913 weight = g.weight
914 weights[ml] = weight
915 groups[ml] = rig_weight_groups.index(name)
916 tot += weight
917 #}
918 #}
919
920 if len(weight_groups) > 0:#{
921 inv_norm = (1.0/tot) * 65535.0
922 for ml in range(3):#{
923 weights[ml] = int( weights[ml] * inv_norm )
924 weights[ml] = min( weights[ml], 65535 )
925 weights[ml] = max( weights[ml], 0 )
926 #}
927 #}
928 #}
929 else:#{
930 li1 = tri.loops[(j+1)%3]
931 vi1 = data.loops[li1].vertex_index
932 e0 = data.edges[ data.loops[li].edge_index ]
933
934 if e0.use_freestyle_mark and \
935 ((e0.vertices[0] == vi and e0.vertices[1] == vi1) or \
936 (e0.vertices[0] == vi1 and e0.vertices[1] == vi)):
937 #{
938 weights[0] = 1
939 #}
940 #}
941
942 TOLERENCE = float(10**4)
943 key = (int(co[0]*TOLERENCE+0.5),
944 int(co[1]*TOLERENCE+0.5),
945 int(co[2]*TOLERENCE+0.5),
946 int(norm[0]*TOLERENCE+0.5),
947 int(norm[1]*TOLERENCE+0.5),
948 int(norm[2]*TOLERENCE+0.5),
949 int(uv[0]*TOLERENCE+0.5),
950 int(uv[1]*TOLERENCE+0.5),
951 colour[0], # these guys are already quantized
952 colour[1], # .
953 colour[2], # .
954 colour[3], # .
955 weights[0], # v
956 weights[1],
957 weights[2],
958 weights[3],
959 groups[0],
960 groups[1],
961 groups[2],
962 groups[3])
963
964 if key in vertex_reference:
965 index = vertex_reference[key]
966 else:#{
967 index = bytearray(c_uint32(sm.vertex_count))
968 sm.vertex_count+=1
969
970 vertex_reference[key] = index
971 v = mdl_vert()
972 v.co[0] = co[0]
973 v.co[1] = co[2]
974 v.co[2] = -co[1]
975 v.norm[0] = norm[0]
976 v.norm[1] = norm[2]
977 v.norm[2] = -norm[1]
978 v.uv[0] = uv[0]
979 v.uv[1] = uv[1]
980 v.colour[0] = colour[0]
981 v.colour[1] = colour[1]
982 v.colour[2] = colour[2]
983 v.colour[3] = colour[3]
984 v.weights[0] = weights[0]
985 v.weights[1] = weights[1]
986 v.weights[2] = weights[2]
987 v.weights[3] = weights[3]
988 v.groups[0] = groups[0]
989 v.groups[1] = groups[1]
990 v.groups[2] = groups[2]
991 v.groups[3] = groups[3]
992
993 for i in range(3):#{
994 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
995 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
996 #}
997
998 sr_compile.vertex_data.extend(bytearray(v))
999 #}
1000
1001 sm.indice_count += 1
1002 sr_compile.indice_data.extend( index )
1003 #}
1004 #}
1005
1006 # Make sure bounding box isn't -inf -> inf if no vertices
1007 #
1008 if sm.vertex_count == 0:
1009 for j in range(2):
1010 for i in range(3):
1011 sm.bbx[j][i] = 0
1012
1013 # Add submesh to encoder
1014 #
1015 sr_compile.submesh_data.extend( bytearray(sm) )
1016 submesh_count += 1
1017 #}
1018
1019 if armature:#{
1020 armature.data.pose_position = POSE_OR_REST_CACHE
1021 #}
1022
1023 # Save a reference to this mesh since we want to reuse the submesh indices
1024 # later.
1025 sr_compile.mesh_cache[obj.data.name]=(submesh_start,submesh_count)
1026 return (submesh_start,submesh_count,armature_id)
1027 #}
1028
1029 def sr_compile_mesh( obj ):
1030 #{
1031 node=mdl_mesh()
1032 compile_obj_transform(obj, node.transform)
1033 node.pstr_name = sr_compile_string(obj.name)
1034 ent_type = obj_ent_type( obj )
1035
1036 node.entity_id = 0
1037
1038 if ent_type != 'none':#{
1039 ent_id_lwr = sr_compile.entity_ids[obj.name]
1040 ent_id_upr = get_entity_enum_id( obj_ent_type(obj) )
1041 node.entity_id = (ent_id_upr << 16) | ent_id_lwr
1042 #}
1043
1044 node.submesh_start, node.submesh_count, node.armature_id = \
1045 sr_compile_mesh_internal( obj )
1046
1047 sr_compile.mesh_data.extend(bytearray(node))
1048 #}
1049
1050 def sr_compile_fonts( collection ):
1051 #{
1052 print( F"[SR] Compiling fonts" )
1053
1054 glyph_count = 0
1055 variant_count = 0
1056
1057 for obj in collection.all_objects:#{
1058 if obj_ent_type(obj) != 'ent_font': continue
1059
1060 data = obj.SR_data.ent_font[0]
1061
1062 font=ent_font()
1063 font.alias = sr_compile_string( data.alias )
1064 font.variant_start = variant_count
1065 font.variant_count = 0
1066 font.glyph_start = glyph_count
1067
1068 glyph_base = data.glyphs[0].utf32
1069 glyph_range = data.glyphs[-1].utf32+1 - glyph_base
1070
1071 font.glyph_utf32_base = glyph_base
1072 font.glyph_count = glyph_range
1073
1074 for i in range(len(data.variants)):#{
1075 data_var = data.variants[i]
1076 if not data_var.mesh: continue
1077
1078 mesh = data_var.mesh.data
1079
1080 variant = ent_font_variant()
1081 variant.name = sr_compile_string( data_var.tipo )
1082
1083 # fonts (variants) only support one material each
1084 mat = None
1085 if len(mesh.materials) != 0:
1086 mat = mesh.materials[0]
1087 variant.material_id = sr_compile_material( mat )
1088
1089 font.variant_count += 1
1090
1091 islands = mesh_utils.mesh_linked_triangles(mesh)
1092 centroids = [Vector((0,0)) for _ in range(len(islands))]
1093
1094 for j in range(len(islands)):#{
1095 for tri in islands[j]:#{
1096 centroids[j].x += tri.center[0]
1097 centroids[j].y += tri.center[2]
1098 #}
1099
1100 centroids[j] /= len(islands[j])
1101 #}
1102
1103 for j in range(glyph_range):#{
1104 data_glyph = data.glyphs[j]
1105 glyph = ent_glyph()
1106 glyph.indice_start = len(sr_compile.indice_data)//sizeof(c_uint32)
1107 glyph.indice_count = 0
1108 glyph.size[0] = data_glyph.bounds[2]
1109 glyph.size[1] = data_glyph.bounds[3]
1110
1111 vertex_reference = {}
1112
1113 for k in range(len(islands)):#{
1114 if centroids[k].x < data_glyph.bounds[0] or \
1115 centroids[k].x > data_glyph.bounds[0]+data_glyph.bounds[2] or\
1116 centroids[k].y < data_glyph.bounds[1] or \
1117 centroids[k].y > data_glyph.bounds[1]+data_glyph.bounds[3]:
1118 #{
1119 continue
1120 #}
1121
1122 for l in range(len(islands[k])):#{
1123 tri = islands[k][l]
1124 for m in range(3):#{
1125 vert = mesh.vertices[tri.vertices[m]]
1126 li = tri.loops[m]
1127 vi = mesh.loops[li].vertex_index
1128
1129 # Gather vertex information
1130 #
1131 co = [vert.co[_] for _ in range(3)]
1132 co[0] -= data_glyph.bounds[0]
1133 co[2] -= data_glyph.bounds[1]
1134 norm = mesh.loops[li].normal
1135 uv = (0,0)
1136 if mesh.uv_layers: uv = mesh.uv_layers.active.data[li].uv
1137
1138 TOLERENCE = float(10**4)
1139 key = (int(co[0]*TOLERENCE+0.5),
1140 int(co[1]*TOLERENCE+0.5),
1141 int(co[2]*TOLERENCE+0.5),
1142 int(norm[0]*TOLERENCE+0.5),
1143 int(norm[1]*TOLERENCE+0.5),
1144 int(norm[2]*TOLERENCE+0.5),
1145 int(uv[0]*TOLERENCE+0.5),
1146 int(uv[1]*TOLERENCE+0.5))
1147
1148 if key in vertex_reference:
1149 index = vertex_reference[key]
1150 else:#{
1151 vindex = len(sr_compile.vertex_data)//sizeof(mdl_vert)
1152 index = bytearray(c_uint32(vindex))
1153 vertex_reference[key] = index
1154 v = mdl_vert()
1155 v.co[0] = co[0]
1156 v.co[1] = co[2]
1157 v.co[2] = -co[1]
1158 v.norm[0] = norm[0]
1159 v.norm[1] = norm[2]
1160 v.norm[2] = -norm[1]
1161 v.uv[0] = uv[0]
1162 v.uv[1] = uv[1]
1163
1164 sr_compile.vertex_data.extend(bytearray(v))
1165 #}
1166
1167 glyph.indice_count += 1
1168 sr_compile.indice_data.extend( index )
1169 #}
1170 #}
1171 #}
1172 sr_ent_push( glyph )
1173 #}
1174 sr_ent_push( variant )
1175 #}
1176 sr_ent_push( font )
1177 #}
1178 #}
1179
1180 def sr_compile_menus( collection ):
1181 #{
1182 print( "[SR1] Compiling menus" )
1183 groups = []
1184
1185 for obj in collection.all_objects:#{
1186 if obj_ent_type(obj) != 'ent_menuitem': continue
1187 obj_data = obj.SR_data.ent_menuitem[0]
1188
1189 bitmask = 0x00000000
1190
1191 for col in obj.users_collection:#{
1192 name = col.name
1193 if name not in groups: groups.append( name )
1194 bitmask |= (0x1 << groups.index(name))
1195 #}
1196
1197 item = ent_menuitem()
1198 item.type = int( obj_data.tipo )
1199 item.groups = bitmask
1200
1201 compile_obj_transform( obj, item.transform )
1202 if obj.type == 'MESH':#{
1203 item.submesh_start, item.submesh_count, _ = \
1204 sr_compile_mesh_internal( obj )
1205 #}
1206
1207 if item.type == 1 or item.type == 2 or item.type == 7:#{
1208 item_button = item._anonymous_union.button
1209 item_button.pstr = sr_compile_string( obj_data.string )
1210 item_button.stack_behaviour = int( obj_data.stack_behaviour )
1211 #}
1212 elif item.type == 0:#{
1213 item_visual = item._anonymous_union.visual
1214 item_visual.pstr_name = sr_compile_string( obj_data.string )
1215 #}
1216 elif item.type == 3:#{
1217 item_checkmark = item._anonymous_union.checkmark
1218 item_checkmark.pstr_data = sr_compile_string( obj_data.string )
1219 item_checkmark.id_check = sr_entity_id( obj_data.checkmark )
1220 delta = obj_data.checkmark.location - obj.location
1221 item_checkmark.offset[0] = delta[0]
1222 item_checkmark.offset[1] = delta[2]
1223 item_checkmark.offset[2] = -delta[1]
1224 #}
1225 elif item.type == 4:#{
1226 item_slider = item._anonymous_union.slider
1227 item_slider.id_min = sr_entity_id( obj_data.slider_minloc )
1228 item_slider.id_max = sr_entity_id( obj_data.slider_maxloc )
1229 item_slider.id_handle = sr_entity_id( obj_data.slider_handle )
1230 item_slider.pstr_data = sr_compile_string( obj_data.string )
1231 #}
1232 elif item.type == 5:#{
1233 item_page = item._anonymous_union.page
1234 item_page.pstr_name = sr_compile_string( obj_data.string )
1235 item_page.id_entrypoint = sr_entity_id( obj_data.newloc )
1236 item_page.id_viewpoint = sr_entity_id( obj_data.camera )
1237 #}
1238 elif item.type == 6:#{
1239 item_binding = item._anonymous_union.binding
1240 item_binding.pstr_bind = sr_compile_string( obj_data.string )
1241 item_binding.font_variant = obj_data.font_variant
1242 #}
1243
1244 if obj_data.link0:
1245 item.id_links[0] = sr_entity_id( obj_data.link0 )
1246 if obj_data.link1:
1247 item.id_links[1] = sr_entity_id( obj_data.link1 )
1248 if item.type != 4:#{
1249 if obj_data.link2:
1250 item.id_links[2] = sr_entity_id( obj_data.link2 )
1251 if obj_data.link3:
1252 item.id_links[3] = sr_entity_id( obj_data.link3 )
1253 #}
1254
1255 sr_ent_push( item )
1256 #}
1257 #}
1258
1259 def sr_compile_armature( obj ):
1260 #{
1261 node = mdl_armature()
1262 node.bone_start = len(sr_compile.bone_data)//sizeof(mdl_bone)
1263 node.bone_count = 0
1264 node.anim_start = len(sr_compile.anim_data)//sizeof(mdl_animation)
1265 node.anim_count = 0
1266
1267 bones = [_ for _ in sr_armature_bones(obj)]
1268 bones_names = [None]+[_.name for _ in bones]
1269
1270 for b in bones:#{
1271 bone = mdl_bone()
1272 if b.use_deform: bone.flags = 0x1
1273 if b.parent: bone.parent = bones_names.index(b.parent.name)
1274
1275 bone.collider = int(b.SR_data.collider)
1276
1277 if bone.collider>0:#{
1278 bone.hitbox[0][0] = b.SR_data.collider_min[0]
1279 bone.hitbox[0][1] = b.SR_data.collider_min[2]
1280 bone.hitbox[0][2] = -b.SR_data.collider_max[1]
1281 bone.hitbox[1][0] = b.SR_data.collider_max[0]
1282 bone.hitbox[1][1] = b.SR_data.collider_max[2]
1283 bone.hitbox[1][2] = -b.SR_data.collider_min[1]
1284 #}
1285
1286 if b.SR_data.cone_constraint:#{
1287 bone.flags |= 0x4
1288 bone.conevx[0] = b.SR_data.conevx[0]
1289 bone.conevx[1] = b.SR_data.conevx[2]
1290 bone.conevx[2] = -b.SR_data.conevx[1]
1291 bone.conevy[0] = b.SR_data.conevy[0]
1292 bone.conevy[1] = b.SR_data.conevy[2]
1293 bone.conevy[2] = -b.SR_data.conevy[1]
1294 bone.coneva[0] = b.SR_data.coneva[0]
1295 bone.coneva[1] = b.SR_data.coneva[2]
1296 bone.coneva[2] = -b.SR_data.coneva[1]
1297 bone.conet = b.SR_data.conet
1298 #}
1299
1300 bone.co[0] = b.head_local[0]
1301 bone.co[1] = b.head_local[2]
1302 bone.co[2] = -b.head_local[1]
1303 bone.end[0] = b.tail_local[0] - bone.co[0]
1304 bone.end[1] = b.tail_local[2] - bone.co[1]
1305 bone.end[2] = -b.tail_local[1] - bone.co[2]
1306 bone.pstr_name = sr_compile_string( b.name )
1307
1308 for c in obj.pose.bones[b.name].constraints:#{
1309 if c.type == 'IK':#{
1310 bone.flags |= 0x2
1311 bone.ik_target = bones_names.index(c.subtarget)
1312 bone.ik_pole = bones_names.index(c.pole_subtarget)
1313 #}
1314 #}
1315
1316 node.bone_count += 1
1317 sr_compile.bone_data.extend(bytearray(bone))
1318 #}
1319
1320 # Compile anims
1321 #
1322 if obj.animation_data and sr_compile.pack_animations: #{
1323 # So we can restore later
1324 #
1325 previous_frame = bpy.context.scene.frame_current
1326 previous_action = obj.animation_data.action
1327 POSE_OR_REST_CACHE = obj.data.pose_position
1328 obj.data.pose_position = 'POSE'
1329
1330 for NLALayer in obj.animation_data.nla_tracks:#{
1331 for NLAStrip in NLALayer.strips:#{
1332 # set active
1333 #
1334 for a in bpy.data.actions:#{
1335 if a.name == NLAStrip.name:#{
1336 obj.animation_data.action = a
1337 break
1338 #}
1339 #}
1340
1341 # Clip to NLA settings
1342 #
1343 anim_start = int(NLAStrip.action_frame_start)
1344 anim_end = int(NLAStrip.action_frame_end)
1345
1346 # Export strips
1347 #
1348 anim = mdl_animation()
1349 anim.pstr_name = sr_compile_string( NLAStrip.action.name )
1350 anim.rate = 30.0
1351 anim.keyframe_start = len(sr_compile.keyframe_data)//\
1352 sizeof(mdl_transform)
1353 anim.length = anim_end-anim_start
1354
1355 i = 0
1356 # Export the keyframes
1357 for frame in range(anim_start,anim_end):#{
1358 bpy.context.scene.frame_set(frame)
1359
1360 for rb in bones:#{
1361 pb = obj.pose.bones[rb.name]
1362
1363 # relative bone matrix
1364 if rb.parent is not None:#{
1365 offset_mtx = rb.parent.matrix_local
1366 offset_mtx = offset_mtx.inverted_safe() @ \
1367 rb.matrix_local
1368
1369 inv_parent = pb.parent.matrix @ offset_mtx
1370 inv_parent.invert_safe()
1371 fpm = inv_parent @ pb.matrix
1372 #}
1373 else:#{
1374 bone_mtx = rb.matrix.to_4x4()
1375 local_inv = rb.matrix_local.inverted_safe()
1376 fpm = bone_mtx @ local_inv @ pb.matrix
1377 #}
1378
1379 loc, rot, sca = fpm.decompose()
1380
1381 # rotation
1382 lc_m = pb.matrix_channel.to_3x3()
1383 if pb.parent is not None:#{
1384 smtx = pb.parent.matrix_channel.to_3x3()
1385 lc_m = smtx.inverted() @ lc_m
1386 #}
1387 rq = lc_m.to_quaternion()
1388 q_normalize( rq )
1389
1390 kf = mdl_transform()
1391 kf.co[0] = loc[0]
1392 kf.co[1] = loc[2]
1393 kf.co[2] = -loc[1]
1394 kf.q[0] = rq[1]
1395 kf.q[1] = rq[3]
1396 kf.q[2] = -rq[2]
1397 kf.q[3] = rq[0]
1398 kf.s[0] = sca[0]
1399 kf.s[1] = sca[1]
1400 kf.s[2] = sca[2]
1401 sr_compile.keyframe_data.extend(bytearray(kf))
1402
1403 i+=1
1404 #}
1405 #}
1406
1407 # Add to animation buffer
1408 #
1409 sr_compile.anim_data.extend(bytearray(anim))
1410 node.anim_count += 1
1411
1412 # Report progress
1413 #
1414 print( F"[SR] | anim( {NLAStrip.action.name} )" )
1415 #}
1416 #}
1417
1418 # Restore context to how it was before
1419 #
1420 bpy.context.scene.frame_set( previous_frame )
1421 obj.animation_data.action = previous_action
1422 obj.data.pose_position = POSE_OR_REST_CACHE
1423 #}
1424
1425 sr_compile.armature_data.extend(bytearray(node))
1426 #}
1427
1428 def sr_ent_push( struct ):
1429 #{
1430 clase = type(struct).__name__
1431
1432 if clase not in sr_compile.entity_data:#{
1433 sr_compile.entity_data[ clase ] = bytearray()
1434 sr_compile.entity_info[ clase ] = { 'size': sizeof(struct) }
1435 #}
1436
1437 index = len(sr_compile.entity_data[ clase ])//sizeof(struct)
1438 sr_compile.entity_data[ clase ].extend( bytearray(struct) )
1439 return index
1440 #}
1441
1442 def sr_array_title( arr, name, count, size, offset ):
1443 #{
1444 for i in range(len(name)):#{
1445 arr.name[i] = ord(name[i])
1446 #}
1447 arr.file_offset = offset
1448 arr.item_count = count
1449 arr.item_size = size
1450 #}
1451
1452 def hash_djb2(s):
1453 #{
1454 picadillo = 5381
1455 for x in s:#{
1456 picadillo = (((picadillo << 5) + picadillo) + ord(x)) & 0xFFFFFFFF
1457 #}
1458 return picadillo
1459 #}
1460
1461 def sr_compile( collection ):
1462 #{
1463 print( F"[SR] compiler begin ({collection.name}.mdl)" )
1464 sr_lib_init()
1465
1466 #settings
1467 sr_compile.pack_textures = collection.SR_data.pack_textures
1468 sr_compile.pack_animations = collection.SR_data.animations
1469
1470 # caches
1471 sr_compile.string_cache = {}
1472 sr_compile.mesh_cache = {}
1473 sr_compile.material_cache = {}
1474 sr_compile.texture_cache = {}
1475
1476 # compiled data
1477 sr_compile.mesh_data = bytearray()
1478 sr_compile.submesh_data = bytearray()
1479 sr_compile.vertex_data = bytearray()
1480 sr_compile.indice_data = bytearray()
1481 sr_compile.bone_data = bytearray()
1482 sr_compile.material_data = bytearray()
1483 sr_compile.shader_data = bytearray()
1484 sr_compile.armature_data = bytearray()
1485 sr_compile.anim_data = bytearray()
1486 sr_compile.keyframe_data = bytearray()
1487 sr_compile.texture_data = bytearray()
1488
1489 # just bytes not structures
1490 sr_compile.string_data = bytearray()
1491 sr_compile.pack_data = bytearray()
1492
1493 # variable
1494 sr_compile.entity_data = {}
1495 sr_compile.entity_info = {}
1496
1497 print( F"[SR] assign entity ID's" )
1498 sr_compile.entities = {}
1499 sr_compile.entity_ids = {}
1500
1501 # begin
1502 # -------------------------------------------------------
1503
1504 sr_compile_string( "null" )
1505
1506 mesh_count = 0
1507 for obj in collection.all_objects: #{
1508 if obj.type == 'MESH':#{
1509 mesh_count += 1
1510 #}
1511
1512 ent_type = obj_ent_type( obj )
1513 if ent_type == 'none': continue
1514
1515 if ent_type not in sr_compile.entities: sr_compile.entities[ent_type] = []
1516 sr_compile.entity_ids[obj.name] = len( sr_compile.entities[ent_type] )
1517 sr_compile.entities[ent_type] += [obj]
1518 #}
1519
1520 print( F"[SR] Compiling geometry" )
1521 i=0
1522 for obj in collection.all_objects:#{
1523 if obj.type == 'MESH':#{
1524 i+=1
1525
1526 ent_type = obj_ent_type( obj )
1527
1528 # entity ignore mesh list
1529 #
1530 if ent_type == 'ent_traffic': continue
1531 if ent_type == 'ent_prop': continue
1532 if ent_type == 'ent_font': continue
1533 if ent_type == 'ent_font_variant': continue
1534 if ent_type == 'ent_menuitem': continue
1535 if ent_type == 'ent_objective': continue
1536 if ent_type == 'ent_region': continue
1537
1538 #TODO: This is messy.
1539 if ent_type == 'ent_gate':#{
1540 obj_data = obj.SR_data.ent_gate[0]
1541 if obj_data.custom: continue
1542 #}
1543 #--------------------------
1544
1545 print( F'[SR] {i: 3}/{mesh_count} {obj.name:<40}' )
1546 sr_compile_mesh( obj )
1547 #}
1548 #}
1549
1550 audio_clip_count = 0
1551 entity_file_ref_count = 0
1552
1553 for ent_type, arr in sr_compile.entities.items():#{
1554 print(F"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1555
1556 for i in range(len(arr)):#{
1557 obj = arr[i]
1558
1559 print( F"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end='\r' )
1560
1561 if ent_type == 'mdl_armature': sr_compile_armature(obj)
1562 elif ent_type == 'ent_light': #{
1563 light = ent_light()
1564 compile_obj_transform( obj, light.transform )
1565 light.daytime = obj.data.SR_data.daytime
1566 if obj.data.type == 'POINT':#{
1567 light.type = 0
1568 #}
1569 elif obj.data.type == 'SPOT':#{
1570 light.type = 1
1571 light.angle = obj.data.spot_size*0.5
1572 #}
1573 light.range = obj.data.cutoff_distance
1574 light.colour[0] = obj.data.color[0]
1575 light.colour[1] = obj.data.color[1]
1576 light.colour[2] = obj.data.color[2]
1577 light.colour[3] = obj.data.energy
1578 sr_ent_push( light )
1579 #}
1580 elif ent_type == 'ent_camera': #{
1581 cam = ent_camera()
1582 compile_obj_transform( obj, cam.transform )
1583 cam.fov = obj.data.angle * 45.0
1584 sr_ent_push(cam)
1585 #}
1586 elif ent_type == 'ent_gate': #{
1587 gate = ent_gate()
1588 obj_data = obj.SR_data.ent_gate[0]
1589 mesh_data = obj.data.SR_data.ent_gate[0]
1590
1591 flags = 0x0000
1592
1593 if obj_data.tipo == 'default':#{
1594 if obj_data.target:#{
1595 gate.target = sr_compile.entity_ids[obj_data.target.name]
1596 flags |= 0x0001
1597 #}
1598 #}
1599 elif obj_data.tipo == 'nonlocal':#{
1600 gate.target = 0
1601 gate.key = sr_compile_string(obj_data.key)
1602 flags |= 0x0002
1603 #}
1604
1605 if obj_data.flip: flags |= 0x0004
1606 if obj_data.custom:#{
1607 flags |= 0x0008
1608 gate.submesh_start, gate.submesh_count, _ = \
1609 sr_compile_mesh_internal( obj )
1610 #}
1611 if obj_data.locked: flags |= 0x0010
1612 gate.flags = flags
1613
1614 gate.dimensions[0] = mesh_data.dimensions[0]
1615 gate.dimensions[1] = mesh_data.dimensions[1]
1616 gate.dimensions[2] = mesh_data.dimensions[2]
1617
1618 q = [obj.matrix_local.to_quaternion(), (0,0,0,1)]
1619 co = [obj.matrix_world @ Vector((0,0,0)), (0,0,0)]
1620
1621 if obj_data.target:#{
1622 q[1] = obj_data.target.matrix_local.to_quaternion()
1623 co[1]= obj_data.target.matrix_world @ Vector((0,0,0))
1624 #}
1625
1626 # Setup transform
1627 #
1628 for x in range(2):#{
1629 gate.co[x][0] = co[x][0]
1630 gate.co[x][1] = co[x][2]
1631 gate.co[x][2] = -co[x][1]
1632 gate.q[x][0] = q[x][1]
1633 gate.q[x][1] = q[x][3]
1634 gate.q[x][2] = -q[x][2]
1635 gate.q[x][3] = q[x][0]
1636 #}
1637
1638 sr_ent_push( gate )
1639 #}
1640 elif ent_type == 'ent_spawn': #{
1641 spawn = ent_spawn()
1642 compile_obj_transform( obj, spawn.transform )
1643 obj_data = obj.SR_data.ent_spawn[0]
1644 spawn.pstr_name = sr_compile_string( obj_data.alias )
1645 sr_ent_push( spawn )
1646 #}
1647 elif ent_type == 'ent_water':#{
1648 water = ent_water()
1649 compile_obj_transform( obj, water.transform )
1650 water.max_dist = 0.0
1651 sr_ent_push( water )
1652 #}
1653 elif ent_type == 'ent_audio':#{
1654 obj_data = obj.SR_data.ent_audio[0]
1655 audio = ent_audio()
1656 compile_obj_transform( obj, audio.transform )
1657 audio.clip_start = audio_clip_count
1658 audio.clip_count = len(obj_data.files)
1659 audio_clip_count += audio.clip_count
1660 audio.max_channels = obj_data.max_channels
1661 audio.volume = obj_data.volume
1662
1663 # TODO flags:
1664 # - allow/disable doppler
1665 # - channel group tags with random colours
1666 # - transition properties
1667
1668 if obj_data.flag_loop: audio.flags |= 0x1
1669 if obj_data.flag_nodoppler: audio.flags |= 0x2
1670 if obj_data.flag_3d: audio.flags |= 0x4
1671 if obj_data.flag_auto: audio.flags |= 0x8
1672 if obj_data.formato == '0': audio.flags |= 0x000
1673 elif obj_data.formato == '1': audio.flags |= 0x400
1674 elif obj_data.formato == '2': audio.flags |= 0x1000
1675
1676 audio.channel_behaviour = int(obj_data.channel_behaviour)
1677 if audio.channel_behaviour >= 1:#{
1678 audio.group = obj_data.group
1679 #}
1680 if audio.channel_behaviour == 2:#{
1681 audio.crossfade = obj_data.transition_duration
1682 #}
1683 audio.probability_curve = int(obj_data.probability_curve)
1684
1685 for ci in range(audio.clip_count):#{
1686 entry = obj_data.files[ci]
1687 clip = ent_audio_clip()
1688 clip.probability = entry.probability
1689 if obj_data.formato == '2':#{
1690 sr_pack_file( clip._anon.file, '', vg_str_bin(entry.path) )
1691 #}
1692 else:#{
1693 clip._anon.file.path = sr_compile_string( entry.path )
1694 clip._anon.file.pack_offset = 0
1695 clip._anon.file.pack_size = 0
1696 #}
1697 sr_ent_push( clip )
1698 #}
1699 sr_ent_push( audio )
1700 #}
1701 elif ent_type == 'ent_volume':#{
1702 obj_data = obj.SR_data.ent_volume[0]
1703 volume = ent_volume()
1704 volume.type = int(obj_data.subtype)
1705 compile_obj_transform( obj, volume.transform )
1706
1707 if obj_data.target:#{
1708 volume.target = sr_entity_id( obj_data.target )
1709 volume._anon.trigger.event = obj_data.target_event
1710 volume._anon.trigger.event_leave = obj_data.target_event_leave
1711 #}
1712
1713 sr_ent_push(volume)
1714 #}
1715 elif ent_type == 'ent_marker':#{
1716 marker = ent_marker()
1717 marker.name = sr_compile_string( obj.SR_data.ent_marker[0].alias )
1718 compile_obj_transform( obj, marker.transform )
1719 sr_ent_push(marker)
1720 #}
1721 elif ent_type == 'ent_skateshop':#{
1722 skateshop = ent_skateshop()
1723 obj_data = obj.SR_data.ent_skateshop[0]
1724 skateshop.type = int(obj_data.tipo)
1725 if skateshop.type == 0:#{
1726 boardshop = skateshop._anonymous_union.boards
1727 boardshop.id_display = sr_entity_id( obj_data.mark_display )
1728 boardshop.id_info = sr_entity_id( obj_data.mark_info )
1729 boardshop.id_rack = sr_entity_id( obj_data.mark_rack )
1730 #}
1731 elif skateshop.type == 1:#{
1732 charshop = skateshop._anonymous_union.character
1733 charshop.id_display = sr_entity_id( obj_data.mark_display )
1734 charshop.id_info = sr_entity_id( obj_data.mark_info )
1735 #}
1736 elif skateshop.type == 2:#{
1737 worldshop = skateshop._anonymous_union.worlds
1738 worldshop.id_display = sr_entity_id( obj_data.mark_display )
1739 worldshop.id_info = sr_entity_id( obj_data.mark_info )
1740 #}
1741 elif skateshop.type == 3:#{
1742 server = skateshop._anonymous_union.server
1743 server.id_lever = sr_entity_id( obj_data.mark_display )
1744 #}
1745 skateshop.id_camera = sr_entity_id( obj_data.cam )
1746 compile_obj_transform( obj, skateshop.transform )
1747 sr_ent_push(skateshop)
1748 #}
1749 elif ent_type == 'ent_swspreview':#{
1750 workshop_preview = ent_swspreview()
1751 obj_data = obj.SR_data.ent_swspreview[0]
1752 workshop_preview.id_display = sr_entity_id( obj_data.mark_display )
1753 workshop_preview.id_display1 = sr_entity_id( obj_data.mark_display1)
1754 workshop_preview.id_camera = sr_entity_id( obj_data.cam )
1755 sr_ent_push( workshop_preview )
1756 #}
1757 elif ent_type == 'ent_worldinfo':#{
1758 worldinfo = ent_worldinfo()
1759 obj_data = obj.SR_data.ent_worldinfo[0]
1760 worldinfo.pstr_name = sr_compile_string( obj_data.name )
1761 worldinfo.pstr_author = sr_compile_string( obj_data.author )
1762 worldinfo.pstr_desc = sr_compile_string( obj_data.desc )
1763
1764 flags = 0x00
1765
1766 if obj_data.fix_time:#{
1767 worldinfo.timezone = obj_data.fixed_time
1768 flags |= 0x1
1769 #}
1770 else:
1771 worldinfo.timezone = obj_data.timezone
1772
1773 if obj_data.water_safe:
1774 flags |= 0x2
1775
1776 worldinfo.flags = flags
1777 worldinfo.pstr_skybox = sr_compile_string( obj_data.skybox )
1778 sr_ent_push( worldinfo )
1779 #}
1780 elif ent_type == 'ent_ccmd':#{
1781 ccmd = ent_ccmd()
1782 obj_data = obj.SR_data.ent_ccmd[0]
1783 ccmd.pstr_command = sr_compile_string( obj_data.command )
1784 sr_ent_push( ccmd )
1785 #}
1786 elif ent_type == 'ent_objective':#{
1787 objective = ent_objective()
1788 obj_data = obj.SR_data.ent_objective[0]
1789 objective.id_next = sr_entity_id( obj_data.proxima )
1790 objective.id_win = sr_entity_id( obj_data.target )
1791 objective.win_event = obj_data.target_event
1792 objective.filter = int(obj_data.filtrar)
1793 objective.filter2 = 0
1794 objective.time_limit = obj_data.time_limit
1795
1796 compile_obj_transform( obj, objective.transform )
1797 objective.submesh_start, objective.submesh_count, _ = \
1798 sr_compile_mesh_internal( obj )
1799
1800 sr_ent_push( objective )
1801 #}
1802 elif ent_type == 'ent_challenge':#{
1803 challenge = ent_challenge()
1804 obj_data = obj.SR_data.ent_challenge[0]
1805 compile_obj_transform( obj, challenge.transform )
1806 challenge.pstr_alias = sr_compile_string( obj_data.alias )
1807 challenge.target = sr_entity_id( obj_data.target )
1808 challenge.target_event = obj_data.target_event
1809 challenge.reset = sr_entity_id( obj_data.reset )
1810 challenge.reset_event = obj_data.reset_event
1811 challenge.first = sr_entity_id( obj_data.first )
1812 challenge.flags = 0x00
1813 challenge.camera = sr_entity_id( obj_data.camera )
1814 if obj_data.time_limit: challenge.flags |= 0x01
1815 challenge.status = 0
1816 sr_ent_push( challenge )
1817 #}
1818 elif ent_type == 'ent_region':#{
1819 region = ent_region()
1820 obj_data = obj.SR_data.ent_region[0]
1821 compile_obj_transform( obj, region.transform )
1822 region.submesh_start, region.submesh_count, _ = \
1823 sr_compile_mesh_internal( obj )
1824 region.pstr_title = sr_compile_string( obj_data.title )
1825 region.zone_volume = sr_entity_id( obj_data.zone_volume )
1826 region.target0[0] = sr_entity_id( obj_data.target0 )
1827 region.target0[1] = obj_data.target0_event
1828 sr_ent_push( region )
1829 #}
1830 elif ent_type == 'ent_relay':#{
1831 relay = ent_relay()
1832 obj_data = obj.SR_data.ent_relay[0]
1833 relay.targets[0][0] = sr_entity_id( obj_data.target0 )
1834 relay.targets[1][0] = sr_entity_id( obj_data.target1 )
1835 relay.targets[2][0] = sr_entity_id( obj_data.target2 )
1836 relay.targets[3][0] = sr_entity_id( obj_data.target3 )
1837 relay.targets[0][1] = obj_data.target0_event
1838 relay.targets[1][1] = obj_data.target1_event
1839 relay.targets[2][1] = obj_data.target2_event
1840 relay.targets[3][1] = obj_data.target3_event
1841 sr_ent_push( relay )
1842 #}
1843 # elif ent_type == 'ent_list':#{
1844 # lista = ent_list()
1845 # obj_data = obj.SR_data.ent_list[0]
1846
1847 # lista.entity_ref_start = entity_file_ref_count
1848 # lista.entity_ref_count = len( obj_data.entities )
1849 # entity_file_ref_count += lista.entity_ref_count
1850
1851 # for child in obj_data.entities:#{
1852 # reference_struct = file_entity_ref()
1853 # reference_struct.index = sr_entity_id( child.target )
1854 # sr_ent_push( reference_struct )
1855 # #}
1856
1857 # sr_ent_push( lista )
1858 # #}
1859 elif ent_type == 'ent_glider':#{
1860 glider = ent_glider()
1861 compile_obj_transform( obj, glider.transform )
1862 sr_ent_push( glider )
1863 #}
1864 elif ent_type == 'ent_npc':#{
1865 obj_data = obj.SR_data.ent_npc[0]
1866 npc = ent_npc()
1867 compile_obj_transform( obj, npc.transform )
1868 npc.id = obj_data.au
1869 npc.context = obj_data.context
1870 npc.camera = sr_entity_id( obj_data.cam )
1871 sr_ent_push( npc )
1872 #}
1873 elif ent_type == 'ent_cubemap':#{
1874 cubemap = ent_cubemap()
1875 co = obj.matrix_world @ Vector((0,0,0))
1876 cubemap.co[0] = co[0]
1877 cubemap.co[1] = co[2]
1878 cubemap.co[2] = -co[1]
1879 cubemap.resolution = 0
1880 cubemap.live = 60
1881 sr_ent_push( cubemap )
1882 #}
1883 elif ent_type == 'ent_miniworld':#{
1884 miniworld = ent_miniworld()
1885 obj_data = obj.SR_data.ent_miniworld[0]
1886
1887 compile_obj_transform( obj, miniworld.transform )
1888 miniworld.pstr_world = sr_compile_string( obj_data.world )
1889 miniworld.proxy = sr_entity_id( obj_data.proxy )
1890 miniworld.camera = sr_entity_id( obj_data.camera )
1891 sr_ent_push( miniworld )
1892 #}
1893 elif ent_type == 'ent_prop':#{
1894 prop = ent_prop()
1895 obj_data = obj.SR_data.ent_prop[0]
1896 compile_obj_transform( obj, prop.transform )
1897 prop.submesh_start, prop.submesh_count, _ = \
1898 sr_compile_mesh_internal( obj )
1899 prop.flags = obj_data.flags
1900 prop.pstr_alias = sr_compile_string( obj_data.alias )
1901 sr_ent_push( prop )
1902 #}
1903 #}
1904 #}
1905
1906 sr_compile_menus( collection )
1907 sr_compile_fonts( collection )
1908
1909 def _children( col ):#{
1910 yield col
1911 for c in col.children:#{
1912 yield from _children(c)
1913 #}
1914 #}
1915
1916 checkpoint_count = 0
1917 pathindice_count = 0
1918 routenode_count = 0
1919
1920 for col in _children(collection):#{
1921 print( F"Adding routes for subcollection: {col.name}" )
1922 route_gates = []
1923 route_curves = []
1924 routes = []
1925 traffics = []
1926
1927 for obj in col.objects:#{
1928 if obj.type == 'ARMATURE': pass
1929 else:#{
1930 ent_type = obj_ent_type( obj )
1931
1932 if ent_type == 'ent_gate':
1933 route_gates += [obj]
1934 elif ent_type == 'ent_route_node':#{
1935 if obj.type == 'CURVE':#{
1936 route_curves += [obj]
1937 #}
1938 #}
1939 elif ent_type == 'ent_route':
1940 routes += [obj]
1941 elif ent_type == 'ent_traffic':
1942 traffics += [obj]
1943 #}
1944 #}
1945
1946 dij = create_node_graph( route_curves, route_gates )
1947
1948 for obj in routes:#{
1949 obj_data = obj.SR_data.ent_route[0]
1950 route = ent_route()
1951 route.pstr_name = sr_compile_string( obj_data.alias )
1952 route.checkpoints_start = checkpoint_count
1953 route.checkpoints_count = 0
1954 route.id_camera = sr_entity_id( obj_data.cam )
1955
1956 for ci in range(3):
1957 route.colour[ci] = obj_data.colour[ci]
1958 route.colour[3] = 1.0
1959
1960 compile_obj_transform( obj, route.transform )
1961 checkpoints = obj_data.gates
1962
1963 for i in range(len(checkpoints)):#{
1964 gi = checkpoints[i].target
1965 gj = checkpoints[(i+1)%len(checkpoints)].target
1966 gate = gi
1967
1968 if gi:#{
1969 dest = gi.SR_data.ent_gate[0].target
1970 gi = dest
1971 #}
1972
1973 if gi==gj: continue # error?
1974 if not gi or not gj: continue
1975
1976 checkpoint = ent_checkpoint()
1977 checkpoint.gate_index = sr_compile.entity_ids[gate.name]
1978 checkpoint.path_start = pathindice_count
1979 checkpoint.path_count = 0
1980
1981 path = solve_graph( dij, gi.name, gj.name )
1982
1983 if path:#{
1984 for pi in range(len(path)):#{
1985 pathindice = ent_path_index()
1986 pathindice.index = routenode_count + path[pi]
1987 sr_ent_push( pathindice )
1988
1989 checkpoint.path_count += 1
1990 pathindice_count += 1
1991 #}
1992 #}
1993
1994 sr_ent_push( checkpoint )
1995 route.checkpoints_count += 1
1996 checkpoint_count += 1
1997 #}
1998
1999 sr_ent_push( route )
2000 #}
2001
2002 for obj in traffics:#{
2003 traffic = ent_traffic()
2004 compile_obj_transform( obj, traffic.transform )
2005 traffic.submesh_start, traffic.submesh_count, _ = \
2006 sr_compile_mesh_internal( obj )
2007
2008 # find best subsection
2009
2010 graph_keys = list(dij.graph)
2011 min_dist = 100.0
2012 best_point = 0
2013
2014 for j in range(len(dij.points)):#{
2015 point = dij.points[j]
2016 dist = (point-obj.location).magnitude
2017
2018 if dist < min_dist:#{
2019 min_dist = dist
2020 best_point = j
2021 #}
2022 #}
2023
2024 # scan to each edge
2025 best_begin = best_point
2026 best_end = best_point
2027
2028 while True:#{
2029 map0 = dij.subsections[best_begin]
2030 if map0[1] == -1: break
2031 best_begin = map0[1]
2032 #}
2033 while True:#{
2034 map1 = dij.subsections[best_end]
2035 if map1[2] == -1: break
2036 best_end = map1[2]
2037 #}
2038
2039 traffic.start_node = routenode_count + best_begin
2040 traffic.node_count = best_end - best_begin
2041 traffic.index = best_point - best_begin
2042 traffic.speed = obj.SR_data.ent_traffic[0].speed
2043 traffic.t = 0.0
2044
2045 sr_ent_push(traffic)
2046 #}
2047
2048 for point in dij.points:#{
2049 rn = ent_route_node()
2050 rn.co[0] = point[0]
2051 rn.co[1] = point[2]
2052 rn.co[2] = -point[1]
2053 sr_ent_push( rn )
2054 #}
2055
2056 routenode_count += len(dij.points)
2057 #}
2058
2059 print( F"[SR] Writing file" )
2060
2061 file_array_instructions = {}
2062 file_offset = 0
2063
2064 def _write_array( name, item_size, data ):#{
2065 nonlocal file_array_instructions, file_offset
2066
2067 count = len(data)//item_size
2068 file_array_instructions[name] = {'count':count, 'size':item_size,\
2069 'data':data, 'offset': file_offset}
2070 file_offset += len(data)
2071 file_offset = int_align_to( file_offset, 8 )
2072 #}
2073
2074 _write_array( 'strings', 1, sr_compile.string_data )
2075 _write_array( 'mdl_mesh', sizeof(mdl_mesh), sr_compile.mesh_data )
2076 _write_array( 'mdl_submesh', sizeof(mdl_submesh), sr_compile.submesh_data )
2077 _write_array( 'mdl_material', sizeof(mdl_material), sr_compile.material_data)
2078 _write_array( 'mdl_texture', sizeof(mdl_texture), sr_compile.texture_data)
2079 _write_array( 'mdl_armature', sizeof(mdl_armature), sr_compile.armature_data)
2080 _write_array( 'mdl_bone', sizeof(mdl_bone), sr_compile.bone_data )
2081
2082 for name, buffer in sr_compile.entity_data.items():#{
2083 _write_array( name, sr_compile.entity_info[name]['size'], buffer )
2084 #}
2085
2086 _write_array( 'mdl_animation', sizeof(mdl_animation), sr_compile.anim_data)
2087 _write_array( 'mdl_keyframe', sizeof(mdl_transform),sr_compile.keyframe_data)
2088 _write_array( 'mdl_vert', sizeof(mdl_vert), sr_compile.vertex_data )
2089 _write_array( 'mdl_indice', sizeof(c_uint32), sr_compile.indice_data )
2090 _write_array( 'pack', 1, sr_compile.pack_data )
2091 _write_array( 'shader_data', 1, sr_compile.shader_data )
2092
2093 header_size = int_align_to( sizeof(mdl_header), 8 )
2094 index_size = int_align_to( sizeof(mdl_array)*len(file_array_instructions),8 )
2095
2096 folder = bpy.path.abspath(bpy.context.scene.SR_data.export_dir)
2097 path = F"{folder}{collection.name}.mdl"
2098 print( path )
2099
2100 os.makedirs(os.path.dirname(path),exist_ok=True)
2101 fp = open( path, "wb" )
2102 header = mdl_header()
2103 header.version = MDL_VERSION_NR
2104 sr_array_title( header.arrays, \
2105 'index', len(file_array_instructions), \
2106 sizeof(mdl_array), header_size )
2107
2108 fp.write( bytearray_align_to( bytearray(header), 8 ) )
2109
2110 print( F'[SR] {"name":>16}| count | offset' )
2111 index = bytearray()
2112 for name,info in file_array_instructions.items():#{
2113 arr = mdl_array()
2114 offset = info['offset'] + header_size + index_size
2115 sr_array_title( arr, name, info['count'], info['size'], offset )
2116 index.extend( bytearray(arr) )
2117
2118 print( F'[SR] {name:>16}| {info["count"]: 8} '+\
2119 F' 0x{info["offset"]:02x}' )
2120 #}
2121 fp.write( bytearray_align_to( index, 8 ) )
2122 #bytearray_print_hex( index )
2123
2124 for name,info in file_array_instructions.items():#{
2125 fp.write( bytearray_align_to( info['data'], 8 ) )
2126 #}
2127
2128 fp.close()
2129
2130 print( '[SR] done' )
2131 #}
2132
2133 class SR_SCENE_SETTINGS(bpy.types.PropertyGroup):
2134 #{
2135 use_hidden: bpy.props.BoolProperty( name="use hidden", default=False )
2136 export_dir: bpy.props.StringProperty( name="Export Dir", subtype='DIR_PATH' )
2137 gizmos: bpy.props.BoolProperty( name="Draw Gizmos", default=False )
2138
2139 panel: bpy.props.EnumProperty(
2140 name='Panel',
2141 description='',
2142 items=[
2143 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
2144 ('ENTITY', 'Entity', '', 'MONKEY',1),
2145 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
2146 ],
2147 )
2148 #}
2149
2150 class SR_COLLECTION_SETTINGS(bpy.types.PropertyGroup):
2151 #{
2152 pack_textures: bpy.props.BoolProperty( name="Pack Textures", default=False )
2153 animations: bpy.props.BoolProperty( name="Export animation", default=True)
2154 #}
2155
2156 def sr_get_mirror_bone( bones ):
2157 #{
2158 side = bones.active.name[-1:]
2159 other_name = bones.active.name[:-1]
2160 if side == 'L': other_name += 'R'
2161 elif side == 'R': other_name += 'L'
2162 else: return None
2163
2164 for b in bones:#{
2165 if b.name == other_name:
2166 return b
2167 #}
2168
2169 return None
2170 #}
2171
2172 class SR_MIRROR_BONE_X(bpy.types.Operator):
2173 #{
2174 bl_idname="skaterift.mirror_bone"
2175 bl_label="Mirror bone attributes - SkateRift"
2176
2177 def execute(_,context):
2178 #{
2179 active_object = context.active_object
2180 bones = active_object.data.bones
2181 a = bones.active
2182 b = sr_get_mirror_bone( bones )
2183
2184 if not b: return {'FINISHED'}
2185
2186 b.SR_data.collider = a.SR_data.collider
2187
2188 def _v3copyflipy( a, b ):#{
2189 b[0] = a[0]
2190 b[1] = -a[1]
2191 b[2] = a[2]
2192 #}
2193
2194 _v3copyflipy( a.SR_data.collider_min, b.SR_data.collider_min )
2195 _v3copyflipy( a.SR_data.collider_max, b.SR_data.collider_max )
2196 b.SR_data.collider_min[1] = -a.SR_data.collider_max[1]
2197 b.SR_data.collider_max[1] = -a.SR_data.collider_min[1]
2198
2199 b.SR_data.cone_constraint = a.SR_data.cone_constraint
2200
2201 _v3copyflipy( a.SR_data.conevx, b.SR_data.conevy )
2202 _v3copyflipy( a.SR_data.conevy, b.SR_data.conevx )
2203 _v3copyflipy( a.SR_data.coneva, b.SR_data.coneva )
2204
2205 b.SR_data.conet = a.SR_data.conet
2206
2207 # redraw
2208 ob = bpy.context.scene.objects[0]
2209 ob.hide_render = ob.hide_render
2210 return {'FINISHED'}
2211 #}
2212 #}
2213
2214 class SR_COMPILE(bpy.types.Operator):
2215 #{
2216 bl_idname="skaterift.compile_all"
2217 bl_label="Compile All"
2218
2219 def execute(_,context):
2220 #{
2221 view_layer = bpy.context.view_layer
2222 for col in view_layer.layer_collection.children["export"].children:
2223 if not col.hide_viewport or bpy.context.scene.SR_data.use_hidden:
2224 sr_compile( bpy.data.collections[col.name] )
2225
2226 return {'FINISHED'}
2227 #}
2228 #}
2229
2230 class SR_COMPILE_THIS(bpy.types.Operator):
2231 #{
2232 bl_idname="skaterift.compile_this"
2233 bl_label="Compile This collection"
2234
2235 def execute(_,context):
2236 #{
2237 col = bpy.context.collection
2238 sr_compile( col )
2239
2240 return {'FINISHED'}
2241 #}
2242 #}
2243
2244 class SR_INTERFACE(bpy.types.Panel):
2245 #{
2246 bl_idname = "VIEW3D_PT_skate_rift"
2247 bl_label = "Skate Rift"
2248 bl_space_type = 'VIEW_3D'
2249 bl_region_type = 'UI'
2250 bl_category = "Skate Rift"
2251
2252 def draw(_, context):
2253 #{
2254 # Compiler section
2255
2256 row = _.layout.row()
2257 row.scale_y = 1.75
2258 row.prop( context.scene.SR_data, 'panel', expand=True )
2259
2260 if context.scene.SR_data.panel == 'SETTINGS': #{
2261 _.layout.prop( context.scene.SR_data, 'gizmos' )
2262 #}
2263 elif context.scene.SR_data.panel == 'EXPORT': #{
2264 _.layout.prop( context.scene.SR_data, "export_dir" )
2265 col = bpy.context.collection
2266
2267 found_in_export = False
2268 export_count = 0
2269 view_layer = bpy.context.view_layer
2270 for c1 in view_layer.layer_collection.children["export"].children: #{
2271 if not c1.hide_viewport or bpy.context.scene.SR_data.use_hidden:
2272 export_count += 1
2273
2274 if c1.name == col.name: #{
2275 found_in_export = True
2276 #}
2277 #}
2278
2279 box = _.layout.box()
2280 row = box.row()
2281 row.alignment = 'CENTER'
2282 row.scale_y = 1.5
2283
2284 if found_in_export: #{
2285 row.label( text=col.name + ".mdl" )
2286 box.prop( col.SR_data, "pack_textures" )
2287 box.prop( col.SR_data, "animations" )
2288 box.operator( "skaterift.compile_this" )
2289 #}
2290 else: #{
2291 row.enabled=False
2292 row.label( text=col.name )
2293
2294 row = box.row()
2295 row.enabled=False
2296 row.alignment = 'CENTER'
2297 row.scale_y = 1.5
2298 row.label( text="This collection is not in the export group" )
2299 #}
2300
2301 box = _.layout.box()
2302 row = box.row()
2303
2304 split = row.split( factor=0.3, align=True )
2305 split.prop( context.scene.SR_data, "use_hidden", text="hidden" )
2306
2307 row1 = split.row()
2308 if export_count == 0:
2309 row1.enabled=False
2310 row1.operator( "skaterift.compile_all", \
2311 text=F"Compile all ({export_count} collections)" )
2312 #}
2313 elif context.scene.SR_data.panel == 'ENTITY': #{
2314 active_object = context.active_object
2315 if not active_object: return
2316
2317 amount = max( 0, len(context.selected_objects)-1 )
2318
2319 row = _.layout.row()
2320 row.operator( 'skaterift.copy_entity_data', \
2321 text=F'Copy entity data to {amount} other objects' )
2322 if amount == 0: row.enabled=False
2323
2324 box = _.layout.box()
2325 row = box.row()
2326 row.alignment = 'CENTER'
2327 row.label( text=active_object.name )
2328 row.scale_y = 1.5
2329
2330 def _draw_prop_collection( source, data ): #{
2331 nonlocal box
2332 row = box.row()
2333 row.alignment = 'CENTER'
2334 row.enabled = False
2335 row.scale_y = 1.5
2336 row.label( text=F'{source}' )
2337
2338 if hasattr(type(data[0]),'sr_inspector'):#{
2339 type(data[0]).sr_inspector( box, data )
2340 #}
2341 else:#{
2342 for a in data[0].__annotations__:
2343 box.prop( data[0], a )
2344 #}
2345 #}
2346
2347 if active_object.type == 'ARMATURE': #{
2348 if active_object.mode == 'POSE': #{
2349 bones = active_object.data.bones
2350 mb = sr_get_mirror_bone( bones )
2351 if mb:#{
2352 box.operator( "skaterift.mirror_bone", \
2353 text=F'Mirror attributes to {mb.name}' )
2354 #}
2355
2356 _draw_prop_collection( \
2357 F'bpy.types.Bone["{bones.active.name}"].SR_data',\
2358 [bones.active.SR_data ] )
2359 #}
2360 else: #{
2361 row = box.row()
2362 row.alignment='CENTER'
2363 row.scale_y=2.0
2364 row.enabled=False
2365 row.label( text="Enter pose mode to modify bone properties" )
2366 #}
2367 #}
2368 elif active_object.type == 'LIGHT': #{
2369 _draw_prop_collection( \
2370 F'bpy.types.Light["{active_object.data.name}"].SR_data', \
2371 [active_object.data.SR_data] )
2372 #}
2373 elif active_object.type in ['EMPTY','CURVE','MESH']:#{
2374 box.prop( active_object.SR_data, "ent_type" )
2375 ent_type = active_object.SR_data.ent_type
2376
2377 col = getattr( active_object.SR_data, ent_type, None )
2378 if col != None and len(col)!=0:
2379 _draw_prop_collection( \
2380 F'bpy.types.Object["{active_object.name}"].SR_data.{ent_type}[0]', \
2381 col )
2382
2383 if active_object.type == 'MESH':#{
2384 col = getattr( active_object.data.SR_data, ent_type, None )
2385 if col != None and len(col)!=0:
2386 _draw_prop_collection( \
2387 F'bpy.types.Mesh["{active_object.data.name}"].SR_data.{ent_type}[0]', \
2388 col )
2389 #}
2390 #}
2391 #}
2392 #}
2393 #}
2394
2395 class SR_MATERIAL_PANEL(bpy.types.Panel):
2396 #{
2397 bl_label="Skate Rift material"
2398 bl_idname="MATERIAL_PT_sr_material"
2399 bl_space_type='PROPERTIES'
2400 bl_region_type='WINDOW'
2401 bl_context="material"
2402
2403 def draw(_,context):
2404 #{
2405 active_object = bpy.context.active_object
2406 if active_object == None: return
2407 active_mat = active_object.active_material
2408 if active_mat == None: return
2409
2410 info = material_info( active_mat )
2411
2412 if 'tex_diffuse' in info:#{
2413 _.layout.label( icon='INFO', \
2414 text=F"{info['tex_diffuse'].name} will be compiled" )
2415 #}
2416
2417 _.layout.prop( active_mat.SR_data, "shader" )
2418 _.layout.prop( active_mat.SR_data, "surface_prop" )
2419 _.layout.prop( active_mat.SR_data, "collision" )
2420
2421 if active_mat.SR_data.collision:#{
2422 box = _.layout.box()
2423 row = box.row()
2424
2425 if (active_mat.SR_data.shader != 'invisible') and \
2426 (active_mat.SR_data.shader != 'boundary') and \
2427 (active_mat.SR_data.shader != 'walking'):#{
2428 row.prop( active_mat.SR_data, "skate_surface" )
2429 row.prop( active_mat.SR_data, "grind_surface" )
2430 row.prop( active_mat.SR_data, "grow_grass" )
2431 row.prop( active_mat.SR_data, "preview_visibile" )
2432 #}
2433 #}
2434
2435 if active_mat.SR_data.shader == "terrain_blend":#{
2436 box = _.layout.box()
2437 box.prop( active_mat.SR_data, "blend_offset" )
2438 box.prop( active_mat.SR_data, "sand_colour" )
2439 #}
2440 elif active_mat.SR_data.shader == "vertex_blend":#{
2441 box = _.layout.box()
2442 box.label( icon='INFO', text="Uses vertex colours, the R channel" )
2443 box.prop( active_mat.SR_data, "blend_offset" )
2444 #}
2445 elif active_mat.SR_data.shader == "water":#{
2446 box = _.layout.box()
2447 box.label( icon='INFO', text="Depth scale of 16 meters" )
2448 box.prop( active_mat.SR_data, "shore_colour" )
2449 box.prop( active_mat.SR_data, "ocean_colour" )
2450 box.prop( active_mat.SR_data, "water_fog" )
2451 box.prop( active_mat.SR_data, "water_fresnel" )
2452 box.prop( active_mat.SR_data, "water_scale" )
2453 box.prop( active_mat.SR_data, "water_rate" )
2454 #}
2455 elif active_mat.SR_data.shader == "cubemap":#{
2456 box = _.layout.box()
2457 box.prop( active_mat.SR_data, "cubemap" )
2458 box.prop( active_mat.SR_data, "tint" )
2459 #}
2460
2461 _.layout.label( text="" )
2462 _.layout.label( text="advanced (you probably don't want to edit these)" )
2463 _.layout.prop( active_mat.SR_data, "tex_diffuse_rt" )
2464 #}
2465 #}
2466
2467 def sr_get_type_enum( scene, context ):
2468 #{
2469 items = [('none','None',"")]
2470 mesh_entities=['ent_gate','ent_water']
2471 point_entities=['ent_spawn','ent_route_node','ent_route']
2472
2473 for e in point_entities: items += [(e,e,'')]
2474
2475 if context.scene.SR_data.panel == 'ENTITY': #{
2476 if context.active_object.type == 'MESH': #{
2477 for e in mesh_entities: items += [(e,e,'')]
2478 #}
2479 #}
2480 else: #{
2481 for e in mesh_entities: items += [(e,e,'')]
2482 #}
2483
2484 return items
2485 #}
2486
2487 def sr_on_type_change( _, context ):
2488 #{
2489 obj = context.active_object
2490 ent_type = obj.SR_data.ent_type
2491 if ent_type == 'none': return
2492 if obj.type == 'MESH':#{
2493 col = getattr( obj.data.SR_data, ent_type, None )
2494 if col != None and len(col)==0: col.add()
2495 #}
2496
2497 col = getattr( obj.SR_data, ent_type, None )
2498 if col != None and len(col)==0: col.add()
2499 #}
2500
2501 class SR_OBJECT_ENT_SPAWN(bpy.types.PropertyGroup):
2502 #{
2503 alias: bpy.props.StringProperty( name='alias' )
2504 #}
2505
2506 class SR_OBJECT_ENT_GATE(bpy.types.PropertyGroup):
2507 #{
2508 target: bpy.props.PointerProperty( \
2509 type=bpy.types.Object, name="destination", \
2510 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_gate']))
2511
2512 key: bpy.props.StringProperty()
2513 tipo: bpy.props.EnumProperty(items=(('default', 'Default', ""),
2514 ('nonlocal', 'Non-Local', "")))
2515
2516 flip: bpy.props.BoolProperty( name="Flip exit", default=False )
2517 custom: bpy.props.BoolProperty( name="Mesh is surface", default=False )
2518 locked: bpy.props.BoolProperty( name="Start Locked", default=False )
2519
2520 @staticmethod
2521 def sr_inspector( layout, data ):
2522 #{
2523 box = layout.box()
2524 box.prop( data[0], 'tipo', text="subtype" )
2525
2526 if data[0].tipo == 'default': box.prop( data[0], 'target' )
2527 elif data[0].tipo == 'nonlocal': box.prop( data[0], 'key' )
2528
2529 flags = box.box()
2530 flags.prop( data[0], 'flip' )
2531 flags.prop( data[0], 'custom' )
2532 flags.prop( data[0], 'locked' )
2533 #}
2534 #}
2535
2536 class SR_MESH_ENT_GATE(bpy.types.PropertyGroup):
2537 #{
2538 dimensions: bpy.props.FloatVectorProperty(name="dimensions",size=3)
2539 #}
2540
2541 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy.types.PropertyGroup):
2542 #{
2543 target: bpy.props.PointerProperty( \
2544 type=bpy.types.Object, name='target', \
2545 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_gate']))
2546 #}
2547
2548 class SR_OBJECT_ENT_MINIWORLD(bpy.types.PropertyGroup):
2549 #{
2550 world: bpy.props.StringProperty( name='world UID' )
2551 proxy: bpy.props.PointerProperty( \
2552 type=bpy.types.Object, name='proxy', \
2553 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_prop']))
2554 camera: bpy.props.PointerProperty( \
2555 type=bpy.types.Object, name="Camera", \
2556 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
2557 #}
2558
2559 class SR_UL_ROUTE_NODE_LIST(bpy.types.UIList):
2560 #{
2561 bl_idname = 'SR_UL_ROUTE_NODE_LIST'
2562
2563 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2564 #{
2565 layout.prop( item, 'target', text='', emboss=False )
2566 #}
2567 #}
2568
2569 def internal_listdel_execute(self,context,ent_name,collection_name):
2570 #{
2571 active_object = context.active_object
2572 data = getattr(active_object.SR_data,ent_name)[0]
2573 lista = getattr(data,collection_name)
2574 index = getattr(data,F'{collection_name}_index')
2575
2576 lista.remove(index)
2577
2578 setattr(data,F'{collection_name}_index', min(max(0,index-1), len(lista)-1))
2579 return{'FINISHED'}
2580 #}
2581
2582 def internal_listadd_execute(self,context,ent_name,collection_name):
2583 #{
2584 active_object = context.active_object
2585 getattr(getattr(active_object.SR_data,ent_name)[0],collection_name).add()
2586 return{'FINISHED'}
2587 #}
2588
2589 def copy_propgroup( de, to ):
2590 #{
2591 for a in de.__annotations__:#{
2592 if isinstance(getattr(de,a), bpy.types.bpy_prop_collection):#{
2593 ca = getattr(de,a)
2594 cb = getattr(to,a)
2595
2596 while len(cb) != len(ca):#{
2597 if len(cb) < len(ca): cb.add()
2598 else: cb.remove(0)
2599 #}
2600 for i in range(len(ca)):#{
2601 copy_propgroup(ca[i],cb[i])
2602 #}
2603 #}
2604 else:#{
2605 setattr(to,a,getattr(de,a))
2606 #}
2607 #}
2608 #}
2609
2610 class SR_OT_COPY_ENTITY_DATA(bpy.types.Operator):
2611 #{
2612 bl_idname = "skaterift.copy_entity_data"
2613 bl_label = "Copy entity data"
2614
2615 def execute(self, context):#{
2616 data = context.active_object.SR_data
2617 new_type = data.ent_type
2618 print( F"Copy entity data from: {context.active_object.name}" )
2619
2620 for obj in context.selected_objects:#{
2621 if obj != context.active_object:#{
2622 print( F" To: {obj.name}" )
2623
2624 obj.SR_data.ent_type = new_type
2625
2626 if active_object.type == 'MESH':#{
2627 col = getattr( obj.data.SR_data, new_type, None )
2628 if col != None and len(col)==0: col.add()
2629 mdata = context.active_object.data.SR_data
2630 copy_propgroup( getattr(mdata,new_type)[0], col[0] )
2631 #}
2632
2633 col = getattr( obj.SR_data, new_type, None )
2634 if col != None and len(col)==0: col.add()
2635 copy_propgroup( getattr(data,new_type)[0], col[0] )
2636 #}
2637 #}
2638 return{'FINISHED'}
2639 #}
2640 #}
2641
2642 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy.types.Operator):
2643 #{
2644 bl_idname = "skaterift.new_entry"
2645 bl_label = "Add gate"
2646
2647 def execute(self, context):#{
2648 return internal_listadd_execute(self,context,'ent_route','gates')
2649 #}
2650 #}
2651
2652 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy.types.Operator):
2653 #{
2654 bl_idname = "skaterift.del_entry"
2655 bl_label = "Remove gate"
2656
2657 @classmethod
2658 def poll(cls, context):#{
2659 active_object = context.active_object
2660 if obj_ent_type(active_object) == 'ent_route':#{
2661 return active_object.SR_data.ent_route[0].gates
2662 #}
2663 else: return False
2664 #}
2665
2666 def execute(self, context):#{
2667 return internal_listdel_execute(self,context,'ent_route','gates')
2668 #}
2669 #}
2670
2671 class SR_OT_AUDIO_LIST_NEW_ITEM(bpy.types.Operator):
2672 #{
2673 bl_idname = "skaterift.al_new_entry"
2674 bl_label = "Add file"
2675
2676 def execute(self, context):#{
2677 return internal_listadd_execute(self,context,'ent_audio','files')
2678 #}
2679 #}
2680
2681 class SR_OT_AUDIO_LIST_DEL_ITEM(bpy.types.Operator):
2682 #{
2683 bl_idname = "skaterift.al_del_entry"
2684 bl_label = "Remove file"
2685
2686 @classmethod
2687 def poll(cls, context):#{
2688 active_object = context.active_object
2689 if obj_ent_type(active_object) == 'ent_audio':#{
2690 return active_object.SR_data.ent_audio[0].files
2691 #}
2692 else: return False
2693 #}
2694
2695 def execute(self, context):#{
2696 return internal_listdel_execute(self,context,'ent_audio','files')
2697 return{'FINISHED'}
2698 #}
2699 #}
2700
2701 class SR_OT_GLYPH_LIST_NEW_ITEM(bpy.types.Operator):
2702 #{
2703 bl_idname = "skaterift.gl_new_entry"
2704 bl_label = "Add glyph"
2705
2706 def execute(self, context):#{
2707 active_object = context.active_object
2708
2709 font = active_object.SR_data.ent_font[0]
2710 font.glyphs.add()
2711
2712 if len(font.glyphs) > 1:#{
2713 prev = font.glyphs[-2]
2714 cur = font.glyphs[-1]
2715
2716 cur.bounds = prev.bounds
2717 cur.utf32 = prev.utf32+1
2718 #}
2719
2720 return{'FINISHED'}
2721 #}
2722 #}
2723
2724 class SR_OT_GLYPH_LIST_DEL_ITEM(bpy.types.Operator):
2725 #{
2726 bl_idname = "skaterift.gl_del_entry"
2727 bl_label = "Remove Glyph"
2728
2729 @classmethod
2730 def poll(cls, context):#{
2731 active_object = context.active_object
2732 if obj_ent_type(active_object) == 'ent_font':#{
2733 return active_object.SR_data.ent_font[0].glyphs
2734 #}
2735 else: return False
2736 #}
2737
2738 def execute(self, context):#{
2739 return internal_listdel_execute(self,context,'ent_font','glyphs')
2740 #}
2741 #}
2742
2743 class SR_OT_GLYPH_LIST_MOVE_ITEM(bpy.types.Operator):
2744 #{
2745 bl_idname = "skaterift.gl_move_item"
2746 bl_label = "aa"
2747 direction: bpy.props.EnumProperty(items=(('UP', 'Up', ""),
2748 ('DOWN', 'Down', ""),))
2749
2750 @classmethod
2751 def poll(cls, context):#{
2752 active_object = context.active_object
2753 if obj_ent_type(active_object) == 'ent_font':#{
2754 return active_object.SR_data.ent_font[0].glyphs
2755 #}
2756 else: return False
2757 #}
2758
2759 def execute(_, context):#{
2760 active_object = context.active_object
2761 data = active_object.SR_data.ent_font[0]
2762
2763 index = data.glyphs_index
2764 neighbor = index + (-1 if _.direction == 'UP' else 1)
2765 data.glyphs.move( neighbor, index )
2766
2767 list_length = len(data.glyphs) - 1
2768 new_index = index + (-1 if _.direction == 'UP' else 1)
2769
2770 data.glyphs_index = max(0, min(new_index, list_length))
2771
2772 return{'FINISHED'}
2773 #}
2774 #}
2775
2776 class SR_OT_FONT_VARIANT_LIST_NEW_ITEM(bpy.types.Operator):
2777 #{
2778 bl_idname = "skaterift.fv_new_entry"
2779 bl_label = "Add variant"
2780
2781 def execute(self, context):#{
2782 return internal_listadd_execute(self,context,'ent_font','variants')
2783 #}
2784 #}
2785
2786 class SR_OT_FONT_VARIANT_LIST_DEL_ITEM(bpy.types.Operator):
2787 #{
2788 bl_idname = "skaterift.fv_del_entry"
2789 bl_label = "Remove variant"
2790
2791 @classmethod
2792 def poll(cls, context):#{
2793 active_object = context.active_object
2794 if obj_ent_type(active_object) == 'ent_font':#{
2795 return active_object.SR_data.ent_font[0].variants
2796 #}
2797 else: return False
2798 #}
2799
2800 def execute(self, context):#{
2801 return internal_listdel_execute(self,context,'ent_font','variants')
2802 #}
2803 #}
2804
2805 class SR_OBJECT_ENT_AUDIO_FILE_ENTRY(bpy.types.PropertyGroup):
2806 #{
2807 path: bpy.props.StringProperty( name="Path" )
2808 probability: bpy.props.FloatProperty( name="Probability",default=100.0 )
2809 #}
2810
2811 class SR_UL_AUDIO_LIST(bpy.types.UIList):
2812 #{
2813 bl_idname = 'SR_UL_AUDIO_LIST'
2814
2815 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2816 #{
2817 split = layout.split(factor=0.7)
2818 c = split.column()
2819 c.prop( item, 'path', text='', emboss=False )
2820 c = split.column()
2821 c.prop( item, 'probability', text='%', emboss=True )
2822 #}
2823 #}
2824
2825 class SR_UL_FONT_VARIANT_LIST(bpy.types.UIList):
2826 #{
2827 bl_idname = 'SR_UL_FONT_VARIANT_LIST'
2828
2829 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2830 #{
2831 layout.prop( item, 'mesh', emboss=False )
2832 layout.prop( item, 'tipo' )
2833 #}
2834 #}
2835
2836 class SR_UL_FONT_GLYPH_LIST(bpy.types.UIList):
2837 #{
2838 bl_idname = 'SR_UL_FONT_GLYPH_LIST'
2839
2840 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2841 #{
2842 s0 = layout.split(factor=0.3)
2843 c = s0.column()
2844 s1 = c.split(factor=0.3)
2845 c = s1.column()
2846 row = c.row()
2847 lbl = chr(item.utf32) if item.utf32 >= 32 and item.utf32 <= 126 else \
2848 f'x{item.utf32:x}'
2849 row.label(text=lbl)
2850 c = s1.column()
2851 c.prop( item, 'utf32', text='', emboss=True )
2852 c = s0.column()
2853 row = c.row()
2854 row.prop( item, 'bounds', text='', emboss=False )
2855 #}
2856 #}
2857
2858 class SR_OBJECT_ENT_ROUTE(bpy.types.PropertyGroup):
2859 #{
2860 gates: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY)
2861 gates_index: bpy.props.IntProperty()
2862
2863 colour: bpy.props.FloatVectorProperty( \
2864 name="Colour",\
2865 subtype='COLOR',\
2866 min=0.0,max=1.0,\
2867 default=Vector((0.79,0.63,0.48)),\
2868 description="Route colour"\
2869 )
2870
2871 alias: bpy.props.StringProperty(\
2872 name="Alias",\
2873 default="Untitled Course")
2874
2875 cam: bpy.props.PointerProperty( \
2876 type=bpy.types.Object, name="Viewpoint", \
2877 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
2878
2879 @staticmethod
2880 def sr_inspector( layout, data ):
2881 #{
2882 layout.prop( data[0], 'alias' )
2883 layout.prop( data[0], 'colour' )
2884 layout.prop( data[0], 'cam' )
2885
2886 layout.label( text='Checkpoints' )
2887 layout.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
2888 data[0], 'gates', data[0], 'gates_index', rows=5)
2889
2890 row = layout.row()
2891 row.operator( 'skaterift.new_entry', text='Add' )
2892 row.operator( 'skaterift.del_entry', text='Remove' )
2893 #}
2894 #}
2895
2896
2897 class SR_OT_ENT_LIST_NEW_ITEM(bpy.types.Operator):#{
2898 bl_idname = "skaterift.ent_list_new_entry"
2899 bl_label = "Add entity"
2900
2901 def execute(self, context):#{
2902 return internal_listadd_execute(self,context,'ent_list','entities')
2903 #}
2904 #}
2905
2906 class SR_OT_ENT_LIST_DEL_ITEM(bpy.types.Operator):#{
2907 bl_idname = "skaterift.ent_list_del_entry"
2908 bl_label = "Remove entity"
2909
2910 @classmethod
2911 def poll(cls, context):#{
2912 active_object = context.active_object
2913 if obj_ent_type(active_object) == 'ent_list':#{
2914 return active_object.SR_data.ent_list[0].entities
2915 #}
2916 else: return False
2917 #}
2918
2919 def execute(self, context):#{
2920 return internal_listdel_execute(self,context,'ent_list','entities')
2921 #}
2922 #}
2923
2924 class SR_OBJECT_ENT_LIST_ENTRY(bpy.types.PropertyGroup):
2925 #{
2926 target: bpy.props.PointerProperty( \
2927 type=bpy.types.Object, name='target' )
2928 #}
2929
2930 class SR_UL_ENT_LIST(bpy.types.UIList):#{
2931 bl_idname = 'SR_UL_ENT_LIST'
2932
2933 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):#{
2934 layout.prop( item, 'target', text='', emboss=False )
2935 #}
2936 #}
2937
2938 class SR_OBJECT_ENT_LIST(bpy.types.PropertyGroup):#{
2939 entities: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_LIST_ENTRY)
2940 entities_index: bpy.props.IntProperty()
2941
2942 @staticmethod
2943 def sr_inspector( layout, data ):#{
2944 layout.label( text='Entities' )
2945 layout.template_list('SR_UL_ENT_LIST', 'Entities', \
2946 data[0], 'entities', data[0], \
2947 'entities_index', rows=5)
2948
2949 row = layout.row()
2950 row.operator( 'skaterift.ent_list_new_entry', text='Add' )
2951 row.operator( 'skaterift.ent_list_del_entry', text='Remove' )
2952 #}
2953 #}
2954
2955 class SR_OBJECT_ENT_GLIDER(bpy.types.PropertyGroup):#{
2956 nothing: bpy.props.StringProperty()
2957 #}
2958
2959 class SR_OBJECT_ENT_NPC(bpy.types.PropertyGroup):#{
2960 au: bpy.props.IntProperty()
2961 context: bpy.props.IntProperty()
2962 cam: bpy.props.PointerProperty( \
2963 type=bpy.types.Object, name="Viewpoint", \
2964 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
2965 #}
2966
2967 class SR_OBJECT_ENT_VOLUME(bpy.types.PropertyGroup):#{
2968 subtype: bpy.props.EnumProperty(
2969 name="Subtype",
2970 items=[('0','Trigger',''),
2971 ('1','Particles (0.1s)','')]
2972 )
2973
2974 target: bpy.props.PointerProperty( \
2975 type=bpy.types.Object, name="Target", \
2976 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
2977 target_event: bpy.props.IntProperty( name="Enter Ev" )
2978 target_event_leave: bpy.props.IntProperty( name="Leave Ev", default=-1 )
2979
2980 @staticmethod
2981 def inspect_target( layout, data, propname, evs = ['_event'] ):#{
2982 box = layout.box()
2983 box.prop( data[0], propname )
2984
2985 for evname in evs:#{
2986 row = box.row()
2987 row.prop( data[0], propname + evname )
2988
2989 target = getattr( data[0], propname )
2990 if target:#{
2991 tipo = target.SR_data.ent_type
2992 cls = globals()[ tipo ]
2993
2994 table = getattr( cls, 'sr_functions', None )
2995 if table:#{
2996 index = getattr( data[0], propname + evname )
2997 if index in table:
2998 row.label( text=table[index] )
2999 else:
3000 row.label( text="undefined function" )
3001 #}
3002 #}
3003 else:#{
3004 row.label( text="..." )
3005 row.enabled=False
3006 #}
3007 #}
3008 #}
3009
3010 @staticmethod
3011 def sr_inspector( layout, data ):#{
3012 layout.prop( data[0], 'subtype' )
3013 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target', \
3014 ['_event','_event_leave'] )
3015 #}
3016 #}
3017
3018 class SR_OBJECT_ENT_AUDIO(bpy.types.PropertyGroup):
3019 #{
3020 files: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_AUDIO_FILE_ENTRY)
3021 files_index: bpy.props.IntProperty()
3022
3023 flag_3d: bpy.props.BoolProperty( name="3D audio",default=True )
3024 flag_loop: bpy.props.BoolProperty( name="Loop",default=False )
3025 flag_auto: bpy.props.BoolProperty( name="Play at start",default=False )
3026 flag_nodoppler: bpy.props.BoolProperty( name="No Doppler",default=False )
3027
3028 group: bpy.props.IntProperty( name="Group ID", default=0 )
3029 formato: bpy.props.EnumProperty(
3030 name="Format",
3031 items=[('0','Uncompressed Mono',''),
3032 ('1','Compressed Vorbis',''),
3033 ('2','[vg] Bird Synthesis','')]
3034 )
3035 probability_curve: bpy.props.EnumProperty(
3036 name="Probability Curve",
3037 items=[('0','Constant',''),
3038 ('1','Wildlife Daytime',''),
3039 ('2','Wildlife Nighttime','')])
3040 channel_behaviour: bpy.props.EnumProperty(
3041 name="Channel Behaviour",
3042 items=[('0','Unlimited',''),
3043 ('1','Discard if group full', ''),
3044 ('2','Crossfade if group full','')])
3045
3046 transition_duration: bpy.props.FloatProperty(name="Transition Time",\
3047 default=0.2)
3048
3049 max_channels: bpy.props.IntProperty( name="Max Channels", default=1 )
3050 volume: bpy.props.FloatProperty( name="Volume",default=1.0 )
3051
3052 @staticmethod
3053 def sr_inspector( layout, data ):
3054 #{
3055 layout.prop( data[0], 'formato' )
3056 layout.prop( data[0], 'volume' )
3057
3058 box = layout.box()
3059 box.label( text='Channels' )
3060 split = box.split(factor=0.3)
3061 c = split.column()
3062 c.prop( data[0], 'max_channels' )
3063 c = split.column()
3064 c.prop( data[0], 'channel_behaviour', text='Behaviour' )
3065 if data[0].channel_behaviour >= '1':
3066 box.prop( data[0], 'group' )
3067 if data[0].channel_behaviour == '2':
3068 box.prop( data[0], 'transition_duration' )
3069
3070 box = layout.box()
3071 box.label( text='Flags' )
3072 box.prop( data[0], 'flag_3d' )
3073 if data[0].flag_3d: box.prop( data[0], 'flag_nodoppler' )
3074
3075 box.prop( data[0], 'flag_loop' )
3076 box.prop( data[0], 'flag_auto' )
3077
3078 layout.prop( data[0], 'probability_curve' )
3079
3080 split = layout.split(factor=0.7)
3081 c = split.column()
3082 c.label( text='Filepath' )
3083 c = split.column()
3084 c.label( text='Chance' )
3085 layout.template_list('SR_UL_AUDIO_LIST', 'Files', \
3086 data[0], 'files', data[0], 'files_index', rows=5)
3087
3088 row = layout.row()
3089 row.operator( 'skaterift.al_new_entry', text='Add' )
3090 row.operator( 'skaterift.al_del_entry', text='Remove' )
3091 #}
3092 #}
3093
3094 class SR_OBJECT_ENT_MARKER(bpy.types.PropertyGroup):
3095 #{
3096 alias: bpy.props.StringProperty()
3097 flags: bpy.props.IntProperty()
3098 #}
3099
3100 class SR_OBJECT_ENT_GLYPH(bpy.types.PropertyGroup):
3101 #{
3102 mini: bpy.props.FloatVectorProperty(size=2)
3103 maxi: bpy.props.FloatVectorProperty(size=2)
3104 utf32: bpy.props.IntProperty()
3105 #}
3106
3107 class SR_OBJECT_ENT_GLYPH_ENTRY(bpy.types.PropertyGroup):
3108 #{
3109 bounds: bpy.props.FloatVectorProperty(size=4,subtype='NONE')
3110 utf32: bpy.props.IntProperty()
3111 #}
3112
3113 class SR_OBJECT_ENT_FONT_VARIANT(bpy.types.PropertyGroup):
3114 #{
3115 mesh: bpy.props.PointerProperty(type=bpy.types.Object)
3116 tipo: bpy.props.StringProperty()
3117 #}
3118
3119 class SR_OBJECT_ENT_FONT(bpy.types.PropertyGroup):
3120 #{
3121 variants: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_FONT_VARIANT)
3122 glyphs: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GLYPH_ENTRY)
3123 alias: bpy.props.StringProperty()
3124
3125 glyphs_index: bpy.props.IntProperty()
3126 variants_index: bpy.props.IntProperty()
3127
3128 @staticmethod
3129 def sr_inspector( layout, data ):
3130 #{
3131 layout.prop( data[0], 'alias' )
3132
3133 layout.label( text='Variants' )
3134 layout.template_list('SR_UL_FONT_VARIANT_LIST', 'Variants', \
3135 data[0], 'variants', data[0], 'variants_index',\
3136 rows=5 )
3137 row = layout.row()
3138 row.operator( 'skaterift.fv_new_entry', text='Add' )
3139 row.operator( 'skaterift.fv_del_entry', text='Remove' )
3140
3141 layout.label( text='ASCII Glyphs' )
3142 layout.template_list('SR_UL_FONT_GLYPH_LIST', 'Glyphs', \
3143 data[0], 'glyphs', data[0], 'glyphs_index', rows=5)
3144
3145 row = layout.row()
3146 row.operator( 'skaterift.gl_new_entry', text='Add' )
3147 row.operator( 'skaterift.gl_del_entry', text='Remove' )
3148 row.operator( 'skaterift.gl_move_item', text='^' ).direction='UP'
3149 row.operator( 'skaterift.gl_move_item', text='v' ).direction='DOWN'
3150 #}
3151 #}
3152
3153 class SR_OBJECT_ENT_TRAFFIC(bpy.types.PropertyGroup):
3154 #{
3155 speed: bpy.props.FloatProperty(default=1.0)
3156 #}
3157
3158 class SR_OBJECT_ENT_SKATESHOP(bpy.types.PropertyGroup):
3159 #{
3160 tipo: bpy.props.EnumProperty( name='Type',
3161 items=[('0','boards',''),
3162 ('1','character',''),
3163 ('2','world',''),
3164 ('4','server','')] )
3165 mark_rack: bpy.props.PointerProperty( \
3166 type=bpy.types.Object, name="Board Rack", \
3167 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3168 mark_display: bpy.props.PointerProperty( \
3169 type=bpy.types.Object, name="Selected Board Display", \
3170 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3171 mark_info: bpy.props.PointerProperty( \
3172 type=bpy.types.Object, name="Selected Board Info", \
3173 poll=lambda self,obj: sr_filter_ent_type(obj,\
3174 ['ent_marker','ent_prop']))
3175 cam: bpy.props.PointerProperty( \
3176 type=bpy.types.Object, name="Viewpoint", \
3177 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3178 #}
3179
3180 class SR_OBJECT_ENT_WORKSHOP_PREVIEW(bpy.types.PropertyGroup):
3181 #{
3182 mark_display: bpy.props.PointerProperty( \
3183 type=bpy.types.Object, name="Board Display", \
3184 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3185 mark_display1: bpy.props.PointerProperty( \
3186 type=bpy.types.Object, name="Board Display (other side)", \
3187 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3188 cam: bpy.props.PointerProperty( \
3189 type=bpy.types.Object, name="Viewpoint", \
3190 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3191 #}
3192
3193 class SR_OBJECT_ENT_MENU_ITEM(bpy.types.PropertyGroup):
3194 #{
3195 link0: bpy.props.PointerProperty( \
3196 type=bpy.types.Object, name="Link 0", \
3197 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3198 link1: bpy.props.PointerProperty( \
3199 type=bpy.types.Object, name="Link 1", \
3200 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3201 link2: bpy.props.PointerProperty( \
3202 type=bpy.types.Object, name="Link 2", \
3203 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3204 link3: bpy.props.PointerProperty( \
3205 type=bpy.types.Object, name="Link 3", \
3206 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3207
3208 newloc: bpy.props.PointerProperty( \
3209 type=bpy.types.Object, name="New location", \
3210 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3211 stack_behaviour: bpy.props.EnumProperty( name='Stack Behaviour',
3212 items=[('0','append',''),
3213 ('1','replace','')])
3214
3215 camera: bpy.props.PointerProperty( \
3216 type=bpy.types.Object, name="Camera", \
3217 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3218
3219 slider_minloc: bpy.props.PointerProperty( \
3220 type=bpy.types.Object, name="Slider min", \
3221 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3222 slider_maxloc: bpy.props.PointerProperty( \
3223 type=bpy.types.Object, name="Slider max", \
3224 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3225 slider_handle: bpy.props.PointerProperty( \
3226 type=bpy.types.Object, name="Slider handle", \
3227 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3228
3229 checkmark: bpy.props.PointerProperty( \
3230 type=bpy.types.Object, name="Checked", \
3231 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3232
3233 font_variant: bpy.props.IntProperty( name="Font Variant" )
3234
3235 string: bpy.props.StringProperty( name="String" )
3236 tipo: bpy.props.EnumProperty( name='Type',
3237 items=[('0','visual',''),
3238 ('1','event button',''),
3239 ('2','page button',''),
3240 ('3','toggle', ''),
3241 ('4','slider',''),
3242 ('5','page',''),
3243 ('6','binding',''),
3244 ('7','visual(no colourize)','')])
3245
3246 @staticmethod
3247 def sr_inspector( layout, data ):
3248 #{
3249 data = data[0]
3250 box = layout.box()
3251 box.prop( data, 'tipo' )
3252
3253 if data.tipo == '0' or data.tipo == '7':#{
3254 box.prop( data, 'string', text='Name' )
3255 return
3256 #}
3257 elif data.tipo == '1':#{
3258 box.prop( data, 'string', text='Event' )
3259 #}
3260 elif data.tipo == '2':#{
3261 box.prop( data, 'string', text='Page' )
3262 box.prop( data, 'stack_behaviour' )
3263 #}
3264 elif data.tipo == '3':#{
3265 box.prop( data, 'string', text='Data (i32)' )
3266 box.prop( data, 'checkmark' )
3267 #}
3268 elif data.tipo == '4':#{
3269 box.prop( data, 'string', text='Data (f32)' )
3270 box.prop( data, 'slider_minloc' )
3271 box.prop( data, 'slider_maxloc' )
3272 box.prop( data, 'slider_handle' )
3273 box = box.box()
3274 box.label( text="Links" )
3275 box.prop( data, 'link0', text='v0' )
3276 box.prop( data, 'link1', text='v1' )
3277 return
3278 #}
3279 elif data.tipo == '5':#{
3280 box.prop( data, 'string', text='Page Name' )
3281 box.prop( data, 'newloc', text='Entry Point' )
3282 box.prop( data, 'camera', text='Viewpoint' )
3283 return
3284 #}
3285 elif data.tipo == '6':#{
3286 box.prop( data, 'string', text='ID' )
3287 box.prop( data, 'font_variant' )
3288 return
3289 #}
3290
3291 box = box.box()
3292 box.label( text="Links" )
3293 box.prop( data, 'link0' )
3294 box.prop( data, 'link1' )
3295 box.prop( data, 'link2' )
3296 box.prop( data, 'link3' )
3297 #}
3298 #}
3299
3300 class SR_OBJECT_ENT_WORLD_INFO(bpy.types.PropertyGroup):
3301 #{
3302 name: bpy.props.StringProperty(name="Name")
3303 desc: bpy.props.StringProperty(name="Description")
3304 author: bpy.props.StringProperty(name="Author")
3305 skybox: bpy.props.StringProperty(name="Skybox")
3306
3307 fix_time: bpy.props.BoolProperty(name="Fix Time")
3308 timezone: bpy.props.FloatProperty(name="Timezone(hrs) (UTC0 +hrs)")
3309 fixed_time: bpy.props.FloatProperty(name="Fixed Time (0-1)")
3310
3311 water_safe: bpy.props.BoolProperty(name="Water is Safe")
3312
3313 @staticmethod
3314 def sr_inspector( layout, data ):
3315 #{
3316 layout.prop( data[0], 'name' )
3317 layout.prop( data[0], 'desc' )
3318 layout.prop( data[0], 'author' )
3319
3320 layout.prop( data[0], 'fix_time' )
3321 if data[0].fix_time:
3322 layout.prop( data[0], 'fixed_time' )
3323 else:
3324 layout.prop( data[0], 'timezone' )
3325
3326 layout.prop( data[0], 'water_safe' )
3327 #}
3328 #}
3329
3330 class SR_OBJECT_ENT_CCMD(bpy.types.PropertyGroup):
3331 #{
3332 command: bpy.props.StringProperty(name="Command Line")
3333 #}
3334
3335 class SR_OBJECT_ENT_OBJECTIVE(bpy.types.PropertyGroup):#{
3336 proxima: bpy.props.PointerProperty( \
3337 type=bpy.types.Object, name="Next", \
3338 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_objective']))
3339 target: bpy.props.PointerProperty( \
3340 type=bpy.types.Object, name="Win", \
3341 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3342 target_event: bpy.props.IntProperty( name="Event/Method" )
3343 time_limit: bpy.props.FloatProperty( name="Time Limit", default=1.0 )
3344 filtrar: bpy.props.EnumProperty( name='Filter',\
3345 items=[('0','none',''),
3346 (str(0x1),'trick_shuvit',''),
3347 (str(0x2),'trick_kickflip',''),
3348 (str(0x4),'trick_treflip',''),
3349 (str(0x1|0x2|0x4),'trick_any',''),
3350 (str(0x8),'flip_back',''),
3351 (str(0x10),'flip_front',''),
3352 (str(0x8|0x10),'flip_any',''),
3353 (str(0x20),'grind_truck_any',''),
3354 (str(0x40),'grind_board_any',''),
3355 (str(0x20|0x40),'grind_any',''),
3356 (str(0x80),'footplant',''),
3357 (str(0x100),'passthrough',''),
3358 ])
3359
3360 @staticmethod
3361 def sr_inspector( layout, data ):#{
3362 layout.prop( data[0], 'proxima' )
3363 layout.prop( data[0], 'time_limit' )
3364 layout.prop( data[0], 'filtrar' )
3365 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target' )
3366 #}
3367 #}
3368
3369 class SR_OBJECT_ENT_CHALLENGE(bpy.types.PropertyGroup):#{
3370 alias: bpy.props.StringProperty( name="Alias" )
3371
3372 target: bpy.props.PointerProperty( \
3373 type=bpy.types.Object, name="On Complete", \
3374 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3375 target_event: bpy.props.IntProperty( name="Event/Method" )
3376 reset: bpy.props.PointerProperty( \
3377 type=bpy.types.Object, name="On Reset", \
3378 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3379 reset_event: bpy.props.IntProperty( name="Event/Method" )
3380
3381 time_limit: bpy.props.BoolProperty( name="Time Limit" )
3382
3383 first: bpy.props.PointerProperty( \
3384 type=bpy.types.Object, name="First Objective", \
3385 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_objective']))
3386
3387 camera: bpy.props.PointerProperty( \
3388 type=bpy.types.Object, name="Camera", \
3389 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3390
3391
3392 @staticmethod
3393 def sr_inspector( layout, data ):#{
3394 layout.prop( data[0], 'alias' )
3395 layout.prop( data[0], 'camera' )
3396 layout.prop( data[0], 'first' )
3397 layout.prop( data[0], 'time_limit' )
3398 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target' )
3399 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'reset' )
3400 #}
3401 #}
3402
3403 class SR_OBJECT_ENT_REGION(bpy.types.PropertyGroup):#{
3404 title: bpy.props.StringProperty( name="Title" )
3405 zone_volume: bpy.props.PointerProperty(
3406 type=bpy.types.Object, name="Zone Volume", \
3407 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_volume']))
3408
3409 target0: bpy.props.PointerProperty( \
3410 type=bpy.types.Object, name="Triger on unlock", \
3411 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3412 target0_event: bpy.props.IntProperty( name="Event/Method" )
3413
3414 @staticmethod
3415 def sr_inspector( layout, data ):#{
3416 layout.prop( data[0], 'title' )
3417 layout.prop( data[0], 'zone_volume' )
3418 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target0' )
3419 #}
3420 #}
3421
3422 class SR_OBJECT_ENT_RELAY(bpy.types.PropertyGroup):#{
3423 target0: bpy.props.PointerProperty( \
3424 type=bpy.types.Object, name="Target 0", \
3425 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3426 target1: bpy.props.PointerProperty( \
3427 type=bpy.types.Object, name="Target 1", \
3428 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3429 target2: bpy.props.PointerProperty( \
3430 type=bpy.types.Object, name="Target 2", \
3431 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3432 target3: bpy.props.PointerProperty( \
3433 type=bpy.types.Object, name="Target 3", \
3434 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3435
3436 target0_event: bpy.props.IntProperty( name="Event" )
3437 target1_event: bpy.props.IntProperty( name="Event" )
3438 target2_event: bpy.props.IntProperty( name="Event" )
3439 target3_event: bpy.props.IntProperty( name="Event" )
3440
3441 @staticmethod
3442 def sr_inspector( layout, data ):#{
3443 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target0' )
3444 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target1' )
3445 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target2' )
3446 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target3' )
3447 #}
3448 #}
3449
3450 class SR_OBJECT_PROPERTIES(bpy.types.PropertyGroup):
3451 #{
3452 ent_gate: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GATE)
3453 ent_spawn: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_SPAWN)
3454 ent_route: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_ROUTE)
3455 ent_volume: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_VOLUME)
3456 ent_audio: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_AUDIO)
3457 ent_marker: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MARKER)
3458 ent_prop: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MARKER)
3459 ent_glyph: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GLYPH)
3460 ent_font: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_FONT)
3461 ent_traffic: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_TRAFFIC)
3462 ent_skateshop: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_SKATESHOP)
3463 ent_swspreview: \
3464 bpy.props.CollectionProperty(type=SR_OBJECT_ENT_WORKSHOP_PREVIEW)
3465 ent_menuitem: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MENU_ITEM)
3466 ent_worldinfo: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_WORLD_INFO)
3467 ent_ccmd: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_CCMD)
3468 ent_objective: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_OBJECTIVE)
3469 ent_challenge: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_CHALLENGE)
3470 ent_region: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_REGION)
3471 ent_relay: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_RELAY)
3472 ent_miniworld: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MINIWORLD)
3473 ent_list: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_LIST)
3474 ent_glider: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GLIDER)
3475 ent_npc: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_NPC)
3476
3477 ent_type: bpy.props.EnumProperty(
3478 name="Type",
3479 items=sr_entity_list,
3480 update=sr_on_type_change
3481 )
3482 #}
3483
3484 class SR_MESH_PROPERTIES(bpy.types.PropertyGroup):
3485 #{
3486 ent_gate: bpy.props.CollectionProperty(type=SR_MESH_ENT_GATE)
3487 #}
3488
3489 class SR_LIGHT_PROPERTIES(bpy.types.PropertyGroup):
3490 #{
3491 daytime: bpy.props.BoolProperty( name='Daytime' )
3492 #}
3493
3494 class SR_BONE_PROPERTIES(bpy.types.PropertyGroup):
3495 #{
3496 collider: bpy.props.EnumProperty( name='Collider Type',
3497 items=[('0','none',''),
3498 ('1','box',''),
3499 ('2','capsule','')])
3500
3501 collider_min: bpy.props.FloatVectorProperty( name='Collider Min', size=3 )
3502 collider_max: bpy.props.FloatVectorProperty( name='Collider Max', size=3 )
3503
3504 cone_constraint: bpy.props.BoolProperty( name='Cone constraint' )
3505
3506 conevx: bpy.props.FloatVectorProperty( name='vx' )
3507 conevy: bpy.props.FloatVectorProperty( name='vy' )
3508 coneva: bpy.props.FloatVectorProperty( name='va' )
3509 conet: bpy.props.FloatProperty( name='t' )
3510
3511 @staticmethod
3512 def sr_inspector( layout, data ):
3513 #{
3514 data = data[0]
3515 box = layout.box()
3516 box.prop( data, 'collider' )
3517
3518 if int(data.collider)>0:#{
3519 row = box.row()
3520 row.prop( data, 'collider_min' )
3521 row = box.row()
3522 row.prop( data, 'collider_max' )
3523 #}
3524
3525 box = layout.box()
3526 box.prop( data, 'cone_constraint' )
3527 if data.cone_constraint:#{
3528 row = box.row()
3529 row.prop( data, 'conevx' )
3530 row = box.row()
3531 row.prop( data, 'conevy' )
3532 row = box.row()
3533 row.prop( data, 'coneva' )
3534 box.prop( data, 'conet' )
3535 #}
3536 #}
3537 #}
3538
3539 class SR_MATERIAL_PROPERTIES(bpy.types.PropertyGroup):
3540 #{
3541 shader: bpy.props.EnumProperty(
3542 name="Format",
3543 items = [
3544 ('standard',"standard",''),
3545 ('standard_cutout', "standard_cutout", ''),
3546 ('terrain_blend', "terrain_blend", ''),
3547 ('vertex_blend', "vertex_blend", ''),
3548 ('water',"water",''),
3549 ('invisible','Invisible',''),
3550 ('boundary','Boundary',''),
3551 ('fxglow','FX Glow',''),
3552 ('cubemap','Cubemap',''),
3553 ('walking','Walking',''),
3554 ('foliage','Foliage','')
3555 ])
3556
3557 surface_prop: bpy.props.EnumProperty(
3558 name="Surface Property",
3559 items = [
3560 ('0','concrete',''),
3561 ('1','wood',''),
3562 ('2','grass',''),
3563 ('3','tiles',''),
3564 ('4','metal',''),
3565 ('5','snow (low friction)',''),
3566 ('6','sand (medium friction)','')
3567 ])
3568
3569 collision: bpy.props.BoolProperty( \
3570 name="Collisions Enabled",\
3571 default=True,\
3572 description = "Can the player collide with this material?"\
3573 )
3574 skate_surface: bpy.props.BoolProperty( \
3575 name="Skate Target", \
3576 default=True,\
3577 description = "Should the game try to target this surface?" \
3578 )
3579 grind_surface: bpy.props.BoolProperty( \
3580 name="Grindable", \
3581 default=True,\
3582 description = "Can you grind on this surface?" \
3583 )
3584 grow_grass: bpy.props.BoolProperty( \
3585 name="Grow Grass", \
3586 default=False,\
3587 description = "Spawn grass sprites on this surface?" \
3588 )
3589 preview_visibile: bpy.props.BoolProperty( \
3590 name="Preview visibile", \
3591 default=True,\
3592 description = "Show this material in preview models?" \
3593 )
3594 blend_offset: bpy.props.FloatVectorProperty( \
3595 name="Blend Offset", \
3596 size=2, \
3597 default=Vector((0.5,0.0)),\
3598 description="When surface is more than 45 degrees, add this vector " +\
3599 "to the UVs" \
3600 )
3601 sand_colour: bpy.props.FloatVectorProperty( \
3602 name="Sand Colour",\
3603 subtype='COLOR',\
3604 min=0.0,max=1.0,size=4,\
3605 default=Vector((0.79,0.63,0.48,1.0)),\
3606 description="Blend to this colour near the 0 coordinate on UP axis"\
3607 )
3608 shore_colour: bpy.props.FloatVectorProperty( \
3609 name="Shore Colour",\
3610 subtype='COLOR',\
3611 min=0.0,max=1.0,\
3612 default=Vector((0.03,0.32,0.61)),\
3613 description="Water colour at the shoreline"\
3614 )
3615 ocean_colour: bpy.props.FloatVectorProperty( \
3616 name="Ocean Colour",\
3617 subtype='COLOR',\
3618 min=0.0,max=1.0,\
3619 default=Vector((0.0,0.006,0.03)),\
3620 description="Water colour in the deep bits"\
3621 )
3622 tint: bpy.props.FloatVectorProperty( \
3623 name="Tint",\
3624 subtype='COLOR',\
3625 min=0.0,max=1.0,\
3626 size=4,\
3627 default=Vector((1.0,1.0,1.0,1.0)),\
3628 description="Reflection tint"\
3629 )
3630
3631 water_fog: bpy.props.FloatProperty( \
3632 name="Water fog", \
3633 default=0.04,\
3634 description="(default: 0.04) Cloudiness of water"\
3635 )
3636 water_fresnel: bpy.props.FloatProperty( \
3637 name="Water Fresnel", \
3638 default=5.0,\
3639 description="(default: 5.0) Reflection/Fog Fresnel Ratio"\
3640 )
3641 water_scale: bpy.props.FloatProperty( \
3642 name="Water Scale", \
3643 default=0.008,\
3644 description="(default: 0.008) Size of water texture"\
3645 )
3646 water_rate: bpy.props.FloatVectorProperty( \
3647 name = "Water Wave Rate",\
3648 size=4,\
3649 default=Vector(( 0.008, 0.006, 0.003, 0.03 )),\
3650 description=\
3651 "(default: 0.008, 0.006, 0.003, 0.03) Rate which water bump texture moves"\
3652 )
3653
3654 cubemap: bpy.props.PointerProperty( \
3655 type=bpy.types.Object, name="cubemap", \
3656 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_cubemap']))
3657
3658 tex_diffuse_rt: bpy.props.IntProperty( name="diffuse: RT index", default=-1 )
3659 #}
3660
3661 # ---------------------------------------------------------------------------- #
3662 # #
3663 # GUI section #
3664 # #
3665 # ---------------------------------------------------------------------------- #
3666
3667 cv_view_draw_handler = None
3668 cv_view_pixel_handler = None
3669 cv_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
3670 cv_view_verts = []
3671 cv_view_colours = []
3672 cv_view_course_i = 0
3673
3674 # Draw axis alligned sphere at position with radius
3675 #
3676 def cv_draw_sphere( pos, radius, colour ):
3677 #{
3678 global cv_view_verts, cv_view_colours
3679
3680 ly = pos + Vector((0,0,radius))
3681 lx = pos + Vector((0,radius,0))
3682 lz = pos + Vector((0,0,radius))
3683
3684 pi = 3.14159265358979323846264
3685
3686 for i in range(16):#{
3687 t = ((i+1.0) * 1.0/16.0) * pi * 2.0
3688 s = math.sin(t)
3689 c = math.cos(t)
3690
3691 py = pos + Vector((s*radius,0.0,c*radius))
3692 px = pos + Vector((s*radius,c*radius,0.0))
3693 pz = pos + Vector((0.0,s*radius,c*radius))
3694
3695 cv_view_verts += [ px, lx ]
3696 cv_view_verts += [ py, ly ]
3697 cv_view_verts += [ pz, lz ]
3698
3699 cv_view_colours += [ colour, colour, colour, colour, colour, colour ]
3700
3701 ly = py
3702 lx = px
3703 lz = pz
3704 #}
3705 cv_draw_lines()
3706 #}
3707
3708 # Draw axis alligned sphere at position with radius
3709 #
3710 def cv_draw_halfsphere( pos, tx, ty, tz, radius, colour ):
3711 #{
3712 global cv_view_verts, cv_view_colours
3713
3714 ly = pos + tz*radius
3715 lx = pos + ty*radius
3716 lz = pos + tz*radius
3717
3718 pi = 3.14159265358979323846264
3719
3720 for i in range(16):#{
3721 t = ((i+1.0) * 1.0/16.0) * pi
3722 s = math.sin(t)
3723 c = math.cos(t)
3724
3725 s1 = math.sin(t*2.0)
3726 c1 = math.cos(t*2.0)
3727
3728 py = pos + s*tx*radius + c *tz*radius
3729 px = pos + s*tx*radius + c *ty*radius
3730 pz = pos + s1*ty*radius + c1*tz*radius
3731
3732 cv_view_verts += [ px, lx ]
3733 cv_view_verts += [ py, ly ]
3734 cv_view_verts += [ pz, lz ]
3735
3736 cv_view_colours += [ colour, colour, colour, colour, colour, colour ]
3737
3738 ly = py
3739 lx = px
3740 lz = pz
3741 #}
3742 cv_draw_lines()
3743 #}
3744
3745 # Draw transformed -1 -> 1 cube
3746 #
3747 def cv_draw_ucube( transform, colour, s=Vector((1,1,1)), o=Vector((0,0,0)) ):
3748 #{
3749 global cv_view_verts, cv_view_colours
3750
3751 a = o + -1.0 * s
3752 b = o + 1.0 * s
3753
3754 vs = [None]*8
3755 vs[0] = transform @ Vector((a[0], a[1], a[2]))
3756 vs[1] = transform @ Vector((a[0], b[1], a[2]))
3757 vs[2] = transform @ Vector((b[0], b[1], a[2]))
3758 vs[3] = transform @ Vector((b[0], a[1], a[2]))
3759 vs[4] = transform @ Vector((a[0], a[1], b[2]))
3760 vs[5] = transform @ Vector((a[0], b[1], b[2]))
3761 vs[6] = transform @ Vector((b[0], b[1], b[2]))
3762 vs[7] = transform @ Vector((b[0], a[1], b[2]))
3763
3764 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
3765 (0,4),(1,5),(2,6),(3,7)]
3766
3767 for l in indices:#{
3768 v0 = vs[l[0]]
3769 v1 = vs[l[1]]
3770 cv_view_verts += [(v0[0],v0[1],v0[2])]
3771 cv_view_verts += [(v1[0],v1[1],v1[2])]
3772 cv_view_colours += [colour, colour]
3773 #}
3774 cv_draw_lines()
3775 #}
3776
3777 # Draw line with colour
3778 #
3779 def cv_draw_line( p0, p1, colour ):
3780 #{
3781 global cv_view_verts, cv_view_colours
3782
3783 cv_view_verts += [p0,p1]
3784 cv_view_colours += [colour, colour]
3785 cv_draw_lines()
3786 #}
3787
3788 # Draw line with colour(s)
3789 #
3790 def cv_draw_line2( p0, p1, c0, c1 ):
3791 #{
3792 global cv_view_verts, cv_view_colours
3793
3794 cv_view_verts += [p0,p1]
3795 cv_view_colours += [c0,c1]
3796 cv_draw_lines()
3797 #}
3798
3799 #
3800 #
3801 def cv_tangent_basis( n, tx, ty ):
3802 #{
3803 if abs( n[0] ) >= 0.57735027:#{
3804 tx[0] = n[1]
3805 tx[1] = -n[0]
3806 tx[2] = 0.0
3807 #}
3808 else:#{
3809 tx[0] = 0.0
3810 tx[1] = n[2]
3811 tx[2] = -n[1]
3812 #}
3813
3814 tx.normalize()
3815 _ty = n.cross( tx )
3816
3817 ty[0] = _ty[0]
3818 ty[1] = _ty[1]
3819 ty[2] = _ty[2]
3820 #}
3821
3822 # Draw coloured arrow
3823 #
3824 def cv_draw_arrow( p0, p1, c0, size=0.25, outline=True ):
3825 #{
3826 global cv_view_verts, cv_view_colours
3827
3828 n = p1-p0
3829 midpt = p0 + n*0.5
3830 n.normalize()
3831
3832 tx = Vector((1,0,0))
3833 ty = Vector((1,0,0))
3834 cv_tangent_basis( n, tx, ty )
3835 tx *= 0.5
3836 ty *= 0.5
3837
3838 if outline:#{
3839 cv_draw_lines()
3840 gpu.state.line_width_set(1.0)
3841 #}
3842
3843 cv_view_verts += [p0,p1, midpt+(tx-n)*size,midpt, midpt+(-tx-n)*size,midpt ]
3844 cv_view_colours += [c0,c0,c0,c0,c0,c0]
3845 cv_draw_lines()
3846
3847 if outline:#{
3848 gpu.state.line_width_set(3.0)
3849 cv_view_verts += [p0,p1,midpt+(tx-n)*size,midpt,midpt+(-tx-n)*size,midpt]
3850 b0 = (0,0,0)
3851 cv_view_colours += [b0,b0,b0,b0,b0,b0]
3852 cv_draw_lines()
3853 gpu.state.line_width_set(2.0)
3854 #}
3855 #}
3856
3857 def cv_draw_line_dotted( p0, p1, c0, dots=10 ):
3858 #{
3859 global cv_view_verts, cv_view_colours
3860
3861 for i in range(dots):#{
3862 t0 = i/dots
3863 t1 = (i+0.25)/dots
3864
3865 p2 = p0*(1.0-t0)+p1*t0
3866 p3 = p0*(1.0-t1)+p1*t1
3867
3868 cv_view_verts += [p2,p3]
3869 cv_view_colours += [c0,c0]
3870 #}
3871 #cv_draw_lines()
3872 #}
3873
3874 # Drawhandles of a bezier control point
3875 #
3876 def cv_draw_bhandle( obj, direction, colour ):
3877 #{
3878 global cv_view_verts, cv_view_colours
3879
3880 p0 = obj.location
3881 h0 = obj.matrix_world @ Vector((0,direction,0))
3882
3883 cv_view_verts += [p0]
3884 cv_view_verts += [h0]
3885 cv_view_colours += [colour,colour]
3886 cv_draw_lines()
3887 #}
3888
3889 # Draw a bezier curve (at fixed resolution 10)
3890 #
3891 def cv_draw_bezier( p0,h0,p1,h1,c0,c1 ):
3892 #{
3893 global cv_view_verts, cv_view_colours
3894
3895 last = p0
3896 for i in range(10):#{
3897 t = (i+1)/10
3898 a0 = 1-t
3899
3900 tt = t*t
3901 ttt = tt*t
3902 p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
3903
3904 cv_view_verts += [(last[0],last[1],last[2])]
3905 cv_view_verts += [(p[0],p[1],p[2])]
3906 cv_view_colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
3907
3908 last = p
3909 #}
3910 cv_draw_lines()
3911 #}
3912
3913 # I think this one extends the handles of the bezier otwards......
3914 #
3915 def cv_draw_sbpath( o0,o1,c0,c1,s0,s1 ):
3916 #{
3917 global cv_view_course_i
3918
3919 offs = ((cv_view_course_i % 2)*2-1) * cv_view_course_i * 0.02
3920
3921 p0 = o0.matrix_world @ Vector((offs, 0,0))
3922 h0 = o0.matrix_world @ Vector((offs, s0,0))
3923 p1 = o1.matrix_world @ Vector((offs, 0,0))
3924 h1 = o1.matrix_world @ Vector((offs,-s1,0))
3925
3926 cv_draw_bezier( p0,h0,p1,h1,c0,c1 )
3927 cv_draw_lines()
3928 #}
3929
3930 # Flush the lines buffers. This is called often because god help you if you want
3931 # to do fixed, fast buffers in this catastrophic programming language.
3932 #
3933 def cv_draw_lines():
3934 #{
3935 global cv_view_shader, cv_view_verts, cv_view_colours
3936
3937 if len(cv_view_verts) < 2:
3938 return
3939
3940 lines = batch_for_shader(\
3941 cv_view_shader, 'LINES', \
3942 { "pos":cv_view_verts, "color":cv_view_colours })
3943
3944 if bpy.context.scene.SR_data.gizmos:
3945 lines.draw( cv_view_shader )
3946
3947 cv_view_verts = []
3948 cv_view_colours = []
3949 #}
3950
3951 # I dont remember what this does exactly
3952 #
3953 def cv_draw_bpath( o0,o1,c0,c1 ):
3954 #{
3955 cv_draw_sbpath( o0,o1,c0,c1,1.0,1.0 )
3956 #}
3957
3958 # Semi circle to show the limit. and some lines
3959 #
3960 def draw_limit( obj, center, major, minor, amin, amax, colour ):
3961 #{
3962 global cv_view_verts, cv_view_colours
3963 f = 0.05
3964 ay = major*f
3965 ax = minor*f
3966
3967 for x in range(16):#{
3968 t0 = x/16
3969 t1 = (x+1)/16
3970 a0 = amin*(1.0-t0)+amax*t0
3971 a1 = amin*(1.0-t1)+amax*t1
3972
3973 p0 = center + major*f*math.cos(a0) + minor*f*math.sin(a0)
3974 p1 = center + major*f*math.cos(a1) + minor*f*math.sin(a1)
3975
3976 p0=obj.matrix_world @ p0
3977 p1=obj.matrix_world @ p1
3978 cv_view_verts += [p0,p1]
3979 cv_view_colours += [colour,colour]
3980
3981 if x == 0:#{
3982 cv_view_verts += [p0,center]
3983 cv_view_colours += [colour,colour]
3984 #}
3985 if x == 15:#{
3986 cv_view_verts += [p1,center]
3987 cv_view_colours += [colour,colour]
3988 #}
3989 #}
3990
3991 cv_view_verts += [center+major*1.2*f,center+major*f*0.8]
3992 cv_view_colours += [colour,colour]
3993
3994 cv_draw_lines()
3995 #}
3996
3997 # Cone and twist limit
3998 #
3999 def draw_cone_twist( center, vx, vy, va ):
4000 #{
4001 global cv_view_verts, cv_view_colours
4002 axis = vy.cross( vx )
4003 axis.normalize()
4004
4005 size = 0.12
4006
4007 cv_view_verts += [center, center+va*size]
4008 cv_view_colours += [ (1,1,1), (1,1,1) ]
4009
4010 for x in range(32):#{
4011 t0 = (x/32) * math.tau
4012 t1 = ((x+1)/32) * math.tau
4013
4014 c0 = math.cos(t0)
4015 s0 = math.sin(t0)
4016 c1 = math.cos(t1)
4017 s1 = math.sin(t1)
4018
4019 p0 = center + (axis + vx*c0 + vy*s0).normalized() * size
4020 p1 = center + (axis + vx*c1 + vy*s1).normalized() * size
4021
4022 col0 = ( abs(c0), abs(s0), 0.0 )
4023 col1 = ( abs(c1), abs(s1), 0.0 )
4024
4025 cv_view_verts += [center, p0, p0, p1]
4026 cv_view_colours += [ (0,0,0), col0, col0, col1 ]
4027 #}
4028
4029 cv_draw_lines()
4030 #}
4031
4032 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
4033 #
4034 def draw_skeleton_helpers( obj ):
4035 #{
4036 global cv_view_verts, cv_view_colours
4037
4038 if obj.data.pose_position != 'REST':#{
4039 return
4040 #}
4041
4042 for bone in obj.data.bones:#{
4043 c = bone.head_local
4044 a = Vector((bone.SR_data.collider_min[0],
4045 bone.SR_data.collider_min[1],
4046 bone.SR_data.collider_min[2]))
4047 b = Vector((bone.SR_data.collider_max[0],
4048 bone.SR_data.collider_max[1],
4049 bone.SR_data.collider_max[2]))
4050
4051 if bone.SR_data.collider == '1':#{
4052 vs = [None]*8
4053 vs[0]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+a[2]))
4054 vs[1]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+a[2]))
4055 vs[2]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+a[2]))
4056 vs[3]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+a[2]))
4057 vs[4]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+b[2]))
4058 vs[5]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+b[2]))
4059 vs[6]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+b[2]))
4060 vs[7]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+b[2]))
4061
4062 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
4063 (0,4),(1,5),(2,6),(3,7)]
4064
4065 for l in indices:#{
4066 v0 = vs[l[0]]
4067 v1 = vs[l[1]]
4068
4069 cv_view_verts += [(v0[0],v0[1],v0[2])]
4070 cv_view_verts += [(v1[0],v1[1],v1[2])]
4071 cv_view_colours += [(0.5,0.5,0.5),(0.5,0.5,0.5)]
4072 #}
4073 #}
4074 elif bone.SR_data.collider == '2':#{
4075 v0 = b-a
4076 major_axis = 0
4077 largest = -1.0
4078
4079 for i in range(3):#{
4080 if abs(v0[i]) > largest:#{
4081 largest = abs(v0[i])
4082 major_axis = i
4083 #}
4084 #}
4085
4086 v1 = Vector((0,0,0))
4087 v1[major_axis] = 1.0
4088
4089 tx = Vector((0,0,0))
4090 ty = Vector((0,0,0))
4091
4092 cv_tangent_basis( v1, tx, ty )
4093 r = (abs(tx.dot( v0 )) + abs(ty.dot( v0 ))) * 0.25
4094 l = v0[ major_axis ] - r*2
4095
4096 p0 = obj.matrix_world@Vector( c + (a+b)*0.5 + v1*l*-0.5 )
4097 p1 = obj.matrix_world@Vector( c + (a+b)*0.5 + v1*l* 0.5 )
4098
4099 colour = [0.2,0.2,0.2]
4100 colour[major_axis] = 0.5
4101
4102 cv_draw_halfsphere( p0, -v1, ty, tx, r, colour )
4103 cv_draw_halfsphere( p1, v1, ty, tx, r, colour )
4104 cv_draw_line( p0+tx* r, p1+tx* r, colour )
4105 cv_draw_line( p0+tx*-r, p1+tx*-r, colour )
4106 cv_draw_line( p0+ty* r, p1+ty* r, colour )
4107 cv_draw_line( p0+ty*-r, p1+ty*-r, colour )
4108 #}
4109 else:#{
4110 continue
4111 #}
4112
4113 center = obj.matrix_world @ c
4114 if bone.SR_data.cone_constraint:#{
4115 vx = Vector([bone.SR_data.conevx[_] for _ in range(3)])
4116 vy = Vector([bone.SR_data.conevy[_] for _ in range(3)])
4117 va = Vector([bone.SR_data.coneva[_] for _ in range(3)])
4118 draw_cone_twist( center, vx, vy, va )
4119 #}
4120 #}
4121 #}
4122
4123 def cv_draw_wireframe( mdl, points, colour ):#{
4124 for i in range(len(points)//2):#{
4125 p0 = mdl@points[i*2+0]
4126 p1 = mdl@points[i*2+1]
4127 cv_draw_line( p0, p1, colour )
4128 #}
4129 #}
4130
4131 def cv_ent_gate( obj ):
4132 #{
4133 global cv_view_verts, cv_view_colours
4134
4135 if obj.type != 'MESH': return
4136
4137 mesh_data = obj.data.SR_data.ent_gate[0]
4138 data = obj.SR_data.ent_gate[0]
4139 dims = mesh_data.dimensions
4140
4141 vs = [None]*9
4142 c = Vector((0,0,dims[2]))
4143
4144 vs[0] = obj.matrix_world @ Vector((-dims[0],0.0,-dims[1]+dims[2]))
4145 vs[1] = obj.matrix_world @ Vector((-dims[0],0.0, dims[1]+dims[2]))
4146 vs[2] = obj.matrix_world @ Vector(( dims[0],0.0, dims[1]+dims[2]))
4147 vs[3] = obj.matrix_world @ Vector(( dims[0],0.0,-dims[1]+dims[2]))
4148 vs[4] = obj.matrix_world @ (c+Vector((-1,0,-2)))
4149 vs[5] = obj.matrix_world @ (c+Vector((-1,0, 2)))
4150 vs[6] = obj.matrix_world @ (c+Vector(( 1,0, 2)))
4151 vs[7] = obj.matrix_world @ (c+Vector((-1,0, 0)))
4152 vs[8] = obj.matrix_world @ (c+Vector(( 1,0, 0)))
4153
4154 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
4155
4156 r3d = bpy.context.area.spaces.active.region_3d
4157
4158 p0 = r3d.view_matrix.inverted().translation
4159 v0 = (obj.matrix_world@Vector((0,0,0))) - p0
4160 v1 = obj.matrix_world.to_3x3() @ Vector((0,1,0))
4161
4162 if v0.dot(v1) > 0.0: cc = (0,1,0)
4163 else: cc = (1,0,0)
4164
4165 for l in indices:#{
4166 v0 = vs[l[0]]
4167 v1 = vs[l[1]]
4168 cv_view_verts += [(v0[0],v0[1],v0[2])]
4169 cv_view_verts += [(v1[0],v1[1],v1[2])]
4170 cv_view_colours += [cc,cc]
4171 #}
4172
4173 sw = (0.4,0.4,0.4)
4174 if data.target != None:
4175 cv_draw_arrow( obj.location, data.target.location, sw )
4176 #}
4177
4178 def cv_ent_volume( obj ):
4179 #{
4180 global cv_view_verts, cv_view_colours
4181
4182 data = obj.SR_data.ent_volume[0]
4183
4184 if data.subtype == '0':#{
4185 cv_draw_ucube( obj.matrix_world, (0,1,0), Vector((0.99,0.99,0.99)) )
4186
4187 if data.target:#{
4188 cv_draw_arrow( obj.location, data.target.location, (1,1,1) )
4189 #}
4190 #}
4191 elif data.subtype == '1':#{
4192 cv_draw_ucube( obj.matrix_world, (1,1,0) )
4193
4194 if data.target:#{
4195 cv_draw_arrow( obj.location, data.target.location, (1,1,1) )
4196 #}
4197 #}
4198 #}
4199
4200 def dijkstra( graph, start_node, target_node ):
4201 #{
4202 unvisited = [_ for _ in graph]
4203 shortest_path = {}
4204 previous_nodes = {}
4205
4206 for n in unvisited:
4207 shortest_path[n] = 9999999.999999
4208 shortest_path[start_node] = 0
4209
4210 while unvisited:#{
4211 current_min_node = None
4212 for n in unvisited:#{
4213 if current_min_node == None:
4214 current_min_node = n
4215 elif shortest_path[n] < shortest_path[current_min_node]:
4216 current_min_node = n
4217 #}
4218
4219 for branch in graph[current_min_node]:#{
4220 tentative_value = shortest_path[current_min_node]
4221 tentative_value += graph[current_min_node][branch]
4222 if tentative_value < shortest_path[branch]:#{
4223 shortest_path[branch] = tentative_value
4224 previous_nodes[branch] = current_min_node
4225 #}
4226 #}
4227
4228 unvisited.remove(current_min_node)
4229 #}
4230
4231 path = []
4232 node = target_node
4233 while node != start_node:#{
4234 path.append(node)
4235
4236 if node not in previous_nodes: return None
4237 node = previous_nodes[node]
4238 #}
4239
4240 # Add the start node manually
4241 path.append(start_node)
4242 return path
4243 #}
4244
4245 class dij_graph():
4246 #{
4247 def __init__(_,points,graph,subsections):#{
4248 _.points = points
4249 _.graph = graph
4250 _.subsections = subsections
4251 #}
4252 #}
4253
4254 def create_node_graph( curves, gates ):
4255 #{
4256 # add endpoints of curves
4257 graph = {}
4258 route_points = []
4259 subsections = []
4260 point_count = 0
4261 spline_count = 0
4262
4263 for c in range(len(curves)):#{
4264 for s in range(len(curves[c].data.splines)):#{
4265 spline = curves[c].data.splines[s]
4266 l = len(spline.points)
4267 if l < 2: continue
4268
4269 dist = round(spline.calc_length(),2)
4270
4271 ia = point_count
4272 ib = point_count+l-1
4273
4274 graph[ia] = { ib: dist }
4275 graph[ib] = { ia: dist }
4276
4277 for i in range(len(spline.points)):#{
4278 wco = curves[c].matrix_world @ spline.points[i].co
4279 route_points.append(Vector((wco[0],wco[1],wco[2]+0.5)))
4280
4281 previous = ia+i-1
4282 proxima = ia+i+1
4283
4284 if i == 0: previous = -1
4285 if i == len(spline.points)-1: proxima = -1
4286
4287 subsections.append((spline_count,previous,proxima))
4288 point_count += 1
4289 #}
4290
4291 spline_count += 1
4292 #}
4293 #}
4294
4295 # link endpoints
4296 graph_keys = list(graph)
4297 for i in range(len(graph_keys)-1):#{
4298 for j in range(i+1, len(graph_keys)):#{
4299 if i%2==0 and i+1==j: continue
4300
4301 ni = graph_keys[i]
4302 nj = graph_keys[j]
4303 pi = route_points[ni]
4304 pj = route_points[nj]
4305
4306 dist = round((pj-pi).magnitude,2)
4307
4308 if dist < 10.0:#{
4309 graph[ni][nj] = dist
4310 graph[nj][ni] = dist
4311 #}
4312 #}
4313 #}
4314
4315 # add and link gates( by name )
4316 for gate in gates:#{
4317 v1 = gate.matrix_world.to_3x3() @ Vector((0,1,0))
4318 if gate.SR_data.ent_gate[0].target:
4319 v1 = v1 * -1.0
4320
4321 graph[ gate.name ] = {}
4322
4323 for i in range(len(graph_keys)):#{
4324 ni = graph_keys[i]
4325 pi = route_points[ni]
4326
4327 v0 = pi-gate.location
4328 if v0.dot(v1) < 0.0: continue
4329
4330 dist = round(v0.magnitude,2)
4331
4332 if dist < 10.0:#{
4333 graph[ gate.name ][ ni ] = dist
4334 graph[ ni ][ gate.name ] = dist
4335 #}
4336 #}
4337 #}
4338
4339 return dij_graph(route_points,graph,subsections)
4340 #}
4341
4342 def solve_graph( dij, start, end ):
4343 #{
4344 path = dijkstra( dij.graph, end, start )
4345 full = []
4346
4347 if path:#{
4348 for sj in range(1,len(path)-2):#{
4349 i0 = path[sj]
4350 i1 = path[sj+1]
4351 map0 = dij.subsections[i0]
4352 map1 = dij.subsections[i1]
4353
4354 if map0[0] == map1[0]:#{
4355 if map0[1] == -1: direction = 2
4356 else: direction = 1
4357 sent = 0
4358
4359 while True:#{
4360 map0 = dij.subsections[i0]
4361 i1 = map0[direction]
4362 if i1 == -1: break
4363
4364 full.append( i0 )
4365 sent += 1
4366 i0 = i1
4367 if sent > 50: break
4368 #}
4369 #}
4370 else:#{
4371 full.append( i0 )
4372 #}
4373 #}
4374
4375 full.append( path[-2] )
4376 #}
4377 return full
4378 #}
4379
4380 def cv_draw_route( route, dij ):
4381 #{
4382 pole = Vector((0.2,0.2,10))
4383 hat = Vector((1,8,0.2))
4384 cc = (route.SR_data.ent_route[0].colour[0],
4385 route.SR_data.ent_route[0].colour[1],
4386 route.SR_data.ent_route[0].colour[2])
4387
4388 cv_draw_ucube(route.matrix_world,cc,Vector((0.5,-7.5,6)),\
4389 Vector((0,-6.5,5.5)))
4390 cv_draw_ucube(route.matrix_world,cc,pole, Vector(( 0.5, 0.5,0)) )
4391 cv_draw_ucube(route.matrix_world,cc,pole, Vector(( 0.5,-13.5,0)) )
4392 cv_draw_ucube(route.matrix_world,cc,hat, Vector((-0.5,-6.5, 12)) )
4393 cv_draw_ucube(route.matrix_world,cc,hat, Vector((-0.5,-6.5,-1)) )
4394
4395 checkpoints = route.SR_data.ent_route[0].gates
4396
4397 for i in range(len(checkpoints)):#{
4398 gi = checkpoints[i].target
4399 gj = checkpoints[(i+1)%len(checkpoints)].target
4400
4401 if gi:#{
4402 dest = gi.SR_data.ent_gate[0].target
4403 if dest:
4404 cv_draw_line_dotted( gi.location, dest.location, cc )
4405 gi = dest
4406 #}
4407
4408 if gi==gj: continue # error?
4409 if not gi or not gj: continue
4410
4411 path = solve_graph( dij, gi.name, gj.name )
4412
4413 if path:#{
4414 cv_draw_arrow(gi.location,dij.points[path[0]],cc,1.5,False)
4415 cv_draw_arrow(dij.points[path[len(path)-1]],gj.location,cc,1.5,False)
4416 for j in range(len(path)-1):#{
4417 i0 = path[j]
4418 i1 = path[j+1]
4419 o0 = dij.points[ i0 ]
4420 o1 = dij.points[ i1 ]
4421 cv_draw_arrow(o0,o1,cc,1.5,False)
4422 #}
4423 #}
4424 else:#{
4425 cv_draw_line_dotted( gi.location, gj.location, cc )
4426 #}
4427 #}
4428 #}
4429
4430 def cv_draw():#{
4431 global cv_view_shader
4432 global cv_view_verts
4433 global cv_view_colours
4434 global cv_view_course_i
4435
4436 cv_view_course_i = 0
4437 cv_view_verts = []
4438 cv_view_colours = []
4439
4440 cv_view_shader.bind()
4441 gpu.state.depth_mask_set(True)
4442 gpu.state.line_width_set(2.0)
4443 gpu.state.face_culling_set('BACK')
4444 gpu.state.depth_test_set('LESS')
4445 gpu.state.blend_set('NONE')
4446
4447 route_gates = []
4448 route_curves = []
4449 routes = []
4450
4451 for obj in bpy.context.collection.objects:#{
4452 if obj.type == 'ARMATURE':#{
4453 if obj.data.pose_position == 'REST':
4454 draw_skeleton_helpers( obj )
4455 #}
4456 else:#{
4457 ent_type = obj_ent_type( obj )
4458
4459 if ent_type == 'ent_gate':#{
4460 cv_ent_gate( obj )
4461 route_gates += [obj]
4462 #}
4463 elif ent_type == 'ent_route_node':#{
4464 if obj.type == 'CURVE':#{
4465 route_curves += [obj]
4466 #}
4467 #}
4468 elif ent_type == 'ent_route':
4469 routes += [obj]
4470 elif ent_type == 'ent_volume':#{
4471 cv_ent_volume( obj )
4472 #}
4473 elif ent_type == 'ent_objective':#{
4474 data = obj.SR_data.ent_objective[0]
4475 if data.proxima:#{
4476 cv_draw_arrow( obj.location, data.proxima.location, (1,0.6,0.2) )
4477 #}
4478 if data.target:
4479 cv_draw_arrow( obj.location, data.target.location, (1,1,1) )
4480 #}
4481 elif ent_type == 'ent_relay':#{
4482 data = obj.SR_data.ent_relay[0]
4483 if data.target0:
4484 cv_draw_arrow( obj.location, data.target0.location, (1,1,1) )
4485 if data.target1:
4486 cv_draw_arrow( obj.location, data.target1.location, (1,1,1) )
4487 if data.target2:
4488 cv_draw_arrow( obj.location, data.target2.location, (1,1,1) )
4489 if data.target3:
4490 cv_draw_arrow( obj.location, data.target3.location, (1,1,1) )
4491 #}
4492 elif ent_type == 'ent_challenge':#{
4493 data = obj.SR_data.ent_challenge[0]
4494 if data.target:
4495 cv_draw_arrow( obj.location, data.target.location, (1,1,1) )
4496 if data.reset:
4497 cv_draw_arrow( obj.location, data.reset.location, (0.9,0,0) )
4498 if data.first:
4499 cv_draw_arrow( obj.location, data.first.location, (1,0.6,0.2) )
4500
4501 cc1 = (0.4,0.3,0.2)
4502 info_cu = Vector((1.2,0.01,0.72))*0.5
4503 info_co = Vector((0.0,0.0,0.72))*0.5
4504 cv_draw_ucube( obj.matrix_world, cc1, info_cu, info_co)
4505 if data.camera:
4506 cv_draw_line_dotted( obj.location, data.camera.location, (1,1,1))
4507
4508 vs = [Vector((-0.2,0.0,0.10)),Vector((-0.2,0.0,0.62)),\
4509 Vector(( 0.2,0.0,0.62)),Vector((-0.2,0.0,0.30)),\
4510 Vector(( 0.1,0.0,0.30))]
4511 for v in range(len(vs)):#{
4512 vs[v] = obj.matrix_world @ vs[v]
4513 #}
4514
4515 cv_view_verts += [vs[0],vs[1],vs[1],vs[2],vs[3],vs[4]]
4516 cv_view_colours += [cc1,cc1,cc1,cc1,cc1,cc1]
4517 #}
4518 elif ent_type == 'ent_audio':#{
4519 if obj.SR_data.ent_audio[0].flag_3d:
4520 cv_draw_sphere( obj.location, obj.scale[0], (1,1,0) )
4521 #}
4522 elif ent_type == 'ent_font':#{
4523 data = obj.SR_data.ent_font[0]
4524
4525 for i in range(len(data.variants)):#{
4526 sub = data.variants[i].mesh
4527 if not sub: continue
4528
4529 for ch in data.glyphs:#{
4530 mini = (ch.bounds[0],ch.bounds[1])
4531 maxi = (ch.bounds[2]+mini[0],ch.bounds[3]+mini[1])
4532 p0 = sub.matrix_world @ Vector((mini[0],0.0,mini[1]))
4533 p1 = sub.matrix_world @ Vector((maxi[0],0.0,mini[1]))
4534 p2 = sub.matrix_world @ Vector((maxi[0],0.0,maxi[1]))
4535 p3 = sub.matrix_world @ Vector((mini[0],0.0,maxi[1]))
4536
4537 if i == data.variants_index: cc = (0.5,0.5,0.5)
4538 else: cc = (0,0,0)
4539
4540 cv_view_verts += [p0,p1,p1,p2,p2,p3,p3,p0]
4541 cv_view_colours += [cc,cc,cc,cc,cc,cc,cc,cc]
4542 #}
4543 #}
4544 #}
4545 elif ent_type == 'ent_glider':#{
4546 mesh = [Vector((-1.13982, 0.137084, -0.026358)), \
4547 Vector(( 1.13982, 0.137084, -0.026358)), \
4548 Vector(( 0.0, 1.6, 1.0)), \
4549 Vector(( 0.0, -3.0, 1.0)), \
4550 Vector(( -3.45, -1.78, 0.9)), \
4551 Vector(( 0.0, 1.6, 1.0)), \
4552 Vector(( 3.45, -1.78, 0.9)), \
4553 Vector(( 0.0, 1.6, 1.0)), \
4554 Vector(( 3.45, -1.78, 0.9)), \
4555 Vector(( -3.45, -1.78, 0.9))]
4556
4557 cv_draw_wireframe( obj.matrix_world, mesh, (1,1,1) )
4558 #}
4559 elif ent_type == 'ent_skateshop':#{
4560 data = obj.SR_data.ent_skateshop[0]
4561 display = data.mark_display
4562 info = data.mark_info
4563
4564 if data.tipo == '0':#{
4565 cc = (0.0,0.9,0.6)
4566 cc1 = (0.4,0.9,0.2)
4567 cc2 = (0.9,0.6,0.1)
4568
4569 rack = data.mark_rack
4570
4571 rack_cu = Vector((3.15,2.0,0.1))*0.5
4572 rack_co = Vector((0.0,0.0,0.0))
4573 display_cu = Vector((0.3,1.2,0.1))*0.5
4574 display_co = Vector((0.0,0.0,0.1))*0.5
4575 info_cu = Vector((1.2,0.01,0.3))*0.5
4576 info_co = Vector((0.0,0.0,0.0))*0.5
4577 #}
4578 elif data.tipo == '1':#{
4579 rack = None
4580 cc1 = (1.0,0.0,0.0)
4581 cc2 = (1.0,0.5,0.0)
4582 display_cu = Vector((0.4,0.4,2.0))*0.5
4583 display_co = Vector((0.0,0.0,1.0))*0.5
4584 info_cu = Vector((1.2,0.01,0.3))*0.5
4585 info_co = Vector((0.0,0.0,0.0))*0.5
4586 #}
4587 elif data.tipo == '2':#{
4588 rack = None
4589 cc1 = (1.0,0.0,0.0)
4590 cc2 = (1.0,0.5,0.0)
4591 display_cu = Vector((1.0,1.0,0.5))*0.5
4592 display_co = Vector((0.0,0.0,0.5))*0.5
4593 info_cu = Vector((1.2,0.01,0.3))*0.5
4594 info_co = Vector((0.0,0.0,0.0))*0.5
4595 #}
4596 elif data.tipo == '3':#{
4597 rack = None
4598 display = None
4599 info = None
4600 #}
4601 elif data.tipo == '4':#{
4602 rack = None
4603 display = None
4604 info = None
4605 #}
4606
4607 if rack:
4608 cv_draw_ucube( rack.matrix_world, cc, rack_cu, rack_co )
4609 if display:
4610 cv_draw_ucube( display.matrix_world, cc1, display_cu, display_co)
4611 if info:
4612 cv_draw_ucube( info.matrix_world, cc2, info_cu, info_co )
4613 #}
4614 elif ent_type == 'ent_swspreview':#{
4615 cc1 = (0.4,0.9,0.2)
4616 data = obj.SR_data.ent_swspreview[0]
4617 display = data.mark_display
4618 display1 = data.mark_display1
4619 display_cu = Vector((0.3,1.2,0.1))*0.5
4620 display_co = Vector((0.0,0.0,0.1))*0.5
4621 if display:
4622 cv_draw_ucube( display.matrix_world, cc1, display_cu, display_co)
4623 if display1:
4624 cv_draw_ucube(display1.matrix_world, cc1, display_cu, display_co)
4625 #}
4626 # elif ent_type == 'ent_list':#{
4627 # data = obj.SR_data.ent_list[0]
4628 # for child in data.entities:#{
4629 # if child.target:#{
4630 # cv_draw_arrow( obj.location, child.target.location, \
4631 # (.5,.5,.5), 0.1 )
4632 # #}
4633 # #}
4634 # #}
4635 elif ent_type == 'ent_region':#{
4636 data = obj.SR_data.ent_region[0]
4637 if data.target0:#{
4638 cv_draw_arrow( obj.location, data.target0.location, \
4639 (.5,.5,.5), 0.1 )
4640 #}
4641 #}
4642 elif ent_type == 'ent_menuitem':#{
4643 for i,col in enumerate(obj.users_collection):#{
4644 colour32 = hash_djb2( col.name )
4645 r = pow(((colour32 ) & 0xff) / 255.0, 2.2 )
4646 g = pow(((colour32>>8 ) & 0xff) / 255.0, 2.2 )
4647 b = pow(((colour32>>16) & 0xff) / 255.0, 2.2 )
4648 cc = (r,g,b)
4649 vs = [None for _ in range(8)]
4650 scale = i*0.02
4651 for j in range(8):#{
4652 v0 = Vector([(obj.bound_box[j][z]+\
4653 ((-1.0 if obj.bound_box[j][z]<0.0 else 1.0)*scale)) \
4654 for z in range(3)])
4655 vs[j] = obj.matrix_world @ v0
4656 #}
4657 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
4658 (0,4),(1,5),(2,6),(3,7)]
4659 for l in indices:#{
4660 v0 = vs[l[0]]
4661 v1 = vs[l[1]]
4662 cv_view_verts += [(v0[0],v0[1],v0[2])]
4663 cv_view_verts += [(v1[0],v1[1],v1[2])]
4664 cv_view_colours += [cc,cc]
4665 #}
4666 #}
4667 cv_draw_lines()
4668 cc = (1.0,1.0,1.0)
4669 data = obj.SR_data.ent_menuitem[0]
4670 if data.tipo == '4':#{
4671 if data.slider_minloc and data.slider_maxloc:#{
4672 v0 = data.slider_minloc.location
4673 v1 = data.slider_maxloc.location
4674 cv_draw_line( v0, v1, cc )
4675 #}
4676 #}
4677
4678 colour32 = hash_djb2(obj.name)
4679 r = ((colour32 ) & 0xff) / 255.0
4680 g = ((colour32>>8 ) & 0xff) / 255.0
4681 b = ((colour32>>16) & 0xff) / 255.0
4682 cc = (r,g,b)
4683 origin = obj.location + (Vector((r,g,b))*2.0-Vector((1.0,1.0,1.0)))\
4684 * 0.04
4685
4686 size = 0.01
4687
4688 if data.tipo != '0':#{
4689 if data.tipo == '4':#{
4690 if data.link0:#{
4691 cv_draw_arrow( origin, data.link0.location, cc, size )
4692 #}
4693 if data.link1:#{
4694 cv_draw_arrow( origin, data.link1.location, cc, size )
4695 #}
4696 #}
4697 else:#{
4698 if data.link0:#{
4699 cv_draw_arrow( origin, data.link0.location, cc, size )
4700 #}
4701 if data.link1:#{
4702 cv_draw_arrow( origin, data.link1.location, cc, size )
4703 #}
4704 if data.link2:#{
4705 cv_draw_arrow( origin, data.link2.location, cc, size )
4706 #}
4707 if data.link3:#{
4708 cv_draw_arrow( origin, data.link3.location, cc, size )
4709 #}
4710 #}
4711 #}
4712 #}
4713 #}
4714 #}
4715
4716 dij = create_node_graph( route_curves, route_gates )
4717
4718 #cv_draw_route_map( route_nodes )
4719 for route in routes:#{
4720 cv_draw_route( route, dij )
4721 #}
4722
4723 cv_draw_lines()
4724 #}
4725
4726 def pos3d_to_2d( pos ):#{
4727 return view3d_utils.location_3d_to_region_2d( \
4728 bpy.context.region, \
4729 bpy.context.space_data.region_3d, pos )
4730 #}
4731
4732 def cv_draw_pixel():#{
4733 if not bpy.context.scene.SR_data.gizmos: return
4734 blf.size(0,10)
4735 blf.color(0, 1.0,1.0,1.0,0.9)
4736 blf.enable(0,blf.SHADOW)
4737 blf.shadow(0,3,0.0,0.0,0.0,1.0)
4738 for obj in bpy.context.collection.objects:#{
4739 ent_type = obj_ent_type( obj )
4740
4741 if ent_type != 'none':#{
4742 co = pos3d_to_2d( obj.location )
4743
4744 if not co: continue
4745 blf.position(0,co[0],co[1],0)
4746 blf.draw(0,ent_type)
4747 #}
4748 #}
4749 #}
4750
4751 classes = [ SR_INTERFACE, SR_MATERIAL_PANEL,\
4752 SR_COLLECTION_SETTINGS, SR_SCENE_SETTINGS, \
4753 SR_COMPILE, SR_COMPILE_THIS, SR_MIRROR_BONE_X,\
4754 \
4755 SR_OBJECT_ENT_GATE, SR_MESH_ENT_GATE, SR_OBJECT_ENT_SPAWN, \
4756 SR_OBJECT_ENT_ROUTE_ENTRY, SR_UL_ROUTE_NODE_LIST, \
4757 SR_OBJECT_ENT_ROUTE, SR_OT_ROUTE_LIST_NEW_ITEM,\
4758 SR_OT_GLYPH_LIST_NEW_ITEM, SR_OT_GLYPH_LIST_DEL_ITEM,\
4759 SR_OT_GLYPH_LIST_MOVE_ITEM,\
4760 SR_OT_AUDIO_LIST_NEW_ITEM,SR_OT_AUDIO_LIST_DEL_ITEM,\
4761 SR_OT_FONT_VARIANT_LIST_NEW_ITEM,SR_OT_FONT_VARIANT_LIST_DEL_ITEM,\
4762 SR_OT_COPY_ENTITY_DATA, \
4763 SR_OBJECT_ENT_VOLUME, \
4764 SR_UL_AUDIO_LIST, SR_OBJECT_ENT_AUDIO_FILE_ENTRY,\
4765 SR_OT_ROUTE_LIST_DEL_ITEM,\
4766 SR_OBJECT_ENT_AUDIO,SR_OBJECT_ENT_MARKER,SR_OBJECT_ENT_GLYPH,\
4767 SR_OBJECT_ENT_FONT_VARIANT,
4768 SR_OBJECT_ENT_GLYPH_ENTRY,\
4769 SR_UL_FONT_VARIANT_LIST,SR_UL_FONT_GLYPH_LIST,\
4770 SR_OBJECT_ENT_FONT,SR_OBJECT_ENT_TRAFFIC,SR_OBJECT_ENT_SKATESHOP,\
4771 SR_OBJECT_ENT_WORKSHOP_PREVIEW,SR_OBJECT_ENT_MENU_ITEM,\
4772 SR_OBJECT_ENT_WORLD_INFO,SR_OBJECT_ENT_CCMD,\
4773 SR_OBJECT_ENT_OBJECTIVE,SR_OBJECT_ENT_CHALLENGE,\
4774 SR_OBJECT_ENT_REGION,\
4775 SR_OBJECT_ENT_RELAY,SR_OBJECT_ENT_MINIWORLD,\
4776 SR_OBJECT_ENT_LIST_ENTRY, SR_UL_ENT_LIST, SR_OBJECT_ENT_LIST, \
4777 SR_OT_ENT_LIST_NEW_ITEM, SR_OT_ENT_LIST_DEL_ITEM,\
4778 SR_OBJECT_ENT_GLIDER, SR_OBJECT_ENT_NPC, \
4779 \
4780 SR_OBJECT_PROPERTIES, SR_LIGHT_PROPERTIES, SR_BONE_PROPERTIES,
4781 SR_MESH_PROPERTIES, SR_MATERIAL_PROPERTIES \
4782 ]
4783
4784 def register():
4785 #{
4786 for c in classes:
4787 bpy.utils.register_class(c)
4788
4789 bpy.types.Scene.SR_data = \
4790 bpy.props.PointerProperty(type=SR_SCENE_SETTINGS)
4791 bpy.types.Collection.SR_data = \
4792 bpy.props.PointerProperty(type=SR_COLLECTION_SETTINGS)
4793
4794 bpy.types.Object.SR_data = \
4795 bpy.props.PointerProperty(type=SR_OBJECT_PROPERTIES)
4796 bpy.types.Light.SR_data = \
4797 bpy.props.PointerProperty(type=SR_LIGHT_PROPERTIES)
4798 bpy.types.Bone.SR_data = \
4799 bpy.props.PointerProperty(type=SR_BONE_PROPERTIES)
4800 bpy.types.Mesh.SR_data = \
4801 bpy.props.PointerProperty(type=SR_MESH_PROPERTIES)
4802 bpy.types.Material.SR_data = \
4803 bpy.props.PointerProperty(type=SR_MATERIAL_PROPERTIES)
4804
4805 global cv_view_draw_handler, cv_view_pixel_handler
4806 cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
4807 cv_draw,(),'WINDOW','POST_VIEW')
4808 cv_view_pixel_handler = bpy.types.SpaceView3D.draw_handler_add(\
4809 cv_draw_pixel,(),'WINDOW','POST_PIXEL')
4810 #}
4811
4812 def unregister():
4813 #{
4814 for c in classes:
4815 bpy.utils.unregister_class(c)
4816
4817 global cv_view_draw_handler, cv_view_pixel_handler
4818 bpy.types.SpaceView3D.draw_handler_remove(cv_view_draw_handler,'WINDOW')
4819 bpy.types.SpaceView3D.draw_handler_remove(cv_view_pixel_handler,'WINDOW')
4820 #}