ScenePartition/Runtime/SceneGrid.cs
max d225f897ba long ids but not long enough!
- Object and prefab file id loading
- Generate grid update
- Switched uint to long but should probably be ulong!
2023-06-30 03:28:44 +02:00

69 lines
1.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[System.Serializable]
public class SceneGrid
{
[SerializeField]
private int cellSize = 10;
[SerializeField]
private SceneGridDictionary grid = new SceneGridDictionary();
public SceneGridDictionary Grid => grid;
public void Insert(long scenePartitionId, Vector3 point)
{
int gridId = CalculateGridPosition(point, cellSize);
if (grid.TryGetValue(gridId, out List<long> ids))
{
ids.Add(scenePartitionId);
}
else
{
grid.Add(gridId, new List<long> { scenePartitionId });
}
}
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);
}
}
}