2021-02-24 10:58:31 +00:00
|
|
|
#version 450
|
2023-06-28 04:19:10 +00:00
|
|
|
#extension GL_GOOGLE_include_directive : enable
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2023-06-28 04:19:10 +00:00
|
|
|
#include "lighting.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2023-08-01 04:32:46 +00:00
|
|
|
layout (input_attachment_index = 0, set = 2, binding = 0) uniform subpassInput color;
|
|
|
|
layout (input_attachment_index = 1, set = 2, binding = 1) uniform subpassInput emission;
|
|
|
|
layout (input_attachment_index = 2, set = 2, binding = 2) uniform subpassInput normal;
|
|
|
|
layout (input_attachment_index = 3, set = 2, binding = 3) uniform subpassInput position;
|
2021-04-25 01:01:57 +00:00
|
|
|
|
2023-08-01 14:34:08 +00:00
|
|
|
layout (set = 3, binding = 0) uniform sampler2DArrayShadow shadowCascade[32];
|
|
|
|
layout (set = 3, binding = 0) uniform sampler2DArrayShadow shadowPlane[32];
|
|
|
|
layout (set = 3, binding = 0) uniform samplerCubeArrayShadow shadowCube[32];
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
layout (location = 0) out vec4 frag_color;
|
|
|
|
|
2023-08-01 14:34:08 +00:00
|
|
|
layout (constant_id = 0) const int ShadowType = ST_NONE;
|
|
|
|
|
2021-04-25 01:01:57 +00:00
|
|
|
float
|
2023-06-28 03:53:58 +00:00
|
|
|
spot_cone (LightData light, vec3 incoming)
|
2021-02-25 06:51:54 +00:00
|
|
|
{
|
2023-06-28 03:53:58 +00:00
|
|
|
vec3 dir = light.direction.xyz;
|
|
|
|
float cone = light.direction.w;
|
2022-05-04 23:40:02 +00:00
|
|
|
float spotdot = dot (incoming, dir);
|
2022-05-05 23:32:19 +00:00
|
|
|
return 1 - smoothstep (cone, .995 * cone + 0.005, spotdot);
|
2021-04-25 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
diffuse (vec3 incoming, vec3 normal)
|
|
|
|
{
|
2021-03-20 07:08:44 +00:00
|
|
|
float lightdot = dot (incoming, normal);
|
2021-04-25 01:01:57 +00:00
|
|
|
return clamp (lightdot, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2023-08-01 14:34:08 +00:00
|
|
|
shadow_cascade (sampler2DArrayShadow map, uint layer, uint mat_id, vec3 pos)
|
2021-04-24 01:40:39 +00:00
|
|
|
{
|
2021-04-25 01:01:57 +00:00
|
|
|
return 1;
|
2021-04-24 01:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 01:01:57 +00:00
|
|
|
float
|
2023-08-01 14:34:08 +00:00
|
|
|
shadow_plane (sampler2DArrayShadow map, uint layer, uint mat_id, vec3 pos)
|
2021-04-24 01:40:39 +00:00
|
|
|
{
|
2021-04-25 01:01:57 +00:00
|
|
|
return 1;
|
2021-04-24 01:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 01:01:57 +00:00
|
|
|
float
|
2023-08-01 14:34:08 +00:00
|
|
|
shadow_cube (samplerCubeArrayShadow map, uint layer, uint mat_id, vec3 pos)
|
2021-04-24 01:40:39 +00:00
|
|
|
{
|
2021-04-25 01:01:57 +00:00
|
|
|
return 1;
|
2021-04-24 01:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
void
|
|
|
|
main (void)
|
|
|
|
{
|
2023-06-28 03:53:58 +00:00
|
|
|
vec3 c = subpassLoad (color).rgb;
|
|
|
|
vec3 e = subpassLoad (emission).rgb;
|
2021-02-25 04:46:33 +00:00
|
|
|
vec3 n = subpassLoad (normal).rgb;
|
|
|
|
vec3 p = subpassLoad (position).rgb;
|
2021-03-20 07:08:44 +00:00
|
|
|
vec3 light = vec3 (0);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2023-06-28 03:53:58 +00:00
|
|
|
//vec3 minLight = vec3 (0);
|
|
|
|
for (int i = 0; i < lightCount; i++) {
|
2023-08-01 14:34:08 +00:00
|
|
|
uint id = lightIds[i];
|
|
|
|
LightData l = lights[id];
|
2023-06-28 03:53:58 +00:00
|
|
|
vec3 dir = l.position.xyz - l.position.w * p;
|
|
|
|
float r2 = dot (dir, dir);
|
|
|
|
vec4 a = l.attenuation;
|
|
|
|
|
|
|
|
if (l.position.w * a.w * a.w * r2 >= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vec4 r = vec4 (r2, sqrt(r2), 1, 0);
|
|
|
|
vec3 incoming = dir / r.y;
|
|
|
|
float I = (1 - a.w * r.y) / dot (a, r);
|
2022-05-10 02:15:52 +00:00
|
|
|
|
2023-08-01 14:34:08 +00:00
|
|
|
uint id_data = renderer[id].id_data;
|
|
|
|
uint mat_id = bitfieldExtract (id_data, 0, 13);
|
|
|
|
uint map_id = bitfieldExtract (id_data, 13, 5);
|
|
|
|
uint layer = bitfieldExtract (id_data, 18, 11);
|
|
|
|
if (ShadowType == ST_CASCADE) {
|
|
|
|
I *= shadow_cascade (shadowCascade[map_id], layer, mat_id, p);
|
|
|
|
} else if (ShadowType == ST_PLANE) {
|
|
|
|
I *= shadow_plane (shadowPlane[map_id], layer, mat_id, p);
|
|
|
|
} else if (ShadowType == ST_CUBE) {
|
|
|
|
I *= shadow_cube (shadowCube[map_id], layer, mat_id, p);
|
|
|
|
}
|
2022-05-10 02:15:52 +00:00
|
|
|
|
2023-06-28 03:53:58 +00:00
|
|
|
float namb = dot(l.direction.xyz, l.direction.xyz);
|
|
|
|
I *= spot_cone (l, incoming) * diffuse (incoming, n);
|
|
|
|
I = mix (1, I, namb);
|
2023-08-01 14:34:08 +00:00
|
|
|
vec4 col = l.color;
|
|
|
|
if (bitfieldExtract(id_data, 31, 1) == 0) {
|
|
|
|
col *= style[renderer[id].style];
|
|
|
|
}
|
|
|
|
light += I * col.w * col.xyz;
|
2023-06-28 03:53:58 +00:00
|
|
|
}
|
|
|
|
//light = max (light, minLight);
|
2022-05-10 02:15:52 +00:00
|
|
|
|
2023-06-28 04:19:10 +00:00
|
|
|
frag_color = vec4 (light, 1);
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|