summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-14 15:26:55 +0100
committerskal <pascal.massimino@gmail.com>2026-02-14 15:26:55 +0100
commit0127c747a41f972fd68e3e6e6b472859bfdb80dd (patch)
tree85d7277dbce1581f583a259368471e874b372a19 /workspaces/main/shaders
parentde7514e6b9ff9c0b9320975ba7d44754b5115b54 (diff)
refactor(wgsl): consolidate SDF shapes into single common file
Merge sdf_primitives.wgsl into math/sdf_shapes.wgsl to eliminate duplication and establish single source of truth for all SDF functions. Changes: - Delete common/shaders/sdf_primitives.wgsl (duplicate of math/sdf_shapes.wgsl) - Add sdBox2D() and sdEllipse() to math/sdf_shapes.wgsl - Update ellipse.wgsl (main/test) to use #include "math/sdf_shapes" - Update scene1.wgsl to use math/sdf_shapes instead of sdf_primitives - Rename asset SHADER_SDF_PRIMITIVES → SHADER_SDF_SHAPES - Update shader registration and tests Impact: - ~60 lines eliminated from ellipse shaders - Single source for 3D primitives (sphere, box, torus, plane) and 2D (box, ellipse) - Consistent include path across codebase All tests passing (34/34). handoff(Claude): SDF shapes consolidated to math/sdf_shapes.wgsl Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'workspaces/main/shaders')
-rw-r--r--workspaces/main/shaders/ellipse.wgsl33
-rw-r--r--workspaces/main/shaders/scene1.wgsl2
2 files changed, 2 insertions, 33 deletions
diff --git a/workspaces/main/shaders/ellipse.wgsl b/workspaces/main/shaders/ellipse.wgsl
index 07f0c9a..0aaa4ae 100644
--- a/workspaces/main/shaders/ellipse.wgsl
+++ b/workspaces/main/shaders/ellipse.wgsl
@@ -1,40 +1,9 @@
#include "common_uniforms"
#include "render/fullscreen_vs"
+#include "math/sdf_shapes"
@group(0) @binding(0) var<uniform> uniforms: CommonUniforms;
-fn sdEllipse(p: vec2<f32>, ab: vec2<f32>) -> f32 {
- var p_abs = abs(p);
- if (p_abs.x > p_abs.y) {
- p_abs = vec2<f32>(p_abs.y, p_abs.x);
- }
- let l = ab.y * ab.y - ab.x * ab.x;
- let m = ab.x * p_abs.x / l;
- let n = ab.y * p_abs.y / l;
- let m2 = m * m;
- let n2 = n * n;
- let c = (m2 + n2 - 1.0) / 3.0;
- let c3 = c * c * c;
- let d = c3 + m2 * n2;
- let g = m + m * n2;
- var co: f32;
- if (d < 0.0) {
- let h = acos((c3 + m2 * n2 * 2.0) / c3) / 3.0;
- let s = cos(h);
- let t = sin(h) * sqrt(3.0);
- co = (sqrt(-c * (s + t * 2.0) + m2) + sign(l) * sqrt(-c * (s - t * 2.0) + m2) + abs(g) / (sqrt(-c * (s + t * 2.0) + m2) * sqrt(-c * (s - t * 2.0) + m2)) - m) / 2.0;
- } else {
- let h = 2.0 * m * n * sqrt(d);
- let s = sign(c3 + m2 * n2 + h) * pow(abs(c3 + m2 * n2 + h), 1.0 / 3.0);
- let u = sign(c3 + m2 * n2 - h) * pow(abs(c3 + m2 * n2 - h), 1.0 / 3.0);
- let rx = -s - u + m2 * 2.0;
- let ry = (s - u) * sqrt(3.0);
- co = (ry / sqrt(sqrt(rx * rx + ry * ry) - rx) + 2.0 * g / sqrt(rx * rx + ry * ry) - m) / 2.0;
- }
- let si = sqrt(max(0.0, 1.0 - co * co));
- return length(p_abs - vec2<f32>(ab.x * co, ab.y * si)) * sign(p_abs.y * ab.x * co - p_abs.x * ab.y * si);
-}
-
@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
let uv = (p.xy / uniforms.resolution - 0.5) * 2.0;
let movement = vec2<f32>(sin(uniforms.time * 0.7), cos(uniforms.time * 0.5));
diff --git a/workspaces/main/shaders/scene1.wgsl b/workspaces/main/shaders/scene1.wgsl
index 2a811f7..2723b66 100644
--- a/workspaces/main/shaders/scene1.wgsl
+++ b/workspaces/main/shaders/scene1.wgsl
@@ -4,7 +4,7 @@
#include "common_uniforms"
#include "math/color"
#include "math/utils"
-#include "sdf_primitives"
+#include "math/sdf_shapes"
#include "render/raymarching"
@group(0) @binding(0) var<uniform> uniforms: CommonUniforms;