8cd60ea7d15240eb82ade9c56dc8afb5471a9b93
2 # =============================================================================
4 # Copyright . . . -----, ,----- ,---. .---.
5 # 2021-2022 |\ /| | / | | | | /|
6 # | \ / | +-- / +----- +---' | / |
7 # | \ / | | / | | \ | / |
8 # | \/ | | / | | \ | / |
9 # ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
11 # =============================================================================
13 # Python exporter for Blender, compiles .mdl format for Skate Rift.
15 # Its really slow, sorry, I don't know how to speed it up.
16 # Also not sure why you need to put # before {} in code blocks, there is errors
20 import bpy
, math
, gpu
, os
23 from mathutils
import *
24 from gpu_extras
.batch
import batch_for_shader
27 "name":"Skate Rift model compiler",
28 "author": "Harry Godden (hgn)",
35 "category":"Import/Export",
38 class mdl_vert(Structure
): # 48 bytes. Quite large. Could compress
39 #{ # the normals and uvs to i16s. Not an
40 _pack_
= 1 # real issue, yet.
41 _fields_
= [("co",c_float
*3),
45 ("weights",c_uint16
*4),
49 class mdl_submesh(Structure
):
52 _fields_
= [("indice_start",c_uint32
),
53 ("indice_count",c_uint32
),
54 ("vertex_start",c_uint32
),
55 ("vertex_count",c_uint32
),
56 ("bbx",(c_float
*3)*2),
57 ("material_id",c_uint32
)] # index into the material array
60 class mdl_texture(Structure
):
63 _fields_
= [("pstr_name",c_uint32
),
64 ("pack_offset",c_uint32
),
65 ("pack_length",c_uint32
)]
68 class mdl_material(Structure
):
71 _fields_
= [("pstr_name",c_uint32
),
74 ("surface_prop",c_uint32
),
76 ("colour1",c_float
*4),
77 ("tex_diffuse",c_uint32
),
78 ("tex_decal",c_uint32
),
79 ("tex_normal",c_uint32
)]
82 class mdl_node(Structure
):
85 _fields_
= [("co",c_float
*3),
88 ("sub_uid",c_uint32
), # dont use
89 ("submesh_start",c_uint32
),
90 ("submesh_count",c_uint32
),
91 ("classtype",c_uint32
),
94 ("pstr_name",c_uint32
)]
97 class mdl_header(Structure
):
100 _fields_
= [("identifier",c_uint32
),
101 ("version",c_uint32
),
102 ("file_length",c_uint32
),
105 ("node_count",c_uint32
),
106 ("node_offset",c_uint32
),
108 ("submesh_count",c_uint32
),
109 ("submesh_offset",c_uint32
),
111 ("material_count",c_uint32
),
112 ("material_offset",c_uint32
),
114 ("texture_count",c_uint32
),
115 ("texture_offset",c_uint32
),
117 ("anim_count",c_uint32
),
118 ("anim_offset",c_uint32
),
120 ("entdata_size",c_uint32
),
121 ("entdata_offset",c_uint32
),
123 ("strings_size",c_uint32
),
124 ("strings_offset",c_uint32
),
126 ("keyframe_count",c_uint32
),
127 ("keyframe_offset",c_uint32
),
129 ("vertex_count",c_uint32
),
130 ("vertex_offset",c_uint32
),
132 ("indice_count",c_uint32
),
133 ("indice_offset",c_uint32
),
135 ("pack_size",c_uint32
),
136 ("pack_offset",c_uint32
)]
139 class mdl_animation(Structure
):
142 _fields_
= [("pstr_name",c_uint32
),
148 class mdl_keyframe(Structure
):
151 _fields_
= [("co",c_float
*3),
156 # ---------------------------------------------------------------------------- #
158 # Entity definitions #
160 # ---------------------------------------------------------------------------- #
162 # ctypes _fields_ defines the data which is filled in by:
163 # def encode_obj( _, node, node_def ):
165 # gizmos get drawn into the viewport via:
167 # def draw_scene_helpers( obj ):
169 # editor enterface, simiraliy:
171 # def editor_interface( layout, obj ):
176 # Purpose: A rift. must target another gate, the target gate can not have more
177 # than one target nodes of its own.
179 class classtype_gate(Structure
):
182 _fields_
= [("target",c_uint32
),
185 def encode_obj(_
, node
,node_def
):
189 obj
= node_def
['obj']
191 if obj
.cv_data
.target
!= None:
192 _
.target
= obj
.cv_data
.target
.cv_data
.uid
194 if obj
.type == 'MESH':
196 _
.dims
[0] = obj
.data
.cv_data
.v0
[0]
197 _
.dims
[1] = obj
.data
.cv_data
.v0
[1]
198 _
.dims
[2] = obj
.data
.cv_data
.v0
[2]
202 _
.dims
[0] = obj
.cv_data
.v0
[0]
203 _
.dims
[1] = obj
.cv_data
.v0
[1]
204 _
.dims
[2] = obj
.cv_data
.v0
[2]
209 def draw_scene_helpers( obj
):
211 global cv_view_verts
, cv_view_colours
213 if obj
.type == 'MESH':
214 dims
= obj
.data
.cv_data
.v0
216 dims
= obj
.cv_data
.v0
219 c
= Vector((0,0,dims
[2]))
221 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
222 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
223 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
224 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
225 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
226 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
227 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
228 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
229 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
231 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
237 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
238 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
239 cv_view_colours
+= [(1,1,0,1),(1,1,0,1)]
242 sw
= (0.4,0.4,0.4,0.2)
243 if obj
.cv_data
.target
!= None:
244 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, sw
)
248 def editor_interface( layout
, obj
):
250 layout
.prop( obj
.cv_data
, "target" )
253 layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
254 layout
.prop( mesh
.cv_data
, "v0", text
="Gate dimensions" )
260 # Purpose: player can reset here, its a safe place
261 # spawns can share the same name, the closest one will be picked
263 # when the world loads it will pick the one named 'start' first.
265 class classtype_spawn(Structure
):
268 _fields_
= [("pstr_alias",c_uint32
)]
270 def encode_obj(_
, node
,node_def
):
273 _
.pstr_alias
= encoder_process_pstr( node_def
['obj'].cv_data
.strp
)
277 def draw_scene_helpers( obj
):
279 global cv_view_verts
, cv_view_colours
282 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
283 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
284 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
285 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
286 indices
= [(0,1),(1,2),(1,3)]
293 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
294 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
295 cv_view_colours
+= [(0,1,1,1),(0,1,1,1)]
298 cv_draw_sphere( obj
.location
, 20.0, [0.1,0,0.9,0.4] )
302 def editor_interface( layout
, obj
):
304 layout
.prop( obj
.cv_data
, "strp", text
="Alias" )
310 # Purpose: Tells the game to draw water HERE, at this entity.
312 class classtype_water(Structure
):
315 _fields_
= [("temp",c_uint32
)]
317 def encode_obj(_
, node
,node_def
):
326 # Purpose: Defines a route node and links to up to two more nodes
328 class classtype_route_node(Structure
):
331 _fields_
= [("target",c_uint32
),
332 ("target1",c_uint32
)]
334 def encode_obj(_
, node
,node_def
):
337 obj
= node_def
['obj']
339 if obj
.cv_data
.target
!= None:
340 _
.target
= obj
.cv_data
.target
.cv_data
.uid
341 if obj
.cv_data
.target1
!= None:
342 _
.target1
= obj
.cv_data
.target1
.cv_data
.uid
346 def draw_scene_helpers( obj
):
348 global cv_view_verts
, cv_view_colours
350 sw
= Vector((0.4,0.4,0.4,0.2))
351 sw2
= Vector((1.5,0.2,0.2,0.0))
352 if obj
.cv_data
.target
!= None:
353 cv_draw_bpath( obj
, obj
.cv_data
.target
, sw
, sw
)
354 if obj
.cv_data
.target1
!= None:
355 cv_draw_bpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
357 cv_draw_bhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
358 cv_draw_bhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
361 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
362 cv_draw_arrow( obj
.location
, p1
, sw
)
366 def editor_interface( layout
, obj
):
368 layout
.prop( obj
.cv_data
, "target", text
="Left" )
369 layout
.prop( obj
.cv_data
, "target1", text
="Right" )
375 # Purpose: Defines a route, its 'starting' point, and the colour to use for it
377 class classtype_route(Structure
):
380 _fields_
= [("id_start",c_uint32
),
381 ("pstr_name",c_uint32
),
382 ("colour",c_float
*3)]
384 def encode_obj(_
, node
,node_def
):
387 obj
= node_def
['obj']
389 _
.colour
[0] = obj
.cv_data
.colour
[0]
390 _
.colour
[1] = obj
.cv_data
.colour
[1]
391 _
.colour
[2] = obj
.cv_data
.colour
[2]
392 _
.pstr_name
= encoder_process_pstr( obj
.cv_data
.strp
)
394 if obj
.cv_data
.target
!= None:
395 _
.id_start
= obj
.cv_data
.target
.cv_data
.uid
399 def draw_scene_helpers( obj
):
401 global cv_view_verts
, cv_view_colours
, cv_view_course_i
403 if obj
.cv_data
.target
:
404 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, [1,1,1,1] )
406 # Tries to simulate how we do it in the game
410 stack
[0] = obj
.cv_data
.target
412 loop_complete
= False
416 if stack_i
[si
-1] == 2:
421 if si
== 0: # Loop failed to complete
427 targets
= [None,None]
428 targets
[0] = node
.cv_data
.target
430 if node
.cv_data
.classtype
== 'classtype_route_node':
432 targets
[1] = node
.cv_data
.target1
435 nextnode
= targets
[stack_i
[si
-1]]
438 if nextnode
!= None: # branch
440 if nextnode
== stack
[0]: # Loop completed
449 if stack
[sj
] == nextnode
: # invalidated path
468 cc
= Vector((obj
.cv_data
.colour
[0],\
469 obj
.cv_data
.colour
[1],\
470 obj
.cv_data
.colour
[2],\
477 if stack
[sj
].cv_data
.classtype
== 'classtype_gate' and \
478 stack
[sk
].cv_data
.classtype
== 'classtype_gate':
480 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
481 cv_draw_sbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
484 cv_draw_bpath( stack
[sj
], stack
[sk
], cc
, cc
)
487 cv_view_course_i
+= 1
492 def editor_interface( layout
, obj
):
494 layout
.prop( obj
.cv_data
, "target", text
="'Start' from" )
495 layout
.prop( obj
.cv_data
, "colour" )
496 layout
.prop( obj
.cv_data
, "strp", text
="Name" )
502 # Purpose: links an mesh node to a type 11
504 class classtype_skin(Structure
):
507 _fields_
= [("skeleton",c_uint32
)]
509 def encode_obj(_
, node
,node_def
):
513 armature_def
= node_def
['linked_armature']
514 _
.skeleton
= armature_def
['obj'].cv_data
.uid
520 # Purpose: defines the allocation requirements for a skeleton
522 class classtype_skeleton(Structure
):
525 _fields_
= [("channels",c_uint32
),
526 ("ik_count",c_uint32
),
527 ("collider_count",c_uint32
),
528 ("anim_start",c_uint32
),
529 ("anim_count",c_uint32
)]
531 def encode_obj(_
, node
,node_def
):
535 _
.channels
= len( node_def
['bones'] )
536 _
.ik_count
= node_def
['ik_count']
537 _
.collider_count
= node_def
['collider_count']
538 _
.anim_start
= node_def
['anim_start']
539 _
.anim_count
= node_def
['anim_count']
546 # Purpose: intrinsic bone type, stores collision information and limits too
548 class classtype_bone(Structure
):
551 _fields_
= [("deform",c_uint32
),
552 ("ik_target",c_uint32
),
553 ("ik_pole",c_uint32
),
554 ("collider",c_uint32
),
555 ("use_limits",c_uint32
),
556 ("angle_limits",(c_float
*3)*2),
557 ("hitbox",(c_float
*3)*2)]
559 def encode_obj(_
, node
,node_def
):
563 armature_def
= node_def
['linked_armature']
564 obj
= node_def
['bone']
566 _
.deform
= node_def
['deform']
568 if 'ik_target' in node_def
:
570 _
.ik_target
= armature_def
['bones'].index( node_def
['ik_target'] )
571 _
.ik_pole
= armature_def
['bones'].index( node_def
['ik_pole'] )
576 if obj
.cv_data
.collider
:
579 _
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
580 _
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
581 _
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
582 _
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
583 _
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
584 _
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
590 _
.angle_limits
[0][0] = obj
.cv_data
.mins
[0]
591 _
.angle_limits
[0][1] = obj
.cv_data
.mins
[2]
592 _
.angle_limits
[0][2] = -obj
.cv_data
.maxs
[1]
593 _
.angle_limits
[1][0] = obj
.cv_data
.maxs
[0]
594 _
.angle_limits
[1][1] = obj
.cv_data
.maxs
[2]
595 _
.angle_limits
[1][2] = -obj
.cv_data
.mins
[1]
602 # Purpose: sends a signal to another entity
604 class classtype_trigger(Structure
):
607 _fields_
= [("target",c_uint32
)]
609 def encode_obj(_
, node
,node_def
):
612 if node_def
['obj'].cv_data
.target
:
613 _
.target
= node_def
['obj'].cv_data
.target
.cv_data
.uid
617 def draw_scene_helpers( obj
):
619 global cv_view_verts
, cv_view_colours
620 cv_draw_ucube( obj
.matrix_world
, [0,1,0,1] )
622 if obj
.cv_data
.target
:
623 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, [1,1,1,1] )
627 def editor_interface( layout
, obj
):
629 layout
.prop( obj
.cv_data
, "target", text
="Triggers" )
635 # Purpose: Gives the player an achievement.
636 # No cheating! You shouldn't use this entity anyway, since only ME can
637 # add achievements to the steam ;)
639 class classtype_logic_achievement(Structure
):
642 _fields_
= [("pstr_name",c_uint32
)]
644 def encode_obj(_
, node
,node_def
):
647 _
.pstr_name
= encoder_process_pstr( node_def
['obj'].cv_data
.strp
)
651 def editor_interface( layout
, obj
):
653 layout
.prop( obj
.cv_data
, "strp", text
="Achievement ID" )
659 # Purpose: sends a signal to another entity
661 class classtype_logic_relay(Structure
):
664 _fields_
= [("targets",c_uint32
*4)]
666 def encode_obj(_
, node
,node_def
):
669 obj
= node_def
['obj']
670 if obj
.cv_data
.target
:
671 _
.targets
[0] = obj
.cv_data
.target
.cv_data
.uid
672 if obj
.cv_data
.target1
:
673 _
.targets
[1] = obj
.cv_data
.target1
.cv_data
.uid
674 if obj
.cv_data
.target2
:
675 _
.targets
[2] = obj
.cv_data
.target2
.cv_data
.uid
676 if obj
.cv_data
.target3
:
677 _
.targets
[3] = obj
.cv_data
.target3
.cv_data
.uid
681 def draw_scene_helpers( obj
):
683 global cv_view_verts
, cv_view_colours
685 if obj
.cv_data
.target
:
686 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, [1,1,1,1] )
687 if obj
.cv_data
.target1
:
688 cv_draw_arrow( obj
.location
, obj
.cv_data
.target1
.location
, [1,1,1,1] )
689 if obj
.cv_data
.target2
:
690 cv_draw_arrow( obj
.location
, obj
.cv_data
.target2
.location
, [1,1,1,1] )
691 if obj
.cv_data
.target3
:
692 cv_draw_arrow( obj
.location
, obj
.cv_data
.target3
.location
, [1,1,1,1] )
696 def editor_interface( layout
, obj
):
698 layout
.prop( obj
.cv_data
, "target", text
="Triggers" )
699 layout
.prop( obj
.cv_data
, "target1", text
="Triggers" )
700 layout
.prop( obj
.cv_data
, "target2", text
="Triggers" )
701 layout
.prop( obj
.cv_data
, "target3", text
="Triggers" )
707 # Purpose: Plays some audio (44100hz .ogg vorbis only)
708 # NOTE: There is a 32mb limit on the audio buffer, world audio is
709 # decompressed and stored in signed 16 bit integers (2 bytes)
712 # volume: not used if has 3D flag
714 # AUDIO_FLAG_LOOP 0x1
715 # AUDIO_FLAG_ONESHOT 0x2 (DONT USE THIS, it breaks semaphores)
716 # AUDIO_FLAG_SPACIAL_3D 0x4 (Probably what you want)
717 # AUDIO_FLAG_AUTO_START 0x8 (Play when the world starts)
719 # the rest are just internal flags, only use the above 3.
721 class classtype_audio(Structure
):
724 _fields_
= [("pstr_file",c_uint32
),
728 def encode_obj(_
, node
,node_def
):
732 obj
= node_def
['obj']
734 _
.pstr_file
= encoder_process_pstr( obj
.cv_data
.strp
)
737 if obj
.cv_data
.bp0
: flags |
= 0x1
738 if obj
.cv_data
.bp1
: flags |
= 0x4
739 if obj
.cv_data
.bp2
: flags |
= 0x8
742 _
.volume
= obj
.cv_data
.fltp
746 def editor_interface( layout
, obj
):
748 layout
.prop( obj
.cv_data
, "strp" )
750 layout
.prop( obj
.cv_data
, "bp0", text
= "Looping" )
751 layout
.prop( obj
.cv_data
, "bp1", text
= "3D Audio" )
752 layout
.prop( obj
.cv_data
, "bp2", text
= "Auto Start" )
756 def draw_scene_helpers( obj
):
758 global cv_view_verts
, cv_view_colours
760 cv_draw_sphere( obj
.location
, obj
.scale
[0], [1,1,0,1] )
764 class classtype_spawn_link(Structure
):
767 _fields_
= [("connections",c_uint32
*4)]
769 def encode_obj(_
, node
,node_def
):
775 def editor_interface( layout
, obj
):
781 def draw_scene_helpers( obj
):
783 global cv_view_verts
, cv_view_colours
787 for obj1
in bpy
.context
.collection
.objects
:
789 if (obj1
.cv_data
.classtype
!= 'classtype_spawn_link') and \
790 (obj1
.cv_data
.classtype
!= 'classtype_spawn') :
793 if (obj1
.location
- obj
.location
).length
< 40.0:
795 cv_draw_line( obj
.location
, obj1
.location
, [1,1,1,1] )
803 cv_draw_sphere( obj
.location
, 20.0, [0.5,0,0.2,0.4] )
807 # ---------------------------------------------------------------------------- #
811 # ---------------------------------------------------------------------------- #
813 # Current encoder state
819 def encoder_init( collection
):
825 # The actual file header
827 'header': mdl_header(),
831 'pack_textures': collection
.cv_data
.pack_textures
,
833 # Compiled data chunks (each can be read optionally by the client)
837 #1---------------------------------
838 'node': [], # Metadata 'chunk'
843 'entdata': bytearray(), # variable width
844 'strings': bytearray(), # .
845 #2---------------------------------
846 'keyframe': [], # Animations
847 #3---------------------------------
848 'vertex': [], # Mesh data
850 #4---------------------------------
851 'pack': bytearray() # Other generic packed data
854 # All objects of the model in their final heirachy
860 # Allows us to reuse definitions
864 'material_cache': {},
868 g_encoder
['header'].identifier
= 0xABCD0000
869 g_encoder
['header'].version
= 1
871 # Add fake NoneID material and texture
873 none_material
= mdl_material()
874 none_material
.pstr_name
= encoder_process_pstr( "" )
875 none_material
.texture_id
= 0
877 none_texture
= mdl_texture()
878 none_texture
.pstr_name
= encoder_process_pstr( "" )
879 none_texture
.pack_offset
= 0
880 none_texture
.pack_length
= 0
882 g_encoder
['data']['material'] += [none_material
]
883 g_encoder
['data']['texture'] += [none_texture
]
885 g_encoder
['data']['pack'].extend( b
'datapack\0\0\0\0\0\0\0\0' )
900 root
.pstr_name
= encoder_process_pstr('')
901 root
.submesh_start
= 0
902 root
.submesh_count
= 0
905 root
.parent
= 0xffffffff
907 g_encoder
['data']['node'] += [root
]
911 # fill with 0x00 until a multiple of align. Returns how many bytes it added
913 def bytearray_align_to( buffer, align
, offset
=0 ):
917 while ((len(buffer)+offset
) % align
) != 0:
919 buffer.extend( b
'\0' )
926 # Add a string to the string buffer except if it already exists there then we
927 # just return its ID.
929 def encoder_process_pstr( s
):
933 cache
= g_encoder
['string_cache']
938 cache
[s
] = len( g_encoder
['data']['strings'] )
940 buffer = g_encoder
['data']['strings']
941 buffer.extend( s
.encode('utf-8') )
942 buffer.extend( b
'\0' )
944 bytearray_align_to( buffer, 4 )
948 def get_texture_resource_name( img
):
950 return os
.path
.splitext( img
.name
)[0]
955 def encoder_process_texture( img
):
962 cache
= g_encoder
['texture_cache']
963 buffer = g_encoder
['data']['texture']
964 pack
= g_encoder
['data']['pack']
966 name
= get_texture_resource_name( img
)
971 cache
[name
] = len( buffer )
974 tex
.pstr_name
= encoder_process_pstr( name
)
976 if g_encoder
['pack_textures']:
978 tex
.pack_offset
= len( pack
)
979 pack
.extend( qoi_encode( img
) )
980 tex
.pack_length
= len( pack
) - tex
.pack_offset
989 def material_tex_image(v
):
999 cxr_graph_mapping
= \
1001 # Default shader setup
1008 "image": "tex_diffuse"
1012 "A": material_tex_image("tex_diffuse"),
1013 "B": material_tex_image("tex_decal")
1020 "Color": material_tex_image("tex_normal")
1026 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
1028 def material_info(mat
):
1032 # Using the cv_graph_mapping as a reference, go through the shader
1033 # graph and gather all $props from it.
1035 def _graph_read( node_def
, node
=None, depth
=0 ):
1044 _graph_read
.extracted
= []
1046 for node_idname
in node_def
:
1048 for n
in mat
.node_tree
.nodes
:
1050 if n
.name
== node_idname
:
1052 node_def
= node_def
[node_idname
]
1060 for link
in node_def
:
1062 link_def
= node_def
[link
]
1064 if isinstance( link_def
, dict ):
1067 for x
in node
.inputs
:
1069 if isinstance( x
, bpy
.types
.NodeSocketColor
):
1079 if node_link
and node_link
.is_linked
:
1081 # look for definitions for the connected node type
1083 from_node
= node_link
.links
[0].from_node
1085 node_name
= from_node
.name
.split('.')[0]
1086 if node_name
in link_def
:
1088 from_node_def
= link_def
[ node_name
]
1090 _graph_read( from_node_def
, from_node
, depth
+1 )
1094 # TODO: Make a warning for this?
1098 if "default" in link_def
:
1100 prop
= link_def
['default']
1101 info
[prop
] = node_link
.default_value
1108 info
[prop
] = getattr( node
, link
)
1113 _graph_read( cxr_graph_mapping
)
1117 # Add a material to the material buffer. Returns 0 (None ID) if invalid
1119 def encoder_process_material( mat
):
1126 cache
= g_encoder
['material_cache']
1127 buffer = g_encoder
['data']['material']
1129 if mat
.name
in cache
:
1130 return cache
[mat
.name
]
1132 cache
[mat
.name
] = len( buffer )
1134 dest
= mdl_material()
1135 dest
.pstr_name
= encoder_process_pstr( mat
.name
)
1138 if mat
.cv_data
.collision
:
1140 if mat
.cv_data
.skate_surface
: flags |
= 0x1
1141 if mat
.cv_data
.grind_surface
: flags |
= (0x8|
0x1)
1143 if mat
.cv_data
.grow_grass
: flags |
= 0x4
1146 if mat
.cv_data
.surface_prop
== 'concrete': dest
.surface_prop
= 0
1147 if mat
.cv_data
.surface_prop
== 'wood': dest
.surface_prop
= 1
1148 if mat
.cv_data
.surface_prop
== 'grass': dest
.surface_prop
= 2
1150 if mat
.cv_data
.shader
== 'standard': dest
.shader
= 0
1151 if mat
.cv_data
.shader
== 'standard_cutout': dest
.shader
= 1
1152 if mat
.cv_data
.shader
== 'terrain_blend':
1156 dest
.colour
[0] = pow( mat
.cv_data
.sand_colour
[0], 1.0/2.2 )
1157 dest
.colour
[1] = pow( mat
.cv_data
.sand_colour
[1], 1.0/2.2 )
1158 dest
.colour
[2] = pow( mat
.cv_data
.sand_colour
[2], 1.0/2.2 )
1159 dest
.colour
[3] = 1.0
1161 dest
.colour1
[0] = mat
.cv_data
.blend_offset
[0]
1162 dest
.colour1
[1] = mat
.cv_data
.blend_offset
[1]
1165 if mat
.cv_data
.shader
== 'vertex_blend':
1169 dest
.colour1
[0] = mat
.cv_data
.blend_offset
[0]
1170 dest
.colour1
[1] = mat
.cv_data
.blend_offset
[1]
1173 if mat
.cv_data
.shader
== 'water':
1177 dest
.colour
[0] = pow( mat
.cv_data
.shore_colour
[0], 1.0/2.2 )
1178 dest
.colour
[1] = pow( mat
.cv_data
.shore_colour
[1], 1.0/2.2 )
1179 dest
.colour
[2] = pow( mat
.cv_data
.shore_colour
[2], 1.0/2.2 )
1180 dest
.colour
[3] = 1.0
1181 dest
.colour1
[0] = pow( mat
.cv_data
.ocean_colour
[0], 1.0/2.2 )
1182 dest
.colour1
[1] = pow( mat
.cv_data
.ocean_colour
[1], 1.0/2.2 )
1183 dest
.colour1
[2] = pow( mat
.cv_data
.ocean_colour
[2], 1.0/2.2 )
1184 dest
.colour1
[3] = 1.0
1187 inf
= material_info( mat
)
1189 if mat
.cv_data
.shader
== 'standard' or \
1190 mat
.cv_data
.shader
== 'standard_cutout' or \
1191 mat
.cv_data
.shader
== 'terrain_blend' or \
1192 mat
.cv_data
.shader
== 'vertex_blend':
1194 if 'tex_diffuse' in inf
:
1195 dest
.tex_diffuse
= encoder_process_texture(inf
['tex_diffuse'])
1199 return cache
[mat
.name
]
1202 # Create a tree structure containing all the objects in the collection
1204 def encoder_build_scene_graph( collection
):
1208 print( " creating scene graph" )
1212 graph
= g_encoder
['scene_graph']
1213 graph_lookup
= g_encoder
['graph_lookup']
1216 graph
["children"] = []
1218 graph
["parent"] = None
1223 uid
= g_encoder
['uid_count']
1224 g_encoder
['uid_count'] += 1
1228 for obj
in collection
.all_objects
:
1230 if obj
.parent
: continue
1232 def _extend( p
, n
, d
):
1237 tree
["children"] = []
1243 # Descend into amature
1245 if n
.type == 'ARMATURE':
1247 tree
["bones"] = [None] # None is the root transform
1248 tree
["ik_count"] = 0
1249 tree
["collider_count"] = 0
1251 # Here also collects some information about constraints, ik and
1252 # counts colliders for the armature.
1254 def _extendb( p
, n
, d
):
1260 btree
["linked_armature"] = tree
1261 btree
["uid"] = _new_uid()
1262 btree
["children"] = []
1265 tree
["bones"] += [n
.name
]
1267 for c
in n
.children
:
1269 _extendb( btree
, c
, d
+1 )
1272 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
1276 btree
["ik_target"] = c
.subtarget
1277 btree
["ik_pole"] = c
.pole_subtarget
1278 tree
["ik_count"] += 1
1282 if n
.cv_data
.collider
:
1283 tree
['collider_count'] += 1
1285 btree
['deform'] = n
.use_deform
1286 p
['children'] += [btree
]
1289 for b
in n
.data
.bones
:
1291 _extendb( tree
, b
, d
+1 )
1294 # Recurse into children of this object
1296 for obj1
in n
.children
:
1299 for c1
in obj1
.users_collection
:
1301 if c1
== collection
:
1303 _extend( tree
, obj1
, d
+1 )
1309 p
["children"] += [tree
]
1310 graph_lookup
[n
] = tree
1314 _extend( graph
, obj
, 1 )
1320 # Kind of a useless thing i made but it looks cool and adds complexity!!1
1322 def encoder_graph_iterator( root
):
1324 for c
in root
['children']:
1327 yield from encoder_graph_iterator(c
)
1332 # Push a vertex into the model file, or return a cached index (c_uint32)
1334 def encoder_vertex_push( vertex_reference
, co
,norm
,uv
,colour
,groups
,weights
):
1337 buffer = g_encoder
['data']['vertex']
1340 m
= float(10**TOLERENCE
)
1342 # Would be nice to know if this can be done faster than it currently runs,
1345 key
= (int(co
[0]*m
+0.5),
1353 colour
[0], # these guys are already quantized
1366 if key
in vertex_reference
:
1367 return vertex_reference
[key
]
1370 index
= c_uint32( len(vertex_reference
) )
1371 vertex_reference
[key
] = index
1379 v
.norm
[2] = -norm
[1]
1382 v
.colour
[0] = colour
[0]
1383 v
.colour
[1] = colour
[1]
1384 v
.colour
[2] = colour
[2]
1385 v
.colour
[3] = colour
[3]
1386 v
.weights
[0] = weights
[0]
1387 v
.weights
[1] = weights
[1]
1388 v
.weights
[2] = weights
[2]
1389 v
.weights
[3] = weights
[3]
1390 v
.groups
[0] = groups
[0]
1391 v
.groups
[1] = groups
[1]
1392 v
.groups
[2] = groups
[2]
1393 v
.groups
[3] = groups
[3]
1401 # Compile a mesh (or use one from the cache) onto node, based on node_def
1404 def encoder_compile_mesh( node
, node_def
):
1408 graph
= g_encoder
['scene_graph']
1409 graph_lookup
= g_encoder
['graph_lookup']
1410 mesh_cache
= g_encoder
['mesh_cache']
1411 obj
= node_def
['obj']
1413 can_use_cache
= True
1415 # Check for modifiers that typically change the data per-instance
1416 # there is no well defined rule for the choices here, its just what i've
1417 # needed while producing the game.
1419 # It may be possible to detect these cases automatically.
1421 for mod
in obj
.modifiers
:
1423 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
1424 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
1425 mod
.type == 'ARRAY':
1427 can_use_cache
= False
1430 if mod
.type == 'ARMATURE':
1431 armature_def
= graph_lookup
[mod
.object]
1433 # Check the cache first
1435 if can_use_cache
and (obj
.data
.name
in mesh_cache
):
1437 ref
= mesh_cache
[obj
.data
.name
]
1438 node
.submesh_start
= ref
.submesh_start
1439 node
.submesh_count
= ref
.submesh_count
1443 # Compile a whole new mesh
1445 node
.submesh_start
= len( g_encoder
['data']['submesh'] )
1446 node
.submesh_count
= 0
1448 dgraph
= bpy
.context
.evaluated_depsgraph_get()
1449 data
= obj
.evaluated_get(dgraph
).data
1450 data
.calc_loop_triangles()
1451 data
.calc_normals_split()
1453 # Mesh is split into submeshes based on their material
1455 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
1456 for material_id
, mat
in enumerate(mat_list
):
1461 sm
.indice_start
= len( g_encoder
['data']['indice'] )
1462 sm
.vertex_start
= len( g_encoder
['data']['vertex'] )
1465 sm
.material_id
= encoder_process_material( mat
)
1469 sm
.bbx
[0][i
] = 999999
1470 sm
.bbx
[1][i
] = -999999
1473 # Keep a reference to very very very similar vertices
1475 vertex_reference
= {}
1477 # Write the vertex / indice data
1479 for tri_index
, tri
in enumerate(data
.loop_triangles
):
1481 if tri
.material_index
!= material_id
:
1486 vert
= data
.vertices
[tri
.vertices
[j
]]
1488 vi
= data
.loops
[li
].vertex_index
1490 # Gather vertex information
1493 norm
= data
.loops
[li
].normal
1495 colour
= (255,255,255,255)
1502 uv
= data
.uv_layers
.active
.data
[li
].uv
1506 if data
.vertex_colors
:
1508 colour
= data
.vertex_colors
.active
.data
[li
].color
1509 colour
= (int(colour
[0]*255.0),\
1510 int(colour
[1]*255.0),\
1511 int(colour
[2]*255.0),\
1512 int(colour
[3]*255.0))
1515 # Weight groups: truncates to the 3 with the most influence. The
1516 # fourth bone ID is never used by the shader so it is
1521 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
1522 if obj
.vertex_groups
[_
.group
].name
in \
1523 armature_def
['bones']]
1525 weight_groups
= sorted( src_groups
, key
= \
1526 lambda a
: a
.weight
, reverse
=True )
1530 if len(weight_groups
) > ml
:
1532 g
= weight_groups
[ml
]
1533 name
= obj
.vertex_groups
[g
.group
].name
1536 weights
[ml
] = weight
1537 groups
[ml
] = armature_def
['bones'].index(name
)
1542 if len(weight_groups
) > 0:
1544 inv_norm
= (1.0/tot
) * 65535.0
1547 weights
[ml
] = int( weights
[ml
] * inv_norm
)
1548 weights
[ml
] = min( weights
[ml
], 65535 )
1549 weights
[ml
] = max( weights
[ml
], 0 )
1555 li1
= tri
.loops
[(j
+1)%3]
1556 vi1
= data
.loops
[li1
].vertex_index
1557 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
1559 if e0
.use_freestyle_mark
and \
1560 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
1561 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
1567 # Add vertex and expand bound box
1569 index
= encoder_vertex_push( vertex_reference
, co
, \
1575 g_encoder
['data']['indice'] += [index
]
1579 # How many unique verts did we add in total
1581 sm
.vertex_count
= len(g_encoder
['data']['vertex']) - sm
.vertex_start
1582 sm
.indice_count
= len(g_encoder
['data']['indice']) - sm
.indice_start
1584 # Make sure bounding box isn't -inf -> inf if no vertices
1586 if sm
.vertex_count
== 0:
1592 for j
in range(sm
.vertex_count
):
1594 vert
= g_encoder
['data']['vertex'][ sm
.vertex_start
+ j
]
1598 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], vert
.co
[i
] )
1599 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], vert
.co
[i
] )
1604 # Add submesh to encoder
1606 g_encoder
['data']['submesh'] += [sm
]
1607 node
.submesh_count
+= 1
1611 # Save a reference to this node since we want to reuse the submesh indices
1613 g_encoder
['mesh_cache'][obj
.data
.name
] = node
1617 def encoder_compile_ent_as( name
, node
, node_def
):
1621 if name
== 'classtype_none':
1627 elif name
not in globals():
1629 print( "Classtype '" +name
+ "' is unknown!" )
1633 buffer = g_encoder
['data']['entdata']
1634 node
.offset
= len(buffer)
1636 cl
= globals()[ name
]
1638 inst
.encode_obj( node
, node_def
)
1640 buffer.extend( bytearray(inst
) )
1641 bytearray_align_to( buffer, 4 )
1644 # Compiles animation data into model and gives us some extra node_def entries
1646 def encoder_compile_armature( node
, node_def
):
1650 entdata
= g_encoder
['data']['entdata']
1651 animdata
= g_encoder
['data']['anim']
1652 keyframedata
= g_encoder
['data']['keyframe']
1653 mesh_cache
= g_encoder
['mesh_cache']
1654 obj
= node_def
['obj']
1655 bones
= node_def
['bones']
1658 node_def
['anim_start'] = len(animdata
)
1659 node_def
['anim_count'] = 0
1663 if obj
.animation_data
:
1665 # So we can restore later
1667 previous_frame
= bpy
.context
.scene
.frame_current
1668 previous_action
= obj
.animation_data
.action
1669 POSE_OR_REST_CACHE
= obj
.data
.pose_position
1670 obj
.data
.pose_position
= 'POSE'
1672 for NLALayer
in obj
.animation_data
.nla_tracks
:
1674 for NLAStrip
in NLALayer
.strips
:
1678 for a
in bpy
.data
.actions
:
1680 if a
.name
== NLAStrip
.name
:
1682 obj
.animation_data
.action
= a
1687 # Clip to NLA settings
1689 anim_start
= int(NLAStrip
.action_frame_start
)
1690 anim_end
= int(NLAStrip
.action_frame_end
)
1694 anim
= mdl_animation()
1695 anim
.pstr_name
= encoder_process_pstr( NLAStrip
.action
.name
)
1697 anim
.offset
= len(keyframedata
)
1698 anim
.length
= anim_end
-anim_start
1700 # Export the keyframes
1701 for frame
in range(anim_start
,anim_end
):
1703 bpy
.context
.scene
.frame_set(frame
)
1705 for bone_name
in bones
:
1707 for pb
in obj
.pose
.bones
:
1709 if pb
.name
!= bone_name
: continue
1711 rb
= obj
.data
.bones
[ bone_name
]
1713 # relative bone matrix
1714 if rb
.parent
is not None:
1716 offset_mtx
= rb
.parent
.matrix_local
1717 offset_mtx
= offset_mtx
.inverted_safe() @ \
1720 inv_parent
= pb
.parent
.matrix
@ offset_mtx
1721 inv_parent
.invert_safe()
1722 fpm
= inv_parent
@ pb
.matrix
1726 bone_mtx
= rb
.matrix
.to_4x4()
1727 local_inv
= rb
.matrix_local
.inverted_safe()
1728 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
1731 loc
, rot
, sca
= fpm
.decompose()
1734 final_pos
= Vector(( loc
[0], loc
[2], -loc
[1] ))
1737 lc_m
= pb
.matrix_channel
.to_3x3()
1738 if pb
.parent
is not None:
1740 smtx
= pb
.parent
.matrix_channel
.to_3x3()
1741 lc_m
= smtx
.inverted() @ lc_m
1743 rq
= lc_m
.to_quaternion()
1746 kf
.co
[0] = final_pos
[0]
1747 kf
.co
[1] = final_pos
[1]
1748 kf
.co
[2] = final_pos
[2]
1760 keyframedata
+= [kf
]
1766 # Add to animation buffer
1769 node_def
['anim_count'] += 1
1773 status_name
= F
" " + " |"*(node_def
['depth']-1)
1774 print( F
"{status_name} | *anim: {NLAStrip.action.name}" )
1778 # Restore context to how it was before
1780 bpy
.context
.scene
.frame_set( previous_frame
)
1781 obj
.animation_data
.action
= previous_action
1782 obj
.data
.pose_position
= POSE_OR_REST_CACHE
1786 # We are trying to compile this node_def
1788 def encoder_process_definition( node_def
):
1792 # data sources for object/bone are taken differently
1794 if 'obj' in node_def
:
1796 obj
= node_def
['obj']
1798 obj_co
= obj
.location
1800 if obj_type
== 'ARMATURE':
1801 obj_classtype
= 'classtype_skeleton'
1804 obj_classtype
= obj
.cv_data
.classtype
1806 # Check for armature deform
1808 for mod
in obj
.modifiers
:
1810 if mod
.type == 'ARMATURE':
1812 obj_classtype
= 'classtype_skin'
1814 # Make sure to freeze armature in rest while we collect
1815 # vertex information
1817 armature_def
= g_encoder
['graph_lookup'][mod
.object]
1818 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
1819 armature_def
['obj'].data
.pose_position
= 'REST'
1820 node_def
['linked_armature'] = armature_def
1827 elif 'bone' in node_def
:
1829 obj
= node_def
['bone']
1831 obj_co
= obj
.head_local
1832 obj_classtype
= 'classtype_bone'
1838 node
.pstr_name
= encoder_process_pstr( obj
.name
)
1840 if node_def
["parent"]:
1841 node
.parent
= node_def
["parent"]["uid"]
1845 node
.co
[0] = obj_co
[0]
1846 node
.co
[1] = obj_co
[2]
1847 node
.co
[2] = -obj_co
[1]
1849 # Convert rotation quat to our space type
1851 quat
= obj
.matrix_local
.to_quaternion()
1854 node
.q
[2] = -quat
[2]
1857 # Bone scale is just a vector to the tail
1859 if obj_type
== 'BONE':
1861 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
1862 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
1863 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
1867 node
.s
[0] = obj
.scale
[0]
1868 node
.s
[1] = obj
.scale
[2]
1869 node
.s
[2] = obj
.scale
[1]
1874 tot_uid
= g_encoder
['uid_count']-1
1875 obj_uid
= node_def
['uid']
1876 obj_depth
= node_def
['depth']-1
1878 status_id
= F
" [{obj_uid: 3}/{tot_uid}]" + " |"*obj_depth
1879 status_name
= status_id
+ F
" L {obj.name}"
1881 if obj_classtype
!= 'classtype_none': status_type
= obj_classtype
1882 else: status_type
= obj_type
1884 status_parent
= F
"{node.parent: 3}"
1887 if obj_classtype
== 'classtype_skin':
1888 status_armref
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
1890 print(F
"{status_name:<32} {status_type:<22} {status_parent} {status_armref}")
1892 # Process mesh if needed
1894 if obj_type
== 'MESH':
1896 encoder_compile_mesh( node
, node_def
)
1898 elif obj_type
== 'ARMATURE':
1900 encoder_compile_armature( node
, node_def
)
1903 encoder_compile_ent_as( obj_classtype
, node
, node_def
)
1905 # Make sure to reset the armature we just mucked about with
1907 if obj_classtype
== 'classtype_skin':
1908 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
1910 g_encoder
['data']['node'] += [node
]
1913 # The post processing step or the pre processing to the writing step
1915 def encoder_write_to_file( path
):
1919 # Compile down to a byte array
1921 header
= g_encoder
['header']
1922 file_pos
= sizeof(header
)
1923 file_data
= bytearray()
1924 print( " Compositing data arrays" )
1926 for array_name
in g_encoder
['data']:
1928 file_pos
+= bytearray_align_to( file_data
, 16, sizeof(header
) )
1929 arr
= g_encoder
['data'][array_name
]
1931 setattr( header
, array_name
+ "_offset", file_pos
)
1933 print( F
" {array_name:<16} @{file_pos:> 8X}[{len(arr)}]" )
1935 if isinstance( arr
, bytearray
):
1937 setattr( header
, array_name
+ "_size", len(arr
) )
1939 file_data
.extend( arr
)
1940 file_pos
+= len(arr
)
1944 setattr( header
, array_name
+ "_count", len(arr
) )
1948 bbytes
= bytearray(item
)
1949 file_data
.extend( bbytes
)
1950 file_pos
+= sizeof(item
)
1955 # This imperitive for this field to be santized in the future!
1957 header
.file_length
= file_pos
1959 print( " Writing file" )
1960 # Write header and data chunk to file
1962 fp
= open( path
, "wb" )
1963 fp
.write( bytearray( header
) )
1964 fp
.write( file_data
)
1968 # Main compiler, uses string as the identifier for the collection
1970 def write_model(collection_name
):
1973 print( F
"Model graph | Create mode '{collection_name}'" )
1974 folder
= bpy
.path
.abspath(bpy
.context
.scene
.cv_data
.export_dir
)
1975 path
= F
"{folder}{collection_name}.mdl"
1978 collection
= bpy
.data
.collections
[collection_name
]
1980 encoder_init( collection
)
1981 encoder_build_scene_graph( collection
)
1985 print( " Comping objects" )
1986 it
= encoder_graph_iterator( g_encoder
['scene_graph'] )
1988 encoder_process_definition( node_def
)
1992 encoder_write_to_file( path
)
1994 print( F
"Completed {collection_name}.mdl" )
1997 # ---------------------------------------------------------------------------- #
2001 # ---------------------------------------------------------------------------- #
2003 cv_view_draw_handler
= None
2004 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
2006 cv_view_colours
= []
2007 cv_view_course_i
= 0
2009 # Draw axis alligned sphere at position with radius
2011 def cv_draw_sphere( pos
, radius
, colour
):
2013 global cv_view_verts
, cv_view_colours
2015 ly
= pos
+ Vector((0,0,radius
))
2016 lx
= pos
+ Vector((0,radius
,0))
2017 lz
= pos
+ Vector((0,0,radius
))
2019 pi
= 3.14159265358979323846264
2023 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
2027 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
2028 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
2029 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
2031 cv_view_verts
+= [ px
, lx
]
2032 cv_view_verts
+= [ py
, ly
]
2033 cv_view_verts
+= [ pz
, lz
]
2035 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2044 # Draw transformed -1 -> 1 cube
2046 def cv_draw_ucube( transform
, colour
):
2048 global cv_view_verts
, cv_view_colours
2050 a
= Vector((-1,-1,-1))
2054 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
2055 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
2056 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
2057 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
2058 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
2059 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
2060 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
2061 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
2063 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2064 (0,4),(1,5),(2,6),(3,7)]
2070 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2071 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2072 cv_view_colours
+= [(0,1,0,1),(0,1,0,1)]
2077 # Draw line with colour
2079 def cv_draw_line( p0
, p1
, colour
):
2081 global cv_view_verts
, cv_view_colours
2083 cv_view_verts
+= [p0
,p1
]
2084 cv_view_colours
+= [colour
, colour
]
2088 # Draw line with colour(s)
2090 def cv_draw_line2( p0
, p1
, c0
, c1
):
2092 global cv_view_verts
, cv_view_colours
2094 cv_view_verts
+= [p0
,p1
]
2095 cv_view_colours
+= [c0
,c1
]
2099 # Just the tx because we dont really need ty for this app
2101 def cv_tangent_basis_tx( n
, tx
):
2103 if abs( n
[0] ) >= 0.57735027:
2119 # Draw coloured arrow
2121 def cv_draw_arrow( p0
, p1
, c0
):
2123 global cv_view_verts
, cv_view_colours
2129 tx
= Vector((1,0,0))
2130 cv_tangent_basis_tx( n
, tx
)
2132 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*0.15,midpt
, midpt
+(-tx
-n
)*0.15,midpt
]
2133 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
2137 # Drawhandles of a bezier control point
2139 def cv_draw_bhandle( obj
, direction
, colour
):
2141 global cv_view_verts
, cv_view_colours
2144 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2146 cv_view_verts
+= [p0
]
2147 cv_view_verts
+= [h0
]
2148 cv_view_colours
+= [colour
,colour
]
2152 # Draw a bezier curve (at fixed resolution 10)
2154 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2156 global cv_view_verts
, cv_view_colours
2166 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2168 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2169 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2170 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2177 # I think this one extends the handles of the bezier otwards......
2179 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2181 global cv_view_course_i
2183 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2185 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2186 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2187 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2188 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2190 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2194 # Flush the lines buffers. This is called often because god help you if you want
2195 # to do fixed, fast buffers in this catastrophic programming language.
2197 def cv_draw_lines():
2199 global cv_view_shader
, cv_view_verts
, cv_view_colours
2201 if len(cv_view_verts
) < 2:
2204 lines
= batch_for_shader(\
2205 cv_view_shader
, 'LINES', \
2206 { "pos":cv_view_verts
, "color":cv_view_colours
})
2208 lines
.draw( cv_view_shader
)
2211 cv_view_colours
= []
2214 # I dont remember what this does exactly
2216 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2218 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2221 # Semi circle to show the limit. and some lines
2223 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2225 global cv_view_verts
, cv_view_colours
2234 a0
= amin
*(1.0-t0
)+amax
*t0
2235 a1
= amin
*(1.0-t1
)+amax
*t1
2237 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2238 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2240 p0
=obj
.matrix_world
@ p0
2241 p1
=obj
.matrix_world
@ p1
2242 cv_view_verts
+= [p0
,p1
]
2243 cv_view_colours
+= [colour
,colour
]
2247 cv_view_verts
+= [p0
,center
]
2248 cv_view_colours
+= [colour
,colour
]
2252 cv_view_verts
+= [p1
,center
]
2253 cv_view_colours
+= [colour
,colour
]
2257 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2258 cv_view_colours
+= [colour
,colour
]
2263 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
2265 def draw_skeleton_helpers( obj
):
2267 global cv_view_verts
, cv_view_colours
2269 for bone
in obj
.data
.bones
:
2271 if bone
.cv_data
.collider
and (obj
.data
.pose_position
== 'REST'):
2278 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
2279 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
2280 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
2281 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
2282 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
2283 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
2284 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
2285 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
2287 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2288 (0,4),(1,5),(2,6),(3,7)]
2295 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2296 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2297 cv_view_colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
2300 center
= obj
.matrix_world
@ c
2301 if bone
.cv_data
.con0
:
2303 draw_limit( obj
, c
, Vector((0,1,0)),Vector((0,0,1)), \
2304 bone
.cv_data
.mins
[0], bone
.cv_data
.maxs
[0], \
2306 draw_limit( obj
, c
, Vector((0,0,1)),Vector((1,0,0)), \
2307 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
2309 draw_limit( obj
, c
, Vector((1,0,0)),Vector((0,1,0)), \
2310 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
2319 global cv_view_shader
2320 global cv_view_verts
2321 global cv_view_colours
2322 global cv_view_course_i
2324 cv_view_course_i
= 0
2326 cv_view_colours
= []
2328 cv_view_shader
.bind()
2329 gpu
.state
.depth_mask_set(False)
2330 gpu
.state
.line_width_set(2.0)
2331 gpu
.state
.face_culling_set('BACK')
2332 gpu
.state
.depth_test_set('LESS')
2333 gpu
.state
.blend_set('NONE')
2335 for obj
in bpy
.context
.collection
.objects
:
2337 if obj
.type == 'ARMATURE':
2339 if obj
.data
.pose_position
== 'REST':
2340 draw_skeleton_helpers( obj
)
2344 classtype
= obj
.cv_data
.classtype
2345 if (classtype
!= 'classtype_none') and (classtype
in globals()):
2347 cl
= globals()[ classtype
]
2349 if getattr( cl
, "draw_scene_helpers", None ):
2351 cl
.draw_scene_helpers( obj
)
2362 # ---------------------------------------------------------------------------- #
2366 # ---------------------------------------------------------------------------- #
2368 # Checks whether this object has a classtype assigned. we can only target other
2370 def cv_poll_target(scene
, obj
):
2372 if obj
== bpy
.context
.active_object
:
2374 if obj
.cv_data
.classtype
== 'classtype_none':
2380 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
2382 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
2383 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
2384 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
2385 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
2388 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
2390 uid
: bpy
.props
.IntProperty( name
="" )
2392 strp
: bpy
.props
.StringProperty( name
="strp" )
2393 intp
: bpy
.props
.IntProperty( name
="intp" )
2394 fltp
: bpy
.props
.FloatProperty( name
="fltp" )
2395 bp0
: bpy
.props
.BoolProperty( name
="bp0" )
2396 bp1
: bpy
.props
.BoolProperty( name
="bp1" )
2397 bp2
: bpy
.props
.BoolProperty( name
="bp2" )
2398 bp3
: bpy
.props
.BoolProperty( name
="bp3" )
2400 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
2401 poll
=cv_poll_target
)
2402 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
2403 poll
=cv_poll_target
)
2404 target2
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target2", \
2405 poll
=cv_poll_target
)
2406 target3
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target3", \
2407 poll
=cv_poll_target
)
2409 colour
: bpy
.props
.FloatVectorProperty( name
="colour",subtype
='COLOR',\
2412 classtype
: bpy
.props
.EnumProperty(
2415 ('classtype_none', "classtype_none", "", 0),
2416 ('classtype_gate', "classtype_gate", "", 1),
2417 ('classtype_spawn', "classtype_spawn", "", 3),
2418 ('classtype_water', "classtype_water", "", 4),
2419 ('classtype_route_node', "classtype_route_node", "", 8 ),
2420 ('classtype_route', "classtype_route", "", 9 ),
2421 ('classtype_audio',"classtype_audio","",14),
2422 ('classtype_trigger',"classtype_trigger","",100),
2423 ('classtype_logic_achievement',"classtype_logic_achievement","",101),
2424 ('classtype_logic_relay',"classtype_logic_relay","",102),
2425 ('classtype_spawn_link',"classtype_spawn_link","",150),
2429 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
2431 collider
: bpy
.props
.BoolProperty(name
="Collider",default
=False)
2432 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
2433 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
2435 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
2436 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
2437 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
2440 class CV_BONE_PANEL(bpy
.types
.Panel
):
2442 bl_label
="Bone Config"
2443 bl_idname
="SCENE_PT_cv_bone"
2444 bl_space_type
='PROPERTIES'
2445 bl_region_type
='WINDOW'
2448 def draw(_
,context
):
2450 active_object
= context
.active_object
2451 if active_object
== None: return
2453 bone
= active_object
.data
.bones
.active
2454 if bone
== None: return
2456 _
.layout
.prop( bone
.cv_data
, "collider" )
2457 _
.layout
.prop( bone
.cv_data
, "v0" )
2458 _
.layout
.prop( bone
.cv_data
, "v1" )
2460 _
.layout
.label( text
="Angle Limits" )
2461 _
.layout
.prop( bone
.cv_data
, "con0" )
2462 _
.layout
.prop( bone
.cv_data
, "mins" )
2463 _
.layout
.prop( bone
.cv_data
, "maxs" )
2467 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
2469 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
2470 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
2473 class CV_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
2475 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
2478 class CV_MATERIAL_SETTINGS(bpy
.types
.PropertyGroup
):
2480 shader
: bpy
.props
.EnumProperty(
2483 ('standard',"standard","",0),
2484 ('standard_cutout', "standard_cutout", "", 1),
2485 ('terrain_blend', "terrain_blend", "", 2),
2486 ('vertex_blend', "vertex_blend", "", 3),
2487 ('water',"water","",4),
2490 surface_prop
: bpy
.props
.EnumProperty(
2491 name
="Surface Property",
2493 ('concrete','concrete','',0),
2494 ('wood','wood','',1),
2495 ('grass','grass','',2)
2498 collision
: bpy
.props
.BoolProperty( \
2499 name
="Collisions Enabled",\
2501 description
= "Can the player collide with this material"\
2503 skate_surface
: bpy
.props
.BoolProperty( \
2504 name
="Skate Surface", \
2506 description
= "Should the game try to target this surface?" \
2508 grind_surface
: bpy
.props
.BoolProperty( \
2509 name
="Grind Surface", \
2511 description
= "Grind face?" \
2513 grow_grass
: bpy
.props
.BoolProperty( \
2514 name
="Grow Grass", \
2516 description
= "Spawn grass sprites on this surface?" \
2518 blend_offset
: bpy
.props
.FloatVectorProperty( \
2519 name
="Blend Offset", \
2521 default
=Vector((0.5,0.0)),\
2522 description
="When surface is more than 45 degrees, add this vector " +\
2525 sand_colour
: bpy
.props
.FloatVectorProperty( \
2526 name
="Sand Colour",\
2529 default
=Vector((0.79,0.63,0.48)),\
2530 description
="Blend to this colour near the 0 coordinate on UP axis"\
2532 shore_colour
: bpy
.props
.FloatVectorProperty( \
2533 name
="Shore Colour",\
2536 default
=Vector((0.03,0.32,0.61)),\
2537 description
="Water colour at the shoreline"\
2539 ocean_colour
: bpy
.props
.FloatVectorProperty( \
2540 name
="Ocean Colour",\
2543 default
=Vector((0.0,0.006,0.03)),\
2544 description
="Water colour in the deep bits"\
2548 class CV_MATERIAL_PANEL(bpy
.types
.Panel
):
2550 bl_label
="Skate Rift material"
2551 bl_idname
="MATERIAL_PT_cv_material"
2552 bl_space_type
='PROPERTIES'
2553 bl_region_type
='WINDOW'
2554 bl_context
="material"
2556 def draw(_
,context
):
2558 active_object
= bpy
.context
.active_object
2559 if active_object
== None: return
2560 active_mat
= active_object
.active_material
2561 if active_mat
== None: return
2563 info
= material_info( active_mat
)
2565 if 'tex_diffuse' in info
:
2567 _
.layout
.label( icon
='INFO', \
2568 text
=F
"{info['tex_diffuse'].name} will be compiled" )
2571 _
.layout
.prop( active_mat
.cv_data
, "shader" )
2572 _
.layout
.prop( active_mat
.cv_data
, "surface_prop" )
2573 _
.layout
.prop( active_mat
.cv_data
, "collision" )
2575 if active_mat
.cv_data
.collision
:
2576 _
.layout
.prop( active_mat
.cv_data
, "skate_surface" )
2577 _
.layout
.prop( active_mat
.cv_data
, "grind_surface" )
2578 _
.layout
.prop( active_mat
.cv_data
, "grow_grass" )
2580 if active_mat
.cv_data
.shader
== "terrain_blend":
2582 box
= _
.layout
.box()
2583 box
.prop( active_mat
.cv_data
, "blend_offset" )
2584 box
.prop( active_mat
.cv_data
, "sand_colour" )
2586 elif active_mat
.cv_data
.shader
== "vertex_blend":
2588 box
= _
.layout
.box()
2589 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
2590 box
.prop( active_mat
.cv_data
, "blend_offset" )
2592 elif active_mat
.cv_data
.shader
== "water":
2594 box
= _
.layout
.box()
2595 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
2596 box
.prop( active_mat
.cv_data
, "shore_colour" )
2597 box
.prop( active_mat
.cv_data
, "ocean_colour" )
2602 class CV_OBJ_PANEL(bpy
.types
.Panel
):
2604 bl_label
="Entity Config"
2605 bl_idname
="SCENE_PT_cv_entity"
2606 bl_space_type
='PROPERTIES'
2607 bl_region_type
='WINDOW'
2610 def draw(_
,context
):
2612 active_object
= bpy
.context
.active_object
2613 if active_object
== None: return
2614 if active_object
.type == 'ARMATURE':
2616 row
= _
.layout
.row()
2618 row
.label( text
="This object has the intrinsic classtype of skeleton" )
2622 _
.layout
.prop( active_object
.cv_data
, "classtype" )
2624 classtype
= active_object
.cv_data
.classtype
2626 if (classtype
!= 'classtype_none') and (classtype
in globals()):
2628 cl
= globals()[ classtype
]
2630 if getattr( cl
, "editor_interface", None ):
2632 cl
.editor_interface( _
.layout
, active_object
)
2638 class CV_COMPILE(bpy
.types
.Operator
):
2640 bl_idname
="carve.compile_all"
2641 bl_label
="Compile All"
2643 def execute(_
,context
):
2645 view_layer
= bpy
.context
.view_layer
2646 for col
in view_layer
.layer_collection
.children
["export"].children
:
2647 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
2648 write_model( col
.name
)
2654 class CV_COMPILE_THIS(bpy
.types
.Operator
):
2656 bl_idname
="carve.compile_this"
2657 bl_label
="Compile This collection"
2659 def execute(_
,context
):
2661 col
= bpy
.context
.collection
2662 write_model( col
.name
)
2668 class CV_INTERFACE(bpy
.types
.Panel
):
2670 bl_idname
= "VIEW3D_PT_carve"
2671 bl_label
= "Skate Rift"
2672 bl_space_type
= 'VIEW_3D'
2673 bl_region_type
= 'UI'
2674 bl_category
= "Skate Rift"
2676 def draw(_
, context
):
2679 layout
.prop( context
.scene
.cv_data
, "export_dir" )
2681 col
= bpy
.context
.collection
2683 found_in_export
= False
2685 view_layer
= bpy
.context
.view_layer
2686 for c1
in view_layer
.layer_collection
.children
["export"].children
:
2688 if not c1
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
2691 if c1
.name
== col
.name
:
2693 found_in_export
= True
2700 box
.label( text
=col
.name
+ ".mdl" )
2701 box
.prop( col
.cv_data
, "pack_textures" )
2702 box
.operator( "carve.compile_this" )
2708 row
.label( text
=col
.name
)
2709 box
.label( text
="This collection is not in the export group" )
2715 split
= row
.split( factor
= 0.3, align
=True )
2716 split
.prop( context
.scene
.cv_data
, "use_hidden", text
="hidden" )
2719 if export_count
== 0:
2721 row1
.operator( "carve.compile_all", \
2722 text
=F
"Compile all ({export_count} collections)" )
2727 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
2728 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
2729 CV_BONE_PANEL
, CV_COLLECTION_SETTINGS
, CV_COMPILE_THIS
,\
2730 CV_MATERIAL_SETTINGS
, CV_MATERIAL_PANEL
]
2734 global cv_view_draw_handler
2737 bpy
.utils
.register_class(c
)
2739 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
2740 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
2741 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
2742 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
2743 bpy
.types
.Collection
.cv_data
= \
2744 bpy
.props
.PointerProperty(type=CV_COLLECTION_SETTINGS
)
2745 bpy
.types
.Material
.cv_data
= \
2746 bpy
.props
.PointerProperty(type=CV_MATERIAL_SETTINGS
)
2748 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
2749 cv_draw
,(),'WINDOW','POST_VIEW')
2754 global cv_view_draw_handler
2757 bpy
.utils
.unregister_class(c
)
2759 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
2762 # ---------------------------------------------------------------------------- #
2766 # ---------------------------------------------------------------------------- #
2768 # Transliteration of: #
2769 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
2771 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
2772 # SPDX-License-Identifier: MIT #
2773 # QOI - The "Quite OK Image" format for fast, lossless image compression #
2775 # ---------------------------------------------------------------------------- #
2777 class qoi_rgba_t(Structure
):
2780 _fields_
= [("r",c_uint8
),
2786 QOI_OP_INDEX
= 0x00 # 00xxxxxx
2787 QOI_OP_DIFF
= 0x40 # 01xxxxxx
2788 QOI_OP_LUMA
= 0x80 # 10xxxxxx
2789 QOI_OP_RUN
= 0xc0 # 11xxxxxx
2790 QOI_OP_RGB
= 0xfe # 11111110
2791 QOI_OP_RGBA
= 0xff # 11111111
2793 QOI_MASK_2
= 0xc0 # 11000000
2795 def qoi_colour_hash( c
):
2797 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
2802 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
2807 return bytearray([ (0xff000000 & v
) >> 24, \
2808 (0x00ff0000 & v
) >> 16, \
2809 (0x0000ff00 & v
) >> 8, \
2813 def qoi_encode( img
):
2817 print(F
" . Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]")
2819 index
= [ qoi_rgba_t() for _
in range(64) ]
2823 data
.extend( bytearray(c_uint32(0x66696f71)) )
2824 data
.extend( qoi_32bit( img
.size
[0] ) )
2825 data
.extend( qoi_32bit( img
.size
[1] ) )
2826 data
.extend( bytearray(c_uint8(4)) )
2827 data
.extend( bytearray(c_uint8(0)) )
2830 px_prev
= qoi_rgba_t()
2831 px_prev
.r
= c_uint8(0)
2832 px_prev
.g
= c_uint8(0)
2833 px_prev
.b
= c_uint8(0)
2834 px_prev
.a
= c_uint8(255)
2842 px_len
= img
.size
[0] * img
.size
[1]
2844 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
2846 for px_pos
in range( px_len
):
2848 idx
= px_pos
* img
.channels
2851 px
.r
= paxels
[idx
+min(0,nc
)]
2852 px
.g
= paxels
[idx
+min(1,nc
)]
2853 px
.b
= paxels
[idx
+min(2,nc
)]
2854 px
.a
= paxels
[idx
+min(3,nc
)]
2856 if qoi_eq( px
, px_prev
):
2860 if (run
== 62) or (px_pos
== px_len
-1):
2862 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2870 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2874 index_pos
= qoi_colour_hash(px
) % 64
2876 if qoi_eq( index
[index_pos
], px
):
2878 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
2882 index
[ index_pos
].r
= px
.r
2883 index
[ index_pos
].g
= px
.g
2884 index
[ index_pos
].b
= px
.b
2885 index
[ index_pos
].a
= px
.a
2887 if px
.a
== px_prev
.a
:
2889 vr
= int(px
.r
) - int(px_prev
.r
)
2890 vg
= int(px
.g
) - int(px_prev
.g
)
2891 vb
= int(px
.b
) - int(px_prev
.b
)
2896 if (vr
> -3) and (vr
< 2) and\
2897 (vg
> -3) and (vg
< 2) and\
2898 (vb
> -3) and (vb
< 2):
2900 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
2901 data
.extend( bytearray( c_uint8(op
) ))
2903 elif (vg_r
> -9) and (vg_r
< 8) and\
2904 (vg
> -33) and (vg
< 32 ) and\
2905 (vg_b
> -9) and (vg_b
< 8):
2907 op
= QOI_OP_LUMA |
(vg
+32)
2908 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
2909 data
.extend( bytearray( c_uint8(op
) ) )
2910 data
.extend( bytearray( c_uint8(delta
) ))
2914 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
2915 data
.extend( bytearray( c_uint8(px
.r
) ))
2916 data
.extend( bytearray( c_uint8(px
.g
) ))
2917 data
.extend( bytearray( c_uint8(px
.b
) ))
2922 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
2923 data
.extend( bytearray( c_uint8(px
.r
) ))
2924 data
.extend( bytearray( c_uint8(px
.g
) ))
2925 data
.extend( bytearray( c_uint8(px
.b
) ))
2926 data
.extend( bytearray( c_uint8(px
.a
) ))
2939 data
.extend( bytearray( c_uint8(0) ))
2940 data
.extend( bytearray( c_uint8(1) ))
2941 bytearray_align_to( data
, 16, 0 )