Skip to content

Commit 930d9f3

Browse files
committed
Leave compatibility renderer unchanged
1 parent 60bd8d0 commit 930d9f3

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

drivers/gles3/rasterizer_scene_gles3.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,7 @@ void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, b
17411741
continue;
17421742
}
17431743

1744-
Vector3 camera_to_light = li->transform.origin - p_render_data->cam_transform.origin;
1745-
real_t sum_abs_cam_to_light = 1.0 + abs(camera_to_light.x) + abs(camera_to_light.y) + abs(camera_to_light.z); // Used to avoid overflows with length() when camera_to_light components exceed sqrt(MAX_REAL_T_VALUE)
1746-
const real_t distance = (camera_to_light / sum_abs_cam_to_light).length() * sum_abs_cam_to_light;
1744+
const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
17471745

17481746
if (light_storage->light_is_distance_fade_enabled(li->light)) {
17491747
const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
@@ -1766,9 +1764,7 @@ void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, b
17661764
continue;
17671765
}
17681766

1769-
Vector3 camera_to_light = li->transform.origin - p_render_data->cam_transform.origin;
1770-
real_t sum_abs_cam_to_light = abs(camera_to_light.x) + abs(camera_to_light.y) + abs(camera_to_light.z);
1771-
const real_t distance = (camera_to_light / sum_abs_cam_to_light).length() * sum_abs_cam_to_light;
1767+
const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
17721768

17731769
if (light_storage->light_is_distance_fade_enabled(li->light)) {
17741770
const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);

drivers/gles3/shaders/scene.glsl

+12-17
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,11 @@ float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
386386
void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness,
387387
inout vec3 diffuse_light, inout vec3 specular_light) {
388388
vec3 light_rel_vec = omni_lights[idx].position - vertex;
389-
float sum_abs_rel_vec = 1.0 + abs(light_rel_vec.x) + abs(light_rel_vec.y) + abs(light_rel_vec.z); // Used to avoid overflows with length() when light_rel_vec components exceed sqrt(MAX_FLOAT_VALUE)
390-
float light_length = length(light_rel_vec / sum_abs_rel_vec) * sum_abs_rel_vec;
389+
float light_length = length(light_rel_vec);
391390
float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
392391
vec3 color = omni_lights[idx].color * omni_attenuation; // No light shaders here, so combine.
393392

394-
light_compute(normal, light_rel_vec / light_length, eye_vec, color, false, roughness,
393+
light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
395394
diffuse_light,
396395
specular_light);
397396
}
@@ -402,19 +401,18 @@ void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float
402401
inout vec3 diffuse_light,
403402
inout vec3 specular_light) {
404403
vec3 light_rel_vec = spot_lights[idx].position - vertex;
405-
float sum_abs_rel_vec = 1.0 + abs(light_rel_vec.x) + abs(light_rel_vec.y) + abs(light_rel_vec.z); // Used to avoid overflows with length() when light_rel_vec components exceed sqrt(MAX_FLOAT_VALUE)
406-
float light_length = length(light_rel_vec / sum_abs_rel_vec) * sum_abs_rel_vec;
404+
float light_length = length(light_rel_vec);
407405
float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
408406
vec3 spot_dir = spot_lights[idx].direction;
409-
float scos = max(dot(-light_rel_vec / light_length, spot_dir), spot_lights[idx].cone_angle);
407+
float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
410408
float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
411409

412410
mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
413411
spot_attenuation *= 1.0 - pow(spot_rim, cone_attenuation);
414412

415413
vec3 color = spot_lights[idx].color * spot_attenuation;
416414

417-
light_compute(normal, light_rel_vec / light_length, eye_vec, color, false, roughness,
415+
light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
418416
diffuse_light, specular_light);
419417
}
420418
#endif // !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
@@ -666,9 +664,8 @@ void main() {
666664
#if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
667665
// Apply normal bias at draw time to avoid issues with scaling non-fused geometry.
668666
vec3 light_rel_vec = positional_shadows[positional_shadow_index].light_position - vertex_interp;
669-
float sum_abs_rel_vec = 1.0 + abs(light_rel_vec.x) + abs(light_rel_vec.y) + abs(light_rel_vec.z); // Used to avoid overflows with length() when light_rel_vec components exceed sqrt(MAX_FLOAT_VALUE)
670-
float light_length = length(light_rel_vec / sum_abs_rel_vec) * sum_abs_rel_vec;
671-
float aNdotL = abs(dot(normalize(normal_interp), light_rel_vec / light_length));
667+
float light_length = length(light_rel_vec);
668+
float aNdotL = abs(dot(normalize(normal_interp), normalize(light_rel_vec)));
672669
vec3 normal_offset = (1.0 - aNdotL) * positional_shadows[positional_shadow_index].shadow_normal_bias * light_length * normal_interp;
673670

674671
#ifdef ADDITIVE_SPOT
@@ -1461,8 +1458,7 @@ void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f
14611458
#endif
14621459
inout vec3 diffuse_light, inout vec3 specular_light) {
14631460
vec3 light_rel_vec = omni_lights[idx].position - vertex;
1464-
float sum_abs_rel_vec = 1.0 + abs(light_rel_vec.x) + abs(light_rel_vec.y) + abs(light_rel_vec.z); // Used to avoid overflows with length() when light_rel_vec components exceed sqrt(MAX_FLOAT_VALUE)
1465-
float light_length = length(light_rel_vec / sum_abs_rel_vec) * sum_abs_rel_vec;
1461+
float light_length = length(light_rel_vec);
14661462
float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
14671463
vec3 color = omni_lights[idx].color;
14681464
float size_A = 0.0;
@@ -1474,7 +1470,7 @@ void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f
14741470

14751471
omni_attenuation *= shadow;
14761472

1477-
light_compute(normal, light_rel_vec / light_length, eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, screen_uv,
1473+
light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, screen_uv,
14781474
#ifdef LIGHT_BACKLIGHT_USED
14791475
backlight,
14801476
#endif
@@ -1510,11 +1506,10 @@ void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f
15101506
inout vec3 specular_light) {
15111507

15121508
vec3 light_rel_vec = spot_lights[idx].position - vertex;
1513-
float sum_abs_rel_vec = 1.0 + abs(light_rel_vec.x) + abs(light_rel_vec.y) + abs(light_rel_vec.z); // Used to avoid overflows with length() when light_rel_vec components exceed sqrt(MAX_FLOAT_VALUE)
1514-
float light_length = length(light_rel_vec / sum_abs_rel_vec) * sum_abs_rel_vec;
1509+
float light_length = length(light_rel_vec);
15151510
float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
15161511
vec3 spot_dir = spot_lights[idx].direction;
1517-
float scos = max(dot(-light_rel_vec / light_length, spot_dir), spot_lights[idx].cone_angle);
1512+
float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
15181513
float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
15191514

15201515
mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
@@ -1531,7 +1526,7 @@ void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f
15311526

15321527
spot_attenuation *= shadow;
15331528

1534-
light_compute(normal, light_rel_vec / light_length, eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, screen_uv,
1529+
light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, screen_uv,
15351530
#ifdef LIGHT_BACKLIGHT_USED
15361531
backlight,
15371532
#endif

0 commit comments

Comments
 (0)