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

Improve the 3D editor selection box appearance #40106

Merged
merged 1 commit into from
Nov 9, 2020
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
65 changes: 50 additions & 15 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,7 @@ void Node3DEditorViewport::_notification(int p_what) {
t.basis = t.basis * aabb_s;

RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t);
}

if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
Expand Down Expand Up @@ -4417,6 +4418,9 @@ Node3DEditorSelectedItem::~Node3DEditorSelectedItem() {
if (sbox_instance.is_valid()) {
RenderingServer::get_singleton()->free(sbox_instance);
}
if (sbox_instance_xray.is_valid()) {
RenderingServer::get_singleton()->free(sbox_instance_xray);
}
}

void Node3DEditor::select_gizmo_highlight_axis(int p_axis) {
Expand Down Expand Up @@ -4500,42 +4504,73 @@ Object *Node3DEditor::_get_editor_data(Object *p_what) {
Node3DEditorSelectedItem *si = memnew(Node3DEditorSelectedItem);

si->sp = sp;
si->sbox_instance = RenderingServer::get_singleton()->instance_create2(selection_box->get_rid(), sp->get_world_3d()->get_scenario());
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, RS::SHADOW_CASTING_SETTING_OFF);
si->sbox_instance = RenderingServer::get_singleton()->instance_create2(
selection_box->get_rid(),
sp->get_world_3d()->get_scenario());
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
si->sbox_instance,
RS::SHADOW_CASTING_SETTING_OFF);
si->sbox_instance_xray = RenderingServer::get_singleton()->instance_create2(
selection_box_xray->get_rid(),
sp->get_world_3d()->get_scenario());
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
si->sbox_instance_xray,
RS::SHADOW_CASTING_SETTING_OFF);

return si;
}

void Node3DEditor::_generate_selection_box() {
void Node3DEditor::_generate_selection_boxes() {
// Use two AABBs to create the illusion of a slightly thicker line.
AABB aabb(Vector3(), Vector3(1, 1, 1));
aabb.grow_by(aabb.get_longest_axis_size() / 20.0);

AABB aabb_offset(Vector3(), Vector3(1, 1, 1));
// Grow the bounding boxes slightly to avoid Z-fighting with the mesh's edges.
aabb.grow_by(0.005);
aabb_offset.grow_by(0.01);

// Create a x-ray (visible through solid surfaces) and standard version of the selection box.
// Both will be drawn at the same position, but with different opacity.
// This lets the user see where the selection is while still having a sense of depth.
Ref<SurfaceTool> st = memnew(SurfaceTool);
Ref<SurfaceTool> st_xray = memnew(SurfaceTool);

st->begin(Mesh::PRIMITIVE_LINES);
st_xray->begin(Mesh::PRIMITIVE_LINES);
for (int i = 0; i < 12; i++) {
Vector3 a, b;
aabb.get_edge(i, a, b);

st->add_color(Color(1.0, 1.0, 0.8, 0.8));
st->add_vertex(a);
st->add_color(Color(1.0, 1.0, 0.8, 0.4));
st->add_vertex(a.lerp(b, 0.2));
st->add_vertex(b);
st_xray->add_vertex(a);
st_xray->add_vertex(b);
}

st->add_color(Color(1.0, 1.0, 0.8, 0.4));
st->add_vertex(a.lerp(b, 0.8));
st->add_color(Color(1.0, 1.0, 0.8, 0.8));
for (int i = 0; i < 12; i++) {
Vector3 a, b;
aabb_offset.get_edge(i, a, b);

st->add_vertex(a);
st->add_vertex(b);
st_xray->add_vertex(a);
st_xray->add_vertex(b);
}

Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
mat->set_albedo(Color(1, 1, 1));
// Use a similar color to the 2D editor selection.
mat->set_albedo(Color(1, 0.5, 0));
mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
st->set_material(mat);
selection_box = st->commit();

Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D);
mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
mat_xray->set_albedo(Color(1, 0.5, 0, 0.15));
mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
st_xray->set_material(mat_xray);
selection_box_xray = st_xray->commit();
}

Dictionary Node3DEditor::get_state() const {
Expand Down Expand Up @@ -5514,7 +5549,7 @@ void Node3DEditor::_init_indicators() {
}
}

_generate_selection_box();
_generate_selection_boxes();
}

void Node3DEditor::_update_gizmos_menu() {
Expand Down
4 changes: 3 additions & 1 deletion editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ class Node3DEditorSelectedItem : public Object {
bool last_xform_dirty;
Node3D *sp;
RID sbox_instance;
RID sbox_instance_xray;

Node3DEditorSelectedItem() {
sp = nullptr;
Expand Down Expand Up @@ -613,6 +614,7 @@ class Node3DEditor : public VBoxContainer {
float snap_rotate_value;
float snap_scale_value;

Ref<ArrayMesh> selection_box_xray;
Ref<ArrayMesh> selection_box;
RID indicators;
RID indicators_instance;
Expand Down Expand Up @@ -701,7 +703,7 @@ class Node3DEditor : public VBoxContainer {

HBoxContainer *hbc_menu;

void _generate_selection_box();
void _generate_selection_boxes();
UndoRedo *undo_redo;

int camera_override_viewport_id;
Expand Down