csg_gizmos.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.2, 0.5, 1, 0.1));
  34. create_material("shape_material", gizmo_color);
  35. create_handle_material("handles");
  36. }
  37. String CSGShapeSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  38. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  39. if (Object::cast_to<CSGSphere>(cs)) {
  40. return "Radius";
  41. }
  42. if (Object::cast_to<CSGBox>(cs)) {
  43. static const char *hname[3] = { "Width", "Height", "Depth" };
  44. return hname[p_idx];
  45. }
  46. if (Object::cast_to<CSGCylinder>(cs)) {
  47. return p_idx == 0 ? "Radius" : "Height";
  48. }
  49. if (Object::cast_to<CSGTorus>(cs)) {
  50. return p_idx == 0 ? "InnerRadius" : "OuterRadius";
  51. }
  52. return "";
  53. }
  54. Variant CSGShapeSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  55. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  56. if (Object::cast_to<CSGSphere>(cs)) {
  57. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  58. return s->get_radius();
  59. }
  60. if (Object::cast_to<CSGBox>(cs)) {
  61. CSGBox *s = Object::cast_to<CSGBox>(cs);
  62. switch (p_idx) {
  63. case 0: return s->get_width();
  64. case 1: return s->get_height();
  65. case 2: return s->get_depth();
  66. }
  67. }
  68. if (Object::cast_to<CSGCylinder>(cs)) {
  69. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  70. return p_idx == 0 ? s->get_radius() : s->get_height();
  71. }
  72. if (Object::cast_to<CSGTorus>(cs)) {
  73. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  74. return p_idx == 0 ? s->get_inner_radius() : s->get_outer_radius();
  75. }
  76. return Variant();
  77. }
  78. void CSGShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  79. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  80. Transform gt = cs->get_global_transform();
  81. //gt.orthonormalize();
  82. Transform gi = gt.affine_inverse();
  83. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  84. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  85. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  86. if (Object::cast_to<CSGSphere>(cs)) {
  87. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  88. Vector3 ra, rb;
  89. Geometry::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  90. float d = ra.x;
  91. if (d < 0.001)
  92. d = 0.001;
  93. s->set_radius(d);
  94. }
  95. if (Object::cast_to<CSGBox>(cs)) {
  96. CSGBox *s = Object::cast_to<CSGBox>(cs);
  97. Vector3 axis;
  98. axis[p_idx] = 1.0;
  99. Vector3 ra, rb;
  100. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  101. float d = ra[p_idx];
  102. if (d < 0.001)
  103. d = 0.001;
  104. switch (p_idx) {
  105. case 0: s->set_width(d * 2); break;
  106. case 1: s->set_height(d * 2); break;
  107. case 2: s->set_depth(d * 2); break;
  108. }
  109. }
  110. if (Object::cast_to<CSGCylinder>(cs)) {
  111. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  112. Vector3 axis;
  113. axis[p_idx == 0 ? 0 : 1] = 1.0;
  114. Vector3 ra, rb;
  115. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  116. float d = axis.dot(ra);
  117. if (d < 0.001)
  118. d = 0.001;
  119. if (p_idx == 0)
  120. s->set_radius(d);
  121. else if (p_idx == 1)
  122. s->set_height(d * 2.0);
  123. }
  124. if (Object::cast_to<CSGTorus>(cs)) {
  125. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  126. Vector3 axis;
  127. axis[0] = 1.0;
  128. Vector3 ra, rb;
  129. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  130. float d = axis.dot(ra);
  131. if (d < 0.001)
  132. d = 0.001;
  133. if (p_idx == 0)
  134. s->set_inner_radius(d);
  135. else if (p_idx == 1)
  136. s->set_outer_radius(d);
  137. }
  138. }
  139. void CSGShapeSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  140. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  141. if (Object::cast_to<CSGSphere>(cs)) {
  142. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  143. if (p_cancel) {
  144. s->set_radius(p_restore);
  145. return;
  146. }
  147. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  148. ur->create_action(TTR("Change Sphere Shape Radius"));
  149. ur->add_do_method(s, "set_radius", s->get_radius());
  150. ur->add_undo_method(s, "set_radius", p_restore);
  151. ur->commit_action();
  152. }
  153. if (Object::cast_to<CSGBox>(cs)) {
  154. CSGBox *s = Object::cast_to<CSGBox>(cs);
  155. if (p_cancel) {
  156. switch (p_idx) {
  157. case 0: s->set_width(p_restore); break;
  158. case 1: s->set_height(p_restore); break;
  159. case 2: s->set_depth(p_restore); break;
  160. }
  161. return;
  162. }
  163. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  164. ur->create_action(TTR("Change Box Shape Extents"));
  165. static const char *method[3] = { "set_width", "set_height", "set_depth" };
  166. float current = 0;
  167. switch (p_idx) {
  168. case 0: current = s->get_width(); break;
  169. case 1: current = s->get_height(); break;
  170. case 2: current = s->get_depth(); break;
  171. }
  172. ur->add_do_method(s, method[p_idx], current);
  173. ur->add_undo_method(s, method[p_idx], p_restore);
  174. ur->commit_action();
  175. }
  176. if (Object::cast_to<CSGCylinder>(cs)) {
  177. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  178. if (p_cancel) {
  179. if (p_idx == 0)
  180. s->set_radius(p_restore);
  181. else
  182. s->set_height(p_restore);
  183. return;
  184. }
  185. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  186. if (p_idx == 0) {
  187. ur->create_action(TTR("Change Cylinder Radius"));
  188. ur->add_do_method(s, "set_radius", s->get_radius());
  189. ur->add_undo_method(s, "set_radius", p_restore);
  190. } else {
  191. ur->create_action(TTR("Change Cylinder Height"));
  192. ur->add_do_method(s, "set_height", s->get_height());
  193. ur->add_undo_method(s, "set_height", p_restore);
  194. }
  195. ur->commit_action();
  196. }
  197. if (Object::cast_to<CSGTorus>(cs)) {
  198. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  199. if (p_cancel) {
  200. if (p_idx == 0)
  201. s->set_inner_radius(p_restore);
  202. else
  203. s->set_outer_radius(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 Torus Inner Radius"));
  209. ur->add_do_method(s, "set_inner_radius", s->get_inner_radius());
  210. ur->add_undo_method(s, "set_inner_radius", p_restore);
  211. } else {
  212. ur->create_action(TTR("Change Torus Outer Radius"));
  213. ur->add_do_method(s, "set_outer_radius", s->get_outer_radius());
  214. ur->add_undo_method(s, "set_outer_radius", p_restore);
  215. }
  216. ur->commit_action();
  217. }
  218. }
  219. bool CSGShapeSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  220. 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);
  221. }
  222. String CSGShapeSpatialGizmoPlugin::get_name() const {
  223. return "CSGShapes";
  224. }
  225. bool CSGShapeSpatialGizmoPlugin::is_selectable_when_hidden() const {
  226. return true;
  227. }
  228. void CSGShapeSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  229. CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
  230. p_gizmo->clear();
  231. Ref<Material> material = get_material("shape_material", p_gizmo);
  232. Ref<Material> handles_material = get_material("handles");
  233. PoolVector<Vector3> faces = cs->get_brush_faces();
  234. Vector<Vector3> lines;
  235. lines.resize(faces.size() * 2);
  236. {
  237. PoolVector<Vector3>::Read r = faces.read();
  238. for (int i = 0; i < lines.size(); i += 6) {
  239. int f = i / 6;
  240. for (int j = 0; j < 3; j++) {
  241. int j_n = (j + 1) % 3;
  242. lines.write[i + j * 2 + 0] = r[f * 3 + j];
  243. lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
  244. }
  245. }
  246. }
  247. p_gizmo->add_lines(lines, material);
  248. p_gizmo->add_collision_segments(lines);
  249. if (Object::cast_to<CSGSphere>(cs)) {
  250. CSGSphere *s = Object::cast_to<CSGSphere>(cs);
  251. float r = s->get_radius();
  252. Vector<Vector3> handles;
  253. handles.push_back(Vector3(r, 0, 0));
  254. p_gizmo->add_handles(handles, handles_material);
  255. }
  256. if (Object::cast_to<CSGBox>(cs)) {
  257. CSGBox *s = Object::cast_to<CSGBox>(cs);
  258. Vector<Vector3> handles;
  259. handles.push_back(Vector3(s->get_width() * 0.5, 0, 0));
  260. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  261. handles.push_back(Vector3(0, 0, s->get_depth() * 0.5));
  262. p_gizmo->add_handles(handles, handles_material);
  263. }
  264. if (Object::cast_to<CSGCylinder>(cs)) {
  265. CSGCylinder *s = Object::cast_to<CSGCylinder>(cs);
  266. Vector<Vector3> handles;
  267. handles.push_back(Vector3(s->get_radius(), 0, 0));
  268. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  269. p_gizmo->add_handles(handles, handles_material);
  270. }
  271. if (Object::cast_to<CSGTorus>(cs)) {
  272. CSGTorus *s = Object::cast_to<CSGTorus>(cs);
  273. Vector<Vector3> handles;
  274. handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
  275. handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
  276. p_gizmo->add_handles(handles, handles_material);
  277. }
  278. }
  279. EditorPluginCSG::EditorPluginCSG(EditorNode *p_editor) {
  280. Ref<CSGShapeSpatialGizmoPlugin> gizmo_plugin = Ref<CSGShapeSpatialGizmoPlugin>(memnew(CSGShapeSpatialGizmoPlugin));
  281. SpatialEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  282. }