Added split normals layer to extracted meshes

This commit is contained in:
Jack Andersen 2015-09-21 12:45:55 -10:00
parent 8221caac35
commit 9f05f40994
2 changed files with 87 additions and 106 deletions

View File

@ -17,22 +17,11 @@ void InitGeomBlenderContext(HECL::BlenderConnection::PyOutStream& os,
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
"\n"
"# Property to convey original vert indices in overdraw meshes\n"
"class CMDLOriginalIndex(bpy.types.PropertyGroup):\n"
" index = bpy.props.IntProperty(name='Original Vertex Index')\n"
"bpy.utils.register_class(CMDLOriginalIndex)\n"
"bpy.types.Mesh.cmdl_orig_verts = bpy.props.CollectionProperty(type=CMDLOriginalIndex)\n"
"\n"
"def loop_from_facevert(face, vert_idx):\n"
"def loop_from_facevert(bm, face, vert_idx):\n"
" for loop in face.loops:\n"
" if loop.vert.index == vert_idx:\n"
" if loop.vert[bm.verts.layers.int['CMDLOriginalPosIdxs']] == vert_idx:\n"
" return loop\n"
"\n"
"def eq_normals(n1, n2):\n"
" if n1[0] == n2[0] and n1[1] == n2[1] and n1[2] == n2[2]:\n"
" return True\n"
" return False\n"
"\n"
"def add_triangle(bm, vert_seq, vert_indices, norm_seq, norm_indices, mat_nr, od_list):\n"
" if len(set(vert_indices)) != 3:\n"
" return None, None\n"
@ -40,12 +29,7 @@ void InitGeomBlenderContext(HECL::BlenderConnection::PyOutStream& os,
" ret_mesh = bm\n"
" vert_seq.ensure_lookup_table()\n"
" verts = [vert_seq[i] for i in vert_indices]\n"
" norms = [norm_seq[i] for i in norm_indices]\n"
"\n"
" for i in range(3):\n"
" if not eq_normals(verts[i].normal, norms[i]):\n"
" verts[i] = bm.verts.new(verts[i].co, verts[i])\n"
" vert_seq.ensure_lookup_table()\n"
" #norms = [norm_seq[i] for i in norm_indices]\n"
"\n"
" # Make the face\n"
" face = bm.faces.get(verts)\n"
@ -80,12 +64,9 @@ void InitGeomBlenderContext(HECL::BlenderConnection::PyOutStream& os,
" else: \n"
" face = bm.faces.new(verts)\n"
"\n"
" # Apply normals\n"
" for i in range(3):\n"
" verts[i].normal = norms[i]\n"
"\n"
" for i in range(3):\n"
" face.verts[i].index = vert_indices[i]\n"
" face.verts[i][ret_mesh.verts.layers.int['CMDLOriginalPosIdxs']] = vert_indices[i]\n"
" face.loops[i][ret_mesh.loops.layers.int['CMDLOriginalNormIdxs']] = norm_indices[i]\n"
" face.material_index = mat_nr\n"
" face.smooth = True\n"
"\n"
@ -123,13 +104,10 @@ void FinishBlenderMesh(HECL::BlenderConnection::PyOutStream& os,
"bpy.context.scene.objects.link(obj)\n"
"mesh.hecl_material_count = %u\n", meshIdx, matSetCount);
os << "for material in materials:\n"
" mesh.materials.append(material)\n"
os << "mesh.use_auto_smooth = True\n"
"\n"
"# Preserve original indices\n"
"for vert in bm.verts:\n"
" ov = mesh.cmdl_orig_verts.add()\n"
" ov.index = vert.index\n"
"for material in materials:\n"
" mesh.materials.append(material)\n"
"\n"
"# Merge OD meshes\n"
"for od_entry in od_list:\n"
@ -137,12 +115,12 @@ void FinishBlenderMesh(HECL::BlenderConnection::PyOutStream& os,
"\n"
" for vert in od_entry['bm'].verts:\n"
" if len(vert.link_faces):\n"
" vert_dict[vert.index] = bm.verts.new(vert.co, vert)\n"
" ov = mesh.cmdl_orig_verts.add()\n"
" ov.index = vert.index\n"
" copy_vert = bm.verts.new(vert.co, vert)\n"
" vert_dict[vert[od_entry['bm'].verts.layers.int['CMDLOriginalPosIdxs']]] = copy_vert\n"
" copy_vert[orig_pidx_lay] = vert[od_entry['bm'].verts.layers.int['CMDLOriginalPosIdxs']]\n"
"\n"
" for face in od_entry['bm'].faces:\n"
" merge_verts = [vert_dict[fv.index] for fv in face.verts]\n"
" merge_verts = [vert_dict[fv[od_entry['bm'].verts.layers.int['CMDLOriginalPosIdxs']]] for fv in face.verts]\n"
" if bm.faces.get(merge_verts) is not None:\n"
" continue\n"
" merge_face = bm.faces.new(merge_verts)\n"
@ -151,19 +129,21 @@ void FinishBlenderMesh(HECL::BlenderConnection::PyOutStream& os,
" new = merge_face.loops[i]\n"
" for j in range(len(od_entry['bm'].loops.layers.uv)):\n"
" new[bm.loops.layers.uv[j]] = old[od_entry['bm'].loops.layers.uv[j]]\n"
" new[orig_nidx_lay] = old[od_entry['bm'].loops.layers.int['CMDLOriginalNormIdxs']]\n"
" merge_face.smooth = True\n"
" merge_face.material_index = face.material_index\n"
"\n"
" od_entry['bm'].free()\n"
"\n"
"# Remove loose vertices\n"
"#to_remove = []\n"
"#for vert in bm.verts:\n"
"# if not len(vert.link_faces):\n"
"# to_remove.append(vert)\n"
"#bmesh.ops.delete(bm, geom=to_remove, context=1)\n"
"\n"
"bm.to_mesh(mesh)\n"
"\n"
"# Load split normal data\n"
"split_normals = []\n"
"for face in bm.faces:\n"
" for loop in face.loops:\n"
" split_normals.append(norm_list[loop[orig_nidx_lay]])\n"
"mesh.normals_split_custom_set(split_normals)\n"
"\n"
"bm.free()\n"
"\n"
"# Remove redundant materials\n"

View File

@ -339,15 +339,16 @@ atUint32 ReadGeomSectionsToBlender(HECL::BlenderConnection::PyOutStream& os,
{
os << "# Begin bmesh\n"
"bm = bmesh.new()\n"
"\n";
"\n"
"# Overdraw-tracking\n"
"od_list = []\n"
"\n"
"orig_pidx_lay = bm.verts.layers.int.new('CMDLOriginalPosIdxs')\n"
"orig_nidx_lay = bm.loops.layers.int.new('CMDLOriginalNormIdxs')\n";
if (rp.first)
os << "dvert_lay = bm.verts.layers.deform.verify()\n";
os << "# Overdraw-tracking\n"
"od_list = []\n"
"\n";
/* Pre-read pass to determine maximum used vert indices */
atUint32 matSecCount = 0;
if (matSetCount)
@ -612,17 +613,17 @@ atUint32 ReadGeomSectionsToBlender(HECL::BlenderConnection::PyOutStream& os,
{
if (j==0 && matShortUVs)
os.format(" uv_tri = expand_lightmap_triangle(suv_list[%u], suv_list[%u], suv_list[%u])\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
primVerts[c%3].uvs[j], primVerts[(c+2)%3].uvs[j], primVerts[(c+1)%3].uvs[j],
primVerts[c%3].pos, j,
primVerts[(c+2)%3].pos, j,
primVerts[(c+1)%3].pos, j);
else
os.format(" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
os.format(" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
primVerts[c%3].pos, j, primVerts[c%3].uvs[j],
primVerts[(c+2)%3].pos, j, primVerts[(c+2)%3].uvs[j],
primVerts[(c+1)%3].pos, j, primVerts[(c+1)%3].uvs[j]);
@ -646,17 +647,17 @@ atUint32 ReadGeomSectionsToBlender(HECL::BlenderConnection::PyOutStream& os,
{
if (j==0 && matShortUVs)
os.format(" uv_tri = expand_lightmap_triangle(suv_list[%u], suv_list[%u], suv_list[%u])\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
primVerts[c%3].uvs[j], primVerts[(c+1)%3].uvs[j], primVerts[(c+2)%3].uvs[j],
primVerts[c%3].pos, j,
primVerts[(c+1)%3].pos, j,
primVerts[(c+2)%3].pos, j);
else
os.format(" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
os.format(" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
primVerts[c%3].pos, j, primVerts[c%3].uvs[j],
primVerts[(c+1)%3].pos, j, primVerts[(c+1)%3].uvs[j],
primVerts[(c+2)%3].pos, j, primVerts[(c+2)%3].uvs[j]);
@ -692,17 +693,17 @@ atUint32 ReadGeomSectionsToBlender(HECL::BlenderConnection::PyOutStream& os,
{
if (j==0 && matShortUVs)
os.format(" uv_tri = expand_lightmap_triangle(suv_list[%u], suv_list[%u], suv_list[%u])\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
primVerts[0].uvs[j], primVerts[1].uvs[j], primVerts[2].uvs[j],
primVerts[0].pos, j,
primVerts[1].pos, j,
primVerts[2].pos, j);
else
os.format(" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
os.format(" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
primVerts[0].pos, j, primVerts[0].uvs[j],
primVerts[1].pos, j, primVerts[1].uvs[j],
primVerts[2].pos, j, primVerts[2].uvs[j]);
@ -738,17 +739,17 @@ atUint32 ReadGeomSectionsToBlender(HECL::BlenderConnection::PyOutStream& os,
{
if (j==0 && matShortUVs)
os.format(" uv_tri = expand_lightmap_triangle(suv_list[%u], suv_list[%u], suv_list[%u])\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[0]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[1]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_tri[2]\n",
firstPrimVert.uvs[j], primVerts[c%3].uvs[j], primVerts[(c+1)%3].uvs[j],
firstPrimVert.pos, j,
primVerts[c%3].pos, j,
primVerts[(c+1)%3].pos, j);
else
os.format(" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
os.format(" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n"
" loop_from_facevert(last_mesh, last_face, %u)[last_mesh.loops.layers.uv[%u]].uv = uv_list[%u]\n",
firstPrimVert.pos, j, firstPrimVert.uvs[j],
primVerts[c%3].pos, j, primVerts[c%3].uvs[j],
primVerts[(c+1)%3].pos, j, primVerts[(c+1)%3].uvs[j]);