diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl index 0d26e1e2..fd91eab6 100644 --- a/shaders/light_common.glsl +++ b/shaders/light_common.glsl @@ -42,6 +42,7 @@ float calc_shadow() #endif } +float glossiness = 1.0; // [0] - diffuse, [1] - specular // do magic here vec2 calc_light(vec3 light_dir, vec3 fragnormal) @@ -50,7 +51,7 @@ vec2 calc_light(vec3 light_dir, vec3 fragnormal) 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; + float specular_v = pow(max(dot(fragnormal, halfway_dir), 0.0), max(glossiness, 0.01)) * diffuse_v; return vec2(diffuse_v, specular_v); } @@ -106,6 +107,8 @@ vec2 calc_headlights(light_s light, vec3 fragnormal) return part * atten * lightintensity; } + bool metalic = false; + vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) { vec3 emissioncolor = param[0].rgb * emission; @@ -147,15 +150,15 @@ vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float refl } fragcolor += emissioncolor; vec3 specularcolor = specularamount * lights[0].color; - if (param[1].w >= 0.0) + if ((param[1].w < 0.0) || (metalic == true)) { - fragcolor *= texturecolor; fragcolor += specularcolor; + fragcolor *= texturecolor; } else { - fragcolor += specularcolor; fragcolor *= texturecolor; + fragcolor += specularcolor; } return fragcolor; diff --git a/shaders/mat_colored.frag b/shaders/mat_colored.frag index 2d808326..47b350f3 100644 --- a/shaders/mat_colored.frag +++ b/shaders/mat_colored.frag @@ -33,6 +33,7 @@ void main() vec3 fragnormal = normalize(f_normal); float reflectivity = param[1].z; float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); diff --git a/shaders/mat_default.frag b/shaders/mat_default.frag index 89cf61cc..826dedee 100644 --- a/shaders/mat_default.frag +++ b/shaders/mat_default.frag @@ -39,6 +39,7 @@ void main() vec3 fragnormal = normalize(f_normal); float reflectivity = param[1].z; float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); diff --git a/shaders/mat_default_specgloss.frag b/shaders/mat_default_specgloss.frag new file mode 100644 index 00000000..6cc35a6e --- /dev/null +++ b/shaders/mat_default_specgloss.frag @@ -0,0 +1,70 @@ +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; + +#texture (specgloss, 1, RGBA) +uniform sampler2D specgloss; + +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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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_normalmap_specgloss.frag b/shaders/mat_normalmap_specgloss.frag new file mode 100644 index 00000000..e3d6eb55 --- /dev/null +++ b/shaders/mat_normalmap_specgloss.frag @@ -0,0 +1,74 @@ +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; + +#texture (specgloss, 2, RGBA) +uniform sampler2D specgloss; + + +#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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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_specgloss.frag b/shaders/mat_parallax_specgloss.frag new file mode 100644 index 00000000..d9ac41bd --- /dev/null +++ b/shaders/mat_parallax_specgloss.frag @@ -0,0 +1,127 @@ +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; + +#texture (specgloss, 2, RGBA) +uniform sampler2D specgloss; + + +#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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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 index 24d139d5..3b02e092 100644 --- a/shaders/mat_reflmap.frag +++ b/shaders/mat_reflmap.frag @@ -41,7 +41,8 @@ void main() 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; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); diff --git a/shaders/mat_reflmap_specgloss.frag b/shaders/mat_reflmap_specgloss.frag new file mode 100644 index 00000000..a95db18d --- /dev/null +++ b/shaders/mat_reflmap_specgloss.frag @@ -0,0 +1,72 @@ +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; + +#texture (specgloss, 2, RGBA) +uniform sampler2D specgloss; + + +#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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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 index b2b49b1f..cb2c040e 100644 --- a/shaders/mat_shadowlessnormalmap.frag +++ b/shaders/mat_shadowlessnormalmap.frag @@ -47,7 +47,8 @@ void main() 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; + float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; + glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, 1.0); vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); diff --git a/shaders/mat_shadowlessnormalmap_specgloss.frag b/shaders/mat_shadowlessnormalmap_specgloss.frag new file mode 100644 index 00000000..1c30b58e --- /dev/null +++ b/shaders/mat_shadowlessnormalmap_specgloss.frag @@ -0,0 +1,73 @@ +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; + +#texture (specgloss, 2, RGBA) +uniform sampler2D specgloss; + + +#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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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 index 9da44100..aabc3ef3 100644 --- a/shaders/mat_stars.frag +++ b/shaders/mat_stars.frag @@ -15,6 +15,7 @@ void main() 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 diff --git a/shaders/mat_sunlessnormalmap.frag b/shaders/mat_sunlessnormalmap.frag index 380a17b0..361b00db 100644 --- a/shaders/mat_sunlessnormalmap.frag +++ b/shaders/mat_sunlessnormalmap.frag @@ -43,6 +43,7 @@ vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, fl float diffuseamount = (sunlight.x * param[1].x) * lights[0].intensity; fragcolor += envcolor * reflectivity; float specularamount = (sunlight.y * param[1].y * specularity) * lights[0].intensity; + glossiness = abs(param[1].w); for (uint i = 1U; i < lights_count; i++) { diff --git a/shaders/mat_sunlessnormalmap_specgloss.frag b/shaders/mat_sunlessnormalmap_specgloss.frag new file mode 100644 index 00000000..44d87c63 --- /dev/null +++ b/shaders/mat_sunlessnormalmap_specgloss.frag @@ -0,0 +1,116 @@ +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; + +#texture (specgloss, 2, RGBA) +uniform sampler2D specgloss; + + +#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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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 index 23e933cb..ca461f21 100644 --- a/shaders/mat_water.frag +++ b/shaders/mat_water.frag @@ -59,6 +59,7 @@ void main() vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz)); float reflectivity = param[1].z * texture(normalmap, texture_coords ).a; float specularity = 1.0; + glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); diff --git a/shaders/mat_water_specgloss.frag b/shaders/mat_water_specgloss.frag new file mode 100644 index 00000000..a6d0dbb6 --- /dev/null +++ b/shaders/mat_water_specgloss.frag @@ -0,0 +1,95 @@ +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; + +#texture (specgloss, 3, RGBA) +uniform sampler2D specgloss; + + +//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 = texture(specgloss, f_coord).r; + glossiness = texture(specgloss, f_coord).g * 8; + metalic = (texture(specgloss, f_coord).b > 0.5) ? true : false; + + 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 +}