Browse Source

Improve normals export

pull/434/head
Jrius 2 weeks ago
parent
commit
30d2a305d5
  1. 22
      korman/exporter/mesh.py

22
korman/exporter/mesh.py

@ -394,10 +394,12 @@ class MeshConverter(_MeshManager):
continue continue
face_verts = [] face_verts = []
use_smooth = tessface.use_smooth
dPosDu = hsVector3(0.0, 0.0, 0.0) dPosDu = hsVector3(0.0, 0.0, 0.0)
dPosDv = hsVector3(0.0, 0.0, 0.0) dPosDv = hsVector3(0.0, 0.0, 0.0)
# Unpack normals
tessface_normals = [tuple(n) for n in tessface.split_normals]
# Unpack the UV coordinates from each UV Texture layer # Unpack the UV coordinates from each UV Texture layer
# NOTE: Blender has no third (W) coordinate # NOTE: Blender has no third (W) coordinate
tessface_uvws = [uvtex.data[i].uv for uvtex in mesh.tessface_uv_textures] tessface_uvws = [uvtex.data[i].uv for uvtex in mesh.tessface_uv_textures]
@ -446,6 +448,7 @@ class MeshConverter(_MeshManager):
# Convert to per-material indices # Convert to per-material indices
for j, vertex in enumerate(tessface.vertices): for j, vertex in enumerate(tessface.vertices):
vertex_normal = tessface_normals[j]
uvws = tuple([tuple(uvw[j]) for uvw in tessface_uvws]) uvws = tuple([tuple(uvw[j]) for uvw in tessface_uvws])
# Calculate vertex colors. # Calculate vertex colors.
@ -462,18 +465,14 @@ class MeshConverter(_MeshManager):
# Now, we'll index into the vertex dict using the per-face elements :( # Now, we'll index into the vertex dict using the per-face elements :(
# We're using tuples because lists are not hashable. The many mathutils and PyHSPlasma # We're using tuples because lists are not hashable. The many mathutils and PyHSPlasma
# types are not either, and it's entirely too much work to fool with all that. # types are not either, and it's entirely too much work to fool with all that.
coluv = (vertex_color, uvws) normcoluv = (vertex_normal, vertex_color, uvws)
if coluv not in data.blender2gs[vertex]: if normcoluv not in data.blender2gs[vertex]:
source = mesh.vertices[vertex] source = mesh.vertices[vertex]
geoVertex = plGeometrySpan.TempVertex() geoVertex = plGeometrySpan.TempVertex()
geoVertex.position = hsVector3(*source.co) geoVertex.position = hsVector3(*source.co)
# If this face has smoothing, use the vertex normal
# Otherwise, use the face normal
normal = source.normal if use_smooth else tessface.normal
# MOUL/DX9 craps its pants if any element of the normal is exactly 0.0 # MOUL/DX9 craps its pants if any element of the normal is exactly 0.0
normal = map(lambda x: max(x, 0.01) if x >= 0.0 else min(x, -0.01), normal) normal = map(lambda x: max(x, 0.01) if x >= 0.0 else min(x, -0.01), vertex_normal)
normal = hsVector3(*normal) normal = hsVector3(*normal)
normal.normalize() normal.normalize()
geoVertex.normal = normal geoVertex.normal = normal
@ -486,7 +485,7 @@ class MeshConverter(_MeshManager):
geoVertex.uvs = uvs geoVertex.uvs = uvs
idx = len(data.vertices) idx = len(data.vertices)
data.blender2gs[vertex][coluv] = idx data.blender2gs[vertex][normcoluv] = idx
data.vertices.append(geoVertex) data.vertices.append(geoVertex)
face_verts.append(idx) face_verts.append(idx)
else: else:
@ -494,7 +493,7 @@ class MeshConverter(_MeshManager):
# this face to the vertex's magic channels # this face to the vertex's magic channels
if bumpmap is not None: if bumpmap is not None:
num_user_uvs = len(uvws) num_user_uvs = len(uvws)
geoVertex = data.vertices[data.blender2gs[vertex][coluv]] geoVertex = data.vertices[data.blender2gs[vertex][normcoluv]]
# Unfortunately, PyHSPlasma returns a copy of everything. Previously, editing # Unfortunately, PyHSPlasma returns a copy of everything. Previously, editing
# in place would result in silent failures; however, as of python_refactor, # in place would result in silent failures; however, as of python_refactor,
@ -503,7 +502,7 @@ class MeshConverter(_MeshManager):
geoUVs[num_user_uvs] += dPosDu geoUVs[num_user_uvs] += dPosDu
geoUVs[num_user_uvs+1] += dPosDv geoUVs[num_user_uvs+1] += dPosDv
geoVertex.uvs = geoUVs geoVertex.uvs = geoUVs
face_verts.append(data.blender2gs[vertex][coluv]) face_verts.append(data.blender2gs[vertex][normcoluv])
# Convert to triangles, if need be... # Convert to triangles, if need be...
num_faces = len(face_verts) num_faces = len(face_verts)
@ -614,6 +613,7 @@ class MeshConverter(_MeshManager):
return self._export_mesh(bo, mesh) return self._export_mesh(bo, mesh)
def _export_mesh(self, bo, mesh): def _export_mesh(self, bo, mesh):
mesh.calc_normals_split()
mesh.calc_tessface() mesh.calc_tessface()
# Step 0.8: Determine materials needed for export... Three considerations here: # Step 0.8: Determine materials needed for export... Three considerations here:

Loading…
Cancel
Save