Skip to content

Commit

Permalink
Merge command now makes a best guess for copying materials.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackears committed May 11, 2023
1 parent 10a862f commit 88ba3c4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
4 changes: 2 additions & 2 deletions addons/cyclops_level_builder/commands/cmd_delete_blocks.gd
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func will_change_anything():


func do_it():
print("Delete do_it")
#print("Delete do_it")

if tracked_blocks.is_empty():
var points:PackedVector3Array
Expand All @@ -75,7 +75,7 @@ func do_it():


func undo_it():
print("Delete undo_it")
#print("Delete undo_it")
for tracked in tracked_blocks:
var parent = builder.get_node(tracked.path_parent)

Expand Down
46 changes: 46 additions & 0 deletions addons/cyclops_level_builder/commands/cmd_merge_blocks.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,54 @@ var block_name_prefix:String = "Block_"
#Private
var tracked_blocks:Array[TrackedBlock]
var merged_block_data:ConvexBlockData
var merged_mat_list:Array[Material]
var merged_block_path:NodePath

func _init():
command_name = "Merge blocks"

func get_best_face(centroid:Vector3, ref_list:Array[NodePath])->Array:
var best_face:ConvexVolume.FaceInfo
var best_dist:float = INF
var best_block:CyclopsConvexBlock

for block_path in ref_list:
var block:CyclopsConvexBlock = builder.get_node(block_path)
var vol:ConvexVolume = block.control_mesh
for f in vol.faces:
var face_center:Vector3 = f.get_centroid()
var offset:float = centroid.distance_squared_to(face_center)
if offset < best_dist:
best_dist = offset
best_face = f
best_block = block

if best_face.material_id == -1:
return [best_face, null]
return [best_face, best_block.materials[best_face.material_id]]

func copy_face_attributes(target:ConvexVolume, ref_list:Array[NodePath])->Array[Material]:
var mat_list:Array[Material]

for f in target.faces:
var centroid:Vector3 = f.get_centroid()
var res:Array = get_best_face(centroid, ref_list)
var ref_face:ConvexVolume.FaceInfo = res[0]
var material:Material = res[1]

var mat_idx:int = -1
if material != null:
mat_idx = mat_list.find(material)
if mat_idx == -1:
mat_idx = mat_list.size()
mat_list.append(material)

f.material_id = mat_idx
f.uv_transform = ref_face.uv_transform
f.selected = ref_face.selected

return mat_list

func do_it():
if tracked_blocks.is_empty():
var points:PackedVector3Array
Expand All @@ -66,8 +109,10 @@ func do_it():

var merged_vol:ConvexVolume = ConvexVolume.new()
merged_vol.init_from_points(points)
merged_mat_list = copy_face_attributes(merged_vol, block_paths)
merged_block_data = merged_vol.to_convex_block_data()


#Delete source blocks
for block_path in block_paths:
var del_block:CyclopsConvexBlock = builder.get_node(block_path)
Expand All @@ -80,6 +125,7 @@ func do_it():
block.owner = builder.get_editor_interface().get_edited_scene_root()
block.name = GeneralUtil.find_unique_name(parent, block_name_prefix)
block.block_data = merged_block_data
block.materials = merged_mat_list
#block.materials

merged_block_path = block.get_path()
Expand Down
32 changes: 16 additions & 16 deletions test_level.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -1752,43 +1752,43 @@ face_selected = PackedByteArray(0, 0, 1, 0, 0, 0)
active_face = 2
face_ids = PackedInt32Array(0, 1, 2, 3, 4, 5)

[sub_resource type="Resource" id="Resource_gr5y2"]
[sub_resource type="Resource" id="Resource_80d17"]
script = ExtResource("4_qjut0")
selected = false
active = false
collsion = true
vertex_points = PackedVector3Array(-13, 28, 65, -13, 28, 64, -24, 28, 64, -24, 28, 65, -13, 34, 64, -13, 34, 65, -24, 34, 65, -24, 34, 64)
vertex_points = PackedVector3Array(-10, 33, 63, -10, 33, 64, -10, 28, 64, -10, 28, 63, 5, 33, 63, 5, 33, 64, 5, 28, 64, 5, 28, 63)
vertex_selected = PackedByteArray(0, 0, 0, 0, 0, 0, 0, 0)
active_vertex = -1
edge_vertex_indices = PackedInt32Array(0, 1, 1, 2, 2, 3, 3, 0, 4, 1, 0, 5, 5, 4, 6, 5, 3, 6, 7, 6, 2, 7, 4, 7)
edge_vertex_indices = PackedInt32Array(0, 1, 1, 2, 2, 3, 3, 0, 7, 6, 6, 5, 5, 4, 4, 7, 0, 4, 5, 1, 6, 2, 7, 3)
edge_selected = PackedByteArray(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
active_edge = -1
edge_face_indices = PackedInt32Array(0, 1, 0, 4, 0, 3, 0, 2, 1, 4, 1, 2, 1, 5, 2, 5, 2, 3, 3, 5, 3, 4, 4, 5)
edge_face_indices = PackedInt32Array(0, 2, 0, 3, 0, 4, 0, 5, 1, 4, 1, 3, 1, 2, 1, 5, 2, 5, 2, 3, 3, 4, 4, 5)
face_vertex_count = PackedInt32Array(4, 4, 4, 4, 4, 4)
face_vertex_indices = PackedInt32Array(0, 1, 2, 3, 4, 1, 0, 5, 6, 5, 0, 3, 7, 6, 3, 2, 4, 7, 2, 1, 4, 5, 6, 7)
face_vertex_indices = PackedInt32Array(0, 1, 2, 3, 7, 6, 5, 4, 1, 0, 4, 5, 2, 1, 5, 6, 3, 2, 6, 7, 0, 3, 7, 4)
face_material_indices = PackedInt32Array(0, 0, 0, 0, 0, 0)
face_uv_transform = Array[Transform2D]([Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0)])
face_selected = PackedByteArray(0, 0, 1, 0, 0, 0)
face_selected = PackedByteArray(0, 0, 0, 0, 0, 0)
active_face = -1
face_ids = PackedInt32Array(0, 1, 2, 3, 4, 5)

[sub_resource type="Resource" id="Resource_ouh11"]
[sub_resource type="Resource" id="Resource_vve6v"]
script = ExtResource("4_qjut0")
selected = false
active = false
collsion = true
vertex_points = PackedVector3Array(-10, 33, 63, -10, 33, 64, -10, 28, 64, -10, 28, 63, 5, 33, 63, 5, 33, 64, 5, 28, 64, 5, 28, 63)
vertex_points = PackedVector3Array(-13, 28, 65, -13, 28, 64, -24, 28, 64, -24, 28, 65, -13, 34, 64, -13, 34, 65, -24, 34, 65, -24, 34, 64)
vertex_selected = PackedByteArray(0, 0, 0, 0, 0, 0, 0, 0)
active_vertex = -1
edge_vertex_indices = PackedInt32Array(0, 1, 1, 2, 2, 3, 3, 0, 7, 6, 6, 5, 5, 4, 4, 7, 0, 4, 5, 1, 6, 2, 7, 3)
edge_vertex_indices = PackedInt32Array(0, 1, 1, 2, 2, 3, 3, 0, 4, 1, 0, 5, 5, 4, 6, 5, 3, 6, 7, 6, 2, 7, 4, 7)
edge_selected = PackedByteArray(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
active_edge = -1
edge_face_indices = PackedInt32Array(0, 2, 0, 3, 0, 4, 0, 5, 1, 4, 1, 3, 1, 2, 1, 5, 2, 5, 2, 3, 3, 4, 4, 5)
edge_face_indices = PackedInt32Array(0, 1, 0, 4, 0, 3, 0, 2, 1, 4, 1, 2, 1, 5, 2, 5, 2, 3, 3, 5, 3, 4, 4, 5)
face_vertex_count = PackedInt32Array(4, 4, 4, 4, 4, 4)
face_vertex_indices = PackedInt32Array(0, 1, 2, 3, 7, 6, 5, 4, 1, 0, 4, 5, 2, 1, 5, 6, 3, 2, 6, 7, 0, 3, 7, 4)
face_vertex_indices = PackedInt32Array(0, 1, 2, 3, 4, 1, 0, 5, 6, 5, 0, 3, 7, 6, 3, 2, 4, 7, 2, 1, 4, 5, 6, 7)
face_material_indices = PackedInt32Array(0, 0, 0, 0, 0, 0)
face_uv_transform = Array[Transform2D]([Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0), Transform2D(1, 0, 0, 1, 0, 0)])
face_selected = PackedByteArray(0, 0, 0, 0, 0, 0)
face_selected = PackedByteArray(0, 0, 1, 0, 0, 0)
active_face = -1
face_ids = PackedInt32Array(0, 1, 2, 3, 4, 5)

Expand Down Expand Up @@ -2233,15 +2233,15 @@ script = ExtResource("3_s3g63")
materials = Array[Material]([ExtResource("6_ve4fo")])
block_data = SubResource("Resource_uj2oh")

[node name="Block_23" type="Node" parent="CyclopsBlocks"]
[node name="Block_20" type="Node" parent="CyclopsBlocks"]
script = ExtResource("3_s3g63")
materials = Array[Material]([ExtResource("4_b0iic")])
block_data = SubResource("Resource_gr5y2")
block_data = SubResource("Resource_80d17")

[node name="Block_20" type="Node" parent="CyclopsBlocks"]
[node name="Block_23" type="Node" parent="CyclopsBlocks"]
script = ExtResource("3_s3g63")
materials = Array[Material]([ExtResource("4_b0iic")])
block_data = SubResource("Resource_ouh11")
block_data = SubResource("Resource_vve6v")

[node name="Marker3D" type="Marker3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.5, 39.5, 5)
Expand Down

0 comments on commit 88ba3c4

Please sign in to comment.