fog_volume_gizmo_plugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**************************************************************************/
  2. /* fog_volume_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 "fog_volume_gizmo_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/plugins/gizmos/gizmo_3d_helper.h"
  35. #include "scene/3d/fog_volume.h"
  36. FogVolumeGizmoPlugin::FogVolumeGizmoPlugin() {
  37. helper.instantiate();
  38. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/fog_volume");
  39. create_material("shape_material", gizmo_color);
  40. gizmo_color.a = 0.15;
  41. create_material("shape_material_internal", gizmo_color);
  42. create_icon_material("fog_volume_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoFogVolume"), EditorStringName(EditorIcons)));
  43. create_handle_material("handles");
  44. }
  45. FogVolumeGizmoPlugin::~FogVolumeGizmoPlugin() {
  46. }
  47. bool FogVolumeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  48. return (Object::cast_to<FogVolume>(p_spatial) != nullptr);
  49. }
  50. String FogVolumeGizmoPlugin::get_gizmo_name() const {
  51. return "FogVolume";
  52. }
  53. int FogVolumeGizmoPlugin::get_priority() const {
  54. return -1;
  55. }
  56. String FogVolumeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  57. return helper->box_get_handle_name(p_id);
  58. }
  59. Variant FogVolumeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  60. return Vector3(p_gizmo->get_node_3d()->call("get_size"));
  61. }
  62. void FogVolumeGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
  63. helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
  64. }
  65. void FogVolumeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  66. FogVolume *fog_volume = Object::cast_to<FogVolume>(p_gizmo->get_node_3d());
  67. Vector3 size = fog_volume->get_size();
  68. Vector3 sg[2];
  69. helper->get_segment(p_camera, p_point, sg);
  70. Vector3 position;
  71. helper->box_set_handle(sg, p_id, size, position);
  72. fog_volume->set_size(size);
  73. fog_volume->set_global_position(position);
  74. }
  75. void FogVolumeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  76. helper->box_commit_handle(TTR("Change FogVolume Size"), p_cancel, p_gizmo->get_node_3d());
  77. }
  78. void FogVolumeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  79. FogVolume *fog_volume = Object::cast_to<FogVolume>(p_gizmo->get_node_3d());
  80. p_gizmo->clear();
  81. if (fog_volume->get_shape() != RS::FOG_VOLUME_SHAPE_WORLD) {
  82. const Ref<Material> material =
  83. get_material("shape_material", p_gizmo);
  84. const Ref<Material> material_internal =
  85. get_material("shape_material_internal", p_gizmo);
  86. Ref<Material> handles_material = get_material("handles");
  87. Vector<Vector3> lines;
  88. AABB aabb;
  89. aabb.size = fog_volume->get_size();
  90. aabb.position = aabb.size / -2;
  91. for (int i = 0; i < 12; i++) {
  92. Vector3 a, b;
  93. aabb.get_edge(i, a, b);
  94. lines.push_back(a);
  95. lines.push_back(b);
  96. }
  97. Vector<Vector3> handles = helper->box_get_handles(fog_volume->get_size());
  98. p_gizmo->add_lines(lines, material);
  99. p_gizmo->add_collision_segments(lines);
  100. const Ref<Material> icon = get_material("fog_volume_icon", p_gizmo);
  101. p_gizmo->add_unscaled_billboard(icon, 0.05);
  102. p_gizmo->add_handles(handles, handles_material);
  103. }
  104. }