| 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 | |
| 31 | #include "fog_volume_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_settings.h" |
| 35 | #include "editor/editor_string_names.h" |
| 36 | #include "editor/editor_undo_redo_manager.h" |
| 37 | #include "editor/plugins/node_3d_editor_plugin.h" |
| 38 | #include "scene/3d/fog_volume.h" |
| 39 | |
| 40 | FogVolumeGizmoPlugin::FogVolumeGizmoPlugin() { |
| 41 | Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/fog_volume" , Color(0.5, 0.7, 1)); |
| 42 | create_material("shape_material" , gizmo_color); |
| 43 | gizmo_color.a = 0.15; |
| 44 | create_material("shape_material_internal" , gizmo_color); |
| 45 | |
| 46 | create_icon_material("fog_volume_icon" , EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoFogVolume" ), EditorStringName(EditorIcons))); |
| 47 | |
| 48 | create_handle_material("handles" ); |
| 49 | } |
| 50 | |
| 51 | bool FogVolumeGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 52 | return (Object::cast_to<FogVolume>(p_spatial) != nullptr); |
| 53 | } |
| 54 | |
| 55 | String FogVolumeGizmoPlugin::get_gizmo_name() const { |
| 56 | return "FogVolume" ; |
| 57 | } |
| 58 | |
| 59 | int FogVolumeGizmoPlugin::get_priority() const { |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | String FogVolumeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 64 | return "Size" ; |
| 65 | } |
| 66 | |
| 67 | Variant FogVolumeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 68 | return Vector3(p_gizmo->get_node_3d()->call("get_size" )); |
| 69 | } |
| 70 | |
| 71 | void FogVolumeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 72 | Node3D *sn = p_gizmo->get_node_3d(); |
| 73 | |
| 74 | Transform3D gt = sn->get_global_transform(); |
| 75 | Transform3D gi = gt.affine_inverse(); |
| 76 | |
| 77 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
| 78 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
| 79 | |
| 80 | Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) }; |
| 81 | |
| 82 | Vector3 axis; |
| 83 | axis[p_id] = 1.0; |
| 84 | Vector3 ra, rb; |
| 85 | Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); |
| 86 | float d = ra[p_id] * 2; |
| 87 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
| 88 | d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); |
| 89 | } |
| 90 | |
| 91 | if (d < 0.001) { |
| 92 | d = 0.001; |
| 93 | } |
| 94 | |
| 95 | Vector3 he = sn->call("get_size" ); |
| 96 | he[p_id] = d; |
| 97 | sn->call("set_size" , he); |
| 98 | } |
| 99 | |
| 100 | void FogVolumeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 101 | Node3D *sn = p_gizmo->get_node_3d(); |
| 102 | |
| 103 | if (p_cancel) { |
| 104 | sn->call("set_size" , p_restore); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 109 | ur->create_action(TTR("Change Fog Volume Size" )); |
| 110 | ur->add_do_method(sn, "set_size" , sn->call("get_size" )); |
| 111 | ur->add_undo_method(sn, "set_size" , p_restore); |
| 112 | ur->commit_action(); |
| 113 | } |
| 114 | |
| 115 | void FogVolumeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 116 | Node3D *cs = p_gizmo->get_node_3d(); |
| 117 | |
| 118 | p_gizmo->clear(); |
| 119 | |
| 120 | if (RS::FogVolumeShape(int(p_gizmo->get_node_3d()->call("get_shape" ))) != RS::FOG_VOLUME_SHAPE_WORLD) { |
| 121 | const Ref<Material> material = |
| 122 | get_material("shape_material" , p_gizmo); |
| 123 | const Ref<Material> material_internal = |
| 124 | get_material("shape_material_internal" , p_gizmo); |
| 125 | |
| 126 | Ref<Material> handles_material = get_material("handles" ); |
| 127 | |
| 128 | Vector<Vector3> lines; |
| 129 | AABB aabb; |
| 130 | aabb.size = cs->call("get_size" ).operator Vector3(); |
| 131 | aabb.position = aabb.size / -2; |
| 132 | |
| 133 | for (int i = 0; i < 12; i++) { |
| 134 | Vector3 a, b; |
| 135 | aabb.get_edge(i, a, b); |
| 136 | lines.push_back(a); |
| 137 | lines.push_back(b); |
| 138 | } |
| 139 | |
| 140 | Vector<Vector3> handles; |
| 141 | |
| 142 | for (int i = 0; i < 3; i++) { |
| 143 | Vector3 ax; |
| 144 | ax[i] = cs->call("get_size" ).operator Vector3()[i] / 2; |
| 145 | handles.push_back(ax); |
| 146 | } |
| 147 | |
| 148 | p_gizmo->add_lines(lines, material); |
| 149 | p_gizmo->add_collision_segments(lines); |
| 150 | const Ref<Material> icon = get_material("fog_volume_icon" , p_gizmo); |
| 151 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
| 152 | p_gizmo->add_handles(handles, handles_material); |
| 153 | } |
| 154 | } |
| 155 | |