如何在围绕原点旋转的 3D 网格中为任何给定点找到最近的网格点?

How to find the closest grid point, for any given point, in a 3D grid that has been rotated around the origin?

给定一个步长为 1 的无限规则 3D 网格,它与所有三个空间轴对齐。如果我选择任意点,找到最近的网格点很容易:我只需将向量的每个分量四舍五入到最近的步长。

但是,如果我围绕原点旋转整个网格,这个问题似乎更难解决。当网格与全球坐标系/世界不对齐时,我如何找到任何点的最近网格点space?

我想要这个用于我的 Unity 项目。因此,如果这在 C# 中可行,那就太棒了。对可能方法的一般解释也很好。

正如@Berthur 和@user3386109 在评论中指出的那样,解决方案非常简单,只需对给定点应用反向网格旋转,将矢量分量捕捉到网格,然后将结果旋转回来。谢谢。

这是解决方案的 Unity C# 片段:

point = Quaternion.Inverse(gridRotation) * point;
point.x = Snapping.Snap(point.x, grid.stepSize);
point.y = Snapping.Snap(point.y, grid.stepSize);
point.z = Snapping.Snap(point.z, grid.stepSize);
point = gridRotation * point;