Skip to content

Commit 8b67d62

Browse files
authored
Merge pull request #434 from Jrius/normals_export_fix
Correctly export vertex normals
2 parents 67b7b58 + b1d0465 commit 8b67d62

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

korman/exporter/mesh.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,12 @@ def _export_geometry(self, bo, mesh, materials, geospans, mat2span_LUT):
394394
continue
395395

396396
face_verts = []
397-
use_smooth = tessface.use_smooth
398397
dPosDu = hsVector3(0.0, 0.0, 0.0)
399398
dPosDv = hsVector3(0.0, 0.0, 0.0)
400399

400+
# Unpack normals
401+
tessface_normals = tessface.split_normals
402+
401403
# Unpack the UV coordinates from each UV Texture layer
402404
# NOTE: Blender has no third (W) coordinate
403405
tessface_uvws = [uvtex.data[i].uv for uvtex in mesh.tessface_uv_textures]
@@ -446,6 +448,7 @@ def _export_geometry(self, bo, mesh, materials, geospans, mat2span_LUT):
446448

447449
# Convert to per-material indices
448450
for j, vertex in enumerate(tessface.vertices):
451+
vertex_normal = tuple(tessface_normals[j])
449452
uvws = tuple([tuple(uvw[j]) for uvw in tessface_uvws])
450453

451454
# Calculate vertex colors.
@@ -462,18 +465,14 @@ def _export_geometry(self, bo, mesh, materials, geospans, mat2span_LUT):
462465
# Now, we'll index into the vertex dict using the per-face elements :(
463466
# We're using tuples because lists are not hashable. The many mathutils and PyHSPlasma
464467
# types are not either, and it's entirely too much work to fool with all that.
465-
coluv = (vertex_color, uvws)
466-
if coluv not in data.blender2gs[vertex]:
468+
normcoluv = (vertex_normal, vertex_color, uvws)
469+
if normcoluv not in data.blender2gs[vertex]:
467470
source = mesh.vertices[vertex]
468471
geoVertex = plGeometrySpan.TempVertex()
469472
geoVertex.position = hsVector3(*source.co)
470473

471-
# If this face has smoothing, use the vertex normal
472-
# Otherwise, use the face normal
473-
normal = source.normal if use_smooth else tessface.normal
474-
475474
# MOUL/DX9 craps its pants if any element of the normal is exactly 0.0
476-
normal = map(lambda x: max(x, 0.01) if x >= 0.0 else min(x, -0.01), normal)
475+
normal = map(lambda x: max(x, 0.01) if x >= 0.0 else min(x, -0.01), vertex_normal)
477476
normal = hsVector3(*normal)
478477
normal.normalize()
479478
geoVertex.normal = normal
@@ -486,15 +485,15 @@ def _export_geometry(self, bo, mesh, materials, geospans, mat2span_LUT):
486485
geoVertex.uvs = uvs
487486

488487
idx = len(data.vertices)
489-
data.blender2gs[vertex][coluv] = idx
488+
data.blender2gs[vertex][normcoluv] = idx
490489
data.vertices.append(geoVertex)
491490
face_verts.append(idx)
492491
else:
493492
# If we have a bump mapping layer, then we need to add the bump gradients for
494493
# this face to the vertex's magic channels
495494
if bumpmap is not None:
496495
num_user_uvs = len(uvws)
497-
geoVertex = data.vertices[data.blender2gs[vertex][coluv]]
496+
geoVertex = data.vertices[data.blender2gs[vertex][normcoluv]]
498497

499498
# Unfortunately, PyHSPlasma returns a copy of everything. Previously, editing
500499
# in place would result in silent failures; however, as of python_refactor,
@@ -503,7 +502,7 @@ def _export_geometry(self, bo, mesh, materials, geospans, mat2span_LUT):
503502
geoUVs[num_user_uvs] += dPosDu
504503
geoUVs[num_user_uvs+1] += dPosDv
505504
geoVertex.uvs = geoUVs
506-
face_verts.append(data.blender2gs[vertex][coluv])
505+
face_verts.append(data.blender2gs[vertex][normcoluv])
507506

508507
# Convert to triangles, if need be...
509508
num_faces = len(face_verts)
@@ -614,6 +613,7 @@ def _export_object(self, bo):
614613
return self._export_mesh(bo, mesh)
615614

616615
def _export_mesh(self, bo, mesh):
616+
mesh.calc_normals_split()
617617
mesh.calc_tessface()
618618

619619
# Step 0.8: Determine materials needed for export... Three considerations here:

0 commit comments

Comments
 (0)