Skip to content

Instantly share code, notes, and snippets.

@me2beats
Last active November 22, 2021 02:19
Show Gist options
  • Save me2beats/bd5cd53d93b125f7175d2b49371d296e to your computer and use it in GitHub Desktop.
Save me2beats/bd5cd53d93b125f7175d2b49371d296e to your computer and use it in GitHub Desktop.
auto_pos_3d
tool
extends EditorPlugin
var ray_length = 1000
var position3d_node_name = "Pos3D"
func handles(object):
return true
func _reset_all_colliders(node) -> void:
if node is CollisionShape and not node.disabled:
# print("reset: ", node)
node.disabled = true
node.disabled = false
for c in node.get_children():
_reset_all_colliders(c)
func _on_clicked(camera:Camera, event:InputEventMouseButton):
var current_root: = get_editor_interface().get_edited_scene_root()
_reset_all_colliders(current_root)
var mouse_pos = event.position
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * ray_length
# var space_state = camera.get_world().direct_space_state
var space_state = get_viewport().get_world().direct_space_state
var result = space_state.intersect_ray( from, to )
if !result: return
# assuming its a Spatial
var pos3d:Position3D
pos3d = current_root.get_node_or_null(position3d_node_name)
if !pos3d:
pos3d = Position3D.new()
pos3d.name = position3d_node_name
current_root.add_child(pos3d)
pos3d.owner = current_root
pos3d.translation = result.position
pos3d.rotation = Vector3.ZERO
var selection = get_editor_interface().get_selection()
selection.clear()
selection.add_node(pos3d)
# detect mouse click, ignore dragging
var recent_button_down:=Vector2.ZERO
func forward_spatial_gui_input(camera:Camera, event:InputEvent):
event = event as InputEventMouseButton
if not event or !event.button_index == BUTTON_MIDDLE: return
if event.pressed:
recent_button_down = event.position
else:
if ! event.position == recent_button_down: return
_on_clicked(camera, event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment