mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 23:14:59 +01:00
Clouds and fog tweaks
This commit is contained in:
@@ -4,7 +4,12 @@
|
||||
// (sky_weather.frag). mode 0: the view of the main camera at a part of the resolution, smoothed over frames by
|
||||
// clouds_temporal.comp; mode 1: a panorama around the camera (azimuth across, elevation up), a band of rows per frame, for the
|
||||
// environment map and the other views. rgb: the light of the clouds with the fog in front of them, a: what they let through
|
||||
// of the sky behind; the depth image gets the distance of the clouds, for the reprojection
|
||||
// of the sky behind; the depth image gets the distance of the clouds, for the reprojection.
|
||||
// mode 0, gfx.weather.occlusion: the pixels the scenery covered in the last frame, with everything around them, in front of all the
|
||||
// clouds of their ray aren't marched (the depth pyramid of the culling of that frame says so); their alpha is -1, which tells
|
||||
// clouds_temporal.comp to keep what the history holds for them.
|
||||
// mode 0, gfx.clouds.interleave: one pixel of every block of stride x stride is marched a frame, the one at phase, each in its
|
||||
// turn; the others keep what they were given in their own turn, and clouds_temporal.comp makes the image of the frame of them
|
||||
|
||||
#define LAYOUT_COMPUTE 1
|
||||
#include "common.glsl"
|
||||
@@ -14,20 +19,30 @@ layout(local_size_x = WEATHER_GROUP_SIZE, local_size_y = WEATHER_GROUP_SIZE, loc
|
||||
|
||||
layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image2D clouds_color;
|
||||
layout(r32f, set = COMPUTE_SET, binding = 1) uniform writeonly image2D clouds_depth;
|
||||
// the depth pyramid of the main view of the last frame (hiz_build.comp: the farthest depth of what a texel covers, reversed, 0 the
|
||||
// sky); read only with occlusion set
|
||||
layout(set = COMPUTE_SET, binding = 2) uniform sampler2D occlusion_depth;
|
||||
|
||||
// the parameters of the kernel (push constants)
|
||||
layout(push_constant) uniform kernel_constants
|
||||
{
|
||||
int mode;
|
||||
// mode 0: the level of the depth pyramid looked at for what the scenery hides, plus 1; 0: nothing is taken for hidden
|
||||
int occlusion;
|
||||
ivec2 size;
|
||||
// mode 0: clip space to view space of the camera, and view space to world axes
|
||||
mat4 inverse_projection;
|
||||
mat4 view_to_world;
|
||||
int frame;
|
||||
// mode 0: the pixels marched in this frame are the ones at phase in the blocks of stride x stride pixels (1: all of them)
|
||||
int stride;
|
||||
ivec2 phase;
|
||||
// x: samples through a layer at most, y: samples towards the sun, z: farthest distance (m), w: 1 with the detail noise
|
||||
vec4 quality;
|
||||
// mode 1: first row of the band and the rows in it
|
||||
ivec2 rows;
|
||||
// world axes to the clip space of the frame the depth pyramid was made in
|
||||
mat4 occlusion_projection_view;
|
||||
};
|
||||
|
||||
// lowest elevation of the panorama (radians); its rows go with the square of the height, denser at the horizon
|
||||
@@ -60,6 +75,9 @@ const int CLOUD_SCATTER_OCTAVES = 6;
|
||||
// light falls off fastest (about a 30th of the reach at 4 steps) and the steps get close to even when there are many of them
|
||||
const float CLOUD_LIGHT_GROWTH = 24.0;
|
||||
|
||||
// the phase of the view for each order of the scattering (cloud_sunlight): the same for every sample of a ray, worked out once
|
||||
float cloud_scatter_phase[CLOUD_SCATTER_OCTAVES];
|
||||
|
||||
float henyey_greenstein(float cosangle, float g)
|
||||
{
|
||||
// times 4 pi: 1 for an even scattering
|
||||
@@ -82,8 +100,12 @@ vec3 cloud_sky_light(float up)
|
||||
}
|
||||
|
||||
// light of the sun reaching a point inside a layer through the cloud towards it, with the light scattered many times
|
||||
// (octaves after Wrenninge), and the phase of the view
|
||||
float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float powder, bool detail)
|
||||
// (octaves after Wrenninge), and the phase of the view (cloud_scatter_phase). Offset: where in its step each sample is taken
|
||||
// (0-1), another place for every sample of the view and every frame. taken always in the middle, a far sample (the last step is
|
||||
// some 40% of the reach) came into the cloud at the same place for all the pixels around, and the shaded sides of the clouds
|
||||
// were cut into bands of grey along it; moved about, the bands turn into a noise the samples of the ray and the blending over
|
||||
// frames (clouds_temporal.comp) smooth out
|
||||
float cloud_sunlight(int index, vec3 position, int steps, float powder, bool detail, float offset)
|
||||
{
|
||||
vec3 sun = weather_sun_direction.xyz;
|
||||
cloud_layer_s layer = cloud_layers[index];
|
||||
@@ -97,7 +119,7 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
|
||||
float step = reach * (growth - 1.0) / (pow(growth, float(steps)) - 1.0);
|
||||
for (int s = 0; s < steps; s++)
|
||||
{
|
||||
vec3 point = position + sun * (travelled + 0.5 * step);
|
||||
vec3 point = position + sun * (travelled + offset * step);
|
||||
travelled += step;
|
||||
// the nearest steps with the detail noise: the small billows shade one another, lit on top and grey beneath
|
||||
depth += cloud_layer_density(index, point, cloud_height(point), detail && (s < CLOUD_LIGHT_DETAIL_STEPS)) * step;
|
||||
@@ -106,31 +128,57 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
|
||||
float light = 0.0;
|
||||
float amount = 1.0;
|
||||
float fade = 1.0;
|
||||
float spread = 1.0;
|
||||
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
|
||||
{
|
||||
light += amount * exp(-depth * fade) * cloud_phase(cosangle, spread) * (octave == 0 ? powder : 1.0);
|
||||
light += amount * exp(-depth * fade) * cloud_scatter_phase[octave] * (octave == 0 ? powder : 1.0);
|
||||
amount *= 0.5;
|
||||
fade *= 0.5;
|
||||
spread *= 0.5;
|
||||
}
|
||||
return light;
|
||||
}
|
||||
|
||||
// optical depth of the cloud over a point up to Reach above it, from Samples of the coarse density: under a lump and deep in a
|
||||
// thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud
|
||||
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples)
|
||||
// thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud.
|
||||
// Offset: where in its part of the reach each sample is taken (0-1), as in cloud_sunlight()
|
||||
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples, float offset)
|
||||
{
|
||||
float depth = 0.0;
|
||||
for (int s = 0; s < samples; s++)
|
||||
{
|
||||
// up along the vertical: the height grows by the same distance (cloud_height bends only with the horizontal distance)
|
||||
float up = reach * (float(s) + 0.5) / float(samples);
|
||||
float up = reach * (float(s) + offset) / float(samples);
|
||||
depth += cloud_layer_density(index, position + vec3(0.0, up, 0.0), height + up, false);
|
||||
}
|
||||
return depth * reach / float(samples);
|
||||
}
|
||||
|
||||
// how far along a ray of the view (world axes) the scenery reached at the farthest in the last frame, in the four texels of the
|
||||
// depth pyramid around the ray: some 16 to 48 pixels each way at the level of 32 pixels, so what an edge of the scenery uncovers
|
||||
// as the camera moves is marched before it shows. 0: the sky showed there, the ray wasn't in that view, or there's no pyramid.
|
||||
// the depth is that of a projection with the far plane at infinity (near / w); longer by a margin for the camera moved since and
|
||||
// the angle of the ray to the axis of the view changing over the texels
|
||||
float scenery_reach(vec3 direction)
|
||||
{
|
||||
if (occlusion <= 0)
|
||||
return 0.0;
|
||||
vec4 clip = occlusion_projection_view * vec4(direction, 0.0);
|
||||
if (clip.w <= 1e-3)
|
||||
return 0.0;
|
||||
vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
|
||||
if (any(lessThan(coords, vec2(0.0))) || any(greaterThan(coords, vec2(1.0))))
|
||||
return 0.0;
|
||||
int level = occlusion - 1;
|
||||
ivec2 levelsize = textureSize(occlusion_depth, level);
|
||||
ivec2 base = ivec2(floor(coords * vec2(levelsize) - 0.5));
|
||||
float farthest = 1.0;
|
||||
for (int y = 0; y < 2; y++)
|
||||
for (int x = 0; x < 2; x++)
|
||||
farthest = min(farthest, texelFetch(occlusion_depth, clamp(base + ivec2(x, y), ivec2(0), levelsize - 1), level).r);
|
||||
if (farthest <= 0.0)
|
||||
return 0.0;
|
||||
return projection[3][2] / farthest / clip.w * 1.1 + 2.0;
|
||||
}
|
||||
|
||||
float interleaved_noise(vec2 pixel)
|
||||
{
|
||||
return fract(52.9829189 * fract(dot(pixel, vec2(0.06711056, 0.00583715))));
|
||||
@@ -142,6 +190,7 @@ void main()
|
||||
vec3 direction;
|
||||
if (mode == 0)
|
||||
{
|
||||
texel = texel * max(stride, 1) + phase;
|
||||
if (any(greaterThanEqual(texel, size)))
|
||||
return;
|
||||
vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0;
|
||||
@@ -169,12 +218,25 @@ void main()
|
||||
int skysteps = (lightsteps >= 4) ? 2 : 1;
|
||||
bool detail = quality.w > 0.5;
|
||||
float jitter = fract(interleaved_noise(vec2(texel)) + float(frame) * 0.618034);
|
||||
// where the samples of the light are taken in their steps: another sequence than the one of the samples of the view
|
||||
float lightjitter = fract(interleaved_noise(vec2(texel) + vec2(37.0, 17.0)) + float(frame) * 0.754878);
|
||||
{
|
||||
float spread = 1.0;
|
||||
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
|
||||
{
|
||||
cloud_scatter_phase[octave] = cloud_phase(cosangle, spread);
|
||||
spread *= 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
vec3 scattered = vec3(0.0);
|
||||
float transmittance = 1.0;
|
||||
float depthsum = 0.0;
|
||||
float weightsum = 0.0;
|
||||
int layers = int(weather_camera.w);
|
||||
// what the scenery hid of the ray in the last frame: the clouds past it aren't seen
|
||||
float scenery = (mode == 0) ? scenery_reach(direction) : 0.0;
|
||||
bool hidden = scenery > 0.0;
|
||||
|
||||
// the stretches of the volumetric layers along the ray, nearest first
|
||||
vec2 segments[MAX_CLOUD_LAYERS];
|
||||
@@ -214,6 +276,10 @@ void main()
|
||||
int index = order[k];
|
||||
cloud_layer_s layer = cloud_layers[index];
|
||||
vec2 segment = segments[k];
|
||||
// the scenery in front of the layer, and of the ones past it
|
||||
if (scenery > 0.0 && scenery < segment.x)
|
||||
break;
|
||||
hidden = false;
|
||||
float span = segment.y - segment.x;
|
||||
float thickness = layer.heights.y - layer.heights.x;
|
||||
int steps = clamp(int(float(maxsteps) * span / (thickness * 4.0)), 8, maxsteps);
|
||||
@@ -227,13 +293,15 @@ void main()
|
||||
if (density <= 0.0)
|
||||
continue;
|
||||
float powder = mix(1.0, 1.0 - exp(-density / max(layer.shape.y, 1e-6) * CLOUD_POWDER), powderview);
|
||||
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, cosangle, lightsteps, powder, detail) : 0.0;
|
||||
// every sample of the view takes the samples of its light at another place of their steps
|
||||
float lightoffset = fract(lightjitter + float(s) * 0.754878);
|
||||
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, lightsteps, powder, detail, lightoffset) : 0.0;
|
||||
// the light of the sky reaches inside through the cloud above, that of the ground through the cloud below: the base of a
|
||||
// thick cloud is darker than its top, and greyer, lit only by the ground and the sky at its sides
|
||||
float column = layer.shape.y * layer.shape.x;
|
||||
float top = cloud_layer_bounds(layer).y;
|
||||
float reach = clamp(top - height, 0.0, CLOUD_SKY_REACH);
|
||||
float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0));
|
||||
float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps, fract(lightoffset + 0.569840)) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0));
|
||||
float below = column * max(height - layer.heights.x, 0.0);
|
||||
vec3 light = sunlight * sunshare + (ambienttop * mix(CLOUD_SKY_FLOOR, 1.0, sky) + ambientbottom * exp(-CLOUD_AMBIENT_FADE * below));
|
||||
float through = exp(-density * step);
|
||||
@@ -260,6 +328,9 @@ void main()
|
||||
float t = crossing.x;
|
||||
if (t > maxdistance * 2.0)
|
||||
continue;
|
||||
if (scenery > 0.0 && scenery < t)
|
||||
continue;
|
||||
hidden = false;
|
||||
vec3 position = direction * t;
|
||||
// the slant of the ray through the sheet where it crosses it, the earth curving away
|
||||
float slope = max(abs(direction.y - (1.0 - direction.y * direction.y) * t / CLOUD_EARTH_RADIUS), 0.03);
|
||||
@@ -289,6 +360,11 @@ void main()
|
||||
scattered = vec3(0.0);
|
||||
transmittance = 1.0;
|
||||
}
|
||||
if (hidden)
|
||||
{
|
||||
scattered = vec3(0.0);
|
||||
transmittance = -1.0;
|
||||
}
|
||||
imageStore(clouds_color, texel, vec4(scattered, transmittance));
|
||||
if (mode == 0)
|
||||
imageStore(clouds_depth, texel, vec4(span_distance));
|
||||
|
||||
@@ -3,7 +3,14 @@
|
||||
// the clouds of the main view smoothed over frames: the image of this frame (clouds_march.comp, ray started at another
|
||||
// distance every frame) blended into the one of the previous frames, found where the clouds were by the distance of the
|
||||
// clouds, the rotation and the move of the camera. the history is held to the range of the neighbourhood of this frame,
|
||||
// so moving clouds and a turning camera don't leave trails
|
||||
// so moving clouds and a turning camera don't leave trails. the pixels the scenery hid (alpha -1 from clouds_march.comp, which
|
||||
// didn't march them) keep what the history holds where they were, as it is: when the scenery uncovers them there's something to
|
||||
// blend into.
|
||||
// with a part of the pixels marched a frame (stride over 1: the ones at phase in the blocks of stride x stride pixels, the others
|
||||
// hold what they were given in their turn, some frames ago and for the view of that frame) the pixels of this frame are blended
|
||||
// in as they are; the others take the pixels of this frame next to them, evened out, with a smaller share, so every pixel gets
|
||||
// something of every frame and the turns of the pixels don't show as a pattern. the history is held to the range of the pixels
|
||||
// of this frame around
|
||||
|
||||
#define LAYOUT_COMPUTE 1
|
||||
#include "common.glsl"
|
||||
@@ -27,6 +34,9 @@ layout(push_constant) uniform kernel_constants
|
||||
vec3 previous_camera;
|
||||
// share of this frame in the result; 1 when there is no history
|
||||
float blend;
|
||||
// the pixels marched in this frame: the ones at phase in the blocks of stride x stride pixels (1: all of them)
|
||||
int stride;
|
||||
ivec2 phase;
|
||||
};
|
||||
|
||||
void main()
|
||||
@@ -35,34 +45,80 @@ void main()
|
||||
if (any(greaterThanEqual(texel, size)))
|
||||
return;
|
||||
vec4 current = texelFetch(current_color, texel, 0);
|
||||
bool hidden = current.a < 0.0;
|
||||
// no clouds, where there's nothing to keep
|
||||
if (hidden)
|
||||
current = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
if (blend >= 1.0)
|
||||
{
|
||||
imageStore(result, texel, current);
|
||||
return;
|
||||
}
|
||||
// the range of the neighbourhood
|
||||
vec4 low = current;
|
||||
vec4 high = current;
|
||||
for (int y = -1; y <= 1; y++)
|
||||
for (int x = -1; x <= 1; x++)
|
||||
{
|
||||
vec4 neighbour = texelFetch(current_color, clamp(texel + ivec2(x, y), ivec2(0), size - 1), 0);
|
||||
low = min(low, neighbour);
|
||||
high = max(high, neighbour);
|
||||
}
|
||||
// the pixels of this frame around: the one the pixel is, or the one before it, and those a stride away on both sides (a
|
||||
// pixel between two of them reaches as far both ways). their range holds the history, and the nearest stands for a pixel of
|
||||
// another frame which has no history
|
||||
// the share of the pixels of this frame next to a pixel of another frame, of the share of a pixel of this frame
|
||||
const float BESIDE_SHARE = 0.35;
|
||||
int spacing = max(stride, 1);
|
||||
ivec2 past = (texel + spacing - phase) % spacing;
|
||||
bool fresh = all(equal(past, ivec2(0)));
|
||||
ivec2 base = texel - past;
|
||||
vec4 low = vec4(1e30);
|
||||
vec4 high = vec4(-1e30);
|
||||
vec4 nearest = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
float nearestdistance = 1e30;
|
||||
bool ranged = false;
|
||||
// the pixels of this frame on both sides of a pixel of another frame, which is half way between them
|
||||
vec4 beside = vec4(0.0);
|
||||
float besidecount = 0.0;
|
||||
if (!hidden)
|
||||
{
|
||||
for (int y = -1; y <= (past.y == 0 ? 1 : 2); y++)
|
||||
for (int x = -1; x <= (past.x == 0 ? 1 : 2); x++)
|
||||
{
|
||||
ivec2 place = base + ivec2(x, y) * spacing;
|
||||
if (any(lessThan(place, ivec2(0))) || any(greaterThanEqual(place, size)))
|
||||
continue;
|
||||
vec4 neighbour = texelFetch(current_color, place, 0);
|
||||
if (neighbour.a < 0.0)
|
||||
continue;
|
||||
low = min(low, neighbour);
|
||||
high = max(high, neighbour);
|
||||
ranged = true;
|
||||
vec2 away = vec2(place - texel);
|
||||
float distance2 = dot(away, away);
|
||||
if (distance2 < nearestdistance)
|
||||
{
|
||||
nearestdistance = distance2;
|
||||
nearest = neighbour;
|
||||
}
|
||||
if (x >= 0 && y >= 0 && x <= min(past.x, 1) && y <= min(past.y, 1))
|
||||
{
|
||||
beside += neighbour;
|
||||
besidecount += 1.0;
|
||||
}
|
||||
}
|
||||
// what a pixel of another frame holds is of the view of that frame
|
||||
if (!fresh)
|
||||
current = (besidecount > 0.0) ? beside / besidecount : nearest;
|
||||
}
|
||||
vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0;
|
||||
vec4 near = inverse_projection * vec4(ndc, 1.0, 1.0);
|
||||
vec3 direction = normalize(mat3(view_to_world) * normalize(near.xyz / near.w));
|
||||
float span_distance = texelFetch(current_depth, texel, 0).r;
|
||||
vec3 position = direction * span_distance - previous_camera;
|
||||
vec4 clip = previous_projection_view * vec4(position, 1.0);
|
||||
float share = blend;
|
||||
float share = hidden ? 0.0 : blend * (fresh ? 1.0 : BESIDE_SHARE);
|
||||
vec4 history = current;
|
||||
if (clip.w > 0.0)
|
||||
{
|
||||
vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
|
||||
if (all(greaterThanEqual(coords, vec2(0.0))) && all(lessThanEqual(coords, vec2(1.0))))
|
||||
history = clamp(textureLod(history_color, coords, 0.0), low, high);
|
||||
{
|
||||
history = textureLod(history_color, coords, 0.0);
|
||||
if (ranged)
|
||||
history = clamp(history, low, high);
|
||||
}
|
||||
else
|
||||
share = 1.0;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,10 @@
|
||||
// the points from a place of its own, so the volume doesn't flicker as a whole, and the blending smooths what a cell holds over
|
||||
// its whole size: a beam doesn't swim over the grid of the cells as the camera moves and turns. what the cell takes the lights
|
||||
// from doesn't depend on the point (the clusters of the middle of the cell)
|
||||
// rgb: light scattered towards the camera per metre, a: extinction per metre
|
||||
// rgb: light scattered towards the camera per metre, a: extinction per metre.
|
||||
// gfx.weather.occlusion: the cells behind everything the scenery held around them in the last frame (the depth pyramid of the
|
||||
// culling of that frame) aren't seen through, nothing reads them: they take what the history holds where they were as it is,
|
||||
// without working out the fog anew, so there's something to blend into when the scenery uncovers them
|
||||
|
||||
#define LAYOUT_COMPUTE 1
|
||||
#include "common.glsl"
|
||||
@@ -19,6 +22,9 @@ layout(local_size_x = WEATHER_GROUP_SIZE, local_size_y = WEATHER_GROUP_SIZE, loc
|
||||
|
||||
layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image3D fog_scattering;
|
||||
layout(set = COMPUTE_SET, binding = 1) uniform sampler3D fog_history;
|
||||
// the depth pyramid of the main view of the last frame (hiz_build.comp: the farthest depth of what a texel covers, reversed, 0 the
|
||||
// sky); read only with occlusion.x set
|
||||
layout(set = COMPUTE_SET, binding = 3) uniform sampler2D occlusion_depth;
|
||||
|
||||
layout(set = PASS_SET, binding = SHADOW_TEX) uniform sampler2DArrayShadow shadowmap;
|
||||
layout(set = PASS_SET, binding = HEADLIGHT_TEX) uniform sampler2D headlightmap;
|
||||
@@ -45,6 +51,11 @@ layout(std140, set = COMPUTE_SET, binding = 2) uniform kernel_block
|
||||
vec2 previous_slicing;
|
||||
// where in its cell the cell is sampled this frame across the view (0-1), as slicing.w is along the depth
|
||||
vec2 jitter;
|
||||
// world axes to the clip space of the frame the depth pyramid was made in
|
||||
mat4 occlusion_projection_view;
|
||||
// x: the level of the depth pyramid looked at for what the scenery hides, plus 1 (0: nothing is taken for hidden), y: the near
|
||||
// plane of the projection the depth is of (a depth d is a surface y / d away along the axis of the view)
|
||||
vec4 occlusion;
|
||||
};
|
||||
|
||||
// ray of the view space through a point of the view (0-1), one metre of the depth long
|
||||
@@ -225,11 +236,67 @@ vec3 lamp_segment(light_s lamp, light_s cone, vec3 start, vec3 direction, float
|
||||
return sum * (0.5 * (last - first) / (reach * max(span, 1e-4)));
|
||||
}
|
||||
|
||||
// slices past the surfaces of the scenery which are still worked out: the fog of a surface is read between two slices, each
|
||||
// holding the cells up to it (fog_integrate.comp, fog_volume.glsl), with one to spare
|
||||
const float FOG_HIDDEN_SLICES = 2.0;
|
||||
|
||||
// whether all the scenery around the column of a cell was nearer than the cell in the last frame, by the nine texels of the depth
|
||||
// pyramid around the column. their level is chosen for the size of the cells (vulkan14_renderer::render_weather_view()): the fog
|
||||
// of a pixel is read from the columns two cells around it, each smoothed with the ones next to it, so a column counts for the
|
||||
// pixels three and a half cells away, and the texels reach at least that far. the surfaces are taken for farther by a margin, for
|
||||
// the camera moved and turned since
|
||||
bool cell_hidden(ivec3 cell)
|
||||
{
|
||||
if (occlusion.x < 0.5)
|
||||
return false;
|
||||
vec3 direction = mat3(view_to_world) * view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy));
|
||||
vec4 clip = occlusion_projection_view * vec4(direction, 0.0);
|
||||
if (clip.w <= 1e-3)
|
||||
return false;
|
||||
vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
|
||||
if (any(lessThan(coords, vec2(0.0))) || any(greaterThan(coords, vec2(1.0))))
|
||||
return false;
|
||||
int level = int(occlusion.x) - 1;
|
||||
ivec2 levelsize = textureSize(occlusion_depth, level);
|
||||
ivec2 middle = ivec2(coords * vec2(levelsize));
|
||||
float farthest = 1.0;
|
||||
for (int y = -1; y <= 1; y++)
|
||||
for (int x = -1; x <= 1; x++)
|
||||
farthest = min(farthest, texelFetch(occlusion_depth, clamp(middle + ivec2(x, y), ivec2(0), levelsize - 1), level).r);
|
||||
if (farthest <= 0.0)
|
||||
return false;
|
||||
return slice_depth(float(cell.z) - FOG_HIDDEN_SLICES) > occlusion.y / farthest * 1.1 + 1.0;
|
||||
}
|
||||
|
||||
// the cell in the fog of the previous frame, where its middle was then (0-1 on each axis); false: it wasn't in that view
|
||||
bool cell_history(ivec3 cell, out vec3 previous)
|
||||
{
|
||||
vec3 centre = mat3(view_to_world) * (view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy)) * slice_depth(float(cell.z) + 0.5));
|
||||
vec4 clip = previous_projection_view * vec4(centre - previous_camera, 1.0);
|
||||
previous = vec3(0.0);
|
||||
if (clip.w <= 0.0)
|
||||
return false;
|
||||
float previousslice = log(max((clip.w + FOG_VOLUME_BIAS) / (previous_slicing.x + FOG_VOLUME_BIAS), 1e-6)) * previous_slicing.y;
|
||||
previous = vec3(clip.xy / clip.w * 0.5 + 0.5, previousslice / float(grid.z));
|
||||
return all(greaterThanEqual(previous, vec3(0.0))) && all(lessThanEqual(previous, vec3(1.0)));
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec3 cell = ivec3(gl_GlobalInvocationID);
|
||||
if (any(greaterThanEqual(cell, grid)))
|
||||
return;
|
||||
// the previous frame, where the middle of the cell was then: the cells of the history hold what the cells were on the whole,
|
||||
// not at the point they were sampled at
|
||||
float share = options.x;
|
||||
bool hidden = cell_hidden(cell);
|
||||
vec3 previous = vec3(0.0);
|
||||
bool found = ((share < 1.0) || hidden) && cell_history(cell, previous);
|
||||
if (hidden && found)
|
||||
{
|
||||
imageStore(fog_scattering, cell, textureLod(fog_history, previous, 0.0));
|
||||
return;
|
||||
}
|
||||
vec3 offset = cell_offset(cell);
|
||||
vec2 coords = (vec2(cell.xy) + offset.xy) / vec2(grid.xy);
|
||||
float depth = slice_depth(float(cell.z) + offset.z);
|
||||
@@ -306,21 +373,8 @@ void main()
|
||||
result = vec4(light * fog_haze.w * extinction, extinction);
|
||||
}
|
||||
|
||||
// the previous frame, where the middle of the cell was then: the cells of the history hold what the cells were on the whole,
|
||||
// not at the point they were sampled at
|
||||
float share = options.x;
|
||||
if (share < 1.0)
|
||||
{
|
||||
vec3 centre = mat3(view_to_world) * (view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy)) * slice_depth(float(cell.z) + 0.5));
|
||||
vec4 clip = previous_projection_view * vec4(centre - previous_camera, 1.0);
|
||||
if (clip.w > 0.0)
|
||||
{
|
||||
float previousslice = log(max((clip.w + FOG_VOLUME_BIAS) / (previous_slicing.x + FOG_VOLUME_BIAS), 1e-6)) * previous_slicing.y;
|
||||
vec3 previous = vec3(clip.xy / clip.w * 0.5 + 0.5, previousslice / float(grid.z));
|
||||
if (all(greaterThanEqual(previous, vec3(0.0))) && all(lessThanEqual(previous, vec3(1.0))))
|
||||
result = mix(textureLod(fog_history, previous, 0.0), result, share);
|
||||
}
|
||||
}
|
||||
if ((share < 1.0) && found)
|
||||
result = mix(textureLod(fog_history, previous, 0.0), result, share);
|
||||
if (any(isnan(result)))
|
||||
result = vec4(0.0);
|
||||
imageStore(fog_scattering, cell, result);
|
||||
|
||||
Reference in New Issue
Block a user