Skip to content

Commit eea2ad4

Browse files
committed
Add texel_scale property to LightmapGI
1 parent dbd139c commit eea2ad4

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

doc/classes/LightmapGI.xml

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
The quality preset to use when baking lightmaps. This affects bake times, but output file sizes remain mostly identical across quality levels.
6666
To further speed up bake times, decrease [member bounces], disable [member use_denoiser] and increase the lightmap texel size on 3D scenes in the Import doc.
6767
</member>
68+
<member name="texel_scale" type="float" setter="set_texel_scale" getter="get_texel_scale" default="1.0">
69+
Scales the lightmap texel density of all meshes for the current bake. This is a multiplier that builds upon the existing lightmap texel size defined in each imported 3D scene, along with the per-mesh density multiplier (which is designed to be used when the same mesh is used at different scales). Lower values will result in faster bake times.
70+
</member>
6871
<member name="use_denoiser" type="bool" setter="set_use_denoiser" getter="is_using_denoiser" default="true">
6972
If [code]true[/code], uses a GPU-based denoising algorithm on the generated lightmap. This eliminates most noise within the generated lightmap at the cost of longer bake times. File sizes are generally not impacted significantly by the use of a denoiser, although lossless compression may do a better job at compressing a denoised image.
7073
</member>

editor/plugins/lightmap_gi_editor_plugin.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {
107107
case LightmapGI::BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL: {
108108
EditorNode::get_singleton()->show_warning(TTR("Maximum texture size is too small for the lightmap images."));
109109
} break;
110+
case LightmapGI::BAKE_ERROR_LIGHTMAP_TOO_SMALL: {
111+
EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure all meshes selected to bake have `lightmap_size_hint` value set high enough, and `texel_scale` value of LightmapGI is not too low."));
112+
} break;
110113
default: {
111114
} break;
112115
}

scene/3d/lightmap_gi.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -734,15 +734,15 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa
734734

735735
MeshesFound &mf = meshes_found.write[m_i];
736736

737-
Size2i lightmap_size = mf.mesh->get_lightmap_size_hint();
738-
739-
if (lightmap_size == Size2i(0, 0)) {
737+
Size2i mesh_lightmap_size = mf.mesh->get_lightmap_size_hint();
738+
if (mesh_lightmap_size == Size2i(0, 0)) {
740739
// TODO we should compute a size if no lightmap hint is set, as we did in 3.x.
741740
// For now set to basic size to avoid crash.
742-
lightmap_size = Size2i(64, 64);
741+
mesh_lightmap_size = Size2i(64, 64);
743742
}
743+
Size2i lightmap_size = Size2i(Size2(mesh_lightmap_size) * mf.lightmap_scale * texel_scale);
744+
ERR_FAIL_COND_V(lightmap_size.x == 0 || lightmap_size.y == 0, BAKE_ERROR_LIGHTMAP_TOO_SMALL);
744745

745-
lightmap_size *= mf.lightmap_scale;
746746
TypedArray<RID> overrides;
747747
overrides.resize(mf.overrides.size());
748748
for (int i = 0; i < mf.overrides.size(); i++) {
@@ -1493,6 +1493,15 @@ float LightmapGI::get_bias() const {
14931493
return bias;
14941494
}
14951495

1496+
void LightmapGI::set_texel_scale(float p_multiplier) {
1497+
ERR_FAIL_COND(p_multiplier < (0.01 - CMP_EPSILON));
1498+
texel_scale = p_multiplier;
1499+
}
1500+
1501+
float LightmapGI::get_texel_scale() const {
1502+
return texel_scale;
1503+
}
1504+
14961505
void LightmapGI::set_max_texture_size(int p_size) {
14971506
ERR_FAIL_COND_MSG(p_size < 2048, vformat("The LightmapGI maximum texture size supplied (%d) is too small. The minimum allowed value is 2048.", p_size));
14981507
ERR_FAIL_COND_MSG(p_size > 16384, vformat("The LightmapGI maximum texture size supplied (%d) is too large. The maximum allowed value is 16384.", p_size));
@@ -1576,6 +1585,9 @@ void LightmapGI::_bind_methods() {
15761585
ClassDB::bind_method(D_METHOD("set_environment_custom_energy", "energy"), &LightmapGI::set_environment_custom_energy);
15771586
ClassDB::bind_method(D_METHOD("get_environment_custom_energy"), &LightmapGI::get_environment_custom_energy);
15781587

1588+
ClassDB::bind_method(D_METHOD("set_texel_scale", "texel_scale"), &LightmapGI::set_texel_scale);
1589+
ClassDB::bind_method(D_METHOD("get_texel_scale"), &LightmapGI::get_texel_scale);
1590+
15791591
ClassDB::bind_method(D_METHOD("set_max_texture_size", "max_texture_size"), &LightmapGI::set_max_texture_size);
15801592
ClassDB::bind_method(D_METHOD("get_max_texture_size"), &LightmapGI::get_max_texture_size);
15811593

@@ -1609,6 +1621,7 @@ void LightmapGI::_bind_methods() {
16091621
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_denoiser"), "set_use_denoiser", "is_using_denoiser");
16101622
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "denoiser_strength", PROPERTY_HINT_RANGE, "0.001,0.2,0.001,or_greater"), "set_denoiser_strength", "get_denoiser_strength");
16111623
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0.00001,0.1,0.00001,or_greater"), "set_bias", "get_bias");
1624+
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texel_scale", PROPERTY_HINT_RANGE, "0.01,100.0,0.01"), "set_texel_scale", "get_texel_scale");
16121625
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_texture_size", PROPERTY_HINT_RANGE, "2048,16384,1"), "set_max_texture_size", "get_max_texture_size");
16131626
ADD_GROUP("Environment", "environment_");
16141627
ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_mode", PROPERTY_HINT_ENUM, "Disabled,Scene,Custom Sky,Custom Color"), "set_environment_mode", "get_environment_mode");

scene/3d/lightmap_gi.h

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class LightmapGI : public VisualInstance3D {
142142
BAKE_ERROR_CANT_CREATE_IMAGE,
143143
BAKE_ERROR_USER_ABORTED,
144144
BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL,
145+
BAKE_ERROR_LIGHTMAP_TOO_SMALL,
145146
};
146147

147148
enum EnvironmentMode {
@@ -158,6 +159,7 @@ class LightmapGI : public VisualInstance3D {
158159
int bounces = 3;
159160
float bounce_indirect_energy = 1.0;
160161
float bias = 0.0005;
162+
float texel_scale = 1.0;
161163
int max_texture_size = 16384;
162164
bool interior = false;
163165
EnvironmentMode environment_mode = ENVIRONMENT_MODE_SCENE;
@@ -284,6 +286,9 @@ class LightmapGI : public VisualInstance3D {
284286
void set_bias(float p_bias);
285287
float get_bias() const;
286288

289+
void set_texel_scale(float p_multiplier);
290+
float get_texel_scale() const;
291+
287292
void set_max_texture_size(int p_size);
288293
int get_max_texture_size() const;
289294

0 commit comments

Comments
 (0)