Skip to content

Commit 3e3602a

Browse files
committed
Move ambient-occlusion gamma into static lighting API
1 parent e279de4 commit 3e3602a

File tree

11 files changed

+36
-26
lines changed

11 files changed

+36
-26
lines changed

builtin/settingtypes.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,6 @@ fov (Field of view) int 72 45 160
514514
# light, it has very little effect on natural night light.
515515
display_gamma (Light curve gamma) float 1.0 0.33 3.0
516516

517-
# The strength (darkness) of node ambient-occlusion shading.
518-
# Lower is darker, Higher is lighter. The valid range of values for this
519-
# setting is 0.25 to 4.0 inclusive. If the value is out of range it will be
520-
# set to the nearest valid value.
521-
ambient_occlusion_gamma (Ambient occlusion gamma) float 1.8 0.25 4.0
522-
523517
[**Screenshots]
524518

525519
# Path to save screenshots at. Can be an absolute or relative path.

doc/lua_api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9156,6 +9156,9 @@ child will follow movement and rotation of that bone.
91569156
* table containing 16 numbers or an empty table to reset to the default
91579157
* The values should be monotonically increasing and the last value should
91589158
be 255. Otherwise there might be lighting bugs.
9159+
* `ao_gamma`: Strength of node ambient-occlusion shading. Lower is darker, Higher is lighter.
9160+
* Recommended range: from 0.25 to 3.0, default: 1.8
9161+
* A value of 0.0 will disable this shading entirely.
91599162

91609163
* `get_lighting()`: returns the current state of lighting for the player.
91619164
* Result is a table with the same fields as `light_definition` in `set_lighting`.

src/client/mapblock_mesh.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ static u16 getSmoothLightCombined(const v3s16 &p,
144144
{
145145
const NodeDefManager *ndef = data->m_nodedef;
146146

147+
assert(data->m_smooth_lighting);
148+
147149
u16 ambient_occlusion = 0;
148150
u16 light_count = 0;
149151
u8 light_source_max = 0;
@@ -160,12 +162,12 @@ static u16 getSmoothLightCombined(const v3s16 &p,
160162
if (n.getContent() == CONTENT_IGNORE)
161163
return true;
162164
const ContentFeatures &f = ndef->get(n);
163-
if (f.light_source > light_source_max)
164-
light_source_max = f.light_source;
165+
light_source_max = std::max(light_source_max, f.light_source);
165166
// Check f.solidness because fast-style leaves look better this way
166167
if (f.param_type == CPT_LIGHT && f.visuals->solidness != 2) {
167-
u8 light_level_day = n.getLight(LIGHTBANK_DAY, f.getLightingFlags());
168-
u8 light_level_night = n.getLight(LIGHTBANK_NIGHT, f.getLightingFlags());
168+
const auto lf = f.getLightingFlags();
169+
u8 light_level_day = n.getLight(LIGHTBANK_DAY, lf);
170+
u8 light_level_night = n.getLight(LIGHTBANK_NIGHT, lf);
169171
if (light_level_day == LIGHT_SUN)
170172
direct_sunlight = true;
171173
light_day += decode_light(light_level_day);
@@ -219,25 +221,20 @@ static u16 getSmoothLightCombined(const v3s16 &p,
219221
}
220222

221223
if (ambient_occlusion > 4) {
222-
static thread_local const float ao_gamma = rangelim(
223-
g_settings->getFloat("ambient_occlusion_gamma"), 0.25, 4.0);
224-
225-
// Table of gamma space multiply factors.
226-
static thread_local const float light_amount[3] = {
227-
powf(0.75, 1.0 / ao_gamma),
228-
powf(0.5, 1.0 / ao_gamma),
229-
powf(0.25, 1.0 / ao_gamma)
230-
};
231-
232-
//calculate table index for gamma space multiplier
233-
ambient_occlusion -= 5;
224+
float light_amount; // gamma space multiplier
225+
if (ambient_occlusion == 5)
226+
light_amount = std::pow(0.75f, data->m_ao_gamma_inv);
227+
else if (ambient_occlusion == 6)
228+
light_amount = std::pow(0.75f, data->m_ao_gamma_inv);
229+
else // >= 7
230+
light_amount = std::pow(0.25f, data->m_ao_gamma_inv);
234231

235232
if (!skip_ambient_occlusion_day)
236233
light_day = rangelim(core::round32(
237-
light_day * light_amount[ambient_occlusion]), 0, 255);
234+
light_day * light_amount), 0, 255);
238235
if (!skip_ambient_occlusion_night)
239236
light_night = rangelim(core::round32(
240-
light_night * light_amount[ambient_occlusion]), 0, 255);
237+
light_night * light_amount), 0, 255);
241238
}
242239

243240
return light_day | (light_night << 8);

src/client/mapblock_mesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct MeshMakeData
4545

4646
// relative to blockpos
4747
v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
48+
float m_ao_gamma_inv = 0.0f;
4849
bool m_generate_minimap = false;
4950
bool m_smooth_lighting = false;
5051
bool m_enable_water_reflections = false;

src/client/mesh_generator_thread.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q)
236236
}
237237

238238
data->setCrack(q->crack_level, q->crack_pos);
239+
240+
// Changes to static lighting will cause a re-mesh of the entire map, so
241+
// this doesn't need an update mechanism.
242+
float ao_gamma = m_client->getCommittedStaticLighting().ao_gamma;
243+
data->m_ao_gamma_inv = ao_gamma == 0.0f ? 0.0f : (1.0f / ao_gamma);
244+
239245
data->m_generate_minimap = !!m_client->getMinimap();
240246
data->m_smooth_lighting = m_cache_smooth_lighting;
241247
data->m_enable_water_reflections = m_cache_enable_water_reflections;

src/defaultsettings.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ void set_default_settings()
295295
settings->setDefault("hud_hotbar_max_width", "1.0");
296296
settings->setDefault("enable_local_map_saving", "false");
297297
settings->setDefault("show_entity_selectionbox", "false");
298-
settings->setDefault("ambient_occlusion_gamma", "1.8");
299298
settings->setDefault("arm_inertia", "true");
300299
settings->setDefault("show_nametag_backgrounds", "true");
301300
settings->setDefault("show_block_bounds_radius_near", "4");

src/lighting.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
bool StaticLighting::operator==(const StaticLighting &other) const
1010
{
11-
if (light_curve_set != other.light_curve_set)
11+
if (light_curve_set != other.light_curve_set ||
12+
ao_gamma != other.ao_gamma)
1213
return false;
1314
if (light_curve_set && memcmp(light_curve, other.light_curve, sizeof(light_curve)) != 0)
1415
return false;

src/lighting.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ struct StaticLighting
5050
{
5151
static constexpr int LIGHT_CURVE_SIZE = 16;
5252

53+
// Light curve: used to decode map light (0-15) into intensity (0-255)
5354
bool light_curve_set = false;
5455
u8 light_curve[LIGHT_CURVE_SIZE];
5556

57+
// Ambient occlusion gamma
58+
float ao_gamma = 1.8f;
59+
5660
bool operator==(const StaticLighting &other) const;
5761
bool operator!=(const StaticLighting &other) const {
5862
return !(*this == other);

src/network/clientpackethandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,7 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
18411841
*pkt >> s.light_curve_set;
18421842
if (s.light_curve_set)
18431843
pkt->readRawString(reinterpret_cast<char*>(s.light_curve), sizeof(s.light_curve));
1844+
*pkt >> s.ao_gamma;
18441845

18451846
#undef IF_DONE_RETURN
18461847
}

src/script/lua_api/l_object.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,7 @@ int ObjectRef::l_set_lighting(lua_State *L)
27352735
s.light_curve_set = any;
27362736
}
27372737
lua_pop(L, 1); // light_curve
2738+
getfloatfield(L, -1, "ao_gamma", s.ao_gamma);
27382739
}
27392740
lua_pop(L, 1); // static
27402741

@@ -2803,6 +2804,8 @@ int ObjectRef::l_get_lighting(lua_State *L)
28032804
}
28042805
}
28052806
lua_setfield(L, -2, "light_curve");
2807+
lua_pushnumber(L, s.ao_gamma);
2808+
lua_setfield(L, -2, "ao_gamma");
28062809
lua_setfield(L, -2, "static");
28072810

28082811
return 1;

0 commit comments

Comments
 (0)