csg_gizmos.cpp 16 KB

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