generated from max/template-unity-project
Simple grid generation and loading
- Grid generation - Grid loading - GridIds - Still having issues with serialization of the grid data structure
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VertexColor.ScenePartition.Editor
|
||||
{
|
||||
[Serializable]
|
||||
[System.Serializable]
|
||||
public class SceneGrid
|
||||
{
|
||||
[SerializeField]
|
||||
@ -13,25 +12,42 @@ namespace VertexColor.ScenePartition.Editor
|
||||
[SerializeField]
|
||||
private SceneGridDictionary grid = new SceneGridDictionary();
|
||||
|
||||
public void Insert(uint id, Vector2 point)
|
||||
public SceneGridDictionary Grid => grid;
|
||||
|
||||
public void Insert(uint id, Vector3 point)
|
||||
{
|
||||
Vector2 gridPos = CalculateGridPosition(point);
|
||||
if (grid.TryGetValue(gridPos, out List<uint> ids))
|
||||
int gridId = CalculateGridPosition(point);
|
||||
if (grid.TryGetValue(gridId, out List<uint> ids))
|
||||
{
|
||||
ids.Add(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
grid.Add(gridPos, new List<uint> { id });
|
||||
grid.Add(gridId, new List<uint> { id });
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 CalculateGridPosition(Vector2 point)
|
||||
public int CalculateGridPosition(Vector3 point)
|
||||
{
|
||||
int x = Mathf.FloorToInt(point.x / cellSize);
|
||||
int y = Mathf.FloorToInt(point.y / cellSize);
|
||||
int z = Mathf.FloorToInt(point.z / cellSize);
|
||||
|
||||
return new Vector2(x, y);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VertexColor.ScenePartition
|
||||
{
|
||||
@ -10,5 +9,5 @@ namespace VertexColor.ScenePartition
|
||||
public class UintSortedSet : SerializableSortedSet<uint> { }
|
||||
|
||||
[System.Serializable]
|
||||
public class SceneGridDictionary : SerializableDictionary<Vector2, List<uint>> { }
|
||||
public class SceneGridDictionary : SerializableDictionary<int, List<uint>> { }
|
||||
}
|
Reference in New Issue
Block a user