Path: blob/master/editor/scene/3d/voxel_gi_editor_plugin.cpp
9913 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 "editor/editor_interface.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/gui/editor_file_dialog.h"3637void VoxelGIEditorPlugin::_bake() {38if (voxel_gi) {39Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data();4041if (voxel_gi_data.is_null()) {42String path = get_tree()->get_edited_scene_root()->get_scene_file_path();43if (path.is_empty()) {44path = "res://" + voxel_gi->get_name() + "_data.res";45} else {46String ext = path.get_extension();47path = path.get_basename() + "." + voxel_gi->get_name() + "_data.res";48}49probe_file->set_current_path(path);50probe_file->popup_file_dialog();51return;52} else {53String path = voxel_gi_data->get_path();54if (!path.is_resource_file()) {55int srpos = path.find("::");56if (srpos != -1) {57String base = path.substr(0, srpos);58if (ResourceLoader::get_resource_type(base) == "PackedScene") {59if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {60EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is not local to the scene."));61return;62}63} else {64if (FileAccess::exists(base + ".import")) {65EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is part of an imported resource."));66return;67}68}69}70} else {71if (FileAccess::exists(path + ".import")) {72EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is an imported resource."));73return;74}75}76}7778voxel_gi->bake();79}80}8182void VoxelGIEditorPlugin::edit(Object *p_object) {83VoxelGI *s = Object::cast_to<VoxelGI>(p_object);84if (!s) {85return;86}8788voxel_gi = s;89}9091bool VoxelGIEditorPlugin::handles(Object *p_object) const {92return p_object->is_class("VoxelGI");93}9495void VoxelGIEditorPlugin::_notification(int p_what) {96switch (p_what) {97case NOTIFICATION_PROCESS: {98if (!voxel_gi) {99return;100}101102// Set information tooltip on the Bake button. This information is useful103// to optimize performance (video RAM size) and reduce light leaking (individual cell size).104105const Vector3i cell_size = voxel_gi->get_estimated_cell_size();106107const Vector3 half_size = voxel_gi->get_size() / 2;108109const int data_size = 4;110const double size_mb = cell_size.x * cell_size.y * cell_size.z * data_size / (1024.0 * 1024.0);111// Add a qualitative measurement to help the user assess whether a VoxelGI node is using a lot of VRAM.112String size_quality;113if (size_mb < 16.0) {114size_quality = TTR("Low");115} else if (size_mb < 64.0) {116size_quality = TTR("Moderate");117} else {118size_quality = TTR("High");119}120121String text;122text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", cell_size.x, cell_size.y, cell_size.z)) + "\n";123text += 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";124text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality);125126// Only update the tooltip when needed to avoid constant redrawing.127if (bake->get_tooltip(Point2()) == text) {128return;129}130131bake->set_tooltip_text(text);132} break;133}134}135136void VoxelGIEditorPlugin::make_visible(bool p_visible) {137if (p_visible) {138bake_hb->show();139set_process(true);140} else {141bake_hb->hide();142set_process(false);143}144}145146EditorProgress *VoxelGIEditorPlugin::tmp_progress = nullptr;147148void VoxelGIEditorPlugin::bake_func_begin() {149ERR_FAIL_COND(tmp_progress != nullptr);150151tmp_progress = memnew(EditorProgress("bake_gi", TTR("Bake VoxelGI"), 1000, true));152}153154bool VoxelGIEditorPlugin::bake_func_step(int p_progress, const String &p_description) {155ERR_FAIL_NULL_V(tmp_progress, false);156return tmp_progress->step(p_description, p_progress, false);157}158159void VoxelGIEditorPlugin::bake_func_end() {160ERR_FAIL_NULL(tmp_progress);161memdelete(tmp_progress);162tmp_progress = nullptr;163}164165void VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake(const String &p_path) {166probe_file->hide();167if (voxel_gi) {168voxel_gi->bake();169// Ensure the VoxelGIData is always saved to an external resource.170// This avoids bloating the scene file with large binary data,171// which would be serialized as Base64 if the scene is a `.tscn` file.172Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data();173ERR_FAIL_COND(voxel_gi_data.is_null());174voxel_gi_data->set_path(p_path);175ResourceSaver::save(voxel_gi_data, p_path, ResourceSaver::FLAG_CHANGE_PATH);176}177}178179VoxelGIEditorPlugin::VoxelGIEditorPlugin() {180bake_hb = memnew(HBoxContainer);181bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);182bake_hb->hide();183bake = memnew(Button);184bake->set_theme_type_variation(SceneStringName(FlatButton));185// TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it186// when the editor theme updates.187bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));188bake->set_text(TTR("Bake VoxelGI"));189bake->connect(SceneStringName(pressed), callable_mp(this, &VoxelGIEditorPlugin::_bake));190bake_hb->add_child(bake);191192add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);193voxel_gi = nullptr;194probe_file = memnew(EditorFileDialog);195probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);196probe_file->add_filter("*.res");197probe_file->connect("file_selected", callable_mp(this, &VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake));198EditorInterface::get_singleton()->get_base_control()->add_child(probe_file);199probe_file->set_title(TTR("Select path for VoxelGI Data File"));200201VoxelGI::bake_begin_function = bake_func_begin;202VoxelGI::bake_step_function = bake_func_step;203VoxelGI::bake_end_function = bake_func_end;204}205206207