From 03ba7acef35f88e78795dd878ae35d7fc21e23fa Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sat, 18 Nov 2023 15:32:37 +0100 Subject: [PATCH] generate mesh from room walls --- content/raycast.gd | 1 + content/ui/menu/menu.gd | 9 +++--- content/ui/menu/menu.tscn | 2 ++ content/ui/menu/room/room_menu.gd | 48 +++++++++++++++++++++++++++++ content/ui/menu/room/room_menu.tscn | 32 ++++++++++++++++++- content/ui/menu/room/walls.tres | 3 ++ 6 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 content/ui/menu/room/walls.tres diff --git a/content/raycast.gd b/content/raycast.gd index bc3da7f..4551aa1 100644 --- a/content/raycast.gd +++ b/content/raycast.gd @@ -94,6 +94,7 @@ func _call_fn(collider: Object, fn_name: String, node: Node3D = null, event = nu if result != null && result is Dictionary: result.merge(event, true) + event = result if result != null && result is bool && result == false: # Stop the event from bubbling up diff --git a/content/ui/menu/menu.gd b/content/ui/menu/menu.gd index 3ab0e63..2f9b07f 100644 --- a/content/ui/menu/menu.gd +++ b/content/ui/menu/menu.gd @@ -4,6 +4,7 @@ extends Node3D @onready var nav_edit: Button3D = $Navigation/Edit @onready var menu_edit: Node3D = $Content/EditMenu @onready var nav_room = $Navigation/Room +@onready var menu_room: Node3D = $Content/RoomMenu @onready var nav_automate = $Navigation/Automate @onready var nav_settings = $Navigation/Settings @@ -18,7 +19,7 @@ enum Menu { var selected_menu := Menu.EDIT func _ready(): - pass + select_menu(selected_menu) func _on_click(event): if event.target == nav_view: @@ -36,7 +37,7 @@ func select_menu(menu: Menu): selected_menu = menu for child in $Content.get_children(): if child.is_visible(): - child.hide() + $Content.remove_child(child) var menu_node = enum_to_menu(menu) var nav_node = enum_to_nav(menu) @@ -45,7 +46,7 @@ func select_menu(menu: Menu): nav_node.disabled = true if menu_node != null: - menu_node.show() + $Content.add_child(menu_node) for child in $Navigation.get_children(): if child.active && child != nav_node: @@ -72,7 +73,7 @@ func enum_to_menu(menu: Menu): Menu.EDIT: return menu_edit Menu.ROOM: - return null + return menu_room Menu.AUTOMATE: return null Menu.SETTINGS: diff --git a/content/ui/menu/menu.tscn b/content/ui/menu/menu.tscn index 116ecc8..feef3d6 100644 --- a/content/ui/menu/menu.tscn +++ b/content/ui/menu/menu.tscn @@ -23,6 +23,7 @@ mesh = SubResource("BoxMesh_08du6") [node name="ImmersiveHomePanels" type="MeshInstance3D" parent="."] transform = Transform3D(-4.37114e-10, 0, 0.01, 0, 0.01, 0, -0.01, 0, -4.37114e-10, 0.32, 0, -0.0500001) +visible = false mesh = ExtResource("7_f4u4o") [node name="Navigation" type="Node3D" parent="."] @@ -40,6 +41,7 @@ texture = ExtResource("5_8o1rb") [node name="Edit" parent="Navigation" instance=ExtResource("5_w4i01")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.03, 0, 0.09) toggleable = true +initial_active = true [node name="Sprite3D" type="Sprite3D" parent="Navigation/Edit"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.012, 0) diff --git a/content/ui/menu/room/room_menu.gd b/content/ui/menu/room/room_menu.gd index eacdd7c..57c448e 100644 --- a/content/ui/menu/room/room_menu.gd +++ b/content/ui/menu/room/room_menu.gd @@ -6,24 +6,72 @@ const wall_edge_scene = preload("res://content/ui/menu/room/wall_edge.tscn") @onready var teleport_root = $TeleportRoot @onready var wall_corners = $TeleportRoot/WallCorners @onready var wall_edges = $TeleportRoot/WallEdges +@onready var wall_mesh = $TeleportRoot/WallMesh +@onready var toggle_edit_button = $Interface/ToggleEdit var moving = null var ground_plane = Plane(Vector3.UP, Vector3.ZERO) +var edit_enabled = false func _ready(): remove_child(teleport_root) get_tree().get_root().get_node("Main").add_child.call_deferred(teleport_root) teleport_root.get_node("Ground/Clickable").on_click.connect(func(event): + if !edit_enabled: + return + add_corner(event.ray.get_collision_point()) ) + toggle_edit_button.get_node("Clickable").on_click.connect(func(event): + edit_enabled = event.active + if edit_enabled == false: + wall_corners.visible = false + wall_edges.visible = false + generate_mesh() + wall_mesh.visible = true + else: + wall_corners.visible = true + wall_edges.visible = true + wall_mesh.visible = false + ) + +func generate_mesh(): + var st = SurfaceTool.new() + var wall_up = Vector3.UP * 3 + + st.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP) + + for i in range(wall_corners.get_child_count()): + var corner = get_corner(i) + + print(corner.position, " ", corner.position + wall_up) + + st.add_vertex(corner.position) + st.add_vertex(corner.position + wall_up) + + var first_corner = get_corner(0) + + st.add_vertex(first_corner.position) + st.add_vertex(first_corner.position + wall_up) + + st.index() + st.generate_normals() + st.generate_tangents() + var mesh = st.commit() + + wall_mesh.mesh = mesh + func add_corner(position: Vector3): var corner = wall_corner_scene.instantiate() corner.position = position corner.get_node("Clickable").on_grab_down.connect(func(event): + if !edit_enabled: + return + moving = event.target ) diff --git a/content/ui/menu/room/room_menu.tscn b/content/ui/menu/room/room_menu.tscn index 880a387..3f44423 100644 --- a/content/ui/menu/room/room_menu.tscn +++ b/content/ui/menu/room/room_menu.tscn @@ -1,10 +1,17 @@ -[gd_scene load_steps=4 format=3 uid="uid://c01gkeldvjwtr"] +[gd_scene load_steps=8 format=3 uid="uid://c01gkeldvjwtr"] [ext_resource type="Script" path="res://content/ui/menu/room/room_menu.gd" id="1_ch4jb"] [ext_resource type="Script" path="res://content/functions/clickable.gd" id="2_elugy"] +[ext_resource type="Material" uid="uid://bbx6fv7jq50tr" path="res://content/ui/menu/room/walls.tres" id="3_fke3j"] +[ext_resource type="PackedScene" uid="uid://bsjqdvkt0u87c" path="res://content/ui/components/button/button.tscn" id="3_whl7a"] [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_08sv0"] +[sub_resource type="ArrayMesh" id="ArrayMesh_7dibq"] + +[sub_resource type="BoxMesh" id="BoxMesh_e37nn"] +size = Vector3(0.3, 0.01, 0.3) + [node name="RoomMenu" type="Node3D"] script = ExtResource("1_ch4jb") @@ -21,3 +28,26 @@ script = ExtResource("2_elugy") [node name="WallCorners" type="Node3D" parent="TeleportRoot"] [node name="WallEdges" type="Node3D" parent="TeleportRoot"] + +[node name="WallMesh" type="MeshInstance3D" parent="TeleportRoot"] +material_override = ExtResource("3_fke3j") +mesh = SubResource("ArrayMesh_7dibq") + +[node name="Background" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.15, 0, 0.15) +mesh = SubResource("BoxMesh_e37nn") + +[node name="Interface" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.01, 0) + +[node name="ToggleEdit" parent="Interface" instance=ExtResource("3_whl7a")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.05, 0, 0.05) +toggleable = true + +[node name="Clickable" type="Node" parent="Interface/ToggleEdit"] +script = ExtResource("2_elugy") + +[node name="Label3D" type="Label3D" parent="Interface"] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.17, -1.86265e-09, 0.05) +pixel_size = 0.001 +text = "Edit Room" diff --git a/content/ui/menu/room/walls.tres b/content/ui/menu/room/walls.tres new file mode 100644 index 0000000..bbbac1b --- /dev/null +++ b/content/ui/menu/room/walls.tres @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a84837d131d1a1676286c56bc80fc1dde59dc234ca68dc089b86d19c2aefc5a7 +size 101