From 89231426e34d2c93a7ad627f4c9c13b13eb7a1c6 Mon Sep 17 00:00:00 2001 From: stele Date: Sat, 22 Dec 2018 16:44:22 +0100 Subject: [PATCH 1/4] Changed reflection value to normalmaps alfa. Fixed order in color conversion in stars shader. --- shaders/mat_normalmap.frag | 4 ++-- shaders/mat_reflmap.frag | 4 ++-- shaders/mat_stars.frag | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/shaders/mat_normalmap.frag b/shaders/mat_normalmap.frag index 044efe34..c4b7a4fa 100644 --- a/shaders/mat_normalmap.frag +++ b/shaders/mat_normalmap.frag @@ -22,7 +22,7 @@ layout(location = 1) out vec4 out_motion; #texture (diffuse, 0, sRGB_A) uniform sampler2D diffuse; -#texture (normalmap, 1, RGB) +#texture (normalmap, 1, RGBA) uniform sampler2D normalmap; #if SHADOWMAP_ENABLED @@ -58,7 +58,7 @@ void main() { vec2 part = calc_dir_light(lights[0]); vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color; - result += mix(c, envcolor, param[1].z); + result += mix(c, envcolor, param[1].z * texture(normalmap, f_coord).a); } for (uint i = 1U; i < lights_count; i++) diff --git a/shaders/mat_reflmap.frag b/shaders/mat_reflmap.frag index 50afdd38..fc1df7d3 100644 --- a/shaders/mat_reflmap.frag +++ b/shaders/mat_reflmap.frag @@ -21,7 +21,7 @@ layout(location = 1) out vec4 out_motion; #texture (diffuse, 0, sRGB_A) uniform sampler2D diffuse; -#texture (reflmap, 1, R) +#texture (reflmap, 1, RGBA) uniform sampler2D reflmap; #if SHADOWMAP_ENABLED @@ -56,7 +56,7 @@ void main() { vec2 part = calc_dir_light(lights[0]); vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color; - result += mix(c, envcolor, param[1].z * texture(reflmap, f_coord).r); + result += mix(c, envcolor, param[1].z * texture(reflmap, f_coord).a); } for (uint i = 1U; i < lights_count; i++) diff --git a/shaders/mat_stars.frag b/shaders/mat_stars.frag index 1a324857..aabc3ef3 100644 --- a/shaders/mat_stars.frag +++ b/shaders/mat_stars.frag @@ -17,7 +17,7 @@ void main() discard; // color data space is shared with normals, ugh - vec4 color = vec4(pow(f_normal_raw.bgr, vec3(2.2)), 1.0f); + vec4 color = vec4(pow(f_normal_raw.rgb, vec3(2.2)), 1.0f); #if POSTFX_ENABLED out_color = color; #else From b7d6d80225972791f87dc68fb6e9026d4a4ae6ac Mon Sep 17 00:00:00 2001 From: stele Date: Sat, 22 Dec 2018 17:34:03 +0100 Subject: [PATCH 2/4] Added water shader --- shaders/light_common.glsl | 2 + shaders/mat_water.frag | 115 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 shaders/mat_water.frag diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl index f4a9ec78..5d53a25d 100644 --- a/shaders/light_common.glsl +++ b/shaders/light_common.glsl @@ -39,6 +39,8 @@ vec2 calc_light(vec3 light_dir) { #ifdef NORMALMAP vec3 normal = normalize(f_tbn * normalize(texture(normalmap, f_coord).rgb * 2.0 - 1.0)); +#elif defined(WATER) + vec3 normal = normal_d; #else vec3 normal = normalize(f_normal); #endif diff --git a/shaders/mat_water.frag b/shaders/mat_water.frag new file mode 100644 index 00000000..72a5de06 --- /dev/null +++ b/shaders/mat_water.frag @@ -0,0 +1,115 @@ +in vec3 f_normal; +in vec2 f_coord; +in vec4 f_pos; +in mat3 f_tbn; +in vec4 f_light_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 (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; + +#if SHADOWMAP_ENABLED +uniform sampler2DShadow shadowmap; +#endif +#if ENVMAP_ENABLED +uniform samplerCube envmap; +#endif + +//wave distortion variables +float move_factor = 0; +vec3 normal_d; + +#define WATER +#include +#include + +void main() +{ + + vec4 tex_color = texture(diffuse, f_coord); +//wave distortion + move_factor += (param[2].z * time); + move_factor = mod(move_factor, 1); + 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; + + normal_d = f_tbn * normalize(texture(normalmap, texture_coords).rgb * 2.0 - 1.0); + vec3 refvec = reflect(f_pos.xyz, normal_d); +#if ENVMAP_ENABLED + vec3 envcolor = texture(envmap, refvec).rgb; +#else + vec3 envcolor = vec3(0.5); +#endif +//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 ); + + vec3 result = ambient * 0.5 + param[0].rgb * emission; + + if (lights_count > 0U) + { + vec2 part = calc_dir_light(lights[0]); + vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color; + result += mix(c, envcolor, param[1].z * texture(normalmap, texture_coords ).a); + } + //result = mix(result, param[0].rgb, param[1].z); + result = (result * param[0].rgb * param[1].z) + (result * (1.0 - param[1].z)); //multiply + //result = ( max(result + param[0].rgb -1.0,0.0) * param[1].z + result * (1.0 - param[1].z)); //linear burn + //result = ( min(param[0].rgb,result) * param[1].z + result * (1.0 - param[1].z)); //darken + + 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); + else if (light.type == LIGHT_POINT) + part = calc_point_light(light); + else if (light.type == LIGHT_DIR) + part = calc_dir_light(light); + result += light.color * (part.x * param[1].x + part.y * param[1].y); + } + + vec4 color = vec4(apply_fog(clamp(result,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 +} From 141e81230805e2d73cd3096dc54c1f52b4e401b6 Mon Sep 17 00:00:00 2001 From: stele Date: Sat, 22 Dec 2018 19:06:39 +0100 Subject: [PATCH 3/4] Using diffuse texture in water shader. --- shaders/mat_water.frag | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shaders/mat_water.frag b/shaders/mat_water.frag index 72a5de06..2b0649bf 100644 --- a/shaders/mat_water.frag +++ b/shaders/mat_water.frag @@ -47,8 +47,6 @@ vec3 normal_d; void main() { - - vec4 tex_color = texture(diffuse, f_coord); //wave distortion move_factor += (param[2].z * time); move_factor = mod(move_factor, 1); @@ -65,6 +63,7 @@ void main() #else vec3 envcolor = vec3(0.5); #endif + vec4 tex_color = texture(diffuse, texture_coords); //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 ); @@ -79,7 +78,7 @@ void main() result += mix(c, envcolor, param[1].z * texture(normalmap, texture_coords ).a); } //result = mix(result, param[0].rgb, param[1].z); - result = (result * param[0].rgb * param[1].z) + (result * (1.0 - param[1].z)); //multiply + result = (result * tex_color.rgb * param[1].z) + (result * (1.0 - param[1].z)); //multiply //result = ( max(result + param[0].rgb -1.0,0.0) * param[1].z + result * (1.0 - param[1].z)); //linear burn //result = ( min(param[0].rgb,result) * param[1].z + result * (1.0 - param[1].z)); //darken From 8aca8bffe4e3a0a7a0731b025b3a847696f2eecb Mon Sep 17 00:00:00 2001 From: stele Date: Mon, 24 Dec 2018 23:50:17 +0100 Subject: [PATCH 4/4] Fog applied to line objects. --- shaders/apply_fog.glsl | 9 +++++++++ shaders/light_common.glsl | 12 ++---------- shaders/traction.frag | 4 +++- shaders/traction.vert | 2 ++ 4 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 shaders/apply_fog.glsl diff --git a/shaders/apply_fog.glsl b/shaders/apply_fog.glsl new file mode 100644 index 00000000..fc58fb15 --- /dev/null +++ b/shaders/apply_fog.glsl @@ -0,0 +1,9 @@ +vec3 apply_fog(vec3 color) +{ + float sun_amount = 0.0; + if (lights_count >= 1U && lights[0].type == LIGHT_DIR) + sun_amount = max(dot(normalize(f_pos.xyz), normalize(-lights[0].dir)), 0.0); + vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0)); + float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos.xyz) * fog_density)); + return mix(color, fog_color_v, fog_amount_v); +} \ No newline at end of file diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl index 5d53a25d..aed7d107 100644 --- a/shaders/light_common.glsl +++ b/shaders/light_common.glsl @@ -1,3 +1,5 @@ +#include + float calc_shadow() { #if SHADOWMAP_ENABLED @@ -23,16 +25,6 @@ float calc_shadow() #endif } -vec3 apply_fog(vec3 color) -{ - float sun_amount = 0.0; - if (lights_count >= 1U && lights[0].type == LIGHT_DIR) - sun_amount = max(dot(normalize(f_pos.xyz), normalize(-lights[0].dir)), 0.0); - vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0)); - float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos.xyz) * fog_density)); - return mix(color, fog_color_v, fog_amount_v); -} - // [0] - diffuse, [1] - specular // do magic here vec2 calc_light(vec3 light_dir) diff --git a/shaders/traction.frag b/shaders/traction.frag index af40e8e3..f80639df 100644 --- a/shaders/traction.frag +++ b/shaders/traction.frag @@ -1,5 +1,6 @@ #include +in vec4 f_pos; in vec4 f_clip_pos; in vec4 f_clip_future_pos; @@ -9,10 +10,11 @@ layout(location = 1) out vec4 out_motion; #endif #include +#include void main() { - vec4 color = vec4(pow(param[0].rgb, vec3(2.2)), param[0].a); + vec4 color = vec4(apply_fog(pow(param[0].rgb, vec3(2.2))), param[0].a); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/traction.vert b/shaders/traction.vert index 87ff1fa4..55d5bd9a 100644 --- a/shaders/traction.vert +++ b/shaders/traction.vert @@ -6,9 +6,11 @@ layout(location = 2) in vec2 v_coord; 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);