occluder_instance_3d_gizmo_plugin.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**************************************************************************/
  2. /* occluder_instance_3d_gizmo_plugin.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 "occluder_instance_3d_gizmo_plugin.h"
  31. #include "editor/editor_settings.h"
  32. #include "editor/editor_undo_redo_manager.h"
  33. #include "editor/plugins/node_3d_editor_plugin.h"
  34. #include "scene/3d/occluder_instance_3d.h"
  35. OccluderInstance3DGizmoPlugin::OccluderInstance3DGizmoPlugin() {
  36. create_material("line_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/occluder"));
  37. create_handle_material("handles");
  38. }
  39. bool OccluderInstance3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  40. return Object::cast_to<OccluderInstance3D>(p_spatial) != nullptr;
  41. }
  42. String OccluderInstance3DGizmoPlugin::get_gizmo_name() const {
  43. return "OccluderInstance3D";
  44. }
  45. int OccluderInstance3DGizmoPlugin::get_priority() const {
  46. return -1;
  47. }
  48. String OccluderInstance3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  49. const OccluderInstance3D *cs = Object::cast_to<OccluderInstance3D>(p_gizmo->get_node_3d());
  50. Ref<Occluder3D> o = cs->get_occluder();
  51. if (o.is_null()) {
  52. return "";
  53. }
  54. if (Object::cast_to<SphereOccluder3D>(*o)) {
  55. return "Radius";
  56. }
  57. if (Object::cast_to<BoxOccluder3D>(*o) || Object::cast_to<QuadOccluder3D>(*o)) {
  58. return "Size";
  59. }
  60. return "";
  61. }
  62. Variant OccluderInstance3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  63. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_node_3d());
  64. Ref<Occluder3D> o = oi->get_occluder();
  65. if (o.is_null()) {
  66. return Variant();
  67. }
  68. if (Object::cast_to<SphereOccluder3D>(*o)) {
  69. Ref<SphereOccluder3D> so = o;
  70. return so->get_radius();
  71. }
  72. if (Object::cast_to<BoxOccluder3D>(*o)) {
  73. Ref<BoxOccluder3D> bo = o;
  74. return bo->get_size();
  75. }
  76. if (Object::cast_to<QuadOccluder3D>(*o)) {
  77. Ref<QuadOccluder3D> qo = o;
  78. return qo->get_size();
  79. }
  80. return Variant();
  81. }
  82. void OccluderInstance3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  83. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_node_3d());
  84. Ref<Occluder3D> o = oi->get_occluder();
  85. if (o.is_null()) {
  86. return;
  87. }
  88. Transform3D gt = oi->get_global_transform();
  89. Transform3D gi = gt.affine_inverse();
  90. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  91. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  92. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  93. bool snap_enabled = Node3DEditor::get_singleton()->is_snap_enabled();
  94. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  95. if (Object::cast_to<SphereOccluder3D>(*o)) {
  96. Ref<SphereOccluder3D> so = o;
  97. Vector3 ra, rb;
  98. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  99. float d = ra.x;
  100. if (snap_enabled) {
  101. d = Math::snapped(d, snap);
  102. }
  103. if (d < 0.001) {
  104. d = 0.001;
  105. }
  106. so->set_radius(d);
  107. }
  108. if (Object::cast_to<BoxOccluder3D>(*o)) {
  109. Vector3 axis;
  110. axis[p_id] = 1.0;
  111. Ref<BoxOccluder3D> bo = o;
  112. Vector3 ra, rb;
  113. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  114. float d = ra[p_id] * 2;
  115. if (snap_enabled) {
  116. d = Math::snapped(d, snap);
  117. }
  118. if (d < 0.001) {
  119. d = 0.001;
  120. }
  121. Vector3 he = bo->get_size();
  122. he[p_id] = d;
  123. bo->set_size(he);
  124. }
  125. if (Object::cast_to<QuadOccluder3D>(*o)) {
  126. Ref<QuadOccluder3D> qo = o;
  127. Plane p = Plane(Vector3(0.0f, 0.0f, 1.0f), 0.0f);
  128. Vector3 intersection;
  129. if (!p.intersects_segment(sg[0], sg[1], &intersection)) {
  130. return;
  131. }
  132. if (p_id == 2) {
  133. Vector2 s = Vector2(intersection.x, intersection.y) * 2.0f;
  134. if (snap_enabled) {
  135. s = s.snappedf(snap);
  136. }
  137. s = s.maxf(0.001);
  138. qo->set_size(s);
  139. } else {
  140. float d = intersection[p_id];
  141. if (snap_enabled) {
  142. d = Math::snapped(d, snap);
  143. }
  144. if (d < 0.001) {
  145. d = 0.001;
  146. }
  147. Vector2 he = qo->get_size();
  148. he[p_id] = d * 2.0f;
  149. qo->set_size(he);
  150. }
  151. }
  152. }
  153. void OccluderInstance3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  154. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_node_3d());
  155. Ref<Occluder3D> o = oi->get_occluder();
  156. if (o.is_null()) {
  157. return;
  158. }
  159. if (Object::cast_to<SphereOccluder3D>(*o)) {
  160. Ref<SphereOccluder3D> so = o;
  161. if (p_cancel) {
  162. so->set_radius(p_restore);
  163. return;
  164. }
  165. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  166. ur->create_action(TTR("Change Sphere Shape Radius"));
  167. ur->add_do_method(so.ptr(), "set_radius", so->get_radius());
  168. ur->add_undo_method(so.ptr(), "set_radius", p_restore);
  169. ur->commit_action();
  170. }
  171. if (Object::cast_to<BoxOccluder3D>(*o)) {
  172. Ref<BoxOccluder3D> bo = o;
  173. if (p_cancel) {
  174. bo->set_size(p_restore);
  175. return;
  176. }
  177. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  178. ur->create_action(TTR("Change Box Shape Size"));
  179. ur->add_do_method(bo.ptr(), "set_size", bo->get_size());
  180. ur->add_undo_method(bo.ptr(), "set_size", p_restore);
  181. ur->commit_action();
  182. }
  183. if (Object::cast_to<QuadOccluder3D>(*o)) {
  184. Ref<QuadOccluder3D> qo = o;
  185. if (p_cancel) {
  186. qo->set_size(p_restore);
  187. return;
  188. }
  189. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  190. ur->create_action(TTR("Change Box Shape Size"));
  191. ur->add_do_method(qo.ptr(), "set_size", qo->get_size());
  192. ur->add_undo_method(qo.ptr(), "set_size", p_restore);
  193. ur->commit_action();
  194. }
  195. }
  196. void OccluderInstance3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  197. OccluderInstance3D *occluder_instance = Object::cast_to<OccluderInstance3D>(p_gizmo->get_node_3d());
  198. p_gizmo->clear();
  199. Ref<Occluder3D> o = occluder_instance->get_occluder();
  200. if (!o.is_valid()) {
  201. return;
  202. }
  203. Vector<Vector3> lines = o->get_debug_lines();
  204. if (!lines.is_empty()) {
  205. Ref<Material> material = get_material("line_material", p_gizmo);
  206. p_gizmo->add_lines(lines, material);
  207. p_gizmo->add_collision_segments(lines);
  208. }
  209. Ref<Material> handles_material = get_material("handles");
  210. if (Object::cast_to<SphereOccluder3D>(*o)) {
  211. Ref<SphereOccluder3D> so = o;
  212. float r = so->get_radius();
  213. Vector<Vector3> handles = { Vector3(r, 0, 0) };
  214. p_gizmo->add_handles(handles, handles_material);
  215. }
  216. if (Object::cast_to<BoxOccluder3D>(*o)) {
  217. Ref<BoxOccluder3D> bo = o;
  218. Vector<Vector3> handles;
  219. for (int i = 0; i < 3; i++) {
  220. Vector3 ax;
  221. ax[i] = bo->get_size()[i] / 2;
  222. handles.push_back(ax);
  223. }
  224. p_gizmo->add_handles(handles, handles_material);
  225. }
  226. if (Object::cast_to<QuadOccluder3D>(*o)) {
  227. Ref<QuadOccluder3D> qo = o;
  228. Vector2 size = qo->get_size();
  229. Vector3 s = Vector3(size.x, size.y, 0.0f) / 2.0f;
  230. Vector<Vector3> handles = { Vector3(s.x, 0.0f, 0.0f), Vector3(0.0f, s.y, 0.0f), Vector3(s.x, s.y, 0.0f) };
  231. p_gizmo->add_handles(handles, handles_material);
  232. }
  233. }