Skip to content

Commit 36fac3d

Browse files
committed
Merge pull request godotengine#75164 from Calinou/visualinstance3d-lightmap-scale-float
Allow more flexible adjustments of VisualInstance3D Lightmap Scale
2 parents 0cc1a21 + 1e5f0a8 commit 36fac3d

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

doc/classes/GeometryInstance3D.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@
3939
<member name="extra_cull_margin" type="float" setter="set_extra_cull_margin" getter="get_extra_cull_margin" default="0.0">
4040
The extra distance added to the GeometryInstance3D's bounding box ([AABB]) to increase its cull box.
4141
</member>
42-
<member name="gi_lightmap_scale" type="int" setter="set_lightmap_scale" getter="get_lightmap_scale" enum="GeometryInstance3D.LightmapScale" default="0">
42+
<member name="gi_lightmap_scale" type="int" setter="set_lightmap_scale" getter="get_lightmap_scale" enum="GeometryInstance3D.LightmapScale" default="0" deprecated="Use [member gi_lightmap_texel_scale] instead.">
43+
The texel density to use for lightmapping in [LightmapGI].
44+
</member>
45+
<member name="gi_lightmap_texel_scale" type="float" setter="set_lightmap_texel_scale" getter="get_lightmap_texel_scale" default="1.0">
4346
The texel density to use for lightmapping in [LightmapGI]. Greater scale values provide higher resolution in the lightmap, which can result in sharper shadows for lights that have both direct and indirect light baked. However, greater scale values will also increase the space taken by the mesh in the lightmap texture, which increases the memory, storage, and bake time requirements. When using a single mesh at different scales, consider adjusting this value to keep the lightmap texel density consistent across meshes.
47+
For example, doubling [member gi_lightmap_texel_scale] doubles the lightmap texture resolution for this object [i]on each axis[/i], so it will [i]quadruple[/i] the texel count.
4448
</member>
4549
<member name="gi_mode" type="int" setter="set_gi_mode" getter="get_gi_mode" enum="GeometryInstance3D.GIMode" default="1" keywords="global_illumination_mode, light_bake_mode">
4650
The global illumination mode to use for the whole geometry. To avoid inconsistent results, use a mode that matches the purpose of the mesh during gameplay (static/dynamic).
@@ -111,19 +115,19 @@
111115
<constant name="GI_MODE_DYNAMIC" value="2" enum="GIMode">
112116
Dynamic global illumination mode. Use for dynamic objects that contribute to global illumination. This GI mode is only effective when using [VoxelGI], but it has a higher performance impact than [constant GI_MODE_STATIC]. When using other GI methods, this will act the same as [constant GI_MODE_DISABLED]. When using [LightmapGI], the object will receive indirect lighting using lightmap probes instead of using the baked lightmap texture.
113117
</constant>
114-
<constant name="LIGHTMAP_SCALE_1X" value="0" enum="LightmapScale">
118+
<constant name="LIGHTMAP_SCALE_1X" value="0" enum="LightmapScale" deprecated="Use [member gi_lightmap_texel_scale] instead.">
115119
The standard texel density for lightmapping with [LightmapGI].
116120
</constant>
117-
<constant name="LIGHTMAP_SCALE_2X" value="1" enum="LightmapScale">
121+
<constant name="LIGHTMAP_SCALE_2X" value="1" enum="LightmapScale" deprecated="Use [member gi_lightmap_texel_scale] instead.">
118122
Multiplies texel density by 2× for lightmapping with [LightmapGI]. To ensure consistency in texel density, use this when scaling a mesh by a factor between 1.5 and 3.0.
119123
</constant>
120-
<constant name="LIGHTMAP_SCALE_4X" value="2" enum="LightmapScale">
124+
<constant name="LIGHTMAP_SCALE_4X" value="2" enum="LightmapScale" deprecated="Use [member gi_lightmap_texel_scale] instead.">
121125
Multiplies texel density by 4× for lightmapping with [LightmapGI]. To ensure consistency in texel density, use this when scaling a mesh by a factor between 3.0 and 6.0.
122126
</constant>
123-
<constant name="LIGHTMAP_SCALE_8X" value="3" enum="LightmapScale">
127+
<constant name="LIGHTMAP_SCALE_8X" value="3" enum="LightmapScale" deprecated="Use [member gi_lightmap_texel_scale] instead.">
124128
Multiplies texel density by 8× for lightmapping with [LightmapGI]. To ensure consistency in texel density, use this when scaling a mesh by a factor greater than 6.0.
125129
</constant>
126-
<constant name="LIGHTMAP_SCALE_MAX" value="4" enum="LightmapScale">
130+
<constant name="LIGHTMAP_SCALE_MAX" value="4" enum="LightmapScale" deprecated="Use [member gi_lightmap_texel_scale] instead.">
127131
Represents the size of the [enum LightmapScale] enum.
128132
</constant>
129133
<constant name="VISIBILITY_RANGE_FADE_DISABLED" value="0" enum="VisibilityRangeFadeMode">

doc/classes/LightmapGI.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
</member>
7171
<member name="texel_scale" type="float" setter="set_texel_scale" getter="get_texel_scale" default="1.0">
7272
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.
73+
For example, doubling [member texel_scale] doubles the lightmap texture resolution for all objects [i]on each axis[/i], so it will [i]quadruple[/i] the texel count.
7374
</member>
7475
<member name="use_denoiser" type="bool" setter="set_use_denoiser" getter="is_using_denoiser" default="true">
7576
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.

scene/3d/lightmap_gi.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,7 @@ void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &m
333333
mf.node_path = get_path_to(mi);
334334
mf.subindex = -1;
335335
mf.mesh = mesh;
336-
337-
static const int lightmap_scale[GeometryInstance3D::LIGHTMAP_SCALE_MAX] = { 1, 2, 4, 8 };
338-
mf.lightmap_scale = lightmap_scale[mi->get_lightmap_scale()];
336+
mf.lightmap_scale = mi->get_lightmap_texel_scale();
339337

340338
Ref<Material> all_override = mi->get_material_override();
341339
for (int i = 0; i < mesh->get_surface_count(); i++) {
@@ -369,7 +367,7 @@ void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &m
369367
mf.xform = xf * mesh_xf;
370368
mf.node_path = get_path_to(s);
371369
mf.subindex = i / 2;
372-
mf.lightmap_scale = 1;
370+
mf.lightmap_scale = 1.0;
373371
mf.mesh = mesh;
374372

375373
meshes.push_back(mf);

scene/3d/lightmap_gi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class LightmapGI : public VisualInstance3D {
194194
NodePath node_path;
195195
int32_t subindex = 0;
196196
Ref<Mesh> mesh;
197-
int32_t lightmap_scale = 0;
197+
float lightmap_scale = 0.0;
198198
Vector<Ref<Material>> overrides;
199199
};
200200

scene/3d/visual_instance_3d.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,48 @@ AABB GeometryInstance3D::get_custom_aabb() const {
454454
return custom_aabb;
455455
}
456456

457+
void GeometryInstance3D::set_lightmap_texel_scale(float p_scale) {
458+
lightmap_texel_scale = p_scale;
459+
}
460+
461+
float GeometryInstance3D::get_lightmap_texel_scale() const {
462+
return lightmap_texel_scale;
463+
}
464+
465+
#ifndef DISABLE_DEPRECATED
457466
void GeometryInstance3D::set_lightmap_scale(LightmapScale p_scale) {
458467
ERR_FAIL_INDEX(p_scale, LIGHTMAP_SCALE_MAX);
459-
lightmap_scale = p_scale;
468+
switch (p_scale) {
469+
case GeometryInstance3D::LIGHTMAP_SCALE_1X:
470+
lightmap_texel_scale = 1.0f;
471+
break;
472+
case GeometryInstance3D::LIGHTMAP_SCALE_2X:
473+
lightmap_texel_scale = 2.0f;
474+
break;
475+
case GeometryInstance3D::LIGHTMAP_SCALE_4X:
476+
lightmap_texel_scale = 4.0f;
477+
break;
478+
case GeometryInstance3D::LIGHTMAP_SCALE_8X:
479+
lightmap_texel_scale = 8.0f;
480+
break;
481+
case GeometryInstance3D::LIGHTMAP_SCALE_MAX:
482+
break; // Can't happen, but silences warning.
483+
}
460484
}
461485

462486
GeometryInstance3D::LightmapScale GeometryInstance3D::get_lightmap_scale() const {
463-
return lightmap_scale;
487+
// Return closest approximation.
488+
if (lightmap_texel_scale < 1.5f) {
489+
return GeometryInstance3D::LIGHTMAP_SCALE_1X;
490+
} else if (lightmap_texel_scale < 3.0f) {
491+
return GeometryInstance3D::LIGHTMAP_SCALE_2X;
492+
} else if (lightmap_texel_scale < 6.0f) {
493+
return GeometryInstance3D::LIGHTMAP_SCALE_4X;
494+
}
495+
496+
return GeometryInstance3D::LIGHTMAP_SCALE_8X;
464497
}
498+
#endif // DISABLE_DEPRECATED
465499

466500
void GeometryInstance3D::set_gi_mode(GIMode p_mode) {
467501
switch (p_mode) {
@@ -565,8 +599,13 @@ void GeometryInstance3D::_bind_methods() {
565599
ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance3D::set_extra_cull_margin);
566600
ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance3D::get_extra_cull_margin);
567601

602+
ClassDB::bind_method(D_METHOD("set_lightmap_texel_scale", "scale"), &GeometryInstance3D::set_lightmap_texel_scale);
603+
ClassDB::bind_method(D_METHOD("get_lightmap_texel_scale"), &GeometryInstance3D::get_lightmap_texel_scale);
604+
605+
#ifndef DISABLE_DEPRECATED
568606
ClassDB::bind_method(D_METHOD("set_lightmap_scale", "scale"), &GeometryInstance3D::set_lightmap_scale);
569607
ClassDB::bind_method(D_METHOD("get_lightmap_scale"), &GeometryInstance3D::get_lightmap_scale);
608+
#endif // DISABLE_DEPRECATED
570609

571610
ClassDB::bind_method(D_METHOD("set_gi_mode", "mode"), &GeometryInstance3D::set_gi_mode);
572611
ClassDB::bind_method(D_METHOD("get_gi_mode"), &GeometryInstance3D::get_gi_mode);
@@ -591,7 +630,10 @@ void GeometryInstance3D::_bind_methods() {
591630

592631
ADD_GROUP("Global Illumination", "gi_");
593632
ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_mode", PROPERTY_HINT_ENUM, "Disabled,Static,Dynamic"), "set_gi_mode", "get_gi_mode");
594-
ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_lightmap_scale", PROPERTY_HINT_ENUM, String::utf8("1×,2×,4×,8×")), "set_lightmap_scale", "get_lightmap_scale");
633+
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gi_lightmap_texel_scale", PROPERTY_HINT_RANGE, "0.01,10,0.0001,or_greater"), "set_lightmap_texel_scale", "get_lightmap_texel_scale");
634+
#ifndef DISABLE_DEPRECATED
635+
ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_lightmap_scale", PROPERTY_HINT_ENUM, String::utf8("1×,2×,4×,8×"), PROPERTY_USAGE_NONE), "set_lightmap_scale", "get_lightmap_scale");
636+
#endif // DISABLE_DEPRECATED
595637

596638
ADD_GROUP("Visibility Range", "visibility_range_");
597639
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_begin", "get_visibility_range_begin");

scene/3d/visual_instance_3d.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class GeometryInstance3D : public VisualInstance3D {
134134

135135
float extra_cull_margin = 0.0;
136136
AABB custom_aabb;
137-
LightmapScale lightmap_scale = LIGHTMAP_SCALE_1X;
137+
float lightmap_texel_scale = 1.0f;
138138
GIMode gi_mode = GI_MODE_STATIC;
139139
bool ignore_occlusion_culling = false;
140140

@@ -185,8 +185,13 @@ class GeometryInstance3D : public VisualInstance3D {
185185
void set_gi_mode(GIMode p_mode);
186186
GIMode get_gi_mode() const;
187187

188-
void set_lightmap_scale(LightmapScale p_scale);
188+
void set_lightmap_texel_scale(float p_scale);
189+
float get_lightmap_texel_scale() const;
190+
191+
#ifndef DISABLE_DEPRECATED
192+
void set_lightmap_scale(GeometryInstance3D::LightmapScale p_scale);
189193
LightmapScale get_lightmap_scale() const;
194+
#endif // DISABLE_DEPRECATED
190195

191196
void set_instance_shader_parameter(const StringName &p_name, const Variant &p_value);
192197
Variant get_instance_shader_parameter(const StringName &p_name) const;
@@ -203,8 +208,8 @@ class GeometryInstance3D : public VisualInstance3D {
203208
};
204209

205210
VARIANT_ENUM_CAST(GeometryInstance3D::ShadowCastingSetting);
206-
VARIANT_ENUM_CAST(GeometryInstance3D::LightmapScale);
207211
VARIANT_ENUM_CAST(GeometryInstance3D::GIMode);
212+
VARIANT_ENUM_CAST(GeometryInstance3D::LightmapScale);
208213
VARIANT_ENUM_CAST(GeometryInstance3D::VisibilityRangeFadeMode);
209214

210215
#endif // VISUAL_INSTANCE_3D_H

0 commit comments

Comments
 (0)