csg_gizmos.cpp 13 KB

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