summaryrefslogtreecommitdiff
path: root/common/shaders/math/sdf_utils.wgsl
diff options
context:
space:
mode:
Diffstat (limited to 'common/shaders/math/sdf_utils.wgsl')
-rw-r--r--common/shaders/math/sdf_utils.wgsl32
1 files changed, 16 insertions, 16 deletions
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);
}