Compare commits

..

5 Commits

Author SHA1 Message Date
gsemaj
7af28d8e70
fix submesh overlap 2023-09-09 17:45:47 -07:00
gsemaj
3bb3df7a2d
Split terrain into chunks of chunk_size 2023-09-09 16:40:22 -07:00
gsemaj
f6dc2d690f
code to show all selected faces 2023-09-09 11:24:47 -07:00
gsemaj
af2988e062
fix error 2023-09-09 10:43:43 -07:00
gsemaj
03a3e21532
Fix UVs 2023-09-09 10:28:48 -07:00

View File

@ -8,7 +8,14 @@ dongpath = r'C:\Users\gents\AppData\LocalLow\Unity\Web Player\Cache\Fusionfall'
env = UnityEnvironment(base_path=dongpath)
outpath = r'C:\Users\gents\3D Objects\FFTerrainMeshes'
def rip_terrain_mesh(f, outpath):
def uvs_from_vert(uv_layer, v):
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)
for k, v in dong.objects.items():
@ -25,12 +32,6 @@ def rip_terrain_mesh(f, outpath):
context = bpy.context
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')
bm = bmesh.from_edit_mesh(context.edit_object.data)
bm.verts.ensure_lookup_table()
@ -48,6 +49,8 @@ def rip_terrain_mesh(f, outpath):
indices = []
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
for shift in terrainData['m_Heightmap']['m_Shifts']:
shift_index = shift['y'] + shift['x'] * 129
@ -56,12 +59,30 @@ def rip_terrain_mesh(f, outpath):
flags = shift['flags'] # bits: +X -X +Y -Y
if flags & 0b1000: # +X
v.co.x += shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.x += uv_shift_amt
if flags & 0b0100: # -X
v.co.x -= shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.x -= uv_shift_amt
if flags & 0b0010: # +Y
v.co.y += shift_amt
for uv in uvs_from_vert(uv_layer, v):
uv.y += uv_shift_amt
if flags & 0b0001: # -Y
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
for v in bm.verts:
@ -73,27 +94,47 @@ def rip_terrain_mesh(f, outpath):
for f in bm.faces:
f.normal_flip()
# export
# 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
bpy.ops.object.select_all(action='SELECT')
name = terrainData['m_Name']
outfile = f"{name}.obj"
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
# clear the scene
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
if(clear):
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
dongs = os.listdir(dongpath)
for dongname in dongs: