add 'sound-absorption' block material parameter

This commit is contained in:
MihailRis 2026-02-11 21:19:55 +03:00
parent b355e4568d
commit 20155b88d0
14 changed files with 82 additions and 28 deletions

View file

@ -1,5 +1,6 @@
{
"steps-sound": "steps/carpet",
"place-sound": "blocks/carpet_place",
"break-sound": "blocks/carpet_break"
"break-sound": "blocks/carpet_break",
"sound-absorption": 0.96
}

View file

@ -1,5 +1,6 @@
{
"steps-sound": "steps/glass",
"place-sound": "blocks/glass_place",
"break-sound": "blocks/glass_break"
"break-sound": "blocks/glass_break",
"sound-absorption": 0.3
}

View file

@ -1,5 +1,6 @@
{
"steps-sound": "steps/grass",
"place-sound": "steps/grass",
"break-sound": "steps/grass"
"break-sound": "steps/grass",
"sound-absorption": 0.85
}

View file

@ -2,5 +2,6 @@
"steps-sound": "steps/grass",
"place-sound": "blocks/ground_place",
"break-sound": "blocks/ground_break",
"hit-sound": "blocks/ground_hit"
"hit-sound": "blocks/ground_hit",
"sound-absorption": 0.8
}

View file

@ -2,5 +2,6 @@
"steps-sound": "steps/ground",
"place-sound": "blocks/ground_place",
"break-sound": "blocks/ground_break",
"hit-sound": "blocks/ground_hit"
"hit-sound": "blocks/ground_hit",
"sound-absorption": 0.7
}

View file

@ -1,5 +1,6 @@
{
"steps-sound": "steps/metal",
"place-sound": "blocks/metal_place",
"break-sound": "blocks/metal_break"
"break-sound": "blocks/metal_break",
"sound-absorption": 0.1
}

View file

@ -1,5 +1,6 @@
{
"steps-sound": "steps/sand",
"place-sound": "blocks/ground_place",
"break-sound": "blocks/ground_break"
"break-sound": "blocks/ground_break",
"sound-absorption": 0.8
}

View file

@ -2,5 +2,6 @@
"steps-sound": "steps/stone",
"place-sound": "blocks/stone_place",
"break-sound": "blocks/stone_break",
"hit-sound": "blocks/stone_hit"
"hit-sound": "blocks/stone_hit",
"sound-absorption": 0.3
}

View file

@ -2,5 +2,6 @@
"steps-sound": "steps/wood",
"place-sound": "blocks/wood_place",
"break-sound": "blocks/wood_break",
"hit-sound": "blocks/wood_hit"
"hit-sound": "blocks/wood_hit",
"sound-absorption": 0.5
}

View file

@ -788,10 +788,18 @@ void ALAudio::setAcoustics(Acoustics acoustics) {
return;
}
int reverbEffect = effects[REVERB_EFFECT];
AL_CHECK(alEffectf(
reverbEffect,
AL_REVERB_GAIN,
glm::max(0.1f, glm::min(acoustics.reverbGain, 0.8f) *
(1.0f - glm::min(1.0f, acoustics.reverbAbsorption)))
));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_REFLECTIONS_DELAY, acoustics.reverbReflectionsDelay));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_LATE_REVERB_DELAY, acoustics.reverbLateReflectionsDelay));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_DECAY_TIME, std::max<float>(0.1f, acoustics.reverbDecayTime)));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_ROOM_ROLLOFF_FACTOR, 0.14f));
// AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAIN, 0.0f));
// AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAINHF, 0.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_REFLECTIONS_GAIN, 0.99f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_ROOM_ROLLOFF_FACTOR, acoustics.reverbRoomRolloff));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_REFLECTIONS_GAIN, glm::pow(acoustics.reverbGain, 2.0f)));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_LATE_REVERB_GAIN, glm::pow(acoustics.reverbGain, 2.0f)));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_DECAY_HFRATIO, 1.0f - acoustics.reverbAbsorption));
alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, reverbEffect);
}

View file

@ -28,6 +28,11 @@ namespace audio {
};
struct Acoustics {
float reverbGain;
float reverbReflectionsDelay;
float reverbLateReflectionsDelay;
float reverbDecayTime;
float reverbRoomRolloff;
float reverbAbsorption;
};
}

View file

@ -97,34 +97,63 @@ void Decorator::updateAcoustics(const Camera& camera) {
const auto& start = camera.position;
float rayLength = 40.0f;
auto& blocks = level.content.getIndices()->blocks;
int rays = 100;
int hit = 0;
float averageDistance = 0.0f;
float averageAbsorption = 0.0f;
float speedOfSound = 300.0f;
float minDistance = rayLength;
for (int i = 0; i < rays; i++) {
glm::vec3 dir {
random.randFloat() - 0.5f,
random.randFloat() - 0.5f,
random.randFloat() - 0.5f,
};
float u1 = random.randFloat();
float u2 = random.randFloat();
float z = 2.0f * u1 - 1.0f;
float phi = 2.0f * glm::pi<float>() * u2;
float r = std::sqrt(1.0f - z * z);
glm::vec3 dir = { r * std::cos(phi), r * std::sin(phi), z };
dir = glm::normalize(dir);
auto end = chunks.rayCastToObstacle(start, dir, rayLength);
glm::vec3 end;
glm::ivec3 norm;
glm::ivec3 iend;
auto vox = chunks.rayCast(start, dir, rayLength, end, norm, iend);
if (vox == nullptr) {
continue;
}
auto distance = glm::distance(start, end);
if (distance >= rayLength * 0.98f) {
continue;
}
if (distance < minDistance) {
minDistance = distance;
}
auto def = blocks.get(vox->id);
if (def == nullptr) {
continue;
}
auto material = level.content.findBlockMaterial(def->material);
if (material == nullptr) {
return;
}
hit++;
averageDistance += distance * 0.2f;
averageDistance += distance;
averageAbsorption += material->soundAbsorption;
}
float decayTime =
glm::sqrt(averageDistance / hit) *
glm::max(0.0f, glm::pow(hit / static_cast<float>(rays), 3.0f) - 0.6f) *
1.6f;
decayTime *= 2.5f;
decayTime *= decayTime;
logger.info() << "rays hit " << hit << "/" << rays << " decay-time: " << decayTime;
averageDistance /= hit;
averageAbsorption /= hit;
float decayTime = 20.0f * (averageDistance / rayLength);
float escapeRatio = static_cast<float>(rays - hit) / static_cast<float>(rays);
acoustics.reverbGain = glm::pow(hit / static_cast<float>(rays), 3.0f);
acoustics.reverbReflectionsDelay = minDistance / speedOfSound;
acoustics.reverbLateReflectionsDelay = averageDistance / speedOfSound;
acoustics.reverbDecayTime = decayTime;
acoustics.reverbRoomRolloff = escapeRatio * 1.5f;
acoustics.reverbAbsorption = averageAbsorption;
audio::set_acoustics(std::move(acoustics));
}

View file

@ -24,7 +24,8 @@ dv::value BlockMaterial::serialize() const {
{"steps-sound", stepsSound},
{"place-sound", placeSound},
{"break-sound", breakSound},
{"hit-sound", hitSound}
{"hit-sound", hitSound},
{"sound-absorption", soundAbsorption}
});
}
@ -34,6 +35,7 @@ void BlockMaterial::deserialize(const dv::value& src) {
src.at("place-sound").get(placeSound);
src.at("break-sound").get(breakSound);
src.at("hit-sound").get(hitSound);
src.at("sound-absorption").get(soundAbsorption);
}
CoordSystem::CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ)

View file

@ -147,6 +147,7 @@ struct BlockMaterial : Serializable {
std::string placeSound;
std::string breakSound;
std::string hitSound;
float soundAbsorption = 0.5f;
dv::value toTable() const; // for compatibility
dv::value serialize() const override;