Skip to content
Snippets Groups Projects
Commit f5f442a7 authored by Julien Duroure's avatar Julien Duroure
Browse files

glTF importer: performance improvement importing meshes & normals

parent 75855d72
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
bl_info = { bl_info = {
'name': 'glTF 2.0 format', 'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors', 'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (1, 2, 5), "version": (1, 2, 6),
'blender': (2, 81, 6), 'blender': (2, 81, 6),
'location': 'File > Import-Export', 'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0', 'description': 'Import-Export as glTF 2.0',
......
...@@ -124,19 +124,14 @@ class BlenderMesh(): ...@@ -124,19 +124,14 @@ class BlenderMesh():
if gltf.import_settings['import_shading'] == "NORMALS": if gltf.import_settings['import_shading'] == "NORMALS":
mesh.create_normals_split() mesh.create_normals_split()
# use_smooth for faces use_smooths = [] # whether to smooth for each poly
face_idx = 0 face_idx = 0
for prim in pymesh.primitives: for prim in pymesh.primitives:
if 'NORMAL' not in prim.attributes: if gltf.import_settings['import_shading'] == "FLAT" or \
face_idx += prim.num_faces 'NORMAL' not in prim.attributes:
continue use_smooths += [False] * prim.num_faces
if gltf.import_settings['import_shading'] == "FLAT":
for fi in range(face_idx, face_idx + prim.num_faces):
mesh.polygons[fi].use_smooth = False
elif gltf.import_settings['import_shading'] == "SMOOTH": elif gltf.import_settings['import_shading'] == "SMOOTH":
for fi in range(face_idx, face_idx + prim.num_faces): use_smooths += [True] * prim.num_faces
mesh.polygons[fi].use_smooth = True
elif gltf.import_settings['import_shading'] == "NORMALS": elif gltf.import_settings['import_shading'] == "NORMALS":
mesh_loops = mesh.loops mesh_loops = mesh.loops
for fi in range(face_idx, face_idx + prim.num_faces): for fi in range(face_idx, face_idx + prim.num_faces):
...@@ -146,14 +141,16 @@ class BlenderMesh(): ...@@ -146,14 +141,16 @@ class BlenderMesh():
for loop_idx in range(poly.loop_start, poly.loop_start + poly.loop_total): for loop_idx in range(poly.loop_start, poly.loop_start + poly.loop_total):
vi = mesh_loops[loop_idx].vertex_index vi = mesh_loops[loop_idx].vertex_index
if poly.normal.dot(bme.verts[vi].normal) <= 0.9999999: if poly.normal.dot(bme.verts[vi].normal) <= 0.9999999:
poly.use_smooth = True use_smooths.append(True)
break break
else:
use_smooths.append(False)
else: else:
# shouldn't happen # shouldn't happen
pass assert False
face_idx += prim.num_faces face_idx += prim.num_faces
mesh.polygons.foreach_set('use_smooth', use_smooths)
# Custom normals, now that every update is done # Custom normals, now that every update is done
if gltf.import_settings['import_shading'] == "NORMALS": if gltf.import_settings['import_shading'] == "NORMALS":
......
...@@ -155,9 +155,10 @@ class BlenderPrimitive(): ...@@ -155,9 +155,10 @@ class BlenderPrimitive():
pyprimitive.num_faces = 0 pyprimitive.num_faces = 0
for face in faces: for face in faces:
try: try:
face = bme_faces.new(tuple( face = bme_faces.new((
bme_verts[pidx_to_bidx[i]] bme_verts[pidx_to_bidx[face[0]]],
for i in face bme_verts[pidx_to_bidx[face[1]]],
bme_verts[pidx_to_bidx[face[2]]],
)) ))
if material_index is not None: if material_index is not None:
...@@ -204,15 +205,15 @@ class BlenderPrimitive(): ...@@ -204,15 +205,15 @@ class BlenderPrimitive():
) )
for bidx, pidx in vert_idxs: for bidx, pidx in vert_idxs:
color = colors[pidx]
col = (
color_linear_to_srgb(color[0]),
color_linear_to_srgb(color[1]),
color_linear_to_srgb(color[2]),
color[3] if is_rgba else 1.0,
)[:blender_num_components]
for loop in bme_verts[bidx].link_loops: for loop in bme_verts[bidx].link_loops:
color = colors[pidx] loop[layer] = col
col = (
color_linear_to_srgb(color[0]),
color_linear_to_srgb(color[1]),
color_linear_to_srgb(color[2]),
color[3] if is_rgba else 1.0,
)
loop[layer] = col[:blender_num_components]
set_num += 1 set_num += 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment