ScenePartition/Runtime/SceneGrid.cs
max 8b44c33813 Editor layout
- Cleanup the editors a bit
- Editor foldout scope
- Cell size to editor (this is user preference data, could be put in EditorPrefs later)
2023-07-09 23:01:48 +02:00

81 lines
2.2 KiB
C#

using UnityEngine;
namespace VertexColor.ScenePartition
{
[System.Serializable]
public class SceneGrid
{
[SerializeField]
private SceneGridDictionary grid = new SceneGridDictionary();
public SceneGridDictionary Grid => grid;
public void Insert(ulong scenePartitionId, Vector3 point, int cellSize)
{
int gridId = CalculateGridPosition(point, cellSize);
if (grid.TryGetValue(gridId, out GridList ids))
{
ids.list.Add(scenePartitionId);
}
else
{
var l = new GridList();
l.list.Add(scenePartitionId);
grid.Add(gridId, l);
}
}
public static int CalculateGridPosition(Vector3 point, int cellSize)
{
int x = Mathf.FloorToInt(point.x / cellSize);
int z = Mathf.FloorToInt(point.z / cellSize);
return IntPairToInt(x, z);
}
public static int IntPairToInt(int x, int y)
{
// Combine x and y components into a single int
return (x << 16) | (ushort)y;
}
public static (int, int) IntToIntPair(int value)
{
// Extract x and y components from the combined int
int x = value >> 16;
int y = (short)value;
return (x, y);
}
public static long LongPairToLong(long x, long y)
{
// Combine x and y components into a single long
return (x << 32) | (uint)y;
}
public static (long, long) LongToLongPair(long value)
{
// Extract x and y components from the combined long
long x = value >> 32;
long y = (uint)value;
return (x, y);
}
public static ulong ULongPairToULong(ulong x, ulong y)
{
// Combine x and y components into a single ulong
return (x << 32) | (uint)y;
}
public static (ulong, ulong) ULongToULongPair(ulong value)
{
// Extract x and y components from the combined ulong
ulong x = value >> 32;
ulong y = (uint)value;
return (x, y);
}
}
}