1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00
luanti/client/shaders/clouds_merge/opengl_fragment.glsl

88 lines
2.6 KiB
Text
Raw Normal View History

2024-07-05 12:15:22 +02:00
#define cloudsTexture texture0
#define sceneTexture texture1
2024-08-18 15:13:26 +02:00
#define depthmap texture2
2024-07-05 12:15:22 +02:00
uniform sampler2D cloudsTexture;
uniform sampler2D sceneTexture;
2024-08-18 15:13:26 +02:00
uniform sampler2D depthmap;
2024-07-05 12:15:22 +02:00
uniform vec2 texelSize0;
uniform vec3 dayLight;
2024-08-18 15:13:26 +02:00
uniform vec3 cloudColor;
2024-07-05 12:15:22 +02:00
varying vec2 screenspaceCoordinate;
2024-08-18 15:13:26 +02:00
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));
}
2024-07-05 12:15:22 +02:00
vec4 sampleClouds(vec2 uv) {
vec4 cloudsKey = texture2D(cloudsTexture, uv);
2024-08-18 15:13:26 +02:00
//const vec3 darkColor = vec3(0.05, 0.1, 0.2);
vec3 darkColor = vec3(0.2, 0.4, 0.8) * dayLight;
2024-07-05 12:15:22 +02:00
const vec3 auroraDark = vec3(0., 0.5, 0.5);
const vec3 auroraBright = vec3(0., 0.5, .0);
2024-08-18 15:13:26 +02:00
//const vec3 auroraDark = vec3(0.);
//const vec3 auroraBright = vec3(0.);
2024-07-05 12:15:22 +02:00
return vec4(
2024-08-18 15:13:26 +02:00
mix(auroraDark, auroraBright, cloudsKey.b) * cloudsKey.b * auroraFactor +
cloudsKey.r * cloudColor * (darkColor * max(0., 1. - cloudsKey.g) + dayLight * sunTint * cloudsKey.g),
2024-07-05 12:15:22 +02:00
cloudsKey.r);
}
vec4 getClouds(vec2 uv) {
2024-08-18 16:11:30 +02:00
#if (VOLUMETRICS_UNDERSAMPLING <= 1)
return sampleClouds(uv);
#else
2024-07-05 12:15:22 +02:00
return
sampleClouds(uv - texelSize0 * vec2(-1.0, -1.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 0.0, -1.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 1.0, -1.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2(-1.0, 0.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 0.0, 0.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 1.0, 0.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2(-1.0, 1.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 0.0, 1.0)) / 9.0 +
sampleClouds(uv - texelSize0 * vec2( 1.0, 1.0)) / 9.0;
2024-08-18 16:11:30 +02:00
#endif
2024-07-05 12:15:22 +02:00
}
void main(void)
{
2024-08-18 15:13:26 +02:00
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);
2024-07-05 12:15:22 +02:00
vec4 sceneColor = texture2D(sceneTexture, screenspaceCoordinate * 0.5 + 0.5);
2024-08-18 15:13:26 +02:00
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;
}
2024-07-05 12:15:22 +02:00
}