OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / addons / nubis / shaders / cloud_composite.glsl

39 lines · glsl ref:
#[compute]
#version 450

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform sampler2D cloud_texture;
layout(set = 0, binding = 1) uniform sampler2D depth_texture;
layout(set = 0, binding = 2, rgba16f) uniform restrict image2D color_buffer;

layout(push_constant, std430) uniform Params {
	vec2 screen_size;
	float debug_mode;
	float _pad;
} p;

void main() {
	ivec2 pixel = ivec2(gl_GlobalInvocationID.xy);
	if (pixel.x >= int(p.screen_size.x) || pixel.y >= int(p.screen_size.y)) return;

	vec2 uv = (vec2(pixel) + 0.5) / p.screen_size;
	vec4 cloud = textureLod(cloud_texture, uv, 0.0);

	// Depth test: only composite clouds where there's no scene geometry.
	// Godot reverse-Z: 0.0 = far/sky, 1.0 = near.
	// Clouds should only appear in sky pixels (depth near 0).
	float raw_depth = texelFetch(depth_texture, pixel, 0).r;
	bool is_sky = raw_depth < 0.0001;

	vec4 color = imageLoad(color_buffer, pixel);

	if (p.debug_mode > 0.5) {
		color.rgb = cloud.aaa;
	} else if (is_sky && cloud.a > 0.001) {
		// Only composite clouds onto sky pixels
		color.rgb = cloud.rgb * cloud.a + color.rgb * (1.0 - cloud.a);
	}
	imageStore(color_buffer, pixel, color);
}