Skip to content

Commit 93195f5

Browse files
committed
Ignore backfaces during Node3D selection
1 parent 89f32c6 commit 93195f5

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

core/math/triangle_mesh.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en
290290
return inters;
291291
}
292292

293-
bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index, int32_t *r_face_index) const {
293+
bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index, int32_t *r_face_index, bool p_ignore_backfaces) const {
294294
if (!valid) {
295295
return false;
296296
}
@@ -338,18 +338,23 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V
338338
Vector3 res;
339339

340340
if (f3.intersects_ray(p_begin, p_dir, &res)) {
341-
real_t nd = n.dot(res);
342-
if (nd < d) {
343-
d = nd;
344-
r_point = res;
345-
r_normal = f3.get_plane().get_normal();
346-
if (r_surf_index) {
347-
*r_surf_index = s.surface_index;
341+
Vector3 face_normal = f3.get_plane().get_normal();
342+
if (p_ignore_backfaces && p_dir.dot(face_normal) >= 0) {
343+
// Skip this triangle.
344+
} else {
345+
real_t nd = n.dot(res);
346+
if (nd < d) {
347+
d = nd;
348+
r_point = res;
349+
r_normal = face_normal;
350+
if (r_surf_index) {
351+
*r_surf_index = s.surface_index;
352+
}
353+
if (r_face_index) {
354+
*r_face_index = b.face_index;
355+
}
356+
inters = true;
348357
}
349-
if (r_face_index) {
350-
*r_face_index = b.face_index;
351-
}
352-
inters = true;
353358
}
354359
}
355360

core/math/triangle_mesh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class TriangleMesh : public RefCounted {
8585
public:
8686
bool is_valid() const;
8787
bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr, int32_t *r_face_index = nullptr) const;
88-
bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr, int32_t *r_face_index = nullptr) const;
88+
bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr, int32_t *r_face_index = nullptr, bool p_ignore_backfaces = false) const;
8989
bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const;
9090
Vector<Face3> get_faces() const;
9191

editor/scene/3d/node_3d_editor_gizmos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ bool EditorNode3DGizmo::intersect_ray(Camera3D *p_camera, const Point2 &p_point,
764764

765765
for (Ref<TriangleMesh> collision_mesh : collision_meshes) {
766766
if (collision_mesh.is_valid()) {
767-
if (collision_mesh->intersect_ray(ray_from, ray_dir, rpos, rnorm)) {
767+
if (collision_mesh->intersect_ray(ray_from, ray_dir, rpos, rnorm, nullptr, nullptr, true)) {
768768
r_pos = gt.xform(rpos);
769769
r_normal = gt.basis.xform(rnorm).normalized();
770770
return true;

scene/debugger/scene_debugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
25622562
Transform3D gt = geo_instance->get_global_transform();
25632563
Transform3D ai = gt.affine_inverse();
25642564
Vector3 point, normal;
2565-
if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal)) {
2565+
if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal, nullptr, nullptr, true)) {
25662566
SelectResult res;
25672567
res.item = Object::cast_to<Node>(obj);
25682568
res.order = -pos.distance_to(gt.xform(point));

0 commit comments

Comments
 (0)