summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-08-12 14:09:48 -0700
committeryum <yum.food.vr@gmail.com>2023-08-12 14:09:48 -0700
commita7779ddd57fb1bdb2f27a35005d7d8c1eb691f63 (patch)
tree7a48edcc1eb916d37614ed36caad05876c5fb12a
parent42c32eb259480922a441658300eb79fbe10dccc1 (diff)
Optimize skew-pyramid frame SDF
Use symmetry to reduce # of distance calculations by 50%. Because the pyramid can be skewed, we can't reduce this by another factor of 2.
-rw-r--r--Shaders/ray_march.cginc24
1 files changed, 15 insertions, 9 deletions
diff --git a/Shaders/ray_march.cginc b/Shaders/ray_march.cginc
index d40286a..b83722c 100644
--- a/Shaders/ray_march.cginc
+++ b/Shaders/ray_march.cginc
@@ -62,19 +62,25 @@ float distance_from_rect_pyramid_frame(float3 p, float dx, float dy, float h, fl
{
float3 p0 = float3(dx/2, dy/2, 0);
float3 p1 = float3(dx/2, -dy/2, 0);
- float3 p2 = float3(-dx/2, -dy/2, 0);
float3 p3 = float3(-dx/2, dy/2, 0);
float3 p4 = float3(skew, 0, h);
+ float3 pp = p;
+ // Symmetry
+ pp.x = abs(pp.x);
+ float d01 = distance_from_line_segment(pp, p0, p1, e);
+
+ pp = p;
+ pp.y = abs(pp.y);
+ float d03 = distance_from_line_segment(pp, p0, p3, e);
+ float d04 = distance_from_line_segment(pp, p0, p4, e);
+ float d14 = distance_from_line_segment(pp, p3, p4, e);
+
float dist = 1000;
- dist = min(dist, distance_from_line_segment(p, p0, p1, e));
- dist = min(dist, distance_from_line_segment(p, p1, p2, e));
- dist = min(dist, distance_from_line_segment(p, p2, p3, e));
- dist = min(dist, distance_from_line_segment(p, p3, p0, e));
- dist = min(dist, distance_from_line_segment(p, p0, p4, e));
- dist = min(dist, distance_from_line_segment(p, p1, p4, e));
- dist = min(dist, distance_from_line_segment(p, p2, p4, e));
- dist = min(dist, distance_from_line_segment(p, p3, p4, e));
+ dist = min(dist, d01);
+ dist = min(dist, d04);
+ dist = min(dist, d03);
+ dist = min(dist, d14);
return dist;
}