Path: blob/master/editor/scene/3d/lightmap_gi_editor_plugin.cpp
9903 views
/**************************************************************************/1/* lightmap_gi_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "lightmap_gi_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/gui/editor_file_dialog.h"3536#include "modules/modules_enabled.gen.h" // For lightmapper_rd.3738void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {39if (lightmap) {40LightmapGI::BakeError err = LightmapGI::BAKE_ERROR_OK;41const uint64_t time_started = OS::get_singleton()->get_ticks_msec();42if (get_tree()->get_edited_scene_root()) {43Ref<LightmapGIData> lightmapGIData = lightmap->get_light_data();4445if (lightmapGIData.is_valid()) {46String path = lightmapGIData->get_path();47if (!path.is_resource_file()) {48int srpos = path.find("::");49if (srpos != -1) {50String base = path.substr(0, srpos);51if (ResourceLoader::get_resource_type(base) == "PackedScene") {52if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {53err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;54}55} else {56if (FileAccess::exists(base + ".import")) {57err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;58}59}60}61} else {62if (FileAccess::exists(path + ".import")) {63err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;64}65}66}6768if (err == LightmapGI::BAKE_ERROR_OK) {69if (get_tree()->get_edited_scene_root() == lightmap) {70err = lightmap->bake(lightmap, p_file, bake_func_step);71} else {72err = lightmap->bake(lightmap->get_parent(), p_file, bake_func_step);73}74}75} else {76err = LightmapGI::BAKE_ERROR_NO_SCENE_ROOT;77}7879bake_func_end(time_started);8081switch (err) {82case LightmapGI::BAKE_ERROR_NO_SAVE_PATH: {83String scene_path = lightmap->get_scene_file_path();84if (scene_path.is_empty() && lightmap->get_owner()) {85scene_path = lightmap->get_owner()->get_scene_file_path();86}87if (scene_path.is_empty()) {88EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));89break;90}91scene_path = scene_path.get_basename() + ".lmbake";9293file_dialog->set_current_path(scene_path);94file_dialog->popup_file_dialog();95} break;96case LightmapGI::BAKE_ERROR_NO_MESHES: {97EditorNode::get_singleton()->show_warning(98TTR("No meshes with lightmapping support to bake. Make sure they contain UV2 data and their Global Illumination property is set to Static.") +99String::utf8("\n\n• ") + TTR("To import a scene with lightmapping support, set Meshes > Light Baking to Static Lightmaps in the Import dock.") +100String::utf8("\n• ") + TTR("To enable lightmapping support on a primitive mesh, edit the PrimitiveMesh resource in the inspector and check Add UV2.") +101String::utf8("\n• ") + TTR("To enable lightmapping support on a CSG mesh, select the root CSG node and choose CSG > Bake Mesh Instance at the top of the 3D editor viewport.\nSelect the generated MeshInstance3D node and choose Mesh > Unwrap UV2 for Lightmap/AO at the top of the 3D editor viewport."));102} break;103case LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE: {104EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure the lightmap destination path is writable."));105} break;106case LightmapGI::BAKE_ERROR_NO_SCENE_ROOT: {107EditorNode::get_singleton()->show_warning(TTR("No editor scene root found."));108} break;109case LightmapGI::BAKE_ERROR_FOREIGN_DATA: {110EditorNode::get_singleton()->show_warning(TTR("Lightmap data is not local to the scene."));111} break;112case LightmapGI::BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL: {113EditorNode::get_singleton()->show_warning(TTR("Maximum texture size is too small for the lightmap images.\nWhile this can be fixed by increasing the maximum texture size, it is recommended you split the scene into more objects instead."));114} break;115case LightmapGI::BAKE_ERROR_LIGHTMAP_TOO_SMALL: {116EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure all meshes to bake have the Lightmap Size Hint property set high enough, and the LightmapGI's Texel Scale value is not too low."));117} break;118case LightmapGI::BAKE_ERROR_ATLAS_TOO_SMALL: {119EditorNode::get_singleton()->show_warning(TTR("Failed fitting a lightmap image into an atlas. This should never happen and should be reported."));120} break;121default: {122} break;123}124}125}126127void LightmapGIEditorPlugin::_bake() {128_bake_select_file("");129}130131void LightmapGIEditorPlugin::edit(Object *p_object) {132LightmapGI *s = Object::cast_to<LightmapGI>(p_object);133if (!s) {134return;135}136137lightmap = s;138}139140bool LightmapGIEditorPlugin::handles(Object *p_object) const {141return p_object->is_class("LightmapGI");142}143144void LightmapGIEditorPlugin::make_visible(bool p_visible) {145if (p_visible) {146bake->show();147} else {148bake->hide();149}150}151152EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;153154bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {155if (!tmp_progress) {156tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, true));157ERR_FAIL_NULL_V(tmp_progress, false);158}159return tmp_progress->step(p_description, p_progress * 1000, p_refresh);160}161162void LightmapGIEditorPlugin::bake_func_end(uint64_t p_time_started) {163if (tmp_progress != nullptr) {164memdelete(tmp_progress);165tmp_progress = nullptr;166}167168const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;169print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));170// Request attention in case the user was doing something else.171// Baking lightmaps is likely the editor task that can take the most time,172// so only request the attention for baking lightmaps.173DisplayServer::get_singleton()->window_request_attention();174}175176void LightmapGIEditorPlugin::_bind_methods() {177ClassDB::bind_method("_bake", &LightmapGIEditorPlugin::_bake);178}179180LightmapGIEditorPlugin::LightmapGIEditorPlugin() {181bake = memnew(Button);182bake->set_theme_type_variation(SceneStringName(FlatButton));183// TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it184// when the editor theme updates.185bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));186bake->set_text(TTR("Bake Lightmaps"));187188#ifdef MODULE_LIGHTMAPPER_RD_ENABLED189// Disable lightmap baking if not supported on the current GPU.190if (!DisplayServer::get_singleton()->can_create_rendering_device()) {191bake->set_disabled(true);192bake->set_tooltip_text(vformat(TTR("Lightmap baking is not supported on this GPU (%s)."), RenderingServer::get_singleton()->get_video_adapter_name()));193}194#else195// Disable lightmap baking if the module is disabled at compile-time.196bake->set_disabled(true);197#if defined(ANDROID_ENABLED) || defined(APPLE_EMBEDDED_ENABLED)198bake->set_tooltip_text(vformat(TTR("Lightmaps cannot be baked on %s."), OS::get_singleton()->get_name()));199#else200bake->set_tooltip_text(TTR("Lightmaps cannot be baked, as the `lightmapper_rd` module was disabled at compile-time."));201#endif202#endif // MODULE_LIGHTMAPPER_RD_ENABLED203204bake->hide();205bake->connect(SceneStringName(pressed), Callable(this, "_bake"));206add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);207lightmap = nullptr;208209file_dialog = memnew(EditorFileDialog);210file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);211file_dialog->add_filter("*.lmbake", TTR("LightMap Bake"));212file_dialog->set_title(TTR("Select lightmap bake file:"));213file_dialog->connect("file_selected", callable_mp(this, &LightmapGIEditorPlugin::_bake_select_file));214bake->add_child(file_dialog);215}216217218