add fog_curve parameter & update shaders

This commit is contained in:
MihailRis 2025-02-26 04:49:45 +03:00
parent d7f20e7388
commit e6ea4fe37e
8 changed files with 29 additions and 27 deletions

View File

@ -16,5 +16,6 @@
},
"fog_opacity": 0.8,
"fog_dencity": 2.0,
"fog_curve": 0.5,
"clouds": 0.5
}

View File

@ -1,7 +1,7 @@
in vec4 a_color;
in vec2 a_texCoord;
in float a_distance;
in vec3 a_dir;
in float a_fog;
out vec4 f_color;
uniform sampler2D u_texture0;
@ -14,12 +14,10 @@ uniform bool u_alphaClip;
void main() {
vec3 fogColor = texture(u_cubemap, a_dir).rgb;
vec4 tex_color = texture(u_texture0, a_texCoord);
float depth = (a_distance/256.0);
float alpha = a_color.a * tex_color.a;
// anyway it's any alpha-test alternative required
if (alpha < (u_alphaClip ? 0.5f : 0.15f))
discard;
f_color = mix(a_color * tex_color, vec4(fogColor,1.0),
min(1.0, pow(depth*u_fogFactor, u_fogCurve)));
f_color = mix(a_color * tex_color, vec4(fogColor,1.0), a_fog);
f_color.a = alpha;
}

View File

@ -7,7 +7,7 @@ layout (location = 3) in float v_light;
out vec4 a_color;
out vec2 a_texCoord;
out float a_distance;
out float a_fog;
out vec3 a_dir;
uniform mat4 u_model;
@ -16,6 +16,11 @@ uniform mat4 u_view;
uniform vec3 u_cameraPos;
uniform float u_gamma;
uniform float u_opacity;
uniform float u_fogFactor;
uniform float u_fogCurve;
uniform float u_weatherFogOpacity;
uniform float u_weatherFogDencity;
uniform float u_weatherFogCurve;
uniform samplerCube u_cubemap;
uniform vec3 u_torchlightColor;
@ -38,6 +43,10 @@ void main() {
vec3 skyLightColor = pick_sky_color(u_cubemap);
a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a) * v_color;
a_color.a = u_opacity;
a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0));
float dist = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0));
float depth = (dist / 256.0);
a_fog = min(1.0, max(pow(depth * u_fogFactor, u_fogCurve),
min(pow(depth * u_weatherFogDencity, u_weatherFogCurve), u_weatherFogOpacity)));
gl_Position = u_proj * u_view * modelpos;
}

View File

@ -1,13 +1,11 @@
in vec4 a_color;
in vec2 a_texCoord;
in float a_distance;
in float a_fog;
in vec3 a_dir;
out vec4 f_color;
uniform sampler2D u_texture0;
uniform samplerCube u_cubemap;
uniform vec3 u_fogColor;
uniform bool u_alphaClip;
void main() {

View File

@ -19,6 +19,8 @@ uniform float u_fogFactor;
uniform float u_fogCurve;
uniform float u_weatherFogOpacity;
uniform float u_weatherFogDencity;
uniform float u_weatherFogCurve;
uniform float u_timer;
uniform samplerCube u_cubemap;
uniform vec3 u_torchlightColor;
@ -44,6 +46,6 @@ void main() {
a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0));
float depth = (a_distance / 256.0);
a_fog = min(1.0, max(pow(depth * u_fogFactor, u_fogCurve),
min(depth * u_weatherFogDencity, u_weatherFogOpacity)));
min(pow(depth * u_weatherFogDencity, u_weatherFogCurve), u_weatherFogOpacity)));
gl_Position = u_proj * u_view * modelpos;
}

View File

@ -103,21 +103,7 @@ WorldRenderer::WorldRenderer(
);
weather = {};
auto& fall = weather.fall;
fall.vspeed = 2.0f;
fall.texture = "misc/rain";
fall.noise = "ambient/rain";
ParticlesPreset rainSplash;
rainSplash.frames = {
"particles:rain_splash_0",
"particles:rain_splash_1",
"particles:rain_splash_2"
};
rainSplash.lifetime = 0.2f;
rainSplash.spawnInterval = 0.0f;
rainSplash.size = {0.2f, 0.2f, 0.2f};
fall.splash = std::move(rainSplash);
weather.deserialize(io::read_json("res:presets/weather/rain.json"));
}
WorldRenderer::~WorldRenderer() = default;
@ -138,6 +124,7 @@ void WorldRenderer::setupWorldShader(
shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get());
shader.uniform1f("u_weatherFogOpacity", weather.fogOpacity * weather.intensity);
shader.uniform1f("u_weatherFogDencity", weather.fogDencity);
shader.uniform1f("u_weatherFogCurve", weather.fogCurve);
shader.uniform1f("u_dayTime", level.getWorld()->getInfo().daytime);
shader.uniform2f("u_lightDir", skybox->getLightDir());
shader.uniform3f("u_cameraPos", camera.position);
@ -216,8 +203,10 @@ void WorldRenderer::renderLevel(
entityShader.uniform1i("u_alphaClip", false);
float zero = weather.fall.minOpacity;
entityShader.uniform1f("u_opacity", (weather.intensity * (1.0f - zero)) + zero);
precipitation->render(camera, pause ? 0.0f : delta, weather);
weather.intensity = -glm::cos(timer * 0.2f) * 0.5f + 0.5f;
if (weather.intensity > 1.e-3f && !weather.fall.texture.empty()) {
precipitation->render(camera, pause ? 0.0f : delta, weather);
}
//weather.intensity = -glm::cos(timer * 0.2f) * 0.5f + 0.5f;
skybox->unbind();
}

View File

@ -18,6 +18,7 @@ dv::value WeatherPreset::serialize() const {
root["fog_opacity"] = fogOpacity;
root["fog_dencity"] = fogDencity;
root["fog_curve"] = fogCurve;
root["clouds"] = clouds;
return root;
@ -40,5 +41,6 @@ void WeatherPreset::deserialize(const dv::value& src) {
}
src.at("fog_opacity").get(fogOpacity);
src.at("fog_dencity").get(fogDencity);
src.at("fog_curve").get(fogCurve);
src.at("clouds").get(clouds);
}

View File

@ -31,10 +31,13 @@ struct WeatherPreset : Serializable {
/// @brief Weather fog depth multiplier
float fogDencity = 2.0f;
/// @brief Weather fog curve
float fogCurve = 0.5f;
float clouds = 0.5f;
/// @brief Weather effects intensity
float intensity = 0.9f;
float intensity = 1.0f;
dv::value serialize() const override;
void deserialize(const dv::value& src) override;