Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2022-07-13 16:01:47 +0300
committerClément Foucault <foucault.clem@gmail.com>2022-07-21 16:50:35 +0300
commit2bad3577c079a738030ec252e906122c24315e98 (patch)
tree301cf3b9178a028ea911eab14d4e2cb164d7cdf9 /source/blender/draw/intern/shaders/common_math_geom_lib.glsl
parent4ba6bac2f1a45fbd0715dd076992cf21f41f262f (diff)
DRW: common_math_geom_lib.glsl: Add line_aabb_clipping_dist
Diffstat (limited to 'source/blender/draw/intern/shaders/common_math_geom_lib.glsl')
-rw-r--r--source/blender/draw/intern/shaders/common_math_geom_lib.glsl17
1 files changed, 17 insertions, 0 deletions
diff --git a/source/blender/draw/intern/shaders/common_math_geom_lib.glsl b/source/blender/draw/intern/shaders/common_math_geom_lib.glsl
index 6d4452c18c8..af8a8cd8784 100644
--- a/source/blender/draw/intern/shaders/common_math_geom_lib.glsl
+++ b/source/blender/draw/intern/shaders/common_math_geom_lib.glsl
@@ -90,6 +90,23 @@ float line_unit_box_intersect_dist_safe(vec3 lineorigin, vec3 linedirection)
return line_unit_box_intersect_dist(lineorigin, safe_linedirection);
}
+/**
+ * Returns clipping distance (intersection with the nearest plane) with the given axis-aligned
+ * bound box along \a line_direction.
+ * Safe even if \a line_direction is degenerate.
+ * It assumes that an intersection exists (i.e: that \a line_direction points towards the AABB).
+ */
+float line_aabb_clipping_dist(vec3 line_origin, vec3 line_direction, vec3 aabb_min, vec3 aabb_max)
+{
+ vec3 safe_dir = select(line_direction, vec3(1e-5), lessThan(abs(line_direction), vec3(1e-5)));
+ vec3 dir_inv = 1.0 / safe_dir;
+
+ vec3 first_plane = (aabb_min - line_origin) * dir_inv;
+ vec3 second_plane = (aabb_max - line_origin) * dir_inv;
+ vec3 nearest_plane = min(first_plane, second_plane);
+ return max_v3(nearest_plane);
+}
+
/** \} */
/* ---------------------------------------------------------------------- */