csg_gizmos.cpp 14 KB

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