Skip to content

Commit

Permalink
removed unnecessary weight dividing, some code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-schelkov committed Nov 18, 2022
1 parent 8294a40 commit ebe517b
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions models_converter/formats/gltf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def parse_node(self, gltf_node: Node, parent: str = None):
parent=parent
)

# Transform
translation = gltf_node.translation
scale = gltf_node.scale
rotation = gltf_node.rotation

instance = None
if gltf_node.mesh is not None and type(self.gltf.meshes) is list:
mesh = self.gltf.meshes[gltf_node.mesh]
Expand All @@ -158,21 +163,17 @@ def parse_node(self, gltf_node: Node, parent: str = None):
if gltf_node.skin is not None:
instance = universal.Node.Instance(name=geometry.get_name(), instance_type='CONT')

geometry.set_controller_bind_matrix([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
geometry.set_controller_bind_matrix([
1, 0, 0, translation.x,
0, 1, 0, translation.y,
0, 0, 1, translation.z,
0, 0, 0, 1
])

skin_id = gltf_node.skin
skin = self.gltf.skins[skin_id]
bind_matrices = self.accessors[skin.inverse_bind_matrices]
bind_matrices = [[m[0::4], m[1::4], m[2::4], m[3::4]] for m in bind_matrices]
for matrix_index in range(len(bind_matrices)):
m = bind_matrices[matrix_index]

matrix = m[0]
matrix.extend(m[1])
matrix.extend(m[2])
matrix.extend(m[3])

bind_matrices[matrix_index] = matrix
bind_matrices = [[*m[0::4], *m[1::4], *m[2::4], *m[3::4]] for m in bind_matrices]

for joint in skin.joints:
joint_index = skin['joints'].index(joint)
Expand All @@ -196,9 +197,12 @@ def parse_node(self, gltf_node: Node, parent: str = None):
polygons_id = primitive.indices

triangles = self.accessors[polygons_id]
material = self.gltf.materials[material_id]

material_name = material.extensions['SC_shader']['name']
material_name = f'{name}_material'
if primitive.material is not None:
material = self.gltf.materials[material_id]
if 'SC_shader' in material.extensions:
material_name = material.extensions['SC_shader']['name']
instance.add_bind(material_name, material_name)

position = []
Expand All @@ -215,9 +219,9 @@ def parse_node(self, gltf_node: Node, parent: str = None):
position = self.accessors[attribute]
points = list(map(
lambda point: (
point[0] * gltf_node.scale.x + gltf_node.translation.x,
point[1] * gltf_node.scale.y + gltf_node.translation.y,
point[2] * gltf_node.scale.z + gltf_node.translation.z
point[0] * scale.x,
point[1] * scale.y,
point[2] * scale.z
),
position
))
Expand All @@ -234,19 +238,19 @@ def parse_node(self, gltf_node: Node, parent: str = None):
elif attribute_id.startswith('TEXCOORD'):
texcoord = self.accessors[attribute]

texcoord = [[item[0], 1 - item[1]] for item in texcoord]
attribute_id = 'TEXCOORD'
points = texcoord
# TODO: look how to resize it this
points = [[item[0], 1 - item[1]] for item in texcoord]
elif attribute_id.startswith('JOINTS'):
joint_ids = self.accessors[attribute]
elif attribute_id.startswith('WEIGHTS'):
weights = self.accessors[attribute]

for x in range(len(joint_ids)):
geometry.add_weight(Geometry.Weight(joint_ids[x][0], weights[x][0] / 255))
geometry.add_weight(Geometry.Weight(joint_ids[x][1], weights[x][1] / 255))
geometry.add_weight(Geometry.Weight(joint_ids[x][2], weights[x][2] / 255))
geometry.add_weight(Geometry.Weight(joint_ids[x][3], weights[x][3] / 255))
geometry.add_weight(Geometry.Weight(joint_ids[x][0], weights[x][0]))
geometry.add_weight(Geometry.Weight(joint_ids[x][1], weights[x][1]))
geometry.add_weight(Geometry.Weight(joint_ids[x][2], weights[x][2]))
geometry.add_weight(Geometry.Weight(joint_ids[x][3], weights[x][3]))

if points:
geometry.add_vertex(Geometry.Vertex(
Expand Down Expand Up @@ -282,15 +286,15 @@ def parse_node(self, gltf_node: Node, parent: str = None):
if instance is not None:
node.add_instance(instance)

self.scene.add_node(node)

node.add_frame(universal.Node.Frame(
0,
gltf_node.translation,
gltf_node.scale,
gltf_node.rotation
translation,
scale,
rotation
))

self.scene.add_node(node)

if gltf_node.children:
for child_id in gltf_node.children:
child = self.gltf.nodes[child_id]
Expand Down

0 comments on commit ebe517b

Please sign in to comment.