diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-13 08:34:24 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-13 08:34:24 +0100 |
| commit | a109983c194c45ad85f0e481232bc605c7cfd85b (patch) | |
| tree | 2b2fadb054fb8ea52d3e31fefdc4678d5a3ed7dc /workspaces/main/shaders/math/sdf_utils.wgsl | |
| parent | 3ce45fcf073047d71ed0b2c88f4d6c5362f6b620 (diff) | |
Remediation: Implement shared common/shaders/ directory
Eliminates 36 duplicate shader files across workspaces.
Structure:
- common/shaders/{math,render,compute}/ - Shared utilities (20 files)
- workspaces/*/shaders/ - Workspace-specific only
Changes:
- Created common/shaders/ with math, render, compute subdirectories
- Moved 20 common shaders from workspaces to common/
- Removed duplicates from test workspace
- Updated assets.txt: ../../common/shaders/ references
- Enhanced asset_packer.cc: filesystem path normalization for ../ resolution
Implementation: Option 1 from SHADER_REUSE_INVESTIGATION.md
- Single source of truth for common code
- Workspace references via relative paths
- Path normalization in asset packer
handoff(Claude): Common shader directory implemented
Diffstat (limited to 'workspaces/main/shaders/math/sdf_utils.wgsl')
| -rw-r--r-- | workspaces/main/shaders/math/sdf_utils.wgsl | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/workspaces/main/shaders/math/sdf_utils.wgsl b/workspaces/main/shaders/math/sdf_utils.wgsl deleted file mode 100644 index 660a4ce..0000000 --- a/workspaces/main/shaders/math/sdf_utils.wgsl +++ /dev/null @@ -1,115 +0,0 @@ -fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { - let obj_type = obj_params.x; - if (obj_type == 1.0) { return normalize(p); } - let e = vec2<f32>(0.001, 0.0); - return normalize(vec3<f32>( - get_dist(p + e.xyy, obj_params) - get_dist(p - e.xyy, obj_params), - get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params), - get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params) - )); -} - -// Optimized normal estimation using tetrahedron pattern (4 SDF evals instead of 6). -// Slightly less accurate than central differences but faster. -// Uses tetrahedral gradient approximation with corners at (±1, ±1, ±1). -fn get_normal_fast(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { - let obj_type = obj_params.x; - if (obj_type == 1.0) { return normalize(p); } - let eps = 0.0001; - let k = vec2<f32>(1.0, -1.0); - return normalize( - k.xyy * get_dist(p + k.xyy * eps, obj_params) + - k.yyx * get_dist(p + k.yyx * eps, obj_params) + - k.yxy * get_dist(p + k.yxy * eps, obj_params) + - k.xxx * get_dist(p + k.xxx * eps, obj_params) - ); -} - -// Bump-mapped normal using central differences (6 samples: SDF + texture). -// High quality, suitable for detailed surfaces with displacement mapping. -// Note: Requires spherical_uv() function and get_dist() to be available in calling context. -fn get_normal_bump( - p: vec3<f32>, - obj_params: vec4<f32>, - noise_tex: texture_2d<f32>, - noise_sampler: sampler, - disp_strength: f32 -) -> vec3<f32> { - let e = vec2<f32>(0.005, 0.0); - - let q_x1 = p + e.xyy; - let uv_x1 = spherical_uv(q_x1); - let h_x1 = textureSample(noise_tex, noise_sampler, uv_x1).r; - let d_x1 = get_dist(q_x1, obj_params) - disp_strength * h_x1; - - let q_x2 = p - e.xyy; - let uv_x2 = spherical_uv(q_x2); - let h_x2 = textureSample(noise_tex, noise_sampler, uv_x2).r; - let d_x2 = get_dist(q_x2, obj_params) - disp_strength * h_x2; - - let q_y1 = p + e.yxy; - let uv_y1 = spherical_uv(q_y1); - let h_y1 = textureSample(noise_tex, noise_sampler, uv_y1).r; - let d_y1 = get_dist(q_y1, obj_params) - disp_strength * h_y1; - - let q_y2 = p - e.yxy; - let uv_y2 = spherical_uv(q_y2); - let h_y2 = textureSample(noise_tex, noise_sampler, uv_y2).r; - let d_y2 = get_dist(q_y2, obj_params) - disp_strength * h_y2; - - let q_z1 = p + e.yyx; - let uv_z1 = spherical_uv(q_z1); - let h_z1 = textureSample(noise_tex, noise_sampler, uv_z1).r; - let d_z1 = get_dist(q_z1, obj_params) - disp_strength * h_z1; - - let q_z2 = p - e.yyx; - let uv_z2 = spherical_uv(q_z2); - let h_z2 = textureSample(noise_tex, noise_sampler, uv_z2).r; - let d_z2 = get_dist(q_z2, obj_params) - disp_strength * h_z2; - - return normalize(vec3<f32>(d_x1 - d_x2, d_y1 - d_y2, d_z1 - d_z2)); -} - -// Optimized bump-mapped normal using tetrahedron pattern (4 samples instead of 6). -// 33% faster than get_normal_bump(), slightly less accurate. -// Suitable for real-time rendering with displacement mapping. -fn get_normal_bump_fast( - p: vec3<f32>, - obj_params: vec4<f32>, - noise_tex: texture_2d<f32>, - noise_sampler: sampler, - disp_strength: f32 -) -> vec3<f32> { - let eps = 0.0005; - let k = vec2<f32>(1.0, -1.0); - - let q1 = p + k.xyy * eps; - let uv1 = spherical_uv(q1); - let h1 = textureSample(noise_tex, noise_sampler, uv1).r; - let d1 = get_dist(q1, obj_params) - disp_strength * h1; - - let q2 = p + k.yyx * eps; - let uv2 = spherical_uv(q2); - let h2 = textureSample(noise_tex, noise_sampler, uv2).r; - let d2 = get_dist(q2, obj_params) - disp_strength * h2; - - let q3 = p + k.yxy * eps; - let uv3 = spherical_uv(q3); - let h3 = textureSample(noise_tex, noise_sampler, uv3).r; - let d3 = get_dist(q3, obj_params) - disp_strength * h3; - - let q4 = p + k.xxx * eps; - let uv4 = spherical_uv(q4); - let h4 = textureSample(noise_tex, noise_sampler, uv4).r; - let d4 = get_dist(q4, obj_params) - disp_strength * h4; - - return normalize(k.xyy * d1 + k.yyx * d2 + k.yxy * d3 + k.xxx * d4); -} - -// Distance to an Axis-Aligned Bounding Box -fn aabb_sdf(p: vec3<f32>, min_p: vec3<f32>, max_p: vec3<f32>) -> f32 { - let center = (min_p + max_p) * 0.5; - let extent = (max_p - min_p) * 0.5; - let q = abs(p - center) - extent; - return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); -} |
