From 0f15d0f3a00f8f2c4490bc724b8df48898dc4d0f Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 6 May 2026 07:58:56 -0700 Subject: [PATCH] fix(terrain): reject NaN rays in raycastTerrain Without this, the AABB tests divide by ray.direction components and NaN propagates through tmin/tmax into the triangle intersection, returning undefined behavior at the hit position. --- tools/editor/terrain_editor.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/editor/terrain_editor.cpp b/tools/editor/terrain_editor.cpp index 95abb3d3..4c7eef0e 100644 --- a/tools/editor/terrain_editor.cpp +++ b/tools/editor/terrain_editor.cpp @@ -129,6 +129,13 @@ void TerrainEditor::setVertexHeight(int chunkIdx, int vertIdx, float height) { bool TerrainEditor::raycastTerrain(const rendering::Ray& ray, glm::vec3& hitPos) const { if (!terrain_) return false; + // Reject NaN ray. The AABB test divides by ray.direction[i], so NaN + // would propagate through tmin/tmax and the result would be undefined. + if (!std::isfinite(ray.origin.x) || !std::isfinite(ray.origin.y) || + !std::isfinite(ray.origin.z) || !std::isfinite(ray.direction.x) || + !std::isfinite(ray.direction.y) || !std::isfinite(ray.direction.z)) { + return false; + } float bestT = 1e30f; bool hit = false;