From 25dc87528915fb0fd89181333324b215d77ad014 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 21 Feb 2026 08:42:13 +0100 Subject: refactor(wgsl): Factorize getScreenCoord helper Factorizes the screen coordinate calculation from scene1.wgsl into a reusable getScreenCoord function in common/shaders/math/common_utils.wgsl. This improves code reuse and simplifies fragment shaders. --- common/shaders/math/common_utils.wgsl | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'common') diff --git a/common/shaders/math/common_utils.wgsl b/common/shaders/math/common_utils.wgsl index 7131216..49aaead 100644 --- a/common/shaders/math/common_utils.wgsl +++ b/common/shaders/math/common_utils.wgsl @@ -34,3 +34,13 @@ fn grid_pattern(uv: vec2) -> f32 { // NOTE: calc_sdf_normal_bumped() removed - too specialized, depends on get_dist() // from scene_query snippets. Keep bump mapping code inline in shaders that use it. + +// Calculates normalized screen coordinates from fragment position and resolution. +// Input `p` is the fragment's @builtin(position), `resolution` is the screen resolution. +// Returns a vec2 in NDC space, with X adjusted for aspect ratio. +fn getScreenCoord(p: vec4, resolution: vec2) -> vec2 { + let q = p.xy / resolution; + var coord = -1.0 + 2.0 * q; + coord.x *= resolution.x / resolution.y; + return coord; +} -- cgit v1.2.3