summaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
Diffstat (limited to 'assets')
-rw-r--r--assets/final/shaders/math/sdf_utils.wgsl16
1 files changed, 16 insertions, 0 deletions
diff --git a/assets/final/shaders/math/sdf_utils.wgsl b/assets/final/shaders/math/sdf_utils.wgsl
index c2e49cf..3b06b93 100644
--- a/assets/final/shaders/math/sdf_utils.wgsl
+++ b/assets/final/shaders/math/sdf_utils.wgsl
@@ -9,6 +9,22 @@ 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> {
+ 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)
+ );
+}
+
// 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;