csg_gizmos.cpp 13 KB

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