2026-05-07 10:14:44 +02:00

54 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Wing.Utils;
namespace Wing.Gizmos
{
public class GridSnap : SnapBase
{
public Vector3 Center = Vector3.zero;
public GridSnap()
{
Precision = 1;
}
protected override void CalcTolerance()
{
mTolerance = 0.2f * Precision;
}
public override bool Snap(Vector3 inVal, ref Vector3 outVal, bool force = false)
{
if (!Enable)
{
return false;
}
Vector3 position = inVal;
Vector3 offset = position - Center;
Vector3 dp = GemetryHelper.Vector3Mod(offset, Precision);
Vector3 df = position - dp;
df.x = Mathf.Abs(df.x);
df.y = Mathf.Abs(df.y);
df.z = Mathf.Abs(df.z);
float max = Mathf.Max(df.x, df.y, df.z);
if (force || max <= mTolerance)
{
outVal = dp;
return true;
}
else
{
return false;
}
}
}
}