summaryrefslogtreecommitdiff
path: root/common/shaders/math/common_utils.wgsl
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-21 08:42:13 +0100
committerskal <pascal.massimino@gmail.com>2026-02-21 08:42:13 +0100
commit25dc87528915fb0fd89181333324b215d77ad014 (patch)
tree5994db01a9d68a8d4166fa9ee5058c2b793b9795 /common/shaders/math/common_utils.wgsl
parentfc40d39ac302fae6f5fe726c6bc077f53580c052 (diff)
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.
Diffstat (limited to 'common/shaders/math/common_utils.wgsl')
-rw-r--r--common/shaders/math/common_utils.wgsl10
1 files changed, 10 insertions, 0 deletions
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>) -> 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<f32> in NDC space, with X adjusted for aspect ratio.
+fn getScreenCoord(p: vec4<f32>, resolution: vec2<f32>) -> vec2<f32> {
+ let q = p.xy / resolution;
+ var coord = -1.0 + 2.0 * q;
+ coord.x *= resolution.x / resolution.y;
+ return coord;
+}