From 6b1ab6c125778b746e1fe1aaf4d1985e56484d42 Mon Sep 17 00:00:00 2001 From: tmj-fstate Date: Fri, 21 Feb 2020 22:50:52 +0100 Subject: [PATCH] opengl 3.3 renderer shaders --- shaders/alphashadowmap.frag | 10 ++ shaders/apply_fog.glsl | 30 +++++ shaders/billboard.frag | 25 +++++ shaders/celestial.frag | 25 +++++ shaders/celestial.vert | 28 +++++ shaders/color.frag | 23 ++++ shaders/envmapping.glsl | 15 +++ shaders/freespot.frag | 38 +++++++ shaders/freespot.vert | 19 ++++ shaders/gles_depthpointer.frag | 11 ++ shaders/light_common.glsl | 162 +++++++++++++++++++++++++++ shaders/map.frag | 8 ++ shaders/map.vert | 14 +++ shaders/map_poi.frag | 14 +++ shaders/map_poi.geom | 30 +++++ shaders/mat_clouds.frag | 61 ++++++++++ shaders/mat_colored.frag | 53 +++++++++ shaders/mat_default.frag | 65 +++++++++++ shaders/mat_default_0.frag | 1 + shaders/mat_default_1.frag | 1 + shaders/mat_default_2.frag | 1 + shaders/mat_invalid.frag | 20 ++++ shaders/mat_normalmap.frag | 68 +++++++++++ shaders/mat_parallax.frag | 121 ++++++++++++++++++++ shaders/mat_reflmap.frag | 66 +++++++++++ shaders/mat_shadowlessnormalmap.frag | 67 +++++++++++ shaders/mat_stars.frag | 28 +++++ shaders/mat_sunlessnormalmap.frag | 110 ++++++++++++++++++ shaders/mat_water.frag | 89 +++++++++++++++ shaders/pick.frag | 8 ++ shaders/postfx_motionblur.frag | 29 +++++ shaders/postfx_tonemapping.frag | 15 +++ shaders/precipitation.frag | 35 ++++++ shaders/precipitation.vert | 18 +++ shaders/quad.vert | 23 ++++ shaders/shadowmap.frag | 5 + shaders/simpleuv.vert | 12 ++ shaders/smoke.frag | 36 ++++++ shaders/smoke.vert | 23 ++++ shaders/texturewindow.frag | 14 +++ shaders/texturewindow.vert | 31 +++++ shaders/tonemapping.glsl | 47 ++++++++ shaders/traction.frag | 32 ++++++ shaders/traction.vert | 18 +++ shaders/vbocolor.vert | 17 +++ shaders/vertex.vert | 46 ++++++++ shaders/vertexonly.vert | 8 ++ 47 files changed, 1620 insertions(+) create mode 100644 shaders/alphashadowmap.frag create mode 100644 shaders/apply_fog.glsl create mode 100644 shaders/billboard.frag create mode 100644 shaders/celestial.frag create mode 100644 shaders/celestial.vert create mode 100644 shaders/color.frag create mode 100644 shaders/envmapping.glsl create mode 100644 shaders/freespot.frag create mode 100644 shaders/freespot.vert create mode 100644 shaders/gles_depthpointer.frag create mode 100644 shaders/light_common.glsl create mode 100644 shaders/map.frag create mode 100644 shaders/map.vert create mode 100644 shaders/map_poi.frag create mode 100644 shaders/map_poi.geom create mode 100644 shaders/mat_clouds.frag create mode 100644 shaders/mat_colored.frag create mode 100644 shaders/mat_default.frag create mode 100644 shaders/mat_default_0.frag create mode 100644 shaders/mat_default_1.frag create mode 100644 shaders/mat_default_2.frag create mode 100644 shaders/mat_invalid.frag create mode 100644 shaders/mat_normalmap.frag create mode 100644 shaders/mat_parallax.frag create mode 100644 shaders/mat_reflmap.frag create mode 100644 shaders/mat_shadowlessnormalmap.frag create mode 100644 shaders/mat_stars.frag create mode 100644 shaders/mat_sunlessnormalmap.frag create mode 100644 shaders/mat_water.frag create mode 100644 shaders/pick.frag create mode 100644 shaders/postfx_motionblur.frag create mode 100644 shaders/postfx_tonemapping.frag create mode 100644 shaders/precipitation.frag create mode 100644 shaders/precipitation.vert create mode 100644 shaders/quad.vert create mode 100644 shaders/shadowmap.frag create mode 100644 shaders/simpleuv.vert create mode 100644 shaders/smoke.frag create mode 100644 shaders/smoke.vert create mode 100644 shaders/texturewindow.frag create mode 100644 shaders/texturewindow.vert create mode 100644 shaders/tonemapping.glsl create mode 100644 shaders/traction.frag create mode 100644 shaders/traction.vert create mode 100644 shaders/vbocolor.vert create mode 100644 shaders/vertex.vert create mode 100644 shaders/vertexonly.vert diff --git a/shaders/alphashadowmap.frag b/shaders/alphashadowmap.frag new file mode 100644 index 00000000..9ff0e1d7 --- /dev/null +++ b/shaders/alphashadowmap.frag @@ -0,0 +1,10 @@ +in vec2 f_coord; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +void main() +{ + if (texture(tex1, f_coord).a < 0.5f) + discard; +} diff --git a/shaders/apply_fog.glsl b/shaders/apply_fog.glsl new file mode 100644 index 00000000..34fc529b --- /dev/null +++ b/shaders/apply_fog.glsl @@ -0,0 +1,30 @@ +vec3 get_fog_color() +{ + vec3 fog_color_v = fog_color; + if (lights_count >= 1U && lights[0].type == LIGHT_DIR) { + float sun_amount = max(dot(normalize(f_pos.xyz), normalize(-lights[0].dir)), 0.0); + fog_color_v += lights[0].color * pow(sun_amount, 30.0); + } + return fog_color_v; +} + +float get_fog_amount() +{ + float z = length(f_pos.xyz); +// float fog_amount_v = 1.0 - z * fog_density; // linear +// float fog_amount_v = exp( -fog_density * z ); // exp + float fog_amount_v = exp2( -fog_density * fog_density * z * z * 1.442695 ); // exp2 + + fog_amount_v = 1.0 - clamp(fog_amount_v, 0.0, 1.0); + return fog_amount_v; +} + +vec3 apply_fog(vec3 color) +{ + return mix(color, get_fog_color(), get_fog_amount()); +} + +vec3 add_fog(vec3 color, float amount) +{ + return color + get_fog_color() * get_fog_amount() * amount; +} diff --git a/shaders/billboard.frag b/shaders/billboard.frag new file mode 100644 index 00000000..7ef0756f --- /dev/null +++ b/shaders/billboard.frag @@ -0,0 +1,25 @@ +in vec2 f_coord; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +#include +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ + vec4 tex_color = texture(tex1, f_coord); +#if POSTFX_ENABLED + out_color = tex_color * param[0]; +#else + out_color = tonemap(tex_color * param[0]); +#endif +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/celestial.frag b/shaders/celestial.frag new file mode 100644 index 00000000..7ef0756f --- /dev/null +++ b/shaders/celestial.frag @@ -0,0 +1,25 @@ +in vec2 f_coord; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +#include +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ + vec4 tex_color = texture(tex1, f_coord); +#if POSTFX_ENABLED + out_color = tex_color * param[0]; +#else + out_color = tonemap(tex_color * param[0]); +#endif +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/celestial.vert b/shaders/celestial.vert new file mode 100644 index 00000000..168ba49f --- /dev/null +++ b/shaders/celestial.vert @@ -0,0 +1,28 @@ +const vec2 vert[4] = vec2[] +( + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2( 1.0, -1.0) +); + +out vec2 f_coord; + +#include + +void main() +{ + gl_Position = projection * (vec4(param[1].xyz, 1.0) + vec4(vert[gl_VertexID] * param[1].w, 0.0, 0.0)); + + vec2 coord = param[2].xy; + float size = param[2].z; + + if (gl_VertexID == 0) + f_coord = vec2(coord.x, coord.y); + if (gl_VertexID == 1) + f_coord = vec2(coord.x, coord.y - size); + if (gl_VertexID == 2) + f_coord = vec2(coord.x + size, coord.y); + if (gl_VertexID == 3) + f_coord = vec2(coord.x + size, coord.y - size); +} diff --git a/shaders/color.frag b/shaders/color.frag new file mode 100644 index 00000000..3ecfb644 --- /dev/null +++ b/shaders/color.frag @@ -0,0 +1,23 @@ +in vec3 f_color; +in vec4 f_pos; + +#include +#include +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ +#if POSTFX_ENABLED + out_color = vec4(apply_fog(f_color), 1.0f); +#else + out_color = tonemap(vec4(apply_fog(f_color), 1.0f)); +#endif +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/envmapping.glsl b/shaders/envmapping.glsl new file mode 100644 index 00000000..72a0ddc1 --- /dev/null +++ b/shaders/envmapping.glsl @@ -0,0 +1,15 @@ +#if ENVMAP_ENABLED +uniform samplerCube envmap; +#endif + +vec3 envmap_color( vec3 normal ) +{ +#if ENVMAP_ENABLED + vec3 refvec = reflect(f_pos.xyz, normal); // view space + refvec = vec3(inv_view * vec4(refvec, 0.0)); // world space + vec3 envcolor = texture(envmap, refvec).rgb; +#else + vec3 envcolor = vec3(0.5); +#endif + return envcolor; +} diff --git a/shaders/freespot.frag b/shaders/freespot.frag new file mode 100644 index 00000000..3dad9031 --- /dev/null +++ b/shaders/freespot.frag @@ -0,0 +1,38 @@ +in vec4 f_pos; +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#include +#include + +void main() +{ + float x = (gl_PointCoord.x - 0.5f) * 2.0f; + float y = (gl_PointCoord.y - 0.5f) * 2.0f; + float dist = sqrt(x * x + y * y); + if (dist > 0.5f) + discard; +// vec4 color = vec4(param[0].rgb * emission, mix(param[0].a, 0.0f, dist * 2.0f)); + vec4 color = vec4(apply_fog(param[0].rgb * emission), mix(param[0].a, 0.0f, dist * 2.0f)); +// vec4 color = vec4(add_fog(param[0].rgb * emission * (1.0 - get_fog_amount()), 1.0), mix(param[0].a, 0.0f, dist * 2.0f)); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/freespot.vert b/shaders/freespot.vert new file mode 100644 index 00000000..590520df --- /dev/null +++ b/shaders/freespot.vert @@ -0,0 +1,19 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec3 v_normal; +layout(location = 2) in vec2 v_coord; + +#include + +out vec4 f_pos; +out vec4 f_clip_pos; +out vec4 f_clip_future_pos; + +void main() +{ + f_pos = modelview * vec4(v_vert, 1.0); + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0f); + f_clip_future_pos = (projection * future * modelview) * vec4(v_vert, 1.0f); + + gl_Position = f_clip_pos; + gl_PointSize = param[1].x; +} diff --git a/shaders/gles_depthpointer.frag b/shaders/gles_depthpointer.frag new file mode 100644 index 00000000..42b9cb60 --- /dev/null +++ b/shaders/gles_depthpointer.frag @@ -0,0 +1,11 @@ +in vec2 f_coords; + +#texture (tex1, 0, R) +uniform sampler2D tex1; + +layout(location = 0) out uvec4 out_color; + +void main() +{ + out_color = uvec4(uint(texture(tex1, f_coords).r * 65535.0), 0, 0, 0xFFFF); +} diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl new file mode 100644 index 00000000..0d26e1e2 --- /dev/null +++ b/shaders/light_common.glsl @@ -0,0 +1,162 @@ +#if SHADOWMAP_ENABLED +in vec4 f_light_pos[MAX_CASCADES]; +uniform sampler2DArrayShadow shadowmap; +#endif +uniform sampler2D headlightmap; + +#include + +float length2(vec3 v) +{ + return dot(v, v); +} + +float calc_shadow() +{ +#if SHADOWMAP_ENABLED + float distance = dot(f_pos.xyz, f_pos.xyz); + uint cascade; + for (cascade = 0U; cascade < MAX_CASCADES; cascade++) + if (distance <= cascade_end[cascade]) + break; + + vec3 coords = f_light_pos[cascade].xyz / f_light_pos[cascade].w; + if (coords.z < 0.0f) + return 0.0f; + + float shadow = 0.0; + //basic +// shadow = texture(shadowmap, coords.xyz + vec3(0.0, 0.0, bias)); + //PCF + float bias = 0.00005f * (cascade + 1U); + vec2 texel = vec2(1.0) / vec2(textureSize(shadowmap, 0)); + float radius = 1.0; + for (float y = -1.5; y <= 1.5; y += 1.0) + for (float x = -1.5; x <= 1.5; x += 1.0) + shadow += texture( shadowmap, vec4(coords.xy + vec2(x, y) * radius * texel, cascade, coords.z + bias) ); + shadow /= 16.0; + + return shadow; +#else + return 0.0; +#endif +} + +// [0] - diffuse, [1] - specular +// do magic here +vec2 calc_light(vec3 light_dir, vec3 fragnormal) +{ + vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos.xyz); + vec3 halfway_dir = normalize(light_dir + view_dir); + + float diffuse_v = max(dot(fragnormal, light_dir), 0.0); + float specular_v = pow(max(dot(fragnormal, halfway_dir), 0.0), max(abs(param[1].w), 0.01)) * diffuse_v; + + return vec2(diffuse_v, specular_v); +} + +vec2 calc_point_light(light_s light, vec3 fragnormal) +{ + vec3 light_dir = normalize(light.pos - f_pos.xyz); + vec2 val = calc_light(light_dir, fragnormal); + val.x += light.ambient; + val *= light.intensity; + + float distance = length(light.pos - f_pos.xyz); + float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance)); + + return val * atten; +} + +vec2 calc_spot_light(light_s light, vec3 fragnormal) +{ + vec3 light_dir = normalize(light.pos - f_pos.xyz); + + float theta = dot(light_dir, normalize(-light.dir)); + float epsilon = light.in_cutoff - light.out_cutoff; + float intensity = clamp((theta - light.out_cutoff) / epsilon, 0.0, 1.0); + + vec2 point = calc_point_light(light, fragnormal); + return point * intensity; +} + +vec2 calc_dir_light(light_s light, vec3 fragnormal) +{ + vec3 light_dir = normalize(-light.dir); + return calc_light(light_dir, fragnormal); +} + +vec2 calc_headlights(light_s light, vec3 fragnormal) +{ + vec4 headlightpos = light.headlight_projection * f_pos; + vec3 coords = headlightpos.xyz / headlightpos.w; + + if (coords.z > 1.0) + return vec2(0.0); + if (coords.z < 0.0) + return vec2(0.0); + + vec3 light_dir = normalize(light.pos - f_pos.xyz); + vec2 part = vec2(1.0) * clamp(dot(fragnormal, light_dir) + 0.25, 0.0, 1.0); + float distance = length(light.pos - f_pos.xyz); + float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance)); + atten *= mix(1.0, 0.0, clamp((coords.z - 0.998) * 500.0, 0.0, 1.0)); + vec3 lights = textureProj(headlightmap, headlightpos).rgb * light.headlight_weights.rgb; + float lightintensity = max(max(lights.r, lights.g), lights.b); + return part * atten * lightintensity; +} + +vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) +{ + vec3 emissioncolor = param[0].rgb * emission; + vec3 envcolor = envmap_color(fragnormal); + + if(lights_count == 0U) + return (fragcolor + emissioncolor + envcolor * reflectivity) * texturecolor; + + vec2 sunlight = calc_dir_light(lights[0], fragnormal); + + float diffuseamount = (sunlight.x * param[1].x) * lights[0].intensity; +// fragcolor += mix(lights[0].color * diffuseamount, envcolor, reflectivity); + fragcolor += lights[0].color * diffuseamount; + fragcolor = mix(fragcolor, envcolor, reflectivity); + + for (uint i = 1U; i < lights_count; i++) + { + light_s light = lights[i]; + vec2 part = vec2(0.0); + +// if (light.type == LIGHT_SPOT) +// part = calc_spot_light(light, fragnormal); +// else if (light.type == LIGHT_POINT) +// part = calc_point_light(light, fragnormal); +// else if (light.type == LIGHT_DIR) +// part = calc_dir_light(light, fragnormal); +// else if (light.type == LIGHT_HEADLIGHTS) + part = calc_headlights(light, fragnormal); + + fragcolor += light.color * (part.x * param[1].x + part.y * param[1].y) * light.intensity; + } + + float specularamount = (sunlight.y * param[1].y * specularity) * lights[0].intensity * clamp(1.0 - shadowtone, 0.0, 1.0); + if (shadowtone < 1.0) + { + float shadow = calc_shadow(); + specularamount *= clamp(1.0 - shadow, 0.0, 1.0); + fragcolor = mix(fragcolor, fragcolor * shadowtone, clamp(diffuseamount * shadow + specularamount, 0.0, 1.0)); + } + fragcolor += emissioncolor; + vec3 specularcolor = specularamount * lights[0].color; + if (param[1].w >= 0.0) + { + fragcolor *= texturecolor; + fragcolor += specularcolor; + } + else + { + fragcolor += specularcolor; + fragcolor *= texturecolor; + } + + return fragcolor; +} diff --git a/shaders/map.frag b/shaders/map.frag new file mode 100644 index 00000000..3e73e714 --- /dev/null +++ b/shaders/map.frag @@ -0,0 +1,8 @@ +#include + +layout(location = 0) out vec4 out_color; + +void main() +{ + out_color = vec4(scene_extra, 1.0f); +} diff --git a/shaders/map.vert b/shaders/map.vert new file mode 100644 index 00000000..149efb62 --- /dev/null +++ b/shaders/map.vert @@ -0,0 +1,14 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 2) in vec2 v_coords; + +out vec2 g_coords; + +#include + +void main() +{ + vec4 clip_pos = projection * vec4(v_vert, 1.0f); + + gl_Position = vec4(clip_pos.xz, 0.5, 1.0); + g_coords = v_coords; +} diff --git a/shaders/map_poi.frag b/shaders/map_poi.frag new file mode 100644 index 00000000..cb8c43ea --- /dev/null +++ b/shaders/map_poi.frag @@ -0,0 +1,14 @@ +in vec2 f_coords; + +layout(location = 0) out vec4 out_color; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +void main() +{ + vec2 texcoord = f_coords; + vec4 color = texture(tex1, texcoord); + + out_color = color; +} diff --git a/shaders/map_poi.geom b/shaders/map_poi.geom new file mode 100644 index 00000000..25f12fe6 --- /dev/null +++ b/shaders/map_poi.geom @@ -0,0 +1,30 @@ +layout (points) in; +layout (triangle_strip, max_vertices = 4) out; + +in vec2 g_coords[]; +out vec2 f_coords; + +#include + +void main() { + vec4 position = gl_in[0].gl_Position; + vec2 size = vec2(0.1) * scene_extra.xy; + + gl_Position = position + vec4(-size.x, -size.y, 0.0, 0.0); + f_coords = vec2(g_coords[0].s, 0.0); + EmitVertex(); + + gl_Position = position + vec4( size.x, -size.y, 0.0, 0.0); + f_coords = vec2(g_coords[0].t, 0.0); + EmitVertex(); + + gl_Position = position + vec4(-size.x, size.y, 0.0, 0.0); + f_coords = vec2(g_coords[0].s, 1.0); + EmitVertex(); + + gl_Position = position + vec4( size.x, size.y, 0.0, 0.0); + f_coords = vec2(g_coords[0].t, 1.0); + EmitVertex(); + + EndPrimitive(); +} diff --git a/shaders/mat_clouds.frag b/shaders/mat_clouds.frag new file mode 100644 index 00000000..5e6f4f39 --- /dev/null +++ b/shaders/mat_clouds.frag @@ -0,0 +1,61 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +#param (diffuse, 1, 0, 1, diffuse) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#include +#include + +vec3 apply_lights_clouds(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor) +{ + if(lights_count == 0U) + return fragcolor * texturecolor; + + vec3 light_dir = normalize(-lights[0].dir); + vec3 view_dir = normalize(vec3(0.0) - f_pos.xyz); + vec3 halfway_dir = normalize(light_dir + view_dir); + float diffuse_v = max(dot(fragnormal, light_dir), 0.0); + float diffuseamount = (diffuse_v * param[1].x) * lights[0].intensity; + fragcolor += lights[0].color * diffuseamount; + fragcolor *= texturecolor; + + return fragcolor; +} + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + tex_color.a = clamp(tex_color.a * 1.25, 0.0, 1.0); + if(tex_color.a < 0.01) + discard; + + vec3 fragcolor = param[0].rgb; + vec3 fragnormal = normalize(f_normal); + + fragcolor = apply_lights_clouds(fragcolor, fragnormal, tex_color.rgb); + vec4 color = vec4(apply_fog(fragcolor), tex_color.a); + +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif + +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/mat_colored.frag b/shaders/mat_colored.frag new file mode 100644 index 00000000..2d808326 --- /dev/null +++ b/shaders/mat_colored.frag @@ -0,0 +1,53 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, zero) +#param (glossiness, 1, 3, 1, glossiness) + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#include +#include +#include + +void main() +{ + vec4 tex_color = vec4(pow(param[0].rgb, vec3(2.2)), param[0].a); + +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + vec3 fragnormal = normalize(f_normal); + float reflectivity = param[1].z; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/mat_default.frag b/shaders/mat_default.frag new file mode 100644 index 00000000..89cf61cc --- /dev/null +++ b/shaders/mat_default.frag @@ -0,0 +1,65 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, zero) +#param (glossiness, 1, 3, 1, glossiness) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#include +#include +#include + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + vec3 fragnormal = normalize(f_normal); + float reflectivity = param[1].z; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +/* + float distance = dot(f_pos.xyz, f_pos.xyz); + if( distance <= cascade_end.x ) { color.r += 0.25; } + else if( distance <= cascade_end.y ) { color.g += 0.25; } + else if( distance <= cascade_end.z ) { color.b += 0.25; } +*/ +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif + +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, tex_color.a * alpha_mult); + } +#endif +} diff --git a/shaders/mat_default_0.frag b/shaders/mat_default_0.frag new file mode 100644 index 00000000..f951f70d --- /dev/null +++ b/shaders/mat_default_0.frag @@ -0,0 +1 @@ +#include diff --git a/shaders/mat_default_1.frag b/shaders/mat_default_1.frag new file mode 100644 index 00000000..8eb248b7 --- /dev/null +++ b/shaders/mat_default_1.frag @@ -0,0 +1 @@ +#include diff --git a/shaders/mat_default_2.frag b/shaders/mat_default_2.frag new file mode 100644 index 00000000..9761f4fe --- /dev/null +++ b/shaders/mat_default_2.frag @@ -0,0 +1 @@ +#include diff --git a/shaders/mat_invalid.frag b/shaders/mat_invalid.frag new file mode 100644 index 00000000..4a94ae47 --- /dev/null +++ b/shaders/mat_invalid.frag @@ -0,0 +1,20 @@ +#include +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ + vec4 color = vec4(1.0, 0.0, 1.0, 1.0); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/mat_normalmap.frag b/shaders/mat_normalmap.frag new file mode 100644 index 00000000..3ef505eb --- /dev/null +++ b/shaders/mat_normalmap.frag @@ -0,0 +1,68 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, one) +#param (glossiness, 1, 3, 1, glossiness) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +#texture (normalmap, 1, RGBA) +uniform sampler2D normalmap; + +#define NORMALMAP +#include +#include +#include + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + + vec3 normal; + normal.xy = (texture(normalmap, f_coord).rg * 2.0 - 1.0); + normal.z = sqrt(1.0 - clamp((dot(normal.xy, normal.xy)), 0.0, 1.0)); + vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); + float reflectivity = param[1].z * texture(normalmap, f_coord).a; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/mat_parallax.frag b/shaders/mat_parallax.frag new file mode 100644 index 00000000..7adac054 --- /dev/null +++ b/shaders/mat_parallax.frag @@ -0,0 +1,121 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; //tangent matrix nietransponowany; mnożyć przez f_tbn dla TangentLightPos; TangentViewPos; TangentFragPos; +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; +in vec3 TangentFragPos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, one) +#param (glossiness, 1, 3, 1, glossiness) +#param (height_scale, 2, 1, 1, zero) +#param (height_offset, 2, 2, 1, zero) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +#texture (normalmap, 1, RGBA) +uniform sampler2D normalmap; + +#define PARALLAX +#include +#include +#include + +vec2 ParallaxMapping(vec2 f_coord, vec3 viewDir); + +void main() +{ + //parallax mapping + vec3 viewDir = normalize(vec3(0.0f, 0.0f, 0.0f) - TangentFragPos); //tangent view pos - tangent frag pos + vec2 f_coord_p = ParallaxMapping(f_coord, viewDir); + + vec4 tex_color = texture(diffuse, f_coord_p); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + + vec3 normal; + normal.xy = (texture(normalmap, f_coord_p).rg * 2.0 - 1.0); + normal.z = sqrt(1.0 - clamp((dot(normal.xy, normal.xy)), 0.0, 1.0)); + vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); + float reflectivity = param[1].z * texture(normalmap, f_coord_p).a; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} + +vec2 ParallaxMapping(vec2 f_coord, vec3 viewDir) +{ + float pos_len = length(f_pos.xyz); + + if (pos_len > 100.0) { + return f_coord; + } + +#if EXTRAEFFECTS_ENABLED + const float minLayers = 8.0; + const float maxLayers = 32.0; + float LayersWeight = pos_len / 20.0; + vec2 currentTexCoords = f_coord; + float currentDepthMapValue = texture(normalmap, currentTexCoords).b; + LayersWeight = min(abs(dot(vec3(0.0, 0.0, 1.0), viewDir)),LayersWeight); + float numLayers = mix(maxLayers, minLayers, clamp(LayersWeight, 0.0, 1.0)); // number of depth layers + float layerDepth = 1.0 / numLayers; // calculate the size of each layer + float currentLayerDepth = 0.0; // depth of current layer + vec2 P = viewDir.xy * param[2].y; // the amount to shift the texture coordinates per layer (from vector P) + vec2 deltaTexCoords = P / numLayers; + + + while(currentLayerDepth < currentDepthMapValue) + { + currentTexCoords -= deltaTexCoords; // shift texture coordinates along direction of P + currentDepthMapValue = texture(normalmap, currentTexCoords).b; // get depthmap value at current texture coordinates + currentLayerDepth += layerDepth; // get depth of next layer + } + + vec2 prevTexCoords = currentTexCoords + deltaTexCoords; // get texture coordinates before collision (reverse operations) + + float afterDepth = currentDepthMapValue - currentLayerDepth; // get depth after and before collision for linear interpolation + float beforeDepth = texture(normalmap, prevTexCoords).b - currentLayerDepth + layerDepth; + + float weight = afterDepth / (afterDepth - beforeDepth); // interpolation of texture coordinates + vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight); + + return finalTexCoords; +#else + float height = texture(normalmap, f_coord).b; + vec2 p = viewDir.xy / viewDir.z * (height * (param[2].y - param[2].z) * 0.2); + return f_coord - p; +#endif +} diff --git a/shaders/mat_reflmap.frag b/shaders/mat_reflmap.frag new file mode 100644 index 00000000..24d139d5 --- /dev/null +++ b/shaders/mat_reflmap.frag @@ -0,0 +1,66 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, one) +#param (glossiness, 1, 3, 1, glossiness) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +#texture (reflmap, 1, RGBA) +uniform sampler2D reflmap; + +#include +#include +#include + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + vec3 fragnormal = normalize(f_normal); + float reflectivity = param[1].z * texture(reflmap, f_coord).a; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + + if(alphatestfail) + fragcolor.r = 1.0; + + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/mat_shadowlessnormalmap.frag b/shaders/mat_shadowlessnormalmap.frag new file mode 100644 index 00000000..b2b49b1f --- /dev/null +++ b/shaders/mat_shadowlessnormalmap.frag @@ -0,0 +1,67 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, zero) +#param (glossiness, 1, 3, 1, glossiness) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +#texture (normalmap, 1, RGBA) +uniform sampler2D normalmap; + +#define NORMALMAP +#include +#include +#include + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + + vec3 normal; + normal.xy = (texture(normalmap, f_coord).rg * 2.0 - 1.0); + normal.z = sqrt(1.0 - clamp((dot(normal.xy, normal.xy)), 0.0, 1.0)); + vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); + float reflectivity = param[1].z * texture(normalmap, f_coord).a; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, 1.0); + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/mat_stars.frag b/shaders/mat_stars.frag new file mode 100644 index 00000000..9da44100 --- /dev/null +++ b/shaders/mat_stars.frag @@ -0,0 +1,28 @@ +#include +#include + +flat in vec3 f_normal_raw; + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ + float x = (gl_PointCoord.x - 0.5f) * 2.0f; + float y = (gl_PointCoord.y - 0.5f) * 2.0f; + float dist2 = abs(x * x + y * y); + if (dist2 > 0.5f * 0.5f) + discard; + // color data space is shared with normals, ugh + vec4 color = vec4(pow(f_normal_raw.rgb, vec3(2.2)), 1.0f); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + out_motion = vec4(0.0f); +#endif +} diff --git a/shaders/mat_sunlessnormalmap.frag b/shaders/mat_sunlessnormalmap.frag new file mode 100644 index 00000000..380a17b0 --- /dev/null +++ b/shaders/mat_sunlessnormalmap.frag @@ -0,0 +1,110 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, zero) +#param (glossiness, 1, 3, 1, glossiness) + +#texture (diffuse, 0, sRGB_A) +uniform sampler2D diffuse; + +#texture (normalmap, 1, RGBA) +uniform sampler2D normalmap; + +#define NORMALMAP +#include +#include +#include + +vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) +{ + vec3 emissioncolor = param[0].rgb * emission; + vec3 envcolor = envmap_color(fragnormal); + + if(lights_count == 0U) + return (fragcolor + emissioncolor + envcolor * reflectivity) * texturecolor; + + vec2 sunlight = calc_dir_light(lights[0], fragnormal); + + float diffuseamount = (sunlight.x * param[1].x) * lights[0].intensity; + fragcolor += envcolor * reflectivity; + float specularamount = (sunlight.y * param[1].y * specularity) * lights[0].intensity; + + for (uint i = 1U; i < lights_count; i++) + { + light_s light = lights[i]; + vec2 part = vec2(0.0); + +// if (light.type == LIGHT_SPOT) +// part = calc_spot_light(light, fragnormal); +// else if (light.type == LIGHT_POINT) +// part = calc_point_light(light, fragnormal); +// else if (light.type == LIGHT_DIR) +// part = calc_dir_light(light, fragnormal); +// else if (light.type == LIGHT_HEADLIGHTS) + part = calc_headlights(light, fragnormal); + + fragcolor += light.color * (part.x * param[1].x + part.y * param[1].y) * light.intensity; + } + + if (shadowtone < 1.0) + { + float shadow = calc_shadow(); + specularamount *= clamp(1.0 - shadow, 0.0, 1.0); + fragcolor = mix(fragcolor, fragcolor * shadowtone, clamp(diffuseamount * shadow + specularamount, 0.0, 1.0)); + } + fragcolor += emissioncolor; + fragcolor *= texturecolor; + + return fragcolor; +} + +void main() +{ + vec4 tex_color = texture(diffuse, f_coord); + + bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + if(alphatestfail) + discard; +// if (tex_color.a < opacity) +// discard; + + vec3 fragcolor = ambient; + + vec3 normal; + normal.xy = (texture(normalmap, f_coord).rg * 2.0 - 1.0); + normal.z = sqrt(1.0 - clamp((dot(normal.xy, normal.xy)), 0.0, 1.0)); + vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); + float reflectivity = param[1].z * texture(normalmap, f_coord).a; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + + fragcolor = apply_lights_sunless(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/mat_water.frag b/shaders/mat_water.frag new file mode 100644 index 00000000..23e933cb --- /dev/null +++ b/shaders/mat_water.frag @@ -0,0 +1,89 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#param (color, 0, 0, 4, diffuse) +#param (diffuse, 1, 0, 1, diffuse) +#param (specular, 1, 1, 1, specular) +#param (reflection, 1, 2, 1, one) +#param (glossiness, 1, 3, 1, glossiness) +#param (wave_strength, 2, 1, 1, one) +#param (wave_speed, 2, 2, 1, one) + +#texture (normalmap, 0, RGBA) +uniform sampler2D normalmap; + +#texture (dudvmap, 1, RGB) +uniform sampler2D dudvmap; + +#texture (diffuse, 2, sRGB_A) +uniform sampler2D diffuse; + +//wave distortion variables +float move_factor = 0.0; + +#define WATER +#include +#include +#include + +void main() +{ + //wave distortion + move_factor += (param[2].z * time); + move_factor = mod(move_factor, 1.0); + vec2 texture_coords = f_coord; + vec2 distorted_tex_coord = texture(dudvmap, vec2(texture_coords.x + move_factor, texture_coords.y)).rg * 0.1; + distorted_tex_coord = texture_coords + vec2(distorted_tex_coord.x , distorted_tex_coord.y + move_factor); + vec2 total_distorted_tex_coord = (texture(dudvmap, distorted_tex_coord).rg * 2.0 - 1.0 ) * param[2].y; + texture_coords += total_distorted_tex_coord; + + vec4 tex_color = texture(diffuse, texture_coords); + + vec3 fragcolor = ambient * 0.25; + + vec3 normal; + normal.xy = (texture(normalmap, texture_coords).rg * 2.0 - 1.0); + normal.z = sqrt(1.0 - clamp((dot(normal.xy, normal.xy)), 0.0, 1.0)); + vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); + float reflectivity = param[1].z * texture(normalmap, texture_coords ).a; + float specularity = 1.0; + + fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); + + //fragcolor = mix(fragcolor, param[0].rgb, param[1].z); +// fragcolor = (fragcolor * tex_color.rgb * param[1].z) + (fragcolor * (1.0 - param[1].z)); //multiply + //fragcolor = ( max(fragcolor + param[0].rgb -1.0,0.0) * param[1].z + fragcolor * (1.0 - param[1].z)); //linear burn + //fragcolor = ( min(param[0].rgb,fragcolor) * param[1].z + fragcolor * (1.0 - param[1].z)); //darken + + // fresnel effect + vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos.xyz); + float fresnel = pow ( dot (f_normal, view_dir), 0.2 ); + float fresnel_inv = ((fresnel - 1.0 ) * -1.0 ); + + vec4 color = vec4(apply_fog(clamp(fragcolor, 0.0, 1.0)), clamp((fresnel_inv + param[0].a), 0.0, 1.0)); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/pick.frag b/shaders/pick.frag new file mode 100644 index 00000000..cd9abc4a --- /dev/null +++ b/shaders/pick.frag @@ -0,0 +1,8 @@ +#include + +layout(location = 0) out vec4 out_color; + +void main() +{ + out_color = vec4(param[0].rgb, 1.0); +} diff --git a/shaders/postfx_motionblur.frag b/shaders/postfx_motionblur.frag new file mode 100644 index 00000000..331fc0d7 --- /dev/null +++ b/shaders/postfx_motionblur.frag @@ -0,0 +1,29 @@ +in vec2 f_coords; +layout(location = 0) out vec4 out_color; + +#texture (color_tex, 0, RGB) +uniform sampler2D color_tex; + +#texture (velocity_tex, 1, RG) +uniform sampler2D velocity_tex; + +#include + +void main() +{ + vec2 texelSize = 1.0 / vec2(textureSize(color_tex, 0)); + vec2 velocity = texture(velocity_tex, f_coords).rg * param[0].r; + + float speed = length(velocity / texelSize); + int nSamples = clamp(int(speed), 1, 64); + + vec4 oResult = texture(color_tex, f_coords); + for (int i = 1; i < nSamples; ++i) + { + vec2 offset = velocity * (float(i) / float(nSamples - 1) - 0.5); + oResult += texture(color_tex, f_coords + offset); + } + oResult /= float(nSamples); + + out_color = oResult; +} diff --git a/shaders/postfx_tonemapping.frag b/shaders/postfx_tonemapping.frag new file mode 100644 index 00000000..81b3c3f4 --- /dev/null +++ b/shaders/postfx_tonemapping.frag @@ -0,0 +1,15 @@ +in vec2 f_coords; + +layout(location = 0) out vec4 out_color; + +#texture (tex1, 0, RGB) +uniform sampler2D tex1; + +#include + +void main() +{ + vec2 texcoord = f_coords; + vec3 hdr_color = texture(tex1, texcoord).xyz; + out_color = tonemap(vec4(hdr_color, 1.0)); +} diff --git a/shaders/precipitation.frag b/shaders/precipitation.frag new file mode 100644 index 00000000..2b1ea6a4 --- /dev/null +++ b/shaders/precipitation.frag @@ -0,0 +1,35 @@ +in vec2 f_coord; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#include + +void main() +{ + vec4 tex_color = texture(tex1, vec2(f_coord.x, f_coord.y + param[1].x)); + vec4 color = tex_color * param[0]; +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5; + + out_motion = vec4(a - b, 0.0f, tex_color.a * alpha_mult); + } +#endif +} diff --git a/shaders/precipitation.vert b/shaders/precipitation.vert new file mode 100644 index 00000000..4b33ccac --- /dev/null +++ b/shaders/precipitation.vert @@ -0,0 +1,18 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec2 v_coord; + +out vec2 f_coord; + +out vec4 f_clip_pos; +out vec4 f_clip_future_pos; + +#include + +void main() +{ + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0f); + f_clip_future_pos = (projection * future * modelview) * vec4(v_vert, 1.0f); + + gl_Position = f_clip_pos; + f_coord = v_coord; +} diff --git a/shaders/quad.vert b/shaders/quad.vert new file mode 100644 index 00000000..850d3fee --- /dev/null +++ b/shaders/quad.vert @@ -0,0 +1,23 @@ +const vec2 vert[4] = vec2[] +( + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2( 1.0, -1.0) +); + +const vec2 uv[4] = vec2[] +( + vec2(0.0, 1.0), + vec2(0.0, 0.0), + vec2(1.0, 1.0), + vec2(1.0, 0.0) +); + +out vec2 f_coords; + +void main() +{ + gl_Position = vec4(vert[gl_VertexID], 0.0, 1.0); + f_coords = uv[gl_VertexID]; +} diff --git a/shaders/shadowmap.frag b/shaders/shadowmap.frag new file mode 100644 index 00000000..14ef7549 --- /dev/null +++ b/shaders/shadowmap.frag @@ -0,0 +1,5 @@ +in vec2 f_coord; + +void main() +{ +} diff --git a/shaders/simpleuv.vert b/shaders/simpleuv.vert new file mode 100644 index 00000000..f3cd0ea2 --- /dev/null +++ b/shaders/simpleuv.vert @@ -0,0 +1,12 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 2) in vec2 v_coord; + +out vec2 f_coord; + +#include + +void main() +{ + gl_Position = (projection * modelview) * vec4(v_vert, 1.0f); + f_coord = v_coord; +} diff --git a/shaders/smoke.frag b/shaders/smoke.frag new file mode 100644 index 00000000..b790ef3c --- /dev/null +++ b/shaders/smoke.frag @@ -0,0 +1,36 @@ +in vec4 f_color; +in vec2 f_coord; +in vec4 f_pos; + +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +#texture (tex1, 0, sRGB_A) +uniform sampler2D tex1; + +#include +#include +#include + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +void main() +{ + vec4 tex_color = texture(tex1, f_coord) * f_color; +#if POSTFX_ENABLED + out_color = vec4(apply_fog(tex_color.rgb), tex_color.a); +#else + out_color = tonemap(vec4(apply_fog(tex_color.rgb), tex_color.a)); +#endif +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, tex_color.a * alpha_mult); + } +#endif +} diff --git a/shaders/smoke.vert b/shaders/smoke.vert new file mode 100644 index 00000000..8769d067 --- /dev/null +++ b/shaders/smoke.vert @@ -0,0 +1,23 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec4 v_color; +layout(location = 2) in vec2 v_coord; + +out vec4 f_pos; +out vec4 f_color; +out vec2 f_coord; + +out vec4 f_clip_pos; +out vec4 f_clip_future_pos; + +#include + +void main() +{ + f_pos = modelview * vec4(v_vert, 1.0f); + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0f); + f_clip_future_pos = (projection * future * modelview) * vec4(v_vert, 1.0f); + + gl_Position = f_clip_pos; + f_coord = v_coord; + f_color = v_color; +} diff --git a/shaders/texturewindow.frag b/shaders/texturewindow.frag new file mode 100644 index 00000000..80a3ebc8 --- /dev/null +++ b/shaders/texturewindow.frag @@ -0,0 +1,14 @@ +in vec2 f_coords; + +layout(location = 0) out vec4 out_color; + +#texture (tex1, 0, sRGB) +uniform sampler2D tex1; + +void main() +{ + vec2 texcoord = f_coords; + vec3 color = texture(tex1, texcoord).rgb; + + out_color = FBOUT(vec4(color, 1.0)); +} diff --git a/shaders/texturewindow.vert b/shaders/texturewindow.vert new file mode 100644 index 00000000..47e7ecf6 --- /dev/null +++ b/shaders/texturewindow.vert @@ -0,0 +1,31 @@ +layout (std140) uniform scene_ubo +{ + mat4 projection; + mat4 lightview; + float time; +}; + +const vec2 vert[4] = vec2[] +( + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2( 1.0, -1.0) +); + +const vec2 uv[4] = vec2[] +( + vec2(0.0, 1.0), + vec2(0.0, 0.0), + vec2(1.0, 1.0), + vec2(1.0, 0.0) +); + +out vec2 f_coords; + +void main() +{ + gl_Position = vec4(vert[gl_VertexID], 0.0, 1.0); + f_coords = vec2(mat3(projection) * vec3(uv[gl_VertexID], 1.0)); +} + diff --git a/shaders/tonemapping.glsl b/shaders/tonemapping.glsl new file mode 100644 index 00000000..de01a258 --- /dev/null +++ b/shaders/tonemapping.glsl @@ -0,0 +1,47 @@ +const float pureWhite = 1.0; + +vec3 reinhard(vec3 x) +{ +// return x / (x + vec3(1.0)); + + // Reinhard tonemapping operator. + // see: "Photographic Tone Reproduction for Digital Images", eq. 4 + float luminance = dot(x, vec3(0.2126, 0.7152, 0.0722)); + float mappedLuminance = (luminance * (1.0 + luminance/(pureWhite*pureWhite))) / (1.0 + luminance); + // Scale color by ratio of average luminances. + return (mappedLuminance / luminance) * x; +} + +// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ +vec3 ACESFilm(vec3 x) +{ + float a = 2.51f; + float b = 0.03f; + float c = 2.43f; + float d = 0.59f; + float e = 0.14f; + return (x*(a*x+b))/(x*(c*x+d)+e); +} + +// https://www.slideshare.net/ozlael/hable-john-uncharted2-hdr-lighting +vec3 filmicF(vec3 x) +{ + float A = 0.22f; + float B = 0.30f; + float C = 0.10f; + float D = 0.20f; + float E = 0.01f; + float F = 0.30f; + return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F)) - E/F; +} + +vec3 filmic(vec3 x) +{ + return filmicF(x) / filmicF(vec3(11.2f)); +} + +vec4 tonemap(vec4 x) +{ +// return FBOUT(vec4(ACESFilm(x.rgb), x.a)); + return FBOUT(vec4(reinhard(x.rgb), x.a)); +} diff --git a/shaders/traction.frag b/shaders/traction.frag new file mode 100644 index 00000000..f80639df --- /dev/null +++ b/shaders/traction.frag @@ -0,0 +1,32 @@ +#include + +in vec4 f_pos; +in vec4 f_clip_pos; +in vec4 f_clip_future_pos; + +layout(location = 0) out vec4 out_color; +#if MOTIONBLUR_ENABLED +layout(location = 1) out vec4 out_motion; +#endif + +#include +#include + +void main() +{ + vec4 color = vec4(apply_fog(pow(param[0].rgb, vec3(2.2))), param[0].a); +#if POSTFX_ENABLED + out_color = color; +#else + out_color = tonemap(color); +#endif + +#if MOTIONBLUR_ENABLED + { + vec2 a = (f_clip_future_pos.xy / f_clip_future_pos.w) * 0.5 + 0.5;; + vec2 b = (f_clip_pos.xy / f_clip_pos.w) * 0.5 + 0.5;; + + out_motion = vec4(a - b, 0.0f, 0.0f); + } +#endif +} diff --git a/shaders/traction.vert b/shaders/traction.vert new file mode 100644 index 00000000..55d5bd9a --- /dev/null +++ b/shaders/traction.vert @@ -0,0 +1,18 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec3 v_normal; +layout(location = 2) in vec2 v_coord; + +#include + +out vec4 f_clip_pos; +out vec4 f_clip_future_pos; +out vec4 f_pos; + +void main() +{ + f_pos = modelview * vec4(v_vert, 1.0f); + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0f); + f_clip_future_pos = (projection * future * modelview) * vec4(v_vert, 1.0f); + + gl_Position = f_clip_pos; +} diff --git a/shaders/vbocolor.vert b/shaders/vbocolor.vert new file mode 100644 index 00000000..af4d693e --- /dev/null +++ b/shaders/vbocolor.vert @@ -0,0 +1,17 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec3 v_color; + +out vec3 f_color; +out vec4 f_pos; +out vec4 f_clip_pos; + +#include + +void main() +{ + f_pos = modelview * vec4(v_vert, 1.0); + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0); + f_color = v_color; + + gl_Position = f_clip_pos; +} diff --git a/shaders/vertex.vert b/shaders/vertex.vert new file mode 100644 index 00000000..4d274e71 --- /dev/null +++ b/shaders/vertex.vert @@ -0,0 +1,46 @@ +layout(location = 0) in vec3 v_vert; +layout(location = 1) in vec3 v_normal; +layout(location = 2) in vec2 v_coord; +layout(location = 3) in vec4 v_tangent; + +#include + +out vec3 f_normal; +flat out vec3 f_normal_raw; +out vec2 f_coord; +out vec4 f_pos; +out mat3 f_tbn; +out vec4 f_light_pos[MAX_CASCADES]; + +out vec4 f_clip_pos; +out vec4 f_clip_future_pos; + +//out vec3 TangentLightPos; +//out vec3 TangentViewPos; +out vec3 TangentFragPos; + +void main() +{ + f_normal = normalize(modelviewnormal * v_normal); + f_normal_raw = v_normal; + f_coord = v_coord; + f_pos = modelview * vec4(v_vert, 1.0); + for (uint idx = 0U ; idx < MAX_CASCADES ; ++idx) { + f_light_pos[idx] = lightview[idx] * f_pos; + } + f_clip_pos = (projection * modelview) * vec4(v_vert, 1.0); + f_clip_future_pos = (projection * future * modelview) * vec4(v_vert, 1.0); + + gl_Position = f_clip_pos; + gl_PointSize = param[1].x; + + vec3 T = normalize(modelviewnormal * v_tangent.xyz); + vec3 N = f_normal; + vec3 B = normalize(cross(N, T)); + f_tbn = mat3(T, B, N); + + mat3 TBN = transpose(f_tbn); +// TangentLightPos = TBN * f_light_pos.xyz; +// TangentViewPos = TBN * vec3(0.0, 0.0, 0.0); + TangentFragPos = TBN * f_pos.xyz; +} diff --git a/shaders/vertexonly.vert b/shaders/vertexonly.vert new file mode 100644 index 00000000..c121b09b --- /dev/null +++ b/shaders/vertexonly.vert @@ -0,0 +1,8 @@ +layout(location = 0) in vec3 v_vert; + +#include + +void main() +{ + gl_Position = (projection * modelview) * vec4(v_vert, 1.0); +}