diff options
Diffstat (limited to 'src/assets/shaders')
| -rw-r--r-- | src/assets/shaders/common/frag.glsl | 158 | ||||
| -rw-r--r-- | src/assets/shaders/common/vert.glsl | 30 | ||||
| -rw-r--r-- | src/assets/shaders/scale/frag.glsl | 13 | ||||
| -rw-r--r-- | src/assets/shaders/scale/vert.glsl | 22 | ||||
| -rw-r--r-- | src/assets/shaders/test/frag.glsl | 23 | ||||
| -rw-r--r-- | src/assets/shaders/test/vert.glsl | 4 | ||||
| -rw-r--r-- | src/assets/shaders/text/frag.glsl | 55 | ||||
| -rw-r--r-- | src/assets/shaders/text/vert.glsl | 26 |
8 files changed, 331 insertions, 0 deletions
diff --git a/src/assets/shaders/common/frag.glsl b/src/assets/shaders/common/frag.glsl new file mode 100644 index 0000000..908e06c --- /dev/null +++ b/src/assets/shaders/common/frag.glsl @@ -0,0 +1,158 @@ +#version 300 es +precision highp float; + +uniform vec3 camera_pos; +uniform float time; + +uniform vec3 light_ambient_color; +uniform vec3 light_dir; +uniform vec3 light_dir_color; +uniform int light_count; +uniform vec3 light_pos[5]; +uniform vec3 light_color[5]; +uniform vec2 light_attenuation[5]; +uniform highp sampler2DShadow light_shadowbuffer_dir; +uniform samplerCube light_shadowbuffer_point[5]; + +uniform int has_point_shadows; + +in vec2 vertex_texcoord; +in vec3 vertex_normal; +in vec3 vertex_fragpos; +in vec4 vertex_fragpos_shadow_dir; +in vec3 vertex_view_vector; + +out vec4 frag_color; + +mat3 compute_tbn() { + vec3 p = -vertex_view_vector; + vec3 normal = normalize(vertex_normal); + vec3 dpx = dFdx(p); + vec3 dpy = dFdy(p); + vec2 duvx = dFdx(vertex_texcoord); + vec2 duvy = dFdy(vertex_texcoord); + vec3 dpyperp = cross(dpy, normal); + vec3 dpxperp = cross(normal, dpx); + vec3 tangent = dpyperp * duvx.x + dpxperp * duvy.x; + vec3 bitangent = dpyperp * duvx.y + dpxperp * duvy.y; + float invmax = inversesqrt(max(dot(bitangent, bitangent), dot(bitangent, bitangent))); + return mat3(tangent * invmax, bitangent * invmax, normal); +} + +vec4 normal_as_color(vec3 n) { + float r = (128.0 + 127.0 * n.r) / 255.0; + float g = (128.0 + 127.0 * n.g) / 255.0; + float b = (128.0 + 127.0 * n.b) / 255.0; + return vec4(r, g, b, 1.0); +} + +vec3 dir_light(vec3 normal) { + return max(dot(normal, -normalize(light_dir)), 0.0) * light_dir_color; +} + +float dir_shadow(vec3 normal) { + vec3 proj = vertex_fragpos_shadow_dir.xyz / vertex_fragpos_shadow_dir.w; + float bias = 0.002; + // float current_depth = proj.z; + // float bias = max(0.05 * (1.0 - dot(normal, -normalize(light_dir))), 0.005); + proj.z -= bias; + proj *= 0.5; proj += 0.5; + if (proj.z > 1.0) return 0.0; + return 1.0 - texture(light_shadowbuffer_dir, proj.xyz); +} + +float point_shadow(vec3 normal, vec3 shadow_vector, float closest_depth) { + closest_depth *= 25.0; + float current_depth = length(shadow_vector); + float bias = max(0.1 * (1.0 - dot(normal, normalize(shadow_vector))), 0.005); + bias = min(bias + current_depth * 0.01, 0.2); + float shadow = current_depth - bias > closest_depth ? 1.0 : 0.0; + return shadow; +} + +vec3 point_light(vec3 normal, const int idx) { + vec3 pos = light_pos[idx]; + vec3 color = light_color[idx]; + float linear = light_attenuation[idx].x; + float quadratic = light_attenuation[idx].y; + vec3 light_vector = pos - vertex_fragpos; + float distance = length(light_vector); + float attenuation = 1.0 / (1.0 + distance * linear + distance * distance * quadratic); + + float directional = max(dot(normal.xyz, normalize(light_vector)), 0.0); + vec3 directional_light = color * directional; + + vec3 view_dir = normalize(camera_pos - vertex_fragpos); + vec3 reflect_dir = reflect(-normalize(light_vector), normalize(normal.xyz)); + float specular = pow(max(dot(view_dir, reflect_dir), 0.0), 32.0); + vec3 specular_light = 0.5 * specular * color; + return (directional_light + specular_light) * attenuation; +} + +vec3 point_light_billboard(const int idx) { + vec3 pos = light_pos[idx]; + vec3 color = light_color[idx]; + float linear = light_attenuation[idx].x; + float quadratic = light_attenuation[idx].y; + vec3 light_vector = pos - vertex_fragpos; + float distance = length(light_vector); + float attenuation = 1.0 / (1.0 + distance * linear + distance * distance * quadratic); + + return color * attenuation; +} + +vec3 compute_lighting(vec3 normal) { + vec3 ambient_light = light_ambient_color; + + vec3 from_dir = dir_light(normal) * (1.0 - dir_shadow(normal)); + + vec3 shadow_vector[5]; + for (int i = 0; i < light_count; ++i) { + shadow_vector[i] = vertex_fragpos - light_pos[i]; + shadow_vector[i].x *= -1.0; + } + + // cannot only index array of samplers with a constant, hence the weird setup + #define SAMPLE_SHADOW(n) n < light_count ? texture(light_shadowbuffer_point[n], shadow_vector[n]).r : 1.0 + float shadow_depth[5]; + shadow_depth[0] = SAMPLE_SHADOW(0); + shadow_depth[1] = SAMPLE_SHADOW(1); + shadow_depth[2] = SAMPLE_SHADOW(2); + shadow_depth[3] = SAMPLE_SHADOW(3); + shadow_depth[4] = SAMPLE_SHADOW(4); + + vec3 from_points = vec3(0.0, 0.0, 0.0); + for (int i = 0; i < light_count; ++i) { + from_points += has_point_shadows != 0 + ? point_light(normal, i) * (1.0 - point_shadow(normal, shadow_vector[i], shadow_depth[i])) + : point_light(normal, i); + } + + return (ambient_light + from_dir + from_points); +} + +vec3 compute_lighting_noshadow(vec3 normal) { + vec3 ambient_light = light_ambient_color; + + vec3 from_dir = dir_light(normal); + + vec3 from_points = vec3(0.0, 0.0, 0.0); + for (int i = 0; i < light_count; ++i) { + from_points += point_light(normal, i); + } + + return (ambient_light + from_dir + from_points); +} + +vec3 compute_lighting_billboard(vec3 normal) { + vec3 ambient_light = light_ambient_color; + + vec3 from_dir = light_dir_color / 2.0; + + vec3 from_points = vec3(0.0, 0.0, 0.0); + for (int i = 0; i < light_count; ++i) { + from_points += point_light_billboard(i); + } + + return (ambient_light + from_dir + from_points); +} diff --git a/src/assets/shaders/common/vert.glsl b/src/assets/shaders/common/vert.glsl new file mode 100644 index 0000000..b8e11c5 --- /dev/null +++ b/src/assets/shaders/common/vert.glsl @@ -0,0 +1,30 @@ +#version 300 es +precision highp float; + +in vec3 vertex; +in vec3 normal; +in vec2 texcoord; + +uniform mat4 view; +uniform mat4 position; +uniform mat4 projection; +uniform mat4 normal_matrix; +uniform mat4 lightspace_matrix; +uniform vec3 camera_pos; + +out vec2 vertex_texcoord; +out vec3 vertex_normal; +out vec3 vertex_fragpos; +out vec4 vertex_fragpos_shadow_dir; +out vec3 vertex_view_vector; + +void default_main() +{ + vertex_texcoord = texcoord; + vertex_normal = (normal_matrix * vec4(normal, 1.0)).xyz; + vec3 pos = (position * vec4(vertex, 1.0)).xyz; + vertex_fragpos = pos; + vertex_fragpos_shadow_dir = lightspace_matrix * vec4(pos, 1.0); + vertex_view_vector = camera_pos - pos; + gl_Position = projection * view * vec4(pos, 1.0); +} diff --git a/src/assets/shaders/scale/frag.glsl b/src/assets/shaders/scale/frag.glsl new file mode 100644 index 0000000..2cd3c60 --- /dev/null +++ b/src/assets/shaders/scale/frag.glsl @@ -0,0 +1,13 @@ +#version 300 es +precision highp float; + +uniform sampler2D texture_data; + +in vec2 vertex_texcoord; +out vec4 frag_color; + +void main() +{ + vec4 texel = texture(texture_data, vertex_texcoord); + frag_color = texel; +}
\ No newline at end of file diff --git a/src/assets/shaders/scale/vert.glsl b/src/assets/shaders/scale/vert.glsl new file mode 100644 index 0000000..e05bbb6 --- /dev/null +++ b/src/assets/shaders/scale/vert.glsl @@ -0,0 +1,22 @@ +#version 300 es +precision highp float; + +out vec2 vertex_texcoord; + +void main() { + const vec2 positions[4] = vec2[]( + vec2(-1, -1), + vec2(+1, -1), + vec2(-1, +1), + vec2(+1, +1) + ); + const vec2 coords[4] = vec2[]( + vec2(0, 0), + vec2(1, 0), + vec2(0, 1), + vec2(1, 1) + ); + + vertex_texcoord = coords[gl_VertexID]; + gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0); +} diff --git a/src/assets/shaders/test/frag.glsl b/src/assets/shaders/test/frag.glsl new file mode 100644 index 0000000..a52aa15 --- /dev/null +++ b/src/assets/shaders/test/frag.glsl @@ -0,0 +1,23 @@ +// uniform int has_normal_map; +// uniform sampler2D normal_map; + +uniform sampler2D texture_data; + +void main() +{ + vec2 inverted_texcoord = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec4 texel = texture(texture_data, inverted_texcoord); + if (texel.a != 1.0) { + discard; + } + + // mat3 tbn = compute_tbn(); + // vec3 normal = has_normal_map != 0 + // ? normalize(tbn * (texture(normal_map, inverted_texcoord).xyz * 2.0 - 1.0)) + // : normalize(vertex_normal); + vec3 normal = normalize(vertex_normal); + + vec3 lighting = compute_lighting_noshadow(normal); + + frag_color = vec4(texel.rgb * lighting, texel.a); +} diff --git a/src/assets/shaders/test/vert.glsl b/src/assets/shaders/test/vert.glsl new file mode 100644 index 0000000..e324f7e --- /dev/null +++ b/src/assets/shaders/test/vert.glsl @@ -0,0 +1,4 @@ +void main() +{ + default_main(); +}
\ No newline at end of file diff --git a/src/assets/shaders/text/frag.glsl b/src/assets/shaders/text/frag.glsl new file mode 100644 index 0000000..3a1694f --- /dev/null +++ b/src/assets/shaders/text/frag.glsl @@ -0,0 +1,55 @@ +#version 300 es +precision highp float; + +uniform sampler2D texture_data; +uniform int text_length; +uniform int text[256]; +uniform int char_width; +uniform int char_height; +uniform int font_width; +uniform int font_height; +uniform int text_width; +uniform int text_height; + +in vec2 vertex_texcoord; +out vec4 frag_color; + +void main() +{ + vec2 inverted_texcoord = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec2 texcoord_pixels = inverted_texcoord * vec2(float(text_width), float(text_height)); + int texcoord_char_x = int(floor(texcoord_pixels.x)) / char_width; + int texcoord_char_y = int(floor(texcoord_pixels.y)) / char_height; + + int x = 0; + int y = 0; + int i = 0; + for (; i < text_length; ++i) { + if (x == texcoord_char_x && y == texcoord_char_y) { + break; + } + if (text[i] == 10) { + x = 0; + y += 1; + } else { + x += 1; + } + } + if (i == text_length || text[i] == 10) discard; + + int entry = text[i] - 32; + vec2 texcoord_base = vec2( + float(entry % (font_width / char_width)) * float(char_width), + float(entry / (font_width / char_width)) * float(char_height) + ); + // vec2 texcoord_base = vec2(8.0, 0.0); + vec2 texcoord_off = vec2( + mod(texcoord_pixels.x, float(char_width)), + mod(texcoord_pixels.y, float(char_height)) + ); + vec2 texcoord_final = (texcoord_base + texcoord_off) / vec2(float(font_width), float(font_height)); + + vec4 texel = texture(texture_data, texcoord_final); + if (texel.rgb == vec3(0.0, 0.0, 0.0)) discard; + frag_color = texel; +}
\ No newline at end of file diff --git a/src/assets/shaders/text/vert.glsl b/src/assets/shaders/text/vert.glsl new file mode 100644 index 0000000..4005d75 --- /dev/null +++ b/src/assets/shaders/text/vert.glsl @@ -0,0 +1,26 @@ +#version 300 es +precision highp float; + +uniform mat4 view; +uniform mat4 position; + +out vec2 vertex_texcoord; + +void main() { + const vec2 positions[4] = vec2[]( + vec2(-1, -1), + vec2(+1, -1), + vec2(-1, +1), + vec2(+1, +1) + ); + const vec2 coords[4] = vec2[]( + vec2(0, 0), + vec2(1, 0), + vec2(0, 1), + vec2(1, 1) + ); + vec4 vertex = vec4(positions[gl_VertexID], 0.0, 1.0); + + vertex_texcoord = coords[gl_VertexID]; + gl_Position = view * position * vertex; +} |
