Skip to content

Commit

Permalink
Improved exported bounding boxes, still not perfect but definitely re…
Browse files Browse the repository at this point in the history
…asonable
  • Loading branch information
Five-Damned-Dollarz committed Feb 16, 2021
1 parent 1c7fbaa commit d5fd3b9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def __init__(self):
self.child_count = 0

# Version 6 specific
self.bounds_min=Vector()
self.bounds_max=Vector()

self.md_vert_count = 0
self.md_vert_list = []

Expand Down Expand Up @@ -200,7 +203,8 @@ def __init__(self):
self.node_keyframe_transforms = []

# Version 6 specific

self.bounds_min=Vector()
self.bounds_max=Vector()
# Note this should line up with md_vert_list, and go on for md_vert_count * keyframe_count
# List of 3 chars (verts)
self.vertex_deformations = []
Expand Down
14 changes: 12 additions & 2 deletions src/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ def from_armature(armature_object):

node.bind_matrix = matrix

# ABC v6 specific
# FIXME: disgusting, can this be done better?
for piece in model.pieces:
for lod in piece.lods:
for vertex in lod.vertices:
if vertex.weights[0].node_index==bone_index:
node.bounds_min=Vector((min(node.bounds_min.x, vertex.location.x), min(node.bounds_min.y, vertex.location.y), min(node.bounds_min.z, vertex.location.z)))
node.bounds_max=Vector((max(node.bounds_min.x, vertex.location.x), max(node.bounds_min.y, vertex.location.y), max(node.bounds_min.z, vertex.location.z)))

#print("Processed", node.name, node.bind_matrix)
node.child_count = len(bone.children)
model.nodes.append(node)
Expand Down Expand Up @@ -189,7 +198,8 @@ def from_armature(armature_object):
print("Processing animation %s" % action.name)
animation = Animation()
animation.name = action.name
animation.extents = mesh_object.dimensions # TODO: actually calculate the bounds of each animation; mesh/armature_object.bound_box[0]/[-1] maybe?
animation.bounds_min=Vector(mesh_object.bound_box[0]) # will using mesh_object here break if there's multiple mesh
animation.bounds_max=Vector(mesh_object.bound_box[6]) # objects using the same armature as a parent? DON'T DO THAT

armature_object.animation_data.action = action

Expand Down Expand Up @@ -284,7 +294,7 @@ def from_armature(armature_object):
''' AnimBindings '''
anim_binding = AnimBinding()
anim_binding.name = 'base'
animation.extents = Vector((10, 10, 10))
anim_binding.extents = Vector((10, 10, 10))
model.anim_bindings.append(anim_binding)

return model
10 changes: 5 additions & 5 deletions src/reader_abc_v6_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def _read_node(self, f):
node = Node()

# These may be needed to calculate the position...
bounds_min = self._read_vector(f)
bounds_max = self._read_vector(f)
node.bounds_min = self._read_vector(f)
node.bounds_max = self._read_vector(f)

# Bind matrix is set after we read in animations!

Expand Down Expand Up @@ -204,11 +204,11 @@ def _read_animation(self, f):
animation = Animation()
animation.name = self._read_string(f)
animation_length = unpack('I', f)[0]
bounds_min = self._read_vector(f)
bounds_max = self._read_vector(f)
animation.bounds_min = self._read_vector(f)
animation.bounds_max = self._read_vector(f)

# ?
animation.extents = bounds_max
animation.extents = animation.bounds_max

animation.keyframe_count = unpack('I', f)[0]
animation.keyframes = [self._read_keyframe(f) for _ in range(animation.keyframe_count)]
Expand Down
16 changes: 8 additions & 8 deletions src/writer_abc_v6_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, name, data):
normal=vertex.normal.normalized()*127
buffer.extend(struct.pack('3b', int(normal.x), int(-normal.y), int(normal.z)))
buffer.extend(struct.pack('B', vertex.weights[0].node_index)) # TODO: error out on more than a single weight?
buffer.extend(struct.pack('2H', 0, 0)) # TODO: lod related, I think?
buffer.extend(struct.pack('2H', 0, 0)) # TODO: lod related, I think?

sections.append(Section('Geometry', bytes(buffer)))

Expand All @@ -114,8 +114,8 @@ def __init__(self, name, data):
node.flags=self._flag_tris
break

buffer.extend(self._vector_to_bytes(Vector((-10, -10, -10)))) # TODO: min bounds
buffer.extend(self._vector_to_bytes(Vector((10, 10, 10)))) # TODO: max bounds
buffer.extend(self._vector_to_bytes(node.bounds_min))
buffer.extend(self._vector_to_bytes(node.bounds_max))
buffer.extend(self._string_to_bytes(node.name))
buffer.extend(struct.pack('H', node.index))
buffer.extend(struct.pack('B', node.flags))
Expand All @@ -133,13 +133,13 @@ def __init__(self, name, data):
for anim in model.animations:
buffer.extend(self._string_to_bytes(anim.name))
buffer.extend(struct.pack('I', int(anim.keyframes[-1].time))) # final keyframe time; playing past final keyframe's time seems unpredictable
buffer.extend(self._vector_to_bytes(-anim.extents/2)) # TODO: min bounds
buffer.extend(self._vector_to_bytes(anim.extents/2)) # TODO: max bounds
buffer.extend(self._vector_to_bytes(anim.bounds_min))
buffer.extend(self._vector_to_bytes(anim.bounds_max))
buffer.extend(struct.pack('I', len(anim.keyframes)))
for keyframe in anim.keyframes:
buffer.extend(struct.pack('I', int(keyframe.time)))
buffer.extend(self._vector_to_bytes(-anim.extents/2)) # TODO: min bounds
buffer.extend(self._vector_to_bytes(anim.extents/2)) # TODO: max bounds
buffer.extend(self._vector_to_bytes(anim.bounds_min)) # TODO: actual keyframe bounding boxes
buffer.extend(self._vector_to_bytes(anim.bounds_max))
buffer.extend(self._string_to_bytes(keyframe.string))

for node_transform_list in anim.node_keyframe_transforms:
Expand All @@ -159,7 +159,7 @@ def __init__(self, name, data):
buffer=bytearray()

for anim in model.animations:
buffer.extend(self._vector_to_bytes(anim.extents))
buffer.extend(self._vector_to_bytes(-anim.bounds_min+anim.bounds_max))

sections.append(Section('AnimDims', bytes(buffer)))

Expand Down

0 comments on commit d5fd3b9

Please sign in to comment.