navigation_obstacle_3d_editor_plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**************************************************************************/
  2. /* navigation_obstacle_3d_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "navigation_obstacle_3d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/core_string_names.h"
  33. #include "core/input/input.h"
  34. #include "core/io/file_access.h"
  35. #include "core/math/geometry_2d.h"
  36. #include "core/os/keyboard.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/editor_string_names.h"
  40. #include "editor/editor_undo_redo_manager.h"
  41. #include "node_3d_editor_plugin.h"
  42. #include "scene/3d/camera_3d.h"
  43. #include "scene/gui/separator.h"
  44. void NavigationObstacle3DEditor::_notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_READY: {
  47. button_create->set_icon(get_editor_theme_icon(SNAME("Edit")));
  48. button_edit->set_icon(get_editor_theme_icon(SNAME("MovePoint")));
  49. button_edit->set_pressed(true);
  50. get_tree()->connect("node_removed", callable_mp(this, &NavigationObstacle3DEditor::_node_removed));
  51. } break;
  52. }
  53. }
  54. void NavigationObstacle3DEditor::_node_removed(Node *p_node) {
  55. if (p_node == obstacle_node) {
  56. obstacle_node = nullptr;
  57. if (point_lines_meshinstance->get_parent() == p_node) {
  58. p_node->remove_child(point_lines_meshinstance);
  59. }
  60. hide();
  61. }
  62. }
  63. void NavigationObstacle3DEditor::_menu_option(int p_option) {
  64. switch (p_option) {
  65. case MODE_CREATE: {
  66. mode = MODE_CREATE;
  67. button_create->set_pressed(true);
  68. button_edit->set_pressed(false);
  69. } break;
  70. case MODE_EDIT: {
  71. mode = MODE_EDIT;
  72. button_create->set_pressed(false);
  73. button_edit->set_pressed(true);
  74. } break;
  75. }
  76. }
  77. void NavigationObstacle3DEditor::_wip_close() {
  78. ERR_FAIL_NULL_MSG(obstacle_node, "Edited NavigationObstacle3D is not valid.");
  79. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  80. undo_redo->create_action(TTR("Set NavigationObstacle3D Vertices"));
  81. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  82. PackedVector3Array polygon_3d_vertices;
  83. Vector<int> triangulated_polygon_2d_indices = Geometry2D::triangulate_polygon(wip);
  84. if (!triangulated_polygon_2d_indices.is_empty()) {
  85. polygon_3d_vertices.resize(wip.size());
  86. Vector3 *polygon_3d_vertices_ptr = polygon_3d_vertices.ptrw();
  87. for (int i = 0; i < wip.size(); i++) {
  88. const Vector2 &vert = wip[i];
  89. polygon_3d_vertices_ptr[i] = Vector3(vert.x, 0.0, vert.y);
  90. }
  91. }
  92. undo_redo->add_do_method(obstacle_node, "set_vertices", polygon_3d_vertices);
  93. undo_redo->add_do_method(this, "_polygon_draw");
  94. undo_redo->add_undo_method(this, "_polygon_draw");
  95. wip.clear();
  96. wip_active = false;
  97. mode = MODE_EDIT;
  98. button_edit->set_pressed(true);
  99. button_create->set_pressed(false);
  100. edited_point = -1;
  101. undo_redo->commit_action();
  102. }
  103. EditorPlugin::AfterGUIInput NavigationObstacle3DEditor::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  104. if (!obstacle_node) {
  105. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  106. }
  107. Transform3D gt = obstacle_node->get_global_transform();
  108. Transform3D gi = gt.affine_inverse();
  109. Plane p(Vector3(0.0, 1.0, 0.0), gt.origin);
  110. Ref<InputEventMouseButton> mb = p_event;
  111. if (mb.is_valid()) {
  112. Vector2 gpoint = mb->get_position();
  113. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  114. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  115. Vector3 spoint;
  116. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  117. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  118. }
  119. spoint = gi.xform(spoint);
  120. Vector2 cpoint(spoint.x, spoint.z);
  121. //DO NOT snap here, it's confusing in 3D for adding points.
  122. //Let the snap happen when the point is being moved, instead.
  123. //cpoint = CanvasItemEditor::get_singleton()->snap_point(cpoint);
  124. PackedVector2Array poly = _get_polygon();
  125. //first check if a point is to be added (segment split)
  126. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  127. switch (mode) {
  128. case MODE_CREATE: {
  129. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  130. if (!wip_active) {
  131. wip.clear();
  132. wip.push_back(cpoint);
  133. wip_active = true;
  134. edited_point_pos = cpoint;
  135. snap_ignore = false;
  136. _polygon_draw();
  137. edited_point = 1;
  138. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  139. } else {
  140. if (wip.size() > 1 && p_camera->unproject_position(gt.xform(Vector3(wip[0].x, 0.0, wip[0].y))).distance_to(gpoint) < grab_threshold) {
  141. //wip closed
  142. _wip_close();
  143. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  144. } else {
  145. wip.push_back(cpoint);
  146. edited_point = wip.size();
  147. snap_ignore = false;
  148. _polygon_draw();
  149. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  150. }
  151. }
  152. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  153. _wip_close();
  154. }
  155. } break;
  156. case MODE_EDIT: {
  157. if (mb->get_button_index() == MouseButton::LEFT) {
  158. if (mb->is_pressed()) {
  159. if (mb->is_ctrl_pressed()) {
  160. if (poly.size() < 3) {
  161. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  162. undo_redo->create_action(TTR("Edit Vertices"));
  163. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  164. poly.push_back(cpoint);
  165. undo_redo->add_do_method(this, "_polygon_draw");
  166. undo_redo->add_undo_method(this, "_polygon_draw");
  167. undo_redo->commit_action();
  168. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  169. }
  170. //search edges
  171. int closest_idx = -1;
  172. Vector2 closest_pos;
  173. real_t closest_dist = 1e10;
  174. for (int i = 0; i < poly.size(); i++) {
  175. Vector2 points[2] = {
  176. p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y))),
  177. p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, 0.0, poly[(i + 1) % poly.size()].y)))
  178. };
  179. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, points);
  180. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  181. continue; //not valid to reuse point
  182. }
  183. real_t d = cp.distance_to(gpoint);
  184. if (d < closest_dist && d < grab_threshold) {
  185. closest_dist = d;
  186. closest_pos = cp;
  187. closest_idx = i;
  188. }
  189. }
  190. if (closest_idx >= 0) {
  191. pre_move_edit = poly;
  192. poly.insert(closest_idx + 1, cpoint);
  193. edited_point = closest_idx + 1;
  194. edited_point_pos = cpoint;
  195. _set_polygon(poly);
  196. _polygon_draw();
  197. snap_ignore = true;
  198. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  199. }
  200. } else {
  201. //look for points to move
  202. int closest_idx = -1;
  203. Vector2 closest_pos;
  204. real_t closest_dist = 1e10;
  205. for (int i = 0; i < poly.size(); i++) {
  206. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y)));
  207. real_t d = cp.distance_to(gpoint);
  208. if (d < closest_dist && d < grab_threshold) {
  209. closest_dist = d;
  210. closest_pos = cp;
  211. closest_idx = i;
  212. }
  213. }
  214. if (closest_idx >= 0) {
  215. pre_move_edit = poly;
  216. edited_point = closest_idx;
  217. edited_point_pos = poly[closest_idx];
  218. _polygon_draw();
  219. snap_ignore = false;
  220. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  221. }
  222. }
  223. } else {
  224. snap_ignore = false;
  225. if (edited_point != -1) {
  226. //apply
  227. ERR_FAIL_INDEX_V(edited_point, poly.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  228. poly.write[edited_point] = edited_point_pos;
  229. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  230. undo_redo->create_action(TTR("Edit Poly"));
  231. //undo_redo->add_do_method(obj, "set_polygon", poly);
  232. //undo_redo->add_undo_method(obj, "set_polygon", pre_move_edit);
  233. undo_redo->add_do_method(this, "_polygon_draw");
  234. undo_redo->add_undo_method(this, "_polygon_draw");
  235. undo_redo->commit_action();
  236. edited_point = -1;
  237. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  238. }
  239. }
  240. }
  241. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && edited_point == -1) {
  242. int closest_idx = -1;
  243. Vector2 closest_pos;
  244. real_t closest_dist = 1e10;
  245. for (int i = 0; i < poly.size(); i++) {
  246. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y)));
  247. real_t d = cp.distance_to(gpoint);
  248. if (d < closest_dist && d < grab_threshold) {
  249. closest_dist = d;
  250. closest_pos = cp;
  251. closest_idx = i;
  252. }
  253. }
  254. if (closest_idx >= 0) {
  255. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  256. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  257. //undo_redo->add_undo_method(obj, "set_polygon", poly);
  258. poly.remove_at(closest_idx);
  259. //undo_redo->add_do_method(obj, "set_polygon", poly);
  260. undo_redo->add_do_method(this, "_polygon_draw");
  261. undo_redo->add_undo_method(this, "_polygon_draw");
  262. undo_redo->commit_action();
  263. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  264. }
  265. }
  266. } break;
  267. }
  268. }
  269. Ref<InputEventMouseMotion> mm = p_event;
  270. if (mm.is_valid()) {
  271. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  272. Vector2 gpoint = mm->get_position();
  273. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  274. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  275. Vector3 spoint;
  276. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  277. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  278. }
  279. spoint = gi.xform(spoint);
  280. Vector2 cpoint(spoint.x, spoint.z);
  281. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  282. snap_ignore = false;
  283. }
  284. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  285. cpoint = cpoint.snapped(Vector2(
  286. Node3DEditor::get_singleton()->get_translate_snap(),
  287. Node3DEditor::get_singleton()->get_translate_snap()));
  288. }
  289. edited_point_pos = cpoint;
  290. _polygon_draw();
  291. }
  292. }
  293. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  294. }
  295. PackedVector2Array NavigationObstacle3DEditor::_get_polygon() {
  296. ERR_FAIL_NULL_V_MSG(obstacle_node, PackedVector2Array(), "Edited object is not valid.");
  297. return PackedVector2Array(obstacle_node->call("get_polygon"));
  298. }
  299. void NavigationObstacle3DEditor::_set_polygon(PackedVector2Array p_poly) {
  300. ERR_FAIL_NULL_MSG(obstacle_node, "Edited object is not valid.");
  301. obstacle_node->call("set_polygon", p_poly);
  302. }
  303. void NavigationObstacle3DEditor::_polygon_draw() {
  304. if (!obstacle_node) {
  305. return;
  306. }
  307. PackedVector2Array poly;
  308. PackedVector3Array polygon_3d_vertices;
  309. if (wip_active) {
  310. poly = wip;
  311. } else {
  312. poly = _get_polygon();
  313. }
  314. polygon_3d_vertices.resize(poly.size());
  315. Vector3 *polygon_3d_vertices_ptr = polygon_3d_vertices.ptrw();
  316. for (int i = 0; i < poly.size(); i++) {
  317. const Vector2 &vert = poly[i];
  318. polygon_3d_vertices_ptr[i] = Vector3(vert.x, 0.0, vert.y);
  319. }
  320. point_handle_mesh->clear_surfaces();
  321. point_lines_mesh->clear_surfaces();
  322. point_lines_meshinstance->set_material_override(line_material);
  323. point_lines_mesh->surface_begin(Mesh::PRIMITIVE_LINES);
  324. Rect2 rect;
  325. for (int i = 0; i < poly.size(); i++) {
  326. Vector2 p, p2;
  327. if (i == edited_point) {
  328. p = edited_point_pos;
  329. } else {
  330. p = poly[i];
  331. }
  332. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point)) {
  333. p2 = edited_point_pos;
  334. } else {
  335. p2 = poly[(i + 1) % poly.size()];
  336. }
  337. if (i == 0) {
  338. rect.position = p;
  339. } else {
  340. rect.expand_to(p);
  341. }
  342. Vector3 point = Vector3(p.x, 0.0, p.y);
  343. Vector3 next_point = Vector3(p2.x, 0.0, p2.y);
  344. point_lines_mesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  345. point_lines_mesh->surface_add_vertex(point);
  346. point_lines_mesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  347. point_lines_mesh->surface_add_vertex(next_point);
  348. //Color col=Color(1,0.3,0.1,0.8);
  349. //vpc->draw_line(point,next_point,col,2);
  350. //vpc->draw_texture(handle,point-handle->get_size()*0.5);
  351. }
  352. rect = rect.grow(1);
  353. AABB r;
  354. r.position.x = rect.position.x;
  355. r.position.y = 0.0;
  356. r.position.z = rect.position.y;
  357. r.size.x = rect.size.x;
  358. r.size.y = 0;
  359. r.size.z = rect.size.y;
  360. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  361. point_lines_mesh->surface_add_vertex(r.position);
  362. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  363. point_lines_mesh->surface_add_vertex(r.position + Vector3(0.3, 0, 0));
  364. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  365. point_lines_mesh->surface_add_vertex(r.position);
  366. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  367. point_lines_mesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 0));
  368. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  369. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  370. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  371. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) - Vector3(0.3, 0, 0));
  372. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  373. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  374. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  375. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) + Vector3(0, 0.3, 0));
  376. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  377. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  378. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  379. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) - Vector3(0, 0.3, 0));
  380. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  381. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  382. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  383. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + Vector3(0.3, 0, 0));
  384. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  385. point_lines_mesh->surface_add_vertex(r.position + r.size);
  386. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  387. point_lines_mesh->surface_add_vertex(r.position + r.size - Vector3(0.3, 0, 0));
  388. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  389. point_lines_mesh->surface_add_vertex(r.position + r.size);
  390. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  391. point_lines_mesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  392. point_lines_mesh->surface_end();
  393. if (poly.size() == 0) {
  394. return;
  395. }
  396. Array point_handle_mesh_array;
  397. point_handle_mesh_array.resize(Mesh::ARRAY_MAX);
  398. Vector<Vector3> point_handle_mesh_vertices;
  399. point_handle_mesh_vertices.resize(poly.size());
  400. Vector3 *point_handle_mesh_vertices_ptr = point_handle_mesh_vertices.ptrw();
  401. for (int i = 0; i < poly.size(); i++) {
  402. Vector2 point_2d;
  403. Vector2 p2;
  404. if (i == edited_point) {
  405. point_2d = edited_point_pos;
  406. } else {
  407. point_2d = poly[i];
  408. }
  409. Vector3 point_handle_3d = Vector3(point_2d.x, 0.0, point_2d.y);
  410. point_handle_mesh_vertices_ptr[i] = point_handle_3d;
  411. }
  412. point_handle_mesh_array[Mesh::ARRAY_VERTEX] = point_handle_mesh_vertices;
  413. point_handle_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, point_handle_mesh_array);
  414. point_handle_mesh->surface_set_material(0, handle_material);
  415. }
  416. void NavigationObstacle3DEditor::edit(Node *p_node) {
  417. obstacle_node = Object::cast_to<NavigationObstacle3D>(p_node);
  418. if (obstacle_node) {
  419. //Enable the pencil tool if the polygon is empty
  420. if (_get_polygon().is_empty()) {
  421. _menu_option(MODE_CREATE);
  422. }
  423. wip.clear();
  424. wip_active = false;
  425. edited_point = -1;
  426. p_node->add_child(point_lines_meshinstance);
  427. _polygon_draw();
  428. } else {
  429. obstacle_node = nullptr;
  430. if (point_lines_meshinstance->get_parent()) {
  431. point_lines_meshinstance->get_parent()->remove_child(point_lines_meshinstance);
  432. }
  433. }
  434. }
  435. void NavigationObstacle3DEditor::_bind_methods() {
  436. ClassDB::bind_method(D_METHOD("_polygon_draw"), &NavigationObstacle3DEditor::_polygon_draw);
  437. }
  438. NavigationObstacle3DEditor::NavigationObstacle3DEditor() {
  439. obstacle_node = nullptr;
  440. button_create = memnew(Button);
  441. button_create->set_theme_type_variation("FlatButton");
  442. add_child(button_create);
  443. button_create->connect("pressed", callable_mp(this, &NavigationObstacle3DEditor::_menu_option).bind(MODE_CREATE));
  444. button_create->set_toggle_mode(true);
  445. button_edit = memnew(Button);
  446. button_edit->set_theme_type_variation("FlatButton");
  447. add_child(button_edit);
  448. button_edit->connect("pressed", callable_mp(this, &NavigationObstacle3DEditor::_menu_option).bind(MODE_EDIT));
  449. button_edit->set_toggle_mode(true);
  450. mode = MODE_EDIT;
  451. wip_active = false;
  452. point_lines_meshinstance = memnew(MeshInstance3D);
  453. point_lines_mesh.instantiate();
  454. point_lines_meshinstance->set_mesh(point_lines_mesh);
  455. point_lines_meshinstance->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  456. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  457. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  458. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  459. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  460. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  461. line_material->set_albedo(Color(1, 1, 1));
  462. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  463. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  464. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  465. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  466. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  467. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  468. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  469. handle_material->set_point_size(handle->get_width());
  470. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  471. point_handles_meshinstance = memnew(MeshInstance3D);
  472. point_lines_meshinstance->add_child(point_handles_meshinstance);
  473. point_handle_mesh.instantiate();
  474. point_handles_meshinstance->set_mesh(point_handle_mesh);
  475. point_handles_meshinstance->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  476. snap_ignore = false;
  477. }
  478. NavigationObstacle3DEditor::~NavigationObstacle3DEditor() {
  479. memdelete(point_lines_meshinstance);
  480. }
  481. void NavigationObstacle3DEditorPlugin::edit(Object *p_object) {
  482. obstacle_editor->edit(Object::cast_to<Node>(p_object));
  483. }
  484. bool NavigationObstacle3DEditorPlugin::handles(Object *p_object) const {
  485. return Object::cast_to<NavigationObstacle3D>(p_object);
  486. }
  487. void NavigationObstacle3DEditorPlugin::make_visible(bool p_visible) {
  488. if (p_visible) {
  489. obstacle_editor->show();
  490. } else {
  491. obstacle_editor->hide();
  492. obstacle_editor->edit(nullptr);
  493. }
  494. }
  495. NavigationObstacle3DEditorPlugin::NavigationObstacle3DEditorPlugin() {
  496. obstacle_editor = memnew(NavigationObstacle3DEditor);
  497. Node3DEditor::get_singleton()->add_control_to_menu_panel(obstacle_editor);
  498. obstacle_editor->hide();
  499. }
  500. NavigationObstacle3DEditorPlugin::~NavigationObstacle3DEditorPlugin() {
  501. }