Godot Engine – Multi-platform 2D and 3D game engine
Add directional lightmap specular light
Juan Manuel Costello77b89bf08b73
diff --git a/doc/classes/LightmapGI.xml b/doc/classes/LightmapGI.xmlindex 0f45f80b0df..763e1e4f7e1 100644--- a/doc/classes/LightmapGI.xml+++ b/doc/classes/LightmapGI.xml@@ -75,6 +75,10 @@Shadowmasking allows [DirectionalLight3D] nodes to cast shadows even outside the range defined by their [member DirectionalLight3D.directional_shadow_max_distance] property. This is done by baking a texture that contains a shadowmap for the directional light, then using this texture according to the current shadowmask mode.[b]Note:[/b] The shadowmask texture is only created if [member shadowmask_mode] is not [constant LightmapGIData.SHADOWMASK_MODE_NONE]. To see a difference, you need to bake lightmaps again after switching from [constant LightmapGIData.SHADOWMASK_MODE_NONE] to any other mode.</member>+ <member name="specular_intensity" type="float" setter="set_specular_intensity" getter="get_specular_intensity" default="0.0">+ The strength of the lightmap's approximated specular lobe. This helps make lightmapped scenes look closer to real-time lighting, which is particularly noticeable for lights that use the [constant Light3D.BAKE_STATIC] bake mode. Only effective if [member directional] is [code]true[/code].+ [b]Note:[/b] The lightmap is only able to represent one light direction per texel, which means that the specular direction may appear to be incorrect in certain cases. This can manifest as specular lobes appearing to warp around as the camera moves.+ </member><member name="supersampling" type="bool" setter="set_supersampling_enabled" getter="is_supersampling_enabled" default="false">If [code]true[/code], lightmaps are baked with the texel scale multiplied with [member supersampling_factor] and downsampled before saving the lightmap (so the effective texel density is identical to having supersampling disabled).Supersampling provides increased lightmap quality with less noise, smoother shadows and better shadowing of small-scale features in objects. However, it may result in significantly increased bake times and memory usage while baking lightmaps. Padding is automatically adjusted to avoid increasing light leaking.diff --git a/drivers/gles3/effects/copy_effects.cpp b/drivers/gles3/effects/copy_effects.cppindex 2b4f03f4edb..398c4f374ec 100644--- a/drivers/gles3/effects/copy_effects.cpp+++ b/drivers/gles3/effects/copy_effects.cpp@@ -117,7 +117,7 @@ CopyEffects::~CopyEffects() {}void CopyEffects::copy_to_rect(const Rect2 &p_rect, bool p_linear_to_srgb) {- uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;+ uint64_t specializations = p_linear_to_srgb ? (uint64_t)CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION, specializations);if (!success) {@@ -134,7 +134,7 @@ void CopyEffects::copy_to_rect_3d(const Rect2 &p_rect, float p_layer, int p_typeCopyShaderGLES3::ShaderVariant variant = p_type == Texture::TYPE_LAYERED? CopyShaderGLES3::MODE_COPY_SECTION_2D_ARRAY: CopyShaderGLES3::MODE_COPY_SECTION_3D;- uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;+ uint64_t specializations = p_linear_to_srgb ? (uint64_t)CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;bool success = copy.shader.version_bind_shader(copy.shader_version, variant, specializations);if (!success) {@@ -150,7 +150,7 @@ void CopyEffects::copy_to_rect_3d(const Rect2 &p_rect, float p_layer, int p_typevoid CopyEffects::copy_with_lens_distortion(const Rect2 &p_rect, float p_layer, const Vector2 &p_eye_center, float p_k1, float p_k2, float p_upscale, float p_aspect_ration, bool p_linear_to_srgb) {CopyShaderGLES3::ShaderVariant variant = CopyShaderGLES3::MODE_LENS_DISTORTION;- uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;+ uint64_t specializations = p_linear_to_srgb ? (uint64_t)CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0;bool success = copy.shader.version_bind_shader(copy.shader_version, variant, specializations);if (!success) {diff --git a/drivers/gles3/effects/glow.cpp b/drivers/gles3/effects/glow.cppindex ff805784d21..b30f1447ecf 100644--- a/drivers/gles3/effects/glow.cpp+++ b/drivers/gles3/effects/glow.cpp@@ -106,7 +106,7 @@ void Glow::process_glow(GLuint p_source_color, Size2i p_size, const Glow::LevelglActiveTexture(GL_TEXTURE0);glBindTexture(p_use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D, p_source_color);- uint64_t specialization = p_use_multiview ? GlowShaderGLES3::USE_MULTIVIEW : 0;+ uint64_t specialization = p_use_multiview ? (uint64_t)GlowShaderGLES3::USE_MULTIVIEW : 0;bool success = glow.shader.version_bind_shader(glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization);if (!success) {return;diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cppindex 3b570522c95..a6c28184926 100644--- a/drivers/gles3/rasterizer_canvas_gles3.cpp+++ b/drivers/gles3/rasterizer_canvas_gles3.cpp@@ -685,7 +685,7 @@ void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_coustate.current_tex = RID();- const uint64_t base_specialization = GLES3::Config::get_singleton()->float_texture_supported ? 0 : CanvasShaderGLES3::USE_RGBA_SHADOWS;+ const uint64_t base_specialization = GLES3::Config::get_singleton()->float_texture_supported ? 0 : (uint64_t)CanvasShaderGLES3::USE_RGBA_SHADOWS;for (uint32_t i = 0; i <= state.current_batch_index; i++) {// Skipping when there is no instances.diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cppindex 5d458d8fa47..b188b0b0864 100644--- a/drivers/gles3/rasterizer_scene_gles3.cpp+++ b/drivers/gles3/rasterizer_scene_gles3.cpp@@ -896,7 +896,7 @@ void RasterizerSceneGLES3::_draw_sky(RID p_env, const Projection &p_projection,GLES3::SkyShaderData *shader_data = material_data->shader_data;ERR_FAIL_NULL(shader_data);- uint64_t spec_constants = p_use_multiview ? SkyShaderGLES3::USE_MULTIVIEW : 0;+ uint64_t spec_constants = p_use_multiview ? (uint64_t)SkyShaderGLES3::USE_MULTIVIEW : 0;if (p_flip_y) {spec_constants |= SkyShaderGLES3::USE_INVERTED_Y;}@@ -3610,6 +3610,9 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,if (lm->uses_spherical_harmonics) {spec_constants |= SceneShaderGLES3::USE_SH_LIGHTMAP;+ if (lm->specular_intensity > 0.0f) {+ spec_constants |= SceneShaderGLES3::USE_LIGHTMAP_SPECULAR;+ }}if (lightmap_bicubic_upscale) {@@ -3645,6 +3648,10 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,spec_constants |= SceneShaderGLES3::USE_LIGHTMAP;disable_lightmaps = false;+ if (lm->uses_spherical_harmonics && lm->specular_intensity > 0.0f) {+ spec_constants |= SceneShaderGLES3::USE_LIGHTMAP_SPECULAR;+ }+if (lightmap_bicubic_upscale) {spec_constants |= SceneShaderGLES3::LIGHTMAP_BICUBIC_FILTER;}@@ -3768,6 +3775,9 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,}material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SHADOWMASK_MODE, (uint32_t)lm->shadowmask_mode, shader->version, instance_variant, spec_constants);+ if (spec_constants & SceneShaderGLES3::USE_LIGHTMAP_SPECULAR) {+ material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SPECULAR_INTENSITY, lm->specular_intensity, shader->version, instance_variant, spec_constants);+ }if (lm->shadow_texture.is_valid()) {tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lm->shadow_texture);@@ -3824,6 +3834,9 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,exposure_normalization = enf / lm->baked_exposure;}material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_EXPOSURE_NORMALIZATION, exposure_normalization, shader->version, instance_variant, spec_constants);+ if (spec_constants & SceneShaderGLES3::USE_LIGHTMAP_SPECULAR) {+ material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SPECULAR_INTENSITY, lm->specular_intensity, shader->version, instance_variant, spec_constants);+ }if (lm->uses_spherical_harmonics) {Basis to_lm = li->transform.basis.inverse() * p_render_data->cam_transform.basis;diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glslindex 709dc63fe0a..c2d544c1a2b 100644--- a/drivers/gles3/shaders/scene.glsl+++ b/drivers/gles3/shaders/scene.glsl@@ -42,6 +42,7 @@ RENDER_MATERIAL = falseSECOND_REFLECTION_PROBE = falseLIGHTMAP_BICUBIC_FILTER = falseRENDER_MOTION_VECTORS = false+USE_LIGHTMAP_SPECULAR = false#[vertex]@@ -1479,6 +1480,10 @@ uniform highp vec4 lightmap_uv_scale;uniform float lightmap_exposure_normalization;uniform uint lightmap_shadowmask_mode;+#ifdef USE_LIGHTMAP_SPECULAR+uniform float lightmap_specular_intensity;+#endif+#define SHADOWMASK_MODE_NONE uint(0)#define SHADOWMASK_MODE_REPLACE uint(1)#define SHADOWMASK_MODE_OVERLAY uint(2)@@ -1554,7 +1559,7 @@ vec3 F0(float metallic, float specular, vec3 albedo) {#ifndef MODE_RENDER_DEPTH#ifndef USE_VERTEX_LIGHTING-#if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || !defined(DISABLE_LIGHT_AREA) || defined(USE_ADDITIVE_LIGHTING)+#if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || !defined(DISABLE_LIGHT_AREA) || defined(USE_ADDITIVE_LIGHTING) || defined(USE_SH_LIGHTMAP)float D_GGX(float cos_theta_m, float alpha) {float a = cos_theta_m * alpha;@@ -2515,6 +2520,7 @@ void main() {#ifndef MODE_UNSHADEDvec3 f0 = F0(metallic, specular, albedo);vec3 specular_light = vec3(0.0, 0.0, 0.0);+ vec3 lightmap_specular_light = vec3(0.0);vec3 diffuse_light = vec3(0.0, 0.0, 0.0);vec3 ambient_light = vec3(0.0, 0.0, 0.0);@@ -2657,10 +2663,64 @@ void main() {vec3 n = normalize(lightmap_normal_xform * indirect_normal);- ambient_light += lm_light_l0 * lightmap_exposure_normalization;- ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * lightmap_exposure_normalization * 4.0);- ambient_light += lm_light_l1_0 * n.z * (lm_light_l0 * lightmap_exposure_normalization * 4.0);- ambient_light += lm_light_l1p1 * n.x * (lm_light_l0 * lightmap_exposure_normalization * 4.0);+ vec3 sh_light = lm_light_l0;+ sh_light += lm_light_l0 * lm_light_l1n1 * n.y * 4.0;+ sh_light += lm_light_l0 * lm_light_l1_0 * n.z * 4.0;+ sh_light += lm_light_l0 * lm_light_l1p1 * n.x * 4.0;+ sh_light *= lightmap_exposure_normalization;++ ambient_light += sh_light;++#ifdef USE_LIGHTMAP_SPECULAR+ // Fake specular light to create some direct light specular lobes for directional lightmaps.+ // https://media.contentapi.ea.com/content/dam/eacom/frostbite/files/gdc2018-precomputedgiobalilluminationinfrostbite.pdf (slides 66-71)+ const vec3 luminance_weights = vec3(0.2126, 0.7152, 0.0722);+ vec3 l1 = vec3(+ dot(lm_light_l0 * lm_light_l1p1, luminance_weights),+ dot(lm_light_l0 * lm_light_l1n1, luminance_weights),+ dot(lm_light_l0 * lm_light_l1_0, luminance_weights));+ float l1_len = length(l1);+ float l0_luminance = dot(lm_light_l0, luminance_weights);++ if (l1_len > 1e-5 && l0_luminance > 1e-5) {+ vec3 lightmap_direction = l1 / l1_len;+ vec3 L_view = normalize(lightmap_direction * lightmap_normal_xform);+ float NdotL = max(dot(normal, L_view), 0.0);++ if (NdotL > 1e-4) {+ vec3 specular_lightmap_normal = normalize(lightmap_normal_xform * normal);+ vec3 specular_irradiance = lm_light_l0;+ specular_irradiance += lm_light_l0 * lm_light_l1n1 * specular_lightmap_normal.y * 4.0;+ specular_irradiance += lm_light_l0 * lm_light_l1_0 * specular_lightmap_normal.z * 4.0;+ specular_irradiance += lm_light_l0 * lm_light_l1p1 * specular_lightmap_normal.x * 4.0;+ specular_irradiance *= lightmap_exposure_normalization;+ vec3 specular_light_color = max(specular_irradiance, vec3(0.0)) / max(NdotL, 0.1);++ vec3 f0 = F0(metallic, specular, albedo);++ vec3 diffuse_light_discarded = diffuse_light;+ float directionality = clamp(l1_len / l0_luminance, 0.0, 1.0);+ float specular_intensity = directionality * lightmap_specular_intensity * 2.0;++ light_compute(normal, L_view, view, 0.0, specular_light_color, true, 1.0, f0, roughness, metallic, specular_intensity, albedo, alpha, screen_uv,+#ifdef LIGHT_BACKLIGHT_USED+ backlight,+#endif+#ifdef LIGHT_RIM_USED+ rim, rim_tint,+#endif+#ifdef LIGHT_CLEARCOAT_USED+ clearcoat, clearcoat_roughness, normalize(normal_interp),+#endif+#ifdef LIGHT_ANISOTROPY_USED+ binormal, tangent, anisotropy,+#endif+ diffuse_light_discarded,+ lightmap_specular_light);+ }+ }+#endif // USE_LIGHTMAP_SPECULAR+#else#ifdef LIGHTMAP_BICUBIC_FILTERambient_light += textureArray_bicubic(lightmap_textures, uvw, lightmap_texture_size).rgb * lightmap_exposure_normalization;@@ -2719,7 +2779,7 @@ void main() {#endif}#endif // !AMBIENT_LIGHT_DISABLED-+ specular_light += lightmap_specular_light;#ifdef USE_VERTEX_LIGHTINGspecular_light += specular_light_interp * f0;diffuse_light += diffuse_light_interp;diff --git a/drivers/gles3/storage/light_storage.cpp b/drivers/gles3/storage/light_storage.cppindex 94c31aec6aa..86b7b6719b6 100644--- a/drivers/gles3/storage/light_storage.cpp+++ b/drivers/gles3/storage/light_storage.cpp@@ -1303,9 +1303,24 @@ RSE::ShadowmaskMode LightStorage::lightmap_get_shadowmask_mode(RID p_lightmap) {void LightStorage::lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) {Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);ERR_FAIL_NULL(lightmap);+lightmap->shadowmask_mode = p_mode;}+float LightStorage::lightmap_get_specular_intensity(RID p_lightmap) {+ Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);+ ERR_FAIL_NULL_V(lightmap, 0.0f);++ return lightmap->specular_intensity;+}++void LightStorage::lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) {+ Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);+ ERR_FAIL_NULL(lightmap);++ lightmap->specular_intensity = p_intensity;+}+/* LIGHTMAP INSTANCE */RID LightStorage::lightmap_instance_create(RID p_lightmap) {diff --git a/drivers/gles3/storage/light_storage.h b/drivers/gles3/storage/light_storage.hindex 10d31a788c6..b827b26307e 100644--- a/drivers/gles3/storage/light_storage.h+++ b/drivers/gles3/storage/light_storage.h@@ -190,6 +190,7 @@ struct Lightmap {PackedColorArray point_sh;PackedInt32Array tetrahedra;PackedInt32Array bsp_tree;+ float specular_intensity = 0.0f;struct BSP {static const int32_t EMPTY_LEAF = INT32_MIN;@@ -753,6 +754,8 @@ public:virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) override;virtual RSE::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) override;virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) override;+ virtual float lightmap_get_specular_intensity(RID p_lightmap) override;+ virtual void lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) override;/* LIGHTMAP INSTANCE */diff --git a/drivers/gles3/storage/mesh_storage.cpp b/drivers/gles3/storage/mesh_storage.cppindex ec35a8f71a9..9d8cdc25e07 100644--- a/drivers/gles3/storage/mesh_storage.cpp+++ b/drivers/gles3/storage/mesh_storage.cpp@@ -1413,7 +1413,7 @@ void MeshStorage::update_mesh_instances() {if (mi->mesh->blend_shape_count) {SkeletonShaderGLES3::ShaderVariant variant = SkeletonShaderGLES3::MODE_BASE_PASS;uint64_t specialization = 0;- specialization |= array_is_2d ? SkeletonShaderGLES3::MODE_2D : 0;+ specialization |= array_is_2d ? (uint64_t)SkeletonShaderGLES3::MODE_2D : 0;specialization |= SkeletonShaderGLES3::USE_BLEND_SHAPES;if (!array_is_2d) {if ((mi->surfaces[i].format_cache & (1ULL << RSE::ARRAY_NORMAL))) {@@ -1479,8 +1479,8 @@ void MeshStorage::update_mesh_instances() {glBindVertexArray(mi->mesh->surfaces[i]->blend_shapes[bs].vertex_array);_blend_shape_bind_mesh_instance_buffer(mi, i);- specialization |= can_use_skeleton ? SkeletonShaderGLES3::USE_SKELETON : 0;- specialization |= (can_use_skeleton && use_8_weights) ? SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;+ specialization |= can_use_skeleton ? (uint64_t)SkeletonShaderGLES3::USE_SKELETON : 0;+ specialization |= (can_use_skeleton && use_8_weights) ? (uint64_t)SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;specialization |= SkeletonShaderGLES3::FINAL_PASS;success = skeleton_shader.shader.version_bind_shader(skeleton_shader.shader_version, variant, specialization);if (!success) {@@ -1521,10 +1521,10 @@ void MeshStorage::update_mesh_instances() {if (can_use_skeleton) {SkeletonShaderGLES3::ShaderVariant variant = SkeletonShaderGLES3::MODE_BASE_PASS;uint64_t specialization = 0;- specialization |= array_is_2d ? SkeletonShaderGLES3::MODE_2D : 0;+ specialization |= array_is_2d ? (uint64_t)SkeletonShaderGLES3::MODE_2D : 0;specialization |= SkeletonShaderGLES3::USE_SKELETON;specialization |= SkeletonShaderGLES3::FINAL_PASS;- specialization |= use_8_weights ? SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;+ specialization |= use_8_weights ? (uint64_t)SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;if (!array_is_2d) {if ((mi->surfaces[i].format_cache & (1ULL << RSE::ARRAY_NORMAL))) {specialization |= SkeletonShaderGLES3::USE_NORMAL;diff --git a/gles3_builders.py b/gles3_builders.pyindex a2c4d226614..c0cf7aca82d 100644--- a/gles3_builders.py+++ b/gles3_builders.py@@ -286,7 +286,7 @@ public:f"{name.upper()} = {1 << index}" for index, name in enumerate(header_data.specialization_names))file.write(f"""\- enum Specializations {{+ enum Specializations : uint64_t {{{specialization_names},}};diff --git a/scene/3d/lightmap_gi.cpp b/scene/3d/lightmap_gi.cppindex d1e199c6a72..a8b6b119320 100644--- a/scene/3d/lightmap_gi.cpp+++ b/scene/3d/lightmap_gi.cpp@@ -234,6 +234,10 @@ LightmapGIData::ShadowmaskMode LightmapGIData::get_shadowmask_mode() const {return (ShadowmaskMode)RS::get_singleton()->lightmap_get_shadowmask_mode(lightmap);}+void LightmapGIData::update_specular_intensity(float p_intensity) {+ RS::get_singleton()->lightmap_set_specular_intensity(lightmap, p_intensity);+}+void LightmapGIData::set_capture_data(const AABB &p_bounds, bool p_interior, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree, float p_baked_exposure, uint32_t p_lightprobe_hash) {if (p_points.size()) {int pc = p_points.size();@@ -1802,6 +1806,7 @@ void LightmapGI::set_light_data(const Ref<LightmapGIData> &p_data) {_assign_lightmaps();}light_data->update_shadowmask_mode(shadowmask_mode);+ light_data->update_specular_intensity(specular_intensity);}update_gizmos();@@ -1850,6 +1855,8 @@ int LightmapGI::get_denoiser_range() const {void LightmapGI::set_directional(bool p_enable) {directional = p_enable;++ notify_property_list_changed();}bool LightmapGI::is_directional() const {@@ -2000,6 +2007,18 @@ Ref<CameraAttributes> LightmapGI::get_camera_attributes() const {return camera_attributes;}+float LightmapGI::get_specular_intensity() const {+ return specular_intensity;+}++void LightmapGI::set_specular_intensity(float p_strength) {+ specular_intensity = p_strength;++ if (light_data.is_valid()) {+ light_data->update_specular_intensity(p_strength);+ }+}+PackedStringArray LightmapGI::get_configuration_warnings() const {PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();@@ -2051,6 +2070,11 @@ void LightmapGI::_validate_property(PropertyInfo &p_property) const {p_property.usage = PROPERTY_USAGE_NO_EDITOR;}}+ if (p_property.name == "specular_intensity") {+ if (!directional) {+ p_property.usage = PROPERTY_USAGE_NO_EDITOR;+ }+ }}void LightmapGI::_bind_methods() {@@ -2120,6 +2144,9 @@ void LightmapGI::_bind_methods() {ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &LightmapGI::set_camera_attributes);ClassDB::bind_method(D_METHOD("get_camera_attributes"), &LightmapGI::get_camera_attributes);+ ClassDB::bind_method(D_METHOD("set_specular_intensity", "specular_intensity"), &LightmapGI::set_specular_intensity);+ ClassDB::bind_method(D_METHOD("get_specular_intensity"), &LightmapGI::get_specular_intensity);+// ClassDB::bind_method(D_METHOD("bake", "from_node"), &LightmapGI::bake, DEFVAL(Variant()));ADD_GROUP("Tweaks", "");@@ -2129,6 +2156,7 @@ void LightmapGI::_bind_methods() {ADD_PROPERTY(PropertyInfo(Variant::INT, "bounces", PROPERTY_HINT_RANGE, "0,6,1,or_greater"), "set_bounces", "get_bounces");ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce_indirect_energy", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_bounce_indirect_energy", "get_bounce_indirect_energy");ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional"), "set_directional", "is_directional");+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "specular_intensity", PROPERTY_HINT_RANGE, "0,10,0.01"), "set_specular_intensity", "get_specular_intensity");ADD_PROPERTY(PropertyInfo(Variant::INT, "shadowmask_mode", PROPERTY_HINT_ENUM, "None,Replace,Overlay"), "set_shadowmask_mode", "get_shadowmask_mode");ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_for_bounces"), "set_use_texture_for_bounces", "is_using_texture_for_bounces");ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");@@ -2144,6 +2172,7 @@ void LightmapGI::_bind_methods() {ADD_PROPERTY(PropertyInfo(Variant::COLOR, "environment_custom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_environment_custom_color", "get_environment_custom_color");ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "environment_custom_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_environment_custom_energy", "get_environment_custom_energy");ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");+ADD_GROUP("Gen Probes", "generate_probes_");ADD_PROPERTY(PropertyInfo(Variant::INT, "generate_probes_subdiv", PROPERTY_HINT_ENUM, "Disabled,4,8,16,32"), "set_generate_probes", "get_generate_probes");ADD_GROUP("Data", "");diff --git a/scene/3d/lightmap_gi.h b/scene/3d/lightmap_gi.hindex f87e6154089..3028a3243e3 100644--- a/scene/3d/lightmap_gi.h+++ b/scene/3d/lightmap_gi.h@@ -140,6 +140,8 @@ public:void clear_shadowmask_textures();bool has_shadowmask_textures();+ void update_specular_intensity(float p_intensity);+virtual RID get_rid() const override;LightmapGIData();~LightmapGIData();@@ -208,6 +210,7 @@ private:LightmapGIData::ShadowmaskMode shadowmask_mode = LightmapGIData::SHADOWMASK_MODE_NONE;GenerateProbes gen_probes = GENERATE_PROBES_SUBDIV_8;Ref<CameraAttributes> camera_attributes;+ float specular_intensity = 0.0f;Ref<LightmapGIData> light_data;Node *last_owner = nullptr;@@ -349,6 +352,9 @@ public:void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes);Ref<CameraAttributes> get_camera_attributes() const;+ float get_specular_intensity() const;+ void set_specular_intensity(float p_strength);+AABB get_aabb() const override;BakeError bake(Node *p_from_node, String p_image_data_path = "", Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr);diff --git a/servers/rendering/dummy/storage/light_storage.h b/servers/rendering/dummy/storage/light_storage.hindex 1f722816c7b..52a8bb3c053 100644--- a/servers/rendering/dummy/storage/light_storage.h+++ b/servers/rendering/dummy/storage/light_storage.h@@ -205,6 +205,8 @@ public:virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) override {}virtual RSE::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) override { return RSE::SHADOWMASK_MODE_NONE; }virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) override {}+ virtual float lightmap_get_specular_intensity(RID p_lightmap) override { return 0.0f; }+ virtual void lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) override {}/* LIGHTMAP INSTANCE */diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cppindex 36671e57a8c..c418f885aba 100644--- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp+++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp@@ -429,6 +429,7 @@ void RenderForwardClustered::_render_list_template(RenderingDevice::DrawListID ppipeline_specialization.use_light_soft_shadows = element_info.uses_softshadow;pipeline_specialization.use_light_projector = element_info.uses_projector;pipeline_specialization.use_directional_soft_shadows = p_params->use_directional_soft_shadow;+ pipeline_specialization.use_lightmap_specular = element_info.uses_lightmap_specular;}pipeline_key.color_pass_flags = 0;@@ -877,7 +878,7 @@ void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, iconst bool cant_repeat = instance_data.flags & INSTANCE_DATA_FLAG_MULTIMESH || inst->mesh_instance.is_valid();- if (prev_surface != nullptr && !cant_repeat && prev_surface->sort.sort_key1 == surface->sort.sort_key1 && prev_surface->sort.sort_key2 == surface->sort.sort_key2 && inst->mirror == prev_surface->owner->mirror && repeats < RenderElementInfo::MAX_REPEATS) {+ if (prev_surface != nullptr && !cant_repeat && prev_surface->sort.sort_key1 == surface->sort.sort_key1 && prev_surface->sort.sort_key2 == surface->sort.sort_key2 && prev_surface->uses_lightmap_specular == surface->uses_lightmap_specular && inst->mirror == prev_surface->owner->mirror && repeats < RenderElementInfo::MAX_REPEATS) {//this element is the same as the previous one, count repeats to draw it using instancingrepeats++;} else {@@ -895,6 +896,7 @@ void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, iRenderElementInfo &element_info = rl->element_info[p_offset + i];element_info.value = uint32_t(surface->sort.sort_key1 & 0xFFF);+ element_info.uses_lightmap_specular = surface->uses_lightmap_specular;if (cant_repeat) {prev_surface = nullptr;@@ -974,6 +976,7 @@ void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, conflags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE;}bool uses_lightmap = false;+ bool uses_lightmap_specular = false;bool uses_gi = false;bool uses_motion = false;float fade_alpha = 1.0;@@ -1009,6 +1012,7 @@ void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, conflags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP;if (scene_state.lightmap_has_sh[lightmap_cull_index]) {flags |= INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP;+ uses_lightmap_specular = scene_state.lightmap_has_specular[lightmap_cull_index];}uses_lightmap = true;} else {@@ -1102,6 +1106,7 @@ void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, conwhile (surf) {surf->sort.uses_forward_gi = 0;surf->sort.uses_lightmap = 0;+ surf->uses_lightmap_specular = false;// LODif (p_render_data->scene_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) {@@ -1161,6 +1166,7 @@ void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, conif (uses_lightmap) {surf->sort.uses_lightmap = 1;+ surf->uses_lightmap_specular = uses_lightmap_specular;scene_state.used_lightmap = true;}@@ -1244,6 +1250,9 @@ void RenderForwardClustered::_setup_lightmaps(const RenderDataRD *p_render_data,scene_state.lightmap_ids[i] = p_lightmaps[i];scene_state.lightmap_has_sh[i] = light_storage->lightmap_uses_spherical_harmonics(lightmap);+ scene_state.lightmaps[i].specular_intensity = light_storage->lightmap_get_specular_intensity(lightmap);+ scene_state.lightmap_has_specular[i] = scene_state.lightmap_has_sh[i] && (scene_state.lightmaps[i].specular_intensity > 0.0f);+scene_state.lightmaps_used++;}if (scene_state.lightmaps_used > 0) {diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.hindex 7dec40d892d..6a2b90c4a27 100644--- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h+++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h@@ -262,7 +262,9 @@ private:float normal_xform[12];float texture_size[2];float exposure_normalization;+ float specular_intensity;uint32_t flags;+ uint32_t pad[3];};struct LightmapCaptureData {@@ -402,6 +404,7 @@ private:LightmapData lightmaps[MAX_LIGHTMAPS];RID lightmap_ids[MAX_LIGHTMAPS];bool lightmap_has_sh[MAX_LIGHTMAPS];+ bool lightmap_has_specular[MAX_LIGHTMAPS];uint32_t lightmaps_used = 0;uint32_t max_lightmaps;RID lightmap_buffer;@@ -460,6 +463,9 @@ private:uint32_t uses_projector : 1;uint32_t uses_forward_gi : 1;uint32_t uses_lightmap : 1;++ // This value is not part of the sort key as there are no more available bits.+ uint32_t uses_lightmap_specular : 1;};uint32_t value;};@@ -516,6 +522,7 @@ private:uint64_t uses_projector : 1;uint64_t uses_forward_gi : 1;uint64_t uses_lightmap : 1;+ // "uses_lightmap_specular" is excluded as there are no more available bits.// Sorted based on optimal order for respecting priority and reducing the amount of rebinding of shaders, materials,// and geometry. This current order was found to be the most optimal in large projects. If you wish to measure@@ -534,6 +541,7 @@ private:RSE::PrimitiveType primitive = RSE::PRIMITIVE_MAX;uint32_t flags = 0;uint32_t surface_index = 0;+ bool uses_lightmap_specular = false;uint32_t color_pass_inclusion_mask = 0;void *surface = nullptr;diff --git a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.hindex 5e1af939eb9..565834ae0d4 100644--- a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h+++ b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h@@ -128,6 +128,7 @@ public:uint32_t multimesh_has_custom_data : 1;uint32_t fog_use_legacy_blending : 1;uint32_t cluster_has_area_light : 1;+ uint32_t use_lightmap_specular : 1;};};diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cppindex 732ec9bb556..4318a0d0ae1 100644--- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp+++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp@@ -761,6 +761,9 @@ void RenderForwardMobile::_setup_lightmaps(const RenderDataRD *p_render_data, coscene_state.lightmap_ids[i] = p_lightmaps[i];scene_state.lightmap_has_sh[i] = light_storage->lightmap_uses_spherical_harmonics(lightmap);+ scene_state.lightmaps[i].specular_intensity = light_storage->lightmap_get_specular_intensity(lightmap);+ scene_state.lightmap_has_specular[i] = scene_state.lightmap_has_sh[i] && (scene_state.lightmaps[i].specular_intensity > 0.0f);+scene_state.lightmaps_used++;}if (scene_state.lightmaps_used > 0) {@@ -2155,8 +2158,8 @@ void RenderForwardMobile::_fill_instance_data(RenderListType p_render_list, uintRenderElementInfo &element_info = rl->element_info[p_offset + i];- // Sets lod_index and uses_lightmap at once.- element_info.value = uint32_t(surface->sort.sort_key1 & 0x1FF);+ // Sets lod_index, uses_lightmap, and uses_lightmap_specular at once.+ element_info.value = uint32_t(surface->sort.sort_key1 & 0x3FF);}if (p_update_buffer && element_total > 0u) {@@ -2222,6 +2225,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, const}bool uses_lightmap = false;+ bool uses_lightmap_specular = false;// bool uses_gi = false;if (p_render_list == RENDER_LIST_OPAQUE) {@@ -2239,6 +2243,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, constflags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP;if (scene_state.lightmap_has_sh[lightmap_cull_index]) {flags |= INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP;+ uses_lightmap_specular = scene_state.lightmap_has_specular[lightmap_cull_index];}uses_lightmap = true;} else {@@ -2281,6 +2286,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, constwhile (surf) {surf->sort.uses_lightmap = 0;+ surf->sort.uses_lightmap_specular = 0;// LOD@@ -2325,6 +2331,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, constif (uses_lightmap) {surf->sort.uses_lightmap = 1; // This needs to become our lightmap index but we'll do that in a separate PR.+ surf->sort.uses_lightmap_specular = uses_lightmap_specular ? 1 : 0;scene_state.used_lightmap = true;}@@ -2483,6 +2490,7 @@ void RenderForwardMobile::_render_list_template(RenderingDevice::DrawListID p_drpipeline_specialization.area_lights = SceneShaderForwardMobile::shader_count_for(inst->area_light_count);pipeline_specialization.reflection_probes = SceneShaderForwardMobile::shader_count_for(inst->reflection_probe_count);pipeline_specialization.decals = inst->decals_count > 0;+ pipeline_specialization.use_lightmap_specular = element_info.uses_lightmap_specular;#ifdef DEBUG_ENABLEDif (unlikely(get_debug_draw_mode() == RSE::VIEWPORT_DEBUG_DRAW_LIGHTING)) {diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.hindex 1d7bd719a58..21c4c46d866 100644--- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h+++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h@@ -189,7 +189,9 @@ private:float normal_xform[12];float texture_size[2];float exposure_normalization;+ float specular_intensity;uint32_t flags;+ uint32_t pad[3];};struct LightmapCaptureData {@@ -289,6 +291,7 @@ private:LightmapData lightmaps[MAX_LIGHTMAPS];RID lightmap_ids[MAX_LIGHTMAPS];bool lightmap_has_sh[MAX_LIGHTMAPS];+ bool lightmap_has_specular[MAX_LIGHTMAPS];uint32_t lightmaps_used = 0;uint32_t max_lightmaps;RID lightmap_buffer;@@ -402,7 +405,8 @@ private:struct {uint32_t lod_index : 8;uint32_t uses_lightmap : 1;- uint32_t reserved : 23;+ uint32_t uses_lightmap_specular : 1;+ uint32_t reserved : 22;};uint32_t value;};@@ -490,7 +494,8 @@ protected:// Needs to be grouped together to be used in RenderElementInfo, as the value is masked directly.uint64_t lod_index : 8;uint64_t uses_lightmap : 1;- uint64_t pad : 3;+ uint64_t uses_lightmap_specular : 1;+ uint64_t pad : 2;// Sorted based on optimal order for respecting priority and reducing the amount of rebinding of shaders, materials,// and geometry. This current order was found to be the most optimal in large projects. If you wish to measurediff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.hindex bef0e806f4a..0778683a98e 100644--- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h+++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h@@ -129,7 +129,7 @@ public:uint32_t directional_lights : 2;uint32_t decals : 1;uint32_t directional_light_blend_splits : 8;- uint32_t padding_1 : 1;+ uint32_t use_lightmap_specular : 1;};};diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glslindex 1c064975efa..1e4a72816b4 100644--- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl@@ -1854,10 +1854,69 @@ void fragment_shader(in SceneData scene_data) {vec3 n = normalize(lightmaps.data[ofs].normal_xform * indirect_normal);float en = lightmaps.data[ofs].exposure_normalization;- ambient_light += lm_light_l0 * en;- ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * en * 4.0);- ambient_light += lm_light_l1_0 * n.z * (lm_light_l0 * en * 4.0);- ambient_light += lm_light_l1p1 * n.x * (lm_light_l0 * en * 4.0);+ vec3 sh_light = lm_light_l0;+ sh_light += lm_light_l0 * lm_light_l1n1 * n.y * 4.0;+ sh_light += lm_light_l0 * lm_light_l1_0 * n.z * 4.0;+ sh_light += lm_light_l0 * lm_light_l1p1 * n.x * 4.0;+ sh_light *= lightmaps.data[ofs].exposure_normalization;+ ambient_light += sh_light;++ if (sc_use_lightmap_specular()) {+ // fake specular light to create some direct light specular lobes for directional lightmaps.+ // https://media.contentapi.ea.com/content/dam/eacom/frostbite/files/gdc2018-precomputedgiobalilluminationinfrostbite.pdf (slides 66-71)+ const vec3 luminance_weights = vec3(0.2126, 0.7152, 0.0722);+ vec3 l1 = vec3(+ dot(lm_light_l0 * lm_light_l1p1, luminance_weights),+ dot(lm_light_l0 * lm_light_l1n1, luminance_weights),+ dot(lm_light_l0 * lm_light_l1_0, luminance_weights));+ float l1_len = length(l1);+ float l0_luminance = dot(lm_light_l0, luminance_weights);++ if (l1_len > 1e-5 && l0_luminance > 1e-5) {+ vec3 lightmap_direction = l1 / l1_len;+ vec3 L_view = normalize(lightmap_direction * lightmaps.data[ofs].normal_xform);+ float NdotL = max(dot(normal, L_view), 0.0);++ if (NdotL > 1e-4) {+ vec3 specular_lightmap_normal = normalize(lightmaps.data[ofs].normal_xform * normal);+ vec3 specular_irradiance = lm_light_l0;+ specular_irradiance += lm_light_l0 * lm_light_l1n1 * specular_lightmap_normal.y * 4.0;+ specular_irradiance += lm_light_l0 * lm_light_l1_0 * specular_lightmap_normal.z * 4.0;+ specular_irradiance += lm_light_l0 * lm_light_l1p1 * specular_lightmap_normal.x * 4.0;+ specular_irradiance *= lightmaps.data[ofs].exposure_normalization;+ vec3 specular_light_color = max(specular_irradiance, vec3(0.0)) / max(NdotL, 0.1);++ vec3 f0 = F0(metallic, specular, albedo);++ vec3 diffuse_light_discarded = diffuse_light;+ float directionality = clamp(l1_len / l0_luminance, 0.0, 1.0);+ float specular_intensity = directionality * lightmaps.data[ofs].specular_intensity * 2.0;++ light_compute(normal, L_view, view, 0.0, specular_light_color, true, 1.0, f0, roughness, metallic, specular_intensity, albedo, alpha, screen_uv, energy_compensation,+#ifdef LIGHT_BACKLIGHT_USED+ backlight,+#endif+#ifdef LIGHT_TRANSMITTANCE_USED+ transmittance_color,+ transmittance_depth,+ transmittance_boost,+ transmittance_z,+#endif+#ifdef LIGHT_RIM_USED+ rim, rim_tint,+#endif+#ifdef LIGHT_CLEARCOAT_USED+ clearcoat, clearcoat_roughness, geo_normal,+#endif // LIGHT_CLEARCOAT_USED+#ifdef LIGHT_ANISOTROPY_USED+ binormal,+ tangent, anisotropy,+#endif+ diffuse_light_discarded,+ direct_specular_light);+ }+ }+ }} else {if (sc_use_lightmap_bicubic_filter()) {diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered_inc.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered_inc.glslindex 1362d1065a5..17d79d1cf4c 100644--- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered_inc.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered_inc.glsl@@ -146,6 +146,10 @@ bool sc_cluster_has_area_light() {return ((sc_packed_1() >> 5) & 1U) != 0;}+bool sc_use_lightmap_specular() {+ return ((sc_packed_1() >> 6) & 1U) != 0;+}+float sc_luminance_multiplier() {// Not used in clustered renderer but we share some code with the mobile renderer that requires this.return 1.0;@@ -231,7 +235,11 @@ struct Lightmap {mat3 normal_xform;vec2 light_texture_size;float exposure_normalization;+ float specular_intensity;uint flags;+ uint pad1;+ uint pad2;+ uint pad3;};layout(set = 0, binding = 8, std140) restrict readonly buffer Lightmaps {diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glslindex b3834ab0e82..5845f2c53d2 100644--- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl@@ -1745,10 +1745,64 @@ void main() {hvec3 n = hvec3(normalize(lightmaps.data[ofs].normal_xform * indirect_normal));half exposure_normalization = half(lightmaps.data[ofs].exposure_normalization);- ambient_light += lm_light_l0 * exposure_normalization;- ambient_light += lm_light_l1n1 * n.y * lm_light_l0 * exposure_normalization * half(4.0);- ambient_light += lm_light_l1_0 * n.z * lm_light_l0 * exposure_normalization * half(4.0);- ambient_light += lm_light_l1p1 * n.x * lm_light_l0 * exposure_normalization * half(4.0);+ hvec3 sh_light = lm_light_l0;+ sh_light += lm_light_l1n1 * n.y * lm_light_l0 * half(4.0);+ sh_light += lm_light_l1_0 * n.z * lm_light_l0 * half(4.0);+ sh_light += lm_light_l1p1 * n.x * lm_light_l0 * half(4.0);+ sh_light *= exposure_normalization;+ ambient_light += sh_light;++ if (sc_use_lightmap_specular()) {+ // Fake specular light to create some direct light specular lobes for directional lightmaps.+ // https://media.contentapi.ea.com/content/dam/eacom/frostbite/files/gdc2018-precomputedgiobalilluminationinfrostbite.pdf (slides 66-71)+ const vec3 luminance_weights = vec3(0.2126, 0.7152, 0.0722);+ vec3 l1 = vec3(+ dot(vec3(lm_light_l0 * lm_light_l1p1), luminance_weights),+ dot(vec3(lm_light_l0 * lm_light_l1n1), luminance_weights),+ dot(vec3(lm_light_l0 * lm_light_l1_0), luminance_weights));+ float l1_len = length(l1);+ float l0_luminance = dot(vec3(lm_light_l0), luminance_weights);++ if (l1_len > 1e-5 && l0_luminance > 1e-5) {+ vec3 lightmap_direction = l1 / l1_len;+ vec3 L_view_highp = normalize(lightmap_direction * lightmaps.data[ofs].normal_xform);+ float NdotL = max(dot(vec3(normal), L_view_highp), 0.0);++ if (NdotL > 1e-4) {+ vec3 specular_lightmap_normal = normalize(lightmaps.data[ofs].normal_xform * vec3(normal));+ vec3 specular_irradiance = vec3(lm_light_l0);+ specular_irradiance += vec3(lm_light_l0 * lm_light_l1n1) * specular_lightmap_normal.y * 4.0;+ specular_irradiance += vec3(lm_light_l0 * lm_light_l1_0) * specular_lightmap_normal.z * 4.0;+ specular_irradiance += vec3(lm_light_l0 * lm_light_l1p1) * specular_lightmap_normal.x * 4.0;+ specular_irradiance *= lightmaps.data[ofs].exposure_normalization;+ hvec3 specular_light_color = hvec3(max(specular_irradiance, vec3(0.0)) / max(NdotL, 0.1));++ hvec3 f0 = F0(metallic, specular, albedo);++ hvec3 diffuse_light_discarded = diffuse_light;+ float directionality = clamp(l1_len / l0_luminance, 0.0, 1.0);+ float specular_intensity = directionality * lightmaps.data[ofs].specular_intensity * 2.0;++ light_compute(normal, hvec3(L_view_highp), view, saturateHalf(0.0), specular_light_color, true, half(1.0), f0, roughness, metallic, half(specular_intensity), albedo, alpha,+ screen_uv, hvec3(1.0),+#ifdef LIGHT_BACKLIGHT_USED+ backlight,+#endif+#ifdef LIGHT_RIM_USED+ rim, rim_tint,+#endif+#ifdef LIGHT_CLEARCOAT_USED+ clearcoat, clearcoat_roughness, geo_normal,+#endif+#ifdef LIGHT_ANISOTROPY_USED+ binormal, tangent, anisotropy,+#endif+ diffuse_light_discarded,+ direct_specular_light);+ }+ }+ }+} else {if (sc_use_lightmap_bicubic_filter()) {ambient_light += hvec3(textureArray_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization);diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glslindex ee17aad6765..200a0110b50 100644--- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl@@ -217,6 +217,10 @@ bool sc_directional_light_blend_split(uint i) {return ((sc_packed_1() >> (23 + i)) & 1U) != 0;}+bool sc_use_lightmap_specular() {+ return ((sc_packed_1() >> 31) & 1U) != 0;+}+half sc_luminance_multiplier() {return half(sc_packed_2());}@@ -295,7 +299,11 @@ struct Lightmap {mat3 normal_xform;vec2 light_texture_size;float exposure_normalization;+ float specular_intensity;uint flags;+ uint pad1;+ uint pad2;+ uint pad3;};layout(set = 0, binding = 8, std140) restrict readonly buffer Lightmaps {diff --git a/servers/rendering/renderer_rd/storage_rd/light_storage.cpp b/servers/rendering/renderer_rd/storage_rd/light_storage.cppindex 35a7f32ff6e..526d06e54ec 100644--- a/servers/rendering/renderer_rd/storage_rd/light_storage.cpp+++ b/servers/rendering/renderer_rd/storage_rd/light_storage.cpp@@ -2318,6 +2318,20 @@ void LightStorage::lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMlm->shadowmask_mode = p_mode;}+float LightStorage::lightmap_get_specular_intensity(RID p_lightmap) {+ Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);+ ERR_FAIL_NULL_V(lm, 0.0f);++ return lm->specular_intensity;+}++void LightStorage::lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) {+ Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);+ ERR_FAIL_NULL(lm);++ lm->specular_intensity = p_intensity;+}+/* LIGHTMAP INSTANCE */RID LightStorage::lightmap_instance_create(RID p_lightmap) {diff --git a/servers/rendering/renderer_rd/storage_rd/light_storage.h b/servers/rendering/renderer_rd/storage_rd/light_storage.hindex 71043e449bd..bee58680f8c 100644--- a/servers/rendering/renderer_rd/storage_rd/light_storage.h+++ b/servers/rendering/renderer_rd/storage_rd/light_storage.h@@ -362,6 +362,7 @@ private:PackedColorArray point_sh;PackedInt32Array tetrahedra;PackedInt32Array bsp_tree;+ float specular_intensity = 0.0f;struct BSP {static const int32_t EMPTY_LEAF = INT32_MIN;@@ -1037,6 +1038,8 @@ public:virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) override;virtual RSE::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) override;virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) override;+ virtual float lightmap_get_specular_intensity(RID p_lightmap) override;+ virtual void lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) override;virtual float lightmap_get_probe_capture_update_speed() const override {return lightmap_probe_capture_update_speed;@@ -1051,6 +1054,7 @@ public:ERR_FAIL_NULL_V(lm, 1.0);return lm->baked_exposure;}+_FORCE_INLINE_ int32_t lightmap_get_array_index(RID p_lightmap) const {ERR_FAIL_COND_V(!using_lightmap_array, -1); //only for arraysconst Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);diff --git a/servers/rendering/rendering_server.h b/servers/rendering/rendering_server.hindex d208a4432d4..3e561d13dbf 100644--- a/servers/rendering/rendering_server.h+++ b/servers/rendering/rendering_server.h@@ -434,6 +434,8 @@ public:virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) = 0;virtual RSE::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) = 0;virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) = 0;+ virtual float lightmap_get_specular_intensity(RID p_lightmap) = 0;+ virtual void lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) = 0;/* PARTICLES API */diff --git a/servers/rendering/rendering_server_default.h b/servers/rendering/rendering_server_default.hindex cc9518f8607..99454e17d65 100644--- a/servers/rendering/rendering_server_default.h+++ b/servers/rendering/rendering_server_default.h@@ -549,6 +549,8 @@ public:FUNC2(lightmap_set_shadowmask_textures, RID, RID)FUNC1R(RSE::ShadowmaskMode, lightmap_get_shadowmask_mode, RID)FUNC2(lightmap_set_shadowmask_mode, RID, RSE::ShadowmaskMode)+ FUNC1R(float, lightmap_get_specular_intensity, RID)+ FUNC2(lightmap_set_specular_intensity, RID, float)/* Shadow Atlas */FUNC0R(RID, shadow_atlas_create)diff --git a/servers/rendering/storage/light_storage.h b/servers/rendering/storage/light_storage.hindex fe2f4db53a7..07d3e96d523 100644--- a/servers/rendering/storage/light_storage.h+++ b/servers/rendering/storage/light_storage.h@@ -189,6 +189,8 @@ public:virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) = 0;virtual RSE::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) = 0;virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RSE::ShadowmaskMode p_mode) = 0;+ virtual float lightmap_get_specular_intensity(RID p_lightmap) = 0;+ virtual void lightmap_set_specular_intensity(RID p_lightmap, float p_intensity) = 0;/* LIGHTMAP INSTANCE */diff --git a/tests/python_build/fixtures/gles3/vertex_fragment.out b/tests/python_build/fixtures/gles3/vertex_fragment.outindex 8d5358aa361..c10f706d9fe 100644--- a/tests/python_build/fixtures/gles3/vertex_fragment.out+++ b/tests/python_build/fixtures/gles3/vertex_fragment.out@@ -6,7 +6,7 @@ public:MODE_NINEPATCH,};- enum Specializations {+ enum Specializations : uint64_t {DISABLE_LIGHTING = 1,};