polygon_3d_editor_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "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_undo_redo_manager.h"
  40. #include "node_3d_editor_plugin.h"
  41. #include "scene/3d/camera_3d.h"
  42. void Polygon3DEditor::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_READY: {
  45. button_create->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  46. button_edit->set_icon(get_theme_icon(SNAME("MovePoint"), SNAME("EditorIcons")));
  47. button_edit->set_pressed(true);
  48. get_tree()->connect("node_removed", callable_mp(this, &Polygon3DEditor::_node_removed));
  49. } break;
  50. case NOTIFICATION_PROCESS: {
  51. if (!node) {
  52. return;
  53. }
  54. if (_get_depth() != prev_depth) {
  55. _polygon_draw();
  56. prev_depth = _get_depth();
  57. }
  58. } break;
  59. }
  60. }
  61. void Polygon3DEditor::_node_removed(Node *p_node) {
  62. if (p_node == node) {
  63. node = nullptr;
  64. if (imgeom->get_parent() == p_node) {
  65. p_node->remove_child(imgeom);
  66. }
  67. hide();
  68. set_process(false);
  69. }
  70. }
  71. void Polygon3DEditor::_menu_option(int p_option) {
  72. switch (p_option) {
  73. case MODE_CREATE: {
  74. mode = MODE_CREATE;
  75. button_create->set_pressed(true);
  76. button_edit->set_pressed(false);
  77. } break;
  78. case MODE_EDIT: {
  79. mode = MODE_EDIT;
  80. button_create->set_pressed(false);
  81. button_edit->set_pressed(true);
  82. } break;
  83. }
  84. }
  85. void Polygon3DEditor::_wip_close() {
  86. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  87. ERR_FAIL_COND_MSG(!obj, "Edited object is not valid.");
  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_spatial_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_ctrl_pressed()) {
  161. if (poly.size() < 3) {
  162. undo_redo->create_action(TTR("Edit Poly"));
  163. undo_redo->add_undo_method(obj, "set_polygon", poly);
  164. poly.push_back(cpoint);
  165. undo_redo->add_do_method(obj, "set_polygon", poly);
  166. undo_redo->add_do_method(this, "_polygon_draw");
  167. undo_redo->add_undo_method(this, "_polygon_draw");
  168. undo_redo->commit_action();
  169. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  170. }
  171. //search edges
  172. int closest_idx = -1;
  173. Vector2 closest_pos;
  174. real_t closest_dist = 1e10;
  175. for (int i = 0; i < poly.size(); i++) {
  176. Vector2 points[2] = {
  177. p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth))),
  178. p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, poly[(i + 1) % poly.size()].y, depth)))
  179. };
  180. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, points);
  181. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  182. continue; //not valid to reuse point
  183. }
  184. real_t d = cp.distance_to(gpoint);
  185. if (d < closest_dist && d < grab_threshold) {
  186. closest_dist = d;
  187. closest_pos = cp;
  188. closest_idx = i;
  189. }
  190. }
  191. if (closest_idx >= 0) {
  192. pre_move_edit = poly;
  193. poly.insert(closest_idx + 1, cpoint);
  194. edited_point = closest_idx + 1;
  195. edited_point_pos = cpoint;
  196. _set_polygon(poly);
  197. _polygon_draw();
  198. snap_ignore = true;
  199. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  200. }
  201. } else {
  202. //look for points to move
  203. int closest_idx = -1;
  204. Vector2 closest_pos;
  205. real_t closest_dist = 1e10;
  206. for (int i = 0; i < poly.size(); i++) {
  207. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, poly[i].y, depth)));
  208. real_t d = cp.distance_to(gpoint);
  209. if (d < closest_dist && d < grab_threshold) {
  210. closest_dist = d;
  211. closest_pos = cp;
  212. closest_idx = i;
  213. }
  214. }
  215. if (closest_idx >= 0) {
  216. pre_move_edit = poly;
  217. edited_point = closest_idx;
  218. edited_point_pos = poly[closest_idx];
  219. _polygon_draw();
  220. snap_ignore = false;
  221. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  222. }
  223. }
  224. } else {
  225. snap_ignore = false;
  226. if (edited_point != -1) {
  227. //apply
  228. ERR_FAIL_INDEX_V(edited_point, poly.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  229. poly.write[edited_point] = edited_point_pos;
  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. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  256. undo_redo->add_undo_method(obj, "set_polygon", poly);
  257. poly.remove_at(closest_idx);
  258. undo_redo->add_do_method(obj, "set_polygon", poly);
  259. undo_redo->add_do_method(this, "_polygon_draw");
  260. undo_redo->add_undo_method(this, "_polygon_draw");
  261. undo_redo->commit_action();
  262. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  263. }
  264. }
  265. } break;
  266. }
  267. }
  268. Ref<InputEventMouseMotion> mm = p_event;
  269. if (mm.is_valid()) {
  270. if (edited_point != -1 && (wip_active || (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE)) {
  271. Vector2 gpoint = mm->get_position();
  272. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  273. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  274. Vector3 spoint;
  275. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  276. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  277. }
  278. spoint = gi.xform(spoint);
  279. Vector2 cpoint(spoint.x, spoint.y);
  280. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  281. snap_ignore = false;
  282. }
  283. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  284. cpoint = cpoint.snapped(Vector2(
  285. Node3DEditor::get_singleton()->get_translate_snap(),
  286. Node3DEditor::get_singleton()->get_translate_snap()));
  287. }
  288. edited_point_pos = cpoint;
  289. _polygon_draw();
  290. }
  291. }
  292. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  293. }
  294. float Polygon3DEditor::_get_depth() {
  295. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  296. ERR_FAIL_COND_V_MSG(!obj, 0.0f, "Edited object is not valid.");
  297. if (bool(obj->call("_has_editable_3d_polygon_no_depth"))) {
  298. return 0.0f;
  299. }
  300. return float(obj->call("get_depth"));
  301. }
  302. PackedVector2Array Polygon3DEditor::_get_polygon() {
  303. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  304. ERR_FAIL_COND_V_MSG(!obj, PackedVector2Array(), "Edited object is not valid.");
  305. return PackedVector2Array(obj->call("get_polygon"));
  306. }
  307. void Polygon3DEditor::_set_polygon(PackedVector2Array p_poly) {
  308. Object *obj = node_resource.is_valid() ? (Object *)node_resource.ptr() : node;
  309. ERR_FAIL_COND_MSG(!obj, "Edited object is not valid.");
  310. obj->call("set_polygon", p_poly);
  311. }
  312. void Polygon3DEditor::_polygon_draw() {
  313. if (!node) {
  314. return;
  315. }
  316. PackedVector2Array poly;
  317. if (wip_active) {
  318. poly = wip;
  319. } else {
  320. poly = _get_polygon();
  321. }
  322. float depth = _get_depth() * 0.5;
  323. m->clear_surfaces();
  324. imesh->clear_surfaces();
  325. imgeom->set_material_override(line_material);
  326. imesh->surface_begin(Mesh::PRIMITIVE_LINES);
  327. Rect2 rect;
  328. for (int i = 0; i < poly.size(); i++) {
  329. Vector2 p, p2;
  330. p = i == edited_point ? edited_point_pos : poly[i];
  331. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point)) {
  332. p2 = edited_point_pos;
  333. } else {
  334. p2 = poly[(i + 1) % poly.size()];
  335. }
  336. if (i == 0) {
  337. rect.position = p;
  338. } else {
  339. rect.expand_to(p);
  340. }
  341. Vector3 point = Vector3(p.x, p.y, depth);
  342. Vector3 next_point = Vector3(p2.x, p2.y, depth);
  343. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  344. imesh->surface_add_vertex(point);
  345. imesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  346. imesh->surface_add_vertex(next_point);
  347. //Color col=Color(1,0.3,0.1,0.8);
  348. //vpc->draw_line(point,next_point,col,2);
  349. //vpc->draw_texture(handle,point-handle->get_size()*0.5);
  350. }
  351. rect = rect.grow(1);
  352. AABB r;
  353. r.position.x = rect.position.x;
  354. r.position.y = rect.position.y;
  355. r.position.z = depth;
  356. r.size.x = rect.size.x;
  357. r.size.y = rect.size.y;
  358. r.size.z = 0;
  359. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  360. imesh->surface_add_vertex(r.position);
  361. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  362. imesh->surface_add_vertex(r.position + Vector3(0.3, 0, 0));
  363. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  364. imesh->surface_add_vertex(r.position);
  365. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  366. imesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 0));
  367. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  368. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  369. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  370. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) - Vector3(0.3, 0, 0));
  371. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  372. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  373. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  374. imesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) + Vector3(0, 0.3, 0));
  375. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  376. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  377. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  378. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) - Vector3(0, 0.3, 0));
  379. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  380. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  381. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  382. imesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + Vector3(0.3, 0, 0));
  383. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  384. imesh->surface_add_vertex(r.position + r.size);
  385. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  386. imesh->surface_add_vertex(r.position + r.size - Vector3(0.3, 0, 0));
  387. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  388. imesh->surface_add_vertex(r.position + r.size);
  389. imesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  390. imesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  391. imesh->surface_end();
  392. if (poly.size() == 0) {
  393. return;
  394. }
  395. Array a;
  396. a.resize(Mesh::ARRAY_MAX);
  397. Vector<Vector3> va;
  398. {
  399. va.resize(poly.size());
  400. Vector3 *w = va.ptrw();
  401. for (int i = 0; i < poly.size(); i++) {
  402. Vector2 p, p2;
  403. p = i == edited_point ? edited_point_pos : poly[i];
  404. Vector3 point = Vector3(p.x, p.y, depth);
  405. w[i] = point;
  406. }
  407. }
  408. a[Mesh::ARRAY_VERTEX] = va;
  409. m->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  410. m->surface_set_material(0, handle_material);
  411. }
  412. void Polygon3DEditor::edit(Node *p_node) {
  413. if (p_node) {
  414. node = Object::cast_to<Node3D>(p_node);
  415. node_resource = node->call("_get_editable_3d_polygon_resource");
  416. if (node_resource.is_valid()) {
  417. node_resource->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Polygon3DEditor::_polygon_draw));
  418. }
  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(imgeom);
  427. _polygon_draw();
  428. set_process(true);
  429. prev_depth = -1;
  430. } else {
  431. node = nullptr;
  432. if (node_resource.is_valid()) {
  433. node_resource->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Polygon3DEditor::_polygon_draw));
  434. }
  435. node_resource.unref();
  436. if (imgeom->get_parent()) {
  437. imgeom->get_parent()->remove_child(imgeom);
  438. }
  439. set_process(false);
  440. }
  441. }
  442. void Polygon3DEditor::_bind_methods() {
  443. ClassDB::bind_method(D_METHOD("_polygon_draw"), &Polygon3DEditor::_polygon_draw);
  444. }
  445. Polygon3DEditor::Polygon3DEditor() {
  446. node = nullptr;
  447. undo_redo = EditorNode::get_undo_redo();
  448. add_child(memnew(VSeparator));
  449. button_create = memnew(Button);
  450. button_create->set_flat(true);
  451. add_child(button_create);
  452. button_create->connect("pressed", callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_CREATE));
  453. button_create->set_toggle_mode(true);
  454. button_edit = memnew(Button);
  455. button_edit->set_flat(true);
  456. add_child(button_edit);
  457. button_edit->connect("pressed", callable_mp(this, &Polygon3DEditor::_menu_option).bind(MODE_EDIT));
  458. button_edit->set_toggle_mode(true);
  459. mode = MODE_EDIT;
  460. wip_active = false;
  461. imgeom = memnew(MeshInstance3D);
  462. imesh.instantiate();
  463. imgeom->set_mesh(imesh);
  464. imgeom->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  465. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  466. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  467. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  468. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  469. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  470. line_material->set_albedo(Color(1, 1, 1));
  471. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  472. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  473. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  474. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  475. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  476. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  477. Ref<Texture2D> handle = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Editor3DHandle"), SNAME("EditorIcons"));
  478. handle_material->set_point_size(handle->get_width());
  479. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  480. pointsm = memnew(MeshInstance3D);
  481. imgeom->add_child(pointsm);
  482. m.instantiate();
  483. pointsm->set_mesh(m);
  484. pointsm->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  485. snap_ignore = false;
  486. }
  487. Polygon3DEditor::~Polygon3DEditor() {
  488. memdelete(imgeom);
  489. }
  490. void Polygon3DEditorPlugin::edit(Object *p_object) {
  491. polygon_editor->edit(Object::cast_to<Node>(p_object));
  492. }
  493. bool Polygon3DEditorPlugin::handles(Object *p_object) const {
  494. return Object::cast_to<Node3D>(p_object) && bool(p_object->call("_is_editable_3d_polygon"));
  495. }
  496. void Polygon3DEditorPlugin::make_visible(bool p_visible) {
  497. if (p_visible) {
  498. polygon_editor->show();
  499. } else {
  500. polygon_editor->hide();
  501. polygon_editor->edit(nullptr);
  502. }
  503. }
  504. Polygon3DEditorPlugin::Polygon3DEditorPlugin() {
  505. polygon_editor = memnew(Polygon3DEditor);
  506. Node3DEditor::get_singleton()->add_control_to_menu_panel(polygon_editor);
  507. polygon_editor->hide();
  508. }
  509. Polygon3DEditorPlugin::~Polygon3DEditorPlugin() {
  510. }