Repository: godotengine/godot (language: cpp)

Issue:
[glTF] Very high light range values of imported lights are causing shadows not to render
### Tested versions

v4.3.beta.custom_build [8bf8f41fc]

### System information

Godot v4.3.beta (8bf8f41fc) - macOS 14.4.1 - Vulkan (Forward+) - integrated Apple M1 Max - Apple M1 Max (10 Threads)

### Issue description

This point light is imported with a range of 4096. Even though it has shadow enabled you can't see it:

<img width="800" alt="image" src="https://github.com/godotengine/godot/assets/60579014/dfd9ae5b-4e5f-43bf-a7d9-6839c6ded044"><br>

Shadow appears when you reduce its range all the way down to 300:

<img width="800" alt="image" src="https://github.com/godotengine/godot/assets/60579014/513694af-8f6f-4d65-99c1-f24518da950d"><br>

There's a same problem with spotlights iirc. Perhaps if the imported glTF doesn't provide a range value it should use Godot's default light range instead of an abnormally high one

### Steps to reproduce

MRP

### Minimal reproduction project (MRP)

[MRP_light_range.zip](https://github.com/godotengine/godot/files/15501683/MRP_light_range.zip)



Discussion / hints:
> Perhaps if the imported glTF doesn't provide a range value it should use Godot's default light range instead of an abnormally high one

If the light is meant to have infinite range in the source glTF, I wouldn't make it use Godot's default range, but a hardcoded value that is higher (probably between 100 and 250). I'd also only do this for lights with shadows enabled, as lights with very high ranges render correctly when shadows are disabled.
> I'd also only do this for lights with shadows enabled

That might be a problem because blender doesn't export shadow state, it doesn't even seem to be a part of glTF from what I understand

<img width="1062" alt="image" src="https://github.com/godotengine/godot/assets/60579014/d44b9575-afca-49d6-a8df-763156588300">

https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/README.md#light-shared-properties

I'm turning them on myself with an `EditorScenePostImportPlugin` if a light has a `-shadow` suffix
> That might be a problem because blender doesn't export shadow state, it doesn't even seem to be a part of glTF from what I understand

Does the glTF specification even have a way to signal that certain lights should have shadows enabled? If not, it doesn't sound very usable for game development where you can't be "all or nothing" in real world projects 🙁 

I suppose we could add support for a `-shadow` import hint and/or offer an import option to set omni/spot shadow range thresholds (where all lights larger than a certain range have shadows enabled), but that's not great to hear.
> Does the glTF specification even have a way

[No](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/schema/light.schema.json)

> I suppose we could add support for a -shadow import hint

I can't think of a reason not to
We shouldn't default to such a high range. Every scene i've worked on I've had to manually go reduce the range to something reasonable (oftentimes less than 10). 

With proper inverse square falloff, a light with a brightness of 1 won't be visible beyond 20 meters or so (even if the range is significantly higher than that). 

We used to calculate attenuation based on range and intensity. Now we don't touch attenuation because GLTF doesn't have a setting for it. This is a bit of a problem. Godot defaults to a linear falloff (attenuation = 1) so lights imported from GLTF are effectively imported with a linear falloff. 

If we assigned GLTF lights a proper inverse square falloff (attenuation = 2), then we could easily cap their range at 100 or so when no range is specified (and then scale up the range when there is more intensity). At that range, there would be no visible difference with an increased range. However, with a linear falloff, we need a much larger range before we can cap it. 

Technically, we are not compliant with the GLTF spec right now since GLTF lights are not using the inverse square law. 

I suggest we do the following:
1. Import lights with an attenuation of 2 when importing from GLTF
2. Default to a range of 100. If intensity is specified, but range isn't scale up range based on intensity (I suggest increasing by a factor of `clamp(intensity / 100.0, 1.0, 40.96)` as below 100 intensity a range of 100 is fine, then it should scale up linearly after that until a max of 4096)

Of course switching to inverse square falloff will technically break compatibility, but we are not compliant with the spec right now. So I think the breakage is justified. 

Fix patch:
diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
index 43a50e97c4bc..203307355a08 100644
--- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
+++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
@@ -2613,9 +2613,9 @@ void RenderForwardClustered::_render_shadow_pass(RID p_light, RID p_shadow_atlas
 			Rect2 atlas_rect_norm = atlas_rect;
 			atlas_rect_norm.position /= float(atlas_size);
 			atlas_rect_norm.size /= float(atlas_size);
-			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), false);
+			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, false);
 			atlas_rect_norm.position += Vector2(dual_paraboloid_offset) * atlas_rect_norm.size;
-			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), true);
+			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, true);
 
 			//restore transform so it can be properly used
 			light_storage->light_instance_set_shadow_transform(p_light, Projection(), light_storage->light_instance_get_base_transform(p_light), zfar, 0, 0, 0);
diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp
index 2589488d5a7f..eef29794c15d 100644
--- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp
+++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp
@@ -1368,9 +1368,9 @@ void RenderForwardMobile::_render_shadow_pass(RID p_light, RID p_shadow_atlas, i
 			Rect2 atlas_rect_norm = atlas_rect;
 			atlas_rect_norm.position /= float(atlas_size);
 			atlas_rect_norm.size /= float(atlas_size);
-			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), false);
+			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, false);
 			atlas_rect_norm.position += Vector2(dual_paraboloid_offset) * atlas_rect_norm.size;
-			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), true);
+			copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, true);
 
 			//restore transform so it can be properly used
 			light_storage->light_instance_set_shadow_transform(p_light, Projection(), light_storage->light_instance_get_base_transform(p_light), zfar, 0, 0, 0);
diff --git a/servers/rendering/renderer_scene_cull.cpp b/servers/rendering/renderer_scene_cull.cpp
index af17a3553584..2c1112f771b4 100644
--- a/servers/rendering/renderer_scene_cull.cpp
+++ b/servers/rendering/renderer_scene_cull.cpp
@@ -2558,8 +2558,9 @@ bool RendererSceneCull::_light_instance_update_shadow(Instance *p_instance, cons
 				}
 
 				real_t radius = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
+				real_t z_near = 0.005f;
 				Projection cm;
-				cm.set_perspective(90, 1, radius * 0.005f, radius);
+				cm.set_perspective(90, 1, z_near, radius);
 
 				for (int i = 0; i < 6; i++) {
 					RENDER_TIMESTAMP("Cull OmniLight3D Shadow Cube, Side " + itos(i));
@@ -2647,9 +2648,10 @@ bool RendererSceneCull::_light_instance_update_shadow(Instance *p_instance, cons
 
 			real_t radius = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
 			real_t angle = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
+			real_t z_near = 0.005f;
 
 			Projection cm;
-			cm.set_perspective(angle * 2.0, 1.0, 0.005f * radius, radius);
+			cm.set_perspective(angle * 2.0, 1.0, z_near, radius);
 
 			Vector<Plane> planes = cm.get_projection_planes(light_transform);
 


Test patch: