From 82279538a3e24b6f5bf5b72c56bb37a60640fc05 Mon Sep 17 00:00:00 2001 From: Morris Tabor <80684659+mortarroad@users.noreply.github.com> Date: Fri, 20 Aug 2021 15:18:55 +0200 Subject: [PATCH] Fix winding of new convex hull implementation. --- core/math/convex_hull.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/math/convex_hull.cpp b/core/math/convex_hull.cpp index 85fafa47b48..85ab7b9f007 100644 --- a/core/math/convex_hull.cpp +++ b/core/math/convex_hull.cpp @@ -2286,9 +2286,18 @@ Error ConvexHullComputer::convex_hull(const Vector3 *p_points, int32_t p_point_c e = e->get_next_edge_of_face(); } while (e != e_start); + // reverse indices: Godot wants clockwise, but this is counter-clockwise + if (face.indices.size() > 2) { + // reverse all but the first index. + int *indices = face.indices.ptrw(); + for (int c = 0; c < (face.indices.size() - 1) / 2; c++) { + SWAP(indices[c + 1], indices[face.indices.size() - 1 - c]); + } + } + // compute normal if (face.indices.size() >= 3) { - face.plane = Plane(r_mesh.vertices[face.indices[0]], r_mesh.vertices[face.indices[2]], r_mesh.vertices[face.indices[1]]); + face.plane = Plane(r_mesh.vertices[face.indices[0]], r_mesh.vertices[face.indices[1]], r_mesh.vertices[face.indices[2]]); } else { WARN_PRINT("Too few vertices per face."); }