diff options
Diffstat (limited to 'common/shaders/math')
| -rw-r--r-- | common/shaders/math/color.wgsl | 16 | ||||
| -rw-r--r-- | common/shaders/math/common_utils.wgsl | 18 | ||||
| -rw-r--r-- | common/shaders/math/noise.wgsl | 68 | ||||
| -rw-r--r-- | common/shaders/math/sdf_shapes.wgsl | 18 | ||||
| -rw-r--r-- | common/shaders/math/sdf_utils.wgsl | 32 | ||||
| -rw-r--r-- | common/shaders/math/utils.wgsl | 4 |
6 files changed, 78 insertions, 78 deletions
diff --git a/common/shaders/math/color.wgsl b/common/shaders/math/color.wgsl index b63c915..9352053 100644 --- a/common/shaders/math/color.wgsl +++ b/common/shaders/math/color.wgsl @@ -2,26 +2,26 @@ // sRGB to Linear approximation // Note: Assumes input is in sRGB color space. -fn sRGB(t: vec3<f32>) -> vec3<f32> { - return mix(1.055 * pow(t, vec3<f32>(1.0/2.4)) - 0.055, 12.92 * t, step(t, vec3<f32>(0.0031308))); +fn sRGB(t: vec3f) -> vec3f { + return mix(1.055 * pow(t, vec3f(1.0/2.4)) - 0.055, 12.92 * t, step(t, vec3f(0.0031308))); } // ACES Filmic Tone Mapping (Approximate) // A common tone mapping algorithm used in games and film. -fn aces_approx(v_in: vec3<f32>) -> vec3<f32> { - var v = max(v_in, vec3<f32>(0.0)); +fn aces_approx(v_in: vec3f) -> vec3f { + var v = max(v_in, vec3f(0.0)); v *= 0.6; let a = 2.51; let b = 0.03; let c = 2.43; let d = 0.59; let e = 0.14; - return clamp((v * (a * v + b)) / (v * (c * v + d) + e), vec3<f32>(0.0), vec3<f32>(1.0)); + return clamp((v * (a * v + b)) / (v * (c * v + d) + e), vec3f(0.0), vec3f(1.0)); } // HSV to RGB conversion -const hsv2rgb_K = vec4<f32>(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); -fn hsv2rgb(c: vec3<f32>) -> vec3<f32> { +const hsv2rgb_K = vec4f(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); +fn hsv2rgb(c: vec3f) -> vec3f { let p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); - return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, vec3<f32>(0.0), vec3<f32>(1.0)), c.y); + return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, vec3f(0.0), vec3f(1.0)), c.y); } diff --git a/common/shaders/math/common_utils.wgsl b/common/shaders/math/common_utils.wgsl index b8446b4..6ebc25a 100644 --- a/common/shaders/math/common_utils.wgsl +++ b/common/shaders/math/common_utils.wgsl @@ -6,28 +6,28 @@ 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: vec3f) -> vec3f { - let normal_matrix = mat3x3<f32>(inv_model[0].xyz, inv_model[1].xyz, inv_model[2].xyz); +fn transform_normal(inv_model: mat4x4f, normal_local: vec3f) -> vec3f { + let normal_matrix = mat3x3f(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: vec3f) -> vec2<f32> { +fn spherical_uv(p: vec3f) -> vec2f { 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); + return vec2f(u, v); } // Spherical UV from direction vector (for skybox, etc.) -fn spherical_uv_from_dir(dir: vec3f) -> vec2<f32> { +fn spherical_uv_from_dir(dir: vec3f) -> vec2f { 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); + return vec2f(u, v); } // Grid pattern for procedural texturing (checkerboard-like) -fn grid_pattern(uv: vec2<f32>) -> f32 { +fn grid_pattern(uv: vec2f) -> f32 { let grid = 0.5 + 0.5 * sin(uv.x * PI) * sin(uv.y * PI); return smoothstep(0.45, 0.55, grid); } @@ -37,8 +37,8 @@ fn grid_pattern(uv: vec2<f32>) -> f32 { // 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> { +// Returns a vec2f in NDC space, with X adjusted for aspect ratio. +fn getScreenCoord(p: vec4f, resolution: vec2f) -> vec2f { let q = p.xy / resolution; var coord = -1.0 + 2.0 * q; coord.x *= resolution.x / resolution.y; diff --git a/common/shaders/math/noise.wgsl b/common/shaders/math/noise.wgsl index 9f99e4a..dd97e02 100644 --- a/common/shaders/math/noise.wgsl +++ b/common/shaders/math/noise.wgsl @@ -14,31 +14,31 @@ fn hash_1f(x: f32) -> f32 { return fract(v); } -// Hash: vec2<f32> -> f32 +// Hash: vec2f -> f32 // 2D coordinate to single hash value -fn hash_2f(p: vec2<f32>) -> f32 { - var h = dot(p, vec2<f32>(127.1, 311.7)); +fn hash_2f(p: vec2f) -> f32 { + var h = dot(p, vec2f(127.1, 311.7)); return fract(sin(h) * 43758.5453123); } -// Hash: vec2<f32> -> vec2<f32> +// Hash: vec2f -> vec2f // 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)); +fn hash_2f_2f(p: vec2f) -> vec2f { + var p3 = fract(vec3f(p.x, p.y, p.x) * vec3f(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 +// Hash: vec3f -> 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)); +fn hash_3f(p: vec3f) -> f32 { + var h = dot(p, vec3f(127.1, 311.7, 74.7)); return fract(sin(h) * 43758.5453123); } -// Hash: vec3<f32> -> vec3<f32> +// Hash: vec3f -> vec3f // 3D coordinate to 3D hash -fn hash_3f_3f(p: vec3<f32>) -> vec3<f32> { +fn hash_3f_3f(p: vec3f) -> vec3f { var v = fract(p); v += dot(v, v.yxz + 32.41); return fract((v.xxy + v.yzz) * v.zyx); @@ -56,14 +56,14 @@ fn hash_1u(p: u32) -> f32 { 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 -> vec2f +fn hash_1u_2f(p: u32) -> vec2f { + return vec2f(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)); +// Hash: u32 -> vec3f +fn hash_1u_3f(p: u32) -> vec3f { + return vec3f(hash_1u(p), hash_1u(p + 1423u), hash_1u(p + 124453u)); } // ============================================ @@ -72,32 +72,32 @@ fn hash_1u_3f(p: u32) -> vec3<f32> { // Value Noise: 2D // Interpolated grid noise using smoothstep -fn noise_2d(p: vec2<f32>) -> f32 { +fn noise_2d(p: vec2f) -> 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 n0 = hash_2f(i + vec2f(0.0, 0.0)); + let n1 = hash_2f(i + vec2f(1.0, 0.0)); + let n2 = hash_2f(i + vec2f(0.0, 1.0)); + let n3 = hash_2f(i + vec2f(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 { +fn noise_3d(p: vec3f) -> 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 n000 = hash_3f(i + vec3f(0.0, 0.0, 0.0)); + let n100 = hash_3f(i + vec3f(1.0, 0.0, 0.0)); + let n010 = hash_3f(i + vec3f(0.0, 1.0, 0.0)); + let n110 = hash_3f(i + vec3f(1.0, 1.0, 0.0)); + let n001 = hash_3f(i + vec3f(0.0, 0.0, 1.0)); + let n101 = hash_3f(i + vec3f(1.0, 0.0, 1.0)); + let n011 = hash_3f(i + vec3f(0.0, 1.0, 1.0)); + let n111 = hash_3f(i + vec3f(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); @@ -113,13 +113,13 @@ fn noise_3d(p: vec3<f32>) -> f32 { // Gyroid function (periodic triply-orthogonal minimal surface) // Useful for procedural patterns and cellular structures -fn gyroid(p: vec3<f32>) -> f32 { +fn gyroid(p: vec3f) -> 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 { +fn fbm_2d(p: vec2f, octaves: i32) -> f32 { var value = 0.0; var amplitude = 0.5; var frequency = 1.0; @@ -133,7 +133,7 @@ fn fbm_2d(p: vec2<f32>, octaves: i32) -> f32 { } // Fractional Brownian Motion (FBM) 3D -fn fbm_3d(p: vec3<f32>, octaves: i32) -> f32 { +fn fbm_3d(p: vec3f, octaves: i32) -> f32 { var value = 0.0; var amplitude = 0.5; var frequency = 1.0; diff --git a/common/shaders/math/sdf_shapes.wgsl b/common/shaders/math/sdf_shapes.wgsl index 4dcfdd6..2dfae3e 100644 --- a/common/shaders/math/sdf_shapes.wgsl +++ b/common/shaders/math/sdf_shapes.wgsl @@ -1,30 +1,30 @@ // 3D SDF primitives -fn sdSphere(p: vec3<f32>, r: f32) -> f32 { +fn sdSphere(p: vec3f, r: f32) -> f32 { return length(p) - r; } -fn sdBox(p: vec3<f32>, b: vec3<f32>) -> f32 { +fn sdBox(p: vec3f, b: vec3f) -> 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); + return length(max(q, vec3f(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); +fn sdTorus(p: vec3f, t: vec2f) -> f32 { + let q = vec2f(length(p.xz) - t.x, p.y); return length(q) - t.y; } -fn sdPlane(p: vec3<f32>, n: vec3<f32>, h: f32) -> f32 { +fn sdPlane(p: vec3f, n: vec3f, h: f32) -> f32 { return dot(p, n) + h; } // 2D SDF primitives -fn sdBox2D(p: vec2<f32>, b: vec2<f32>) -> f32 { +fn sdBox2D(p: vec2f, b: vec2f) -> f32 { let d = abs(p) - b; - return length(max(d, vec2<f32>(0.0))) + min(max(d.x, d.y), 0.0); + return length(max(d, vec2f(0.0))) + min(max(d.x, d.y), 0.0); } // Approximate -fn sdEllipse(p: vec2<f32>, ab: vec2<f32>) -> f32 { +fn sdEllipse(p: vec2f, ab: vec2f) -> f32 { let d = length(p / ab); return length(p) * (1.0 - 1.0 / d); } diff --git a/common/shaders/math/sdf_utils.wgsl b/common/shaders/math/sdf_utils.wgsl index 660a4ce..5a77c7e 100644 --- a/common/shaders/math/sdf_utils.wgsl +++ b/common/shaders/math/sdf_utils.wgsl @@ -1,8 +1,8 @@ -fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { +fn get_normal_basic(p: vec3f, obj_params: vec4f) -> vec3f { 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>( + let e = vec2f(0.001, 0.0); + return normalize(vec3f( 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) @@ -12,11 +12,11 @@ fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { // 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> { +fn get_normal_fast(p: vec3f, obj_params: vec4f) -> vec3f { 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); + let k = vec2f(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) + @@ -29,13 +29,13 @@ fn get_normal_fast(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { // 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>, + p: vec3f, + obj_params: vec4f, noise_tex: texture_2d<f32>, noise_sampler: sampler, disp_strength: f32 -) -> vec3<f32> { - let e = vec2<f32>(0.005, 0.0); +) -> vec3f { + let e = vec2f(0.005, 0.0); let q_x1 = p + e.xyy; let uv_x1 = spherical_uv(q_x1); @@ -67,21 +67,21 @@ fn get_normal_bump( 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)); + return normalize(vec3f(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>, + p: vec3f, + obj_params: vec4f, noise_tex: texture_2d<f32>, noise_sampler: sampler, disp_strength: f32 -) -> vec3<f32> { +) -> vec3f { let eps = 0.0005; - let k = vec2<f32>(1.0, -1.0); + let k = vec2f(1.0, -1.0); let q1 = p + k.xyy * eps; let uv1 = spherical_uv(q1); @@ -107,9 +107,9 @@ fn get_normal_bump_fast( } // Distance to an Axis-Aligned Bounding Box -fn aabb_sdf(p: vec3<f32>, min_p: vec3<f32>, max_p: vec3<f32>) -> f32 { +fn aabb_sdf(p: vec3f, min_p: vec3f, max_p: vec3f) -> 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); + return length(max(q, vec3f(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); } diff --git a/common/shaders/math/utils.wgsl b/common/shaders/math/utils.wgsl index 85f0bdf..c75cb66 100644 --- a/common/shaders/math/utils.wgsl +++ b/common/shaders/math/utils.wgsl @@ -1,10 +1,10 @@ // General-purpose math utility functions. // Returns a 2x2 rotation matrix. -fn rot(a: f32) -> mat2x2<f32> { +fn rot(a: f32) -> mat2x2f { let c = cos(a); let s = sin(a); - return mat2x2<f32>(c, s, -s, c); + return mat2x2f(c, s, -s, c); } // Fast approximation of tanh. |
