2023-06-26 01:03:14 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
2023-07-03 00:47:53 +02:00
|
|
|
namespace VertexColor.ScenePartition
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
2023-06-28 01:07:27 +02:00
|
|
|
[System.Serializable]
|
2023-06-26 01:03:14 +02:00
|
|
|
public class SceneGrid
|
|
|
|
{
|
|
|
|
[SerializeField]
|
2023-07-04 23:34:54 +02:00
|
|
|
public int cellSize = 10;
|
2023-06-26 01:03:14 +02:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private SceneGridDictionary grid = new SceneGridDictionary();
|
|
|
|
|
2023-06-28 01:07:27 +02:00
|
|
|
public SceneGridDictionary Grid => grid;
|
|
|
|
|
2023-07-03 23:35:29 +02:00
|
|
|
public void Insert(ulong scenePartitionId, Vector3 point)
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
2023-06-29 00:20:01 +02:00
|
|
|
int gridId = CalculateGridPosition(point, cellSize);
|
2023-07-03 23:35:29 +02:00
|
|
|
if (grid.TryGetValue(gridId, out GridList ids))
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
2023-07-03 23:35:29 +02:00
|
|
|
ids.list.Add(scenePartitionId);
|
2023-06-26 01:03:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-03 23:35:29 +02:00
|
|
|
var l = new GridList();
|
|
|
|
l.list.Add(scenePartitionId);
|
|
|
|
grid.Add(gridId, l);
|
2023-06-26 01:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 00:20:01 +02:00
|
|
|
public static int CalculateGridPosition(Vector3 point, int cellSize)
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
|
|
|
int x = Mathf.FloorToInt(point.x / cellSize);
|
2023-06-28 01:07:27 +02:00
|
|
|
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;
|
2023-06-26 01:03:14 +02:00
|
|
|
|
2023-06-28 01:07:27 +02:00
|
|
|
return (x, y);
|
2023-06-26 01:03:14 +02:00
|
|
|
}
|
2023-06-30 03:28:44 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-07-03 23:35:29 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-06-26 01:03:14 +02:00
|
|
|
}
|
2023-07-04 23:34:54 +02:00
|
|
|
}
|