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) {
if (lightmap) {
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) {
err = lightmap->bake(lightmap, p_file);
} else {
err = lightmap->bake(lightmap->get_parent(), p_file);
}
bake_func_end(time_started);
switch (err) {
case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH: {
String scene_path = lightmap->get_filename();
@ -173,6 +170,7 @@ BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
BakedLightmap::bake_step_function = bake_func_step;
BakedLightmap::bake_substep_function = bake_func_substep;
BakedLightmap::bake_end_function = bake_func_end;
}
BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {

View file

@ -257,6 +257,7 @@ BakedLightmapData::~BakedLightmapData() {
Lightmapper::BakeStepFunc BakedLightmap::bake_step_function;
Lightmapper::BakeStepFunc BakedLightmap::bake_substep_function;
Lightmapper::BakeEndFunc BakedLightmap::bake_end_function;
Size2i BakedLightmap::_compute_lightmap_size(const MeshesFound &p_mesh) {
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) {
bool cancelled = bake_step_function(0.0, TTR("Finding meshes and lights"), nullptr, true);
if (cancelled) {
bake_end_function(time_started);
return BAKE_ERROR_USER_ABORTED;
}
}
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<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);
if (meshes_found.size() == 0) {
bake_end_function(time_started);
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();
bool cancelled = bake_step_function(p * 0.05, vformat(TTR("Preparing geometry (%d/%d)"), m_i + 1, meshes_found.size()), nullptr, false);
if (cancelled) {
bake_end_function(time_started);
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);
if (bake_err != Lightmapper::BAKE_OK) {
bake_end_function(time_started);
switch (bake_err) {
case Lightmapper::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) {
bool cancelled = bake_step_function(0.85, TTR("Generating capture"), nullptr, true);
if (cancelled) {
bake_end_function(time_started);
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) {
bool cancelled = bake_step_function(0.9, TTR("Saving lightmaps"), nullptr, true);
if (cancelled) {
bake_end_function(time_started);
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) {
bool cancelled = bake_step_function(1.0, TTR("Done"), nullptr, true);
if (cancelled) {
bake_end_function(time_started);
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);
if (err != OK) {
bake_end_function(time_started);
return BAKE_ERROR_CANT_CREATE_IMAGE;
}
set_light_data(data);
bake_end_function(time_started);
return BAKE_ERROR_OK;
}

View file

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

View file

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