1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00:00

Update volumetrics

This commit is contained in:
Gefüllte Taubenbrust 2024-08-18 15:13:26 +02:00
parent 22ba7449f2
commit e6752008e0
17 changed files with 420 additions and 181 deletions

View file

@ -1,24 +1,49 @@
#define cloudsTexture texture0
#define sceneTexture texture1
#define depthmap texture2
uniform sampler2D cloudsTexture;
uniform sampler2D sceneTexture;
uniform sampler2D depthmap;
uniform vec2 texelSize0;
uniform vec3 dayLight;
uniform vec3 cloudColor;
varying vec2 screenspaceCoordinate;
varying vec3 relativePosition;
varying vec3 viewDirection;
varying vec3 sunTint;
varying float auroraFactor;
uniform vec3 cameraOffset;
uniform vec3 cameraPosition;
uniform float cameraNear;
uniform float cameraFar;
uniform float cloudHeight;
uniform float cloudThickness;
float getDepth(vec2 screenspacePosition) {
float depth = texture2D(depthmap, screenspacePosition * 0.5 + 0.5).r;
return cameraNear * cameraFar / (cameraFar + depth * (cameraNear - cameraFar));
}
vec4 sampleClouds(vec2 uv) {
vec4 cloudsKey = texture2D(cloudsTexture, uv);
const vec3 darkColor = vec3(0.05, 0.1, 0.2);
//const vec3 darkColor = vec3(0.05, 0.1, 0.2);
vec3 darkColor = vec3(0.2, 0.4, 0.8) * dayLight;
const vec3 auroraDark = vec3(0., 0.5, 0.5);
const vec3 auroraBright = vec3(0., 0.5, .0);
//const vec3 auroraDark = vec3(0.);
//const vec3 auroraBright = vec3(0.);
return vec4(
mix(auroraDark, auroraBright, cloudsKey.b) * cloudsKey.b * max(0., 1. - cloudsKey.r) +
cloudsKey.r * (darkColor * max(0., 1. - cloudsKey.g) + dayLight * cloudsKey.g),
mix(auroraDark, auroraBright, cloudsKey.b) * cloudsKey.b * auroraFactor +
cloudsKey.r * cloudColor * (darkColor * max(0., 1. - cloudsKey.g) + dayLight * sunTint * cloudsKey.g),
cloudsKey.r);
}
@ -37,8 +62,22 @@ vec4 getClouds(vec2 uv) {
void main(void)
{
vec4 cloudsColor = getClouds(screenspaceCoordinate * 0.5 + 0.5);
vec3 viewVec = normalize(relativePosition);
vec3 position = cameraOffset + cameraPosition;
float depth = getDepth(screenspaceCoordinate) / normalize(viewDirection).z;
float bottomPlaneIntersect = max((cloudHeight - cameraPosition.y) / viewVec.y, 0.);
float topPlaneIntersect = max((cloudHeight + cloudThickness - cameraPosition.y) / viewVec.y, 0.);
float minPlane = min(bottomPlaneIntersect, topPlaneIntersect);
vec4 sceneColor = texture2D(sceneTexture, screenspaceCoordinate * 0.5 + 0.5);
gl_FragColor = vec4(sceneColor.rgb * (1. - cloudsColor.a) + cloudsColor.rgb, 1.);
if (depth > minPlane) {
vec4 finalColor = getClouds(screenspaceCoordinate * 0.5 + 0.5);
gl_FragColor = vec4(sceneColor.rgb * (1. - finalColor.a) + finalColor.rgb, 1.);
} else {
gl_FragColor = sceneColor;
}
}

View file

@ -1,7 +1,48 @@
uniform mat4 mCameraProjInv;
uniform mat4 mCameraView;
uniform vec3 v_LightDirection;
uniform float f_timeofday;
varying vec3 relativePosition;
varying vec3 viewDirection;
varying vec2 screenspaceCoordinate;
varying vec3 sunTint;
varying float auroraFactor;
vec3 getDirectLightScatteringAtGround(vec3 v_LightDirection)
{
// Based on talk at 2002 Game Developers Conference by Naty Hoffman and Arcot J. Preetham
const float beta_r0 = 1e-5; // Rayleigh scattering beta
// These factors are calculated based on expected value of scattering factor of 1e-5
// for Nitrogen at 532nm (green), 2e25 molecules/m3 in atmosphere
const vec3 beta_r0_l = vec3(3.3362176e-01, 8.75378289198826e-01, 1.95342379700656) * beta_r0; // wavelength-dependent scattering
const float atmosphere_height = 15000.; // height of the atmosphere in meters
// sun/moon light at the ground level, after going through the atmosphere
return exp(-beta_r0_l * atmosphere_height / (1e-5 - dot(v_LightDirection, vec3(0., 1., 0.))));
}
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep
float mtsmoothstep(in float edge0, in float edge1, in float x)
{
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
void main(void)
{
vec4 p = mCameraProjInv * inVertexPosition;
viewDirection = p.xyz / p.w;
relativePosition = (p.xyz / p.w) * mat3(mCameraView);
screenspaceCoordinate = inVertexPosition.xy;
auroraFactor = 1. - mtsmoothstep(0.13, 0.15, f_timeofday) * mtsmoothstep(0.87, 0.85, f_timeofday);
float tintFactor = mtsmoothstep(0.21, 0.24, f_timeofday) * mtsmoothstep(0.793, 0.753, f_timeofday);
sunTint = mix(vec3(1.0), getDirectLightScatteringAtGround(v_LightDirection), tintFactor);
gl_Position = inVertexPosition;
}