polygon_3d_editor_plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**************************************************************************/
  2. /* polygon_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 "polygon_3d_editor_plugin.h"
  31. #include "core/input/input.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/plugins/canvas_item_editor_plugin.h"
  39. #include "editor/plugins/node_3d_editor_plugin.h"
  40. #include "scene/3d/camera_3d.h"
  41. void Polygon3DEditor::_notification(int p_what) {
  42. switch (p_what) {
  43. case NOTIFICATION_READY: {
  44. button_create->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  45. button_edit->set_button_icon(get_editor_theme_icon(SNAME("MovePoint")));
  46. button_edit->set_pressed(true);
  47. get_tree()->connect("node_removed", callable_mp(this, &Polygon3DEditor::_node_removed));
  48. } break;
  49. case NOTIFICATION_PROCESS: {
  50. if (!node) {
  51. return;
  52. }
  53. if (_get_depth() != prev_depth) {
  54. _polygon_draw();
  55. prev_depth = _get_depth();
  56. }
  57. } break;
  58. }
  59. }
  60. void Polygon3DEditor::_node_removed(Node *p_node) {
  61. if (p_node == node) {
  62. node = nullptr;
  63. if (imgeom->get_parent() == p_node) {
  64. p_node->remove_child(imgeom);
  65. }
  66. hide();
  67. set_process(false);
  68. }
  69. }
  70. void Polygon3DEditor::_menu_option(int p_option) {
  71. switch (p_option) {
  72. case MODE_CREATE: {
  73. mode = MODE_CREATE;
  74. button_create->set_pressed(true);
  75. button_edit->set_pressed(false);
  76. } break;
  77. case MODE_EDIT: {
  78. mode = MODE_EDIT;
  79. button_create->set_pressed(false);
  80. button_edit->set_pressed(true);
  81. } break;
  82. }
  83. }
  84. void Polygon3DEditor::_wip_close() {
  85. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  86. ERR_FAIL_NULL_MSG(obj, "Edited object is not valid.");
  87. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  88. undo_redo->create_action(TTR("Create Polygon3D"));
  89. undo_redo->add_undo_method(obj, "set_polygon", obj->call("get_polygon"));
  90. undo_redo->add_do_method(obj, "set_polygon", wip);
  91. undo_redo->add_do_method(this, "_polygon_draw");
  92. undo_redo->add_undo_method(this, "_polygon_draw");
  93. wip.clear();
  94. wip_active = false;
  95. mode = MODE_EDIT;
  96. button_edit->set_pressed(true);
  97. button_create->set_pressed(false);
  98. edited_point = -1;
  99. undo_redo->commit_action();
  100. }
  101. EditorPlugin::AfterGUIInput Polygon3DEditor::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  102. if (!node) {
  103. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  104. }
  105. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  106. Transform3D gt = node->get_global_transform();
  107. Transform3D gi = gt.affine_inverse();
  108. float depth = _get_depth() * 0.5;
  109. Vector3 n = gt.basis.get_column(2).normalized();
  110. Plane p(n, gt.origin + n * depth);
  111. Ref<InputEventMouseButton> mb = p_event;
  112. if (mb.is_valid()) {
  113. Vector2 gpoint = mb->get_position();
  114. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  115. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  116. Vector3 spoint;
  117. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  118. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  119. }
  120. spoint = gi.xform(spoint);
  121. Vector2 cpoint(spoint.x, spoint.y);
  122. //DO NOT snap here, it's confusing in 3D for adding points.
  123. //Let the snap happen when the point is being moved, instead.
  124. //cpoint = CanvasItemEditor::get_singleton()->snap_point(cpoint);
  125. PackedVector2Array poly = _get_polygon();
  126. //first check if a point is to be added (segment split)
  127. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  128. switch (mode) {
  129. case MODE_CREATE: {
  130. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  131. if (!wip_active) {
  132. wip.clear();
  133. wip.push_back(cpoint);
  134. wip_active = true;
  135. edited_point_pos = cpoint;
  136. snap_ignore = false;
  137. _polygon_draw();
  138. edited_point = 1;
  139. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  140. } else {
  141. if (wip.size() > 1 && p_camera->unproject_position(gt.xform(Vector3(wip[0].x, wip[0].y, depth))).distance_to(gpoint) < grab_threshold) {
  142. //wip closed
  143. _wip_close();
  144. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  145. } else {
  146. wip.push_back(cpoint);
  147. edited_point = wip.size();
  148. snap_ignore = false;
  149. _polygon_draw();
  150. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  151. }
  152. }
  153. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  154. _wip_close();
  155. }
  156. } break;
  157. case MODE_EDIT: {
  158. if (mb->get_button_index() == MouseButton::LEFT) {
  159. if (mb->is_pressed()) {
  160. if (mb->is_command_or_control_pressed()) {
  161. if (poly.size() < 3) {
  162. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  163. undo_redo->create_action(TTR("Edit Poly"));
  164. undo_redo->add_undo_method(obj, "set_polygon", poly);
  165. poly.push_back(cpoint);
  166. undo_redo->add_do_method(obj, "set_polygon", poly);
  167. undo_redo->add_do_method(this, "_polygon_draw");
  168. undo_redo->add_undo_method(this, "_polygon_draw");
  169. undo_redo->commit_action();
  170. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  171. }
  172. //search edges
  173. int closest_idx = -1;
  174. Vector2 closest_pos;
  175. real_t closest_dist = 1e10;
  176. for (int i = 0; i < poly.size(); i++) {
  177. const Vector2 segment_a = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  178. const Vector2 segment_b = p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, poly[(i + 1) % poly.size()].y, depth)));
  179. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, segment_a, segment_b);
  180. if (cp.distance_squared_to(segment_a) < CMP_EPSILON2 || cp.distance_squared_to(segment_b) < 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, poly[i].y, depth)));
  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, poly[i].y, depth)));
  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.y);
  281. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  282. snap_ignore = false;
  283. }
  284. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  285. cpoint = cpoint.snappedf(Node3DEditor::get_singleton()->get_translate_snap());
  286. }
  287. edited_point_pos = cpoint;
  288. _polygon_draw();
  289. }
  290. }
  291. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  292. }
  293. float Polygon3DEditor::_get_depth() {
  294. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  295. ERR_FAIL_NULL_V_MSG(obj, 0.0f, "Edited object is not valid.");
  296. if (bool(obj->call("_has_editable_3d_polygon_no_depth"))) {
  297. return 0.0f;
  298. }
  299. return float(obj->call("get_depth"));
  300. }
  301. PackedVector2Array Polygon3DEditor::_get_polygon() {
  302. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  303. ERR_FAIL_NULL_V_MSG(obj, PackedVector2Array(), "Edited object is not valid.");
  304. return PackedVector2Array(obj->call("get_polygon"));
  305. }
  306. void Polygon3DEditor::_set_polygon(const PackedVector2Array &p_poly) {
  307. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  308. ERR_FAIL_NULL_MSG(obj, "Edited object is not valid.");
  309. obj->call("set_polygon", p_poly);
  310. }
  311. void Polygon3DEditor::_polygon_draw() {
  312. if (!node) {
  313. return;
  314. }
  315. PackedVector2Array poly;
  316. if (wip_active) {
  317. poly = wip;
  318. } else {
  319. poly = _get_polygon();
  320. }
  321. float depth = _get_depth() * 0.5;
  322. m->clear_surfaces();
  323. imesh->clear_surfaces();
  324. imgeom->set_material_override(line_material);
  325. imesh->surface_begin(Mesh::PRIMITIVE_LINES);
  326. Rect2 rect;
  327. for (int i = 0; i < poly.size(); i++) {
  328. Vector2 p, p2;
  329. p = i == edited_point ? edited_point_pos : poly[i];
  330. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point)) {
  331. p2 = edited_point_pos;
  332. } else {
  333. p2 = poly[(i + 1) % poly.size()];
  334. }
  335. if (i == 0) {
  336. rect.position = p;
  337. } else {
  338. rect.expand_to(p);
  339. }
  340. Vector3 point = Vector3(p.x, p.y, depth);
  341. Vector3 next_point = Vector3(p2.x, p2.y, depth);
  342. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  343. imesh->surface_add_vertex(point);
  344. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  345. imesh->surface_add_vertex(next_point);
  346. //Color col=Color(1,0.3,0.1,0.8);
  347. //vpc->draw_line(point,next_point,col,2);
  348. //vpc->draw_texture(handle,point-handle->get_size()*0.5);
  349. }
  350. rect = rect.grow(1);
  351. AABB r;
  352. r.position.x = rect.position.x;
  353. r.position.y = rect.position.y;
  354. r.position.z = depth;
  355. r.size.x = rect.size.x;
  356. r.size.y = rect.size.y;
  357. r.size.z = 0;
  358. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  359. imesh->surface_add_vertex(r.position);
  360. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  361. imesh->surface_add_vertex(r.position + Vector3(0.3, 0, 0));
  362. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  363. imesh->surface_add_vertex(r.position);
  364. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  365. imesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 0));
  366. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  367. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  368. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  369. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) - Vector3(0.3, 0, 0));
  370. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  371. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  372. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  373. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) + Vector3(0, 0.3, 0));
  374. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  375. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  376. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  377. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) - Vector3(0, 0.3, 0));
  378. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  379. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  380. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  381. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + Vector3(0.3, 0, 0));
  382. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  383. imesh->surface_add_vertex(r.position + r.size);
  384. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  385. imesh->surface_add_vertex(r.position + r.size - Vector3(0.3, 0, 0));
  386. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  387. imesh->surface_add_vertex(r.position + r.size);
  388. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  389. imesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  390. imesh->surface_end();
  391. if (poly.is_empty()) {
  392. return;
  393. }
  394. Array a;
  395. a.resize(Mesh::ARRAY_MAX);
  396. Vector<Vector3> va;
  397. {
  398. va.resize(poly.size());
  399. Vector3 *w = va.ptrw();
  400. for (int i = 0; i < poly.size(); i++) {
  401. Vector2 p;
  402. p = i == edited_point ? edited_point_pos : poly[i];
  403. Vector3 point = Vector3(p.x, p.y, depth);
  404. w[i] = point;
  405. }
  406. }
  407. a[Mesh::ARRAY_VERTEX] = va;
  408. m->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  409. m->surface_set_material(0, handle_material);
  410. }
  411. void Polygon3DEditor::edit(Node *p_node) {
  412. if (p_node) {
  413. node = Object::cast_to<Node3D>(p_node);
  414. node_resource = node->call("_get_editable_3d_polygon_resource");
  415. if (node_resource.is_valid()) {
  416. node_resource->connect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  417. }
  418. //Enable the pencil tool if the polygon is empty
  419. if (_get_polygon().is_empty()) {
  420. _menu_option(MODE_CREATE);
  421. }
  422. wip.clear();
  423. wip_active = false;
  424. edited_point = -1;
  425. if (imgeom->get_parent()) {
  426. imgeom->reparent(p_node, false);
  427. } else {
  428. p_node->add_child(imgeom);
  429. }
  430. _polygon_draw();
  431. set_process(true);
  432. prev_depth = -1;
  433. } else {
  434. node = nullptr;
  435. if (node_resource.is_valid()) {
  436. node_resource->disconnect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  437. }
  438. node_resource.unref();
  439. if (imgeom->get_parent()) {
  440. imgeom->get_parent()->remove_child(imgeom);
  441. }
  442. set_process(false);
  443. }
  444. }
  445. void Polygon3DEditor::_bind_methods() {
  446. ClassDB::bind_method(D_METHOD("_polygon_draw"), &Polygon3DEditor::_polygon_draw);
  447. }
  448. Polygon3DEditor::Polygon3DEditor() {
  449. node = nullptr;
  450. button_create = memnew(Button);
  451. button_create->set_theme_type_variation(SceneStringName(FlatButton));
  452. button_create->set_accessibility_name(TTRC("Create Polygon"));
  453. button_create->set_tooltip_text(TTRC("Create Polygon"));
  454. add_child(button_create);
  455. button_create->connect(SceneStringName(pressed), callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_CREATE));
  456. button_create->set_toggle_mode(true);
  457. button_edit = memnew(Button);
  458. button_edit->set_theme_type_variation(SceneStringName(FlatButton));
  459. button_edit->set_accessibility_name(TTRC("Edit Polygon"));
  460. button_edit->set_tooltip_text(TTRC("Edit Polygon"));
  461. add_child(button_edit);
  462. button_edit->connect(SceneStringName(pressed), callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_EDIT));
  463. button_edit->set_toggle_mode(true);
  464. mode = MODE_EDIT;
  465. wip_active = false;
  466. imgeom = memnew(MeshInstance3D);
  467. imesh.instantiate();
  468. imgeom->set_mesh(imesh);
  469. imgeom->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  470. line_material.instantiate();
  471. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  472. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  473. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  474. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  475. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  476. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  477. line_material->set_albedo(Color(1, 1, 1));
  478. handle_material.instantiate();
  479. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  480. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  481. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  482. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  483. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  484. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  485. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  486. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  487. handle_material->set_point_size(handle->get_width());
  488. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  489. pointsm = memnew(MeshInstance3D);
  490. imgeom->add_child(pointsm);
  491. m.instantiate();
  492. pointsm->set_mesh(m);
  493. pointsm->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  494. snap_ignore = false;
  495. }
  496. Polygon3DEditor::~Polygon3DEditor() {
  497. memdelete(imgeom);
  498. }
  499. void Polygon3DEditorPlugin::edit(Object *p_object) {
  500. polygon_editor->edit(Object::cast_to<Node>(p_object));
  501. }
  502. bool Polygon3DEditorPlugin::handles(Object *p_object) const {
  503. return Object::cast_to<Node3D>(p_object) && bool(p_object->call("_is_editable_3d_polygon"));
  504. }
  505. void Polygon3DEditorPlugin::make_visible(bool p_visible) {
  506. if (p_visible) {
  507. polygon_editor->show();
  508. } else {
  509. polygon_editor->hide();
  510. polygon_editor->edit(nullptr);
  511. }
  512. }
  513. Polygon3DEditorPlugin::Polygon3DEditorPlugin() {
  514. polygon_editor = memnew(Polygon3DEditor);
  515. Node3DEditor::get_singleton()->add_control_to_menu_panel(polygon_editor);
  516. polygon_editor->hide();
  517. }