mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-02 22:06:21 +01:00
Steady the volumetric fog and add a switch for the old headlight beams
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
// depth), how dense the fog is and how much light it scatters towards the camera. the density: the haze, the patches of the
|
||||
// ground fog, the low clouds; the light: the colour the fog has towards the horizon (weather_common.glsl), less what the
|
||||
// shadows of the sun and of the clouds hold back, and the lamps and headlights of the light clusters. blended with the
|
||||
// previous frame, found where the cell was then; the cells are sampled at another depth every frame.
|
||||
// previous frame, found where the cell was then; the cells are sampled at another point every frame, each cell going through
|
||||
// the points from a place of its own, so the volume doesn't flicker as a whole and the blending smooths the edges of the cells.
|
||||
// rgb: light scattered towards the camera per metre, a: extinction per metre
|
||||
|
||||
#include <common>
|
||||
@@ -22,7 +23,8 @@ layout(binding = HEADLIGHT_SHADOW_TEX) uniform sampler2DArrayShadow headlight_sh
|
||||
layout(location = 0) uniform ivec3 grid;
|
||||
layout(location = 1) uniform mat4 inverse_projection;
|
||||
layout(location = 2) uniform mat4 view_to_world;
|
||||
// x: near, y: far, z: slices / log(far / near), w: where in its slice the cell is sampled this frame (0-1)
|
||||
// x: near, y: far, z: slices / log((far + FOG_VOLUME_BIAS) / (near + FOG_VOLUME_BIAS)), w: where in its slice the cell is
|
||||
// sampled this frame (0-1)
|
||||
layout(location = 3) uniform vec4 slicing;
|
||||
// world axes to the clip space of the previous frame, the camera of the previous frame at the origin
|
||||
layout(location = 4) uniform mat4 previous_projection_view;
|
||||
@@ -32,8 +34,41 @@ layout(location = 5) uniform vec3 previous_camera;
|
||||
layout(location = 6) uniform vec4 options;
|
||||
// the light of the cab lamp, which lights no fog (the air of the cab is clear), or -1
|
||||
layout(location = 7) uniform int cablamp;
|
||||
// x: near, y: slices / log(far / near) of the previous frame
|
||||
// x: near, y: slices / log((far + FOG_VOLUME_BIAS) / (near + FOG_VOLUME_BIAS)) of the previous frame
|
||||
layout(location = 8) uniform 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
|
||||
layout(location = 9) uniform vec2 jitter;
|
||||
|
||||
// ray of the view space through a point of the view (0-1), one metre of the depth long
|
||||
vec3 view_ray(vec2 coords)
|
||||
{
|
||||
vec4 near = inverse_projection * vec4(coords * 2.0 - 1.0, 1.0, 1.0);
|
||||
vec3 ray = near.xyz / near.w;
|
||||
return ray / -ray.z;
|
||||
}
|
||||
|
||||
// view depth at a place along the slices: slice number and the part of the slice
|
||||
float slice_depth(float slice)
|
||||
{
|
||||
return (slicing.x + FOG_VOLUME_BIAS) * exp(slice / slicing.z) - FOG_VOLUME_BIAS;
|
||||
}
|
||||
|
||||
// where in the cell it is sampled this frame, 0-1 on each axis: the point of the sequence of the frames moved by a value of
|
||||
// the cell's own (pcg3d hash), the middle without the blending
|
||||
vec3 cell_offset(ivec3 cell)
|
||||
{
|
||||
if (options.x >= 1.0)
|
||||
return vec3(0.5);
|
||||
uvec3 v = uvec3(cell) * 1664525U + 1013904223U;
|
||||
v.x += v.y * v.z;
|
||||
v.y += v.z * v.x;
|
||||
v.z += v.x * v.y;
|
||||
v ^= v >> 16U;
|
||||
v.x += v.y * v.z;
|
||||
v.y += v.z * v.x;
|
||||
v.z += v.x * v.y;
|
||||
return fract(vec3(jitter, slicing.w) + vec3(v >> 8U) * (1.0 / 16777216.0));
|
||||
}
|
||||
|
||||
float henyey_greenstein(float cosangle, float g)
|
||||
{
|
||||
@@ -108,12 +143,10 @@ void main()
|
||||
ivec3 cell = ivec3(gl_GlobalInvocationID);
|
||||
if (any(greaterThanEqual(cell, grid)))
|
||||
return;
|
||||
vec2 coords = (vec2(cell.xy) + 0.5) / vec2(grid.xy);
|
||||
vec4 near = inverse_projection * vec4(coords * 2.0 - 1.0, 1.0, 1.0);
|
||||
vec3 ray = near.xyz / near.w;
|
||||
ray /= -ray.z;
|
||||
float depth = slicing.x * exp((float(cell.z) + slicing.w) / slicing.z);
|
||||
vec3 view = ray * depth;
|
||||
vec3 offset = cell_offset(cell);
|
||||
vec2 coords = (vec2(cell.xy) + offset.xy) / vec2(grid.xy);
|
||||
float depth = slice_depth(float(cell.z) + offset.z);
|
||||
vec3 view = view_ray(coords) * depth;
|
||||
vec3 world = mat3(view_to_world) * view;
|
||||
float height = weather_camera.x + world.y;
|
||||
|
||||
@@ -161,14 +194,17 @@ void main()
|
||||
result = vec4(light * fog_haze.w * extinction, extinction);
|
||||
}
|
||||
|
||||
// the previous frame, where the cell was then
|
||||
// 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)
|
||||
{
|
||||
vec4 clip = previous_projection_view * vec4(world - previous_camera, 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)
|
||||
{
|
||||
vec3 previous = vec3(clip.xy / clip.w * 0.5 + 0.5, log(max(clip.w / previous_slicing.x, 1e-6)) * previous_slicing.y / float(grid.z));
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user