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