Skip to content

Commit

Permalink
ms2 - improve MeshCollision import
Browse files Browse the repository at this point in the history
  • Loading branch information
HENDRIX-ZT2 committed Nov 11, 2023
1 parent f40bc5d commit 2a686af
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
35 changes: 20 additions & 15 deletions generated/formats/ms2/compounds/MeshCollisionChunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ def __init__(self, context, arg=0, template=None, set_default=True):
super().__init__(context, arg, template, set_default=False)

# ?
self.a = Array(self.context, 0, None, (0,), name_type_map['Ushort'])
self.a = Array(self.context, 0, None, (0,), name_type_map['Short'])

# incrementing by 16 from 0, or if around 32769: incrementing by 1, mostly
self.b = Array(self.context, 0, None, (0,), name_type_map['Ushort'])
# start indices of a group of 16 tris;incrementing by 16 from 0, or if around -32769: incrementing by 1, mostly
self.tri_indices = Array(self.context, 0, None, (0,), name_type_map['Short'])

# usually, but not always the first value
self.min_of_b = name_type_map['Ushort'](self.context, 0, None)
# usually, but not always the first tri index; 0 if tri_indices are negative
self.min_of_indices = name_type_map['Short'](self.context, 0, None)

# counts how many slots in b are used; JWE2: 4, 8
self.num_used_b_slots = name_type_map['Ushort'](self.context, 0, None)
# counts how many slots in tri_indices are used, others are 0
self.num_used_tri_slots = name_type_map['Ubyte'](self.context, 0, None)

# likely
self.salt_index = name_type_map['Ubyte'](self.context, 0, None)

# always 2954754766?
self.consts = Array(self.context, 0, None, (0,), name_type_map['Uint'])
Expand All @@ -31,17 +34,19 @@ def __init__(self, context, arg=0, template=None, set_default=True):
@classmethod
def _get_attribute_list(cls):
yield from super()._get_attribute_list()
yield 'a', Array, (0, None, (24,), name_type_map['Ushort']), (False, None), (None, None)
yield 'b', Array, (0, None, (8,), name_type_map['Ushort']), (False, None), (None, None)
yield 'min_of_b', name_type_map['Ushort'], (0, None), (False, None), (None, None)
yield 'num_used_b_slots', name_type_map['Ushort'], (0, None), (False, None), (None, None)
yield 'a', Array, (0, None, (24,), name_type_map['Short']), (False, None), (None, None)
yield 'tri_indices', Array, (0, None, (8,), name_type_map['Short']), (False, None), (None, None)
yield 'min_of_indices', name_type_map['Short'], (0, None), (False, None), (None, None)
yield 'num_used_tri_slots', name_type_map['Ubyte'], (0, None), (False, None), (None, None)
yield 'salt_index', name_type_map['Ubyte'], (0, None), (False, None), (None, None)
yield 'consts', Array, (0, None, (3,), name_type_map['Uint']), (False, 2954754766), (None, None)

@classmethod
def _get_filtered_attribute_list(cls, instance, include_abstract=True):
yield from super()._get_filtered_attribute_list(instance, include_abstract)
yield 'a', Array, (0, None, (24,), name_type_map['Ushort']), (False, None)
yield 'b', Array, (0, None, (8,), name_type_map['Ushort']), (False, None)
yield 'min_of_b', name_type_map['Ushort'], (0, None), (False, None)
yield 'num_used_b_slots', name_type_map['Ushort'], (0, None), (False, None)
yield 'a', Array, (0, None, (24,), name_type_map['Short']), (False, None)
yield 'tri_indices', Array, (0, None, (8,), name_type_map['Short']), (False, None)
yield 'min_of_indices', name_type_map['Short'], (0, None), (False, None)
yield 'num_used_tri_slots', name_type_map['Ubyte'], (0, None), (False, None)
yield 'salt_index', name_type_map['Ubyte'], (0, None), (False, None)
yield 'consts', Array, (0, None, (3,), name_type_map['Uint']), (False, 2954754766)
6 changes: 3 additions & 3 deletions generated/formats/ms2/compounds/MeshCollisionData.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MeshCollisionData(BaseStruct):

def __init__(self, context, arg=0, template=None, set_default=True):
super().__init__(context, arg, template, set_default=False)
self.mesh_collision_optimizer = name_type_map['MeshCollisionOptimizer'](self.context, 0, None)
self.optimizer = name_type_map['MeshCollisionOptimizer'](self.context, 0, None)
self.vertices_addr = name_type_map['Empty'](self.context, 0, None)

# array of vertices
Expand Down Expand Up @@ -38,7 +38,7 @@ def __init__(self, context, arg=0, template=None, set_default=True):
@classmethod
def _get_attribute_list(cls):
yield from super()._get_attribute_list()
yield 'mesh_collision_optimizer', name_type_map['MeshCollisionOptimizer'], (0, None), (False, None), (None, True)
yield 'optimizer', name_type_map['MeshCollisionOptimizer'], (0, None), (False, None), (None, True)
yield 'vertices_addr', name_type_map['Empty'], (0, None), (False, None), (None, None)
yield 'vertices', Array, (0, None, (None, 3,), name_type_map['Float']), (False, None), (None, None)
yield 'triangles_addr', name_type_map['Empty'], (0, None), (False, None), (None, None)
Expand All @@ -53,7 +53,7 @@ def _get_attribute_list(cls):
def _get_filtered_attribute_list(cls, instance, include_abstract=True):
yield from super()._get_filtered_attribute_list(instance, include_abstract)
if instance.arg.is_optimized:
yield 'mesh_collision_optimizer', name_type_map['MeshCollisionOptimizer'], (0, None), (False, None)
yield 'optimizer', name_type_map['MeshCollisionOptimizer'], (0, None), (False, None)
yield 'vertices_addr', name_type_map['Empty'], (0, None), (False, None)
yield 'vertices', Array, (0, None, (instance.arg.vertex_count, 3,), name_type_map['Float']), (False, None)
yield 'triangles_addr', name_type_map['Empty'], (0, None), (False, None)
Expand Down
11 changes: 6 additions & 5 deletions generated/formats/ms2/ms2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,11 @@
</compound>

<compound name="MeshCollisionChunk">
<add name="a" type="ushort" arr1="24">?</add>
<add name="b" type="ushort" arr1="8">incrementing by 16 from 0, or if around 32769: incrementing by 1, mostly</add>
<add name="min_of_b" type="ushort" >usually, but not always the first value</add>
<add name="num_used_b_slots" type="ushort" >counts how many slots in b are used; JWE2: 4, 8</add>
<add name="a" type="short" arr1="24">?</add>
<add name="tri_indices" type="short" arr1="8">start indices of a group of 16 tris;incrementing by 16 from 0, or if around -32769: incrementing by 1, mostly</add>
<add name="min_of_indices" type="short" >usually, but not always the first tri index; 0 if tri_indices are negative</add>
<add name="num_used_tri_slots" type="ubyte" >counts how many slots in tri_indices are used, others are 0</add>
<add name="salt_index" type="ubyte" >likely</add>
<add name="consts" type="uint" arr1="3" default="2954754766">always 2954754766?</add>
</compound>

Expand Down Expand Up @@ -876,7 +877,7 @@
</compound>

<compound name="MeshCollisionData">
<add name="MeshCollisionOptimizer" type="MeshCollisionOptimizer" cond="#ARG#\is_optimized"/>
<add name="optimizer" type="MeshCollisionOptimizer" cond="#ARG#\is_optimized"/>

<add name="vertices addr" type="Empty"/>
<add name="vertices" type="float" arr1="#ARG#\vertex count" arr2="3">array of vertices</add>
Expand Down
39 changes: 21 additions & 18 deletions plugin/modules_import/collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,27 @@ def import_meshbv(coll, hitcheck_name, corrector):
print(coll)
print(coll.data)
scene = bpy.context.scene
good_tris = []
for i, tri in enumerate(list(coll.data.triangles)):
# print(i, tri)
for v in tri:
if v >= len(coll.data.vertices):
print(f"{i} {tri} is bad")
break
else:
good_tris.append(tri)
continue
salt = coll.data.tris_salt[1]
unsalted = [t - salt for t in tri]
good_tris.append(unsalted)
print(f"unsalted {unsalted}")
print(len(good_tris), max([v for tri in good_tris for v in tri]))
# b_obj, b_me = mesh_from_data(scene, hitcheck_name, [unpack_swizzle(v) for v in coll.vertices], list(coll.triangles), coll_name="hitchecks")
b_obj, b_me = mesh_from_data(scene, hitcheck_name, [unpack_swizzle(v) for v in coll.data.vertices], good_tris, coll_name="hitchecks")
# b_obj, b_me = mesh_from_data(scene, hitcheck_name, [unpack_swizzle(v) for v in coll.vertices], [], coll_name="hitchecks")
tris = coll.data.triangles
if coll.is_optimized:
good_tris = []
optimizer = coll.data.optimizer
# unsalt the tri indices per chunk
for chunk in optimizer.chunks:
for tri_index in chunk.tri_indices[:chunk.num_used_tri_slots]:
salt = optimizer.tris_salt[chunk.salt_index]
if tri_index > -1:
raw_tris = tris[tri_index:tri_index+16]
raw_tris -= salt
good_tris.extend(raw_tris)
# verify the output
for i, tri in enumerate(good_tris):
for v in tri:
if v >= len(coll.data.vertices):
logging.warning(f"{i} {tri} is bad")
else:
# cast array to list for blender
good_tris = list(tris)
b_obj, b_me = mesh_from_data(scene, hitcheck_name, [unpack_swizzle_collision(v) for v in coll.data.vertices], good_tris, coll_name="hitchecks")
mat = import_collision_matrix(coll.rotation, corrector)
mat.translation = unpack_swizzle((coll.offset.x, coll.offset.y, coll.offset.z))
b_obj.matrix_local = mat
Expand Down
11 changes: 6 additions & 5 deletions source/formats/ms2/ms2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,11 @@
</compound>

<compound name="MeshCollisionChunk">
<add name="a" type="ushort" arr1="24">?</add>
<add name="b" type="ushort" arr1="8">incrementing by 16 from 0, or if around 32769: incrementing by 1, mostly</add>
<add name="min_of_b" type="ushort" >usually, but not always the first value</add>
<add name="num_used_b_slots" type="ushort" >counts how many slots in b are used; JWE2: 4, 8</add>
<add name="a" type="short" arr1="24">?</add>
<add name="tri_indices" type="short" arr1="8">start indices of a group of 16 tris;incrementing by 16 from 0, or if around -32769: incrementing by 1, mostly</add>
<add name="min_of_indices" type="short" >usually, but not always the first tri index; 0 if tri_indices are negative</add>
<add name="num_used_tri_slots" type="ubyte" >counts how many slots in tri_indices are used, others are 0</add>
<add name="salt_index" type="ubyte" >likely</add>
<add name="consts" type="uint" arr1="3" default="2954754766">always 2954754766?</add>
</compound>

Expand Down Expand Up @@ -876,7 +877,7 @@
</compound>

<compound name="MeshCollisionData">
<add name="MeshCollisionOptimizer" type="MeshCollisionOptimizer" cond="#ARG#\is_optimized"/>
<add name="optimizer" type="MeshCollisionOptimizer" cond="#ARG#\is_optimized"/>

<add name="vertices addr" type="Empty"/>
<add name="vertices" type="float" arr1="#ARG#\vertex count" arr2="3">array of vertices</add>
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1f2bd90d7 - Sat Nov 11 11:17:32 2023 +0100
f40bc5d2f - Sat Nov 11 11:32:11 2023 +0100

0 comments on commit 2a686af

Please sign in to comment.