From 54dccc480eb03adcf219a7add58f547284f40f76 Mon Sep 17 00:00:00 2001 From: Dmitry Kostenko Date: Thu, 4 Nov 2021 03:03:10 +0100 Subject: Improve lighting of entities. Pass correct natural & artificial light to the shaders Use natural/artificial light ratio for correct rendering of shadows --- client/shaders/object_shader/opengl_fragment.glsl | 2 +- client/shaders/object_shader/opengl_vertex.glsl | 30 ++++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'client/shaders/object_shader') diff --git a/client/shaders/object_shader/opengl_fragment.glsl b/client/shaders/object_shader/opengl_fragment.glsl index 674b6a739..0dcbbd321 100644 --- a/client/shaders/object_shader/opengl_fragment.glsl +++ b/client/shaders/object_shader/opengl_fragment.glsl @@ -471,7 +471,7 @@ void main(void) color = base.rgb; vec4 col = vec4(color.rgb, base.a); col.rgb *= varColor.rgb; - col.rgb *= emissiveColor.rgb * vIDiff; + col.rgb *= vIDiff; #ifdef ENABLE_DYNAMIC_SHADOWS float shadow_int = 0.0; diff --git a/client/shaders/object_shader/opengl_vertex.glsl b/client/shaders/object_shader/opengl_vertex.glsl index 922fba62b..9ca5ef0f3 100644 --- a/client/shaders/object_shader/opengl_vertex.glsl +++ b/client/shaders/object_shader/opengl_vertex.glsl @@ -1,7 +1,9 @@ uniform mat4 mWorld; - +uniform vec3 dayLight; uniform vec3 eyePosition; uniform float animationTimer; +uniform vec4 emissiveColor; + varying vec3 vNormal; varying vec3 vPosition; @@ -29,9 +31,9 @@ centroid varying vec2 varTexCoord; varying vec3 eyeVec; varying float nightRatio; - +// Color of the light emitted by the light sources. +const vec3 artificialLight = vec3(1.04, 1.04, 1.04); varying float vIDiff; - const float e = 2.718281828459; const float BS = 10.0; @@ -75,14 +77,30 @@ void main(void) ? 1.0 : directional_ambient(normalize(inVertexNormal)); #endif - nightRatio = 0.0; #ifdef GL_ES - varColor = inVertexColor.bgra; + vec4 color = inVertexColor.bgra; #else - varColor = inVertexColor; + vec4 color = inVertexColor; #endif + color *= emissiveColor; + + // The alpha gives the ratio of sunlight in the incoming light. + nightRatio = 1.0 - color.a; + color.rgb = color.rgb * (color.a * dayLight.rgb + + nightRatio * artificialLight.rgb) * 2.0; + color.a = 1.0; + + // Emphase blue a bit in darker places + // See C++ implementation in mapblock_mesh.cpp final_color_blend() + float brightness = (color.r + color.g + color.b) / 3.0; + color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) + + 0.07 * brightness); + + varColor = clamp(color, 0.0, 1.0); + + #ifdef ENABLE_DYNAMIC_SHADOWS vec3 nNormal = normalize(vNormal); cosLight = dot(nNormal, -v_LightDirection); -- cgit v1.2.3