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