2023-06-26 01:03:14 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace VertexColor.ScenePartition.Editor
|
|
|
|
{
|
2023-06-28 01:07:27 +02:00
|
|
|
[System.Serializable]
|
2023-06-26 01:03:14 +02:00
|
|
|
public class SceneGrid
|
|
|
|
{
|
|
|
|
[SerializeField]
|
|
|
|
private int cellSize = 10;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private SceneGridDictionary grid = new SceneGridDictionary();
|
|
|
|
|
2023-06-28 01:07:27 +02:00
|
|
|
public SceneGridDictionary Grid => grid;
|
|
|
|
|
|
|
|
public void Insert(uint id, Vector3 point)
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
2023-06-28 01:07:27 +02:00
|
|
|
int gridId = CalculateGridPosition(point);
|
|
|
|
if (grid.TryGetValue(gridId, out List<uint> ids))
|
2023-06-26 01:03:14 +02:00
|
|
|
{
|
|
|
|
ids.Add(id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-28 01:07:27 +02:00
|
|
|
grid.Add(gridId, new List<uint> { id });
|
2023-06-26 01:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-28 01:07:27 +02:00
|
|
|
public int CalculateGridPosition(Vector3 point)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|