2021-02-24 10:58:31 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput depth;
|
|
|
|
layout (input_attachment_index = 1, set = 0, binding = 1) uniform subpassInput color;
|
|
|
|
layout (input_attachment_index = 2, set = 0, binding = 2) uniform subpassInput normal;
|
2021-02-25 04:46:33 +00:00
|
|
|
layout (input_attachment_index = 3, set = 0, binding = 3) uniform subpassInput position;
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
struct LightData {
|
|
|
|
vec3 color;
|
2021-03-22 04:13:26 +00:00
|
|
|
int data;// bits 0-6: intensity key (however, values 0-66)
|
2021-02-24 10:58:31 +00:00
|
|
|
vec3 position;
|
2021-03-03 09:14:16 +00:00
|
|
|
float radius;
|
2021-02-24 10:58:31 +00:00
|
|
|
vec3 direction;
|
|
|
|
float cone;
|
|
|
|
};
|
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
layout (constant_id = 0) const int MaxLights = 128;
|
|
|
|
layout (set = 1, binding = 0) uniform Lights {
|
2021-03-22 04:13:26 +00:00
|
|
|
vec4 intensity[16]; // 64 floats
|
|
|
|
vec3 intensity2;
|
2021-02-24 10:58:31 +00:00
|
|
|
int lightCount;
|
|
|
|
LightData lights[MaxLights];
|
2021-02-25 06:51:54 +00:00
|
|
|
};
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
layout (location = 0) out vec4 frag_color;
|
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
vec3
|
|
|
|
calc_light (LightData light, vec3 position, vec3 normal)
|
|
|
|
{
|
|
|
|
vec3 dist = light.position - position;
|
2021-02-25 07:11:47 +00:00
|
|
|
float d = sqrt (dot (dist, dist));
|
2021-03-20 07:08:44 +00:00
|
|
|
vec3 incoming = dist / d;
|
|
|
|
float spotdot = dot (incoming, light.direction);
|
|
|
|
float lightdot = dot (incoming, normal);
|
2021-02-25 06:51:54 +00:00
|
|
|
float r = light.radius;
|
|
|
|
|
2021-03-22 04:13:26 +00:00
|
|
|
int style = light.data & 0x7f;
|
|
|
|
// deliberate array index error: access intensity2 as well
|
|
|
|
float i = intensity[style / 4][style % 4];
|
|
|
|
i *= step (d, r) * clamp (lightdot, 0, 1);
|
|
|
|
i *= smoothstep (spotdot, 1 - (1 - spotdot) * 0.995, light.cone);
|
|
|
|
return light.color * i * (r - d);
|
2021-02-25 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
void
|
|
|
|
main (void)
|
|
|
|
{
|
2021-02-25 06:51:54 +00:00
|
|
|
//float d = subpassLoad (depth).r;
|
2021-02-25 04:46:33 +00:00
|
|
|
vec3 c = subpassLoad (color).rgb;
|
|
|
|
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
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
if (MaxLights > 0) {
|
|
|
|
for (int i = 0; i < lightCount; i++) {
|
|
|
|
light += calc_light (lights[i], p, n);
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 04:13:26 +00:00
|
|
|
frag_color = vec4 (c * light, 1);
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|