17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-29 23:17:30 +01:00

Light the cab with the sun coming in through its windows

This commit is contained in:
2026-10-28 12:19:43 +01:00
parent 1447dcf075
commit e8a90507f0
28 changed files with 342 additions and 29 deletions

View File

@@ -11,7 +11,10 @@ layout(location = 3) in vec4 f_pos;
layout(binding = 0) uniform sampler2D diffuse;
// the cube shadow map of the cab lamp, see gl::LOCAL_SHADOW_TEX
layout(binding = LOCAL_SHADOW_TEX) uniform samplerCubeArrayShadow probe_shadowmap;
// the probe drawn last, where the windows of the cab are, see gl::CAB_PROBE_MASK_TEX
layout(binding = CAB_PROBE_MASK_TEX) uniform samplerCube cab_probe_mask;
#include "weather_common.glsl"
#include "cabprobe.glsl"
void main()

View File

@@ -2,14 +2,19 @@
// towards the middle of its box lit by the cab lamp alone, with the cube shadow map of the lamp (and what glows in it, the lights
// of the desk), and its albedo, each with 1 in the alpha: the probe is cleared to nothing, so the alpha of a direction tells how
// much of it the cab fills, and both are premultiplied by it. the light of the cab lamp is the one the shading gives it
// (calc_local_light() in light_common.glsl); the ambient light and the sun are left out, the cab gets those as before. the
// parameters of the lamp are in the block of the scene (gl::scene_ubs::cab_probe_lamp), in the space of the probe
// (calc_local_light() in light_common.glsl). the sun (the moon) lights the cab where it comes in through the windows
// (gl::scene_ubs::cab_probe_sun), so the light which bounces around the cab holds it; the ambient light is left out, the cab gets it as
// before. the parameters of the lamp and of the sun are in the block of the scene (gl::scene_ubs::cab_probe_lamp, cab_probe_sun), in
// the space of the probe. the including shader declares cab_probe_mask (CAB_PROBE_MASK_TEX) and includes weather_common.glsl
layout(location = 0) out vec4 out_light;
layout(location = 1) out vec4 out_albedo;
// closest distance (squared) the light of the lamp is computed for, as LOCAL_LIGHT_MIN_DISTANCE2 of light_common.glsl
const float PROBE_LIGHT_MIN_DISTANCE2 = 0.01;
// the light of the sun on a diffuse surface, as SUN_DIFFUSE_SCALE and SUN_NDOTL_SHARPNESS of light_common.glsl
const float PROBE_SUN_SCALE = 1.5;
const float PROBE_SUN_NDOTL_SHARPNESS = 1.25;
// the diffuse colour of the submodel or shape, as material_diffuse() of light_common.glsl
vec3 probe_material_diffuse()
@@ -35,6 +40,20 @@ float probe_lamp_shadow(vec3 position, vec3 normal)
return texture(probe_shadowmap, vec4(position, weights.x), reference);
}
// share of the sun which reaches a point of the space of the probe through the windows of the cab: the ray towards the sun is taken
// to the box of the cab, and the probe drawn last (the mask) tells how much of the direction of the point it leaves the box at the cab
// fills, seen from the middle of the box. what is in the way inside the cab (the desk, the seats) holds the sun back only where it
// hides that point from the middle
float probe_sun_through_windows(vec3 position, vec3 tosun)
{
vec3 box = cab_probe_sun_box.xyz;
position = clamp(position, -box, box);
vec3 invdir = (step(0.0, tosun) * 2.0 - 1.0) / max(abs(tosun), vec3(1e-5));
vec3 far = max((box - position) * invdir, (-box - position) * invdir);
float reach = max(min(min(far.x, far.y), far.z), 0.0);
return 1.0 - textureLod(cab_probe_mask, position + tosun * reach, 0.0).a;
}
void probe_shade(vec4 tex_color)
{
vec3 albedo = tex_color.rgb * probe_material_diffuse();
@@ -56,6 +75,12 @@ void probe_shade(vec4 tex_color)
atten *= 1.0 - probe_lamp_shadow(-tolight, normal);
light += cab_probe_lamp_color.rgb * (NdotL * atten);
}
if (cab_probe_sun.w > 0.0)
{
float NdotL = max(dot(normal, cab_probe_sun.xyz), 0.0);
if (NdotL > 0.0)
light += cab_probe_sun_color.rgb * (PROBE_SUN_SCALE * pow(NdotL, PROBE_SUN_NDOTL_SHARPNESS) * probe_sun_through_windows(position, cab_probe_sun.xyz) * cloud_shadow(cab_probe_sun_world.xyz));
}
out_light = vec4(albedo * light, 1.0);
out_albedo = vec4(albedo, 1.0);
}

View File

@@ -12,7 +12,10 @@ layout(location = 3) in vec4 f_pos;
layout(binding = 0) uniform sampler2D diffuse;
// the cube shadow map of the cab lamp, see gl::LOCAL_SHADOW_TEX
layout(binding = LOCAL_SHADOW_TEX) uniform samplerCubeArrayShadow probe_shadowmap;
// the probe drawn last, where the windows of the cab are, see gl::CAB_PROBE_MASK_TEX
layout(binding = CAB_PROBE_MASK_TEX) uniform samplerCube cab_probe_mask;
#include "weather_common.glsl"
#include "cabprobe.glsl"
void main()

View File

@@ -111,6 +111,7 @@
#define CAB_PROBE_ALBEDO_TEX 28
#define SKY_OCCLUSION_HEIGHT_TEX 29
#define SKY_OCCLUSION_TEX 30
#define CAB_PROBE_MASK_TEX 31
#define CAB_PROBE_SSBO_BINDING 17
#define CAB_PROBE_SH_SIZE 16
#define CAB_PROBE_SH_GROUP_SIZE 64
@@ -247,7 +248,7 @@ struct model_draw_s
float fog_density;
float alpha_mult;
float shadow_tone;
// 1 for the parts of the occupied cab, see gl::model_ubs
// 1 for the parts of the occupied cab, 2 for the low poly interiors, see gl::model_ubs
float cab_interior;
};
@@ -289,7 +290,7 @@ layout(std140, binding = MODEL_UBO_BINDING) uniform model_ubo
float fog_density;
float alpha_mult;
float shadow_tone;
// 1 for the parts of the occupied cab, see gl::model_ubs
// 1 for the parts of the occupied cab, 2 for the low poly interiors, see gl::model_ubs
float cab_interior;
};
#endif
@@ -335,6 +336,11 @@ layout(std140, binding = SCENE_UBO_BINDING) uniform scene_ubo
vec4 sky_occlusion_origin;
vec4 sky_occlusion_params;
vec4 sky_occlusion_center;
// the sun coming in through the windows of the cab, for the drawing of its probe, see gl::scene_ubs
vec4 cab_probe_sun;
vec4 cab_probe_sun_color;
vec4 cab_probe_sun_box;
vec4 cab_probe_sun_world;
};
#ifdef STAGE_VERTEX

View File

@@ -782,7 +782,7 @@ float ao_specular(float visibility, float NdotV, float roughness)
// whether the fragment is a part of the occupied cab with its probe (gl::scene_ubs::cab_probe_box, gl::model_ubs::cab_interior)
bool cab_probe_active()
{
return (cab_probe_box.w > 0.0) && (cab_interior > 0.5);
return (cab_probe_box.w > 0.0) && (cab_interior > 0.5) && (cab_interior < 1.5);
}
// the light of the lamps the cab reflects which reaches a surface of the cab facing the view space Fragnormal, in the units of the
@@ -887,6 +887,10 @@ vec3 shade_surface(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float ref
// bounces off surfaces of the albedo) and the reflection of the surroundings; not the light of the sun and the lamps, nor the
// reflection of the scene, which the rays find as it is
float occlusion = ambient_occlusion();
// in the cab and the low poly interiors walls and things are near all around, and the occlusion of the view darkens the ambient
// light a lot; they take a share of it (gfx.postfx.ao.cab), the light which bounces inside them comes from the probe or below
if (cab_interior > 0.5)
occlusion = mix(1.0, occlusion, ao_params.z);
vec3 diffuse_occlusion = vec3(1.0);
float specular_occlusion = 1.0;
if (occlusion < 1.0)
@@ -971,6 +975,10 @@ vec3 shade_surface(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float ref
// the reflection of the surroundings, before the light of the sun and the lamps joins the ambient light
vec3 env_reflection = fragcolor * env_spec * ambient_share * specular_occlusion
+ (scenecolor.rgb + envcolor * ((1.0 - scenecolor.a) * cubemap_shadow * specular_occlusion)) * cubemap_spec * cubemap_share;
// the sun coming in through the windows and bouncing around a low poly interior, or the cab without its probe (which gives that
// light from where the sun falls in the cab): a share of the light of the sun outside joins the ambient light (gfx.interior.bounce)
if ((cab_interior > 0.5) && (false == cabprobe))
fragcolor += sunlight * env_params.z;
// the occlusion of the ambient light, before the light of the sun and the lamps joins it
fragcolor *= diffuse_occlusion;

View File

@@ -10,7 +10,10 @@ layout(location = 3) in vec4 f_pos;
layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 0) uniform sampler2D diffuse;
// the cube shadow map of the cab lamp
layout(set = PASS_SET, binding = LOCAL_SHADOW_TEX) uniform samplerCubeArrayShadow probe_shadowmap;
// the probe drawn last, where the windows of the cab are
layout(set = PASS_SET, binding = CAB_PROBE_MASK_TEX) uniform samplerCube cab_probe_mask;
#include "weather_common.glsl"
#include "cabprobe.glsl"
void main()

View File

@@ -2,14 +2,19 @@
// towards the middle of its box lit by the cab lamp alone, with the cube shadow map of the lamp (and what glows in it, the lights
// of the desk), and its albedo, each with 1 in the alpha: the probe is cleared to nothing, so the alpha of a direction tells how
// much of it the cab fills, and both are premultiplied by it. the light of the cab lamp is the one the shading gives it
// (calc_local_light() in light_common.glsl); the ambient light and the sun are left out, the cab gets those as before. the
// parameters of the lamp are in the block of the scene (gl::scene_ubs::cab_probe_lamp), in the space of the probe
// (calc_local_light() in light_common.glsl). the sun (the moon) lights the cab where it comes in through the windows
// (gl::scene_ubs::cab_probe_sun), so the light which bounces around the cab holds it; the ambient light is left out, the cab gets it as
// before. the parameters of the lamp and of the sun are in the block of the scene (gl::scene_ubs::cab_probe_lamp, cab_probe_sun), in
// the space of the probe. the including shader declares cab_probe_mask (CAB_PROBE_MASK_TEX) and includes weather_common.glsl
layout(location = 0) out vec4 out_light;
layout(location = 1) out vec4 out_albedo;
// closest distance (squared) the light of the lamp is computed for, as LOCAL_LIGHT_MIN_DISTANCE2 of light_common.glsl
const float PROBE_LIGHT_MIN_DISTANCE2 = 0.01;
// the light of the sun on a diffuse surface, as SUN_DIFFUSE_SCALE and SUN_NDOTL_SHARPNESS of light_common.glsl
const float PROBE_SUN_SCALE = 1.5;
const float PROBE_SUN_NDOTL_SHARPNESS = 1.25;
// the diffuse colour of the submodel or shape, as material_diffuse() of light_common.glsl
vec3 probe_material_diffuse()
@@ -35,6 +40,20 @@ float probe_lamp_shadow(vec3 position, vec3 normal)
return texture(probe_shadowmap, vec4(position, weights.x), reference);
}
// share of the sun which reaches a point of the space of the probe through the windows of the cab: the ray towards the sun is taken
// to the box of the cab, and the probe drawn last (the mask) tells how much of the direction of the point it leaves the box at the cab
// fills, seen from the middle of the box. what is in the way inside the cab (the desk, the seats) holds the sun back only where it
// hides that point from the middle
float probe_sun_through_windows(vec3 position, vec3 tosun)
{
vec3 box = cab_probe_sun_box.xyz;
position = clamp(position, -box, box);
vec3 invdir = (step(0.0, tosun) * 2.0 - 1.0) / max(abs(tosun), vec3(1e-5));
vec3 far = max((box - position) * invdir, (-box - position) * invdir);
float reach = max(min(min(far.x, far.y), far.z), 0.0);
return 1.0 - textureLod(cab_probe_mask, position + tosun * reach, 0.0).a;
}
void probe_shade(vec4 tex_color)
{
vec3 albedo = tex_color.rgb * probe_material_diffuse();
@@ -56,6 +75,12 @@ void probe_shade(vec4 tex_color)
atten *= 1.0 - probe_lamp_shadow(-tolight, normal);
light += cab_probe_lamp_color.rgb * (NdotL * atten);
}
if (cab_probe_sun.w > 0.0)
{
float NdotL = max(dot(normal, cab_probe_sun.xyz), 0.0);
if (NdotL > 0.0)
light += cab_probe_sun_color.rgb * (PROBE_SUN_SCALE * pow(NdotL, PROBE_SUN_NDOTL_SHARPNESS) * probe_sun_through_windows(position, cab_probe_sun.xyz) * cloud_shadow(cab_probe_sun_world.xyz));
}
out_light = vec4(albedo * light, 1.0);
out_albedo = vec4(albedo, 1.0);
}

View File

@@ -11,7 +11,10 @@ layout(location = 3) in vec4 f_pos;
layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 0) uniform sampler2D diffuse;
// the cube shadow map of the cab lamp
layout(set = PASS_SET, binding = LOCAL_SHADOW_TEX) uniform samplerCubeArrayShadow probe_shadowmap;
// the probe drawn last, where the windows of the cab are
layout(set = PASS_SET, binding = CAB_PROBE_MASK_TEX) uniform samplerCube cab_probe_mask;
#include "weather_common.glsl"
#include "cabprobe.glsl"
void main()

View File

@@ -67,7 +67,8 @@
#define CAB_PROBE_SSBO 30
#define SKY_OCCLUSION_HEIGHT_TEX 31
#define SKY_OCCLUSION_TEX 32
#define PASS_BINDING_COUNT 33
#define CAB_PROBE_MASK_TEX 33
#define PASS_BINDING_COUNT 34
#define MATERIAL_TEXTURE_FIRST 0
#define CL_CLUSTER_UBO 0
#define CL_AABB_SSBO 1
@@ -302,6 +303,11 @@ layout(std140, set = PASS_SET, binding = SCENE_UBO) uniform scene_ubo
vec4 sky_occlusion_origin;
vec4 sky_occlusion_params;
vec4 sky_occlusion_center;
// the sun coming in through the windows of the cab, for the drawing of its probe, see gl::scene_ubs
vec4 cab_probe_sun;
vec4 cab_probe_sun_color;
vec4 cab_probe_sun_box;
vec4 cab_probe_sun_world;
};
#ifndef LAYOUT_COMPUTE
@@ -319,7 +325,7 @@ struct model_draw_s
float fog_density;
float alpha_mult;
float shadow_tone;
// 1 for the parts of the occupied cab, see gl::model_ubs
// 1 for the parts of the occupied cab, 2 for the low poly interiors, see gl::model_ubs
float cab_interior;
};

View File

@@ -795,7 +795,7 @@ float ao_specular(float visibility, float NdotV, float roughness)
// whether the fragment is a part of the occupied cab with its probe (gl::scene_ubs::cab_probe_box, gl::model_ubs::cab_interior)
bool cab_probe_active()
{
return (cab_probe_box.w > 0.0) && (cab_interior > 0.5);
return (cab_probe_box.w > 0.0) && (cab_interior > 0.5) && (cab_interior < 1.5);
}
// the light of the lamps the cab reflects which reaches a surface of the cab facing the view space Fragnormal, in the units of the
@@ -900,6 +900,10 @@ vec3 shade_surface(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float ref
// bounces off surfaces of the albedo) and the reflection of the surroundings; not the light of the sun and the lamps, nor the
// reflection of the scene, which the rays find as it is
float occlusion = ambient_occlusion();
// in the cab and the low poly interiors walls and things are near all around, and the occlusion of the view darkens the ambient
// light a lot; they take a share of it (gfx.postfx.ao.cab), the light which bounces inside them comes from the probe or below
if (cab_interior > 0.5)
occlusion = mix(1.0, occlusion, ao_params.z);
vec3 diffuse_occlusion = vec3(1.0);
float specular_occlusion = 1.0;
if (occlusion < 1.0)
@@ -985,6 +989,10 @@ vec3 shade_surface(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float ref
// the reflection of the surroundings, before the light of the sun and the lamps joins the ambient light
vec3 env_reflection = fragcolor * env_spec * ambient_share * specular_occlusion
+ (scenecolor.rgb + envcolor * ((1.0 - scenecolor.a) * cubemap_shadow * specular_occlusion)) * cubemap_spec * cubemap_share;
// the sun coming in through the windows and bouncing around a low poly interior, or the cab without its probe (which gives that
// light from where the sun falls in the cab): a share of the light of the sun outside joins the ambient light (gfx.interior.bounce)
if ((cab_interior > 0.5) && (false == cabprobe))
fragcolor += sunlight * env_params.z;
// the occlusion of the ambient light, before the light of the sun and the lamps joins it
fragcolor *= diffuse_occlusion;