initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_link_3d_editor_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_link_3d_editor_plugin.h"
|
||||
|
||||
#include "editor/scene/3d/node_3d_editor_plugin.h"
|
||||
#include "scene/3d/navigation/navigation_link_3d.h"
|
||||
|
||||
void NavigationLink3DEditorPlugin::edit(Object *p_object) {
|
||||
}
|
||||
|
||||
bool NavigationLink3DEditorPlugin::handles(Object *p_object) const {
|
||||
return Object::cast_to<NavigationLink3D>(p_object) != nullptr;
|
||||
}
|
||||
|
||||
NavigationLink3DEditorPlugin::NavigationLink3DEditorPlugin() {
|
||||
gizmo_plugin.instantiate();
|
||||
Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_link_3d_editor_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/plugins/editor_plugin.h"
|
||||
|
||||
#include "navigation_link_3d_gizmo_plugin.h"
|
||||
|
||||
class NavigationLink3DEditorPlugin : public EditorPlugin {
|
||||
GDCLASS(NavigationLink3DEditorPlugin, EditorPlugin);
|
||||
|
||||
Ref<NavigationLink3DGizmoPlugin> gizmo_plugin;
|
||||
|
||||
public:
|
||||
virtual String get_plugin_name() const override { return "NavigationLink3D"; }
|
||||
bool has_main_screen() const override { return false; }
|
||||
virtual void edit(Object *p_object) override;
|
||||
virtual bool handles(Object *p_object) const override;
|
||||
|
||||
NavigationLink3DEditorPlugin();
|
||||
};
|
272
modules/navigation_3d/editor/navigation_link_3d_gizmo_plugin.cpp
Normal file
272
modules/navigation_3d/editor/navigation_link_3d_gizmo_plugin.cpp
Normal file
@@ -0,0 +1,272 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_link_3d_gizmo_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_link_3d_gizmo_plugin.h"
|
||||
|
||||
#include "editor/editor_undo_redo_manager.h"
|
||||
#include "editor/scene/3d/node_3d_editor_plugin.h"
|
||||
#include "scene/3d/navigation/navigation_link_3d.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
NavigationLink3DGizmoPlugin::NavigationLink3DGizmoPlugin() {
|
||||
create_material("navigation_link_material", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_color());
|
||||
create_material("navigation_link_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_disabled_color());
|
||||
create_handle_material("handles");
|
||||
}
|
||||
|
||||
bool NavigationLink3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
|
||||
return Object::cast_to<NavigationLink3D>(p_spatial) != nullptr;
|
||||
}
|
||||
|
||||
String NavigationLink3DGizmoPlugin::get_gizmo_name() const {
|
||||
return "NavigationLink3D";
|
||||
}
|
||||
|
||||
int NavigationLink3DGizmoPlugin::get_priority() const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
|
||||
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());
|
||||
|
||||
RID nav_map = link->get_world_3d()->get_navigation_map();
|
||||
real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
|
||||
Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
|
||||
Vector3::Axis up_axis = up_vector.max_axis_index();
|
||||
|
||||
Vector3 start_position = link->get_start_position();
|
||||
Vector3 end_position = link->get_end_position();
|
||||
|
||||
Ref<Material> link_material = get_material("navigation_link_material", p_gizmo);
|
||||
Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled", p_gizmo);
|
||||
Ref<Material> handles_material = get_material("handles");
|
||||
|
||||
p_gizmo->clear();
|
||||
|
||||
// Number of points in an octant. So there will be 8 * points_in_octant points in total.
|
||||
// Correspond to the smoothness of the circle.
|
||||
const uint32_t points_in_octant = 8;
|
||||
real_t inc = (Math::PI / (4 * points_in_octant));
|
||||
|
||||
Vector<Vector3> lines;
|
||||
// points_in_octant * 8 * 2 per circle * 2 circles. 2 for the start-end. 4 for the arrow, and another 4 if bidirectionnal.
|
||||
lines.resize(points_in_octant * 8 * 2 * 2 + 2 + 4 + (link->is_bidirectional() ? 4 : 0));
|
||||
uint32_t index = 0;
|
||||
Vector3 *lines_ptrw = lines.ptrw();
|
||||
// Draw line between the points.
|
||||
lines_ptrw[index++] = start_position;
|
||||
lines_ptrw[index++] = end_position;
|
||||
real_t search_radius_squared = search_radius * search_radius;
|
||||
|
||||
// Draw circles at start and end positions in one go.
|
||||
real_t r = 0;
|
||||
Vector2 a = Vector2(search_radius, 0);
|
||||
for (uint32_t i = 0; i < points_in_octant; i++) {
|
||||
r += inc;
|
||||
real_t x = Math::cos(r) * search_radius;
|
||||
real_t y = Math::sqrt(search_radius_squared - (x * x));
|
||||
|
||||
// Draw axis-aligned circle.
|
||||
switch (up_axis) {
|
||||
case Vector3::AXIS_X:
|
||||
#define PUSH_OCTANT(_position, a, b) \
|
||||
lines_ptrw[index++] = _position + Vector3(0, a.x, a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, x, y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -a.x, a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, x, y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, a.x, -a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, x, -y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -a.x, -a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, x, y); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, a.y, a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, y, x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -a.y, a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -y, x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, a.y, -a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, y, -x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -a.y, -a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(0, -y, -x);
|
||||
|
||||
PUSH_OCTANT(start_position, a, b)
|
||||
PUSH_OCTANT(end_position, a, b)
|
||||
#undef PUSH_OCTANT
|
||||
break;
|
||||
case Vector3::AXIS_Y:
|
||||
#define PUSH_OCTANT(_position, a, b) \
|
||||
lines_ptrw[index++] = _position + Vector3(a.x, 0, a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(x, 0, y); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.x, 0, a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(-x, 0, y); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.x, 0, -a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(x, 0, -y); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.x, 0, -a.y); \
|
||||
lines_ptrw[index++] = _position + Vector3(-x, 0, -y); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.y, 0, a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(y, 0, x); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.y, 0, a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(-y, 0, x); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.y, 0, -a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(y, 0, -x); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.y, 0, -a.x); \
|
||||
lines_ptrw[index++] = _position + Vector3(-y, 0, -x);
|
||||
|
||||
PUSH_OCTANT(start_position, a, b)
|
||||
PUSH_OCTANT(end_position, a, b)
|
||||
#undef PUSH_OCTANT
|
||||
break;
|
||||
case Vector3::AXIS_Z:
|
||||
#define PUSH_OCTANT(_position, a, b) \
|
||||
lines_ptrw[index++] = _position + Vector3(a.x, a.y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(x, y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.x, a.y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-x, y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.x, -a.y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(x, -y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.x, -a.y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-x, -y, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.y, a.x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(y, x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.y, a.x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-y, x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(a.y, -a.x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(y, -x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-a.y, -a.x, 0); \
|
||||
lines_ptrw[index++] = _position + Vector3(-y, -x, 0);
|
||||
|
||||
PUSH_OCTANT(start_position, a, b)
|
||||
PUSH_OCTANT(end_position, a, b)
|
||||
#undef PUSH_OCTANT
|
||||
break;
|
||||
}
|
||||
a.x = x;
|
||||
a.y = y;
|
||||
}
|
||||
|
||||
const Vector3 link_segment = end_position - start_position;
|
||||
const Vector3 up = Vector3(0.0, 1.0, 0.0);
|
||||
const float arror_len = 0.5;
|
||||
|
||||
{
|
||||
Vector3 anchor = start_position + (link_segment * 0.75);
|
||||
Vector3 direction = start_position.direction_to(end_position);
|
||||
Vector3 arrow_dir = direction.cross(up);
|
||||
lines_ptrw[index++] = anchor;
|
||||
lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len;
|
||||
|
||||
arrow_dir = -direction.cross(up);
|
||||
lines_ptrw[index++] = anchor;
|
||||
lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len;
|
||||
}
|
||||
|
||||
if (link->is_bidirectional()) {
|
||||
Vector3 anchor = start_position + (link_segment * 0.25);
|
||||
Vector3 direction = end_position.direction_to(start_position);
|
||||
Vector3 arrow_dir = direction.cross(up);
|
||||
lines_ptrw[index++] = anchor;
|
||||
lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len;
|
||||
|
||||
arrow_dir = -direction.cross(up);
|
||||
lines_ptrw[index++] = anchor;
|
||||
lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len;
|
||||
}
|
||||
|
||||
p_gizmo->add_lines(lines, link->is_enabled() ? link_material : link_material_disabled);
|
||||
p_gizmo->add_collision_segments(lines);
|
||||
|
||||
p_gizmo->add_handles(Vector({ start_position, end_position }), handles_material);
|
||||
}
|
||||
|
||||
String NavigationLink3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
|
||||
return p_id == 0 ? TTR("Start Position") : TTR("End Position");
|
||||
}
|
||||
|
||||
Variant NavigationLink3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
|
||||
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());
|
||||
return p_id == 0 ? link->get_start_position() : link->get_end_position();
|
||||
}
|
||||
|
||||
void NavigationLink3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
|
||||
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());
|
||||
|
||||
Transform3D gt = link->get_global_transform();
|
||||
Transform3D gi = gt.affine_inverse();
|
||||
|
||||
Transform3D ct = p_camera->get_global_transform();
|
||||
Vector3 cam_dir = ct.basis.get_column(Vector3::AXIS_Z);
|
||||
|
||||
Vector3 ray_from = p_camera->project_ray_origin(p_point);
|
||||
Vector3 ray_dir = p_camera->project_ray_normal(p_point);
|
||||
|
||||
Vector3 position = p_id == 0 ? link->get_start_position() : link->get_end_position();
|
||||
Plane move_plane = Plane(cam_dir, gt.xform(position));
|
||||
|
||||
Vector3 intersection;
|
||||
if (!move_plane.intersects_ray(ray_from, ray_dir, &intersection)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
|
||||
double snap = Node3DEditor::get_singleton()->get_translate_snap();
|
||||
intersection.snapf(snap);
|
||||
}
|
||||
|
||||
position = gi.xform(intersection);
|
||||
if (p_id == 0) {
|
||||
link->set_start_position(position);
|
||||
} else if (p_id == 1) {
|
||||
link->set_end_position(position);
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationLink3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
|
||||
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());
|
||||
|
||||
if (p_cancel) {
|
||||
if (p_id == 0) {
|
||||
link->set_start_position(p_restore);
|
||||
} else {
|
||||
link->set_end_position(p_restore);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
|
||||
if (p_id == 0) {
|
||||
ur->create_action(TTR("Change Start Position"));
|
||||
ur->add_do_method(link, "set_start_position", link->get_start_position());
|
||||
ur->add_undo_method(link, "set_start_position", p_restore);
|
||||
} else {
|
||||
ur->create_action(TTR("Change End Position"));
|
||||
ur->add_do_method(link, "set_end_position", link->get_end_position());
|
||||
ur->add_undo_method(link, "set_end_position", p_restore);
|
||||
}
|
||||
|
||||
ur->commit_action();
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_link_3d_gizmo_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/scene/3d/node_3d_editor_gizmos.h"
|
||||
|
||||
class NavigationLink3DGizmoPlugin : public EditorNode3DGizmoPlugin {
|
||||
GDCLASS(NavigationLink3DGizmoPlugin, EditorNode3DGizmoPlugin);
|
||||
|
||||
public:
|
||||
bool has_gizmo(Node3D *p_spatial) override;
|
||||
String get_gizmo_name() const override;
|
||||
int get_priority() const override;
|
||||
void redraw(EditorNode3DGizmo *p_gizmo) override;
|
||||
|
||||
String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
|
||||
Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
|
||||
void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override;
|
||||
void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;
|
||||
|
||||
NavigationLink3DGizmoPlugin();
|
||||
};
|
@@ -0,0 +1,901 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_obstacle_3d_editor_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_obstacle_3d_editor_plugin.h"
|
||||
|
||||
#include "core/math/geometry_2d.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/editor_string_names.h"
|
||||
#include "editor/editor_undo_redo_manager.h"
|
||||
#include "editor/scene/3d/node_3d_editor_plugin.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
#include "scene/3d/navigation/navigation_obstacle_3d.h"
|
||||
#include "scene/gui/button.h"
|
||||
#include "scene/gui/dialogs.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
bool NavigationObstacle3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
|
||||
return Object::cast_to<NavigationObstacle3D>(p_spatial) != nullptr;
|
||||
}
|
||||
|
||||
String NavigationObstacle3DGizmoPlugin::get_gizmo_name() const {
|
||||
return "NavigationObstacle3D";
|
||||
}
|
||||
|
||||
void NavigationObstacle3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
|
||||
p_gizmo->clear();
|
||||
|
||||
if (!p_gizmo->is_selected() && get_state() == HIDDEN) {
|
||||
return;
|
||||
}
|
||||
|
||||
NavigationObstacle3D *obstacle = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
|
||||
if (!obstacle) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Vector<Vector3> &vertices = obstacle->get_vertices();
|
||||
if (vertices.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
float height = obstacle->get_height();
|
||||
|
||||
const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle->get_global_rotation().y, obstacle->get_global_basis().get_scale().abs().maxf(0.001));
|
||||
const Basis gbi = obstacle->get_global_basis().inverse();
|
||||
const Basis safe_global_basis = gbi * safe_basis;
|
||||
const int vertex_count = vertices.size();
|
||||
|
||||
Vector<Vector3> lines_mesh_vertices;
|
||||
lines_mesh_vertices.resize(vertex_count * 8);
|
||||
Vector3 *lines_mesh_vertices_ptrw = lines_mesh_vertices.ptrw();
|
||||
|
||||
int vertex_index = 0;
|
||||
|
||||
for (int i = 0; i < vertex_count; i++) {
|
||||
Vector3 point = vertices[i];
|
||||
Vector3 next_point = vertices[(i + 1) % vertex_count];
|
||||
|
||||
Vector3 direction = safe_basis.xform(next_point.direction_to(point));
|
||||
Vector3 arrow_dir = direction.cross(Vector3(0.0, 1.0, 0.0));
|
||||
Vector3 edge_middle = point + ((next_point - point) * 0.5);
|
||||
|
||||
// Ensure vector stays perpendicular even when scaled non-uniformly.
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(edge_middle);
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(edge_middle) + gbi.xform(arrow_dir) * 0.5;
|
||||
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(point);
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(next_point);
|
||||
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(point.x, height, point.z));
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(next_point.x, height, next_point.z));
|
||||
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(point);
|
||||
lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(point.x, height, point.z));
|
||||
}
|
||||
|
||||
NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
|
||||
|
||||
if (obstacle->are_vertices_valid()) {
|
||||
p_gizmo->add_lines(lines_mesh_vertices, ns3d->get_debug_navigation_avoidance_static_obstacle_pushout_edge_material());
|
||||
} else {
|
||||
p_gizmo->add_lines(lines_mesh_vertices, ns3d->get_debug_navigation_avoidance_static_obstacle_pushin_edge_material());
|
||||
}
|
||||
p_gizmo->add_collision_segments(lines_mesh_vertices);
|
||||
|
||||
if (p_gizmo->is_selected()) {
|
||||
NavigationObstacle3DEditorPlugin::singleton->redraw();
|
||||
}
|
||||
}
|
||||
|
||||
bool NavigationObstacle3DGizmoPlugin::can_be_hidden() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
int NavigationObstacle3DGizmoPlugin::get_priority() const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int NavigationObstacle3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
|
||||
if (NavigationObstacle3DEditorPlugin::singleton->get_mode() != 1) { // MODE_EDIT
|
||||
return -1;
|
||||
}
|
||||
|
||||
NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
ERR_FAIL_NULL_V(obstacle_node, -1);
|
||||
|
||||
const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
|
||||
const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
|
||||
const Vector<Vector3> &vertices = obstacle_node->get_vertices();
|
||||
|
||||
for (int idx = 0; idx < vertices.size(); ++idx) {
|
||||
Vector3 pos = gt.xform(vertices[idx]);
|
||||
if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
Vector<int> NavigationObstacle3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
|
||||
Vector<int> contained_points;
|
||||
if (NavigationObstacle3DEditorPlugin::singleton->get_mode() != 1) { // MODE_EDIT
|
||||
return contained_points;
|
||||
}
|
||||
|
||||
NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
ERR_FAIL_NULL_V(obstacle_node, contained_points);
|
||||
|
||||
const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
|
||||
const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
|
||||
const Vector<Vector3> &vertices = obstacle_node->get_vertices();
|
||||
|
||||
for (int idx = 0; idx < vertices.size(); ++idx) {
|
||||
Vector3 pos = gt.xform(vertices[idx]);
|
||||
bool is_contained_in_frustum = true;
|
||||
for (int i = 0; i < p_frustum.size(); ++i) {
|
||||
if (p_frustum[i].distance_to(pos) > 0) {
|
||||
is_contained_in_frustum = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_contained_in_frustum) {
|
||||
contained_points.push_back(idx);
|
||||
}
|
||||
}
|
||||
|
||||
return contained_points;
|
||||
}
|
||||
|
||||
Transform3D NavigationObstacle3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
|
||||
NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
ERR_FAIL_NULL_V(obstacle_node, Transform3D());
|
||||
|
||||
const Vector<Vector3> &vertices = obstacle_node->get_vertices();
|
||||
ERR_FAIL_INDEX_V(p_id, vertices.size(), Transform3D());
|
||||
|
||||
const Basis safe_basis_inverse = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001)).inverse();
|
||||
Transform3D subgizmo_transform = Transform3D(Basis(), safe_basis_inverse.xform(vertices[p_id]));
|
||||
return subgizmo_transform;
|
||||
}
|
||||
|
||||
void NavigationObstacle3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
|
||||
NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
ERR_FAIL_NULL(obstacle_node);
|
||||
|
||||
const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001));
|
||||
Vector3 new_vertex_pos = p_transform.origin;
|
||||
|
||||
Vector<Vector3> vertices = obstacle_node->get_vertices();
|
||||
ERR_FAIL_INDEX(p_id, vertices.size());
|
||||
|
||||
Vector3 vertex = safe_basis.xform(new_vertex_pos);
|
||||
vertex.y = 0.0;
|
||||
vertices.write[p_id] = vertex;
|
||||
|
||||
obstacle_node->set_vertices(vertices);
|
||||
}
|
||||
|
||||
void NavigationObstacle3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
|
||||
NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
|
||||
ERR_FAIL_NULL(obstacle_node);
|
||||
|
||||
const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001));
|
||||
|
||||
Vector<Vector3> vertices = obstacle_node->get_vertices();
|
||||
Vector<Vector3> restore_vertices = vertices;
|
||||
|
||||
for (int i = 0; i < p_ids.size(); ++i) {
|
||||
const int idx = p_ids[i];
|
||||
Vector3 vertex = safe_basis.xform(p_restore[i].origin);
|
||||
vertex.y = 0.0;
|
||||
restore_vertices.write[idx] = vertex;
|
||||
}
|
||||
|
||||
if (p_cancel) {
|
||||
obstacle_node->set_vertices(restore_vertices);
|
||||
return;
|
||||
}
|
||||
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Set Obstacle Vertices"));
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", vertices);
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", restore_vertices);
|
||||
undo_redo->commit_action();
|
||||
}
|
||||
|
||||
NavigationObstacle3DGizmoPlugin::NavigationObstacle3DGizmoPlugin() {
|
||||
current_state = VISIBLE;
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::_notification(int p_what) {
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_ENTER_TREE: {
|
||||
_update_theme();
|
||||
} break;
|
||||
|
||||
case NOTIFICATION_READY: {
|
||||
_update_theme();
|
||||
button_edit->set_pressed(true);
|
||||
get_tree()->connect("node_removed", callable_mp(this, &NavigationObstacle3DEditorPlugin::_node_removed));
|
||||
EditorNode::get_singleton()->get_gui_base()->connect(SceneStringName(theme_changed), callable_mp(this, &NavigationObstacle3DEditorPlugin::_update_theme));
|
||||
} break;
|
||||
|
||||
case NOTIFICATION_EXIT_TREE: {
|
||||
get_tree()->disconnect("node_removed", callable_mp(this, &NavigationObstacle3DEditorPlugin::_node_removed));
|
||||
EditorNode::get_singleton()->get_gui_base()->disconnect(SceneStringName(theme_changed), callable_mp(this, &NavigationObstacle3DEditorPlugin::_update_theme));
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::edit(Object *p_object) {
|
||||
obstacle_node = Object::cast_to<NavigationObstacle3D>(p_object);
|
||||
|
||||
RenderingServer *rs = RenderingServer::get_singleton();
|
||||
|
||||
if (obstacle_node) {
|
||||
if (obstacle_node->get_vertices().is_empty()) {
|
||||
set_mode(MODE_CREATE);
|
||||
} else {
|
||||
set_mode(MODE_EDIT);
|
||||
}
|
||||
wip_vertices.clear();
|
||||
wip_active = false;
|
||||
edited_point = -1;
|
||||
|
||||
rs->instance_set_scenario(point_lines_instance_rid, obstacle_node->get_world_3d()->get_scenario());
|
||||
rs->instance_set_scenario(point_handles_instance_rid, obstacle_node->get_world_3d()->get_scenario());
|
||||
|
||||
redraw();
|
||||
|
||||
} else {
|
||||
obstacle_node = nullptr;
|
||||
|
||||
rs->mesh_clear(point_lines_mesh_rid);
|
||||
rs->mesh_clear(point_handle_mesh_rid);
|
||||
rs->instance_set_scenario(point_lines_instance_rid, RID());
|
||||
rs->instance_set_scenario(point_handles_instance_rid, RID());
|
||||
}
|
||||
}
|
||||
|
||||
bool NavigationObstacle3DEditorPlugin::handles(Object *p_object) const {
|
||||
return Object::cast_to<NavigationObstacle3D>(p_object);
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::make_visible(bool p_visible) {
|
||||
if (p_visible) {
|
||||
obstacle_editor->show();
|
||||
} else {
|
||||
obstacle_editor->hide();
|
||||
edit(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::action_flip_vertices() {
|
||||
if (!obstacle_node) {
|
||||
return;
|
||||
}
|
||||
|
||||
Vector<Vector3> flipped_vertices = obstacle_node->get_vertices();
|
||||
flipped_vertices.reverse();
|
||||
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Flip Winding)"));
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", flipped_vertices);
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
|
||||
undo_redo->commit_action();
|
||||
|
||||
obstacle_node->update_gizmos();
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::action_clear_vertices() {
|
||||
if (!obstacle_node) {
|
||||
return;
|
||||
}
|
||||
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Clear Vertices)"));
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", Vector<Vector3>());
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
|
||||
undo_redo->commit_action();
|
||||
|
||||
obstacle_node->update_gizmos();
|
||||
edit(obstacle_node);
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::_update_theme() {
|
||||
button_create->set_tooltip_text(TTR("Add Vertex"));
|
||||
button_edit->set_tooltip_text(TTR("Edit Vertex"));
|
||||
button_delete->set_tooltip_text(TTR("Delete Vertex"));
|
||||
button_flip->set_tooltip_text(TTR("Flip Winding"));
|
||||
button_clear->set_tooltip_text(TTR("Clear Vertices"));
|
||||
button_create->set_button_icon(button_create->get_editor_theme_icon(SNAME("CurveCreate")));
|
||||
button_edit->set_button_icon(button_edit->get_editor_theme_icon(SNAME("CurveEdit")));
|
||||
button_delete->set_button_icon(button_delete->get_editor_theme_icon(SNAME("CurveDelete")));
|
||||
button_flip->set_button_icon(button_flip->get_editor_theme_icon(SNAME("FlipWinding")));
|
||||
button_clear->set_button_icon(button_clear->get_editor_theme_icon(SNAME("Clear")));
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::_node_removed(Node *p_node) {
|
||||
if (obstacle_node == p_node) {
|
||||
obstacle_node = nullptr;
|
||||
|
||||
RenderingServer *rs = RenderingServer::get_singleton();
|
||||
rs->mesh_clear(point_lines_mesh_rid);
|
||||
rs->mesh_clear(point_handle_mesh_rid);
|
||||
|
||||
obstacle_editor->hide();
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::set_mode(int p_option) {
|
||||
if (p_option == NavigationObstacle3DEditorPlugin::ACTION_FLIP) {
|
||||
button_flip->set_pressed(false);
|
||||
action_flip_vertices();
|
||||
return;
|
||||
}
|
||||
|
||||
if (p_option == NavigationObstacle3DEditorPlugin::ACTION_CLEAR) {
|
||||
button_clear->set_pressed(false);
|
||||
button_clear_dialog->reset_size();
|
||||
button_clear_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
|
||||
mode = p_option;
|
||||
|
||||
button_create->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_CREATE);
|
||||
button_edit->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_EDIT);
|
||||
button_delete->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_DELETE);
|
||||
button_flip->set_pressed(false);
|
||||
button_clear->set_pressed(false);
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::_wip_cancel() {
|
||||
wip_vertices.clear();
|
||||
wip_active = false;
|
||||
|
||||
edited_point = -1;
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::_wip_close() {
|
||||
ERR_FAIL_NULL_MSG(obstacle_node, "Edited NavigationObstacle3D is not valid.");
|
||||
|
||||
Vector<Vector2> wip_2d_vertices;
|
||||
wip_2d_vertices.resize(wip_vertices.size());
|
||||
for (int i = 0; i < wip_vertices.size(); i++) {
|
||||
const Vector3 &vert = wip_vertices[i];
|
||||
wip_2d_vertices.write[i] = Vector2(vert.x, vert.z);
|
||||
}
|
||||
Vector<int> triangulated_polygon_2d_indices = Geometry2D::triangulate_polygon(wip_2d_vertices);
|
||||
|
||||
if (!triangulated_polygon_2d_indices.is_empty()) {
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Set Obstacle Vertices"));
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", wip_vertices);
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
|
||||
undo_redo->commit_action();
|
||||
|
||||
wip_vertices.clear();
|
||||
wip_active = false;
|
||||
//mode = MODE_EDIT;
|
||||
NavigationObstacle3DEditorPlugin::singleton->set_mode(NavigationObstacle3DEditorPlugin::MODE_EDIT);
|
||||
button_edit->set_pressed(true);
|
||||
button_create->set_pressed(false);
|
||||
edited_point = -1;
|
||||
}
|
||||
}
|
||||
|
||||
EditorPlugin::AfterGUIInput NavigationObstacle3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
|
||||
if (!obstacle_node) {
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
if (!obstacle_node->is_visible_in_tree()) {
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
Ref<InputEventMouse> mouse_event = p_event;
|
||||
|
||||
if (mouse_event.is_null()) {
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
Ref<InputEventMouseButton> mb = p_event;
|
||||
|
||||
if (mb.is_valid()) {
|
||||
Vector2 mouse_position = mb->get_position();
|
||||
Vector3 ray_from = p_camera->project_ray_origin(mouse_position);
|
||||
Vector3 ray_dir = p_camera->project_ray_normal(mouse_position);
|
||||
|
||||
const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
|
||||
const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
|
||||
Transform3D gi = gt.affine_inverse();
|
||||
Plane projection_plane(Vector3(0.0, 1.0, 0.0), gt.origin);
|
||||
|
||||
Vector3 spoint;
|
||||
|
||||
if (!projection_plane.intersects_ray(ray_from, ray_dir, &spoint)) {
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
spoint = gi.xform(spoint);
|
||||
|
||||
Vector3 cpoint = Vector3(spoint.x, 0.0, spoint.z);
|
||||
Vector<Vector3> obstacle_vertices = obstacle_node->get_vertices();
|
||||
|
||||
real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
|
||||
|
||||
switch (mode) {
|
||||
case MODE_CREATE: {
|
||||
if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
|
||||
if (obstacle_vertices.size() >= 3) {
|
||||
int closest_idx = -1;
|
||||
Vector2 closest_edge_point;
|
||||
real_t closest_dist = 1e10;
|
||||
for (int i = 0; i < obstacle_vertices.size(); i++) {
|
||||
const Vector2 a = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
|
||||
const Vector2 b = p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]));
|
||||
|
||||
Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, a, b);
|
||||
if (cp.distance_squared_to(a) < grab_threshold || cp.distance_squared_to(b) < grab_threshold) {
|
||||
continue; // Skip edge as clicked point is too close to existing vertex.
|
||||
}
|
||||
|
||||
real_t d = cp.distance_to(mouse_position);
|
||||
if (d < closest_dist && d < grab_threshold) {
|
||||
closest_dist = d;
|
||||
closest_edge_point = cp;
|
||||
closest_idx = i;
|
||||
}
|
||||
}
|
||||
if (closest_idx >= 0) {
|
||||
edited_point = -1;
|
||||
Vector3 _ray_from = p_camera->project_ray_origin(closest_edge_point);
|
||||
Vector3 _ray_dir = p_camera->project_ray_normal(closest_edge_point);
|
||||
Vector3 edge_intersection_point;
|
||||
if (projection_plane.intersects_ray(_ray_from, _ray_dir, &edge_intersection_point)) {
|
||||
edge_intersection_point = gi.xform(edge_intersection_point);
|
||||
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
|
||||
obstacle_vertices.insert(closest_idx + 1, edge_intersection_point);
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
|
||||
undo_redo->commit_action();
|
||||
redraw();
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!wip_active) {
|
||||
wip_vertices.clear();
|
||||
wip_vertices.push_back(cpoint);
|
||||
wip_active = true;
|
||||
edited_point_pos = cpoint;
|
||||
snap_ignore = false;
|
||||
redraw();
|
||||
edited_point = 1;
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
} else {
|
||||
if (wip_vertices.size() > 1 && p_camera->unproject_position(gt.xform(wip_vertices[0])).distance_to(mouse_position) < grab_threshold) {
|
||||
_wip_close();
|
||||
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
} else {
|
||||
wip_vertices.push_back(cpoint);
|
||||
edited_point = wip_vertices.size();
|
||||
snap_ignore = false;
|
||||
redraw();
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
}
|
||||
} else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
|
||||
_wip_close();
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
case MODE_EDIT: {
|
||||
if (mb->get_button_index() == MouseButton::LEFT) {
|
||||
if (mb->is_pressed()) {
|
||||
if (mb->is_ctrl_pressed()) {
|
||||
if (obstacle_vertices.size() < 3) {
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
|
||||
obstacle_vertices.push_back(cpoint);
|
||||
undo_redo->commit_action();
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
|
||||
//search edges
|
||||
int closest_idx = -1;
|
||||
Vector2 closest_pos;
|
||||
real_t closest_dist = 1e10;
|
||||
for (int i = 0; i < obstacle_vertices.size(); i++) {
|
||||
const Vector2 a = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
|
||||
const Vector2 b = p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]));
|
||||
|
||||
Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, a, b);
|
||||
if (cp.distance_squared_to(a) < CMP_EPSILON2 || cp.distance_squared_to(b) < CMP_EPSILON2) {
|
||||
continue; //not valid to reuse point
|
||||
}
|
||||
|
||||
real_t d = cp.distance_to(mouse_position);
|
||||
if (d < closest_dist && d < grab_threshold) {
|
||||
closest_dist = d;
|
||||
closest_pos = cp;
|
||||
closest_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (closest_idx >= 0) {
|
||||
pre_move_edit = obstacle_vertices;
|
||||
obstacle_vertices.insert(closest_idx + 1, cpoint);
|
||||
edited_point = closest_idx + 1;
|
||||
edited_point_pos = cpoint;
|
||||
obstacle_node->set_vertices(obstacle_vertices);
|
||||
redraw();
|
||||
snap_ignore = true;
|
||||
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
} else {
|
||||
int closest_idx = -1;
|
||||
Vector2 closest_pos;
|
||||
real_t closest_dist = 1e10;
|
||||
for (int i = 0; i < obstacle_vertices.size(); i++) {
|
||||
Vector2 cp = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
|
||||
|
||||
real_t d = cp.distance_to(mouse_position);
|
||||
if (d < closest_dist && d < grab_threshold) {
|
||||
closest_dist = d;
|
||||
closest_pos = cp;
|
||||
closest_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (closest_idx >= 0) {
|
||||
pre_move_edit = obstacle_vertices;
|
||||
edited_point = closest_idx;
|
||||
edited_point_pos = obstacle_vertices[closest_idx];
|
||||
redraw();
|
||||
snap_ignore = false;
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
snap_ignore = false;
|
||||
|
||||
if (edited_point != -1) {
|
||||
ERR_FAIL_INDEX_V(edited_point, obstacle_vertices.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
|
||||
obstacle_vertices.write[edited_point] = edited_point_pos;
|
||||
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Move Vertex)"));
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
|
||||
undo_redo->commit_action();
|
||||
|
||||
edited_point = -1;
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
case MODE_DELETE: {
|
||||
if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
|
||||
int closest_idx = -1;
|
||||
real_t closest_dist = 1e10;
|
||||
for (int i = 0; i < obstacle_vertices.size(); i++) {
|
||||
Vector2 point = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
|
||||
real_t d = point.distance_to(mouse_position);
|
||||
if (d < closest_dist && d < grab_threshold) {
|
||||
closest_dist = d;
|
||||
closest_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (closest_idx >= 0) {
|
||||
edited_point = -1;
|
||||
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
||||
undo_redo->create_action(TTR("Edit Obstacle (Remove Vertex)"));
|
||||
undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
|
||||
obstacle_vertices.remove_at(closest_idx);
|
||||
undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
|
||||
undo_redo->commit_action();
|
||||
redraw();
|
||||
return EditorPlugin::AFTER_GUI_INPUT_STOP;
|
||||
}
|
||||
}
|
||||
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
Ref<InputEventMouseMotion> mm = p_event;
|
||||
|
||||
if (mm.is_valid()) {
|
||||
if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
|
||||
Vector2 mouse_position = mm->get_position();
|
||||
|
||||
Vector3 ray_from = p_camera->project_ray_origin(mouse_position);
|
||||
Vector3 ray_dir = p_camera->project_ray_normal(mouse_position);
|
||||
|
||||
const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
|
||||
const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
|
||||
Transform3D gi = gt.affine_inverse();
|
||||
Plane projection_plane(Vector3(0.0, 1.0, 0.0), gt.origin);
|
||||
|
||||
Vector3 intersection_point;
|
||||
|
||||
if (!projection_plane.intersects_ray(ray_from, ray_dir, &intersection_point)) {
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
intersection_point = gi.xform(intersection_point);
|
||||
|
||||
Vector2 cpoint(intersection_point.x, intersection_point.z);
|
||||
|
||||
if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
|
||||
snap_ignore = false;
|
||||
}
|
||||
|
||||
if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
|
||||
cpoint = cpoint.snappedf(Node3DEditor::get_singleton()->get_translate_snap());
|
||||
}
|
||||
edited_point_pos = Vector3(cpoint.x, 0.0, cpoint.y);
|
||||
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
Ref<InputEventKey> k = p_event;
|
||||
|
||||
if (k.is_valid() && k->is_pressed()) {
|
||||
if (wip_active && k->get_keycode() == Key::ENTER) {
|
||||
_wip_close();
|
||||
} else if (wip_active && k->get_keycode() == Key::ESCAPE) {
|
||||
_wip_cancel();
|
||||
}
|
||||
}
|
||||
|
||||
return EditorPlugin::AFTER_GUI_INPUT_PASS;
|
||||
}
|
||||
|
||||
void NavigationObstacle3DEditorPlugin::redraw() {
|
||||
if (!obstacle_node) {
|
||||
return;
|
||||
}
|
||||
RenderingServer *rs = RenderingServer::get_singleton();
|
||||
|
||||
rs->mesh_clear(point_lines_mesh_rid);
|
||||
rs->mesh_clear(point_handle_mesh_rid);
|
||||
|
||||
if (!obstacle_node->is_visible_in_tree()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Vector<Vector3> edited_vertices;
|
||||
|
||||
if (wip_active) {
|
||||
edited_vertices = wip_vertices;
|
||||
} else {
|
||||
edited_vertices = obstacle_node->get_vertices();
|
||||
}
|
||||
|
||||
if (edited_vertices.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Array point_lines_mesh_array;
|
||||
point_lines_mesh_array.resize(Mesh::ARRAY_MAX);
|
||||
|
||||
Vector<Vector3> point_lines_mesh_vertices;
|
||||
point_lines_mesh_vertices.resize(edited_vertices.size() * 2);
|
||||
Vector3 *point_lines_mesh_vertices_ptr = point_lines_mesh_vertices.ptrw();
|
||||
|
||||
int vertex_index = 0;
|
||||
|
||||
for (int i = 0; i < edited_vertices.size(); i++) {
|
||||
Vector3 point, next_point;
|
||||
if (i == edited_point) {
|
||||
point = edited_point_pos;
|
||||
} else {
|
||||
point = edited_vertices[i];
|
||||
}
|
||||
|
||||
if ((wip_active && i == edited_vertices.size() - 1) || (((i + 1) % edited_vertices.size()) == edited_point)) {
|
||||
next_point = edited_point_pos;
|
||||
} else {
|
||||
next_point = edited_vertices[(i + 1) % edited_vertices.size()];
|
||||
}
|
||||
|
||||
point_lines_mesh_vertices_ptr[vertex_index++] = point;
|
||||
point_lines_mesh_vertices_ptr[vertex_index++] = next_point;
|
||||
}
|
||||
|
||||
point_lines_mesh_array[Mesh::ARRAY_VERTEX] = point_lines_mesh_vertices;
|
||||
|
||||
rs->mesh_add_surface_from_arrays(point_lines_mesh_rid, RS::PRIMITIVE_LINES, point_lines_mesh_array);
|
||||
rs->instance_set_surface_override_material(point_lines_instance_rid, 0, line_material->get_rid());
|
||||
const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
|
||||
const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
|
||||
rs->instance_set_transform(point_lines_instance_rid, gt);
|
||||
|
||||
Array point_handle_mesh_array;
|
||||
point_handle_mesh_array.resize(Mesh::ARRAY_MAX);
|
||||
Vector<Vector3> point_handle_mesh_vertices;
|
||||
|
||||
point_handle_mesh_vertices.resize(edited_vertices.size());
|
||||
Vector3 *point_handle_mesh_vertices_ptr = point_handle_mesh_vertices.ptrw();
|
||||
|
||||
for (int i = 0; i < edited_vertices.size(); i++) {
|
||||
Vector3 point_handle_3d;
|
||||
|
||||
if (i == edited_point) {
|
||||
point_handle_3d = edited_point_pos;
|
||||
} else {
|
||||
point_handle_3d = edited_vertices[i];
|
||||
}
|
||||
|
||||
point_handle_mesh_vertices_ptr[i] = point_handle_3d;
|
||||
}
|
||||
|
||||
point_handle_mesh_array[Mesh::ARRAY_VERTEX] = point_handle_mesh_vertices;
|
||||
|
||||
rs->mesh_add_surface_from_arrays(point_handle_mesh_rid, RS::PRIMITIVE_POINTS, point_handle_mesh_array);
|
||||
rs->instance_set_surface_override_material(point_handles_instance_rid, 0, handle_material->get_rid());
|
||||
rs->instance_set_transform(point_handles_instance_rid, gt);
|
||||
}
|
||||
|
||||
NavigationObstacle3DEditorPlugin *NavigationObstacle3DEditorPlugin::singleton = nullptr;
|
||||
|
||||
NavigationObstacle3DEditorPlugin::NavigationObstacle3DEditorPlugin() {
|
||||
singleton = this;
|
||||
|
||||
line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
|
||||
line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
|
||||
line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
||||
line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
|
||||
line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
|
||||
line_material->set_albedo(Color(1, 0.3, 0.1, 0.8));
|
||||
line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
|
||||
|
||||
handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
|
||||
handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
|
||||
handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
|
||||
handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
|
||||
handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
||||
handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
|
||||
handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
|
||||
Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
|
||||
handle_material->set_point_size(handle->get_width());
|
||||
handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
|
||||
handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
|
||||
|
||||
RenderingServer *rs = RenderingServer::get_singleton();
|
||||
|
||||
point_lines_mesh_rid = rs->mesh_create();
|
||||
point_handle_mesh_rid = rs->mesh_create();
|
||||
|
||||
point_lines_instance_rid = rs->instance_create();
|
||||
point_handles_instance_rid = rs->instance_create();
|
||||
|
||||
rs->instance_set_base(point_lines_instance_rid, point_lines_mesh_rid);
|
||||
rs->instance_set_base(point_handles_instance_rid, point_handle_mesh_rid);
|
||||
|
||||
obstacle_editor = memnew(HBoxContainer);
|
||||
obstacle_editor->hide();
|
||||
|
||||
Ref<ButtonGroup> bg;
|
||||
bg.instantiate();
|
||||
|
||||
button_create = memnew(Button);
|
||||
button_create->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
obstacle_editor->add_child(button_create);
|
||||
button_create->set_tooltip_text(TTR("Add Vertex"));
|
||||
button_create->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_CREATE));
|
||||
button_create->set_toggle_mode(true);
|
||||
button_create->set_button_group(bg);
|
||||
|
||||
button_edit = memnew(Button);
|
||||
button_edit->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
button_edit->set_accessibility_name(TTRC("Edit"));
|
||||
obstacle_editor->add_child(button_edit);
|
||||
button_edit->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_EDIT));
|
||||
button_edit->set_toggle_mode(true);
|
||||
button_edit->set_button_group(bg);
|
||||
|
||||
button_delete = memnew(Button);
|
||||
button_delete->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
button_delete->set_accessibility_name(TTRC("Delete"));
|
||||
obstacle_editor->add_child(button_delete);
|
||||
button_delete->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_DELETE));
|
||||
button_delete->set_toggle_mode(true);
|
||||
button_delete->set_button_group(bg);
|
||||
|
||||
button_flip = memnew(Button);
|
||||
button_flip->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
button_flip->set_accessibility_name(TTRC("Flip"));
|
||||
obstacle_editor->add_child(button_flip);
|
||||
button_flip->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::ACTION_FLIP));
|
||||
button_flip->set_toggle_mode(true);
|
||||
|
||||
button_clear = memnew(Button);
|
||||
button_clear->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
button_clear->set_accessibility_name(TTRC("Clear"));
|
||||
obstacle_editor->add_child(button_clear);
|
||||
button_clear->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::ACTION_CLEAR));
|
||||
button_clear->set_toggle_mode(true);
|
||||
|
||||
button_clear_dialog = memnew(ConfirmationDialog);
|
||||
button_clear_dialog->set_title(TTR("Please Confirm..."));
|
||||
button_clear_dialog->set_text(TTR("Remove all vertices?"));
|
||||
button_clear_dialog->connect(SceneStringName(confirmed), callable_mp(NavigationObstacle3DEditorPlugin::singleton, &NavigationObstacle3DEditorPlugin::action_clear_vertices));
|
||||
obstacle_editor->add_child(button_clear_dialog);
|
||||
|
||||
Node3DEditor::get_singleton()->add_control_to_menu_panel(obstacle_editor);
|
||||
|
||||
Ref<NavigationObstacle3DGizmoPlugin> gizmo_plugin = memnew(NavigationObstacle3DGizmoPlugin());
|
||||
obstacle_3d_gizmo_plugin = gizmo_plugin;
|
||||
Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
|
||||
}
|
||||
|
||||
NavigationObstacle3DEditorPlugin::~NavigationObstacle3DEditorPlugin() {
|
||||
RenderingServer *rs = RenderingServer::get_singleton();
|
||||
ERR_FAIL_NULL(rs);
|
||||
|
||||
if (point_lines_instance_rid.is_valid()) {
|
||||
rs->free(point_lines_instance_rid);
|
||||
point_lines_instance_rid = RID();
|
||||
}
|
||||
if (point_lines_mesh_rid.is_valid()) {
|
||||
rs->free(point_lines_mesh_rid);
|
||||
point_lines_mesh_rid = RID();
|
||||
}
|
||||
|
||||
if (point_handles_instance_rid.is_valid()) {
|
||||
rs->free(point_handles_instance_rid);
|
||||
point_handles_instance_rid = RID();
|
||||
}
|
||||
if (point_handle_mesh_rid.is_valid()) {
|
||||
rs->free(point_handle_mesh_rid);
|
||||
point_handle_mesh_rid = RID();
|
||||
}
|
||||
}
|
@@ -0,0 +1,134 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_obstacle_3d_editor_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/plugins/editor_plugin.h"
|
||||
#include "editor/scene/3d/node_3d_editor_gizmos.h"
|
||||
#include "scene/gui/box_container.h"
|
||||
|
||||
class Button;
|
||||
class ConfirmationDialog;
|
||||
class NavigationObstacle3D;
|
||||
|
||||
class NavigationObstacle3DGizmoPlugin : public EditorNode3DGizmoPlugin {
|
||||
GDCLASS(NavigationObstacle3DGizmoPlugin, EditorNode3DGizmoPlugin);
|
||||
|
||||
public:
|
||||
virtual bool has_gizmo(Node3D *p_spatial) override;
|
||||
virtual String get_gizmo_name() const override;
|
||||
|
||||
virtual void redraw(EditorNode3DGizmo *p_gizmo) override;
|
||||
|
||||
bool can_be_hidden() const override;
|
||||
int get_priority() const override;
|
||||
|
||||
virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;
|
||||
virtual Vector<int> subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const override;
|
||||
virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;
|
||||
virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;
|
||||
virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false) override;
|
||||
|
||||
NavigationObstacle3DGizmoPlugin();
|
||||
};
|
||||
|
||||
class NavigationObstacle3DEditorPlugin : public EditorPlugin {
|
||||
GDCLASS(NavigationObstacle3DEditorPlugin, EditorPlugin);
|
||||
|
||||
Ref<NavigationObstacle3DGizmoPlugin> obstacle_3d_gizmo_plugin;
|
||||
|
||||
NavigationObstacle3D *obstacle_node = nullptr;
|
||||
|
||||
Ref<StandardMaterial3D> line_material;
|
||||
Ref<StandardMaterial3D> handle_material;
|
||||
|
||||
RID point_lines_mesh_rid;
|
||||
RID point_lines_instance_rid;
|
||||
RID point_handle_mesh_rid;
|
||||
RID point_handles_instance_rid;
|
||||
|
||||
public:
|
||||
enum Mode {
|
||||
MODE_CREATE = 0,
|
||||
MODE_EDIT,
|
||||
MODE_DELETE,
|
||||
ACTION_FLIP,
|
||||
ACTION_CLEAR,
|
||||
};
|
||||
|
||||
private:
|
||||
int mode = MODE_EDIT;
|
||||
|
||||
int edited_point = 0;
|
||||
Vector3 edited_point_pos;
|
||||
Vector<Vector3> pre_move_edit;
|
||||
Vector<Vector3> wip_vertices;
|
||||
bool wip_active = false;
|
||||
bool snap_ignore = false;
|
||||
|
||||
void _wip_close();
|
||||
void _wip_cancel();
|
||||
void _update_theme();
|
||||
|
||||
Button *button_create = nullptr;
|
||||
Button *button_edit = nullptr;
|
||||
Button *button_delete = nullptr;
|
||||
Button *button_flip = nullptr;
|
||||
Button *button_clear = nullptr;
|
||||
|
||||
ConfirmationDialog *button_clear_dialog = nullptr;
|
||||
|
||||
protected:
|
||||
void _notification(int p_what);
|
||||
void _node_removed(Node *p_node);
|
||||
|
||||
public:
|
||||
HBoxContainer *obstacle_editor = nullptr;
|
||||
static NavigationObstacle3DEditorPlugin *singleton;
|
||||
|
||||
void redraw();
|
||||
|
||||
void set_mode(int p_mode);
|
||||
int get_mode() { return mode; }
|
||||
|
||||
void action_flip_vertices();
|
||||
void action_clear_vertices();
|
||||
|
||||
virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;
|
||||
|
||||
virtual String get_plugin_name() const override { return "NavigationObstacle3DEditor"; }
|
||||
bool has_main_screen() const override { return false; }
|
||||
virtual void edit(Object *p_object) override;
|
||||
virtual bool handles(Object *p_object) const override;
|
||||
virtual void make_visible(bool p_visible) override;
|
||||
|
||||
NavigationObstacle3DEditorPlugin();
|
||||
~NavigationObstacle3DEditorPlugin();
|
||||
};
|
@@ -0,0 +1,324 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_region_3d_editor_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_region_3d_editor_plugin.h"
|
||||
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/editor_string_names.h"
|
||||
#include "editor/inspector/multi_node_edit.h"
|
||||
#include "editor/scene/3d/node_3d_editor_plugin.h"
|
||||
#include "scene/3d/navigation/navigation_region_3d.h"
|
||||
#include "scene/gui/box_container.h"
|
||||
#include "scene/gui/button.h"
|
||||
#include "scene/gui/dialogs.h"
|
||||
#include "scene/gui/label.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
void NavigationRegion3DEditor::_node_removed(Node *p_node) {
|
||||
if (selected_regions.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(p_node);
|
||||
|
||||
if (region && selected_regions.has(region)) {
|
||||
selected_regions.erase(region);
|
||||
if (selected_regions.is_empty()) {
|
||||
hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_notification(int p_what) {
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_ENTER_TREE: {
|
||||
button_bake->set_button_icon(get_theme_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
|
||||
button_reset->set_button_icon(get_theme_icon(SNAME("Reload"), EditorStringName(EditorIcons)));
|
||||
} break;
|
||||
case NOTIFICATION_PROCESS: {
|
||||
if (currently_baking_region) {
|
||||
const String bake_state_msg = NavigationServer3D::get_singleton()->get_baking_navigation_mesh_state_msg(currently_baking_region->get_navigation_mesh());
|
||||
multibake_dialog->set_text(itos(processed_regions_to_bake_count) + " / " + itos(processed_regions_to_bake_count_max) + " - Baking navmesh from region '" + currently_baking_region->get_name() + "'.\n\nBake state: " + bake_state_msg + "\n\nDo NOT change nodes by any means while the baking is parsing the SceneTree.");
|
||||
} else {
|
||||
multibake_dialog->set_text("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_bake_pressed() {
|
||||
button_bake->set_pressed(false);
|
||||
|
||||
if (selected_regions.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bake_in_process) {
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<Ref<NavigationMesh>> unique_navmeshes;
|
||||
regions_to_bake.clear();
|
||||
regions_with_navmesh_to_bake.clear();
|
||||
|
||||
for (NavigationRegion3D *region : selected_regions) {
|
||||
ERR_CONTINUE(region == nullptr);
|
||||
Ref<NavigationMesh> navmesh = region->get_navigation_mesh();
|
||||
if (navmesh.is_null()) {
|
||||
err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
|
||||
String path = navmesh->get_path();
|
||||
if (!path.is_resource_file()) {
|
||||
int srpos = path.find("::");
|
||||
if (srpos != -1) {
|
||||
String base = path.substr(0, srpos);
|
||||
if (ResourceLoader::get_resource_type(base) == "PackedScene") {
|
||||
if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (FileAccess::exists(base + ".import")) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because it belongs to a resource which was imported."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (FileAccess::exists(path + ".import")) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because the resource was imported from another type."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
regions_to_bake.push_back(region);
|
||||
|
||||
if (unique_navmeshes.has(navmesh)) {
|
||||
// No point (re)baking the same resource in case of multi select.
|
||||
// Trying to bake the same navmesh twice would trigger an error.
|
||||
continue;
|
||||
}
|
||||
unique_navmeshes.insert(navmesh);
|
||||
regions_with_navmesh_to_bake.push_back(region);
|
||||
}
|
||||
|
||||
if (!regions_with_navmesh_to_bake.is_empty()) {
|
||||
multibake_dialog->set_ok_button_text(TTR("Bake"));
|
||||
multibake_dialog->get_ok_button()->set_disabled(false);
|
||||
multibake_dialog->set_text("Attempting to bake " + itos(regions_with_navmesh_to_bake.size()) + " unique navmesh(es) from " + itos(regions_to_bake.size()) + " selected NavigationRegion3D node(s).\n\nThis can take some time and freeze the Editor temporarily.\n\nDo NOT change nodes by any means while the baking is parsing the SceneTree.");
|
||||
multibake_dialog->popup_centered();
|
||||
if (regions_with_navmesh_to_bake.size() == 1) {
|
||||
// If we only have a single region start bake immediately.
|
||||
_on_navmesh_multibake_confirmed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_on_navmesh_multibake_confirmed() {
|
||||
multibake_dialog->get_ok_button()->set_disabled(true);
|
||||
|
||||
bake_in_process = true;
|
||||
region_baking_canceled = false;
|
||||
processed_regions_to_bake_count = 0;
|
||||
processed_regions_to_bake_count_max = regions_with_navmesh_to_bake.size();
|
||||
|
||||
set_process(true);
|
||||
_process_regions_to_bake();
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_process_regions_to_bake() {
|
||||
if (region_baking_canceled) {
|
||||
region_baking_canceled = false;
|
||||
regions_with_navmesh_to_bake.clear();
|
||||
}
|
||||
|
||||
if (regions_with_navmesh_to_bake.is_empty()) {
|
||||
regions_to_bake.clear();
|
||||
multibake_dialog->set_visible(false);
|
||||
set_process(false);
|
||||
currently_baking_region = nullptr;
|
||||
bake_in_process = false;
|
||||
return;
|
||||
}
|
||||
|
||||
NavigationRegion3D *region_to_bake = regions_with_navmesh_to_bake[0];
|
||||
regions_with_navmesh_to_bake.remove_at_unordered(0);
|
||||
processed_regions_to_bake_count += 1;
|
||||
if (region_to_bake && region_to_bake->get_navigation_mesh().is_valid()) {
|
||||
currently_baking_region = region_to_bake;
|
||||
region_to_bake->connect(SNAME("bake_finished"), callable_mp(this, &NavigationRegion3DEditor::_process_regions_to_bake), CONNECT_ONE_SHOT);
|
||||
region_to_bake->bake_navigation_mesh(true);
|
||||
return;
|
||||
} else {
|
||||
_process_regions_to_bake();
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_on_navmesh_multibake_canceled() {
|
||||
if (bake_in_process) {
|
||||
region_baking_canceled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
multibake_dialog->set_visible(false);
|
||||
regions_to_bake.clear();
|
||||
regions_with_navmesh_to_bake.clear();
|
||||
processed_regions_to_bake_count = 0;
|
||||
processed_regions_to_bake_count_max = 0;
|
||||
region_baking_canceled = false;
|
||||
currently_baking_region = nullptr;
|
||||
bake_in_process = false;
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::_clear_pressed() {
|
||||
button_bake->set_pressed(false);
|
||||
bake_info->set_text("");
|
||||
|
||||
if (!selected_regions.is_empty()) {
|
||||
for (NavigationRegion3D *region : selected_regions) {
|
||||
if (region->get_navigation_mesh().is_valid()) {
|
||||
region->get_navigation_mesh()->clear();
|
||||
region->update_gizmos();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditor::edit(LocalVector<NavigationRegion3D *> p_regions) {
|
||||
if (p_regions.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
selected_regions = p_regions;
|
||||
}
|
||||
|
||||
NavigationRegion3DEditor::NavigationRegion3DEditor() {
|
||||
bake_hbox = memnew(HBoxContainer);
|
||||
|
||||
button_bake = memnew(Button);
|
||||
button_bake->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
bake_hbox->add_child(button_bake);
|
||||
button_bake->set_toggle_mode(true);
|
||||
button_bake->set_text(TTR("Bake NavigationMesh"));
|
||||
button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and polygons."));
|
||||
button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion3DEditor::_bake_pressed));
|
||||
|
||||
button_reset = memnew(Button);
|
||||
button_reset->set_theme_type_variation(SceneStringName(FlatButton));
|
||||
bake_hbox->add_child(button_reset);
|
||||
button_reset->set_text(TTR("Clear NavigationMesh"));
|
||||
button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
|
||||
button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion3DEditor::_clear_pressed));
|
||||
|
||||
bake_info = memnew(Label);
|
||||
bake_info->set_focus_mode(FOCUS_ACCESSIBILITY);
|
||||
bake_hbox->add_child(bake_info);
|
||||
|
||||
err_dialog = memnew(AcceptDialog);
|
||||
add_child(err_dialog);
|
||||
|
||||
multibake_dialog = memnew(ConfirmationDialog);
|
||||
add_child(multibake_dialog);
|
||||
multibake_dialog->connect(SceneStringName(confirmed), callable_mp(this, &NavigationRegion3DEditor::_on_navmesh_multibake_confirmed));
|
||||
multibake_dialog->connect(SNAME("canceled"), callable_mp(this, &NavigationRegion3DEditor::_on_navmesh_multibake_canceled));
|
||||
multibake_dialog->set_hide_on_ok(false);
|
||||
multibake_dialog->set_title(TTR("Baking NavigationMesh ..."));
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditorPlugin::edit(Object *p_object) {
|
||||
LocalVector<NavigationRegion3D *> regions;
|
||||
|
||||
{
|
||||
NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(p_object);
|
||||
if (region) {
|
||||
regions.push_back(region);
|
||||
navigation_region_editor->edit(regions);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
|
||||
Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
|
||||
if (mne.is_valid() && edited_scene) {
|
||||
for (int i = 0; i < mne->get_node_count(); i++) {
|
||||
NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(edited_scene->get_node(mne->get_node(i)));
|
||||
if (region) {
|
||||
regions.push_back(region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
navigation_region_editor->edit(regions);
|
||||
}
|
||||
|
||||
bool NavigationRegion3DEditorPlugin::handles(Object *p_object) const {
|
||||
if (Object::cast_to<NavigationRegion3D>(p_object)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
|
||||
Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
|
||||
if (mne.is_valid() && edited_scene) {
|
||||
for (int i = 0; i < mne->get_node_count(); i++) {
|
||||
if (Object::cast_to<NavigationRegion3D>(edited_scene->get_node(mne->get_node(i)))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void NavigationRegion3DEditorPlugin::make_visible(bool p_visible) {
|
||||
if (p_visible) {
|
||||
navigation_region_editor->show();
|
||||
navigation_region_editor->bake_hbox->show();
|
||||
} else {
|
||||
navigation_region_editor->hide();
|
||||
navigation_region_editor->bake_hbox->hide();
|
||||
navigation_region_editor->edit(LocalVector<NavigationRegion3D *>());
|
||||
}
|
||||
}
|
||||
|
||||
NavigationRegion3DEditorPlugin::NavigationRegion3DEditorPlugin() {
|
||||
navigation_region_editor = memnew(NavigationRegion3DEditor);
|
||||
EditorNode::get_singleton()->get_gui_base()->add_child(navigation_region_editor);
|
||||
add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_region_editor->bake_hbox);
|
||||
navigation_region_editor->hide();
|
||||
navigation_region_editor->bake_hbox->hide();
|
||||
|
||||
gizmo_plugin.instantiate();
|
||||
Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
|
||||
}
|
@@ -0,0 +1,100 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_region_3d_editor_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/plugins/editor_plugin.h"
|
||||
|
||||
#include "navigation_region_3d_gizmo_plugin.h"
|
||||
|
||||
class AcceptDialog;
|
||||
class Button;
|
||||
class ConfirmationDialog;
|
||||
class HBoxContainer;
|
||||
class Label;
|
||||
class NavigationRegion3D;
|
||||
|
||||
class NavigationRegion3DEditor : public Control {
|
||||
friend class NavigationRegion3DEditorPlugin;
|
||||
|
||||
GDCLASS(NavigationRegion3DEditor, Control);
|
||||
|
||||
AcceptDialog *err_dialog = nullptr;
|
||||
ConfirmationDialog *multibake_dialog = nullptr;
|
||||
|
||||
HBoxContainer *bake_hbox = nullptr;
|
||||
Button *button_bake = nullptr;
|
||||
Button *button_reset = nullptr;
|
||||
Label *bake_info = nullptr;
|
||||
|
||||
LocalVector<NavigationRegion3D *> selected_regions;
|
||||
|
||||
LocalVector<NavigationRegion3D *> regions_to_bake;
|
||||
LocalVector<NavigationRegion3D *> regions_with_navmesh_to_bake;
|
||||
|
||||
int processed_regions_to_bake_count = 0;
|
||||
int processed_regions_to_bake_count_max = 0;
|
||||
bool region_baking_canceled = false;
|
||||
NavigationRegion3D *currently_baking_region = nullptr;
|
||||
|
||||
bool bake_in_process = false;
|
||||
|
||||
void _bake_pressed();
|
||||
void _clear_pressed();
|
||||
|
||||
void _on_navmesh_multibake_confirmed();
|
||||
void _on_navmesh_multibake_canceled();
|
||||
void _process_regions_to_bake();
|
||||
|
||||
protected:
|
||||
void _node_removed(Node *p_node);
|
||||
void _notification(int p_what);
|
||||
|
||||
public:
|
||||
void edit(LocalVector<NavigationRegion3D *> p_regions);
|
||||
NavigationRegion3DEditor();
|
||||
};
|
||||
|
||||
class NavigationRegion3DEditorPlugin : public EditorPlugin {
|
||||
GDCLASS(NavigationRegion3DEditorPlugin, EditorPlugin);
|
||||
|
||||
NavigationRegion3DEditor *navigation_region_editor = nullptr;
|
||||
|
||||
Ref<NavigationRegion3DGizmoPlugin> gizmo_plugin;
|
||||
|
||||
public:
|
||||
virtual String get_plugin_name() const override { return "NavigationRegion3D"; }
|
||||
bool has_main_screen() const override { return false; }
|
||||
virtual void edit(Object *p_object) override;
|
||||
virtual bool handles(Object *p_object) const override;
|
||||
virtual void make_visible(bool p_visible) override;
|
||||
|
||||
NavigationRegion3DEditorPlugin();
|
||||
};
|
@@ -0,0 +1,208 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_region_3d_gizmo_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_region_3d_gizmo_plugin.h"
|
||||
|
||||
#include "core/math/random_pcg.h"
|
||||
#include "scene/3d/navigation/navigation_region_3d.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
NavigationRegion3DGizmoPlugin::NavigationRegion3DGizmoPlugin() {
|
||||
create_material("face_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(), false, false, true);
|
||||
create_material("face_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_color(), false, false, true);
|
||||
create_material("edge_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_color());
|
||||
create_material("edge_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_color());
|
||||
|
||||
Color baking_aabb_material_color = Color(0.8, 0.5, 0.7);
|
||||
baking_aabb_material_color.a = 0.1;
|
||||
create_material("baking_aabb_material", baking_aabb_material_color);
|
||||
}
|
||||
|
||||
bool NavigationRegion3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
|
||||
return Object::cast_to<NavigationRegion3D>(p_spatial) != nullptr;
|
||||
}
|
||||
|
||||
String NavigationRegion3DGizmoPlugin::get_gizmo_name() const {
|
||||
return "NavigationRegion3D";
|
||||
}
|
||||
|
||||
int NavigationRegion3DGizmoPlugin::get_priority() const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void NavigationRegion3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
|
||||
NavigationRegion3D *navigationregion = Object::cast_to<NavigationRegion3D>(p_gizmo->get_node_3d());
|
||||
|
||||
p_gizmo->clear();
|
||||
Ref<NavigationMesh> navigationmesh = navigationregion->get_navigation_mesh();
|
||||
if (navigationmesh.is_null()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AABB baking_aabb = navigationmesh->get_filter_baking_aabb();
|
||||
if (baking_aabb.has_volume()) {
|
||||
Vector3 baking_aabb_offset = navigationmesh->get_filter_baking_aabb_offset();
|
||||
|
||||
if (p_gizmo->is_selected()) {
|
||||
Ref<Material> material = get_material("baking_aabb_material", p_gizmo);
|
||||
p_gizmo->add_solid_box(material, baking_aabb.get_size(), baking_aabb.get_center() + baking_aabb_offset);
|
||||
}
|
||||
}
|
||||
|
||||
Vector<Vector3> vertices = navigationmesh->get_vertices();
|
||||
const Vector3 *vr = vertices.ptr();
|
||||
List<Face3> faces;
|
||||
for (int i = 0; i < navigationmesh->get_polygon_count(); i++) {
|
||||
Vector<int> p = navigationmesh->get_polygon(i);
|
||||
|
||||
for (int j = 2; j < p.size(); j++) {
|
||||
Face3 f;
|
||||
f.vertex[0] = vr[p[0]];
|
||||
f.vertex[1] = vr[p[j - 1]];
|
||||
f.vertex[2] = vr[p[j]];
|
||||
|
||||
faces.push_back(f);
|
||||
}
|
||||
}
|
||||
|
||||
if (faces.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
HashMap<_EdgeKey, bool, _EdgeKey> edge_map;
|
||||
Vector<Vector3> tmeshfaces;
|
||||
tmeshfaces.resize(faces.size() * 3);
|
||||
|
||||
{
|
||||
Vector3 *tw = tmeshfaces.ptrw();
|
||||
int tidx = 0;
|
||||
|
||||
for (const Face3 &f : faces) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
tw[tidx++] = f.vertex[j];
|
||||
_EdgeKey ek;
|
||||
ek.from = f.vertex[j].snappedf(CMP_EPSILON);
|
||||
ek.to = f.vertex[(j + 1) % 3].snappedf(CMP_EPSILON);
|
||||
if (ek.from < ek.to) {
|
||||
SWAP(ek.from, ek.to);
|
||||
}
|
||||
|
||||
HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek);
|
||||
|
||||
if (F) {
|
||||
F->value = false;
|
||||
|
||||
} else {
|
||||
edge_map[ek] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vector<Vector3> lines;
|
||||
|
||||
for (const KeyValue<_EdgeKey, bool> &E : edge_map) {
|
||||
if (E.value) {
|
||||
lines.push_back(E.key.from);
|
||||
lines.push_back(E.key.to);
|
||||
}
|
||||
}
|
||||
|
||||
Ref<TriangleMesh> tmesh = memnew(TriangleMesh);
|
||||
tmesh->create(tmeshfaces);
|
||||
|
||||
p_gizmo->add_collision_triangles(tmesh);
|
||||
p_gizmo->add_collision_segments(lines);
|
||||
|
||||
Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
|
||||
int polygon_count = navigationmesh->get_polygon_count();
|
||||
|
||||
// build geometry face surface
|
||||
Vector<Vector3> face_vertex_array;
|
||||
face_vertex_array.resize(polygon_count * 3);
|
||||
|
||||
for (int i = 0; i < polygon_count; i++) {
|
||||
Vector<int> polygon = navigationmesh->get_polygon(i);
|
||||
|
||||
face_vertex_array.push_back(vertices[polygon[0]]);
|
||||
face_vertex_array.push_back(vertices[polygon[1]]);
|
||||
face_vertex_array.push_back(vertices[polygon[2]]);
|
||||
}
|
||||
|
||||
Array face_mesh_array;
|
||||
face_mesh_array.resize(Mesh::ARRAY_MAX);
|
||||
face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
|
||||
|
||||
// if enabled add vertex colors to colorize each face individually
|
||||
RandomPCG rand;
|
||||
bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
|
||||
if (enabled_geometry_face_random_color) {
|
||||
Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
|
||||
Color polygon_color = debug_navigation_geometry_face_color;
|
||||
|
||||
Vector<Color> face_color_array;
|
||||
face_color_array.resize(polygon_count * 3);
|
||||
|
||||
for (int i = 0; i < polygon_count; i++) {
|
||||
// Generate the polygon color, slightly randomly modified from the settings one.
|
||||
polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
|
||||
polygon_color.a = debug_navigation_geometry_face_color.a;
|
||||
|
||||
Vector<int> polygon = navigationmesh->get_polygon(i);
|
||||
|
||||
face_color_array.push_back(polygon_color);
|
||||
face_color_array.push_back(polygon_color);
|
||||
face_color_array.push_back(polygon_color);
|
||||
}
|
||||
face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
|
||||
}
|
||||
|
||||
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
|
||||
p_gizmo->add_mesh(debug_mesh, navigationregion->is_enabled() ? get_material("face_material", p_gizmo) : get_material("face_material_disabled", p_gizmo));
|
||||
|
||||
// if enabled build geometry edge line surface
|
||||
bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
|
||||
if (enabled_edge_lines) {
|
||||
Vector<Vector3> line_vertex_array;
|
||||
line_vertex_array.resize(polygon_count * 6);
|
||||
|
||||
for (int i = 0; i < polygon_count; i++) {
|
||||
Vector<int> polygon = navigationmesh->get_polygon(i);
|
||||
|
||||
line_vertex_array.push_back(vertices[polygon[0]]);
|
||||
line_vertex_array.push_back(vertices[polygon[1]]);
|
||||
line_vertex_array.push_back(vertices[polygon[1]]);
|
||||
line_vertex_array.push_back(vertices[polygon[2]]);
|
||||
line_vertex_array.push_back(vertices[polygon[2]]);
|
||||
line_vertex_array.push_back(vertices[polygon[0]]);
|
||||
}
|
||||
|
||||
p_gizmo->add_lines(line_vertex_array, navigationregion->is_enabled() ? get_material("edge_material", p_gizmo) : get_material("edge_material_disabled", p_gizmo));
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
/**************************************************************************/
|
||||
/* navigation_region_3d_gizmo_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/scene/3d/node_3d_editor_gizmos.h"
|
||||
|
||||
class NavigationRegion3DGizmoPlugin : public EditorNode3DGizmoPlugin {
|
||||
GDCLASS(NavigationRegion3DGizmoPlugin, EditorNode3DGizmoPlugin);
|
||||
|
||||
struct _EdgeKey {
|
||||
Vector3 from;
|
||||
Vector3 to;
|
||||
|
||||
static uint32_t hash(const _EdgeKey &p_key) {
|
||||
return HashMapHasherDefault::hash(p_key.from) ^ HashMapHasherDefault::hash(p_key.to);
|
||||
}
|
||||
|
||||
bool operator==(const _EdgeKey &p_with) const {
|
||||
return HashMapComparatorDefault<Vector3>::compare(from, p_with.from) && HashMapComparatorDefault<Vector3>::compare(to, p_with.to);
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
bool has_gizmo(Node3D *p_spatial) override;
|
||||
String get_gizmo_name() const override;
|
||||
int get_priority() const override;
|
||||
void redraw(EditorNode3DGizmo *p_gizmo) override;
|
||||
|
||||
NavigationRegion3DGizmoPlugin();
|
||||
};
|
Reference in New Issue
Block a user