diff --git a/gl/ubo.h b/gl/ubo.h index a9a77d32f..66a10e2db 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -767,10 +767,15 @@ namespace gl const size_t WATER_CELLS_OFFSET = WATER_EXTRA_OFFSET + 16 * WATER_SLOT_DROPS; const size_t WATER_CELLS_SIZE = 4 * static_cast(WATER_SLOTS) * WATER_GRID * WATER_GRID * (1 + WATER_CELL_DROPS); const size_t WATER_TRAIL_OFFSET = WATER_CELLS_OFFSET + WATER_CELLS_SIZE; - const size_t WATER_STORAGE_SIZE = WATER_TRAIL_OFFSET + 4 * static_cast(WATER_SLOTS) * WATER_TRAIL * WATER_TRAIL; + // the snow on the glasses (water_snow.comp, mat_snow_windscreen.frag): two maps of every glass, WATER_MAP x WATER_MAP texels of + // two uint each (four halves: the snow, mm; the water, mm; how packed the snow is, 0 .. 1; random 0 .. 1, the shape of its + // flakes), a step reads one and writes the other (the header of the glass says which one is the latest) + const size_t WATER_SNOW_OFFSET = WATER_TRAIL_OFFSET + 4 * static_cast(WATER_SLOTS) * WATER_TRAIL * WATER_TRAIL; + const size_t WATER_SNOW_SIZE = 8 * static_cast(WATER_MAP) * WATER_MAP; + const size_t WATER_STORAGE_SIZE = WATER_SNOW_OFFSET + 2 * WATER_SNOW_SIZE * WATER_SLOTS; // the parts of a glass in the storage: its drops and what the passes keep of them, its map (the axes the second part), its - // cells and its paths, its header and its counts, (offset, size) in bytes - inline std::array, 11> water_slot_parts(size_t const Slot) + // cells and its paths, its header and its counts, its two maps of the snow, (offset, size) in bytes + inline std::array, 13> water_slot_parts(size_t const Slot) { auto const drops{static_cast(WATER_DROPS)}; auto const texels{static_cast(WATER_MAP) * WATER_MAP}; @@ -786,7 +791,9 @@ namespace gl {WATER_CELLS_OFFSET + cells * Slot, cells}, {WATER_TRAIL_OFFSET + trail * Slot, trail}, {WATER_HEADER_OFFSET + 16 * Slot, 16}, - {WATER_COUNTS_OFFSET + 16 * Slot, 16}}}; + {WATER_COUNTS_OFFSET + 16 * Slot, 16}, + {WATER_SNOW_OFFSET + WATER_SNOW_SIZE * Slot, WATER_SNOW_SIZE}, + {WATER_SNOW_OFFSET + WATER_SNOW_SIZE * (WATER_SLOTS + Slot), WATER_SNOW_SIZE}}}; } // layout std140: the step of the simulation of the water on the windscreens (GLSL water_ubo), every vector in the space of the @@ -815,8 +822,14 @@ namespace gl // landings tried at the densest place are all taken), z 1 when it's simulated, w the size of its drops (the param // rain_scale of its material) glm::vec4 slots[WATER_SLOTS]; + // the snow (water_snow.comp): x the flakes coming to a square metre of the ground in a second, y how much warmer than the + // air the glass is (the cab heats it), degrees, z the speed of the vehicle, m/s, w the map of the snow the step reads (0, 1) + glm::vec4 snow; + // every glass: 1 when its snow is simulated + glm::vec4 snow_slots; }; - static_assert(sizeof(water_ubs) == 112 + 16 * WATER_SLOTS, "bad size of water_ubs"); + static_assert(WATER_SLOTS == 4, "snow_slots holds a glass in a component"); + static_assert(sizeof(water_ubs) == 144 + 16 * WATER_SLOTS, "bad size of water_ubs"); // layout std430: a bucket of a scene cell queued in the pass (GLSL: instance_cull_job) struct instance_cull_job diff --git a/rendering/common/windscreenwater.cpp b/rendering/common/windscreenwater.cpp index 9a52eb3fe..f3b51f9c9 100644 --- a/rendering/common/windscreenwater.cpp +++ b/rendering/common/windscreenwater.cpp @@ -20,6 +20,8 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/DynObj.h" #include "vehicle/Train.h" +#include + namespace gfx { @@ -38,17 +40,42 @@ double constexpr WIND_SHARE{0.5}; float constexpr LEAST_DROP{0.5f}; // the water a flake of snow brings, mm3 on average (water_spawn.comp: 0.3 .. 2.5) float constexpr FLAKE_WATER{1.4f}; +// how much warmer than the air a glass of the cab is, degrees: the cab is heated (the blowing on the glass isn't simulated), +// less so at speed, when the air cools the glass from the outside (water_snow.comp) +float constexpr GLASS_WARMTH{3.f}; // the most landings tried on a glass in a step std::uint32_t constexpr MOST_TRIES{1024}; // the longest step, seconds: a longer frame slows the water down rather than let it jump double constexpr LONGEST_STEP{0.05}; -// whether the material is a glass: one of mat_rain_windscreen, with a mask of the wipers -bool is_glass(opengl_material const *Material) +// whether the material is a glass of the water: one of mat_rain_windscreen, with a mask of the wipers +bool is_water_glass(opengl_material const *Material) { return (Material != nullptr) && (Material->shader != nullptr) && (Material->shader->texture_conf.count("wipermask") > 0) && (Material->shader->param_conf.count("water_slot") > 0); } +// whether the material is a glass of the snow: one of mat_snow_windscreen, with a mask of the wipers (its param snow_slot is in +// the place of water_slot) +bool is_snow_glass(opengl_material const *Material) +{ + return (Material != nullptr) && (Material->shader != nullptr) && (Material->shader->texture_conf.count("wipermask") > 0) && (Material->shader->param_conf.count("snow_slot") > 0); +} + +// whether the material is a glass, of the water or of the snow +bool is_glass(opengl_material const *Material) +{ + return is_water_glass(Material) || is_snow_glass(Material); +} + +// what a material of the cab with a mask of the wipers is now: 0 no glass, 1 a glass of the water, 2 a glass of the snow (its +// shader goes with the weather) +int glass_kind(opengl_material const *Material) +{ + if (is_snow_glass(Material)) + return 2; + return is_water_glass(Material) ? 1 : 0; +} + // calls Visit for every submodel of the branch, its children and its siblings template void visit_submodels(TSubModel *Submodel, Visit &&Visitor) { @@ -94,6 +121,7 @@ void windscreen_water::release() assign(false); m_glasses = {}; m_glasscount = 0; + m_kinds.clear(); m_vehicle = nullptr; m_cab = nullptr; } @@ -230,6 +258,12 @@ void windscreen_water::build(TModel3d &Cab) } } auto const material{Submodel->m_material}; + if ((material != null_handle) && (material > 0) && (Submodel->eType == GL_TRIANGLES) && std::ranges::none_of(m_kinds, [material](auto const &Kind) { return Kind.first == material; })) + { + // every material with a mask of the wipers, what it is kept: it's a glass of another kind when the weather changes + if (auto const *opengl{m_host.material(material)}; (opengl != nullptr) && (opengl->shader != nullptr) && (opengl->shader->texture_conf.count("wipermask") > 0)) + m_kinds.emplace_back(material, glass_kind(opengl)); + } if ((material != null_handle) && (material > 0) && (Submodel->eType == GL_TRIANGLES) && (std::find(materials.begin(), materials.end(), material) == materials.end()) && is_glass(m_host.material(material))) { materials.push_back(material); @@ -255,6 +289,7 @@ void windscreen_water::build(TModel3d &Cab) glass = windscreen_water::glass{}; glass.material = material; auto const *opengl{m_host.material(material)}; + glass.snow = is_snow_glass(opengl); glass.mask = opengl->textures[opengl->shader->texture_conf.at("wipermask").id]; for (std::size_t texel = 0; texel < texels; ++texel) { @@ -317,7 +352,10 @@ void windscreen_water::update(double const Seconds) } auto const *vehicle{simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr}; auto *cab{vehicle != nullptr ? vehicle->mdKabina : nullptr}; - if ((vehicle != m_vehicle) || (cab != m_cab)) + // a material of the glasses of another kind: the weather changed, the materials have the shaders of the other weather (the + // snow comes with the shader of the snow and goes with it) + auto const changed{std::ranges::any_of(m_kinds, [this](auto const &Kind) { return glass_kind(m_host.material(Kind.first)) != Kind.second; })}; + if ((vehicle != m_vehicle) || (cab != m_cab) || changed) { // another cab: its glasses dry release(); @@ -409,11 +447,15 @@ void windscreen_water::update(double const Seconds) auto const coming{glm::length(air - up * fall)}; std::uint32_t most{0}; m_tries = 0; + bool anysnow{false}; for (std::size_t slot = 0; slot < m_glasscount; ++slot) { auto &glass{m_glasses[slot]}; auto const *material{m_host.material(glass.material)}; - auto const simulated{glass.baked && is_glass(material)}; + auto const snowed{glass.baked && glass.snow && is_snow_glass(material)}; + block.snow_slots[static_cast(slot)] = (snowed ? 1.f : 0.f); + anysnow = anysnow || snowed; + auto const simulated{glass.baked && (false == glass.snow) && is_water_glass(material)}; glass.landings += static_cast(perground / fall * coming * glass.densest) * seconds; auto const whole{std::floor(glass.landings)}; glass.landings -= whole; @@ -432,8 +474,14 @@ void windscreen_water::update(double const Seconds) m_bladesbefore.x < 0.f ? blades.x : m_bladesbefore.x, m_bladesbefore.y < 0.f ? blades.y : m_bladesbefore.y, m_bladesbefore.z < 0.f ? blades.z : m_bladesbefore.z, m_bladesbefore.w < 0.f ? blades.w : m_bladesbefore.w}; block.rain = glm::vec4{LEAST_DROP, lambda, 1.f, 0.f}; block.counts = glm::uvec4{m_step * 2654435761u, most, 0u, 0u}; + block.snow = glm::vec4{snow ? perground : 0.f, GLASS_WARMTH, static_cast(std::abs(vehicle->MoverParameters->V)), static_cast(m_snowmap)}; m_bladesbefore = blades; gpu_step(block, most); + if (anysnow) + { + // the step wrote the other map of the snow + m_snowmap = 1 - m_snowmap; + } } std::string windscreen_water::info() const diff --git a/rendering/common/windscreenwater.h b/rendering/common/windscreenwater.h index 2c09951ba..6a8ce7c17 100644 --- a/rendering/common/windscreenwater.h +++ b/rendering/common/windscreenwater.h @@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include @@ -89,6 +90,7 @@ class windscreen_water float area{0.f}; // metres squared of the glass double landings{0.0}; // the share of a landing left over from the steps before bool baked{false}; + bool snow{false}; // a glass of the snow (mat_snow_windscreen), else of the water }; // the glasses of the model of a cab found and set up @@ -102,12 +104,14 @@ class windscreen_water int m_state{0}; // 0: not set up yet, 1: ready, -1: can't be used TDynamicObject const *m_vehicle{nullptr}; TModel3d const *m_cab{nullptr}; + std::vector> m_kinds; // the materials of the cab with a mask of the wipers, what kind of glass they were std::array m_glasses{}; std::size_t m_glasscount{0}; glm::vec4 m_bladesbefore{-1.f}; double m_time{0.0}; // seconds the simulation runs std::uint32_t m_step{0}; std::uint32_t m_tries{0}; // landings tried in the last step, all glasses + int m_snowmap{0}; // the map of the snow of the glasses the next step reads (0, 1) }; } // namespace gfx diff --git a/rendering/opengl46/opengl46water.cpp b/rendering/opengl46/opengl46water.cpp index 741536b99..0fd1e9d2d 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -13,6 +13,8 @@ http://mozilla.org/MPL/2.0/. #include "rendering/renderer.h" #include "utilities/Logs.h" +#include + namespace { @@ -28,7 +30,7 @@ bool opengl46_windscreen_water::gpu_init() try { std::array const names{"water_trail.comp", "water_spawn.comp", "water_move.comp", "water_clear.comp", "water_grid.comp", - "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp"}; + "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp", "water_snow.comp"}; for (std::size_t i = 0; i < names.size(); ++i) { gl::shader shader(names[i]); @@ -139,17 +141,24 @@ void opengl46_windscreen_water::gpu_step(gl::water_ubs const &Block, std::uint32 m_programs[Pass]->dispatch(Groups, slots); ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); }; + // the drops only where a glass of the water is (the glasses of the snow have passes of their own) + auto const water{std::ranges::any_of(Block.slots, [](glm::vec4 const &Slot) { return Slot.z > 0.5f; })}; + auto const drop_groups{water ? drops : 0u}; + auto const cell_groups{water ? cells : 0u}; // the wetted paths the blades went over in the step wiped - if (Block.blades != Block.blades_before) + if (water && (Block.blades != Block.blades_before)) run(pass::trail, groups(static_cast(gl::WATER_TRAIL) * gl::WATER_TRAIL)); - run(pass::spawn, groups(Tries)); - run(pass::move, drops); - run(pass::clear, cells); - run(pass::grid, drops); - run(pass::merge, drops); - run(pass::gather, drops); - run(pass::settle, drops); - run(pass::clear, cells); - run(pass::grid, drops); + run(pass::spawn, water ? groups(Tries) : 0u); + run(pass::move, drop_groups); + run(pass::clear, cell_groups); + run(pass::grid, drop_groups); + run(pass::merge, drop_groups); + run(pass::gather, drop_groups); + run(pass::settle, drop_groups); + run(pass::clear, cell_groups); + run(pass::grid, drop_groups); + // the snow of the glasses of the snow, a texel of their maps a thread + if (Block.snow_slots != glm::vec4{0.f}) + run(pass::snow, groups(static_cast(gl::WATER_MAP) * gl::WATER_MAP)); gl::program::unbind(); } diff --git a/rendering/opengl46/opengl46water.h b/rendering/opengl46/opengl46water.h index 7e5a20d29..ce3c92bae 100644 --- a/rendering/opengl46/opengl46water.h +++ b/rendering/opengl46/opengl46water.h @@ -50,6 +50,7 @@ class opengl46_windscreen_water : public gfx::windscreen_water gather, settle, bake, + snow, count }; std::array, pass::count> m_programs; diff --git a/rendering/vulkan14/vulkan14water.cpp b/rendering/vulkan14/vulkan14water.cpp index c3aefc748..1ed3b45a2 100644 --- a/rendering/vulkan14/vulkan14water.cpp +++ b/rendering/vulkan14/vulkan14water.cpp @@ -12,6 +12,8 @@ http://mozilla.org/MPL/2.0/. #include "rendering/vulkan14/vulkan14commands.h" #include "utilities/Logs.h" +#include + #include namespace vk14 @@ -99,7 +101,7 @@ bool windscreen_water::gpu_init() try { std::array const names{"water_trail.comp", "water_spawn.comp", "water_move.comp", "water_clear.comp", "water_grid.comp", - "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp"}; + "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp", "water_snow.comp"}; for (std::size_t i = 0; i < names.size(); ++i) { if (false == m_kernels[i].create(m_device, *m_pipelines, m_shaders->get(names[i]), m_passlayout, {}, names[i])) @@ -283,20 +285,29 @@ void windscreen_water::gpu_step(gl::water_ubs const &Block, std::uint32_t const }; auto const drops{groups(gl::WATER_DROPS)}; auto const cells{groups(static_cast(gl::WATER_GRID) * gl::WATER_GRID)}; + // the drops only where a glass of the water is (the glasses of the snow have passes of their own) + auto const water{std::ranges::any_of(Block.slots, [](glm::vec4 const &Slot) { return Slot.z > 0.5f; })}; + auto const drop_groups{water ? drops : 0u}; + auto const cell_groups{water ? cells : 0u}; // the wetted paths the blades went over in the step wiped - if (Block.blades != Block.blades_before) + if (water && (Block.blades != Block.blades_before)) { run(pass::trail, groups(static_cast(gl::WATER_TRAIL) * gl::WATER_TRAIL)); } - run(pass::spawn, groups(Tries)); - run(pass::move, drops); - run(pass::clear, cells); - run(pass::grid, drops); - run(pass::merge, drops); - run(pass::gather, drops); - run(pass::settle, drops); - run(pass::clear, cells); - run(pass::grid, drops); + run(pass::spawn, water ? groups(Tries) : 0u); + run(pass::move, drop_groups); + run(pass::clear, cell_groups); + run(pass::grid, drop_groups); + run(pass::merge, drop_groups); + run(pass::gather, drop_groups); + run(pass::settle, drop_groups); + run(pass::clear, cell_groups); + run(pass::grid, drop_groups); + // the snow of the glasses of the snow, a texel of their maps a thread + if (Block.snow_slots != glm::vec4{0.f}) + { + run(pass::snow, groups(static_cast(gl::WATER_MAP) * gl::WATER_MAP)); + } } } // namespace vk14 diff --git a/rendering/vulkan14/vulkan14water.h b/rendering/vulkan14/vulkan14water.h index 901e241bd..dd42cf72a 100644 --- a/rendering/vulkan14/vulkan14water.h +++ b/rendering/vulkan14/vulkan14water.h @@ -78,6 +78,7 @@ class windscreen_water : public gfx::windscreen_water gather, settle, bake, + snow, count }; // the block of the step in the ring of the frame; empty when the ring is full diff --git a/shaders/ogl46/mat_snow_windscreen.frag b/shaders/ogl46/mat_snow_windscreen.frag index 50496d520..5d4ce8097 100644 --- a/shaders/ogl46/mat_snow_windscreen.frag +++ b/shaders/ogl46/mat_snow_windscreen.frag @@ -65,11 +65,14 @@ layout(location = 1) out vec4 out_motion; #pragma param(reflection, 1, 2, 1, zero) #pragma param(glossiness, 1, 3, 1, glossiness) #pragma param(raindrop_grid_size, 2, 0, 1, one) +// snow_slot: 1 + the slot of the glass in the simulation of the snow (the renderer sets it), 0: none +#pragma param(snow_slot, 2, 2, 1, zero) #include "light_common.glsl" #include "apply_fog.glsl" #include "tonemapping.glsl" #include "random.glsl" +#include "water.glsl" layout(binding = 0) uniform sampler2D diffuse; layout(binding = 1) uniform sampler2D raindropsatlas; @@ -442,6 +445,158 @@ void snowFilm(float gridSize, inout vec3 accRGB, inout float accA, inout vec3 ac accA = accA + a * under; } +// --- The snow of the simulation (water_snow.comp): the map of the glass --------------------------------------------------- +// When the renderer simulates the snow of the glass (the param snow_slot holds 1 + its slot), the snow, the water, how packed the +// snow is and the shape of its flakes come from the map of the glass (water.glsl), which keeps them from frame to frame: the flakes +// land and stay where they landed, pile up into a layer, the blades push it into ridges at the ends of their sweeps and leave a wet +// trace, it melts at the warm glass and slush slides down. Here it's only drawn: +// - a texel of the map with a little snow is a flake (atlas tiles 0..2: fluffy when dry, a wet lump when wet or packed, crumbs), +// the more snow the bigger; with mostly water left it's the water spot (tile 3) +// - where the snow is thicker it's a layer: its edge torn by the grain of the snow, as opaque as it's thick (Beer-Lambert), grey +// and translucent when wet (slush), smoother and denser when packed; its relief from the slopes of the snow of the map +// - the water: wet glass bends the picture behind (the post-processing), the thin snow hazes it +// Without the simulation (an older renderer, no compute) the procedural snow below draws the glass as before. +const float snow_map_flake = 1.15; // size of a flake relative to a texel of the map (the snow of a texel) +const float snow_map_layer = 1.0; // mm of snow above which the flakes run together into a layer +const float snow_map_opaque = 1.8; // mm of fresh snow which lets 1/e of the light through (packed: half of it) +const float snow_map_relief = 0.6; // how much the slopes of the snow tilt its surface +const float snow_map_grain = 0.35; // the fine grain of the layer (its relief) +const float snow_map_wet_bend = 0.6; // how strongly a wet glass bends the picture behind it + +int snMapSlot = -1; +uint snMapIndex = 0u; +float snWet = 0.0; // set by snowMapped: the film of water (0 .. 1) +vec2 snWetN = vec2(0.0); // set by snowMapped: the slope of the water, for the bend of the picture + +// the snow of the map at a place of the glass +vec4 snMapAt(vec2 uv) +{ + return water_snow_at(uint(snMapSlot), snMapIndex, uv); +} + +// a flake of the map drawn over the accumulators: Local the place in its sprite (0 .. 1), Tile the corner of its tile in the atlas +void snowMapSprite(vec2 local, vec2 tile, float alpha, float tone, float flip, float c, float s, float lod, + inout vec3 accRGB, inout float accA, inout vec3 accN, inout float accTr) +{ + vec4 t = textureLod(raindropsatlas, tile + local * 0.5, lod); + vec4 n = textureLod(normalmap, tile + local * 0.5, lod); + float a = clamp(t.a * alpha * snow_opacity, 0.0, 1.0); + if (a <= 0.0) + return; + vec2 nxy = (n.rg * 2.0 - 1.0) * snow_bump; + nxy.x *= flip; + vec2 nuv = vec2(c * nxy.x - s * nxy.y, s * nxy.x + c * nxy.y); + vec3 nT = vec3(nuv, sqrt(max(1.0 - dot(nuv, nuv), 0.02))); + float tr = (1.0 - t.a * 0.55); + accRGB = t.rgb * tone * a + accRGB * (1.0 - a); + accN = nT * a + accN * (1.0 - a); + accTr = tr * a + accTr * (1.0 - a); + accA = a + accA * (1.0 - a); +} + +// the flakes of the texels around the pixel +void snowMapFlakes(vec2 aspect, float pixelUV, inout vec3 accRGB, inout float accA, inout vec3 accN, inout float accTr) +{ + vec2 p = f_coord * float(WATER_MAP); + ivec2 here = ivec2(floor(p)); + for (int oy = -1; oy <= 1; ++oy) + { + for (int ox = -1; ox <= 1; ++ox) + { + ivec2 texel = here + ivec2(ox, oy); + vec4 q = water_snow_texel(uint(snMapSlot), snMapIndex, texel); + float density = mix(0.1, 0.4, q.z); + float wet = q.y / (q.y + q.x * density + 1e-4); + bool spot = (q.x < 0.05) && (q.y > 0.015); // a flake melted into its water + if ((q.x < 0.05) && (false == spot)) + continue; + uint seed = Hash(uvec3(uvec2(texel), uint(q.w * 65535.0) + 7u * uint(snMapSlot))); + vec2 center = vec2(texel) + vec2(0.15 + 0.7 * RandF(seed), 0.15 + 0.7 * RandF(seed)); + float ang = RandF(seed) * 6.2831853; + float flip = (RandF(seed) < 0.5) ? -1.0 : 1.0; + float kind = RandF(seed); + float tone = mix(0.94, 1.04, RandF(seed)); + float aniso = mix(0.62, 1.0, RandF(seed)); + // the more snow the texel holds the bigger the flake (a lump of a few flakes) + float size = snow_map_flake * mix(0.6, 1.0, RandF(seed)) * (spot ? mix(0.5, 1.1, smoothstep(0.015, 0.2, q.y)) + : mix(0.7, 1.6, smoothstep(0.05, 2.0, q.x))); + vec2 d = (p - center) * aspect; + float c = cos(ang); + float s = sin(ang); + vec2 r = vec2(c * d.x + s * d.y, -s * d.x + c * d.y); + r.x *= flip / aniso; + vec2 local = r / size + 0.5; + if (any(lessThan(local, vec2(0.0))) || any(greaterThan(local, vec2(1.0)))) + continue; + float sizeUV = size / float(WATER_MAP); + float lod = clamp(log2(max(pixelUV / sizeUV * 0.5 * float(textureSize(raindropsatlas, 0).x), 1.0)), 0.0, 4.0); + vec2 tile; + if (spot) + tile = vec2(0.5, 0.0); + else if ((wet > 0.3) || (q.z > 0.5) || (kind < 0.30)) + tile = vec2(0.5, 0.5); // a wet lump + else if (kind < 0.75) + tile = vec2(0.0, 0.5); // fluffy + else + tile = vec2(0.0, 0.0); // crumbs + float alpha = spot ? smoothstep(0.015, 0.08, q.y) : smoothstep(0.05, 0.25, q.x); + snowMapSprite(local, tile, alpha, tone, flip, c, s, lod, accRGB, accA, accN, accTr); + } + } +} + +// the snow of the map at the pixel: the flakes, the layer of the snow under them, the water, the haze +void snowMapped(vec2 aspect, float pixelUV, inout vec3 accRGB, inout float accA, inout float smear, inout vec3 accN, inout float accTr) +{ + vec4 q = snMapAt(f_coord); + float e = 1.0 / float(WATER_MAP); + vec4 qr = snMapAt(f_coord + vec2(e, 0.0)); + vec4 ql = snMapAt(f_coord - vec2(e, 0.0)); + vec4 qu = snMapAt(f_coord + vec2(0.0, e)); + vec4 qd = snMapAt(f_coord - vec2(0.0, e)); + float around = (q.x + qr.x + ql.x + qu.x + qd.x) * 0.2; + + // the flakes of thin snow and the water spots + snowMapFlakes(aspect, pixelUV, accRGB, accA, accN, accTr); + + // the water: the film bends the picture behind + snWet = smoothstep(0.02, 0.25, q.y) * (1.0 - smoothstep(0.5, 2.0, q.x)); + snWetN = vec2(qr.y - ql.y, qu.y - qd.y) * 2.0 + (vec2(snNoise(f_coord * 900.0), snNoise(f_coord * 900.0 + 31.0)) - 0.5) * 0.6 * snWet; + smear = max(smear, snWet * snow_melt_blur * 0.6); + // thin snow hazes the picture around it + snHaze = max(snHaze, smoothstep(0.03, 0.6, around)); + + // the layer, where the snow has run together + vec2 gp = f_coord * float(WATER_MAP) * aspect; + float grain = snFbm(gp * 0.9 + 3.0) * 0.55 + snFbm(gp * 2.7 + 11.0) * 0.30 + snFbm(gp * 7.1 + 5.0) * 0.15; + float edge = (q.x - snow_map_layer) / max(snow_map_layer, 1e-3) + (grain - 0.5) * 1.6; + float cover = smoothstep(0.0, 0.35, edge); + if (cover <= 0.002) + return; + float density = mix(0.1, 0.4, q.z); + float wet = q.y / (q.y + q.x * density + 1e-4); + float opaque = snow_map_opaque * mix(1.0, 0.5, q.z); + float thick = 1.0 - exp(-q.x / opaque); + // the relief: the slopes of the snow of the map (ridges, the edges of the swept arcs) and the grain of the fresh snow + vec2 slope = vec2(qr.x - ql.x, qu.x - qd.x) * 0.5 * snow_map_relief; + float f1 = snFbm(gp * 3.3); + float ef = 0.05; + vec2 gf = vec2(snFbm(gp * 3.3 + vec2(ef, 0.0)) - f1, snFbm(gp * 3.3 + vec2(0.0, ef)) - f1) / ef; + vec3 nT = normalize(vec3(-(slope + gf * snow_map_grain * (1.0 - 0.6 * q.z)) * snow_bump, 1.0)); + + vec3 fresh = vec3(0.93, 0.95, 0.98); + vec3 packed = vec3(0.84, 0.87, 0.91); + vec3 slush = vec3(0.60, 0.66, 0.73); + vec3 col = mix(mix(fresh, packed, q.z), slush, smoothstep(0.2, 0.7, wet)); + col *= (0.80 + 0.30 * f1 * (1.0 - 0.5 * q.z)) * (0.92 + 0.12 * grain); + float a = clamp(cover * mix(0.35, 0.97, thick) * mix(1.0, 0.75, smoothstep(0.3, 0.8, wet)) * snow_pack * snow_opacity, 0.0, 0.97); + float under = 1.0 - accA; + accRGB = accRGB + col * a * under; + accN = accN + nT * a * under; + accTr = accTr + mix(0.60, 0.25, thick) * a * under; + accA = accA + a * under; +} + void main() { // Derivatives first, in uniform control flow (no discard before them). @@ -470,7 +625,20 @@ void main() float smear = 0.0; vec3 snowN = vec3(0.0); float snowTr = 0.0; - if (rain_params.x > 0.0) + // the snow of the simulation where the glass has a map of it, else made up as before + snMapSlot = int(param[2].z + 0.5) - 1; + bool mapped = (snMapSlot >= 0) && (snMapSlot < WATER_SLOTS) && water_snow_map(uint(snMapSlot), snMapIndex); + if (mapped) + { + // the texels of the map as they lie on the glass (the uv may be stretched) + ivec2 texel = clamp(ivec2(floor(f_coord * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); + uint axis = (uint(snMapSlot) * uint(WATER_MAP * WATER_MAP) + uint(texel.y * WATER_MAP + texel.x)) * 2u; + vec2 span = vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)); + if (min(span.x, span.y) > 1e-6) + aspect = span / max(span.x, span.y); + snowMapped(aspect, pixelUV, snowRGB, snowA, smear, snowN, snowTr); + } + else if (rain_params.x > 0.0) { float mass = 0.0; if (snow_small > 0.0) @@ -531,7 +699,13 @@ void main() { float thin = smoothstep(0.04, 0.20, snowA) * (1.0 - smoothstep(0.55, 0.90, snowA)); vec4 req = vec4(0.0); - if ((snow_distort > 0.0) && (thin > 0.30) && (glassA < 0.5)) + if ((snWet > 0.25) && (snowA < 0.35) && (glassA < 0.5)) + { + // the water on the glass: its film and its spots bend the picture behind + vec2 bend = clamp(-snWetN * snow_map_wet_bend, vec2(-1.0), vec2(1.0)) * smoothstep(0.25, 0.6, snWet); + req = vec4(WS_DROP_BIAS + WS_BEND_ENC * bend.x, WS_DROP_BIAS + WS_BEND_ENC * bend.y, 0.0, 1.0); + } + else if ((snow_distort > 0.0) && (thin > 0.30) && (glassA < 0.5)) { vec2 slope = snowN.xy / max(snowA, 1e-3); vec2 bend = clamp(-slope * snow_distort, vec2(-1.0), vec2(1.0)) * smoothstep(0.30, 0.60, thin) * (SNOW_REFR_MAXPX / WS_REFR_MAXPX); diff --git a/shaders/ogl46/water.glsl b/shaders/ogl46/water.glsl index 6c3dcbfba..f35fbdf75 100644 --- a/shaders/ogl46/water.glsl +++ b/shaders/ogl46/water.glsl @@ -53,6 +53,45 @@ layout(std430, binding = WATER_SSBO_BINDING) readonly buffer water_ssbo uint water_cells[]; }; +// the snow on a glass of the snow (water_snow.comp, mat_snow_windscreen.frag): two maps of WATER_MAP x WATER_MAP texels after the +// wetted paths in water_cells, two uint a texel (halves: x the snow, mm, y the water, mm, z how packed the snow is 0 .. 1, w random +// 0 .. 1, the shape of its flakes); a step reads one and writes the other, the header of the glass says which one is the latest +// (z 1 when there's a map of the snow, w the map) +uint water_snow_base(uint slot, uint map) +{ + return uint(WATER_SLOTS * WATER_GRID * WATER_GRID * (1 + WATER_CELL_DROPS)) + uint(WATER_SLOTS * WATER_TRAIL * WATER_TRAIL) + (map * uint(WATER_SLOTS) + slot) * uint(WATER_MAP * WATER_MAP * 2); +} + +// a texel of a map of the snow, nothing outside the map +vec4 water_snow_texel(uint slot, uint map, ivec2 texel) +{ + if (any(lessThan(texel, ivec2(0))) || any(greaterThanEqual(texel, ivec2(WATER_MAP)))) + return vec4(0.0); + uint index = water_snow_base(slot, map) + 2u * uint(texel.y * WATER_MAP + texel.x); + return vec4(unpackHalf2x16(water_cells[index]), unpackHalf2x16(water_cells[index + 1u])); +} + +// the latest map of the snow of a glass; false when there's none yet +bool water_snow_map(uint slot, out uint map) +{ + vec4 header = water_header[slot]; + map = (header.w > 0.5) ? 1u : 0u; + return header.z > 0.5; +} + +// the latest map of the snow filtered between the texels at a place of the glass +vec4 water_snow_at(uint slot, uint map, vec2 uv) +{ + vec2 p = uv * float(WATER_MAP) - 0.5; + ivec2 i = ivec2(floor(p)); + vec2 f = p - vec2(i); + vec4 a = water_snow_texel(slot, map, i); + vec4 b = water_snow_texel(slot, map, i + ivec2(1, 0)); + vec4 c = water_snow_texel(slot, map, i + ivec2(0, 1)); + vec4 d = water_snow_texel(slot, map, i + ivec2(1, 1)); + return mix(mix(a, b, f.x), mix(c, d, f.x), f.y); +} + // the radius of the footprint of a drop holding Volume mm3 of water, metres: a cap of a sphere meeting the glass at 40 degrees float water_radius(float volume) { diff --git a/shaders/ogl46/water_sim.glsl b/shaders/ogl46/water_sim.glsl index 75919c9e9..d611b6c0e 100644 --- a/shaders/ogl46/water_sim.glsl +++ b/shaders/ogl46/water_sim.glsl @@ -41,6 +41,11 @@ layout(std140, binding = WATER_UBO_BINDING) uniform water_ubo // every glass: x the landings tried in the step, y 1 / the most metres squared a unit of its uv holds, z 1 when it's simulated, // w the size of its drops (rain_scale of its material) vec4 water_slots[WATER_SLOTS]; + // the snow (water_snow.comp): x the flakes coming to a square metre of the ground in a second, y how much warmer than the air + // the glass is, degrees, z the speed of the vehicle, m/s, w the map of the snow the step reads (0, 1) + vec4 water_snow; + // every glass: 1 when its snow is simulated + vec4 water_snow_slots; }; const float WATER_GRAVITY = 9.81; diff --git a/shaders/ogl46/water_snow.comp b/shaders/ogl46/water_snow.comp new file mode 100644 index 000000000..ed0c8e34b --- /dev/null +++ b/shaders/ogl46/water_snow.comp @@ -0,0 +1,316 @@ +#version 460 core +#define STAGE_COMPUTE 1 +// the snow on the windscreens (water_sim.glsl, water.glsl): a step of the snow of the glasses of the snow, a thread per texel of +// the map of a glass, a row of groups per glass. the step reads one map of the snow and writes the other (water_snow.w says which +// one it reads), so a texel sees its neighbours as they were before the step +// +// what a texel holds: the snow, mm (as it lies, fluffy or packed), the water, mm (the melted snow, a film on the glass and soaked +// into the snow), how packed the snow is (0 fresh flakes .. 1 snow pressed by a blade) and a random number, the shape of its +// flakes (mat_snow_windscreen.frag). in a step, in this order: +// - the snow and the water move: wet heavy snow (slush) slides down the glass, dry powder is blown along it by the air at speed, +// the water runs down as a film and is pushed by the air; and snow piled higher than its neighbours by more than it holds slips +// onto them (a ridge left by a blade spreads into a wedge). what goes over the edge of the glass falls off +// - the blades: a blade going over a texel in the step takes its snow (a little stays, and a wet trace of the water), and pushes +// it on in front of itself: the texel just ahead of the line of texels the blade went over gets their snow, packed. at the end +// of its sweep the blade leaves it there, a ridge; pushed past the edge of the glass, it's gone +// - the flakes land: as many as come to the glass facing the snow coming at it against the cab (and a few in the eddies of the +// air where it doesn't face it), each bringing its snow to the texel; the faster the vehicle the more of the dry ones bounce off +// - the snow melts: the glass is warmer than the air (the cab is heated), the less so the faster the vehicle goes; above zero the +// snow melts at the glass into its water, the water dries slowly + +#include "common.glsl" +#include "water_sim.glsl" + +layout(local_size_x = WATER_GROUP_SIZE) in; + +// how dense the snow is: fresh flakes and snow pressed by a blade (the water in a mm of it, mm) +const float SNOW_FRESH = 0.1; +const float SNOW_PACKED = 0.4; +// the snow of a flake, mm3 of water (0.3 .. 2.5 as the flakes of the water passes, water_spawn.comp) +const float SNOW_FLAKE_LEAST = 0.3; +const float SNOW_FLAKE_MOST = 2.5; +// the speed of the snow coming at the glass in the eddies of the air, m/s: a glass not facing the snow gets a little of it +const float SNOW_EDDIES = 0.25; +// the most snow a texel holds, mm, and the step of the snow to a neighbour it holds without slipping onto it, mm +const float SNOW_MOST = 40.0; +const float SNOW_REPOSE = 3.0; +// the share of the snow over the step slipping onto the neighbour in a second +const float SNOW_SLIP = 6.0; +// the share of the snow a blade leaves, of the water, and of the snow it takes which it leaves as a wet trace +const float SNOW_WIPE_MISS = 0.03; +const float SNOW_WIPE_WATER = 0.25; +const float SNOW_WIPE_TRACE = 0.08; +// the longest line of texels a blade goes over in a step, texels +const int SNOW_WIPE_LINE = 12; +// the water the snow melts into at the glass a second for each degree the glass is above zero, mm +const float SNOW_MELT = 0.003; +// the water drying a second, mm +const float SNOW_DRY = 0.0004; +// how fast slush slides down a vertical glass, how fast dry powder goes with the air (a share of the speed of the air along the +// glass), how fast the film of water runs down a vertical glass and goes with the air, m/s +const float SNOW_SLIDE = 0.03; +const float SNOW_BLOW = 0.004; +const float SNOW_RUN = 0.02; +const float SNOW_RUN_AIR = 0.002; + +// the neighbours of a texel +const ivec2 SNOW_AROUND[8] = ivec2[8](ivec2(-1, -1), ivec2(0, -1), ivec2(1, -1), ivec2(-1, 0), ivec2(1, 0), ivec2(-1, 1), ivec2(0, 1), ivec2(1, 1)); + +uint snow_slot; +uint snow_from; +uint snow_axes_base; + +bool snow_on_glass(ivec2 texel) +{ + if (any(lessThan(texel, ivec2(0))) || any(greaterThanEqual(texel, ivec2(WATER_MAP)))) + return false; + return water_axes[(snow_axes_base + uint(texel.y * WATER_MAP + texel.x)) * 2u].w > 0.75; +} + +vec4 snow_get(ivec2 texel) +{ + return snow_on_glass(texel) ? water_snow_texel(snow_slot, snow_from, texel) : vec4(0.0); +} + +float snow_blade(ivec2 texel, int blade) +{ + texel = clamp(texel, ivec2(0), ivec2(WATER_MAP - 1)); + return water_blades[snow_axes_base + uint(texel.y * WATER_MAP + texel.x)][blade]; +} + +// whether the blade went over the texel in the step (it reaches it, and the place of the texel along its sweep is between where +// the blade was and where it is, the place it was at belonging to the step before) +bool snow_crossed(ivec2 texel, int blade) +{ + float before = water_blades_before[blade]; + float now = water_blades_now[blade]; + if ((before < 0.0) || (now < 0.0) || (now == before) || (false == snow_on_glass(texel))) + return false; + float place = snow_blade(texel, blade); + if (place <= 0.001) + return false; + return (now > before) ? ((place > before) && (place <= now)) : ((place < before) && (place >= now)); +} + +// the way the blade goes over the texel, a step of a texel (zero when it can't be told) +ivec2 snow_blade_way(ivec2 texel, int blade) +{ + float here = snow_blade(texel, blade); + float right = snow_blade(texel + ivec2(1, 0), blade); + float left = snow_blade(texel - ivec2(1, 0), blade); + float up = snow_blade(texel + ivec2(0, 1), blade); + float down = snow_blade(texel - ivec2(0, 1), blade); + // one-sided where a neighbour is out of the sweep + right = (right > 0.001) ? right : here; + left = (left > 0.001) ? left : here; + up = (up > 0.001) ? up : here; + down = (down > 0.001) ? down : here; + vec2 gradient = vec2(right - left, up - down); + if (dot(gradient, gradient) < 1e-12) + return ivec2(0); + vec2 way = normalize(gradient) * ((water_blades_now[blade] > water_blades_before[blade]) ? 1.0 : -1.0); + return ivec2(round(way)); +} + +// the frame of the glass at a texel, unfiltered +water_frame_s snow_frame(ivec2 texel) +{ + uint index = snow_axes_base + uint(texel.y * WATER_MAP + texel.x); + vec4 a = water_axes[index * 2u]; + vec4 b = water_axes[index * 2u + 1u]; + water_frame_s frame = water_frame_s(a.xyz, b.xyz, vec3(0.0, 0.0, 1.0), a.w); + vec3 n = cross(a.xyz, b.xyz); + if (dot(n, n) > 1e-24) + frame.normal = normalize(n) * ((b.w < 0.0) ? -1.0 : 1.0); + return frame; +} + +// how the snow (xy) and the water (zw) of a texel move, texels a second +vec4 snow_motion(ivec2 texel, vec4 state) +{ + if ((state.x <= 0.001) && (state.y <= 0.001)) + return vec4(0.0); + water_frame_s frame = snow_frame(texel); + vec3 n = frame.normal; + vec3 down = -water_up.xyz; + vec3 slope = down - n * dot(down, n); + vec3 air = water_air.xyz - n * dot(water_air.xyz, n); + float wind = length(air); + float density = mix(SNOW_FRESH, SNOW_PACKED, state.z); + float wet = state.y / (state.y + state.x * density + 1e-4); + // slush: wet and heavy enough to go; dry powder: loose and thin enough to be blown + float slide = SNOW_SLIDE * smoothstep(0.25, 0.7, wet) * smoothstep(2.0, 8.0, state.x); + float blow = SNOW_BLOW * smoothstep(10.0, 25.0, wind) * (1.0 - state.z) * (1.0 - smoothstep(0.1, 0.3, wet)) * (1.0 - smoothstep(1.0, 4.0, state.x)); + vec3 snow = slope * slide + air * blow; + // the film of water runs where there's enough of it, not the water soaked into the snow + float film = (1.0 - smoothstep(0.5, 3.0, state.x)); + vec3 water = (slope * SNOW_RUN * smoothstep(0.03, 0.15, state.y) + air * SNOW_RUN_AIR * smoothstep(0.03, 0.1, state.y)) * film; + return vec4(water_to_uv(frame, snow), water_to_uv(frame, water)) * float(WATER_MAP); +} + +// the shares of a texel going to its neighbours along x and along y in the step, for a motion in texels a second +vec2 snow_out(vec2 motion, float seconds) +{ + return min(abs(motion) * seconds, vec2(0.45)); +} + +// the snow and the water of the texel after they moved in the step: what leaves it, what comes from its neighbours; the +// packing and the shape go with the snow +vec4 snow_move(ivec2 texel, vec4 state, float seconds) +{ + vec4 motion = snow_motion(texel, state); + vec2 snowout = snow_out(motion.xy, seconds); + vec2 waterout = snow_out(motion.zw, seconds); + float snow = state.x * (1.0 - snowout.x - snowout.y); + float water = state.y * (1.0 - waterout.x - waterout.y); + float packed = state.z * snow; + float slip = min(SNOW_SLIP * seconds, 0.2); + const ivec2 sides[4] = ivec2[4](ivec2(1, 0), ivec2(-1, 0), ivec2(0, 1), ivec2(0, -1)); + for (int k = 0; k < 4; ++k) + { + ivec2 side = sides[k]; + ivec2 other = texel + side; + bool glass = snow_on_glass(other); + vec4 neighbour = glass ? water_snow_texel(snow_slot, snow_from, other) : vec4(0.0); + // slipping onto the neighbour (off the glass: falling off) and from it + float away = slip * max(state.x - neighbour.x - SNOW_REPOSE, 0.0); + snow -= away; + packed -= away * state.z; + if (false == glass) + continue; + float coming = slip * max(neighbour.x - state.x - SNOW_REPOSE, 0.0); + // what the motion of the neighbour brings: it goes this way along the axis of the side + vec4 itsmotion = snow_motion(other, neighbour); + vec2 itssnow = snow_out(itsmotion.xy, seconds); + vec2 itswater = snow_out(itsmotion.zw, seconds); + if (side.x != 0) + { + coming += (itsmotion.x * float(side.x) < 0.0) ? neighbour.x * itssnow.x : 0.0; + water += (itsmotion.z * float(side.x) < 0.0) ? neighbour.y * itswater.x : 0.0; + } + else + { + coming += (itsmotion.y * float(side.y) < 0.0) ? neighbour.x * itssnow.y : 0.0; + water += (itsmotion.w * float(side.y) < 0.0) ? neighbour.y * itswater.y : 0.0; + } + snow += coming; + packed += coming * neighbour.z; + } + snow = max(snow, 0.0); + return vec4(snow, max(water, 0.0), (snow > 1e-4) ? clamp(packed / snow, 0.0, 1.0) : 0.0, state.w); +} + +// the snow and the water of the line of texels the blade went over in the step, ending at a texel (the blade goes on from it to +// the texel ahead), and the packing of the snow (summed for its snow) +vec3 snow_line(ivec2 last, int blade, ivec2 way) +{ + vec3 line = vec3(0.0); + for (int k = 0; k < SNOW_WIPE_LINE; ++k) + { + ivec2 texel = last - way * k; + if (false == snow_crossed(texel, blade)) + break; + vec4 state = water_snow_texel(snow_slot, snow_from, texel); + line += vec3(state.x, state.y, state.x * state.z); + } + return line; +} + +void main() +{ + snow_slot = gl_WorkGroupID.y; + uint place = gl_GlobalInvocationID.x; + if ((water_snow_slots[snow_slot] < 0.5) || (place >= uint(WATER_MAP * WATER_MAP))) + return; + snow_from = (water_snow.w > 0.5) ? 1u : 0u; + uint to = 1u - snow_from; + snow_axes_base = snow_slot * uint(WATER_MAP * WATER_MAP); + if (place == 0u) + water_header[snow_slot].zw = vec2(1.0, float(to)); + ivec2 texel = ivec2(int(place % uint(WATER_MAP)), int(place / uint(WATER_MAP))); + uint index = water_snow_base(snow_slot, to) + 2u * place; + if (false == snow_on_glass(texel)) + { + water_cells[index] = 0u; + water_cells[index + 1u] = 0u; + return; + } + float seconds = water_step.x; + vec4 before = water_snow_texel(snow_slot, snow_from, texel); + vec4 state = snow_move(texel, before, seconds); + + // the blades: taken where they went over, pushed on in front of them + bool crossed = false; + vec3 pushed = vec3(0.0); + for (int blade = 0; blade < 4; ++blade) + { + if (snow_crossed(texel, blade)) + { + crossed = true; + continue; + } + for (int k = 0; k < 8; ++k) + { + ivec2 from = texel + SNOW_AROUND[k]; + if (false == snow_crossed(from, blade)) + continue; + ivec2 way = snow_blade_way(from, blade); + if ((way != ivec2(0)) && (from + way == texel)) + pushed += snow_line(from, blade, way); + } + } + if (crossed) + { + float taken = state.x * (1.0 - SNOW_WIPE_MISS); + state.x -= taken; + state.y = state.y * SNOW_WIPE_WATER + taken * mix(SNOW_FRESH, SNOW_PACKED, state.z) * SNOW_WIPE_TRACE; + } + if (pushed.x + pushed.y > 0.0) + { + float snow = state.x + pushed.x; + // pressed by the blade + state.z = (snow > 1e-4) ? clamp((state.z * state.x + max(pushed.z, pushed.x * 0.85)) / snow, 0.0, 1.0) : state.z; + state.x = snow; + state.y += pushed.y * 0.7; + } + + // the flakes landing + water_frame_s frame = snow_frame(texel); + float area = length(cross(frame.tu, frame.tv)) / float(WATER_MAP * WATER_MAP); // m2 + vec3 coming = water_air.xyz - water_up.xyz * water_air.w; // the snow against the cab, m/s + float facing = max(-dot(coming, frame.normal), 0.0) + SNOW_EDDIES; + float speed = water_snow.z; + float density = mix(SNOW_FRESH, SNOW_PACKED, state.z); + float wet = state.y / (state.y + state.x * density + 1e-4); + // the dry flakes bounce off a fast glass, the ones landing on wet snow or water stay + float sticking = 1.0 - 0.6 * smoothstep(15.0, 35.0, speed) * (1.0 - smoothstep(0.1, 0.4, wet)); + float landings = water_snow.x / max(water_air.w, 0.1) * facing * area * seconds * sticking; + vec4 random = water_random(place, snow_slot, water_counts.x, 0x5e0du); + float count = min(floor(landings) + ((random.x < fract(landings)) ? 1.0 : 0.0), 4.0); + if (count > 0.0) + { + float water = count * mix(SNOW_FLAKE_LEAST, SNOW_FLAKE_MOST, random.y * random.y); // mm3 + float added = water / SNOW_FRESH / max(area * 1e6, 1e-3); // mm of fresh snow over the texel + float snow = state.x + added; + state.z = state.z * state.x / snow; + // a flake on the bare glass has a shape of its own + state.w = (state.x < 0.3) ? random.z : state.w; + state.x = snow; + } + + // the melting at the warm glass and the drying + float glass = water_up.w + water_snow.y / (1.0 + speed / 12.0); + float melted = min(SNOW_MELT * max(glass, 0.0) * seconds, state.x * density); + state.x -= melted / max(density, 1e-3); + state.y = max(state.y + melted - SNOW_DRY * seconds, 0.0); + + state.x = clamp(state.x, 0.0, SNOW_MOST); + state.y = min(state.y, 4.0); + if (state.x < 0.002) + { + state.x = 0.0; + state.z = 0.0; + } + water_cells[index] = packHalf2x16(state.xy); + water_cells[index + 1u] = packHalf2x16(state.zw); +} diff --git a/shaders/vk14/mat_snow_windscreen.frag b/shaders/vk14/mat_snow_windscreen.frag index 8a81a1ee8..b0f14979c 100644 --- a/shaders/vk14/mat_snow_windscreen.frag +++ b/shaders/vk14/mat_snow_windscreen.frag @@ -70,11 +70,14 @@ layout(location = 1) out vec4 out_motion; #pragma param(reflection, 1, 2, 1, zero) #pragma param(glossiness, 1, 3, 1, glossiness) #pragma param(raindrop_grid_size, 2, 0, 1, one) +// snow_slot: 1 + the slot of the glass in the simulation of the snow (the renderer sets it), 0: none +#pragma param(snow_slot, 2, 2, 1, zero) #include "light_common.glsl" #include "apply_fog.glsl" #include "tonemapping.glsl" #include "random.glsl" +#include "water.glsl" layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 0) uniform sampler2D diffuse; layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 1) uniform sampler2D raindropsatlas; @@ -447,6 +450,158 @@ void snowFilm(float gridSize, inout vec3 accRGB, inout float accA, inout vec3 ac accA = accA + a * under; } +// --- The snow of the simulation (water_snow.comp): the map of the glass --------------------------------------------------- +// When the renderer simulates the snow of the glass (the param snow_slot holds 1 + its slot), the snow, the water, how packed the +// snow is and the shape of its flakes come from the map of the glass (water.glsl), which keeps them from frame to frame: the flakes +// land and stay where they landed, pile up into a layer, the blades push it into ridges at the ends of their sweeps and leave a wet +// trace, it melts at the warm glass and slush slides down. Here it's only drawn: +// - a texel of the map with a little snow is a flake (atlas tiles 0..2: fluffy when dry, a wet lump when wet or packed, crumbs), +// the more snow the bigger; with mostly water left it's the water spot (tile 3) +// - where the snow is thicker it's a layer: its edge torn by the grain of the snow, as opaque as it's thick (Beer-Lambert), grey +// and translucent when wet (slush), smoother and denser when packed; its relief from the slopes of the snow of the map +// - the water: wet glass bends the picture behind (the post-processing), the thin snow hazes it +// Without the simulation (an older renderer, no compute) the procedural snow below draws the glass as before. +const float snow_map_flake = 1.15; // size of a flake relative to a texel of the map (the snow of a texel) +const float snow_map_layer = 1.0; // mm of snow above which the flakes run together into a layer +const float snow_map_opaque = 1.8; // mm of fresh snow which lets 1/e of the light through (packed: half of it) +const float snow_map_relief = 0.6; // how much the slopes of the snow tilt its surface +const float snow_map_grain = 0.35; // the fine grain of the layer (its relief) +const float snow_map_wet_bend = 0.6; // how strongly a wet glass bends the picture behind it + +int snMapSlot = -1; +uint snMapIndex = 0u; +float snWet = 0.0; // set by snowMapped: the film of water (0 .. 1) +vec2 snWetN = vec2(0.0); // set by snowMapped: the slope of the water, for the bend of the picture + +// the snow of the map at a place of the glass +vec4 snMapAt(vec2 uv) +{ + return water_snow_at(uint(snMapSlot), snMapIndex, uv); +} + +// a flake of the map drawn over the accumulators: Local the place in its sprite (0 .. 1), Tile the corner of its tile in the atlas +void snowMapSprite(vec2 local, vec2 tile, float alpha, float tone, float flip, float c, float s, float lod, + inout vec3 accRGB, inout float accA, inout vec3 accN, inout float accTr) +{ + vec4 t = textureLod(raindropsatlas, tile + local * 0.5, lod); + vec4 n = textureLod(normalmap, tile + local * 0.5, lod); + float a = clamp(t.a * alpha * snow_opacity, 0.0, 1.0); + if (a <= 0.0) + return; + vec2 nxy = (n.rg * 2.0 - 1.0) * snow_bump; + nxy.x *= flip; + vec2 nuv = vec2(c * nxy.x - s * nxy.y, s * nxy.x + c * nxy.y); + vec3 nT = vec3(nuv, sqrt(max(1.0 - dot(nuv, nuv), 0.02))); + float tr = (1.0 - t.a * 0.55); + accRGB = t.rgb * tone * a + accRGB * (1.0 - a); + accN = nT * a + accN * (1.0 - a); + accTr = tr * a + accTr * (1.0 - a); + accA = a + accA * (1.0 - a); +} + +// the flakes of the texels around the pixel +void snowMapFlakes(vec2 aspect, float pixelUV, inout vec3 accRGB, inout float accA, inout vec3 accN, inout float accTr) +{ + vec2 p = f_coord * float(WATER_MAP); + ivec2 here = ivec2(floor(p)); + for (int oy = -1; oy <= 1; ++oy) + { + for (int ox = -1; ox <= 1; ++ox) + { + ivec2 texel = here + ivec2(ox, oy); + vec4 q = water_snow_texel(uint(snMapSlot), snMapIndex, texel); + float density = mix(0.1, 0.4, q.z); + float wet = q.y / (q.y + q.x * density + 1e-4); + bool spot = (q.x < 0.05) && (q.y > 0.015); // a flake melted into its water + if ((q.x < 0.05) && (false == spot)) + continue; + uint seed = Hash(uvec3(uvec2(texel), uint(q.w * 65535.0) + 7u * uint(snMapSlot))); + vec2 center = vec2(texel) + vec2(0.15 + 0.7 * RandF(seed), 0.15 + 0.7 * RandF(seed)); + float ang = RandF(seed) * 6.2831853; + float flip = (RandF(seed) < 0.5) ? -1.0 : 1.0; + float kind = RandF(seed); + float tone = mix(0.94, 1.04, RandF(seed)); + float aniso = mix(0.62, 1.0, RandF(seed)); + // the more snow the texel holds the bigger the flake (a lump of a few flakes) + float size = snow_map_flake * mix(0.6, 1.0, RandF(seed)) * (spot ? mix(0.5, 1.1, smoothstep(0.015, 0.2, q.y)) + : mix(0.7, 1.6, smoothstep(0.05, 2.0, q.x))); + vec2 d = (p - center) * aspect; + float c = cos(ang); + float s = sin(ang); + vec2 r = vec2(c * d.x + s * d.y, -s * d.x + c * d.y); + r.x *= flip / aniso; + vec2 local = r / size + 0.5; + if (any(lessThan(local, vec2(0.0))) || any(greaterThan(local, vec2(1.0)))) + continue; + float sizeUV = size / float(WATER_MAP); + float lod = clamp(log2(max(pixelUV / sizeUV * 0.5 * float(textureSize(raindropsatlas, 0).x), 1.0)), 0.0, 4.0); + vec2 tile; + if (spot) + tile = vec2(0.5, 0.0); + else if ((wet > 0.3) || (q.z > 0.5) || (kind < 0.30)) + tile = vec2(0.5, 0.5); // a wet lump + else if (kind < 0.75) + tile = vec2(0.0, 0.5); // fluffy + else + tile = vec2(0.0, 0.0); // crumbs + float alpha = spot ? smoothstep(0.015, 0.08, q.y) : smoothstep(0.05, 0.25, q.x); + snowMapSprite(local, tile, alpha, tone, flip, c, s, lod, accRGB, accA, accN, accTr); + } + } +} + +// the snow of the map at the pixel: the flakes, the layer of the snow under them, the water, the haze +void snowMapped(vec2 aspect, float pixelUV, inout vec3 accRGB, inout float accA, inout float smear, inout vec3 accN, inout float accTr) +{ + vec4 q = snMapAt(f_coord); + float e = 1.0 / float(WATER_MAP); + vec4 qr = snMapAt(f_coord + vec2(e, 0.0)); + vec4 ql = snMapAt(f_coord - vec2(e, 0.0)); + vec4 qu = snMapAt(f_coord + vec2(0.0, e)); + vec4 qd = snMapAt(f_coord - vec2(0.0, e)); + float around = (q.x + qr.x + ql.x + qu.x + qd.x) * 0.2; + + // the flakes of thin snow and the water spots + snowMapFlakes(aspect, pixelUV, accRGB, accA, accN, accTr); + + // the water: the film bends the picture behind + snWet = smoothstep(0.02, 0.25, q.y) * (1.0 - smoothstep(0.5, 2.0, q.x)); + snWetN = vec2(qr.y - ql.y, qu.y - qd.y) * 2.0 + (vec2(snNoise(f_coord * 900.0), snNoise(f_coord * 900.0 + 31.0)) - 0.5) * 0.6 * snWet; + smear = max(smear, snWet * snow_melt_blur * 0.6); + // thin snow hazes the picture around it + snHaze = max(snHaze, smoothstep(0.03, 0.6, around)); + + // the layer, where the snow has run together + vec2 gp = f_coord * float(WATER_MAP) * aspect; + float grain = snFbm(gp * 0.9 + 3.0) * 0.55 + snFbm(gp * 2.7 + 11.0) * 0.30 + snFbm(gp * 7.1 + 5.0) * 0.15; + float edge = (q.x - snow_map_layer) / max(snow_map_layer, 1e-3) + (grain - 0.5) * 1.6; + float cover = smoothstep(0.0, 0.35, edge); + if (cover <= 0.002) + return; + float density = mix(0.1, 0.4, q.z); + float wet = q.y / (q.y + q.x * density + 1e-4); + float opaque = snow_map_opaque * mix(1.0, 0.5, q.z); + float thick = 1.0 - exp(-q.x / opaque); + // the relief: the slopes of the snow of the map (ridges, the edges of the swept arcs) and the grain of the fresh snow + vec2 slope = vec2(qr.x - ql.x, qu.x - qd.x) * 0.5 * snow_map_relief; + float f1 = snFbm(gp * 3.3); + float ef = 0.05; + vec2 gf = vec2(snFbm(gp * 3.3 + vec2(ef, 0.0)) - f1, snFbm(gp * 3.3 + vec2(0.0, ef)) - f1) / ef; + vec3 nT = normalize(vec3(-(slope + gf * snow_map_grain * (1.0 - 0.6 * q.z)) * snow_bump, 1.0)); + + vec3 fresh = vec3(0.93, 0.95, 0.98); + vec3 packed = vec3(0.84, 0.87, 0.91); + vec3 slush = vec3(0.60, 0.66, 0.73); + vec3 col = mix(mix(fresh, packed, q.z), slush, smoothstep(0.2, 0.7, wet)); + col *= (0.80 + 0.30 * f1 * (1.0 - 0.5 * q.z)) * (0.92 + 0.12 * grain); + float a = clamp(cover * mix(0.35, 0.97, thick) * mix(1.0, 0.75, smoothstep(0.3, 0.8, wet)) * snow_pack * snow_opacity, 0.0, 0.97); + float under = 1.0 - accA; + accRGB = accRGB + col * a * under; + accN = accN + nT * a * under; + accTr = accTr + mix(0.60, 0.25, thick) * a * under; + accA = accA + a * under; +} + void main() { // Derivatives first, in uniform control flow (no discard before them). @@ -475,7 +630,20 @@ void main() float smear = 0.0; vec3 snowN = vec3(0.0); float snowTr = 0.0; - if (rain_params.x > 0.0) + // the snow of the simulation where the glass has a map of it, else made up as before + snMapSlot = int(param[2].z + 0.5) - 1; + bool mapped = (snMapSlot >= 0) && (snMapSlot < WATER_SLOTS) && water_snow_map(uint(snMapSlot), snMapIndex); + if (mapped) + { + // the texels of the map as they lie on the glass (the uv may be stretched) + ivec2 texel = clamp(ivec2(floor(f_coord * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); + uint axis = (uint(snMapSlot) * uint(WATER_MAP * WATER_MAP) + uint(texel.y * WATER_MAP + texel.x)) * 2u; + vec2 span = vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)); + if (min(span.x, span.y) > 1e-6) + aspect = span / max(span.x, span.y); + snowMapped(aspect, pixelUV, snowRGB, snowA, smear, snowN, snowTr); + } + else if (rain_params.x > 0.0) { float mass = 0.0; if (snow_small > 0.0) @@ -536,7 +704,13 @@ void main() { float thin = smoothstep(0.04, 0.20, snowA) * (1.0 - smoothstep(0.55, 0.90, snowA)); vec4 req = vec4(0.0); - if ((snow_distort > 0.0) && (thin > 0.30) && (glassA < 0.5)) + if ((snWet > 0.25) && (snowA < 0.35) && (glassA < 0.5)) + { + // the water on the glass: its film and its spots bend the picture behind + vec2 bend = clamp(-snWetN * snow_map_wet_bend, vec2(-1.0), vec2(1.0)) * smoothstep(0.25, 0.6, snWet); + req = vec4(WS_DROP_BIAS + WS_BEND_ENC * bend.x, WS_DROP_BIAS + WS_BEND_ENC * bend.y, 0.0, 1.0); + } + else if ((snow_distort > 0.0) && (thin > 0.30) && (glassA < 0.5)) { vec2 slope = snowN.xy / max(snowA, 1e-3); vec2 bend = clamp(-slope * snow_distort, vec2(-1.0), vec2(1.0)) * smoothstep(0.30, 0.60, thin) * (SNOW_REFR_MAXPX / WS_REFR_MAXPX); diff --git a/shaders/vk14/water.glsl b/shaders/vk14/water.glsl index 41d672d60..ed2b1094d 100644 --- a/shaders/vk14/water.glsl +++ b/shaders/vk14/water.glsl @@ -54,6 +54,45 @@ layout(std430, set = PASS_SET, binding = WATER_SSBO) readonly buffer water_ssbo uint water_cells[]; }; +// the snow on a glass of the snow (water_snow.comp, mat_snow_windscreen.frag): two maps of WATER_MAP x WATER_MAP texels after the +// wetted paths in water_cells, two uint a texel (halves: x the snow, mm, y the water, mm, z how packed the snow is 0 .. 1, w random +// 0 .. 1, the shape of its flakes); a step reads one and writes the other, the header of the glass says which one is the latest +// (z 1 when there's a map of the snow, w the map) +uint water_snow_base(uint slot, uint map) +{ + return uint(WATER_SLOTS * WATER_GRID * WATER_GRID * (1 + WATER_CELL_DROPS)) + uint(WATER_SLOTS * WATER_TRAIL * WATER_TRAIL) + (map * uint(WATER_SLOTS) + slot) * uint(WATER_MAP * WATER_MAP * 2); +} + +// a texel of a map of the snow, nothing outside the map +vec4 water_snow_texel(uint slot, uint map, ivec2 texel) +{ + if (any(lessThan(texel, ivec2(0))) || any(greaterThanEqual(texel, ivec2(WATER_MAP)))) + return vec4(0.0); + uint index = water_snow_base(slot, map) + 2u * uint(texel.y * WATER_MAP + texel.x); + return vec4(unpackHalf2x16(water_cells[index]), unpackHalf2x16(water_cells[index + 1u])); +} + +// the latest map of the snow of a glass; false when there's none yet +bool water_snow_map(uint slot, out uint map) +{ + vec4 header = water_header[slot]; + map = (header.w > 0.5) ? 1u : 0u; + return header.z > 0.5; +} + +// the latest map of the snow filtered between the texels at a place of the glass +vec4 water_snow_at(uint slot, uint map, vec2 uv) +{ + vec2 p = uv * float(WATER_MAP) - 0.5; + ivec2 i = ivec2(floor(p)); + vec2 f = p - vec2(i); + vec4 a = water_snow_texel(slot, map, i); + vec4 b = water_snow_texel(slot, map, i + ivec2(1, 0)); + vec4 c = water_snow_texel(slot, map, i + ivec2(0, 1)); + vec4 d = water_snow_texel(slot, map, i + ivec2(1, 1)); + return mix(mix(a, b, f.x), mix(c, d, f.x), f.y); +} + // the radius of the footprint of a drop holding Volume mm3 of water, metres: a cap of a sphere meeting the glass at 40 degrees float water_radius(float volume) { diff --git a/shaders/vk14/water_sim.glsl b/shaders/vk14/water_sim.glsl index 82710150a..d1794ca64 100644 --- a/shaders/vk14/water_sim.glsl +++ b/shaders/vk14/water_sim.glsl @@ -41,6 +41,11 @@ layout(std140, set = COMPUTE_SET, binding = 1) uniform water_ubo // every glass: x the landings tried in the step, y 1 / the most metres squared a unit of its uv holds, z 1 when it's simulated, // w the size of its drops (rain_scale of its material) vec4 water_slots[WATER_SLOTS]; + // the snow (water_snow.comp): x the flakes coming to a square metre of the ground in a second, y how much warmer than the air + // the glass is, degrees, z the speed of the vehicle, m/s, w the map of the snow the step reads (0, 1) + vec4 water_snow; + // every glass: 1 when its snow is simulated + vec4 water_snow_slots; }; const float WATER_GRAVITY = 9.81; diff --git a/shaders/vk14/water_snow.comp b/shaders/vk14/water_snow.comp new file mode 100644 index 000000000..d241d5fe3 --- /dev/null +++ b/shaders/vk14/water_snow.comp @@ -0,0 +1,319 @@ +#version 460 core +#define STAGE_COMPUTE 1 +// the snow on the windscreens (water_sim.glsl, water.glsl): a step of the snow of the glasses of the snow, a thread per texel of +// the map of a glass, a row of groups per glass. the step reads one map of the snow and writes the other (water_snow.w says which +// one it reads), so a texel sees its neighbours as they were before the step +// +// what a texel holds: the snow, mm (as it lies, fluffy or packed), the water, mm (the melted snow, a film on the glass and soaked +// into the snow), how packed the snow is (0 fresh flakes .. 1 snow pressed by a blade) and a random number, the shape of its +// flakes (mat_snow_windscreen.frag). in a step, in this order: +// - the snow and the water move: wet heavy snow (slush) slides down the glass, dry powder is blown along it by the air at speed, +// the water runs down as a film and is pushed by the air; and snow piled higher than its neighbours by more than it holds slips +// onto them (a ridge left by a blade spreads into a wedge). what goes over the edge of the glass falls off +// - the blades: a blade going over a texel in the step takes its snow (a little stays, and a wet trace of the water), and pushes +// it on in front of itself: the texel just ahead of the line of texels the blade went over gets their snow, packed. at the end +// of its sweep the blade leaves it there, a ridge; pushed past the edge of the glass, it's gone +// - the flakes land: as many as come to the glass facing the snow coming at it against the cab (and a few in the eddies of the +// air where it doesn't face it), each bringing its snow to the texel; the faster the vehicle the more of the dry ones bounce off +// - the snow melts: the glass is warmer than the air (the cab is heated), the less so the faster the vehicle goes; above zero the +// snow melts at the glass into its water, the water dries slowly + +// none of the pass set: everything the kernel reads is pushed with its dispatch (set COMPUTE_SET, water_sim.glsl), so it runs +// before the passes of the frame set theirs up +#define LAYOUT_UI 1 +#include "common.glsl" +#include "water_sim.glsl" + +layout(local_size_x = WATER_GROUP_SIZE) in; + +// how dense the snow is: fresh flakes and snow pressed by a blade (the water in a mm of it, mm) +const float SNOW_FRESH = 0.1; +const float SNOW_PACKED = 0.4; +// the snow of a flake, mm3 of water (0.3 .. 2.5 as the flakes of the water passes, water_spawn.comp) +const float SNOW_FLAKE_LEAST = 0.3; +const float SNOW_FLAKE_MOST = 2.5; +// the speed of the snow coming at the glass in the eddies of the air, m/s: a glass not facing the snow gets a little of it +const float SNOW_EDDIES = 0.25; +// the most snow a texel holds, mm, and the step of the snow to a neighbour it holds without slipping onto it, mm +const float SNOW_MOST = 40.0; +const float SNOW_REPOSE = 3.0; +// the share of the snow over the step slipping onto the neighbour in a second +const float SNOW_SLIP = 6.0; +// the share of the snow a blade leaves, of the water, and of the snow it takes which it leaves as a wet trace +const float SNOW_WIPE_MISS = 0.03; +const float SNOW_WIPE_WATER = 0.25; +const float SNOW_WIPE_TRACE = 0.08; +// the longest line of texels a blade goes over in a step, texels +const int SNOW_WIPE_LINE = 12; +// the water the snow melts into at the glass a second for each degree the glass is above zero, mm +const float SNOW_MELT = 0.003; +// the water drying a second, mm +const float SNOW_DRY = 0.0004; +// how fast slush slides down a vertical glass, how fast dry powder goes with the air (a share of the speed of the air along the +// glass), how fast the film of water runs down a vertical glass and goes with the air, m/s +const float SNOW_SLIDE = 0.03; +const float SNOW_BLOW = 0.004; +const float SNOW_RUN = 0.02; +const float SNOW_RUN_AIR = 0.002; + +// the neighbours of a texel +const ivec2 SNOW_AROUND[8] = ivec2[8](ivec2(-1, -1), ivec2(0, -1), ivec2(1, -1), ivec2(-1, 0), ivec2(1, 0), ivec2(-1, 1), ivec2(0, 1), ivec2(1, 1)); + +uint snow_slot; +uint snow_from; +uint snow_axes_base; + +bool snow_on_glass(ivec2 texel) +{ + if (any(lessThan(texel, ivec2(0))) || any(greaterThanEqual(texel, ivec2(WATER_MAP)))) + return false; + return water_axes[(snow_axes_base + uint(texel.y * WATER_MAP + texel.x)) * 2u].w > 0.75; +} + +vec4 snow_get(ivec2 texel) +{ + return snow_on_glass(texel) ? water_snow_texel(snow_slot, snow_from, texel) : vec4(0.0); +} + +float snow_blade(ivec2 texel, int blade) +{ + texel = clamp(texel, ivec2(0), ivec2(WATER_MAP - 1)); + return water_blades[snow_axes_base + uint(texel.y * WATER_MAP + texel.x)][blade]; +} + +// whether the blade went over the texel in the step (it reaches it, and the place of the texel along its sweep is between where +// the blade was and where it is, the place it was at belonging to the step before) +bool snow_crossed(ivec2 texel, int blade) +{ + float before = water_blades_before[blade]; + float now = water_blades_now[blade]; + if ((before < 0.0) || (now < 0.0) || (now == before) || (false == snow_on_glass(texel))) + return false; + float place = snow_blade(texel, blade); + if (place <= 0.001) + return false; + return (now > before) ? ((place > before) && (place <= now)) : ((place < before) && (place >= now)); +} + +// the way the blade goes over the texel, a step of a texel (zero when it can't be told) +ivec2 snow_blade_way(ivec2 texel, int blade) +{ + float here = snow_blade(texel, blade); + float right = snow_blade(texel + ivec2(1, 0), blade); + float left = snow_blade(texel - ivec2(1, 0), blade); + float up = snow_blade(texel + ivec2(0, 1), blade); + float down = snow_blade(texel - ivec2(0, 1), blade); + // one-sided where a neighbour is out of the sweep + right = (right > 0.001) ? right : here; + left = (left > 0.001) ? left : here; + up = (up > 0.001) ? up : here; + down = (down > 0.001) ? down : here; + vec2 gradient = vec2(right - left, up - down); + if (dot(gradient, gradient) < 1e-12) + return ivec2(0); + vec2 way = normalize(gradient) * ((water_blades_now[blade] > water_blades_before[blade]) ? 1.0 : -1.0); + return ivec2(round(way)); +} + +// the frame of the glass at a texel, unfiltered +water_frame_s snow_frame(ivec2 texel) +{ + uint index = snow_axes_base + uint(texel.y * WATER_MAP + texel.x); + vec4 a = water_axes[index * 2u]; + vec4 b = water_axes[index * 2u + 1u]; + water_frame_s frame = water_frame_s(a.xyz, b.xyz, vec3(0.0, 0.0, 1.0), a.w); + vec3 n = cross(a.xyz, b.xyz); + if (dot(n, n) > 1e-24) + frame.normal = normalize(n) * ((b.w < 0.0) ? -1.0 : 1.0); + return frame; +} + +// how the snow (xy) and the water (zw) of a texel move, texels a second +vec4 snow_motion(ivec2 texel, vec4 state) +{ + if ((state.x <= 0.001) && (state.y <= 0.001)) + return vec4(0.0); + water_frame_s frame = snow_frame(texel); + vec3 n = frame.normal; + vec3 down = -water_up.xyz; + vec3 slope = down - n * dot(down, n); + vec3 air = water_air.xyz - n * dot(water_air.xyz, n); + float wind = length(air); + float density = mix(SNOW_FRESH, SNOW_PACKED, state.z); + float wet = state.y / (state.y + state.x * density + 1e-4); + // slush: wet and heavy enough to go; dry powder: loose and thin enough to be blown + float slide = SNOW_SLIDE * smoothstep(0.25, 0.7, wet) * smoothstep(2.0, 8.0, state.x); + float blow = SNOW_BLOW * smoothstep(10.0, 25.0, wind) * (1.0 - state.z) * (1.0 - smoothstep(0.1, 0.3, wet)) * (1.0 - smoothstep(1.0, 4.0, state.x)); + vec3 snow = slope * slide + air * blow; + // the film of water runs where there's enough of it, not the water soaked into the snow + float film = (1.0 - smoothstep(0.5, 3.0, state.x)); + vec3 water = (slope * SNOW_RUN * smoothstep(0.03, 0.15, state.y) + air * SNOW_RUN_AIR * smoothstep(0.03, 0.1, state.y)) * film; + return vec4(water_to_uv(frame, snow), water_to_uv(frame, water)) * float(WATER_MAP); +} + +// the shares of a texel going to its neighbours along x and along y in the step, for a motion in texels a second +vec2 snow_out(vec2 motion, float seconds) +{ + return min(abs(motion) * seconds, vec2(0.45)); +} + +// the snow and the water of the texel after they moved in the step: what leaves it, what comes from its neighbours; the +// packing and the shape go with the snow +vec4 snow_move(ivec2 texel, vec4 state, float seconds) +{ + vec4 motion = snow_motion(texel, state); + vec2 snowout = snow_out(motion.xy, seconds); + vec2 waterout = snow_out(motion.zw, seconds); + float snow = state.x * (1.0 - snowout.x - snowout.y); + float water = state.y * (1.0 - waterout.x - waterout.y); + float packed = state.z * snow; + float slip = min(SNOW_SLIP * seconds, 0.2); + const ivec2 sides[4] = ivec2[4](ivec2(1, 0), ivec2(-1, 0), ivec2(0, 1), ivec2(0, -1)); + for (int k = 0; k < 4; ++k) + { + ivec2 side = sides[k]; + ivec2 other = texel + side; + bool glass = snow_on_glass(other); + vec4 neighbour = glass ? water_snow_texel(snow_slot, snow_from, other) : vec4(0.0); + // slipping onto the neighbour (off the glass: falling off) and from it + float away = slip * max(state.x - neighbour.x - SNOW_REPOSE, 0.0); + snow -= away; + packed -= away * state.z; + if (false == glass) + continue; + float coming = slip * max(neighbour.x - state.x - SNOW_REPOSE, 0.0); + // what the motion of the neighbour brings: it goes this way along the axis of the side + vec4 itsmotion = snow_motion(other, neighbour); + vec2 itssnow = snow_out(itsmotion.xy, seconds); + vec2 itswater = snow_out(itsmotion.zw, seconds); + if (side.x != 0) + { + coming += (itsmotion.x * float(side.x) < 0.0) ? neighbour.x * itssnow.x : 0.0; + water += (itsmotion.z * float(side.x) < 0.0) ? neighbour.y * itswater.x : 0.0; + } + else + { + coming += (itsmotion.y * float(side.y) < 0.0) ? neighbour.x * itssnow.y : 0.0; + water += (itsmotion.w * float(side.y) < 0.0) ? neighbour.y * itswater.y : 0.0; + } + snow += coming; + packed += coming * neighbour.z; + } + snow = max(snow, 0.0); + return vec4(snow, max(water, 0.0), (snow > 1e-4) ? clamp(packed / snow, 0.0, 1.0) : 0.0, state.w); +} + +// the snow and the water of the line of texels the blade went over in the step, ending at a texel (the blade goes on from it to +// the texel ahead), and the packing of the snow (summed for its snow) +vec3 snow_line(ivec2 last, int blade, ivec2 way) +{ + vec3 line = vec3(0.0); + for (int k = 0; k < SNOW_WIPE_LINE; ++k) + { + ivec2 texel = last - way * k; + if (false == snow_crossed(texel, blade)) + break; + vec4 state = water_snow_texel(snow_slot, snow_from, texel); + line += vec3(state.x, state.y, state.x * state.z); + } + return line; +} + +void main() +{ + snow_slot = gl_WorkGroupID.y; + uint place = gl_GlobalInvocationID.x; + if ((water_snow_slots[snow_slot] < 0.5) || (place >= uint(WATER_MAP * WATER_MAP))) + return; + snow_from = (water_snow.w > 0.5) ? 1u : 0u; + uint to = 1u - snow_from; + snow_axes_base = snow_slot * uint(WATER_MAP * WATER_MAP); + if (place == 0u) + water_header[snow_slot].zw = vec2(1.0, float(to)); + ivec2 texel = ivec2(int(place % uint(WATER_MAP)), int(place / uint(WATER_MAP))); + uint index = water_snow_base(snow_slot, to) + 2u * place; + if (false == snow_on_glass(texel)) + { + water_cells[index] = 0u; + water_cells[index + 1u] = 0u; + return; + } + float seconds = water_step.x; + vec4 before = water_snow_texel(snow_slot, snow_from, texel); + vec4 state = snow_move(texel, before, seconds); + + // the blades: taken where they went over, pushed on in front of them + bool crossed = false; + vec3 pushed = vec3(0.0); + for (int blade = 0; blade < 4; ++blade) + { + if (snow_crossed(texel, blade)) + { + crossed = true; + continue; + } + for (int k = 0; k < 8; ++k) + { + ivec2 from = texel + SNOW_AROUND[k]; + if (false == snow_crossed(from, blade)) + continue; + ivec2 way = snow_blade_way(from, blade); + if ((way != ivec2(0)) && (from + way == texel)) + pushed += snow_line(from, blade, way); + } + } + if (crossed) + { + float taken = state.x * (1.0 - SNOW_WIPE_MISS); + state.x -= taken; + state.y = state.y * SNOW_WIPE_WATER + taken * mix(SNOW_FRESH, SNOW_PACKED, state.z) * SNOW_WIPE_TRACE; + } + if (pushed.x + pushed.y > 0.0) + { + float snow = state.x + pushed.x; + // pressed by the blade + state.z = (snow > 1e-4) ? clamp((state.z * state.x + max(pushed.z, pushed.x * 0.85)) / snow, 0.0, 1.0) : state.z; + state.x = snow; + state.y += pushed.y * 0.7; + } + + // the flakes landing + water_frame_s frame = snow_frame(texel); + float area = length(cross(frame.tu, frame.tv)) / float(WATER_MAP * WATER_MAP); // m2 + vec3 coming = water_air.xyz - water_up.xyz * water_air.w; // the snow against the cab, m/s + float facing = max(-dot(coming, frame.normal), 0.0) + SNOW_EDDIES; + float speed = water_snow.z; + float density = mix(SNOW_FRESH, SNOW_PACKED, state.z); + float wet = state.y / (state.y + state.x * density + 1e-4); + // the dry flakes bounce off a fast glass, the ones landing on wet snow or water stay + float sticking = 1.0 - 0.6 * smoothstep(15.0, 35.0, speed) * (1.0 - smoothstep(0.1, 0.4, wet)); + float landings = water_snow.x / max(water_air.w, 0.1) * facing * area * seconds * sticking; + vec4 random = water_random(place, snow_slot, water_counts.x, 0x5e0du); + float count = min(floor(landings) + ((random.x < fract(landings)) ? 1.0 : 0.0), 4.0); + if (count > 0.0) + { + float water = count * mix(SNOW_FLAKE_LEAST, SNOW_FLAKE_MOST, random.y * random.y); // mm3 + float added = water / SNOW_FRESH / max(area * 1e6, 1e-3); // mm of fresh snow over the texel + float snow = state.x + added; + state.z = state.z * state.x / snow; + // a flake on the bare glass has a shape of its own + state.w = (state.x < 0.3) ? random.z : state.w; + state.x = snow; + } + + // the melting at the warm glass and the drying + float glass = water_up.w + water_snow.y / (1.0 + speed / 12.0); + float melted = min(SNOW_MELT * max(glass, 0.0) * seconds, state.x * density); + state.x -= melted / max(density, 1e-3); + state.y = max(state.y + melted - SNOW_DRY * seconds, 0.0); + + state.x = clamp(state.x, 0.0, SNOW_MOST); + state.y = min(state.y, 4.0); + if (state.x < 0.002) + { + state.x = 0.0; + state.z = 0.0; + } + water_cells[index] = packHalf2x16(state.xy); + water_cells[index + 1u] = packHalf2x16(state.zw); +}