csg_gizmos.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**************************************************************************/
  2. /* csg_gizmos.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 "csg_gizmos.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/plugins/gizmos/gizmo_3d_helper.h"
  36. #include "editor/plugins/node_3d_editor_plugin.h"
  37. #include "scene/3d/camera_3d.h"
  38. #include "scene/3d/mesh_instance_3d.h"
  39. #include "scene/3d/physics/collision_shape_3d.h"
  40. #include "scene/gui/dialogs.h"
  41. #include "scene/gui/menu_button.h"
  42. void CSGShapeEditor::_node_removed(Node *p_node) {
  43. if (p_node == node) {
  44. node = nullptr;
  45. options->hide();
  46. }
  47. }
  48. void CSGShapeEditor::edit(CSGShape3D *p_csg_shape) {
  49. node = p_csg_shape;
  50. if (node) {
  51. options->show();
  52. } else {
  53. options->hide();
  54. }
  55. }
  56. void CSGShapeEditor::_notification(int p_what) {
  57. switch (p_what) {
  58. case NOTIFICATION_THEME_CHANGED: {
  59. options->set_button_icon(get_editor_theme_icon(SNAME("CSGCombiner3D")));
  60. } break;
  61. }
  62. }
  63. void CSGShapeEditor::_menu_option(int p_option) {
  64. Array meshes = node->get_meshes();
  65. if (meshes.is_empty()) {
  66. err_dialog->set_text(TTR("CSG operation returned an empty array."));
  67. err_dialog->popup_centered();
  68. return;
  69. }
  70. switch (p_option) {
  71. case MENU_OPTION_BAKE_MESH_INSTANCE: {
  72. _create_baked_mesh_instance();
  73. } break;
  74. case MENU_OPTION_BAKE_COLLISION_SHAPE: {
  75. _create_baked_collision_shape();
  76. } break;
  77. }
  78. }
  79. void CSGShapeEditor::_create_baked_mesh_instance() {
  80. if (node == get_tree()->get_edited_scene_root()) {
  81. err_dialog->set_text(TTR("Can not add a baked mesh as sibling for the scene root.\nMove the CSG root node below a parent node."));
  82. err_dialog->popup_centered();
  83. return;
  84. }
  85. Ref<ArrayMesh> mesh = node->bake_static_mesh();
  86. if (mesh.is_null()) {
  87. err_dialog->set_text(TTR("CSG operation returned an empty mesh."));
  88. err_dialog->popup_centered();
  89. return;
  90. }
  91. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  92. ur->create_action(TTR("Create baked CSGShape3D Mesh Instance"));
  93. Node *owner = get_tree()->get_edited_scene_root();
  94. MeshInstance3D *mi = memnew(MeshInstance3D);
  95. mi->set_mesh(mesh);
  96. mi->set_name("CSGBakedMeshInstance3D");
  97. mi->set_transform(node->get_transform());
  98. ur->add_do_method(node, "add_sibling", mi, true);
  99. ur->add_do_method(mi, "set_owner", owner);
  100. ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), mi);
  101. ur->add_do_reference(mi);
  102. ur->add_undo_method(node->get_parent(), "remove_child", mi);
  103. ur->commit_action();
  104. }
  105. void CSGShapeEditor::_create_baked_collision_shape() {
  106. if (node == get_tree()->get_edited_scene_root()) {
  107. err_dialog->set_text(TTR("Can not add a baked collision shape as sibling for the scene root.\nMove the CSG root node below a parent node."));
  108. err_dialog->popup_centered();
  109. return;
  110. }
  111. Ref<Shape3D> shape = node->bake_collision_shape();
  112. if (shape.is_null()) {
  113. err_dialog->set_text(TTR("CSG operation returned an empty shape."));
  114. err_dialog->popup_centered();
  115. return;
  116. }
  117. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  118. ur->create_action(TTR("Create baked CSGShape3D Collision Shape"));
  119. Node *owner = get_tree()->get_edited_scene_root();
  120. CollisionShape3D *cshape = memnew(CollisionShape3D);
  121. cshape->set_shape(shape);
  122. cshape->set_name("CSGBakedCollisionShape3D");
  123. cshape->set_transform(node->get_transform());
  124. ur->add_do_method(node, "add_sibling", cshape, true);
  125. ur->add_do_method(cshape, "set_owner", owner);
  126. ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), cshape);
  127. ur->add_do_reference(cshape);
  128. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  129. ur->commit_action();
  130. }
  131. CSGShapeEditor::CSGShapeEditor() {
  132. options = memnew(MenuButton);
  133. options->hide();
  134. options->set_text(TTR("CSG"));
  135. options->set_switch_on_hover(true);
  136. Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
  137. options->get_popup()->add_item(TTR("Bake Mesh Instance"), MENU_OPTION_BAKE_MESH_INSTANCE);
  138. options->get_popup()->add_item(TTR("Bake Collision Shape"), MENU_OPTION_BAKE_COLLISION_SHAPE);
  139. options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &CSGShapeEditor::_menu_option));
  140. err_dialog = memnew(AcceptDialog);
  141. add_child(err_dialog);
  142. }
  143. ///////////
  144. CSGShape3DGizmoPlugin::CSGShape3DGizmoPlugin() {
  145. helper.instantiate();
  146. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/csg");
  147. create_material("shape_union_material", gizmo_color);
  148. create_material("shape_union_solid_material", gizmo_color);
  149. gizmo_color.invert();
  150. create_material("shape_subtraction_material", gizmo_color);
  151. create_material("shape_subtraction_solid_material", gizmo_color);
  152. gizmo_color.r = 0.95;
  153. gizmo_color.g = 0.95;
  154. gizmo_color.b = 0.95;
  155. create_material("shape_intersection_material", gizmo_color);
  156. create_material("shape_intersection_solid_material", gizmo_color);
  157. create_handle_material("handles");
  158. }
  159. CSGShape3DGizmoPlugin::~CSGShape3DGizmoPlugin() {
  160. }
  161. String CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  162. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  163. if (Object::cast_to<CSGSphere3D>(cs)) {
  164. return "Radius";
  165. }
  166. if (Object::cast_to<CSGBox3D>(cs)) {
  167. return helper->box_get_handle_name(p_id);
  168. }
  169. if (Object::cast_to<CSGCylinder3D>(cs)) {
  170. return p_id == 0 ? "Radius" : "Height";
  171. }
  172. if (Object::cast_to<CSGTorus3D>(cs)) {
  173. return p_id == 0 ? "InnerRadius" : "OuterRadius";
  174. }
  175. return "";
  176. }
  177. Variant CSGShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  178. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  179. if (Object::cast_to<CSGSphere3D>(cs)) {
  180. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  181. return s->get_radius();
  182. }
  183. if (Object::cast_to<CSGBox3D>(cs)) {
  184. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  185. return s->get_size();
  186. }
  187. if (Object::cast_to<CSGCylinder3D>(cs)) {
  188. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  189. return p_id == 0 ? s->get_radius() : s->get_height();
  190. }
  191. if (Object::cast_to<CSGTorus3D>(cs)) {
  192. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  193. return p_id == 0 ? s->get_inner_radius() : s->get_outer_radius();
  194. }
  195. return Variant();
  196. }
  197. void CSGShape3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
  198. helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
  199. }
  200. void CSGShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  201. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  202. Vector3 sg[2];
  203. helper->get_segment(p_camera, p_point, sg);
  204. if (Object::cast_to<CSGSphere3D>(cs)) {
  205. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  206. Vector3 ra, rb;
  207. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  208. float d = ra.x;
  209. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  210. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  211. }
  212. if (d < 0.001) {
  213. d = 0.001;
  214. }
  215. s->set_radius(d);
  216. }
  217. if (Object::cast_to<CSGBox3D>(cs)) {
  218. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  219. Vector3 size = s->get_size();
  220. Vector3 position;
  221. helper->box_set_handle(sg, p_id, size, position);
  222. s->set_size(size);
  223. s->set_global_position(position);
  224. }
  225. if (Object::cast_to<CSGCylinder3D>(cs)) {
  226. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  227. real_t height = s->get_height();
  228. real_t radius = s->get_radius();
  229. Vector3 position;
  230. helper->cylinder_set_handle(sg, p_id, height, radius, position);
  231. s->set_height(height);
  232. s->set_radius(radius);
  233. s->set_global_position(position);
  234. }
  235. if (Object::cast_to<CSGTorus3D>(cs)) {
  236. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  237. Vector3 axis;
  238. axis[0] = 1.0;
  239. Vector3 ra, rb;
  240. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  241. float d = axis.dot(ra);
  242. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  243. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  244. }
  245. if (d < 0.001) {
  246. d = 0.001;
  247. }
  248. if (p_id == 0) {
  249. s->set_inner_radius(d);
  250. } else if (p_id == 1) {
  251. s->set_outer_radius(d);
  252. }
  253. }
  254. }
  255. void CSGShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  256. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  257. if (Object::cast_to<CSGSphere3D>(cs)) {
  258. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  259. if (p_cancel) {
  260. s->set_radius(p_restore);
  261. return;
  262. }
  263. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  264. ur->create_action(TTR("Change Sphere Shape Radius"));
  265. ur->add_do_method(s, "set_radius", s->get_radius());
  266. ur->add_undo_method(s, "set_radius", p_restore);
  267. ur->commit_action();
  268. }
  269. if (Object::cast_to<CSGBox3D>(cs)) {
  270. helper->box_commit_handle(TTR("Change CSG Box Size"), p_cancel, cs);
  271. }
  272. if (Object::cast_to<CSGCylinder3D>(cs)) {
  273. helper->cylinder_commit_handle(p_id, TTR("Change CSG Cylinder Radius"), TTR("Change CSG Cylinder Height"), p_cancel, cs);
  274. }
  275. if (Object::cast_to<CSGTorus3D>(cs)) {
  276. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  277. if (p_cancel) {
  278. if (p_id == 0) {
  279. s->set_inner_radius(p_restore);
  280. } else {
  281. s->set_outer_radius(p_restore);
  282. }
  283. return;
  284. }
  285. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  286. if (p_id == 0) {
  287. ur->create_action(TTR("Change Torus Inner Radius"));
  288. ur->add_do_method(s, "set_inner_radius", s->get_inner_radius());
  289. ur->add_undo_method(s, "set_inner_radius", p_restore);
  290. } else {
  291. ur->create_action(TTR("Change Torus Outer Radius"));
  292. ur->add_do_method(s, "set_outer_radius", s->get_outer_radius());
  293. ur->add_undo_method(s, "set_outer_radius", p_restore);
  294. }
  295. ur->commit_action();
  296. }
  297. }
  298. bool CSGShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  299. return Object::cast_to<CSGSphere3D>(p_spatial) || Object::cast_to<CSGBox3D>(p_spatial) || Object::cast_to<CSGCylinder3D>(p_spatial) || Object::cast_to<CSGTorus3D>(p_spatial) || Object::cast_to<CSGMesh3D>(p_spatial) || Object::cast_to<CSGPolygon3D>(p_spatial);
  300. }
  301. String CSGShape3DGizmoPlugin::get_gizmo_name() const {
  302. return "CSGShape3D";
  303. }
  304. int CSGShape3DGizmoPlugin::get_priority() const {
  305. return -1;
  306. }
  307. bool CSGShape3DGizmoPlugin::is_selectable_when_hidden() const {
  308. return true;
  309. }
  310. void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  311. p_gizmo->clear();
  312. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  313. Vector<Vector3> faces = cs->get_brush_faces();
  314. if (faces.size() == 0) {
  315. return;
  316. }
  317. Vector<Vector3> lines;
  318. lines.resize(faces.size() * 2);
  319. {
  320. const Vector3 *r = faces.ptr();
  321. for (int i = 0; i < lines.size(); i += 6) {
  322. int f = i / 6;
  323. for (int j = 0; j < 3; j++) {
  324. int j_n = (j + 1) % 3;
  325. lines.write[i + j * 2 + 0] = r[f * 3 + j];
  326. lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
  327. }
  328. }
  329. }
  330. Ref<Material> material;
  331. switch (cs->get_operation()) {
  332. case CSGShape3D::OPERATION_UNION:
  333. material = get_material("shape_union_material", p_gizmo);
  334. break;
  335. case CSGShape3D::OPERATION_INTERSECTION:
  336. material = get_material("shape_intersection_material", p_gizmo);
  337. break;
  338. case CSGShape3D::OPERATION_SUBTRACTION:
  339. material = get_material("shape_subtraction_material", p_gizmo);
  340. break;
  341. }
  342. Ref<Material> handles_material = get_material("handles");
  343. p_gizmo->add_lines(lines, material);
  344. p_gizmo->add_collision_segments(lines);
  345. if (cs->is_root_shape()) {
  346. Array csg_meshes = cs->get_meshes();
  347. if (csg_meshes.size() == 2) {
  348. Ref<Mesh> csg_mesh = csg_meshes[1];
  349. if (csg_mesh.is_valid()) {
  350. p_gizmo->add_collision_triangles(csg_mesh->generate_triangle_mesh());
  351. }
  352. }
  353. }
  354. if (p_gizmo->is_selected()) {
  355. // Draw a translucent representation of the CSG node
  356. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  357. Array array;
  358. array.resize(Mesh::ARRAY_MAX);
  359. array[Mesh::ARRAY_VERTEX] = faces;
  360. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  361. Ref<Material> solid_material;
  362. switch (cs->get_operation()) {
  363. case CSGShape3D::OPERATION_UNION:
  364. solid_material = get_material("shape_union_solid_material", p_gizmo);
  365. break;
  366. case CSGShape3D::OPERATION_INTERSECTION:
  367. solid_material = get_material("shape_intersection_solid_material", p_gizmo);
  368. break;
  369. case CSGShape3D::OPERATION_SUBTRACTION:
  370. solid_material = get_material("shape_subtraction_solid_material", p_gizmo);
  371. break;
  372. }
  373. p_gizmo->add_mesh(mesh, solid_material);
  374. }
  375. if (Object::cast_to<CSGSphere3D>(cs)) {
  376. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  377. float r = s->get_radius();
  378. Vector<Vector3> handles;
  379. handles.push_back(Vector3(r, 0, 0));
  380. p_gizmo->add_handles(handles, handles_material);
  381. }
  382. if (Object::cast_to<CSGBox3D>(cs)) {
  383. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  384. Vector<Vector3> handles = helper->box_get_handles(s->get_size());
  385. p_gizmo->add_handles(handles, handles_material);
  386. }
  387. if (Object::cast_to<CSGCylinder3D>(cs)) {
  388. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  389. Vector<Vector3> handles = helper->cylinder_get_handles(s->get_height(), s->get_radius());
  390. p_gizmo->add_handles(handles, handles_material);
  391. }
  392. if (Object::cast_to<CSGTorus3D>(cs)) {
  393. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  394. Vector<Vector3> handles;
  395. handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
  396. handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
  397. p_gizmo->add_handles(handles, handles_material);
  398. }
  399. }
  400. void EditorPluginCSG::edit(Object *p_object) {
  401. CSGShape3D *csg_shape = Object::cast_to<CSGShape3D>(p_object);
  402. if (csg_shape && csg_shape->is_root_shape()) {
  403. csg_shape_editor->edit(csg_shape);
  404. } else {
  405. csg_shape_editor->edit(nullptr);
  406. }
  407. }
  408. bool EditorPluginCSG::handles(Object *p_object) const {
  409. CSGShape3D *csg_shape = Object::cast_to<CSGShape3D>(p_object);
  410. return csg_shape && csg_shape->is_root_shape();
  411. }
  412. EditorPluginCSG::EditorPluginCSG() {
  413. Ref<CSGShape3DGizmoPlugin> gizmo_plugin = Ref<CSGShape3DGizmoPlugin>(memnew(CSGShape3DGizmoPlugin));
  414. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  415. csg_shape_editor = memnew(CSGShapeEditor);
  416. EditorNode::get_singleton()->get_gui_base()->add_child(csg_shape_editor);
  417. }
  418. #endif // TOOLS_ENABLED