mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-22 17:18:39 +00:00
Various changes, fixes and features
This commit is contained in:
parent
b6c099073f
commit
4b88a32c1c
17 changed files with 409 additions and 261 deletions
|
@ -8,7 +8,7 @@ varying mediump vec2 varTexCoord;
|
|||
|
||||
void main (void)
|
||||
{
|
||||
vec2 uv = varTexCoord;
|
||||
vec2 uv = varTexCoord.st;
|
||||
|
||||
//texture sampling rate
|
||||
float step = 1.0 / mapSize;
|
||||
|
@ -20,19 +20,17 @@ void main (void)
|
|||
float b = texture2D(normalTexture, vec2(uv.x, uv.y - step)).r;
|
||||
float bl = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
|
||||
float l = texture2D(normalTexture, vec2(uv.x - step, uv.y )).r;
|
||||
float c = texture2D(normalTexture, vec2(uv.x , uv.y )).r;
|
||||
float AO = 50.0 * (clamp(t - c, -0.001, 0.001) + clamp(b - c, -0.001, 0.001) + clamp(r - c, -0.001, 0.001) + clamp(l - c, -0.001, 0.001));
|
||||
float dX = 4.0 * (l - r);
|
||||
float dY = 4.0 * (t - b);
|
||||
vec3 bump = normalize(vec3 (dX, dY, 0.1));
|
||||
float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
|
||||
float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
|
||||
vec4 bump = vec4 (normalize(vec3 (dX, dY, 0.1)),1.0);
|
||||
float height = 2.0 * texture2D(normalTexture, vec2(uv.x, uv.y)).r - 1.0;
|
||||
vec4 base = texture2D(baseTexture, uv).rgba;
|
||||
vec3 L = normalize(vec3(0.0, 0.0, 1.0));
|
||||
vec3 L = normalize(vec3(0.0, 0.75, 1.0));
|
||||
float specular = pow(clamp(dot(reflect(L, bump.xyz), yawVec), 0.0, 1.0), 1.0);
|
||||
float diffuse = dot(yawVec, bump);
|
||||
float diffuse = dot(yawVec, bump.xyz);
|
||||
|
||||
vec3 color = (1.1 * diffuse + 0.05 * height + 0.5 * specular + AO) * base.rgb;
|
||||
vec4 col = vec4(color, base.a);
|
||||
vec3 color = (1.1 * diffuse + 0.05 * height + 0.5 * specular) * base.rgb;
|
||||
vec4 col = vec4(color.rgb, base.a);
|
||||
col *= varColor;
|
||||
gl_FragColor = vec4(col.rgb, base.a);
|
||||
}
|
||||
}
|
|
@ -40,6 +40,8 @@ uniform float animationTimer;
|
|||
varying float perspective_factor;
|
||||
#endif
|
||||
|
||||
uniform vec2 windowSize;
|
||||
uniform float fov;
|
||||
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vPosition;
|
||||
|
@ -98,6 +100,36 @@ vec3 gnoise(vec3 p){
|
|||
);
|
||||
}
|
||||
|
||||
float snoise(vec3 p)
|
||||
{
|
||||
vec3 a = floor(p);
|
||||
vec3 d = p - a;
|
||||
d = d * d * (3.0 - 2.0 * d);
|
||||
|
||||
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
|
||||
vec4 k1 = perm(b.xyxy);
|
||||
vec4 k2 = perm(k1.xyxy + b.zzww);
|
||||
|
||||
vec4 c = k2 + a.zzzz;
|
||||
vec4 k3 = perm(c);
|
||||
vec4 k4 = perm(c + 1.0);
|
||||
|
||||
vec4 o1 = fract(k3 * (1.0 / 41.0));
|
||||
vec4 o2 = fract(k4 * (1.0 / 41.0));
|
||||
|
||||
vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
|
||||
vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
|
||||
|
||||
return o4.y * d.y + o4.x * (1.0 - d.y);
|
||||
}
|
||||
|
||||
vec3 hnoise(vec3 p) {
|
||||
vec3 g = gnoise(p);
|
||||
float s = snoise(p);
|
||||
g *= 3.0 / (1.0 + exp(-16.0 * (s - 0.5))) - 1.5;
|
||||
return g;
|
||||
}
|
||||
|
||||
vec2 wave_noise(vec3 p, float off) {
|
||||
return (gnoise(p + vec3(0.0, 0.0, off)) * 0.4 + gnoise(2.0 * p + vec3(0.0, off, off)) * 0.2 + gnoise(3.0 * p + vec3(0.0, off, off)) * 0.225 + gnoise(4.0 * p + vec3(-off, off, 0.0)) * 0.2).xz;
|
||||
}
|
||||
|
@ -123,7 +155,7 @@ float mtsmoothstep(in float edge0, in float edge1, in float x)
|
|||
|
||||
float shadowCutoff(float x) {
|
||||
#if defined(ENABLE_TRANSLUCENT_FOLIAGE) && MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES
|
||||
return mtsmoothstep(0.0, 0.002, x);
|
||||
return mtsmoothstep(0.0, 3.0 / f_shadowfar, x);
|
||||
#else
|
||||
return step(0.0, x);
|
||||
#endif
|
||||
|
@ -415,8 +447,26 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if (defined(ENABLE_BUMPMAPS) && !defined(MATERIAL_LIQUID))
|
||||
//This is mostly a placeholder and probably should use proper textures eventually...
|
||||
vec3 getBumpMap(vec2 uv) {
|
||||
vec2 dr = vec2(0.25) * texelSize0;
|
||||
// Sample the texture to then compute the gradient
|
||||
float fx0y0 = texture2D(baseTexture, uv).r;
|
||||
float fx1y0 = texture2D(baseTexture, uv + vec2(dr.x, 0.0)).r;
|
||||
float fx0y1 = texture2D(baseTexture, uv + vec2(0.0, dr.y)).r;
|
||||
vec2 gradient = 0.1 * vec2((fx1y0 - fx0y0) / dr.x, (fx0y1 - fx0y0) / dr.y) + 0.05 * gnoise(vec3(2.0 * uv / texelSize0, 0.0)).xy;
|
||||
// Compute a set of orthonormal basis vectors representing the node's surface plane.
|
||||
vec3 orth1 = normalize(cross(vNormal, mix(vec3(0.0, -1.0, 0.0), vec3(0.0, 0.0, -1.0), step(0.9, abs(vNormal.y)))));
|
||||
vec3 orth2 = normalize(cross(vNormal, orth1));
|
||||
// The normal is computed using the partial derivatives along the texture space x and y axes.
|
||||
// These axes in world space are assumed to be parallel to the basis vectors we defined before.
|
||||
return orth1 * gradient.x + orth2 * gradient.y;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
void main(void)
|
||||
|
@ -444,19 +494,15 @@ void main(void)
|
|||
// Fragment normal, can differ from vNormal which is derived from vertex normals.
|
||||
vec3 fNormal = vNormal;
|
||||
|
||||
vec3 viewVec = normalize(worldPosition + cameraOffset - cameraPosition);
|
||||
|
||||
#if ((defined(ENABLE_DYNAMIC_SHADOWS) && defined(ENABLE_BUMPMAPS)) && !defined(MATERIAL_LIQUID))
|
||||
vec2 dr = vec2(0.25) * texelSize0;
|
||||
// Sample the texture to then compute the gradient
|
||||
float fx0y0 = texture2D(baseTexture, uv).r;
|
||||
float fx1y0 = texture2D(baseTexture, uv + vec2(dr.x, 0.0)).r;
|
||||
float fx0y1 = texture2D(baseTexture, uv + vec2(0.0, dr.y)).r;
|
||||
vec2 gradient = 0.1 * vec2((fx1y0 - fx0y0) / dr.x, (fx0y1 - fx0y0) / dr.y) + 0.05 * gnoise(vec3(2.0 * uv / texelSize0, 0.0)).xy;
|
||||
// Compute a set of orthogonal basis vectors representing the node's surface plane.
|
||||
vec3 orth1 = normalize(cross(vNormal, mix(vec3(0.0, -1.0, 0.0), vec3(0.0, 0.0, -1.0), step(0.9, abs(vNormal.y)))));
|
||||
vec3 orth2 = normalize(cross(vNormal, orth1));
|
||||
// The normal is computed using the partial derivatives along the texture space x and y axes.
|
||||
// These axes in world space are assumed to be parallel to the basis vectors we defined before.
|
||||
fNormal = normalize(vNormal + orth1 * gradient.x + orth2 * gradient.y);
|
||||
vec3 bump_normal = getBumpMap(uv);
|
||||
// When applied to all blocks, these bump maps produce irritating Moiré effects.
|
||||
// So we hide the bump maps when close up.
|
||||
float moire_factor = abs(dot(vNormal, viewVec));
|
||||
bump_normal *= mtsmoothstep(0.4 * moire_factor, 0.2 * moire_factor, length(eyeVec) * fov / windowSize.x);
|
||||
fNormal = normalize(vNormal + bump_normal);
|
||||
float adj_cosLight = max(1e-5, dot(fNormal, -v_LightDirection));
|
||||
#else
|
||||
float adj_cosLight = cosLight;
|
||||
|
@ -526,8 +572,6 @@ void main(void)
|
|||
|
||||
vec3 reflect_ray = -normalize(v_LightDirection - fNormal * dot(v_LightDirection, fNormal) * 2.0);
|
||||
|
||||
vec3 viewVec = normalize(worldPosition + cameraOffset - cameraPosition);
|
||||
|
||||
// Water reflections
|
||||
#if (defined(MATERIAL_WAVING_LIQUID) && defined(ENABLE_WATER_REFLECTIONS))
|
||||
|
||||
|
@ -548,7 +592,7 @@ void main(void)
|
|||
reflect_ray = -normalize(v_LightDirection - fNormal * dot(v_LightDirection, fNormal) * 2.0);
|
||||
float fresnel_factor = dot(fNormal, viewVec);
|
||||
|
||||
float brightness_factor = 1.0 - adjusted_night_ratio;
|
||||
float brightness_factor = (1.0 - adjusted_night_ratio) / base.a;
|
||||
|
||||
// A little trig hack. We go from the dot product of viewVec and normal to the dot product of viewVec and tangent to apply a fresnel effect.
|
||||
fresnel_factor = clamp(pow(1.0 - fresnel_factor * fresnel_factor, 8.0), 0.0, 1.0) * 0.8 + 0.2;
|
||||
|
@ -557,10 +601,12 @@ void main(void)
|
|||
|
||||
// Sky reflection
|
||||
col.rgb += reflection_color * pow(fresnel_factor, 2.0) * 0.5 * brightness_factor;
|
||||
vec3 water_reflect_color = 12.0 * sunTint * dayLight * fresnel_factor * mtsmoothstep(0.85, 0.9, pow(clamp(dot(reflect_ray, viewVec), 0.0, 1.0), 32.0)) * max(1.0 - shadow_uncorrected, 0.0);
|
||||
|
||||
// This line exists to prevent ridiculously bright reflection colors.
|
||||
water_reflect_color /= clamp(max(water_reflect_color.r, max(water_reflect_color.g, water_reflect_color.b)) * 0.375, 1.0, 400.0);
|
||||
// We clip the reflection color if it gets too bright
|
||||
vec3 water_reflect_color = 6.0 * sunTint * dayLight * fresnel_factor * mtsmoothstep(0.85, 0.9, pow(clamp(dot(reflect_ray, viewVec), 0.0, 1.0), 32.0)) * max(1.0 - shadow_uncorrected, 0.0);
|
||||
water_reflect_color /= max(0.4 * length(water_reflect_color), 1.0);
|
||||
|
||||
// Sun reflection
|
||||
col.rgb += water_reflect_color * f_adj_shadow_strength * brightness_factor;
|
||||
#endif
|
||||
|
||||
|
@ -580,7 +626,7 @@ void main(void)
|
|||
|
||||
#if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES) && defined(ENABLE_TRANSLUCENT_FOLIAGE)
|
||||
// Simulate translucent foliage.
|
||||
col.rgb += 4.0 * dayLight * base.rgb * normalize(base.rgb * varColor.rgb * varColor.rgb) * f_adj_shadow_strength * pow(max(-dot(v_LightDirection, viewVec), 0.0), 4.0) * max(1.0 - shadow_uncorrected, 0.0);
|
||||
col.rgb += 4.0 * sunTint * dayLight * base.rgb * normalize(base.rgb * varColor.rgb * varColor.rgb) * f_adj_shadow_strength * pow(max(-dot(v_LightDirection, viewVec), 0.0), 4.0) * max(1.0 - shadow_uncorrected, 0.0);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -54,6 +54,8 @@ uniform float xyPerspectiveBias0;
|
|||
uniform float xyPerspectiveBias1;
|
||||
uniform float zPerspectiveBias;
|
||||
|
||||
uniform vec3 beta_r0_l;
|
||||
|
||||
#ifdef ENABLE_DYNAMIC_SHADOWS
|
||||
|
||||
vec4 getRelativePosition(in vec4 position)
|
||||
|
@ -146,19 +148,17 @@ float snoise(vec3 p)
|
|||
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_TINTED_SUNLIGHT
|
||||
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.))));
|
||||
return exp(-beta_r0_l * beta_r0 * atmosphere_height / (1e-5 - dot(v_LightDirection, vec3(0., 1., 0.))));
|
||||
}
|
||||
#endif
|
||||
|
||||
void main(void)
|
||||
{
|
||||
|
@ -280,7 +280,9 @@ void main(void)
|
|||
adj_shadow_strength = f_shadow_strength *
|
||||
mtsmoothstep(0.21, 0.26, f_timeofday) *
|
||||
(1.0 - mtsmoothstep(0.743, 0.793, f_timeofday));
|
||||
#ifdef ENABLE_TINTED_SUNLIGHT
|
||||
sunTint = mix(vec3(1.0), getDirectLightScatteringAtGround(v_LightDirection), min(1.0, 4.0 * adj_shadow_strength));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -432,7 +432,7 @@ void main(void)
|
|||
// calculate fragment color from components:
|
||||
col.rgb =
|
||||
adjusted_night_ratio * col.rgb + // artificial light
|
||||
(1.0 - adjusted_night_ratio) * ( // natural light
|
||||
sunTint * (1.0 - adjusted_night_ratio) * ( // natural light
|
||||
col.rgb * (1.0 - shadow_int * (1.0 - shadow_color)) + // filtered texture color
|
||||
dayLight * shadow_color * shadow_int); // reflected filtered sunlight/moonlight
|
||||
}
|
||||
|
|
|
@ -12,6 +12,14 @@ struct ExposureParams {
|
|||
float compensationFactor;
|
||||
};
|
||||
|
||||
uniform vec3 cdl_slope;
|
||||
uniform vec3 cdl_offset;
|
||||
uniform vec3 cdl_power;
|
||||
|
||||
uniform float vignette_dark;
|
||||
uniform float vignette_bright;
|
||||
uniform float vignette_power;
|
||||
|
||||
uniform sampler2D rendered;
|
||||
uniform sampler2D bloom;
|
||||
|
||||
|
@ -148,7 +156,7 @@ void main(void)
|
|||
{
|
||||
|
||||
#ifdef ENABLE_VIGNETTE
|
||||
color.rgb *= 0.8 * pow(1.0 - length(uv - vec2(0.5)) * 1.4, 0.9) + 0.3;
|
||||
color.rgb *= (vignette_bright - vignette_dark) * (1.0 - pow(length(uv - vec2(0.5)) * 1.4, vignette_power)) + vignette_dark;
|
||||
#endif
|
||||
|
||||
#if ENABLE_TONE_MAPPING
|
||||
|
@ -157,13 +165,7 @@ void main(void)
|
|||
|
||||
#ifdef ENABLE_COLOR_GRADING
|
||||
// ASC CDL color grading
|
||||
const vec3 slope = vec3(1.2, 1.0, 0.8);
|
||||
const vec3 power = vec3(1.25, 1.0, 0.9);
|
||||
|
||||
// Filter out blue pixels, because the color grading tends to wash them out.
|
||||
float blue_factor = clamp((color.b - max(color.r, color.g)) / max(0.01, min(color.r, color.g)), 0.0, 1.0);
|
||||
|
||||
color.rgb = mix(color.rgb, pow(color.rgb * slope, power), 1.);
|
||||
color.rgb = mix(color.rgb, pow(max(color.rgb * cdl_slope + cdl_offset, 0.0), cdl_power), 1.);
|
||||
#endif
|
||||
color.rgb = applySaturation(color.rgb, saturation);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@ varying mediump vec2 varTexCoord;
|
|||
centroid varying vec2 varTexCoord;
|
||||
#endif
|
||||
|
||||
uniform vec3 beta_r0_l;
|
||||
|
||||
const float far = 1000.;
|
||||
float mapDepth(float depth)
|
||||
{
|
||||
|
@ -46,9 +48,16 @@ float sampleVolumetricLight(vec2 uv, vec3 lightVec, float rawDepth)
|
|||
if (min(samplepos.x, samplepos.y) > 0. && max(samplepos.x, samplepos.y) < 1.)
|
||||
result += texture2D(depthmap, samplepos).r < 1. ? 0.0 : 1.0;
|
||||
}
|
||||
|
||||
#ifdef VOLUMETRIC_DEPTH_ATTENUATION
|
||||
// We use the depth map to approximate the effect of depth on the light intensity.
|
||||
// The exponent was chosen based on aesthetic preference.
|
||||
// To make this phsyically accurate, the brightness here should scale linearly with depth,
|
||||
// but this would make the godrays either too faint or too strong in many cases.
|
||||
return result / samples * pow(texture2D(depthmap, uv).r, 128.0);
|
||||
#else
|
||||
return result / samples;
|
||||
#endif
|
||||
}
|
||||
|
||||
vec3 getDirectLightScatteringAtGround(vec3 v_LightDirection)
|
||||
|
@ -56,13 +65,10 @@ 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.))));
|
||||
|
||||
return exp(-beta_r0_l * beta_r0 * atmosphere_height / (1e-5 - dot(v_LightDirection, vec3(0., 1., 0.))));
|
||||
}
|
||||
|
||||
vec3 applyVolumetricLight(vec3 color, vec2 uv, float rawDepth)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue