Debian Codegraph @

editor/scene/3d/gpu_particles_collision_sdf_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gpu_particles_collision_sdf_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 "gpu_particles_collision_sdf_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 GPUParticlesCollisionSDF3DEditorPlugin::_bake() {
39 if (col_sdf) {
40 if (col_sdf->get_texture().is_null() || !col_sdf->get_texture()->get_path().is_resource_file()) {
41 String path = get_tree()->get_edited_scene_root()->get_scene_file_path();
42 if (path.is_empty()) {
43 path = "res://" + col_sdf->get_name() + "_data.exr";
44 } else {
45 path = path.get_basename() + "." + col_sdf->get_name() + "_data.exr";
46 }
47 probe_file->set_current_path(path);
48 probe_file->popup_file_dialog();
49 return;
50 }
51
52 _sdf_save_path_and_bake(col_sdf->get_texture()->get_path());
53 }
54 }
55
56 void GPUParticlesCollisionSDF3DEditorPlugin::edit(Object *p_object) {
57 GPUParticlesCollisionSDF3D *s = Object::cast_to<GPUParticlesCollisionSDF3D>(p_object);
58 if (!s) {
59 return;
60 }
61
62 col_sdf = s;
63 }
64
65 bool GPUParticlesCollisionSDF3DEditorPlugin::handles(Object *p_object) const {
66 return p_object->is_class("GPUParticlesCollisionSDF3D");
67 }
68
69 void GPUParticlesCollisionSDF3DEditorPlugin::_notification(int p_what) {
70 switch (p_what) {
71 case NOTIFICATION_PROCESS: {
72 if (!col_sdf) {
73 return;
74 }
75
76 // Set information tooltip on the Bake button. This information is useful
77 // to optimize performance (video RAM size) and reduce collision tunneling (individual cell size).
78
79 const Vector3i size = col_sdf->get_estimated_cell_size();
80
81 const Vector3 extents = col_sdf->get_size() / 2;
82
83 int data_size = 2;
84 const double size_mb = size.x * size.y * size.z * data_size / (1024.0 * 1024.0);
85 // Add a qualitative measurement to help the user assess whether a GPUParticlesCollisionSDF3D node is using a lot of VRAM.
86 String size_quality;
87 if (size_mb < 8.0) {
88 size_quality = TTR("Low");
89 } else if (size_mb < 32.0) {
90 size_quality = TTR("Moderate");
91 } else {
92 size_quality = TTR("High");
93 }
94
95 String text;
96 text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", size.x, size.y, size.z)) + "\n";
97 text += vformat(TTR("Cell size: %s"), vformat(U"%.3f × %.3f × %.3f", extents.x / size.x, extents.y / size.y, extents.z / size.z)) + "\n";
98 text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality);
99
100 // Only update the tooltip when needed to avoid constant redrawing.
101 if (bake->get_tooltip(Point2()) == text) {
102 return;
103 }
104
105 bake->set_tooltip_text(text);
106 } break;
107 }
108 }
109
110 void GPUParticlesCollisionSDF3DEditorPlugin::make_visible(bool p_visible) {
111 if (p_visible) {
112 bake_hb->show();
113 set_process(true);
114 } else {
115 bake_hb->hide();
116 set_process(false);
117 }
118 }
119
120 EditorProgress *GPUParticlesCollisionSDF3DEditorPlugin::tmp_progress = nullptr;
121
122 void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_begin(int p_steps) {
123 ERR_FAIL_COND(tmp_progress != nullptr);
124
125 tmp_progress = memnew(EditorProgress("bake_sdf", TTR("Bake SDF"), p_steps));
126 }
127
128 void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_step(int p_step, const String &p_description) {
129 ERR_FAIL_NULL(tmp_progress);
130 tmp_progress->step(p_description, p_step, false);
131 }
132
133 void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_end() {
134 ERR_FAIL_NULL(tmp_progress);
135 memdelete(tmp_progress);
136 tmp_progress = nullptr;
137 }
138
139 void GPUParticlesCollisionSDF3DEditorPlugin::_sdf_save_path_and_bake(const String &p_path) {
140 probe_file->hide();
141 if (col_sdf) {
142 Ref<Image> bake_img = col_sdf->bake();
143 if (bake_img.is_null()) {
144 EditorNode::get_singleton()->show_warning(TTR("No faces detected during GPUParticlesCollisionSDF3D bake.\nCheck whether there are visible meshes matching the bake mask within its extents."));
145 return;
146 }
147
148 Ref<ConfigFile> config;
149
150 config.instantiate();
151 if (FileAccess::exists(p_path + ".import")) {
152 config->load(p_path + ".import");
153 }
154
155 config->set_value("remap", "importer", "3d_texture");
156 config->set_value("remap", "type", "CompressedTexture3D");
157 if (!config->has_section_key("params", "compress/mode")) {
158 config->set_value("params", "compress/mode", 3); //user may want another compression, so leave it be
159 }
160 config->set_value("params", "compress/channel_pack", 1);
161 config->set_value("params", "mipmaps/generate", false);
162 config->set_value("params", "slices/horizontal", 1);
163 config->set_value("params", "slices/vertical", bake_img->get_meta("depth"));
164
165 config->save(p_path + ".import");
166
167 Error err = bake_img->save_exr(p_path, false);
168 ERR_FAIL_COND(err);
169 ResourceLoader::import(p_path);
170 Ref<Texture> t = ResourceLoader::load(p_path); //if already loaded, it will be updated on refocus?
171 ERR_FAIL_COND(t.is_null());
172
173 col_sdf->set_texture(t);
174 }
175 }
176
177 GPUParticlesCollisionSDF3DEditorPlugin::GPUParticlesCollisionSDF3DEditorPlugin() {
178 bake_hb = memnew(HBoxContainer);
179 bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
180 bake_hb->hide();
181 bake = memnew(Button);
182 bake->set_theme_type_variation(SceneStringName(FlatButton));
183 bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
184 bake->set_text(TTR("Bake SDF"));
185 bake->connect(SceneStringName(pressed), callable_mp(this, &GPUParticlesCollisionSDF3DEditorPlugin::_bake));
186 bake_hb->add_child(bake);
187
188 add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);
189 col_sdf = nullptr;
190 probe_file = memnew(EditorFileDialog);
191 probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
192 probe_file->add_filter("*.exr");
193 probe_file->connect("file_selected", callable_mp(this, &GPUParticlesCollisionSDF3DEditorPlugin::_sdf_save_path_and_bake));
194 EditorInterface::get_singleton()->get_base_control()->add_child(probe_file);
195 probe_file->set_title(TTR("Select path for SDF Texture"));
196
197 GPUParticlesCollisionSDF3D::bake_begin_function = bake_func_begin;
198 GPUParticlesCollisionSDF3D::bake_step_function = bake_func_step;
199 GPUParticlesCollisionSDF3D::bake_end_function = bake_func_end;
200 }