| 1 | /**************************************************************************/ |
| 2 | /* voxel_gi_editor_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 "voxel_gi_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_interface.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "editor/editor_string_names.h" |
| 36 | #include "editor/gui/editor_file_dialog.h" |
| 37 | |
| 38 | void VoxelGIEditorPlugin::_bake() { |
| 39 | if (voxel_gi) { |
| 40 | Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data(); |
| 41 | |
| 42 | if (voxel_gi_data.is_null()) { |
| 43 | String path = get_tree()->get_edited_scene_root()->get_scene_file_path(); |
| 44 | if (path.is_empty()) { |
| 45 | path = "res://" + voxel_gi->get_name() + "_data.res" ; |
| 46 | } else { |
| 47 | String ext = path.get_extension(); |
| 48 | path = path.get_basename() + "." + voxel_gi->get_name() + "_data.res" ; |
| 49 | } |
| 50 | probe_file->set_current_path(path); |
| 51 | probe_file->popup_file_dialog(); |
| 52 | return; |
| 53 | } else { |
| 54 | String path = voxel_gi_data->get_path(); |
| 55 | if (!path.is_resource_file()) { |
| 56 | int srpos = path.find("::" ); |
| 57 | if (srpos != -1) { |
| 58 | String base = path.substr(0, srpos); |
| 59 | if (ResourceLoader::get_resource_type(base) == "PackedScene" ) { |
| 60 | if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) { |
| 61 | EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is not local to the scene." )); |
| 62 | return; |
| 63 | } |
| 64 | } else { |
| 65 | if (FileAccess::exists(base + ".import" )) { |
| 66 | EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is part of an imported resource." )); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } else { |
| 72 | if (FileAccess::exists(path + ".import" )) { |
| 73 | EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is an imported resource." )); |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | voxel_gi->bake(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void VoxelGIEditorPlugin::edit(Object *p_object) { |
| 84 | VoxelGI *s = Object::cast_to<VoxelGI>(p_object); |
| 85 | if (!s) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | voxel_gi = s; |
| 90 | } |
| 91 | |
| 92 | bool VoxelGIEditorPlugin::handles(Object *p_object) const { |
| 93 | return p_object->is_class("VoxelGI" ); |
| 94 | } |
| 95 | |
| 96 | void VoxelGIEditorPlugin::_notification(int p_what) { |
| 97 | switch (p_what) { |
| 98 | case NOTIFICATION_PROCESS: { |
| 99 | if (!voxel_gi) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Set information tooltip on the Bake button. This information is useful |
| 104 | // to optimize performance (video RAM size) and reduce light leaking (individual cell size). |
| 105 | |
| 106 | const Vector3i cell_size = voxel_gi->get_estimated_cell_size(); |
| 107 | |
| 108 | const Vector3 half_size = voxel_gi->get_size() / 2; |
| 109 | |
| 110 | const int data_size = 4; |
| 111 | const double size_mb = cell_size.x * cell_size.y * cell_size.z * data_size / (1024.0 * 1024.0); |
| 112 | // Add a qualitative measurement to help the user assess whether a VoxelGI node is using a lot of VRAM. |
| 113 | String size_quality; |
| 114 | if (size_mb < 16.0) { |
| 115 | size_quality = TTR("Low" ); |
| 116 | } else if (size_mb < 64.0) { |
| 117 | size_quality = TTR("Moderate" ); |
| 118 | } else { |
| 119 | size_quality = TTR("High" ); |
| 120 | } |
| 121 | |
| 122 | String text; |
| 123 | text += vformat(TTR("Subdivisions: %s" ), vformat(U"%d × %d × %d" , cell_size.x, cell_size.y, cell_size.z)) + "\n" ; |
| 124 | text += vformat(TTR("Cell size: %s" ), vformat(U"%.3f × %.3f × %.3f" , half_size.x / cell_size.x, half_size.y / cell_size.y, half_size.z / cell_size.z)) + "\n" ; |
| 125 | text += vformat(TTR("Video RAM size: %s MB (%s)" ), String::num(size_mb, 2), size_quality); |
| 126 | |
| 127 | // Only update the tooltip when needed to avoid constant redrawing. |
| 128 | if (bake->get_tooltip(Point2()) == text) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | bake->set_tooltip_text(text); |
| 133 | } break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void VoxelGIEditorPlugin::make_visible(bool p_visible) { |
| 138 | if (p_visible) { |
| 139 | bake_hb->show(); |
| 140 | set_process(true); |
| 141 | } else { |
| 142 | bake_hb->hide(); |
| 143 | set_process(false); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | EditorProgress *VoxelGIEditorPlugin::tmp_progress = nullptr; |
| 148 | |
| 149 | void VoxelGIEditorPlugin::bake_func_begin(int p_steps) { |
| 150 | ERR_FAIL_COND(tmp_progress != nullptr); |
| 151 | |
| 152 | tmp_progress = memnew(EditorProgress("bake_gi" , TTR("Bake VoxelGI" ), p_steps)); |
| 153 | } |
| 154 | |
| 155 | void VoxelGIEditorPlugin::bake_func_step(int p_step, const String &p_description) { |
| 156 | ERR_FAIL_NULL(tmp_progress); |
| 157 | tmp_progress->step(p_description, p_step, false); |
| 158 | } |
| 159 | |
| 160 | void VoxelGIEditorPlugin::bake_func_end() { |
| 161 | ERR_FAIL_NULL(tmp_progress); |
| 162 | memdelete(tmp_progress); |
| 163 | tmp_progress = nullptr; |
| 164 | } |
| 165 | |
| 166 | void VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake(const String &p_path) { |
| 167 | probe_file->hide(); |
| 168 | if (voxel_gi) { |
| 169 | voxel_gi->bake(); |
| 170 | // Ensure the VoxelGIData is always saved to an external resource. |
| 171 | // This avoids bloating the scene file with large binary data, |
| 172 | // which would be serialized as Base64 if the scene is a `.tscn` file. |
| 173 | Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data(); |
| 174 | ERR_FAIL_COND(voxel_gi_data.is_null()); |
| 175 | voxel_gi_data->set_path(p_path); |
| 176 | ResourceSaver::save(voxel_gi_data, p_path, ResourceSaver::FLAG_CHANGE_PATH); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void VoxelGIEditorPlugin::_bind_methods() { |
| 181 | } |
| 182 | |
| 183 | VoxelGIEditorPlugin::VoxelGIEditorPlugin() { |
| 184 | bake_hb = memnew(HBoxContainer); |
| 185 | bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 186 | bake_hb->hide(); |
| 187 | bake = memnew(Button); |
| 188 | bake->set_flat(true); |
| 189 | // TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it |
| 190 | // when the editor theme updates. |
| 191 | bake->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake" ), EditorStringName(EditorIcons))); |
| 192 | bake->set_text(TTR("Bake VoxelGI" )); |
| 193 | bake->connect("pressed" , callable_mp(this, &VoxelGIEditorPlugin::_bake)); |
| 194 | bake_hb->add_child(bake); |
| 195 | |
| 196 | add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb); |
| 197 | voxel_gi = nullptr; |
| 198 | probe_file = memnew(EditorFileDialog); |
| 199 | probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
| 200 | probe_file->add_filter("*.res" ); |
| 201 | probe_file->connect("file_selected" , callable_mp(this, &VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake)); |
| 202 | EditorInterface::get_singleton()->get_base_control()->add_child(probe_file); |
| 203 | probe_file->set_title(TTR("Select path for VoxelGI Data File" )); |
| 204 | |
| 205 | VoxelGI::bake_begin_function = bake_func_begin; |
| 206 | VoxelGI::bake_step_function = bake_func_step; |
| 207 | VoxelGI::bake_end_function = bake_func_end; |
| 208 | } |
| 209 | |
| 210 | VoxelGIEditorPlugin::~VoxelGIEditorPlugin() { |
| 211 | } |
| 212 | |