long ids but not long enough!

- Object and prefab file id loading
- Generate grid update
- Switched uint to long but should probably be ulong!
This commit is contained in:
max 2023-06-30 03:28:44 +02:00
parent 1556f95485
commit d225f897ba
7 changed files with 152 additions and 63 deletions

View File

@ -39,15 +39,17 @@ private void DrawSceneDataCache()
foreach (KeyValuePair<ScenePartitionSO, ScenePartitionData> sceneData in scenePartitionSS.SceneDataCache)
{
if (sceneData.Key == null || sceneData.Value == null) continue;
EditorGUILayout.LabelField($"{sceneData.Key.name}");
if (sceneData.Value.HasCreatedPartitions)
{
EditorGUILayout.LabelField($"scenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in sceneData.Value.ScenePartitions)
foreach (KeyValuePair<long, ScenePartition> scenePartition in sceneData.Value.ScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
EditorGUILayout.LongField(scenePartition.Key);
}
}
@ -57,9 +59,9 @@ private void DrawSceneDataCache()
{
EditorGUILayout.LabelField($"loadedScenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in sceneData.Value.LoadedScenePartitions)
foreach (KeyValuePair<long, ScenePartition> scenePartition in sceneData.Value.LoadedScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
EditorGUILayout.LongField(scenePartition.Value.id);
}
}
@ -67,15 +69,15 @@ private void DrawSceneDataCache()
{
EditorGUILayout.LabelField($"generatedSceneGrid");
foreach (KeyValuePair<int, List<uint>> item in sceneData.Value.SceneGrid.Grid)
foreach (KeyValuePair<int, List<long>> item in sceneData.Value.SceneGrid.Grid)
{
EditorGUILayout.IntField("gridId", item.Key);
EditorGUI.indentLevel++;
foreach (uint id in item.Value)
foreach (long id in item.Value)
{
EditorGUILayout.IntField((int)id);
EditorGUILayout.LongField(id);
}
EditorGUI.indentLevel--;

View File

@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.SceneManagement;
@ -16,7 +15,7 @@ public class ScenePartitionSO : ScriptableObject
public SceneAsset SceneAsset { get; private set; } = null;
public string SceneName => SceneAsset == null ? name : SceneAsset.name;
public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 };
public List<long> alwaysLoadIds = new List<long> { 0, 1, 2, 3, 4 };
public ScenePartitionData Data
{
@ -54,7 +53,7 @@ public void CreateScene()
public void LoadAll()
{
CreateScenePartitions();
SortedSet<uint> ids = new SortedSet<uint>(Data.ScenePartitions.Keys);
SortedSet<long> ids = new SortedSet<long>(Data.ScenePartitions.Keys);
LoadScenePartitions(ids);
}
@ -64,7 +63,7 @@ public void LoadAll()
public void Reload()
{
if (!Data.HasLoadedPartitions) return;
LoadScenePartitions(new SortedSet<uint>(Data.LoadedScenePartitions.Keys));
LoadScenePartitions(new SortedSet<long>(Data.LoadedScenePartitions.Keys));
}
private void CreateScenePartitions()
@ -81,7 +80,7 @@ private void CreateScenePartitions()
}
}
private void LoadScenePartitions(SortedSet<uint> partitionIds)
private void LoadScenePartitions(SortedSet<long> partitionIds)
{
if (!Data.HasCreatedPartitions) return;
@ -91,17 +90,15 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
Data.LoadedScenePartitions.Clear();
// Add default ids.
for (int i = 0; i < alwaysLoadIds.Count; i++)
// Add always load ids.
SortedSet<long> baseIds = GetAlwaysLoadIds();
foreach (var id in baseIds)
{
if (Data.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
{
partitionIds.Add(alwaysLoadIds[i]);
}
partitionIds.Add(id);
}
// Create scene data.
foreach (uint id in partitionIds)
foreach (long id in partitionIds)
{
ScenePartition p = Data.ScenePartitions[id];
sceneData.AddRange(File.ReadAllLines(p.filePath));
@ -181,7 +178,7 @@ private void DeleteLoadedPartitions()
{
if (!Data.HasLoadedPartitions) return;
foreach (KeyValuePair<uint, ScenePartition> scenePartition in Data.LoadedScenePartitions)
foreach (KeyValuePair<long, ScenePartition> scenePartition in Data.LoadedScenePartitions)
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
@ -189,15 +186,14 @@ private void DeleteLoadedPartitions()
}
}
public void LoadPartitions(uint[] ids)
public void LoadPartitions(long[] ids)
{
SortedSet<uint> partitionIds = new SortedSet<uint>();
SortedSet<long> partitionIds = new SortedSet<long>();
for (int i = 0; i < ids.Length; i++)
{
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (uint c in connections)
SortedSet<long> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (long c in connections)
{
partitionIds.Add(c);
}
@ -206,36 +202,90 @@ public void LoadPartitions(uint[] ids)
LoadScenePartitions(partitionIds);
}
private SortedSet<long> GetAlwaysLoadIds()
{
SortedSet<long> partitionIds = new SortedSet<long>();
foreach (long id in alwaysLoadIds)
{
SortedSet<long> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, id);
foreach (long c in connections)
{
partitionIds.Add(c);
}
}
return partitionIds;
}
public void GenerateSceneGridData()
{
if (!Data.HasCreatedPartitions) return;
LoadAll();
if (!Data.HasCreatedPartitions) return;
Scene scene = EditorSceneManager.OpenScene(ScenePartitionUtils.GetScenePath(this), OpenSceneMode.Single);
GameObject[] rootGameObjects = scene.GetRootGameObjects();
Data.SceneGrid.Grid.Clear();
// Maybe later switch to getting the data from disk instead of loading the scene and then unloading it again.
foreach (GameObject gameObject in rootGameObjects)
{
// https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/
PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
SerializedObject serializedObject = new SerializedObject(gameObject.transform);
inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
//// Maybe later switch to getting the data from disk instead of loading the scene and then unloading it again.
//foreach (GameObject gameObject in rootGameObjects)
//{
// // https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/
// PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
// SerializedObject serializedObject = new SerializedObject(gameObject.transform);
// inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
// SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
uint localId = (uint)localIdProp.intValue;
// long localId = localIdProp.longValue;
if (Data.ScenePartitions.ContainsKey(localId))
// if (localId == 0)
// {
// if (PrefabUtility.IsPartOfPrefabInstance(gameObject))
// {
// GlobalObjectId id = GlobalObjectId.GetGlobalObjectIdSlow(gameObject); // We could use this funtion for all objects. Might be a bit slower but is also simple.
// localId = long.Parse(id.targetPrefabId.ToString());
// Debug.Log($"{id.assetGUID} | {id.identifierType} | {id.targetObjectId} | {id.targetPrefabId}");
// if (id.targetObjectId == 0 && id.targetPrefabId == 0)
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// else
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// if (!Data.ScenePartitions.ContainsKey(localId))
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// Data.SceneGrid.Insert(localId, gameObject.transform.position);
//}
GlobalObjectId[] ids = new GlobalObjectId[rootGameObjects.Length];
GlobalObjectId.GetGlobalObjectIdsSlow(rootGameObjects, ids);
for (int i = 0; i < ids.Length; i++)
{
Data.SceneGrid.Insert(localId, gameObject.transform.position);
Debug.Log($"{ids[i].assetGUID} | {ids[i].identifierType} | {ids[i].targetObjectId} | {ids[i].targetPrefabId}");
if (ids[i].targetPrefabId == 0) // 0 = no prefab.
{
Data.SceneGrid.Insert((long)ids[i].targetObjectId, rootGameObjects[i].transform.position);
}
else
{
Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
Data.SceneGrid.Insert((long)ids[i].targetPrefabId, rootGameObjects[i].transform.position);
}
}
@ -244,10 +294,15 @@ public void GenerateSceneGridData()
public void LoadCell(int gridId)
{
if (Data.SceneGrid.Grid.TryGetValue(gridId, out List<uint> ids))
if (Data.SceneGrid.Grid.TryGetValue(gridId, out List<long> ids))
{
LoadPartitions(ids.ToArray());
}
}
public void ClearCache()
{
data = null;
}
}
}

View File

@ -7,7 +7,7 @@ namespace VertexColor.ScenePartition.Editor
[CustomEditor(typeof(ScenePartitionSO))]
public class ScenePartitionSOEditor : UnityEditor.Editor
{
private int id = 0;
private long id = 0;
private int gridId = 0;
public override void OnInspectorGUI()
@ -60,11 +60,11 @@ public override void OnInspectorGUI()
EditorGUILayout.Space();
id = EditorGUILayout.IntField("id", id);
id = EditorGUILayout.LongField("id", id);
if (GUILayout.Button("Load Section"))
{
scenePartitionSO.LoadPartitions(new uint[1] { (uint)id });
scenePartitionSO.LoadPartitions(new long[1] { (long)id });
}
if (GUILayout.Button("GenerateSceneGrid"))
@ -83,20 +83,26 @@ public override void OnInspectorGUI()
{
EditorGUILayout.LabelField($"generatedSceneGrid");
foreach (KeyValuePair<int, List<uint>> item in scenePartitionSO.Data.SceneGrid.Grid)
foreach (KeyValuePair<int, List<long>> item in scenePartitionSO.Data.SceneGrid.Grid)
{
EditorGUILayout.IntField("gridId", item.Key);
EditorGUILayout.LongField("gridId", item.Key);
EditorGUI.indentLevel++;
foreach (uint id in item.Value)
foreach (long id in item.Value)
{
EditorGUILayout.IntField((int)id);
EditorGUILayout.LongField(id);
}
EditorGUI.indentLevel--;
}
}
if (GUILayout.Button("ClearCache"))
{
scenePartitionSO.ClearCache();
ScenePartitionSS.instance.SceneDataCache.Clear();
}
}
}
}

View File

@ -26,26 +26,26 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
return scenePath;
}
public static SortedSet<uint> FindDeeplyLinkedObjects(SortedList<uint, ScenePartition> scenePartitions, uint partitionId)
public static SortedSet<long> FindDeeplyLinkedObjects(SortedList<long, ScenePartition> scenePartitions, long partitionId)
{
SortedSet<uint> linkedObjects = new SortedSet<uint>();
SortedSet<long> linkedObjects = new SortedSet<long>();
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
return linkedObjects;
}
private static void FindDeeplyLinkedObjectsRecursive(SortedList<uint, ScenePartition> scenePartitions, uint partitionId, SortedSet<uint> linkedObjects)
private static void FindDeeplyLinkedObjectsRecursive(SortedList<long, ScenePartition> scenePartitions, long partitionId, SortedSet<long> linkedObjects)
{
if (linkedObjects.Contains(partitionId)) return;
if (!scenePartitions.TryGetValue(partitionId, out ScenePartition partition)) return;
linkedObjects.Add(partitionId);
foreach (uint reference in partition.references)
foreach (long reference in partition.references)
{
FindDeeplyLinkedObjectsRecursive(scenePartitions, reference, linkedObjects);
if (scenePartitions.TryGetValue(reference, out ScenePartition referencedPartition))
{
foreach (uint subReference in referencedPartition.references)
foreach (long subReference in referencedPartition.references)
{
FindDeeplyLinkedObjectsRecursive(scenePartitions, subReference, linkedObjects);
}

View File

@ -14,16 +14,16 @@ public class SceneGrid
public SceneGridDictionary Grid => grid;
public void Insert(uint id, Vector3 point)
public void Insert(long scenePartitionId, Vector3 point)
{
int gridId = CalculateGridPosition(point, cellSize);
if (grid.TryGetValue(gridId, out List<uint> ids))
if (grid.TryGetValue(gridId, out List<long> ids))
{
ids.Add(id);
ids.Add(scenePartitionId);
}
else
{
grid.Add(gridId, new List<uint> { id });
grid.Add(gridId, new List<long> { scenePartitionId });
}
}
@ -49,5 +49,20 @@ public static (int, int) IntToIntPair(int 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);
}
}
}

View File

@ -24,10 +24,11 @@ public int Compare(ScenePartition x, ScenePartition y)
[System.Serializable]
public class ScenePartition
{
public uint id = 0;
public long id = 0;
//public long classId = 0;
public string filePath = null;
//public string[] data = null;
public UintSortedSet references = new UintSortedSet();
public LongSortedSet references = new LongSortedSet();
public ScenePartition(string filePath)
{
@ -44,9 +45,19 @@ public ScenePartition(string filePath)
Match match = Regex.Match(data[0], pattern);
if (!match.Success) return;
if (!uint.TryParse(match.Groups[1].Value, out id)) return;
if (!long.TryParse(match.Groups[1].Value, out id)) return;
}
//{ // Get class id.
// string pattern = @"!u!(\d+)";
// // Find all matches using regex
// Match match = Regex.Match(data[0], pattern);
// if (!match.Success) return;
// if (!long.TryParse(match.Groups[1].Value, out classId)) return;
//}
{ // Get references.
string pattern = @"fileID:\s(\d+)";
@ -57,7 +68,7 @@ public ScenePartition(string filePath)
if (!match.Success) continue;
if (uint.TryParse(match.Groups[1].Value, out uint fileNumber))
if (long.TryParse(match.Groups[1].Value, out long fileNumber))
{
if (fileNumber == 0) continue; // 0 == nothing.

View File

@ -3,11 +3,11 @@
namespace VertexColor.ScenePartition
{
[System.Serializable]
public class ScenePartitionSortedList : SerializableSortedList<uint, ScenePartition> { }
public class ScenePartitionSortedList : SerializableSortedList<long, ScenePartition> { }
[System.Serializable]
public class UintSortedSet : SerializableSortedSet<uint> { }
public class LongSortedSet : SerializableSortedSet<long> { }
[System.Serializable]
public class SceneGridDictionary : SerializableDictionary<int, List<uint>> { }
public class SceneGridDictionary : SerializableDictionary<int, List<long>> { }
}