csg_gizmos.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*************************************************************************/
  2. /* csg_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. CSGShape3DGizmoPlugin::CSGShape3DGizmoPlugin() {
  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 CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_idx) const {
  47. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_spatial_node());
  48. if (Object::cast_to<CSGSphere3D>(cs)) {
  49. return "Radius";
  50. }
  51. if (Object::cast_to<CSGBox3D>(cs)) {
  52. return "Size";
  53. }
  54. if (Object::cast_to<CSGCylinder3D>(cs)) {
  55. return p_idx == 0 ? "Radius" : "Height";
  56. }
  57. if (Object::cast_to<CSGTorus3D>(cs)) {
  58. return p_idx == 0 ? "InnerRadius" : "OuterRadius";
  59. }
  60. return "";
  61. }
  62. Variant CSGShape3DGizmoPlugin::get_handle_value(EditorNode3DGizmo *p_gizmo, int p_idx) const {
  63. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_spatial_node());
  64. if (Object::cast_to<CSGSphere3D>(cs)) {
  65. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  66. return s->get_radius();
  67. }
  68. if (Object::cast_to<CSGBox3D>(cs)) {
  69. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  70. return s->get_size();
  71. }
  72. if (Object::cast_to<CSGCylinder3D>(cs)) {
  73. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  74. return p_idx == 0 ? s->get_radius() : s->get_height();
  75. }
  76. if (Object::cast_to<CSGTorus3D>(cs)) {
  77. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  78. return p_idx == 0 ? s->get_inner_radius() : s->get_outer_radius();
  79. }
  80. return Variant();
  81. }
  82. void CSGShape3DGizmoPlugin::set_handle(EditorNode3DGizmo *p_gizmo, int p_idx, Camera3D *p_camera, const Point2 &p_point) {
  83. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_spatial_node());
  84. Transform gt = cs->get_global_transform();
  85. //gt.orthonormalize();
  86. Transform gi = gt.affine_inverse();
  87. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  88. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  89. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  90. if (Object::cast_to<CSGSphere3D>(cs)) {
  91. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  92. Vector3 ra, rb;
  93. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  94. float d = ra.x;
  95. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  96. d = Math::stepify(d, Node3DEditor::get_singleton()->get_translate_snap());
  97. }
  98. if (d < 0.001) {
  99. d = 0.001;
  100. }
  101. s->set_radius(d);
  102. }
  103. if (Object::cast_to<CSGBox3D>(cs)) {
  104. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  105. Vector3 axis;
  106. axis[p_idx] = 1.0;
  107. Vector3 ra, rb;
  108. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  109. float d = ra[p_idx];
  110. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  111. d = Math::stepify(d, Node3DEditor::get_singleton()->get_translate_snap());
  112. }
  113. if (d < 0.001) {
  114. d = 0.001;
  115. }
  116. Vector3 h = s->get_size();
  117. h[p_idx] = d * 2;
  118. s->set_size(h);
  119. }
  120. if (Object::cast_to<CSGCylinder3D>(cs)) {
  121. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  122. Vector3 axis;
  123. axis[p_idx == 0 ? 0 : 1] = 1.0;
  124. Vector3 ra, rb;
  125. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  126. float d = axis.dot(ra);
  127. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  128. d = Math::stepify(d, Node3DEditor::get_singleton()->get_translate_snap());
  129. }
  130. if (d < 0.001) {
  131. d = 0.001;
  132. }
  133. if (p_idx == 0) {
  134. s->set_radius(d);
  135. } else if (p_idx == 1) {
  136. s->set_height(d * 2.0);
  137. }
  138. }
  139. if (Object::cast_to<CSGTorus3D>(cs)) {
  140. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  141. Vector3 axis;
  142. axis[0] = 1.0;
  143. Vector3 ra, rb;
  144. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  145. float d = axis.dot(ra);
  146. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  147. d = Math::stepify(d, Node3DEditor::get_singleton()->get_translate_snap());
  148. }
  149. if (d < 0.001) {
  150. d = 0.001;
  151. }
  152. if (p_idx == 0) {
  153. s->set_inner_radius(d);
  154. } else if (p_idx == 1) {
  155. s->set_outer_radius(d);
  156. }
  157. }
  158. }
  159. void CSGShape3DGizmoPlugin::commit_handle(EditorNode3DGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  160. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_spatial_node());
  161. if (Object::cast_to<CSGSphere3D>(cs)) {
  162. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  163. if (p_cancel) {
  164. s->set_radius(p_restore);
  165. return;
  166. }
  167. UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
  168. ur->create_action(TTR("Change Sphere Shape Radius"));
  169. ur->add_do_method(s, "set_radius", s->get_radius());
  170. ur->add_undo_method(s, "set_radius", p_restore);
  171. ur->commit_action();
  172. }
  173. if (Object::cast_to<CSGBox3D>(cs)) {
  174. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  175. if (p_cancel) {
  176. s->set_size(p_restore);
  177. return;
  178. }
  179. UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
  180. ur->create_action(TTR("Change Box Shape Size"));
  181. ur->add_do_method(s, "set_size", s->get_size());
  182. ur->add_undo_method(s, "set_size", p_restore);
  183. ur->commit_action();
  184. }
  185. if (Object::cast_to<CSGCylinder3D>(cs)) {
  186. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  187. if (p_cancel) {
  188. if (p_idx == 0) {
  189. s->set_radius(p_restore);
  190. } else {
  191. s->set_height(p_restore);
  192. }
  193. return;
  194. }
  195. UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
  196. if (p_idx == 0) {
  197. ur->create_action(TTR("Change Cylinder Radius"));
  198. ur->add_do_method(s, "set_radius", s->get_radius());
  199. ur->add_undo_method(s, "set_radius", p_restore);
  200. } else {
  201. ur->create_action(TTR("Change Cylinder Height"));
  202. ur->add_do_method(s, "set_height", s->get_height());
  203. ur->add_undo_method(s, "set_height", p_restore);
  204. }
  205. ur->commit_action();
  206. }
  207. if (Object::cast_to<CSGTorus3D>(cs)) {
  208. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  209. if (p_cancel) {
  210. if (p_idx == 0) {
  211. s->set_inner_radius(p_restore);
  212. } else {
  213. s->set_outer_radius(p_restore);
  214. }
  215. return;
  216. }
  217. UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
  218. if (p_idx == 0) {
  219. ur->create_action(TTR("Change Torus Inner Radius"));
  220. ur->add_do_method(s, "set_inner_radius", s->get_inner_radius());
  221. ur->add_undo_method(s, "set_inner_radius", p_restore);
  222. } else {
  223. ur->create_action(TTR("Change Torus Outer Radius"));
  224. ur->add_do_method(s, "set_outer_radius", s->get_outer_radius());
  225. ur->add_undo_method(s, "set_outer_radius", p_restore);
  226. }
  227. ur->commit_action();
  228. }
  229. }
  230. bool CSGShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  231. return Object::cast_to<CSGSphere3D>(p_spatial) || Object::cast_to<CSGBox3D>(p_spatial) || Object::cast_to<CSGCylinder3D>(p_spatial) || Object::cast_to<CSGTorus3D>(p_spatial) || Object::cast_to<CSGMesh3D>(p_spatial) || Object::cast_to<CSGPolygon3D>(p_spatial);
  232. }
  233. String CSGShape3DGizmoPlugin::get_gizmo_name() const {
  234. return "CSGShape3D";
  235. }
  236. int CSGShape3DGizmoPlugin::get_priority() const {
  237. return -1;
  238. }
  239. bool CSGShape3DGizmoPlugin::is_selectable_when_hidden() const {
  240. return true;
  241. }
  242. void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  243. CSGShape3D *cs = Object::cast_to<CSGShape3D>(p_gizmo->get_spatial_node());
  244. p_gizmo->clear();
  245. Ref<Material> material;
  246. switch (cs->get_operation()) {
  247. case CSGShape3D::OPERATION_UNION:
  248. material = get_material("shape_union_material", p_gizmo);
  249. break;
  250. case CSGShape3D::OPERATION_INTERSECTION:
  251. material = get_material("shape_intersection_material", p_gizmo);
  252. break;
  253. case CSGShape3D::OPERATION_SUBTRACTION:
  254. material = get_material("shape_subtraction_material", p_gizmo);
  255. break;
  256. }
  257. Ref<Material> handles_material = get_material("handles");
  258. Vector<Vector3> faces = cs->get_brush_faces();
  259. Vector<Vector3> lines;
  260. lines.resize(faces.size() * 2);
  261. {
  262. const Vector3 *r = faces.ptr();
  263. for (int i = 0; i < lines.size(); i += 6) {
  264. int f = i / 6;
  265. for (int j = 0; j < 3; j++) {
  266. int j_n = (j + 1) % 3;
  267. lines.write[i + j * 2 + 0] = r[f * 3 + j];
  268. lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
  269. }
  270. }
  271. }
  272. p_gizmo->add_lines(lines, material);
  273. p_gizmo->add_collision_segments(lines);
  274. if (p_gizmo->is_selected()) {
  275. // Draw a translucent representation of the CSG node
  276. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  277. Array array;
  278. array.resize(Mesh::ARRAY_MAX);
  279. array[Mesh::ARRAY_VERTEX] = faces;
  280. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  281. Ref<Material> solid_material;
  282. switch (cs->get_operation()) {
  283. case CSGShape3D::OPERATION_UNION:
  284. solid_material = get_material("shape_union_solid_material", p_gizmo);
  285. break;
  286. case CSGShape3D::OPERATION_INTERSECTION:
  287. solid_material = get_material("shape_intersection_solid_material", p_gizmo);
  288. break;
  289. case CSGShape3D::OPERATION_SUBTRACTION:
  290. solid_material = get_material("shape_subtraction_solid_material", p_gizmo);
  291. break;
  292. }
  293. p_gizmo->add_mesh(mesh, false, Ref<SkinReference>(), solid_material);
  294. }
  295. if (Object::cast_to<CSGSphere3D>(cs)) {
  296. CSGSphere3D *s = Object::cast_to<CSGSphere3D>(cs);
  297. float r = s->get_radius();
  298. Vector<Vector3> handles;
  299. handles.push_back(Vector3(r, 0, 0));
  300. p_gizmo->add_handles(handles, handles_material);
  301. }
  302. if (Object::cast_to<CSGBox3D>(cs)) {
  303. CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
  304. Vector<Vector3> handles;
  305. for (int i = 0; i < 3; i++) {
  306. Vector3 h;
  307. h[i] = s->get_size()[i] / 2;
  308. handles.push_back(h);
  309. }
  310. p_gizmo->add_handles(handles, handles_material);
  311. }
  312. if (Object::cast_to<CSGCylinder3D>(cs)) {
  313. CSGCylinder3D *s = Object::cast_to<CSGCylinder3D>(cs);
  314. Vector<Vector3> handles;
  315. handles.push_back(Vector3(s->get_radius(), 0, 0));
  316. handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
  317. p_gizmo->add_handles(handles, handles_material);
  318. }
  319. if (Object::cast_to<CSGTorus3D>(cs)) {
  320. CSGTorus3D *s = Object::cast_to<CSGTorus3D>(cs);
  321. Vector<Vector3> handles;
  322. handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
  323. handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
  324. p_gizmo->add_handles(handles, handles_material);
  325. }
  326. }
  327. EditorPluginCSG::EditorPluginCSG(EditorNode *p_editor) {
  328. Ref<CSGShape3DGizmoPlugin> gizmo_plugin = Ref<CSGShape3DGizmoPlugin>(memnew(CSGShape3DGizmoPlugin));
  329. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  330. }