csg_gizmos.cpp 10 KB

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