csg_gizmos.cpp 13 KB

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