forked from DigitallyTailored/godot-runtime-node-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeSelector.gd
More file actions
73 lines (57 loc) · 2.59 KB
/
RuntimeSelector.gd
File metadata and controls
73 lines (57 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extends Node3D
var collision_added = []
var collision_nodes = [] # List to keep track of the generated collision nodes
@onready var window: Window = $".."
@onready var free_camera: FreeLookCamera = $"../FreeCamera"
func _ready():
# Make sure to enable input processing
set_process_input(true)
func _input(event: InputEvent):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
# Detect click
iterative_add_collision()
var space_state = get_world_3d().direct_space_state
var mouse_vector2 = window.get_viewport().get_mouse_position()
var raycast_origin_vector3 = free_camera.project_ray_origin(mouse_vector2)
var raycast_end = free_camera.project_position(mouse_vector2, 1000)
var query = PhysicsRayQueryParameters3D.create(raycast_origin_vector3, raycast_end)
var intersect = space_state.intersect_ray(query)
remove_added_collision()
if intersect and intersect.collider:
var clicked_path = intersect.collider.get_parent().get_path()
EngineDebugger.send_message("clicked_node:node", [clicked_path])
func iterative_add_collision(node: Node = get_tree().root):
if node.is_class("MeshInstance3D"):
if not node.is_class("CollisionShape3D") and not node.is_class("StaticBody3D"):
collision_added.append(node)
else:
pass
for childNode in node.get_children():
iterative_add_collision(childNode)
# Temporarily add collision to these nodes
for collision_node in collision_added:
add_collision_to_node(collision_node)
func add_collision_to_node(node: MeshInstance3D):
# Create a StaticBody for collisions
var collision_body = StaticBody3D.new()
collision_body.name = "GeneratedCollisionBody"
collision_body.input_ray_pickable = true
# Create a CollisionShape for the StaticBody
var collision_shape = CollisionShape3D.new()
var shape = BoxShape3D.new()
var mesh = node.mesh
if mesh:
# Calculate the extents based on the AABB (Axis-Aligned Bounding Box) of the mesh
var aabb = mesh.get_aabb()
shape.extents = aabb.size * 0.5 # Extents is half the size of the AABB
collision_shape.shape = shape
collision_body.add_child(collision_shape)
collision_shape.owner = collision_body
node.add_child(collision_body)
collision_body.owner = node
collision_nodes.append(collision_body) # Add the collision body to the list of collision nodes
func remove_added_collision():
for collision_node in collision_nodes:
collision_node.queue_free() # Queue the generated collision body for deletion
collision_added.clear() # Clear the list of nodes that had collision added
collision_nodes.clear() # Clear the list of generated collision nodes