Skip to content

Commit

Permalink
Applied the nauta-turbidus' patch with some fixes, returned the check…
Browse files Browse the repository at this point in the history
…s of the remaining bytes for Lighting

Co-authored-by: nauta-turbidus
  • Loading branch information
Andrey2470T committed Oct 6, 2024
1 parent b05952f commit b3bc9b5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 64 deletions.
9 changes: 5 additions & 4 deletions client/shaders/nodes_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ varying vec3 vPosition;
// cameraOffset + worldPosition (for large coordinates the limits of float
// precision must be considered).
varying vec3 worldPosition;
varying lowp vec4 varColor;
varying lowp vec3 lightColor;
varying vec3 hardwareColor;
#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
Expand Down Expand Up @@ -432,7 +433,7 @@ void main(void)
#endif

color = base.rgb;
vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
vec4 col = vec4(color.rgb * hardwareColor.rgb * lightColor.rgb, 1.0);

#ifdef ENABLE_DYNAMIC_SHADOWS
// Fragment normal, can differ from vNormal which is derived from vertex normals.
Expand Down Expand Up @@ -535,7 +536,7 @@ void main(void)
#if (defined(ENABLE_NODE_SPECULAR) && !defined(MATERIAL_WAVING_LIQUID))
// Apply specular to blocks.
if (dot(v_LightDirection, vNormal) < 0.0) {
float intensity = 2.0 * (1.0 - (base.r * varColor.r));
float intensity = 2.0 * (1.0 - (base.r * lightColor.r));
const float specular_exponent = 5.0;
const float fresnel_exponent = 4.0;

Expand All @@ -547,7 +548,7 @@ void main(void)

#if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES) && defined(ENABLE_TRANSLUCENT_FOLIAGE)
// Simulate translucent foliage.
col.rgb += 4.0 * dayLight * base.rgb * normalize(base.rgb * varColor.rgb * varColor.rgb) * f_adj_shadow_strength * pow(max(-dot(v_LightDirection, viewVec), 0.0), 4.0) * max(1.0 - shadow_uncorrected, 0.0);
col.rgb += 4.0 * dayLight * base.rgb * normalize(base.rgb * lightColor.rgb * lightColor.rgb) * f_adj_shadow_strength * pow(max(-dot(v_LightDirection, viewVec), 0.0), 4.0) * max(1.0 - shadow_uncorrected, 0.0);
#endif
}
#endif
Expand Down
30 changes: 21 additions & 9 deletions client/shaders/nodes_shader/opengl_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ varying vec3 vPosition;
// cameraOffset + worldPosition (for large coordinates the limits of float
// precision must be considered).
varying vec3 worldPosition;
varying lowp vec4 varColor;
varying lowp vec3 lightColor;
varying vec3 hardwareColor;
// The centroid keyword ensures that after interpolation the texture coordinates
// lie within the same bounds when MSAA is en- and disabled.
// This fixes the stripes problem with nearest-neighbor textures and MSAA.
Expand Down Expand Up @@ -200,26 +201,37 @@ void main(void)
vNormal = inVertexNormal;

// Calculate color.
vec4 color = inVertexColor;

// Extract the hardware color and send that
// to the fragment shader for mixing with texture color.
hardwareColor.rgb = inVertexColor.rgb;

// Unpack the values from alpha
float ratio = floor(inVertexColor.a*16)/16;
float lightBase = floor((inVertexColor.a - ratio)*256)/16;

// Red, green and blue components are pre-multiplied with
// the brightness, so now we have to multiply these
// colors with the color of the incoming light.
// The pre-baked colors are halved to prevent overflow.
// 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 = 1.0 - ratio;

// Modify the color with the base light
vec3 light = vec3(lightBase, lightBase, lightBase);
light.rgb = light.rgb * (ratio * 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) +
float brightness = (light.r + light.g + light.b) / 3.0;
light.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
0.07 * brightness);

color.rgb += ambientLight;
// In the end add the normalized ambient light and clamp that.
light.rgb += ambientLight;

varColor = clamp(color, 0.0, 1.0);
lightColor = clamp(light, 0.0, 1.0);

#ifdef ENABLE_DYNAMIC_SHADOWS
if (f_shadow_strength > 0.0) {
Expand Down
22 changes: 13 additions & 9 deletions client/shaders/object_shader/opengl_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,29 @@ void main(void)
: directional_ambient(normalize(inVertexNormal));
#endif

vec4 color = inVertexColor;
// Calculate color.

color *= materialColor;
// Unpack the values from alpha
float ratio = floor(materialColor.a*16)/16;
float lightBase = floor((materialColor.a - ratio)*256)/16;

vec3 light = vec3(lightBase, lightBase, lightBase);

// 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 = 1.0 - ratio;
light.rgb = light.rgb * (ratio * 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) +
float brightness = (light.r + light.g + light.b) / 3.0;
light.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
0.07 * brightness);

color.rgb += ambientLight;
// In the end add the normalized ambient light and clamp that.
light.rgb += ambientLight;

varColor = clamp(color, 0.0, 1.0);
varColor = vec4(clamp(light.rgb, 0.0, 1.0), 1.0);


#ifdef ENABLE_DYNAMIC_SHADOWS
Expand Down
8 changes: 2 additions & 6 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,12 +907,8 @@ void GenericCAO::updateLight(u32 day_night_ratio)

if (m_enable_shaders)
light = encode_light(light_at_pos, m_prop.glow);
else {
video::SColor ambient_light = g_settings->getBool("enable_shaders") ?
m_env->getLocalPlayer()->getLighting().ambient_light : video::SColor(255, 0, 0, 0);

final_color_blend(&light, light_at_pos, day_night_ratio, ambient_light);
}
else
final_color_blend(&light, light_at_pos, day_night_ratio);

if (light != m_last_light) {
m_last_light = light;
Expand Down
53 changes: 25 additions & 28 deletions src/client/mapblock_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,29 @@ void final_color_blend(video::SColor *result,
{
static const video::SColorf artificialColor(1.04f, 1.04f, 1.04f);

video::SColorf c(data);
f32 n = 1 - c.a;

f32 r = c.r * (c.a * dayLight.r + n * artificialColor.r) * 2.0f;
f32 g = c.g * (c.a * dayLight.g + n * artificialColor.g) * 2.0f;
f32 b = c.b * (c.a * dayLight.b + n * artificialColor.b) * 2.0f;

// Emphase blue a bit in darker places
// Each entry of this array represents a range of 8 blue levels
static const u8 emphase_blue_when_dark[32] = {
1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
f32 ratio = std::floor(data.getAlpha() / 255.0f * 16.0f)/16.0f;
f32 lightBase = std::floor((data.getAlpha() / 255.0f - ratio)*256.0f)/16.0f;

video::SColorf light(lightBase, lightBase, lightBase, 1.0f);

f32 nightRatio = 1.0f - ratio;

light.r *= ((ratio * dayLight.r + nightRatio * artificialColor.r) * 2.0f);
light.g *= ((ratio * dayLight.g + nightRatio * artificialColor.g) * 2.0f);
light.b *= ((ratio * dayLight.b + nightRatio * artificialColor.b) * 2.0f);

b += emphase_blue_when_dark[irr::core::clamp((s32) ((r + g + b) / 3 * 255),
0, 255) / 8] / 255.0f;
f32 brightness = (light.r + light.g + light.b) / 3.0f;
light.b += std::max(0.0f, 0.021f - std::fabs(0.2f * brightness - 0.021f) +
0.07f * brightness);

// Add ambient light
r += ambientLight.r;
g += ambientLight.g;
b += ambientLight.b;
light.r += ambientLight.r;
light.g += ambientLight.g;
light.b += ambientLight.b;

result->setRed(core::clamp((s32) (r * 255.0f), 0, 255));
result->setGreen(core::clamp((s32) (g * 255.0f), 0, 255));
result->setBlue(core::clamp((s32) (b * 255.0f), 0, 255));
result->setRed(core::clamp((s32) (light.r * 255.0f), 0, 255));
result->setGreen(core::clamp((s32) (light.g * 255.0f), 0, 255));
result->setBlue(core::clamp((s32) (light.b * 255.0f), 0, 255));
}

/*
Expand Down Expand Up @@ -433,10 +431,9 @@ static void applyTileColor(PreMeshBuffer &pmb)
return;
for (video::S3DVertex &vertex : pmb.vertices) {
video::SColor *c = &vertex.Color;
c->set(c->getAlpha(),
c->getRed() * tc.getRed() / 255,
c->getGreen() * tc.getGreen() / 255,
c->getBlue() * tc.getBlue() / 255);
c->setRed(tc.getRed());
c->setGreen(tc.getGreen());
c->setBlue(tc.getBlue());
}
}

Expand Down Expand Up @@ -973,12 +970,12 @@ video::SColor encode_light(u16 light, u8 emissive_light)
// Ratio of sunlight:
u32 r;
if (sum > 0)
r = day * 255 / sum;
r = day * 15 / sum;
else
r = 0;
// Average light:
float b = (day + night) / 2;
return video::SColor(r, b, b, b);
u32 b = (day + night) / 30;
return video::SColor((r<<4)|b, 255, 255, 255);
}

u8 get_solid_sides(MeshMakeData *data)
Expand Down
15 changes: 8 additions & 7 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,21 +1803,22 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
{
Lighting& lighting = m_env.getLocalPlayer()->getLighting();

//if (pkt->getRemainingBytes() >= 4)
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.shadow_intensity;
*pkt >> lighting.ambient_light;
//if (pkt->getRemainingBytes() >= 4)
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.saturation;
//if (pkt->getRemainingBytes() >= 24) {
if (pkt->getRemainingBytes() >= 24) {
*pkt >> lighting.exposure.luminance_min
>> lighting.exposure.luminance_max
>> lighting.exposure.exposure_correction
>> lighting.exposure.speed_dark_bright
>> lighting.exposure.speed_bright_dark
>> lighting.exposure.center_weight_power;
//}
//if (pkt->getRemainingBytes() >= 4)
}
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.volumetric_light_strength;
//if (pkt->getRemainingBytes() >= 4)
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.shadow_tint;
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.ambient_light;
}
3 changes: 2 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,6 @@ void Server::SendSetLighting(session_t peer_id, const Lighting &lighting)
4, peer_id);

pkt << lighting.shadow_intensity;
pkt << lighting.ambient_light;
pkt << lighting.saturation;

pkt << lighting.exposure.luminance_min
Expand All @@ -1921,6 +1920,8 @@ void Server::SendSetLighting(session_t peer_id, const Lighting &lighting)

pkt << lighting.volumetric_light_strength << lighting.shadow_tint;

pkt << lighting.ambient_light;

Send(&pkt);
}

Expand Down

0 comments on commit b3bc9b5

Please sign in to comment.