polygon_3d_editor_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. Vector2 points[2] = {
  178. p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth))),
  179. p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, poly[(i + 1) % poly.size()].y, depth)))
  180. };
  181. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, points);
  182. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  183. continue; //not valid to reuse point
  184. }
  185. real_t d = cp.distance_to(gpoint);
  186. if (d < closest_dist && d < grab_threshold) {
  187. closest_dist = d;
  188. closest_pos = cp;
  189. closest_idx = i;
  190. }
  191. }
  192. if (closest_idx >= 0) {
  193. pre_move_edit = poly;
  194. poly.insert(closest_idx + 1, cpoint);
  195. edited_point = closest_idx + 1;
  196. edited_point_pos = cpoint;
  197. _set_polygon(poly);
  198. _polygon_draw();
  199. snap_ignore = true;
  200. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  201. }
  202. } else {
  203. //look for points to move
  204. int closest_idx = -1;
  205. Vector2 closest_pos;
  206. real_t closest_dist = 1e10;
  207. for (int i = 0; i < poly.size(); i++) {
  208. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  209. real_t d = cp.distance_to(gpoint);
  210. if (d < closest_dist && d < grab_threshold) {
  211. closest_dist = d;
  212. closest_pos = cp;
  213. closest_idx = i;
  214. }
  215. }
  216. if (closest_idx >= 0) {
  217. pre_move_edit = poly;
  218. edited_point = closest_idx;
  219. edited_point_pos = poly[closest_idx];
  220. _polygon_draw();
  221. snap_ignore = false;
  222. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  223. }
  224. }
  225. } else {
  226. snap_ignore = false;
  227. if (edited_point != -1) {
  228. //apply
  229. ERR_FAIL_INDEX_V(edited_point, poly.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  230. poly.write[edited_point] = edited_point_pos;
  231. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  232. undo_redo->create_action(TTR("Edit Poly"));
  233. undo_redo->add_do_method(obj, "set_polygon", poly);
  234. undo_redo->add_undo_method(obj, "set_polygon", pre_move_edit);
  235. undo_redo->add_do_method(this, "_polygon_draw");
  236. undo_redo->add_undo_method(this, "_polygon_draw");
  237. undo_redo->commit_action();
  238. edited_point = -1;
  239. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  240. }
  241. }
  242. }
  243. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && edited_point == -1) {
  244. int closest_idx = -1;
  245. Vector2 closest_pos;
  246. real_t closest_dist = 1e10;
  247. for (int i = 0; i < poly.size(); i++) {
  248. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  249. real_t d = cp.distance_to(gpoint);
  250. if (d < closest_dist && d < grab_threshold) {
  251. closest_dist = d;
  252. closest_pos = cp;
  253. closest_idx = i;
  254. }
  255. }
  256. if (closest_idx >= 0) {
  257. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  258. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  259. undo_redo->add_undo_method(obj, "set_polygon", poly);
  260. poly.remove_at(closest_idx);
  261. undo_redo->add_do_method(obj, "set_polygon", poly);
  262. undo_redo->add_do_method(this, "_polygon_draw");
  263. undo_redo->add_undo_method(this, "_polygon_draw");
  264. undo_redo->commit_action();
  265. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  266. }
  267. }
  268. } break;
  269. }
  270. }
  271. Ref<InputEventMouseMotion> mm = p_event;
  272. if (mm.is_valid()) {
  273. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  274. Vector2 gpoint = mm->get_position();
  275. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  276. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  277. Vector3 spoint;
  278. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  279. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  280. }
  281. spoint = gi.xform(spoint);
  282. Vector2 cpoint(spoint.x, spoint.y);
  283. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  284. snap_ignore = false;
  285. }
  286. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  287. cpoint = cpoint.snappedf(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. float Polygon3DEditor::_get_depth() {
  296. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  297. ERR_FAIL_NULL_V_MSG(obj, 0.0f, "Edited object is not valid.");
  298. if (bool(obj->call("_has_editable_3d_polygon_no_depth"))) {
  299. return 0.0f;
  300. }
  301. return float(obj->call("get_depth"));
  302. }
  303. PackedVector2Array Polygon3DEditor::_get_polygon() {
  304. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  305. ERR_FAIL_NULL_V_MSG(obj, PackedVector2Array(), "Edited object is not valid.");
  306. return PackedVector2Array(obj->call("get_polygon"));
  307. }
  308. void Polygon3DEditor::_set_polygon(const PackedVector2Array &p_poly) {
  309. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  310. ERR_FAIL_NULL_MSG(obj, "Edited object is not valid.");
  311. obj->call("set_polygon", p_poly);
  312. }
  313. void Polygon3DEditor::_polygon_draw() {
  314. if (!node) {
  315. return;
  316. }
  317. PackedVector2Array poly;
  318. if (wip_active) {
  319. poly = wip;
  320. } else {
  321. poly = _get_polygon();
  322. }
  323. float depth = _get_depth() * 0.5;
  324. m->clear_surfaces();
  325. imesh->clear_surfaces();
  326. imgeom->set_material_override(line_material);
  327. imesh->surface_begin(Mesh::PRIMITIVE_LINES);
  328. Rect2 rect;
  329. for (int i = 0; i < poly.size(); i++) {
  330. Vector2 p, p2;
  331. p = i == edited_point ? edited_point_pos : poly[i];
  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, p.y, depth);
  343. Vector3 next_point = Vector3(p2.x, p2.y, depth);
  344. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  345. imesh->surface_add_vertex(point);
  346. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  347. imesh->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 = rect.position.y;
  356. r.position.z = depth;
  357. r.size.x = rect.size.x;
  358. r.size.y = rect.size.y;
  359. r.size.z = 0;
  360. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  361. imesh->surface_add_vertex(r.position);
  362. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  363. imesh->surface_add_vertex(r.position + Vector3(0.3, 0, 0));
  364. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  365. imesh->surface_add_vertex(r.position);
  366. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  367. imesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 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));
  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) - Vector3(0.3, 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));
  374. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  375. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) + Vector3(0, 0.3, 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));
  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) - Vector3(0, 0.3, 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));
  382. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  383. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + Vector3(0.3, 0, 0));
  384. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  385. imesh->surface_add_vertex(r.position + r.size);
  386. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  387. imesh->surface_add_vertex(r.position + r.size - Vector3(0.3, 0, 0));
  388. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  389. imesh->surface_add_vertex(r.position + r.size);
  390. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  391. imesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  392. imesh->surface_end();
  393. if (poly.size() == 0) {
  394. return;
  395. }
  396. Array a;
  397. a.resize(Mesh::ARRAY_MAX);
  398. Vector<Vector3> va;
  399. {
  400. va.resize(poly.size());
  401. Vector3 *w = va.ptrw();
  402. for (int i = 0; i < poly.size(); i++) {
  403. Vector2 p, p2;
  404. p = i == edited_point ? edited_point_pos : poly[i];
  405. Vector3 point = Vector3(p.x, p.y, depth);
  406. w[i] = point;
  407. }
  408. }
  409. a[Mesh::ARRAY_VERTEX] = va;
  410. m->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  411. m->surface_set_material(0, handle_material);
  412. }
  413. void Polygon3DEditor::edit(Node *p_node) {
  414. if (p_node) {
  415. node = Object::cast_to<Node3D>(p_node);
  416. node_resource = node->call("_get_editable_3d_polygon_resource");
  417. if (node_resource.is_valid()) {
  418. node_resource->connect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  419. }
  420. //Enable the pencil tool if the polygon is empty
  421. if (_get_polygon().is_empty()) {
  422. _menu_option(MODE_CREATE);
  423. }
  424. wip.clear();
  425. wip_active = false;
  426. edited_point = -1;
  427. if (imgeom->get_parent()) {
  428. imgeom->reparent(p_node, false);
  429. } else {
  430. p_node->add_child(imgeom);
  431. }
  432. _polygon_draw();
  433. set_process(true);
  434. prev_depth = -1;
  435. } else {
  436. node = nullptr;
  437. if (node_resource.is_valid()) {
  438. node_resource->disconnect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  439. }
  440. node_resource.unref();
  441. if (imgeom->get_parent()) {
  442. imgeom->get_parent()->remove_child(imgeom);
  443. }
  444. set_process(false);
  445. }
  446. }
  447. void Polygon3DEditor::_bind_methods() {
  448. ClassDB::bind_method(D_METHOD("_polygon_draw"), &Polygon3DEditor::_polygon_draw);
  449. }
  450. Polygon3DEditor::Polygon3DEditor() {
  451. node = nullptr;
  452. button_create = memnew(Button);
  453. button_create->set_theme_type_variation(SceneStringName(FlatButton));
  454. button_create->set_tooltip_text(TTRC("Create Polygon"));
  455. add_child(button_create);
  456. button_create->connect(SceneStringName(pressed), callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_CREATE));
  457. button_create->set_toggle_mode(true);
  458. button_edit = memnew(Button);
  459. button_edit->set_theme_type_variation(SceneStringName(FlatButton));
  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_albedo(Color(1, 1, 1));
  477. handle_material.instantiate();
  478. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  479. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  480. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  481. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  482. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  483. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  484. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  485. handle_material->set_point_size(handle->get_width());
  486. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  487. pointsm = memnew(MeshInstance3D);
  488. imgeom->add_child(pointsm);
  489. m.instantiate();
  490. pointsm->set_mesh(m);
  491. pointsm->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  492. snap_ignore = false;
  493. }
  494. Polygon3DEditor::~Polygon3DEditor() {
  495. memdelete(imgeom);
  496. }
  497. void Polygon3DEditorPlugin::edit(Object *p_object) {
  498. polygon_editor->edit(Object::cast_to<Node>(p_object));
  499. }
  500. bool Polygon3DEditorPlugin::handles(Object *p_object) const {
  501. return Object::cast_to<Node3D>(p_object) && bool(p_object->call("_is_editable_3d_polygon"));
  502. }
  503. void Polygon3DEditorPlugin::make_visible(bool p_visible) {
  504. if (p_visible) {
  505. polygon_editor->show();
  506. } else {
  507. polygon_editor->hide();
  508. polygon_editor->edit(nullptr);
  509. }
  510. }
  511. Polygon3DEditorPlugin::Polygon3DEditorPlugin() {
  512. polygon_editor = memnew(Polygon3DEditor);
  513. Node3DEditor::get_singleton()->add_control_to_menu_panel(polygon_editor);
  514. polygon_editor->hide();
  515. }
  516. Polygon3DEditorPlugin::~Polygon3DEditorPlugin() {
  517. }