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