Import and average the surface normals

master
Senpai 2 days ago
parent 0c943aa721
commit d8331cacd3

@ -4,10 +4,11 @@ from .bones import *
import bpy.path import bpy.path
import bpy import bpy
import mathutils import mathutils
from collections import defaultdict
from bpy_extras.io_utils import axis_conversion 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): if bpy.app.version < (2, 80):
# Be sure tessface & co are available! # Be sure tessface & co are available!
if not mesh.tessfaces and mesh.polygons: if not mesh.tessfaces and mesh.polygons:
@ -38,6 +39,14 @@ def convert_mesh(geo_model, mesh, obj):
geomesh = GeoMesh() 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" texture_name = "white.tga"
if bpy.app.version < (2, 80): if bpy.app.version < (2, 80):
faces = mesh.tessfaces faces = mesh.tessfaces
@ -73,28 +82,29 @@ def convert_mesh(geo_model, mesh, obj):
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4] uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4]
else: else:
print("uv.uv: %s" % uv.uv) 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: else:
uv = [(0, 0)] * 4 uv = [(0, 0)] * 4
texture_name = "white.tga" texture_name = "white.tga"
f_verts = f.vertices f_verts = f.vertices
f_verts = (f_verts[0], f_verts[2], f_verts[1])
verts = [] verts = []
norms = [] norms = []
groups = [] groups = []
geoverts = [] geoverts = []
for i, v_index in enumerate(f_verts): for i, v_index in enumerate(f_verts):
v = mesh_verts[v_index] v = mesh_verts[v_index]
verts.append(v.co) verts.append((-v.co[0], v.co[2], -v.co[1]))
norms.append(v.normal) norm = averaged_normals[v_index]
norms.append((-norm[0], norm[2], -norm[1]))
weights = [] weights = []
for weight in v.groups: for weight in v.groups:
group = obj.vertex_groups[weight.group] group = obj.vertex_groups[weight.group]
w = [group.name, weight.weight] w = [group.name, weight.weight]
weights.append(w) weights.append(w)
print("i: %s f_verts: %s uv: %s" % (i, repr(f_verts), repr(uv))) 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.append(gv)
geoverts.reverse()
geomesh.addFace(geoverts, texture_name) geomesh.addFace(geoverts, texture_name)
print("face: vertices: %s uvs: %s norms: %s groups: %s" % (verts, uv, norms, weights)) 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: %s final_scale: %s" % (obj_scale, obj_scale * scale))
print(obj_scale * scale) print(obj_scale * scale)
scale_matrix = Matrix.Scale(obj_scale * scale, 4) 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]) 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 if False: #flip bones
for i in range(len(geo.models) - 1, -1, -1): for i in range(len(geo.models) - 1, -1, -1):

@ -20,7 +20,7 @@ else:
def import_fix_coord(v): def import_fix_coord(v):
return (-v[0], -v[2], v[1]) return (-v[0], -v[2], v[1])
def import_fix_normal(v): def import_fix_normal(v):
return ( v[0], v[2], -v[1]) return ( -v[0], -v[2], v[1])
def import_fix_winding(l): def import_fix_winding(l):
l = list(l) l = list(l)
l.reverse() l.reverse()
@ -58,7 +58,6 @@ def convert_model(geo_model, mesh_data, obj, scale):
mesh_data.polygons.add(len(geomesh.face)) 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)] 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, )) #print("normals: %s" % (normals, ))
loop_totals = [] loop_totals = []
loop_starts = [] loop_starts = []
@ -96,6 +95,9 @@ def convert_model(geo_model, mesh_data, obj, scale):
mesh_data.validate() mesh_data.validate()
mesh_data.update() 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 #todo: attempt to load textures/images
def getBonePositionBody(bone): def getBonePositionBody(bone):

Loading…
Cancel
Save