csg_gizmos.cpp 13 KB

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