polygon_3d_editor_plugin.cpp 20 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/io/file_access.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/plugins/canvas_item_editor_plugin.h"
  40. #include "editor/plugins/node_3d_editor_plugin.h"
  41. #include "scene/3d/camera_3d.h"
  42. #include "scene/gui/separator.h"
  43. void Polygon3DEditor::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_READY: {
  46. button_create->set_icon(get_editor_theme_icon(SNAME("Edit")));
  47. button_edit->set_icon(get_editor_theme_icon(SNAME("MovePoint")));
  48. button_edit->set_pressed(true);
  49. get_tree()->connect("node_removed", callable_mp(this, &Polygon3DEditor::_node_removed));
  50. } break;
  51. case NOTIFICATION_PROCESS: {
  52. if (!node) {
  53. return;
  54. }
  55. if (_get_depth() != prev_depth) {
  56. _polygon_draw();
  57. prev_depth = _get_depth();
  58. }
  59. } break;
  60. }
  61. }
  62. void Polygon3DEditor::_node_removed(Node *p_node) {
  63. if (p_node == node) {
  64. node = nullptr;
  65. if (imgeom->get_parent() == p_node) {
  66. p_node->remove_child(imgeom);
  67. }
  68. hide();
  69. set_process(false);
  70. }
  71. }
  72. void Polygon3DEditor::_menu_option(int p_option) {
  73. switch (p_option) {
  74. case MODE_CREATE: {
  75. mode = MODE_CREATE;
  76. button_create->set_pressed(true);
  77. button_edit->set_pressed(false);
  78. } break;
  79. case MODE_EDIT: {
  80. mode = MODE_EDIT;
  81. button_create->set_pressed(false);
  82. button_edit->set_pressed(true);
  83. } break;
  84. }
  85. }
  86. void Polygon3DEditor::_wip_close() {
  87. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  88. ERR_FAIL_NULL_MSG(obj, "Edited object is not valid.");
  89. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  90. undo_redo->create_action(TTR("Create Polygon3D"));
  91. undo_redo->add_undo_method(obj, "set_polygon", obj->call("get_polygon"));
  92. undo_redo->add_do_method(obj, "set_polygon", wip);
  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 Polygon3DEditor::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  104. if (!node) {
  105. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  106. }
  107. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  108. Transform3D gt = node->get_global_transform();
  109. Transform3D gi = gt.affine_inverse();
  110. float depth = _get_depth() * 0.5;
  111. Vector3 n = gt.basis.get_column(2).normalized();
  112. Plane p(n, gt.origin + n * depth);
  113. Ref<InputEventMouseButton> mb = p_event;
  114. if (mb.is_valid()) {
  115. Vector2 gpoint = mb->get_position();
  116. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  117. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  118. Vector3 spoint;
  119. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  120. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  121. }
  122. spoint = gi.xform(spoint);
  123. Vector2 cpoint(spoint.x, spoint.y);
  124. //DO NOT snap here, it's confusing in 3D for adding points.
  125. //Let the snap happen when the point is being moved, instead.
  126. //cpoint = CanvasItemEditor::get_singleton()->snap_point(cpoint);
  127. PackedVector2Array poly = _get_polygon();
  128. //first check if a point is to be added (segment split)
  129. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  130. switch (mode) {
  131. case MODE_CREATE: {
  132. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  133. if (!wip_active) {
  134. wip.clear();
  135. wip.push_back(cpoint);
  136. wip_active = true;
  137. edited_point_pos = cpoint;
  138. snap_ignore = false;
  139. _polygon_draw();
  140. edited_point = 1;
  141. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  142. } else {
  143. if (wip.size() > 1 && p_camera->unproject_position(gt.xform(Vector3(wip[0].x, wip[0].y, depth))).distance_to(gpoint) < grab_threshold) {
  144. //wip closed
  145. _wip_close();
  146. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  147. } else {
  148. wip.push_back(cpoint);
  149. edited_point = wip.size();
  150. snap_ignore = false;
  151. _polygon_draw();
  152. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  153. }
  154. }
  155. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  156. _wip_close();
  157. }
  158. } break;
  159. case MODE_EDIT: {
  160. if (mb->get_button_index() == MouseButton::LEFT) {
  161. if (mb->is_pressed()) {
  162. if (mb->is_command_or_control_pressed()) {
  163. if (poly.size() < 3) {
  164. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  165. undo_redo->create_action(TTR("Edit Poly"));
  166. undo_redo->add_undo_method(obj, "set_polygon", poly);
  167. poly.push_back(cpoint);
  168. undo_redo->add_do_method(obj, "set_polygon", poly);
  169. undo_redo->add_do_method(this, "_polygon_draw");
  170. undo_redo->add_undo_method(this, "_polygon_draw");
  171. undo_redo->commit_action();
  172. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  173. }
  174. //search edges
  175. int closest_idx = -1;
  176. Vector2 closest_pos;
  177. real_t closest_dist = 1e10;
  178. for (int i = 0; i < poly.size(); i++) {
  179. Vector2 points[2] = {
  180. p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth))),
  181. p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, poly[(i + 1) % poly.size()].y, depth)))
  182. };
  183. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, points);
  184. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  185. continue; //not valid to reuse point
  186. }
  187. real_t d = cp.distance_to(gpoint);
  188. if (d < closest_dist && d < grab_threshold) {
  189. closest_dist = d;
  190. closest_pos = cp;
  191. closest_idx = i;
  192. }
  193. }
  194. if (closest_idx >= 0) {
  195. pre_move_edit = poly;
  196. poly.insert(closest_idx + 1, cpoint);
  197. edited_point = closest_idx + 1;
  198. edited_point_pos = cpoint;
  199. _set_polygon(poly);
  200. _polygon_draw();
  201. snap_ignore = true;
  202. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  203. }
  204. } else {
  205. //look for points to move
  206. int closest_idx = -1;
  207. Vector2 closest_pos;
  208. real_t closest_dist = 1e10;
  209. for (int i = 0; i < poly.size(); i++) {
  210. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  211. real_t d = cp.distance_to(gpoint);
  212. if (d < closest_dist && d < grab_threshold) {
  213. closest_dist = d;
  214. closest_pos = cp;
  215. closest_idx = i;
  216. }
  217. }
  218. if (closest_idx >= 0) {
  219. pre_move_edit = poly;
  220. edited_point = closest_idx;
  221. edited_point_pos = poly[closest_idx];
  222. _polygon_draw();
  223. snap_ignore = false;
  224. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  225. }
  226. }
  227. } else {
  228. snap_ignore = false;
  229. if (edited_point != -1) {
  230. //apply
  231. ERR_FAIL_INDEX_V(edited_point, poly.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  232. poly.write[edited_point] = edited_point_pos;
  233. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  234. undo_redo->create_action(TTR("Edit Poly"));
  235. undo_redo->add_do_method(obj, "set_polygon", poly);
  236. undo_redo->add_undo_method(obj, "set_polygon", pre_move_edit);
  237. undo_redo->add_do_method(this, "_polygon_draw");
  238. undo_redo->add_undo_method(this, "_polygon_draw");
  239. undo_redo->commit_action();
  240. edited_point = -1;
  241. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  242. }
  243. }
  244. }
  245. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && edited_point == -1) {
  246. int closest_idx = -1;
  247. Vector2 closest_pos;
  248. real_t closest_dist = 1e10;
  249. for (int i = 0; i < poly.size(); i++) {
  250. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  251. real_t d = cp.distance_to(gpoint);
  252. if (d < closest_dist && d < grab_threshold) {
  253. closest_dist = d;
  254. closest_pos = cp;
  255. closest_idx = i;
  256. }
  257. }
  258. if (closest_idx >= 0) {
  259. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  260. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  261. undo_redo->add_undo_method(obj, "set_polygon", poly);
  262. poly.remove_at(closest_idx);
  263. undo_redo->add_do_method(obj, "set_polygon", poly);
  264. undo_redo->add_do_method(this, "_polygon_draw");
  265. undo_redo->add_undo_method(this, "_polygon_draw");
  266. undo_redo->commit_action();
  267. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  268. }
  269. }
  270. } break;
  271. }
  272. }
  273. Ref<InputEventMouseMotion> mm = p_event;
  274. if (mm.is_valid()) {
  275. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  276. Vector2 gpoint = mm->get_position();
  277. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  278. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  279. Vector3 spoint;
  280. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  281. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  282. }
  283. spoint = gi.xform(spoint);
  284. Vector2 cpoint(spoint.x, spoint.y);
  285. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  286. snap_ignore = false;
  287. }
  288. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  289. cpoint = cpoint.snapped(Vector2(
  290. Node3DEditor::get_singleton()->get_translate_snap(),
  291. Node3DEditor::get_singleton()->get_translate_snap()));
  292. }
  293. edited_point_pos = cpoint;
  294. _polygon_draw();
  295. }
  296. }
  297. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  298. }
  299. float Polygon3DEditor::_get_depth() {
  300. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  301. ERR_FAIL_NULL_V_MSG(obj, 0.0f, "Edited object is not valid.");
  302. if (bool(obj->call("_has_editable_3d_polygon_no_depth"))) {
  303. return 0.0f;
  304. }
  305. return float(obj->call("get_depth"));
  306. }
  307. PackedVector2Array Polygon3DEditor::_get_polygon() {
  308. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  309. ERR_FAIL_NULL_V_MSG(obj, PackedVector2Array(), "Edited object is not valid.");
  310. return PackedVector2Array(obj->call("get_polygon"));
  311. }
  312. void Polygon3DEditor::_set_polygon(PackedVector2Array p_poly) {
  313. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  314. ERR_FAIL_NULL_MSG(obj, "Edited object is not valid.");
  315. obj->call("set_polygon", p_poly);
  316. }
  317. void Polygon3DEditor::_polygon_draw() {
  318. if (!node) {
  319. return;
  320. }
  321. PackedVector2Array poly;
  322. if (wip_active) {
  323. poly = wip;
  324. } else {
  325. poly = _get_polygon();
  326. }
  327. float depth = _get_depth() * 0.5;
  328. m->clear_surfaces();
  329. imesh->clear_surfaces();
  330. imgeom->set_material_override(line_material);
  331. imesh->surface_begin(Mesh::PRIMITIVE_LINES);
  332. Rect2 rect;
  333. for (int i = 0; i < poly.size(); i++) {
  334. Vector2 p, p2;
  335. p = i == edited_point ? edited_point_pos : poly[i];
  336. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point)) {
  337. p2 = edited_point_pos;
  338. } else {
  339. p2 = poly[(i + 1) % poly.size()];
  340. }
  341. if (i == 0) {
  342. rect.position = p;
  343. } else {
  344. rect.expand_to(p);
  345. }
  346. Vector3 point = Vector3(p.x, p.y, depth);
  347. Vector3 next_point = Vector3(p2.x, p2.y, depth);
  348. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  349. imesh->surface_add_vertex(point);
  350. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  351. imesh->surface_add_vertex(next_point);
  352. //Color col=Color(1,0.3,0.1,0.8);
  353. //vpc->draw_line(point,next_point,col,2);
  354. //vpc->draw_texture(handle,point-handle->get_size()*0.5);
  355. }
  356. rect = rect.grow(1);
  357. AABB r;
  358. r.position.x = rect.position.x;
  359. r.position.y = rect.position.y;
  360. r.position.z = depth;
  361. r.size.x = rect.size.x;
  362. r.size.y = rect.size.y;
  363. r.size.z = 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.3, 0, 0));
  368. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  369. imesh->surface_add_vertex(r.position);
  370. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  371. imesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 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.3, 0, 0));
  376. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  377. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  378. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  379. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 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, 0.3, 0));
  384. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  385. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  386. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  387. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + 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.3, 0, 0));
  392. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  393. imesh->surface_add_vertex(r.position + r.size);
  394. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  395. imesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  396. imesh->surface_end();
  397. if (poly.size() == 0) {
  398. return;
  399. }
  400. Array a;
  401. a.resize(Mesh::ARRAY_MAX);
  402. Vector<Vector3> va;
  403. {
  404. va.resize(poly.size());
  405. Vector3 *w = va.ptrw();
  406. for (int i = 0; i < poly.size(); i++) {
  407. Vector2 p, p2;
  408. p = i == edited_point ? edited_point_pos : poly[i];
  409. Vector3 point = Vector3(p.x, p.y, depth);
  410. w[i] = point;
  411. }
  412. }
  413. a[Mesh::ARRAY_VERTEX] = va;
  414. m->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  415. m->surface_set_material(0, handle_material);
  416. }
  417. void Polygon3DEditor::edit(Node *p_node) {
  418. if (p_node) {
  419. node = Object::cast_to<Node3D>(p_node);
  420. node_resource = node->call("_get_editable_3d_polygon_resource");
  421. if (node_resource.is_valid()) {
  422. node_resource->connect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  423. }
  424. //Enable the pencil tool if the polygon is empty
  425. if (_get_polygon().is_empty()) {
  426. _menu_option(MODE_CREATE);
  427. }
  428. wip.clear();
  429. wip_active = false;
  430. edited_point = -1;
  431. if (imgeom->get_parent()) {
  432. imgeom->reparent(p_node, false);
  433. } else {
  434. p_node->add_child(imgeom);
  435. }
  436. _polygon_draw();
  437. set_process(true);
  438. prev_depth = -1;
  439. } else {
  440. node = nullptr;
  441. if (node_resource.is_valid()) {
  442. node_resource->disconnect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
  443. }
  444. node_resource.unref();
  445. if (imgeom->get_parent()) {
  446. imgeom->get_parent()->remove_child(imgeom);
  447. }
  448. set_process(false);
  449. }
  450. }
  451. void Polygon3DEditor::_bind_methods() {
  452. ClassDB::bind_method(D_METHOD("_polygon_draw"), &Polygon3DEditor::_polygon_draw);
  453. }
  454. Polygon3DEditor::Polygon3DEditor() {
  455. node = nullptr;
  456. button_create = memnew(Button);
  457. button_create->set_theme_type_variation("FlatButton");
  458. add_child(button_create);
  459. button_create->connect("pressed", callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_CREATE));
  460. button_create->set_toggle_mode(true);
  461. button_edit = memnew(Button);
  462. button_edit->set_theme_type_variation("FlatButton");
  463. add_child(button_edit);
  464. button_edit->connect("pressed", callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_EDIT));
  465. button_edit->set_toggle_mode(true);
  466. mode = MODE_EDIT;
  467. wip_active = false;
  468. imgeom = memnew(MeshInstance3D);
  469. imesh.instantiate();
  470. imgeom->set_mesh(imesh);
  471. imgeom->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  472. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  473. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  474. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  475. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  476. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  477. line_material->set_albedo(Color(1, 1, 1));
  478. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  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. 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. }