diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-09 18:51:54 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-09 18:51:54 +0100 |
| commit | 7790472dabfa0ecd06f3408d847860ec6072866e (patch) | |
| tree | 5bce7b119f42d131daf746ddc052da2da5ff0650 /workspaces/main/shaders/math | |
| parent | 002ab9094f638c46d5db95d478e71c10933aceb2 (diff) | |
feat: Implement workspace system (Task #77)
Self-contained workspaces for parallel demo development.
Structure:
- workspaces/main,test - Demo-specific resources
- assets/common - Shared resources
- workspace.cfg - Configuration per workspace
CMake integration:
- DEMO_WORKSPACE option (defaults to main)
- cmake/ParseWorkspace.cmake - Config parser
- Workspace-relative asset/timeline/music paths
Migration:
- Main demo: demo.seq to workspaces/main/timeline.seq
- Test demo: test_demo.seq to workspaces/test/timeline.seq
- Common shaders: assets/common/shaders
- Workspace shaders: workspaces/*/shaders
Build:
cmake -B build -DDEMO_WORKSPACE=main
cmake -B build_test -DDEMO_WORKSPACE=test
All tests passing (36/36).
handoff(Claude): Task #77 workspace system complete. Both main and test workspaces build and pass all tests.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'workspaces/main/shaders/math')
| -rw-r--r-- | workspaces/main/shaders/math/common_utils.wgsl | 36 | ||||
| -rw-r--r-- | workspaces/main/shaders/math/noise.wgsl | 147 | ||||
| -rw-r--r-- | workspaces/main/shaders/math/sdf_shapes.wgsl | 14 | ||||
| -rw-r--r-- | workspaces/main/shaders/math/sdf_utils.wgsl | 115 |
4 files changed, 312 insertions, 0 deletions
diff --git a/workspaces/main/shaders/math/common_utils.wgsl b/workspaces/main/shaders/math/common_utils.wgsl new file mode 100644 index 0000000..7131216 --- /dev/null +++ b/workspaces/main/shaders/math/common_utils.wgsl @@ -0,0 +1,36 @@ +// Common utility functions for WGSL shaders. +// Reduces duplication across renderer_3d, mesh_render, etc. + +// Constants +const PI: f32 = 3.14159265359; +const TAU: f32 = 6.28318530718; + +// Transform normal from local to world space using inverse model matrix +fn transform_normal(inv_model: mat4x4<f32>, normal_local: vec3<f32>) -> vec3<f32> { + let normal_matrix = mat3x3<f32>(inv_model[0].xyz, inv_model[1].xyz, inv_model[2].xyz); + return normalize(normal_matrix * normal_local); +} + +// Spherical UV mapping (sphere or any radial surface) +// Returns UV in [0,1] range +fn spherical_uv(p: vec3<f32>) -> vec2<f32> { + let u = atan2(p.x, p.z) / TAU + 0.5; + let v = acos(clamp(p.y / length(p), -1.0, 1.0)) / PI; + return vec2<f32>(u, v); +} + +// Spherical UV from direction vector (for skybox, etc.) +fn spherical_uv_from_dir(dir: vec3<f32>) -> vec2<f32> { + let u = atan2(dir.z, dir.x) / TAU + 0.5; + let v = asin(clamp(dir.y, -1.0, 1.0)) / PI + 0.5; + return vec2<f32>(u, v); +} + +// Grid pattern for procedural texturing (checkerboard-like) +fn grid_pattern(uv: vec2<f32>) -> f32 { + let grid = 0.5 + 0.5 * sin(uv.x * PI) * sin(uv.y * PI); + return smoothstep(0.45, 0.55, grid); +} + +// 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. diff --git a/workspaces/main/shaders/math/noise.wgsl b/workspaces/main/shaders/math/noise.wgsl new file mode 100644 index 0000000..9f99e4a --- /dev/null +++ b/workspaces/main/shaders/math/noise.wgsl @@ -0,0 +1,147 @@ +// Random number generation and noise functions for WGSL shaders. +// Collection of hash functions and noise generators. + +// ============================================ +// Hash Functions (Float Input) +// ============================================ + +// Hash: f32 -> f32 +// Fast fractional hash for floats +fn hash_1f(x: f32) -> f32 { + var v = fract(x * 0.3351); + v *= v + 33.33; + v *= v + v; + return fract(v); +} + +// Hash: vec2<f32> -> f32 +// 2D coordinate to single hash value +fn hash_2f(p: vec2<f32>) -> f32 { + var h = dot(p, vec2<f32>(127.1, 311.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec2<f32> -> vec2<f32> +// 2D coordinate to 2D hash (from Shadertoy 4djSRW) +fn hash_2f_2f(p: vec2<f32>) -> vec2<f32> { + var p3 = fract(vec3<f32>(p.x, p.y, p.x) * vec3<f32>(0.1021, 0.1013, 0.0977)); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.xx + p3.yz) * p3.zy); +} + +// Hash: vec3<f32> -> f32 +// 3D coordinate to single hash value +fn hash_3f(p: vec3<f32>) -> f32 { + var h = dot(p, vec3<f32>(127.1, 311.7, 74.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec3<f32> -> vec3<f32> +// 3D coordinate to 3D hash +fn hash_3f_3f(p: vec3<f32>) -> vec3<f32> { + var v = fract(p); + v += dot(v, v.yxz + 32.41); + return fract((v.xxy + v.yzz) * v.zyx); +} + +// ============================================ +// Hash Functions (Integer Input) +// ============================================ + +// Hash: u32 -> f32 +// Integer hash with bit operations (high quality) +fn hash_1u(p: u32) -> f32 { + var P = (p << 13u) ^ p; + P = P * (P * P * 15731u + 789221u) + 1376312589u; + return bitcast<f32>((P >> 9u) | 0x3f800000u) - 1.0; +} + +// Hash: u32 -> vec2<f32> +fn hash_1u_2f(p: u32) -> vec2<f32> { + return vec2<f32>(hash_1u(p), hash_1u(p + 1423u)); +} + +// Hash: u32 -> vec3<f32> +fn hash_1u_3f(p: u32) -> vec3<f32> { + return vec3<f32>(hash_1u(p), hash_1u(p + 1423u), hash_1u(p + 124453u)); +} + +// ============================================ +// Noise Functions +// ============================================ + +// Value Noise: 2D +// Interpolated grid noise using smoothstep +fn noise_2d(p: vec2<f32>) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n0 = hash_2f(i + vec2<f32>(0.0, 0.0)); + let n1 = hash_2f(i + vec2<f32>(1.0, 0.0)); + let n2 = hash_2f(i + vec2<f32>(0.0, 1.0)); + let n3 = hash_2f(i + vec2<f32>(1.0, 1.0)); + let ix0 = mix(n0, n1, u.x); + let ix1 = mix(n2, n3, u.x); + return mix(ix0, ix1, u.y); +} + +// Value Noise: 3D +fn noise_3d(p: vec3<f32>) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n000 = hash_3f(i + vec3<f32>(0.0, 0.0, 0.0)); + let n100 = hash_3f(i + vec3<f32>(1.0, 0.0, 0.0)); + let n010 = hash_3f(i + vec3<f32>(0.0, 1.0, 0.0)); + let n110 = hash_3f(i + vec3<f32>(1.0, 1.0, 0.0)); + let n001 = hash_3f(i + vec3<f32>(0.0, 0.0, 1.0)); + let n101 = hash_3f(i + vec3<f32>(1.0, 0.0, 1.0)); + let n011 = hash_3f(i + vec3<f32>(0.0, 1.0, 1.0)); + let n111 = hash_3f(i + vec3<f32>(1.0, 1.0, 1.0)); + let ix00 = mix(n000, n100, u.x); + let ix10 = mix(n010, n110, u.x); + let ix01 = mix(n001, n101, u.x); + let ix11 = mix(n011, n111, u.x); + let iy0 = mix(ix00, ix10, u.y); + let iy1 = mix(ix01, ix11, u.y); + return mix(iy0, iy1, u.z); +} + +// ============================================ +// Special Functions +// ============================================ + +// Gyroid function (periodic triply-orthogonal minimal surface) +// Useful for procedural patterns and cellular structures +fn gyroid(p: vec3<f32>) -> f32 { + return abs(0.04 + dot(sin(p), cos(p.zxy))); +} + +// Fractional Brownian Motion (FBM) 2D +// Multi-octave noise for natural-looking variation +fn fbm_2d(p: vec2<f32>, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_2d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} + +// Fractional Brownian Motion (FBM) 3D +fn fbm_3d(p: vec3<f32>, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_3d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} diff --git a/workspaces/main/shaders/math/sdf_shapes.wgsl b/workspaces/main/shaders/math/sdf_shapes.wgsl new file mode 100644 index 0000000..31bbe2d --- /dev/null +++ b/workspaces/main/shaders/math/sdf_shapes.wgsl @@ -0,0 +1,14 @@ +fn sdSphere(p: vec3<f32>, r: f32) -> f32 { + return length(p) - r; +} +fn sdBox(p: vec3<f32>, b: vec3<f32>) -> f32 { + let q = abs(p) - b; + return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); +} +fn sdTorus(p: vec3<f32>, t: vec2<f32>) -> f32 { + let q = vec2<f32>(length(p.xz) - t.x, p.y); + return length(q) - t.y; +} +fn sdPlane(p: vec3<f32>, n: vec3<f32>, h: f32) -> f32 { + return dot(p, n) + h; +} diff --git a/workspaces/main/shaders/math/sdf_utils.wgsl b/workspaces/main/shaders/math/sdf_utils.wgsl new file mode 100644 index 0000000..660a4ce --- /dev/null +++ b/workspaces/main/shaders/math/sdf_utils.wgsl @@ -0,0 +1,115 @@ +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); +} |
