Skip to content

Commit

Permalink
Define a (sensible) transform for all parts, do not apply a 180° Z ro…
Browse files Browse the repository at this point in the history
…tation to all parts
  • Loading branch information
vbousquet committed Aug 19, 2023
1 parent ed92cdb commit 59bc561
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
7 changes: 4 additions & 3 deletions addons/vpx_lightmapper/vlm_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ def append_structure(src_path, mode, hashed):
# ObjRotX / ObjRotY / ObjRotZ
writer.write_tagged_float(b'RTV6', math.degrees(obj.rotation_euler[0]))
writer.write_tagged_float(b'RTV7', math.degrees(obj.rotation_euler[1]))
writer.write_tagged_float(b'RTV8', -math.degrees(obj.rotation_euler[2]) - 180)
writer.write_tagged_float(b'RTV8', -math.degrees(obj.rotation_euler[2]))
else:
# RotX / RotY / RotZ
writer.write_tagged_float(b'RTV0', math.degrees(obj.rotation_euler[0]))
writer.write_tagged_float(b'RTV1', math.degrees(obj.rotation_euler[1]))
writer.write_tagged_float(b'RTV2', -math.degrees(obj.rotation_euler[2]) - 180)
writer.write_tagged_float(b'RTV2', -math.degrees(obj.rotation_euler[2]))
# TransX / TransY / TransZ
writer.write_tagged_float(b'RTV3', 0)
writer.write_tagged_float(b'RTV4', 0)
Expand Down Expand Up @@ -450,13 +450,14 @@ def append_structure(src_path, mode, hashed):
n_vertices = 0
for poly in obj.data.polygons:
if len(poly.loop_indices) != 3:
logger.error(f'ERROR: invalid polygon encountered in part {obj.name}, it is not triangulated ({len(poly.loop_indices)} edges)')
continue
for loop_index in reversed(poly.loop_indices):
loop = obj.data.loops[loop_index]
x, y, z = obj.data.vertices[loop.vertex_index].co
nx, ny, nz = loop.normal
u, v = uv_layer_nested.data[loop_index].uv
vertex = (-x / global_scale, y / global_scale, z / global_scale, -nx, ny, nz, u, 1.0 - v)
vertex = (x / global_scale, -y / global_scale, z / global_scale, nx, -ny, nz, u, 1.0 - v)
existing_index = vert_dict.get(vertex, None)
if existing_index is None:
vert_dict[vertex] = n_vertices
Expand Down
25 changes: 16 additions & 9 deletions addons/vpx_lightmapper/vlm_meshes_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,14 @@ def create_bake_meshes(op, context):
logger.info(f"\nBuilding solid bake target model for '{bake_name}'")
poly_start = 0
objects_to_join = []
base_instances = []
last_obj = None

for i, obj_name in enumerate(bake_col_object_set):
dup = bpy.data.objects[obj_name].copy()
dup.data = dup.data.copy()
base_instances.append(bpy.data.objects[obj_name])
objects_to_join.append(dup)
result_col.objects.link(dup)
bpy.ops.object.select_all(action='DESELECT')
dup.select_set(True)
Expand Down Expand Up @@ -368,10 +371,8 @@ def create_bake_meshes(op, context):
if is_bake:
dup.data.uv_layers.remove(dup.data.uv_layers[projected_uv])

objects_to_join.append(dup)

if len(objects_to_join) == 0: continue

# Create merged mesh
if len(objects_to_join) > 1:
logger.info(f". {len(objects_to_join)} Objects merged") # {[obj.name for obj in objects_to_join]}")
Expand All @@ -385,6 +386,15 @@ def create_bake_meshes(op, context):
bake_target.select_set(True)
context.view_layer.objects.active = bake_target

# Evaluate bake mesh transform
if sync_obj and bpy.data.objects[sync_obj]:
transform = Matrix(bpy.data.objects[sync_obj].matrix_world)
elif len(objects_to_join) == 1:
transform = Matrix(base_instances[0].matrix_world)
else:
centre = sum((Vector(b) for b in bake_target.bound_box), Vector()) / 8
transform = Matrix.Translation(centre)

# if bake_name == 'Parts':
# return {'FINISHED'}

Expand All @@ -408,21 +418,18 @@ def create_bake_meshes(op, context):
light_name, is_lightmap, _, lights = light_scenario
if is_lightmap: continue
obj_name = f'{bake_name}.BM.{light_name}' # if sync_obj else f'Table.BM.{light_name}.{bake_name}'
print(f'{obj_name} => {transform}')
bake_mesh = bake_mesh.copy()
bake_instance = bpy.data.objects.new(obj_name, bake_mesh)
result_col.objects.link(bake_instance)
if sync_obj:
dup = bpy.data.objects[sync_obj]
bake_mesh.transform(Matrix(dup.matrix_world).inverted())
bake_instance.matrix_world = dup.matrix_world
else:
bake_instance.matrix_world.identity()
adapt_materials(bake_instance.data, light_name, is_lightmap)
bake_instance.vlmSettings.bake_lighting = light_name
bake_instance.vlmSettings.bake_collections = bake_col.name
bake_instance.vlmSettings.bake_sync_light = ''
bake_instance.vlmSettings.bake_sync_trans = sync_obj if sync_obj is not None else ''
bake_instance.vlmSettings.bake_hdr_range = bake_hdr_range[light_name]
bake_mesh.transform(transform.inverted())
bake_instance.matrix_world = transform
if is_translucent:
bake_instance.vlmSettings.bake_type = 'active'
elif sync_obj is None:
Expand Down

0 comments on commit 59bc561

Please sign in to comment.