Merge pull request #52586 from JFonS/lm_bake_end

Fix editor getting stuck after baking lightmaps from a script
This commit is contained in:
Rémi Verschelde 2021-09-14 22:40:48 +02:00 committed by GitHub
commit 20f14e0a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 4 deletions

View File

@ -33,15 +33,12 @@
void BakedLightmapEditorPlugin::_bake_select_file(const String &p_file) { void BakedLightmapEditorPlugin::_bake_select_file(const String &p_file) {
if (lightmap) { if (lightmap) {
BakedLightmap::BakeError err; BakedLightmap::BakeError err;
uint32_t time_started = OS::get_singleton()->get_ticks_msec();
if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == lightmap) { if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == lightmap) {
err = lightmap->bake(lightmap, p_file); err = lightmap->bake(lightmap, p_file);
} else { } else {
err = lightmap->bake(lightmap->get_parent(), p_file); err = lightmap->bake(lightmap->get_parent(), p_file);
} }
bake_func_end(time_started);
switch (err) { switch (err) {
case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH: { case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH: {
String scene_path = lightmap->get_filename(); String scene_path = lightmap->get_filename();
@ -173,6 +170,7 @@ BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
BakedLightmap::bake_step_function = bake_func_step; BakedLightmap::bake_step_function = bake_func_step;
BakedLightmap::bake_substep_function = bake_func_substep; BakedLightmap::bake_substep_function = bake_func_substep;
BakedLightmap::bake_end_function = bake_func_end;
} }
BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() { BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {

View File

@ -257,6 +257,7 @@ BakedLightmapData::~BakedLightmapData() {
Lightmapper::BakeStepFunc BakedLightmap::bake_step_function; Lightmapper::BakeStepFunc BakedLightmap::bake_step_function;
Lightmapper::BakeStepFunc BakedLightmap::bake_substep_function; Lightmapper::BakeStepFunc BakedLightmap::bake_substep_function;
Lightmapper::BakeEndFunc BakedLightmap::bake_end_function;
Size2i BakedLightmap::_compute_lightmap_size(const MeshesFound &p_mesh) { Size2i BakedLightmap::_compute_lightmap_size(const MeshesFound &p_mesh) {
double area = 0; double area = 0;
@ -606,15 +607,21 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
} }
} }
uint32_t time_started = OS::get_singleton()->get_ticks_msec();
if (bake_step_function) { if (bake_step_function) {
bool cancelled = bake_step_function(0.0, TTR("Finding meshes and lights"), nullptr, true); bool cancelled = bake_step_function(0.0, TTR("Finding meshes and lights"), nullptr, true);
if (cancelled) { if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
} }
} }
Ref<Lightmapper> lightmapper = Lightmapper::create(); Ref<Lightmapper> lightmapper = Lightmapper::create();
ERR_FAIL_COND_V(lightmapper.is_null(), BAKE_ERROR_NO_LIGHTMAPPER); if (lightmapper.is_null()) {
bake_end_function(time_started);
return BAKE_ERROR_NO_LIGHTMAPPER;
}
Vector<LightsFound> lights_found; Vector<LightsFound> lights_found;
Vector<MeshesFound> meshes_found; Vector<MeshesFound> meshes_found;
@ -622,6 +629,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
_find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), meshes_found, lights_found); _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), meshes_found, lights_found);
if (meshes_found.size() == 0) { if (meshes_found.size() == 0) {
bake_end_function(time_started);
return BAKE_ERROR_NO_MESHES; return BAKE_ERROR_NO_MESHES;
} }
@ -630,6 +638,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
float p = (float)(m_i) / meshes_found.size(); float p = (float)(m_i) / meshes_found.size();
bool cancelled = bake_step_function(p * 0.05, vformat(TTR("Preparing geometry (%d/%d)"), m_i + 1, meshes_found.size()), nullptr, false); bool cancelled = bake_step_function(p * 0.05, vformat(TTR("Preparing geometry (%d/%d)"), m_i + 1, meshes_found.size()), nullptr, false);
if (cancelled) { if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
} }
} }
@ -818,6 +827,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
Lightmapper::BakeError bake_err = lightmapper->bake(Lightmapper::BakeQuality(bake_quality), use_denoiser, bounces, bounce_indirect_energy, bias, gen_atlas, max_atlas_size, environment_image, environment_xform, _lightmap_bake_step_function, &bsud, bake_substep_function); Lightmapper::BakeError bake_err = lightmapper->bake(Lightmapper::BakeQuality(bake_quality), use_denoiser, bounces, bounce_indirect_energy, bias, gen_atlas, max_atlas_size, environment_image, environment_xform, _lightmap_bake_step_function, &bsud, bake_substep_function);
if (bake_err != Lightmapper::BAKE_OK) { if (bake_err != Lightmapper::BAKE_OK) {
bake_end_function(time_started);
switch (bake_err) { switch (bake_err) {
case Lightmapper::BAKE_ERROR_USER_ABORTED: { case Lightmapper::BAKE_ERROR_USER_ABORTED: {
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
@ -847,6 +857,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
if (bake_step_function) { if (bake_step_function) {
bool cancelled = bake_step_function(0.85, TTR("Generating capture"), nullptr, true); bool cancelled = bake_step_function(0.85, TTR("Generating capture"), nullptr, true);
if (cancelled) { if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
} }
} }
@ -926,6 +937,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
if (bake_step_function) { if (bake_step_function) {
bool cancelled = bake_step_function(0.9, TTR("Saving lightmaps"), nullptr, true); bool cancelled = bake_step_function(0.9, TTR("Saving lightmaps"), nullptr, true);
if (cancelled) { if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
} }
} }
@ -1081,6 +1093,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
if (bake_step_function) { if (bake_step_function) {
bool cancelled = bake_step_function(1.0, TTR("Done"), nullptr, true); bool cancelled = bake_step_function(1.0, TTR("Done"), nullptr, true);
if (cancelled) { if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_USER_ABORTED;
} }
} }
@ -1089,10 +1102,12 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
data->set_path(p_data_save_path); data->set_path(p_data_save_path);
if (err != OK) { if (err != OK) {
bake_end_function(time_started);
return BAKE_ERROR_CANT_CREATE_IMAGE; return BAKE_ERROR_CANT_CREATE_IMAGE;
} }
set_light_data(data); set_light_data(data);
bake_end_function(time_started);
return BAKE_ERROR_OK; return BAKE_ERROR_OK;
} }

View File

@ -203,6 +203,7 @@ protected:
public: public:
static Lightmapper::BakeStepFunc bake_step_function; static Lightmapper::BakeStepFunc bake_step_function;
static Lightmapper::BakeStepFunc bake_substep_function; static Lightmapper::BakeStepFunc bake_substep_function;
static Lightmapper::BakeEndFunc bake_end_function;
void set_light_data(const Ref<BakedLightmapData> &p_data); void set_light_data(const Ref<BakedLightmapData> &p_data);
Ref<BakedLightmapData> get_light_data() const; Ref<BakedLightmapData> get_light_data() const;

View File

@ -154,6 +154,7 @@ public:
protected: protected:
public: public:
typedef bool (*BakeStepFunc)(float, const String &, void *, bool); //progress, step description, userdata, force refresh typedef bool (*BakeStepFunc)(float, const String &, void *, bool); //progress, step description, userdata, force refresh
typedef void (*BakeEndFunc)(uint32_t); // time_started
struct MeshData { struct MeshData {
struct TextureDef { struct TextureDef {