csg_gizmos.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. ///////////
  39. CSGShape3DGizmoPlugin::CSGShape3DGizmoPlugin() {
  40. helper.instantiate();
  41. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/csg", Color(0.0, 0.4, 1, 0.15));
  42. create_material("shape_union_material", gizmo_color);
  43. create_material("shape_union_solid_material", gizmo_color);
  44. gizmo_color.invert();
  45. create_material("shape_subtraction_material", gizmo_color);
  46. create_material("shape_subtraction_solid_material", gizmo_color);
  47. gizmo_color.r = 0.95;
  48. gizmo_color.g = 0.95;
  49. gizmo_color.b = 0.95;
  50. create_material("shape_intersection_material", gizmo_color);
  51. create_material("shape_intersection_solid_material", gizmo_color);
  52. create_handle_material("handles");
  53. }
  54. CSGShape3DGizmoPlugin::~CSGShape3DGizmoPlugin() {
  55. }
  56. String CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  57. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  58. if (Object::cast_to<CSGSphere3D>(cs)) {
  59. return "Radius";
  60. }
  61. if (Object::cast_to<CSGBox3D>(cs)) {
  62. return helper->box_get_handle_name(p_id);
  63. }
  64. if (Object::cast_to<CSGCylinder3D>(cs)) {
  65. return p_id == 0 ? "Radius" : "Height";
  66. }
  67. if (Object::cast_to<CSGTorus3D>(cs)) {
  68. return p_id == 0 ? "InnerRadius" : "OuterRadius";
  69. }
  70. return "";
  71. }
  72. Variant CSGShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  73. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  74. if (Object::cast_to<CSGSphere3D>(cs)) {
  75. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  76. return s->get_radius();
  77. }
  78. if (Object::cast_to<CSGBox3D>(cs)) {
  79. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  80. return s->get_size();
  81. }
  82. if (Object::cast_to<CSGCylinder3D>(cs)) {
  83. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  84. return p_id == 0 ? s->get_radius() : s->get_height();
  85. }
  86. if (Object::cast_to<CSGTorus3D>(cs)) {
  87. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  88. return p_id == 0 ? s->get_inner_radius() : s->get_outer_radius();
  89. }
  90. return Variant();
  91. }
  92. void CSGShape3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
  93. helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
  94. }
  95. void CSGShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  96. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  97. Vector3 sg[2];
  98. helper->get_segment(p_camera, p_point, sg);
  99. if (Object::cast_to<CSGSphere3D>(cs)) {
  100. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  101. Vector3 ra, rb;
  102. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  103. float d = ra.x;
  104. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  105. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  106. }
  107. if (d < 0.001) {
  108. d = 0.001;
  109. }
  110. s->set_radius(d);
  111. }
  112. if (Object::cast_to<CSGBox3D>(cs)) {
  113. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  114. Vector3 size = s->get_size();
  115. Vector3 position;
  116. helper->box_set_handle(sg, p_id, size, position);
  117. s->set_size(size);
  118. s->set_global_position(position);
  119. }
  120. if (Object::cast_to<CSGCylinder3D>(cs)) {
  121. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  122. Vector3 axis;
  123. axis[p_id == 0 ? 0 : 1] = 1.0;
  124. Vector3 ra, rb;
  125. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  126. float d = axis.dot(ra);
  127. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  128. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  129. }
  130. if (d < 0.001) {
  131. d = 0.001;
  132. }
  133. if (p_id == 0) {
  134. s->set_radius(d);
  135. } else if (p_id == 1) {
  136. s->set_height(d * 2.0);
  137. }
  138. }
  139. if (Object::cast_to<CSGTorus3D>(cs)) {
  140. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  141. Vector3 axis;
  142. axis[0] = 1.0;
  143. Vector3 ra, rb;
  144. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  145. float d = axis.dot(ra);
  146. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  147. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  148. }
  149. if (d < 0.001) {
  150. d = 0.001;
  151. }
  152. if (p_id == 0) {
  153. s->set_inner_radius(d);
  154. } else if (p_id == 1) {
  155. s->set_outer_radius(d);
  156. }
  157. }
  158. }
  159. void CSGShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  160. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  161. if (Object::cast_to<CSGSphere3D>(cs)) {
  162. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  163. if (p_cancel) {
  164. s->set_radius(p_restore);
  165. return;
  166. }
  167. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  168. ur->create_action(TTR("Change Sphere Shape Radius"));
  169. ur->add_do_method(s, "set_radius", s->get_radius());
  170. ur->add_undo_method(s, "set_radius", p_restore);
  171. ur->commit_action();
  172. }
  173. if (Object::cast_to<CSGBox3D>(cs)) {
  174. helper->box_commit_handle(TTR("Change Box Shape Size"), p_cancel, cs);
  175. }
  176. if (Object::cast_to<CSGCylinder3D>(cs)) {
  177. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  178. if (p_cancel) {
  179. if (p_id == 0) {
  180. s->set_radius(p_restore);
  181. } else {
  182. s->set_height(p_restore);
  183. }
  184. return;
  185. }
  186. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  187. if (p_id == 0) {
  188. ur->create_action(TTR("Change Cylinder Radius"));
  189. ur->add_do_method(s, "set_radius", s->get_radius());
  190. ur->add_undo_method(s, "set_radius", p_restore);
  191. } else {
  192. ur->create_action(TTR("Change Cylinder Height"));
  193. ur->add_do_method(s, "set_height", s->get_height());
  194. ur->add_undo_method(s, "set_height", p_restore);
  195. }
  196. ur->commit_action();
  197. }
  198. if (Object::cast_to<CSGTorus3D>(cs)) {
  199. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  200. if (p_cancel) {
  201. if (p_id == 0) {
  202. s->set_inner_radius(p_restore);
  203. } else {
  204. s->set_outer_radius(p_restore);
  205. }
  206. return;
  207. }
  208. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  209. if (p_id == 0) {
  210. ur->create_action(TTR("Change Torus Inner Radius"));
  211. ur->add_do_method(s, "set_inner_radius", s->get_inner_radius());
  212. ur->add_undo_method(s, "set_inner_radius", p_restore);
  213. } else {
  214. ur->create_action(TTR("Change Torus Outer Radius"));
  215. ur->add_do_method(s, "set_outer_radius", s->get_outer_radius());
  216. ur->add_undo_method(s, "set_outer_radius", p_restore);
  217. }
  218. ur->commit_action();
  219. }
  220. }
  221. bool CSGShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  222. 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);
  223. }
  224. String CSGShape3DGizmoPlugin::get_gizmo_name() const {
  225. return "CSGShape3D";
  226. }
  227. int CSGShape3DGizmoPlugin::get_priority() const {
  228. return -1;
  229. }
  230. bool CSGShape3DGizmoPlugin::is_selectable_when_hidden() const {
  231. return true;
  232. }
  233. void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  234. p_gizmo->clear();
  235. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_node_3d());
  236. Vector<Vector3> faces = cs->get_brush_faces();
  237. if (faces.size() == 0) {
  238. return;
  239. }
  240. Vector<Vector3> lines;
  241. lines.resize(faces.size() * 2);
  242. {
  243. const Vector3 *r = faces.ptr();
  244. for (int i = 0; i < lines.size(); i += 6) {
  245. int f = i / 6;
  246. for (int j = 0; j < 3; j++) {
  247. int j_n = (j + 1) % 3;
  248. lines.write[i + j * 2 + 0] = r[f * 3 + j];
  249. lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
  250. }
  251. }
  252. }
  253. Ref<Material> material;
  254. switch (cs->get_operation()) {
  255. case CSGShape3D::OPERATION_UNION:
  256. material = get_material("shape_union_material", p_gizmo);
  257. break;
  258. case CSGShape3D::OPERATION_INTERSECTION:
  259. material = get_material("shape_intersection_material", p_gizmo);
  260. break;
  261. case CSGShape3D::OPERATION_SUBTRACTION:
  262. material = get_material("shape_subtraction_material", p_gizmo);
  263. break;
  264. }
  265. Ref<Material> handles_material = get_material("handles");
  266. p_gizmo->add_lines(lines, material);
  267. p_gizmo->add_collision_segments(lines);
  268. if (cs->is_root_shape()) {
  269. Array csg_meshes = cs->get_meshes();
  270. if (csg_meshes.size() == 2) {
  271. Ref<Mesh> csg_mesh = csg_meshes[1];
  272. if (csg_mesh.is_valid()) {
  273. p_gizmo->add_collision_triangles(csg_mesh->generate_triangle_mesh());
  274. }
  275. }
  276. }
  277. if (p_gizmo->is_selected()) {
  278. // Draw a translucent representation of the CSG node
  279. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  280. Array array;
  281. array.resize(Mesh::ARRAY_MAX);
  282. array[Mesh::ARRAY_VERTEX] = faces;
  283. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  284. Ref<Material> solid_material;
  285. switch (cs->get_operation()) {
  286. case CSGShape3D::OPERATION_UNION:
  287. solid_material = get_material("shape_union_solid_material", p_gizmo);
  288. break;
  289. case CSGShape3D::OPERATION_INTERSECTION:
  290. solid_material = get_material("shape_intersection_solid_material", p_gizmo);
  291. break;
  292. case CSGShape3D::OPERATION_SUBTRACTION:
  293. solid_material = get_material("shape_subtraction_solid_material", p_gizmo);
  294. break;
  295. }
  296. p_gizmo->add_mesh(mesh, solid_material);
  297. }
  298. if (Object::cast_to<CSGSphere3D>(cs)) {
  299. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  300. float r = s->get_radius();
  301. Vector<Vector3> handles;
  302. handles.push_back(Vector3(r, 0, 0));
  303. p_gizmo->add_handles(handles, handles_material);
  304. }
  305. if (Object::cast_to<CSGBox3D>(cs)) {
  306. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  307. Vector<Vector3> handles = helper->box_get_handles(s->get_size());
  308. p_gizmo->add_handles(handles, handles_material);
  309. }
  310. if (Object::cast_to<CSGCylinder3D>(cs)) {
  311. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  312. Vector<Vector3> handles;
  313. handles.push_back(Vector3(s->get_radius(), 0, 0));
  314. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  315. p_gizmo->add_handles(handles, handles_material);
  316. }
  317. if (Object::cast_to<CSGTorus3D>(cs)) {
  318. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  319. Vector<Vector3> handles;
  320. handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
  321. handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
  322. p_gizmo->add_handles(handles, handles_material);
  323. }
  324. }
  325. EditorPluginCSG::EditorPluginCSG() {
  326. Ref<CSGShape3DGizmoPlugin> gizmo_plugin = Ref<CSGShape3DGizmoPlugin>(memnew(CSGShape3DGizmoPlugin));
  327. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  328. }
  329. #endif // TOOLS_ENABLED