Merge branch 'zesterer/shader-improvements' into 'master'

Shader improvements

See merge request veloren/veloren!4290
This commit is contained in:
Joshua Barretto 2024-03-22 18:40:45 +00:00
commit 3c846d4d17
6 changed files with 30 additions and 20 deletions

View File

@ -16,7 +16,7 @@ float billow_noise_2d(vec2 pos) {
} }
// Returns vec4(r, g, b, density) // Returns vec4(r, g, b, density)
vec4 cloud_at(vec3 pos, float dist, out vec3 emission, out float not_underground) { vec4 cloud_at(vec3 pos, float dist, vec3 dir, out vec3 emission, out float not_underground) {
#ifdef EXPERIMENTAL_CURVEDWORLD #ifdef EXPERIMENTAL_CURVEDWORLD
pos.z += pow(distance(pos.xy, focus_pos.xy + focus_off.xy) * 0.05, 2); pos.z += pow(distance(pos.xy, focus_pos.xy + focus_off.xy) * 0.05, 2);
#endif #endif
@ -30,7 +30,7 @@ vec4 cloud_at(vec3 pos, float dist, out vec3 emission, out float not_underground
// bright, because it has to travel through an infinite amount of atmosphere. This doesn't happen in reality // bright, because it has to travel through an infinite amount of atmosphere. This doesn't happen in reality
// because the earth has curvature and so there is an upper bound on the amount of atmosphere that a sunset must // because the earth has curvature and so there is an upper bound on the amount of atmosphere that a sunset must
// travel through. We 'simulate' this by fading out the atmosphere density with distance. // travel through. We 'simulate' this by fading out the atmosphere density with distance.
float flat_earth_hack = 1.0 / (1.0 + dist * 0.0001); float flat_earth_hack = max(0.0, 1.0 - dist * 0.00003 * pow(max(0.0, dir.z), 0.2));
float air = 0.025 * clamp((atmosphere_alt - pos.z) / 20000, 0, 1) * flat_earth_hack; float air = 0.025 * clamp((atmosphere_alt - pos.z) / 20000, 0, 1) * flat_earth_hack;
float alt = alt_at(pos.xy - focus_off.xy); float alt = alt_at(pos.xy - focus_off.xy);
@ -95,8 +95,8 @@ vec4 cloud_at(vec3 pos, float dist, out vec3 emission, out float not_underground
; ;
// Sample twice to allow for self-shadowing // Sample twice to allow for self-shadowing
float cloud_p0 = noise_3d((wind_pos + vec3(0, 0, small_nz) * 250 - sun_dir.xyz * 250) * vec3(0.55, 0.55, 1) / (cloud_scale * 20000.0)); float cloud_p0 = noise_3d((wind_pos + vec3(0, 0, small_nz) * 150 - sun_dir.xyz * 150) * vec3(0.55, 0.55, 1) / (cloud_scale * 20000.0));
float cloud_p1 = noise_3d((wind_pos + vec3(0, 0, small_nz) * 250 + sun_dir.xyz * 250) * vec3(0.55, 0.55, 1) / (cloud_scale * 20000.0)); float cloud_p1 = noise_3d((wind_pos + vec3(0, 0, small_nz) * 150 + sun_dir.xyz * 150) * vec3(0.55, 0.55, 1) / (cloud_scale * 20000.0));
float cloud_factor = pow(max(((cloud_p0 + cloud_p1) * 0.5 float cloud_factor = pow(max(((cloud_p0 + cloud_p1) * 0.5
- 0.5 - 0.5
@ -112,8 +112,8 @@ vec4 cloud_at(vec3 pos, float dist, out vec3 emission, out float not_underground
// Basically, just throw together a few values that roughly approximate this term and come up with an average // Basically, just throw together a few values that roughly approximate this term and come up with an average
cloud_sun_access = clamp( cloud_sun_access = clamp(
0.7 0.7
+ pow(abs(cloud_p1 - cloud_p0), 0.5) * sign(cloud_p1 - cloud_p0) * 0.5 + pow(abs(cloud_p1 - cloud_p0), 0.5) * sign(cloud_p1 - cloud_p0) * 0.75
+ (pos.z - cloud_alt) / CLOUD_DEPTH * 0.4 + (pos.z - cloud_alt) / CLOUD_DEPTH * 0.2
- pow(cloud * 10000000.0, 0.2) * 0.0075 - pow(cloud * 10000000.0, 0.2) * 0.0075
, ,
0.15, 0.15,
@ -244,7 +244,7 @@ vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, float max_dist, con
float not_underground; // Used to prevent sunlight leaking underground float not_underground; // Used to prevent sunlight leaking underground
vec3 pos = origin + dir * ldist * splay; vec3 pos = origin + dir * ldist * splay;
// `sample` is a reserved keyword // `sample` is a reserved keyword
vec4 sample_ = cloud_at(origin + dir * ldist * splay, ldist, emission, not_underground); vec4 sample_ = cloud_at(origin + dir * ldist * splay, ldist, dir, emission, not_underground);
// DEBUG // DEBUG
// if (max_dist > ldist && max_dist < ldist * 1.02) { // if (max_dist > ldist && max_dist < ldist * 1.02) {

View File

@ -64,7 +64,7 @@
// An arbitrary value that represents a very far distance (at least as far as the player should be able to see) without // An arbitrary value that represents a very far distance (at least as far as the player should be able to see) without
// being too far that we end up with precision issues (used in clouds and elsewhere). // being too far that we end up with precision issues (used in clouds and elsewhere).
#define DIST_CAP 50000 #define DIST_CAP 500000
/* Constants expected to be defined automatically by configuration: */ /* Constants expected to be defined automatically by configuration: */

View File

@ -18,7 +18,7 @@ void apply_point_glow_light(Light L, vec3 wpos, vec3 dir, float max_dist, inout
#if (CLOUD_MODE >= CLOUD_MODE_HIGH) #if (CLOUD_MODE >= CLOUD_MODE_HIGH)
vec3 _unused; vec3 _unused;
float unused2; float unused2;
float spread = 1.0 / (1.0 + sqrt(max(cloud_at(nearest, 0.0, _unused, unused2).z, 0.0)) * 0.01); float spread = 1.0 / (1.0 + sqrt(max(cloud_at(nearest, 0.0, dir, _unused, unused2).z, 0.0)) * 0.01);
#else #else
const float spread = 1.0; const float spread = 1.0;
#endif #endif

View File

@ -35,7 +35,15 @@ layout(location = 1) out uvec4 tgt_mat;
#include <light.glsl> #include <light.glsl>
#include <lod.glsl> #include <lod.glsl>
const float FADE_DIST = 32.0; float lod_voxel_noise(vec3 f_pos) {
#ifdef EXPERIMENTAL_PROCEDURALLODDETAIL
vec3 block_pos = floor(f_pos) + 0.5;
//return (hash_three(uvec3((block_pos + focus_off.xyz) * 0.35)) - 0.5) * 5.0;
return floor((noise_3d((block_pos + focus_off.xyz) * 0.015) - 0.5) * 5.0);
#else
return 0.0;
#endif
}
void main() { void main() {
#ifdef EXPERIMENTAL_BAREMINIMUM #ifdef EXPERIMENTAL_BAREMINIMUM
@ -94,20 +102,22 @@ void main() {
vec3 top_norm = vec3(0, 0, 1); vec3 top_norm = vec3(0, 0, 1);
voxel_norm = normalize(mix(side_norm, top_norm, cam_dir.z)); voxel_norm = normalize(mix(side_norm, top_norm, cam_dir.z));
#else #else
float t = -1.5; float base_surf_depth = lod_voxel_noise(f_pos);
while (t < 1.5) { float t = -1.5 + base_surf_depth;
while (t < 1.5 + base_surf_depth) {
vec3 deltas = (step(vec3(0), -cam_dir) - fract(f_pos - cam_dir * t)) / -cam_dir; vec3 deltas = (step(vec3(0), -cam_dir) - fract(f_pos - cam_dir * t)) / -cam_dir;
float m = min(min(deltas.x, deltas.y), deltas.z); float m = min(min(deltas.x, deltas.y), deltas.z);
t += max(m, 0.01); t += max(m, 0.01);
vec3 block_pos = floor(f_pos - cam_dir * t) + 0.5; vec3 block_pos = floor(f_pos - cam_dir * t) + 0.5;
if (dot(block_pos - f_pos, -f_norm) < 0.0) { float surf_depth = lod_voxel_noise(f_pos);
if (dot(block_pos - f_pos - f_norm * surf_depth, -f_norm) < 0.0) {
vec3 to_center = abs(block_pos - (f_pos - cam_dir * t)); vec3 to_center = abs(block_pos - (f_pos - cam_dir * t));
voxel_norm = step(max(max(to_center.x, to_center.y), to_center.z), to_center) * sign(-cam_dir); voxel_norm = step(max(max(to_center.x, to_center.y), to_center.z), to_center) * sign(-cam_dir);
voxel_norm = mix(f_norm, voxel_norm, voxelize_factor); voxel_norm = mix(f_norm, voxel_norm, voxelize_factor);
surf_color *= mix(0.65, 1.0, hash_three(uvec3(block_pos + focus_off.xyz))); surf_color *= mix(0.65, 1.0, hash_three(uvec3(block_pos + focus_off.xyz)));
f_ao = mix(1.0, clamp(1.0 + t, 0.2, 1.0), voxelize_factor); f_ao = mix(1.0, clamp(1.0 + t - surf_depth, 0.1, 1.0), voxelize_factor * max(0.0, -dot(cam_dir, f_norm)));
break; break;
} }
} }

View File

@ -130,13 +130,13 @@ void main() {
voxel_norm = normalize(mix(side_norm, top_norm, max(cam_dir.z, 0.0))); voxel_norm = normalize(mix(side_norm, top_norm, max(cam_dir.z, 0.0)));
#else #else
#ifdef EXPERIMENTAL_PROCEDURALLODDETAIL #ifdef EXPERIMENTAL_PROCEDURALLODDETAIL
float nz_offset = (noise_2d((f_pos.xy + focus_off.xy) * 0.01) - 0.5) * 3.0 / f_norm.z; float nz_offset = floor((noise_2d((floor(f_pos.xy) + focus_off.xy) * 0.01) - 0.5) * 3.0 / max(f_norm.z, 0.01));
#else #else
const float nz_offset = 0.0; const float nz_offset = 0.0;
#endif #endif
float t = -2.0; float t = -2.0 + nz_offset;
while (t < 2.0) { while (t < 2.0 + nz_offset) {
vec3 deltas = (step(vec3(0), -cam_dir) - fract(f_pos - cam_dir * t)) / -cam_dir; vec3 deltas = (step(vec3(0), -cam_dir) - fract(f_pos - cam_dir * t)) / -cam_dir;
float m = min(min(deltas.x, deltas.y), deltas.z); float m = min(min(deltas.x, deltas.y), deltas.z);
@ -147,7 +147,7 @@ void main() {
vec3 to_center = abs(block_pos - (f_pos - cam_dir * t)); vec3 to_center = abs(block_pos - (f_pos - cam_dir * t));
voxel_norm = step(max(max(to_center.x, to_center.y), to_center.z), to_center) * sign(-cam_dir); voxel_norm = step(max(max(to_center.x, to_center.y), to_center.z), to_center) * sign(-cam_dir);
voxel_norm = mix(f_norm, voxel_norm, voxelize_factor); voxel_norm = mix(f_norm, voxel_norm, voxelize_factor);
f_ao = mix(1.0, clamp(1.0 + (t - nz_offset) * 0.5, 0.1, 1.0), voxelize_factor); f_ao = mix(1.0, clamp(1.0 + (t - nz_offset) * 0.5, 0.1, 1.0), voxelize_factor * max(0.0, -dot(cam_dir, f_norm)));
break; break;
} }
} }

View File

@ -119,7 +119,7 @@ impl WeatherSim {
let time_scale = 100_000.0; let time_scale = 100_000.0;
let spos = (pos / space_scale).with_z(time / time_scale); let spos = (pos / space_scale).with_z(time / time_scale);
let avg_scale = 20_000.0; let avg_scale = 30_000.0;
let avg_delay = 250_000.0; let avg_delay = 250_000.0;
let pressure = ((base_nz let pressure = ((base_nz
.get((pos / avg_scale).with_z(time / avg_delay).into_array()) .get((pos / avg_scale).with_z(time / avg_delay).into_array())
@ -135,7 +135,7 @@ impl WeatherSim {
- self.consts[point].humidity * 0.6; - self.consts[point].humidity * 0.6;
const RAIN_CLOUD_THRESHOLD: f32 = 0.25; const RAIN_CLOUD_THRESHOLD: f32 = 0.25;
cell.cloud = (1.0 - pressure).max(0.0) * 0.5; cell.cloud = (1.0 - pressure).max(0.0).powi(2) * 4.0;
cell.rain = ((1.0 - pressure - RAIN_CLOUD_THRESHOLD).max(0.0) cell.rain = ((1.0 - pressure - RAIN_CLOUD_THRESHOLD).max(0.0)
* self.consts[point].humidity * self.consts[point].humidity
* 2.5) * 2.5)