csg_gizmos.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*************************************************************************/
  2. /* csg_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 (SpatialEditor::get_singleton()->is_snap_enabled()) {
  115. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  116. }
  117. if (d < 0.001)
  118. d = 0.001;
  119. switch (p_idx) {
  120. case 0: s->set_width(d * 2); break;
  121. case 1: s->set_height(d * 2); break;
  122. case 2: s->set_depth(d * 2); break;
  123. }
  124. }
  125. if (Object::cast_to<CSGCylinder>(cs)) {
  126. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  127. Vector3 axis;
  128. axis[p_idx == 0 ? 0 : 1] = 1.0;
  129. Vector3 ra, rb;
  130. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  131. float d = axis.dot(ra);
  132. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  133. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  134. }
  135. if (d < 0.001)
  136. d = 0.001;
  137. if (p_idx == 0)
  138. s->set_radius(d);
  139. else if (p_idx == 1)
  140. s->set_height(d * 2.0);
  141. }
  142. if (Object::cast_to<CSGTorus>(cs)) {
  143. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  144. Vector3 axis;
  145. axis[0] = 1.0;
  146. Vector3 ra, rb;
  147. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  148. float d = axis.dot(ra);
  149. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  150. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  151. }
  152. if (d < 0.001)
  153. d = 0.001;
  154. if (p_idx == 0)
  155. s->set_inner_radius(d);
  156. else if (p_idx == 1)
  157. s->set_outer_radius(d);
  158. }
  159. }
  160. void CSGShapeSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  161. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  162. if (Object::cast_to<CSGSphere>(cs)) {
  163. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  164. if (p_cancel) {
  165. s->set_radius(p_restore);
  166. return;
  167. }
  168. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  169. ur->create_action(TTR("Change Sphere Shape Radius"));
  170. ur->add_do_method(s, "set_radius", s->get_radius());
  171. ur->add_undo_method(s, "set_radius", p_restore);
  172. ur->commit_action();
  173. }
  174. if (Object::cast_to<CSGBox>(cs)) {
  175. CSGBox *s = Object::cast_to<CSGBox>(cs);
  176. if (p_cancel) {
  177. switch (p_idx) {
  178. case 0: s->set_width(p_restore); break;
  179. case 1: s->set_height(p_restore); break;
  180. case 2: s->set_depth(p_restore); break;
  181. }
  182. return;
  183. }
  184. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  185. ur->create_action(TTR("Change Box Shape Extents"));
  186. static const char *method[3] = { "set_width", "set_height", "set_depth" };
  187. float current = 0;
  188. switch (p_idx) {
  189. case 0: current = s->get_width(); break;
  190. case 1: current = s->get_height(); break;
  191. case 2: current = s->get_depth(); break;
  192. }
  193. ur->add_do_method(s, method[p_idx], current);
  194. ur->add_undo_method(s, method[p_idx], p_restore);
  195. ur->commit_action();
  196. }
  197. if (Object::cast_to<CSGCylinder>(cs)) {
  198. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  199. if (p_cancel) {
  200. if (p_idx == 0)
  201. s->set_radius(p_restore);
  202. else
  203. s->set_height(p_restore);
  204. return;
  205. }
  206. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  207. if (p_idx == 0) {
  208. ur->create_action(TTR("Change Cylinder Radius"));
  209. ur->add_do_method(s, "set_radius", s->get_radius());
  210. ur->add_undo_method(s, "set_radius", p_restore);
  211. } else {
  212. ur->create_action(TTR("Change Cylinder Height"));
  213. ur->add_do_method(s, "set_height", s->get_height());
  214. ur->add_undo_method(s, "set_height", p_restore);
  215. }
  216. ur->commit_action();
  217. }
  218. if (Object::cast_to<CSGTorus>(cs)) {
  219. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  220. if (p_cancel) {
  221. if (p_idx == 0)
  222. s->set_inner_radius(p_restore);
  223. else
  224. s->set_outer_radius(p_restore);
  225. return;
  226. }
  227. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  228. if (p_idx == 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 CSGShapeSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  241. 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);
  242. }
  243. String CSGShapeSpatialGizmoPlugin::get_name() const {
  244. return "CSGShapes";
  245. }
  246. int CSGShapeSpatialGizmoPlugin::get_priority() const {
  247. return -1;
  248. }
  249. bool CSGShapeSpatialGizmoPlugin::is_selectable_when_hidden() const {
  250. return true;
  251. }
  252. void CSGShapeSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  253. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  254. p_gizmo->clear();
  255. Ref<Material> material;
  256. switch (cs->get_operation()) {
  257. case CSGShape::OPERATION_UNION:
  258. material = get_material("shape_union_material", p_gizmo);
  259. break;
  260. case CSGShape::OPERATION_INTERSECTION:
  261. material = get_material("shape_intersection_material", p_gizmo);
  262. break;
  263. case CSGShape::OPERATION_SUBTRACTION:
  264. material = get_material("shape_subtraction_material", p_gizmo);
  265. break;
  266. }
  267. Ref<Material> handles_material = get_material("handles");
  268. PoolVector<Vector3> faces = cs->get_brush_faces();
  269. Vector<Vector3> lines;
  270. lines.resize(faces.size() * 2);
  271. {
  272. PoolVector<Vector3>::Read r = faces.read();
  273. for (int i = 0; i < lines.size(); i += 6) {
  274. int f = i / 6;
  275. for (int j = 0; j < 3; j++) {
  276. int j_n = (j + 1) % 3;
  277. lines.write[i + j * 2 + 0] = r[f * 3 + j];
  278. lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
  279. }
  280. }
  281. }
  282. p_gizmo->add_lines(lines, material);
  283. p_gizmo->add_collision_segments(lines);
  284. if (p_gizmo->is_selected()) {
  285. // Draw a translucent representation of the CSG node
  286. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  287. Array array;
  288. array.resize(Mesh::ARRAY_MAX);
  289. array[Mesh::ARRAY_VERTEX] = faces;
  290. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  291. Ref<Material> solid_material;
  292. switch (cs->get_operation()) {
  293. case CSGShape::OPERATION_UNION:
  294. solid_material = get_material("shape_union_solid_material", p_gizmo);
  295. break;
  296. case CSGShape::OPERATION_INTERSECTION:
  297. solid_material = get_material("shape_intersection_solid_material", p_gizmo);
  298. break;
  299. case CSGShape::OPERATION_SUBTRACTION:
  300. solid_material = get_material("shape_subtraction_solid_material", p_gizmo);
  301. break;
  302. }
  303. p_gizmo->add_mesh(mesh, false, Ref<SkinReference>(), solid_material);
  304. }
  305. if (Object::cast_to<CSGSphere>(cs)) {
  306. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  307. float r = s->get_radius();
  308. Vector<Vector3> handles;
  309. handles.push_back(Vector3(r, 0, 0));
  310. p_gizmo->add_handles(handles, handles_material);
  311. }
  312. if (Object::cast_to<CSGBox>(cs)) {
  313. CSGBox *s = Object::cast_to<CSGBox>(cs);
  314. Vector<Vector3> handles;
  315. handles.push_back(Vector3(s->get_width() * 0.5, 0, 0));
  316. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  317. handles.push_back(Vector3(0, 0, s->get_depth() * 0.5));
  318. p_gizmo->add_handles(handles, handles_material);
  319. }
  320. if (Object::cast_to<CSGCylinder>(cs)) {
  321. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  322. Vector<Vector3> handles;
  323. handles.push_back(Vector3(s->get_radius(), 0, 0));
  324. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  325. p_gizmo->add_handles(handles, handles_material);
  326. }
  327. if (Object::cast_to<CSGTorus>(cs)) {
  328. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  329. Vector<Vector3> handles;
  330. handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
  331. handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
  332. p_gizmo->add_handles(handles, handles_material);
  333. }
  334. }
  335. EditorPluginCSG::EditorPluginCSG(EditorNode *p_editor) {
  336. Ref<CSGShapeSpatialGizmoPlugin> gizmo_plugin = Ref<CSGShapeSpatialGizmoPlugin>(memnew(CSGShapeSpatialGizmoPlugin));
  337. SpatialEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  338. }