From d8331cacd3cb30019c9a3ae848bf7af86926cac6 Mon Sep 17 00:00:00 2001 From: Senpai Date: Thu, 16 Jan 2025 22:58:04 -0600 Subject: [PATCH] Import and average the surface normals --- export_geo.py | 28 +++++++++++++++++----------- import_geo.py | 6 ++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/export_geo.py b/export_geo.py index 655bd62..eb8741c 100644 --- a/export_geo.py +++ b/export_geo.py @@ -4,10 +4,11 @@ from .bones import * import bpy.path import bpy import mathutils +from collections import defaultdict from bpy_extras.io_utils import axis_conversion -def convert_mesh(geo_model, mesh, obj): +def convert_mesh(geo_model, mesh, obj, scale): if bpy.app.version < (2, 80): # Be sure tessface & co are available! if not mesh.tessfaces and mesh.polygons: @@ -38,6 +39,14 @@ def convert_mesh(geo_model, mesh, obj): geomesh = GeoMesh() + vertex_normals = defaultdict(list) + for loop in mesh.loops: + vertex_normals[loop.vertex_index].append(loop.normal) + averaged_normals = { + vertex_index: sum(normals, mathutils.Vector()) / len(normals) + for vertex_index, normals in vertex_normals.items() + } + texture_name = "white.tga" if bpy.app.version < (2, 80): faces = mesh.tessfaces @@ -73,28 +82,29 @@ def convert_mesh(geo_model, mesh, obj): uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4] else: print("uv.uv: %s" % uv.uv) - uv = [active_uv_layer[l].uv[:] for l in f.loops] + uv = [active_uv_layer[l].uv[:] for l in (f.loops[0], f.loops[2], f.loops[1])] else: uv = [(0, 0)] * 4 texture_name = "white.tga" f_verts = f.vertices + f_verts = (f_verts[0], f_verts[2], f_verts[1]) verts = [] norms = [] groups = [] geoverts = [] for i, v_index in enumerate(f_verts): v = mesh_verts[v_index] - verts.append(v.co) - norms.append(v.normal) + verts.append((-v.co[0], v.co[2], -v.co[1])) + norm = averaged_normals[v_index] + norms.append((-norm[0], norm[2], -norm[1])) weights = [] for weight in v.groups: group = obj.vertex_groups[weight.group] w = [group.name, weight.weight] weights.append(w) print("i: %s f_verts: %s uv: %s" % (i, repr(f_verts), repr(uv))) - gv = GeoVertex(v.co, -v.normal, uv[i], weights) + gv = GeoVertex(verts[-1], norms[-1], uv[i], weights) geoverts.append(gv) - geoverts.reverse() geomesh.addFace(geoverts, texture_name) print("face: vertices: %s uvs: %s norms: %s groups: %s" % (verts, uv, norms, weights)) @@ -155,14 +165,10 @@ def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, us print("obj_scale: %s final_scale: %s" % (obj_scale, obj_scale * scale)) print(obj_scale * scale) scale_matrix = Matrix.Scale(obj_scale * scale, 4) - if bpy.app.version < (2, 80): - mesh.transform(global_matrix * scale_matrix * translate_matrix * axis_rotation) - else: - mesh.transform(global_matrix @ scale_matrix @ translate_matrix @ axis_rotation) geo_model = geo.addModel(ob.name.rsplit(".", 1)[0]) - convert_mesh(geo_model, mesh, ob) + convert_mesh(geo_model, mesh, ob, scale * obj_scale) if False: #flip bones for i in range(len(geo.models) - 1, -1, -1): diff --git a/import_geo.py b/import_geo.py index 3d6422b..52ef0b3 100644 --- a/import_geo.py +++ b/import_geo.py @@ -20,7 +20,7 @@ else: def import_fix_coord(v): return (-v[0], -v[2], v[1]) def import_fix_normal(v): - return ( v[0], v[2], -v[1]) + return ( -v[0], -v[2], v[1]) def import_fix_winding(l): l = list(l) l.reverse() @@ -58,7 +58,6 @@ def convert_model(geo_model, mesh_data, obj, scale): mesh_data.polygons.add(len(geomesh.face)) coords = [c for v in geomesh.geovertex for c in import_scale_coord(import_fix_coord(v.coord), scale)] - normals = [n for v in geomesh.geovertex for n in import_fix_normal(v.normal)] #print("normals: %s" % (normals, )) loop_totals = [] loop_starts = [] @@ -96,6 +95,9 @@ def convert_model(geo_model, mesh_data, obj, scale): mesh_data.validate() mesh_data.update() + normals = [import_fix_normal(geomesh.geovertex[i].normal) for f in geomesh.face for i in (f.vert_indexes[0], f.vert_indexes[2], f.vert_indexes[1])] + mesh_data.normals_split_custom_set(normals) + #todo: attempt to load textures/images def getBonePositionBody(bone):