summaryrefslogtreecommitdiffstats
path: root/modules/lightmapper_rd/lightmapper_rd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lightmapper_rd/lightmapper_rd.cpp')
-rw-r--r--modules/lightmapper_rd/lightmapper_rd.cpp16
1 files changed, 12 insertions, 4 deletions
diff --git a/modules/lightmapper_rd/lightmapper_rd.cpp b/modules/lightmapper_rd/lightmapper_rd.cpp
index 7ac7bd8088..33b0b0d015 100644
--- a/modules/lightmapper_rd/lightmapper_rd.cpp
+++ b/modules/lightmapper_rd/lightmapper_rd.cpp
@@ -233,14 +233,14 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
MeshInstance &mi = mesh_instances.write[m_i];
Size2i s = Size2i(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height());
sizes.push_back(s);
- atlas_size = atlas_size.max(s + Size2i(2, 2));
+ atlas_size = atlas_size.max(s + Size2i(2, 2).maxi(p_denoiser_range));
}
int max = nearest_power_of_2_templated(atlas_size.width);
max = MAX(max, nearest_power_of_2_templated(atlas_size.height));
if (max > p_max_texture_size) {
- return BAKE_ERROR_LIGHTMAP_TOO_SMALL;
+ return BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE;
}
if (p_step_function) {
@@ -254,19 +254,27 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
int best_atlas_memory = 0x7FFFFFFF;
Vector<Vector3i> best_atlas_offsets;
- //determine best texture array atlas size by bruteforce fitting
+ // Determine best texture array atlas size by bruteforce fitting.
while (atlas_size.x <= p_max_texture_size && atlas_size.y <= p_max_texture_size) {
Vector<Vector2i> source_sizes;
Vector<int> source_indices;
source_sizes.resize(sizes.size());
source_indices.resize(sizes.size());
for (int i = 0; i < source_indices.size(); i++) {
- source_sizes.write[i] = sizes[i] + Vector2i(2, 2).maxi(p_denoiser_range); // Add padding between lightmaps
+ source_sizes.write[i] = sizes[i] + Vector2i(2, 2).maxi(p_denoiser_range); // Add padding between lightmaps.
source_indices.write[i] = i;
}
Vector<Vector3i> atlas_offsets;
atlas_offsets.resize(source_sizes.size());
+ // Ensure the sizes can all fit into a single atlas layer.
+ // This should always happen, and this check is only in place to prevent an infinite loop.
+ for (int i = 0; i < source_sizes.size(); i++) {
+ if (source_sizes[i] > atlas_size) {
+ return BAKE_ERROR_ATLAS_TOO_SMALL;
+ }
+ }
+
int slices = 0;
while (source_sizes.size() > 0) {