Help fix angled surface grid snap

Hello!

I’m having issues with my grid snapping system when it comes to angled surfaces. It works on every surface except ones rotated on more than 1 axis/with a full surface normal Ex (0.5, 0.7, 0.3).

I have an alternative which is to rotate the position around the object to face (0, 0, 0) snap it on two axis and rotate it back but I’m scared it would be too uneficient as in my benchmark it was 100 times slower to just rotate the position with predefined values.

Currently, I’m using this formula to snap the secondary axis back on the surface after snapping the primary one.

SnappedPosition[Axis1] = SnapNumber(Position[Axis1], 0.25) -- Snaps the primary axis
SnappedPosition[Axis2] += Normal[Axis1] * ((Position[Axis1] - SnappedPosition[Axis1]) * (1 / Normal[Axis2])) -- Use the surface normal and the difference between the primary axis and its snapped counterpart to snap the secondary axis on the surface

I tried using the same technic with surfaces rotated on more than 1 axis but it created weird behaviour.

Could someone help me solve this issue?

Thanks !