From 1a92f70f699389219dbda42d175e7898cdbe7783 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Fri, 23 Jan 2026 17:46:37 +0100 Subject: Working transmittance shader --- shaders/transmittance_lut.comp | 61 +++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'shaders') diff --git a/shaders/transmittance_lut.comp b/shaders/transmittance_lut.comp index 34adbae..bb7e621 100644 --- a/shaders/transmittance_lut.comp +++ b/shaders/transmittance_lut.comp @@ -13,14 +13,13 @@ layout(push_constant) uniform buffers { uint padding; } params; -vec2 sphere_intersection ( in vec3 pos, in vec3 dir, float radius ) -{ - float b = dot(pos, vec3(radius)); - float c = dot(pos, pos) - radius * radius; +vec2 sphere_intersection ( in vec3 orig, in vec3 dir, float radius ) { + float b = dot(orig, dir); + float c = dot(orig, orig) - radius * radius; float h = b * b - c; if (h < 0.f) return vec2(-1.f); // no intersection - h = sqrt( h ); - return vec2( -b - h, -b + h ); + h = sqrt(h); + return vec2(-b - h, -b + h); } vec3 rayleigh_scattering = vec3(5.802f, 13.558f, 33.1f); @@ -31,14 +30,6 @@ vec3 mie_absorption = vec3(4.40f); void main() { ivec2 coords = ivec2(gl_GlobalInvocationID.xy); - vec3 out_color = vec3(0); - if (coords.x % 2 == 0 ) { - out_color = vec3(1); - } - if (coords.y % 2 == 1 ) { - out_color = vec3(1); - } - imageStore(images[nonuniformEXT(params.output_image_index)], coords, vec4(out_color, 1.0)); const uint raymarching_steps = 10U; @@ -46,41 +37,57 @@ void main() { const float top_radius = 6420.f; const float atmosphere_radius = top_radius - ground_radius; - vec2 uvs = coords / params.tex_size; + vec2 uvs = vec2(coords) / params.tex_size; - float mu = uvs.y * 2.f - 1.f; - float theta = acos(mu); + float cos_mu = uvs.y * 2.f - 1.f; + float mu = acos(cos_mu); float height = atmosphere_radius * uvs.x; vec2 pos = vec2(0.f, ground_radius + height); - vec2 dir = vec2(mu, sin(theta)); - vec3 transmittance = vec3(0.f); + vec2 dir = normalize(vec2(cos_mu, sin(mu))); // find ground hit float t = 0.f; vec2 ground_intersection = sphere_intersection(vec3(pos, 0.f), vec3(dir, 0.f), ground_radius); + t = max(max(ground_intersection.x, 0.f), ground_intersection.y); t = ground_intersection.x; + // vec4 val = vec4(vec3(0.f), 1.f); + + vec2 top_intersection = sphere_intersection(vec3(pos, 0.f), vec3(dir, 0.f), top_radius); + // t = max(max(top_intersection.x, 0.f), top_intersection.y); // find atmosphere hit if (ground_intersection.y < 0.f) { vec2 top_intersection = sphere_intersection(vec3(pos, 0.f), vec3(dir, 0.f), top_radius); + t = top_intersection.y; + // val.z = 1.f; + } else { + // val.y = 1.f; } - vec2 hit = pos + dir * t; - vec3 extinction = rayleigh_scattering + rayleigh_absorption + mie_scattering + mie_absorption; - vec3 total_extinction = vec3(0.f); + // val.x = t; + // imageStore(images[nonuniformEXT(params.output_image_index)], coords, val); + // return; + imageStore(images[nonuniformEXT(params.output_image_index)], coords, vec4(t, 0.f, 0.f, 1.f)); + return; + + vec3 transmittance = vec3(0.f); // raymarching float step = t / raymarching_steps; - for (float dist = 0.f; dist < t; dist += step) { - vec2 cur = pos + dir * dist; - dist += step; + for (uint step_index = 0U; step_index < raymarching_steps; step_index++) { + float r_d = exp(-height / 8.f); + vec3 r_e = rayleigh_scattering * r_d; + float m_d = exp(-height / 1.2f); + vec3 m_e = mie_scattering * m_d + mie_absorption; - total_extinction += extinction * step; + transmittance += (r_e + m_e) * step; } - vec4 color = vec4(exp(-total_extinction), 1.f); + vec4 color = vec4(exp(-transmittance), 1.f); + color.xyz = vec3(0.f); + color.x = t; imageStore(images[nonuniformEXT(params.output_image_index)], coords, color); } -- cgit v1.3