Compare commits

..

No commits in common. "7af28d8e70b56abd3e96d09a5a99d6adba8a30d2" and "20d5f6231c9ebab50302fefe3a6e3710fc4fa0cc" have entirely different histories.

View File

@ -8,14 +8,7 @@ dongpath = r'C:\Users\gents\AppData\LocalLow\Unity\Web Player\Cache\Fusionfall'
env = UnityEnvironment(base_path=dongpath) env = UnityEnvironment(base_path=dongpath)
outpath = r'C:\Users\gents\3D Objects\FFTerrainMeshes' outpath = r'C:\Users\gents\3D Objects\FFTerrainMeshes'
def uvs_from_vert(uv_layer, v): def rip_terrain_mesh(f, outpath):
uvs = []
for l in v.link_loops:
uv_data = l[uv_layer]
uvs.append(uv_data.uv)
return uvs
def rip_terrain_mesh(f, outpath, clear=False):
dong = Asset.from_file(f, environment=env) dong = Asset.from_file(f, environment=env)
for k, v in dong.objects.items(): for k, v in dong.objects.items():
@ -32,6 +25,12 @@ def rip_terrain_mesh(f, outpath, clear=False):
context = bpy.context context = bpy.context
grid = context.edit_object grid = context.edit_object
# apply triangulate modifier
mod = grid.modifiers.new("Triangulate", 'TRIANGULATE')
mod.quad_method = 'FIXED' # triangle orientation
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.modifier_apply(modifier="Triangulate")
bpy.ops.object.mode_set(mode='EDIT') bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(context.edit_object.data) bm = bmesh.from_edit_mesh(context.edit_object.data)
bm.verts.ensure_lookup_table() bm.verts.ensure_lookup_table()
@ -49,8 +48,6 @@ def rip_terrain_mesh(f, outpath, clear=False):
indices = [] indices = []
shift_amt = abs(bm.verts[0].co.x - bm.verts[1].co.x) shift_amt = abs(bm.verts[0].co.x - bm.verts[1].co.x)
uv_layer = bm.loops.layers.uv.active
uv_shift_amt = 1 / 256
# gather m_Shifts positions # gather m_Shifts positions
for shift in terrainData['m_Heightmap']['m_Shifts']: for shift in terrainData['m_Heightmap']['m_Shifts']:
shift_index = shift['y'] + shift['x'] * 129 shift_index = shift['y'] + shift['x'] * 129
@ -59,30 +56,12 @@ def rip_terrain_mesh(f, outpath, clear=False):
flags = shift['flags'] # bits: +X -X +Y -Y flags = shift['flags'] # bits: +X -X +Y -Y
if flags & 0b1000: # +X if flags & 0b1000: # +X
v.co.x += shift_amt v.co.x += shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.x += uv_shift_amt
if flags & 0b0100: # -X if flags & 0b0100: # -X
v.co.x -= shift_amt v.co.x -= shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.x -= uv_shift_amt
if flags & 0b0010: # +Y if flags & 0b0010: # +Y
v.co.y += shift_amt v.co.y += shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.y += uv_shift_amt
if flags & 0b0001: # -Y if flags & 0b0001: # -Y
v.co.y -= shift_amt v.co.y -= shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.y -= uv_shift_amt
# apply triangulate modifier
mod = grid.modifiers.new("Triangulate", 'TRIANGULATE')
mod.quad_method = 'FIXED' # triangle orientation
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.modifier_apply(modifier="Triangulate")
bpy.ops.object.mode_set(mode="EDIT")
bm = bmesh.from_edit_mesh(context.edit_object.data)
bm.verts.ensure_lookup_table()
# flip diagonally # flip diagonally
for v in bm.verts: for v in bm.verts:
@ -94,47 +73,27 @@ def rip_terrain_mesh(f, outpath, clear=False):
for f in bm.faces: for f in bm.faces:
f.normal_flip() f.normal_flip()
# select vertex chunks and separate
verts = {}
for x in range(129):
for y in range(129):
idx = y + x * 129
v = bm.verts[idx]
verts[idx] = v
v.select = False
for f in v.link_faces:
f.select = False
chunk_size = 8
for x in range(128 // chunk_size):
for y in range(128 // chunk_size):
for i in range(x * chunk_size, x * chunk_size + chunk_size + 1):
for j in range(y * chunk_size, y * chunk_size + chunk_size + 1):
idx = j + i * 129
v = verts[idx]
v.select = True
bm.select_mode = {'VERT', 'EDGE', 'FACE'}
bm.select_flush_mode()
bpy.context.tool_settings.mesh_select_mode = (False, False, True)
bpy.ops.mesh.duplicate()
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.mesh.select_all(action='DESELECT')
# delete main
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='DESELECT')
grid.select_set(True)
bpy.ops.object.delete()
# export # export
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='SELECT') bpy.ops.object.select_all(action='SELECT')
name = terrainData['m_Name'] name = terrainData['m_Name']
outfile = f"{name}.obj" outfile = f"{name}.obj"
bpy.ops.export_scene.obj(filepath=os.path.join(outpath, outfile)) bpy.ops.export_scene.obj(filepath=os.path.join(outpath, outfile))
# select modified vertices
#bpy.ops.object.mode_set(mode="EDIT")
#bm = bmesh.from_edit_mesh(context.edit_object.data)
#bm.verts.ensure_lookup_table()
#for v in bm.verts:
# v.select = False
#for shift_index in indices:
# v = bm.verts[shift_index]
# v.select = True
if(clear): # clear the scene
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='SELECT') bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete() bpy.ops.object.delete()
dongs = os.listdir(dongpath) dongs = os.listdir(dongpath)
for dongname in dongs: for dongname in dongs: