Debian Codegraph @

editor/scene/3d/voxel_gi_editor_plugin.cpp

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