From 61116e94a539ac1cb8baa8e02a71876bc13ae1a5 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 6 May 2026 08:34:53 -0700 Subject: [PATCH] fix(m2): NaN guards on setInstancePosition and setInstanceTransform Same boundary-rejection pattern as createInstance. NaN in either function would corrupt the spatial grid (stale cells pointing at NaN-bounded instances) and the GPU model-matrix UBO. --- src/rendering/m2_renderer_instance.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/rendering/m2_renderer_instance.cpp b/src/rendering/m2_renderer_instance.cpp index caa16f3c..5be32b13 100644 --- a/src/rendering/m2_renderer_instance.cpp +++ b/src/rendering/m2_renderer_instance.cpp @@ -33,6 +33,8 @@ thread_local std::vector tl_m2_collisionTriScratch; } // namespace m2_internal void M2Renderer::setInstancePosition(uint32_t instanceId, const glm::vec3& position) { + if (!std::isfinite(position.x) || !std::isfinite(position.y) || + !std::isfinite(position.z)) return; auto idxIt = instanceIndexById.find(instanceId); if (idxIt == instanceIndexById.end()) return; auto& inst = instances[idxIt->second]; @@ -132,6 +134,12 @@ float M2Renderer::getInstanceAnimDuration(uint32_t instanceId) const { void M2Renderer::setInstanceTransform(uint32_t instanceId, const glm::mat4& transform) { auto idxIt = instanceIndexById.find(instanceId); if (idxIt == instanceIndexById.end()) return; + // Reject NaN matrix — would propagate into the model matrix uniform + // and the spatial-grid bounds, leaving stale grid cells pointing at + // a NaN-bounded instance. + for (int c = 0; c < 4; c++) + for (int r = 0; r < 4; r++) + if (!std::isfinite(transform[c][r])) return; auto& inst = instances[idxIt->second]; // Remove old grid cells before updating bounds