Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gizmos without visible geometry not being selectable #94553

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions editor/plugins/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ void EditorNode3DGizmo::redraw() {
gizmo_plugin->redraw(this);
}

_update_bvh();

if (Node3DEditor::get_singleton()->is_current_selected_gizmo(this)) {
Node3DEditor::get_singleton()->update_transform_gizmo();
}
Expand Down Expand Up @@ -244,6 +246,32 @@ void EditorNode3DGizmo::add_mesh(const Ref<Mesh> &p_mesh, const Ref<Material> &p
instances.push_back(ins);
}

void EditorNode3DGizmo::_update_bvh() {
ERR_FAIL_NULL(spatial_node);

Transform3D transform = spatial_node->get_global_transform();

float effective_icon_size = selectable_icon_size > 0.0f ? selectable_icon_size : 0.0f;
Vector3 icon_size_vector3 = Vector3(effective_icon_size, effective_icon_size, effective_icon_size);
AABB aabb(spatial_node->get_position() - icon_size_vector3 * 100.0f, icon_size_vector3 * 200.0f);

for (const Vector3 &segment_end : collision_segments) {
aabb.expand_to(transform.xform(segment_end));
}

if (collision_mesh.is_valid()) {
for (const Face3 &face : collision_mesh->get_faces()) {
aabb.expand_to(transform.xform(face.vertex[0]));
aabb.expand_to(transform.xform(face.vertex[1]));
aabb.expand_to(transform.xform(face.vertex[2]));
}
}

Node3DEditor::get_singleton()->update_gizmo_bvh_node(
bvh_node_id,
aabb);
}

void EditorNode3DGizmo::add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard, const Color &p_modulate) {
add_vertices(p_lines, p_material, Mesh::PRIMITIVE_LINES, p_billboard, p_modulate);
}
Expand Down Expand Up @@ -765,6 +793,10 @@ void EditorNode3DGizmo::create() {
instances.write[i].create_instance(spatial_node, hidden);
}

bvh_node_id = Node3DEditor::get_singleton()->insert_gizmo_bvh_node(
spatial_node,
AABB(spatial_node->get_position(), Vector3(0, 0, 0)));

transform();
}

Expand All @@ -774,6 +806,8 @@ void EditorNode3DGizmo::transform() {
for (int i = 0; i < instances.size(); i++) {
RS::get_singleton()->instance_set_transform(instances[i].instance, spatial_node->get_global_transform() * instances[i].xform);
}

_update_bvh();
}

void EditorNode3DGizmo::free() {
Expand All @@ -790,6 +824,9 @@ void EditorNode3DGizmo::free() {

clear();

Node3DEditor::get_singleton()->remove_gizmo_bvh_node(bvh_node_id);
bvh_node_id = DynamicBVH::ID();

valid = false;
}

Expand Down
5 changes: 5 additions & 0 deletions editor/plugins/node_3d_editor_gizmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef NODE_3D_EDITOR_GIZMOS_H
#define NODE_3D_EDITOR_GIZMOS_H

#include "core/math/dynamic_bvh.h"
#include "core/templates/hash_map.h"
#include "core/templates/local_vector.h"
#include "scene/3d/camera_3d.h"
Expand Down Expand Up @@ -72,8 +73,12 @@ class EditorNode3DGizmo : public Node3DGizmo {
Vector<Instance> instances;
Node3D *spatial_node = nullptr;

DynamicBVH::ID bvh_node_id;

void _set_node_3d(Node *p_node) { set_node_3d(Object::cast_to<Node3D>(p_node)); }

void _update_bvh();

protected:
static void _bind_methods();

Expand Down
60 changes: 50 additions & 10 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,17 +800,16 @@ ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos) const {
RS::get_singleton()->sdfgi_set_debug_probe_select(pos, ray);
}

Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
HashSet<Ref<EditorNode3DGizmo>> found_gizmos;

Node *edited_scene = get_tree()->get_edited_scene_root();
ObjectID closest;
Node *item = nullptr;
float closest_dist = 1e20;

for (int i = 0; i < instances.size(); i++) {
Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_ray_query(pos, pos + ray * camera->get_far());

for (Node3D *spat : nodes_with_gizmos) {
if (!spat) {
continue;
}
Expand Down Expand Up @@ -863,12 +862,11 @@ void Node3DEditorViewport::_find_items_at_pos(const Point2 &p_pos, Vector<_RayRe
Vector3 ray = get_ray(p_pos);
Vector3 pos = get_ray_pos(p_pos);

Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
HashSet<Node3D *> found_nodes;
Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_ray_query(pos, pos + ray * camera->get_far());

for (int i = 0; i < instances.size(); i++) {
Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
HashSet<Node3D *> found_nodes;

for (Node3D *spat : nodes_with_gizmos) {
if (!spat) {
continue;
}
Expand Down Expand Up @@ -1046,14 +1044,13 @@ void Node3DEditorViewport::_select_region() {
_clear_selected();
}

Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world_3d()->get_scenario());
Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_frustum_query(frustum);
HashSet<Node3D *> found_nodes;
Vector<Node *> selected;

Node *edited_scene = get_tree()->get_edited_scene_root();

for (int i = 0; i < instances.size(); i++) {
Node3D *sp = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
for (Node3D *sp : nodes_with_gizmos) {
if (!sp || _is_node_locked(sp)) {
continue;
}
Expand Down Expand Up @@ -9205,6 +9202,49 @@ void Node3DEditor::remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) {
_update_gizmos_menu();
}

DynamicBVH::ID Node3DEditor::insert_gizmo_bvh_node(Node3D *p_node, const AABB &p_aabb) {
return gizmo_bvh.insert(p_aabb, p_node);
}

void Node3DEditor::update_gizmo_bvh_node(DynamicBVH::ID p_id, const AABB &p_aabb) {
gizmo_bvh.update(p_id, p_aabb);
gizmo_bvh.optimize_incremental(1);
}

void Node3DEditor::remove_gizmo_bvh_node(DynamicBVH::ID p_id) {
gizmo_bvh.remove(p_id);
}

Vector<Node3D *> Node3DEditor::gizmo_bvh_ray_query(const Vector3 &p_ray_start, const Vector3 &p_ray_end) {
struct Result {
Vector<Node3D *> nodes;
bool operator()(void *p_data) {
nodes.append((Node3D *)p_data);
return false;
}
} result;

gizmo_bvh.ray_query(p_ray_start, p_ray_end, result);

return result.nodes;
}

Vector<Node3D *> Node3DEditor::gizmo_bvh_frustum_query(const Vector<Plane> &p_frustum) {
Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&p_frustum[0], p_frustum.size());

struct Result {
Vector<Node3D *> nodes;
bool operator()(void *p_data) {
nodes.append((Node3D *)p_data);
return false;
}
} result;

gizmo_bvh.convex_query(p_frustum.ptr(), p_frustum.size(), points.ptr(), points.size(), result);

return result.nodes;
}

Node3DEditorPlugin::Node3DEditorPlugin() {
spatial_editor = memnew(Node3DEditor);
spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
Expand Down
9 changes: 9 additions & 0 deletions editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef NODE_3D_EDITOR_PLUGIN_H
#define NODE_3D_EDITOR_PLUGIN_H

#include "core/math/dynamic_bvh.h"
#include "editor/plugins/editor_plugin.h"
#include "editor/plugins/node_3d_editor_gizmos.h"
#include "editor/themes/editor_scale.h"
Expand Down Expand Up @@ -629,6 +630,8 @@ class Node3DEditor : public VBoxContainer {
int current_hover_gizmo_handle;
bool current_hover_gizmo_handle_secondary;

DynamicBVH gizmo_bvh;

real_t snap_translate_value;
real_t snap_rotate_value;
real_t snap_scale_value;
Expand Down Expand Up @@ -933,6 +936,12 @@ class Node3DEditor : public VBoxContainer {
void add_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin);
void remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin);

DynamicBVH::ID insert_gizmo_bvh_node(Node3D *p_node, const AABB &p_aabb);
void update_gizmo_bvh_node(DynamicBVH::ID p_id, const AABB &p_aabb);
void remove_gizmo_bvh_node(DynamicBVH::ID p_id);
Vector<Node3D *> gizmo_bvh_ray_query(const Vector3 &p_ray_start, const Vector3 &p_ray_end);
Vector<Node3D *> gizmo_bvh_frustum_query(const Vector<Plane> &p_frustum);

void edit(Node3D *p_spatial);
void clear();

Expand Down
Loading