nngn
Loading...
Searching...
No Matches
light_frag.h
Go to the documentation of this file.
1#include "../const.h"
2
3LAYOUT2(set = 0, binding = 1) uniform sampler2DArray lights_shadow_map;
4#ifdef HAS_CUBE_ARRAY
5LAYOUT2(set = 0, binding = 2)
6 uniform samplerCubeArray lights_shadow_cube;
7#else
8LAYOUT2(set = 0, binding = 2)
9 uniform samplerCube lights_shadow_cube[NNGN_MAX_LIGHTS];
10#endif
11
12LAYOUT(location = 1) in vec3 frag_pos;
13LAYOUT(location = 2) in vec3 frag_pos_bias;
14LAYOUT(location = 3) flat in vec3 frag_normal;
15LAYOUT(location = 4) in vec3 frag_pos_light[NNGN_MAX_LIGHTS];
16
17float attenuation(uint i, float d) {
18 return 1.0f / (
19 lights.point.att_cutoff[i][0]
20 + lights.point.att_cutoff[i][1] * d
21 + lights.point.att_cutoff[i][2] * d * d);
22}
23
24float spot(uint i, vec3 d) {
25 float inner = lights.point.att_cutoff[i].w;
26 if(inner == 0.0)
27 return 1.0;
28 const float penumbra = 0.1;
29 float outer = inner - penumbra;
30 float a = dot(-d, lights.point.dir[i].xyz);
31 return a > outer ? clamp((a - outer) / penumbra, 0.0, 1.0) : 0.0;
32}
33
34float light_spec(float spec, vec3 dir, vec3 view_dir) {
35 return spec * pow(
36 max(dot(view_dir, reflect(dir, frag_normal)), 0.0f),
37 32.0f);
38}
39
40float sample_shadow(uint i, vec2 v) {
41 return texture(lights_shadow_map, vec3(v, i)).r;
42}
43
45#ifdef HAS_CUBE_ARRAY
46 return texture(lights_shadow_cube, vec4(v, i)).r;
47#else
48 switch(i) {
49 case 0u: return texture(lights_shadow_cube[0], v).r;
50 case 1u: return texture(lights_shadow_cube[1], v).r;
51 case 2u: return texture(lights_shadow_cube[2], v).r;
52 case 3u: return texture(lights_shadow_cube[3], v).r;
53 case 4u: return texture(lights_shadow_cube[4], v).r;
54 case 5u: return texture(lights_shadow_cube[5], v).r;
55 case 6u: return texture(lights_shadow_cube[6], v).r;
56 case 7u: return texture(lights_shadow_cube[7], v).r;
57 };
58#endif
59}
60
61bool in_shadow(uint i) {
62 if((lights.flags & NNGN_SHADOWS_ENABLED_BIT) == 0u)
63 return false;
64 vec3 p = frag_pos_light[i] * 0.5 + 0.5;
65 return p.z > sample_shadow(i, p.xy);
66}
67
68bool in_shadow_point(uint i) {
69 if((lights.flags & NNGN_SHADOWS_ENABLED_BIT) == 0u)
70 return false;
71 vec3 dir = frag_pos_bias - lights.point.pos[i].xyz;
72 vec3 abs_dir = abs(dir);
73 float z = max(abs_dir.x, max(abs_dir.y, abs_dir.z));
74 z = lights.depth_transform0 - lights.depth_transform1 / z;
75 return z > sample_shadow_point(i, dir);
76}
77
79 vec3 view_dir = normalize(lights.view_pos - frag_pos);
80 vec3 light = lights.ambient;
81 for(uint i = 0u; i < lights.n_dir; ++i) {
82 float a = dot(frag_normal, -lights.dir.dir[i].xyz);
83 if(a <= 0.0f || in_shadow(i))
84 continue;
85 light += lights.dir.color_spec[i].xyz * (
86 a + light_spec(
87 lights.dir.color_spec[i].w,
88 lights.dir.dir[i].xyz,
89 view_dir));
90 }
91 for(uint i = 0u; i < lights.n_point; ++i) {
92 float a = dot(frag_normal, lights.point.pos[i].xyz - frag_pos);
93 if(a <= 0.0f || in_shadow_point(i))
94 continue;
95 vec3 d = lights.point.pos[i].xyz - frag_pos;
96 float att = attenuation(i, length(d));
97 d = normalize(d);
98 float diff = max(0.0f, dot(frag_normal, d));
99 float spec = light_spec(lights.point.color_spec[i].w, -d, view_dir);
100 light += lights.point.color_spec[i].xyz
101 * att * spot(i, d) * (diff + spec);
102 }
103 return light;
104}
texture
Definition: particles.lua:4
for i
Definition: font.lua:5
vec3
Definition: zelda.lua:8
#define NNGN_SHADOWS_ENABLED_BIT
Definition: const.h:7
#define NNGN_MAX_LIGHTS
Definition: const.h:6
#define LAYOUT(x)
Definition: common.h:11
precision highp sampler2DArray
Definition: common.h:16
#define LAYOUT2(x, y)
Definition: common.h:12
uniform samplerCube lights_shadow_cube[8]
Definition: light_frag.h:9
float light_spec(float spec, vec3 dir, vec3 view_dir)
Definition: light_frag.h:34
float attenuation(uint i, float d)
Definition: light_frag.h:17
float sample_shadow(uint i, vec2 v)
Definition: light_frag.h:40
float spot(uint i, vec3 d)
Definition: light_frag.h:24
bool in_shadow_point(uint i)
Definition: light_frag.h:68
vec3 light()
Definition: light_frag.h:78
bool in_shadow(uint i)
Definition: light_frag.h:61
float sample_shadow_point(uint i, vec3 v)
Definition: light_frag.h:44
set
Definition: camera.lua:40
z
Definition: input.lua:25
a
Definition: input.lua:31
p
Definition: input.lua:29
function vec2(x, y)
v[1]
Definition: math.lua:19
function clamp(v, min, max) return math.max(min
lights
Definition: models.lua:8