add shadow penumbra

main
Dominik Madarász 2024-09-02 17:00:31 +02:00
parent 116c55cf22
commit 301a21a5e1
9 changed files with 21 additions and 4 deletions

View File

@ -1167,6 +1167,7 @@ typedef struct light_t {
float shadow_bias;
float normal_bias;
float shadow_softness;
float penumbra_size;
bool cached;
bool processed_shadows;
} light_t;

View File

@ -109,7 +109,7 @@ int main(int argc, char** argv) {
enum {
POINT, SPOT, DIR, ALL
};
static unsigned mode = DIR;
static unsigned mode = SPOT;
if (!ui_active()) {
if (input_down(KEY_1)) mode = POINT;

View File

@ -31,6 +31,7 @@ struct light_t {
float min_variance;
float variance_transition;
float shadow_softness;
float penumbra_size;
};
const int LIGHT_DIRECTIONAL = 0;

View File

@ -23,9 +23,12 @@ vec2 shadow_vsm_variance(vec3 dir, int light_index, float distance, float min_va
return vec2(linstep(variance_transition, 1.0, variance / (variance + d * d)), moments.x);
}
float shadow_vsm(float distance, vec3 dir, int light_index, float min_variance, float variance_transition, float shadow_softness_raw) {
distance = distance / 200;
float shadow_vsm(float distance, vec3 dir, int light_index, float min_variance, float variance_transition, float shadow_softness_raw, float penumbra_size) {
float clamped_distance = clamp(distance, 0.0, 200.0);
float shadow_softness = shadow_softness_raw * 10.0;
shadow_softness = mix(shadow_softness, distance * 10.0, penumbra_size);
distance = distance / 200;
// Get the offset coordinates
ivec3 ofs_coord = ivec3(0);
@ -207,7 +210,7 @@ vec4 shadowmap(int idx, in vec4 peye, in vec4 neye) {
vec3 light_pos = (view * vec4(light.pos, 1.0)).xyz;
vec3 dir = light_pos - fragment;
vec4 sc = inv_view * vec4(dir, 0.0);
shadowFactor = shadow_vsm(length(dir), -sc.xyz, idx, light.min_variance, light.variance_transition, light.shadow_softness);
shadowFactor = shadow_vsm(length(dir), -sc.xyz, idx, light.min_variance, light.variance_transition, light.shadow_softness, light.penumbra_size);
}
}

View File

@ -17292,6 +17292,7 @@ typedef struct light_t {
float shadow_bias; //< CSM
float normal_bias; //< CSM
float shadow_softness;
float penumbra_size;
// internals
bool cached; //< used by scene to invalidate cached light data
@ -383306,6 +383307,7 @@ light_t light() {
l.shadow_bias = 0.15f;
l.normal_bias = 0.05f;
l.shadow_softness = 7.0f;
l.penumbra_size = 0.0f;
l.min_variance = 0.00002f;
l.variance_transition = 0.2f;
return l;
@ -383389,6 +383391,7 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].shadow_bias", i), lv[i].shadow_bias);
shader_float(va("u_lights[%d].normal_bias", i), lv[i].normal_bias);
shader_float(va("u_lights[%d].shadow_softness", i), lv[i].shadow_softness);
shader_float(va("u_lights[%d].penumbra_size", i), lv[i].penumbra_size);
shader_float(va("u_lights[%d].min_variance", i), lv[i].min_variance);
shader_float(va("u_lights[%d].variance_transition", i), lv[i].variance_transition);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
@ -383417,6 +383420,7 @@ void ui_light(light_t *l) {
ui_float_("Shadow Bias", &l->shadow_bias, 0.00005);
ui_float_("Normal Bias", &l->normal_bias, 0.00005);
ui_float_("Shadow Softness", &l->shadow_softness, 0.5);
ui_float_("Penumbra Size", &l->penumbra_size, 0.5f);
ui_float_("Min Variance", &l->min_variance, 0.00005);
ui_float_("Variance Transition", &l->variance_transition, 0.0005);
}

View File

@ -1487,6 +1487,7 @@ light_t light() {
l.shadow_bias = 0.15f;
l.normal_bias = 0.05f;
l.shadow_softness = 7.0f;
l.penumbra_size = 0.0f;
l.min_variance = 0.00002f;
l.variance_transition = 0.2f;
return l;
@ -1570,6 +1571,7 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].shadow_bias", i), lv[i].shadow_bias);
shader_float(va("u_lights[%d].normal_bias", i), lv[i].normal_bias);
shader_float(va("u_lights[%d].shadow_softness", i), lv[i].shadow_softness);
shader_float(va("u_lights[%d].penumbra_size", i), lv[i].penumbra_size);
shader_float(va("u_lights[%d].min_variance", i), lv[i].min_variance);
shader_float(va("u_lights[%d].variance_transition", i), lv[i].variance_transition);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
@ -1598,6 +1600,7 @@ void ui_light(light_t *l) {
ui_float_("Shadow Bias", &l->shadow_bias, 0.00005);
ui_float_("Normal Bias", &l->normal_bias, 0.00005);
ui_float_("Shadow Softness", &l->shadow_softness, 0.5);
ui_float_("Penumbra Size", &l->penumbra_size, 0.5f);
ui_float_("Min Variance", &l->min_variance, 0.00005);
ui_float_("Variance Transition", &l->variance_transition, 0.0005);
}

View File

@ -324,6 +324,7 @@ typedef struct light_t {
float shadow_bias; //< CSM
float normal_bias; //< CSM
float shadow_softness;
float penumbra_size;
// internals
bool cached; //< used by scene to invalidate cached light data

View File

@ -18341,6 +18341,7 @@ light_t light() {
l.shadow_bias = 0.15f;
l.normal_bias = 0.05f;
l.shadow_softness = 7.0f;
l.penumbra_size = 0.0f;
l.min_variance = 0.00002f;
l.variance_transition = 0.2f;
return l;
@ -18424,6 +18425,7 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].shadow_bias", i), lv[i].shadow_bias);
shader_float(va("u_lights[%d].normal_bias", i), lv[i].normal_bias);
shader_float(va("u_lights[%d].shadow_softness", i), lv[i].shadow_softness);
shader_float(va("u_lights[%d].penumbra_size", i), lv[i].penumbra_size);
shader_float(va("u_lights[%d].min_variance", i), lv[i].min_variance);
shader_float(va("u_lights[%d].variance_transition", i), lv[i].variance_transition);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
@ -18452,6 +18454,7 @@ void ui_light(light_t *l) {
ui_float_("Shadow Bias", &l->shadow_bias, 0.00005);
ui_float_("Normal Bias", &l->normal_bias, 0.00005);
ui_float_("Shadow Softness", &l->shadow_softness, 0.5);
ui_float_("Penumbra Size", &l->penumbra_size, 0.5f);
ui_float_("Min Variance", &l->min_variance, 0.00005);
ui_float_("Variance Transition", &l->variance_transition, 0.0005);
}

View File

@ -3359,6 +3359,7 @@ typedef struct light_t {
float shadow_bias; //< CSM
float normal_bias; //< CSM
float shadow_softness;
float penumbra_size;
// internals
bool cached; //< used by scene to invalidate cached light data