using System.Collections.Generic; using System.Threading; using UnityEngine; namespace Assets.ThoMagic.Renderer { public class InstanceStreamer { public struct InstanceInfo { public int instanceId; public int objectId; } public class Cell { public float CenterX; public float CenterZ; public bool IsLoaded; public bool IsLoading; public int inRangeOfAnyCamera; public List loadedInstances = new List(); } internal static int nextStreamerId = 1; protected readonly int streamerInstanceId = Interlocked.Increment(ref nextStreamerId); public readonly HashSet owners = new HashSet(); public Dictionary> objectInstanceIds = new Dictionary>(); public virtual int AddInstance(int objectId, Vector3 pos, Quaternion orientation, float scaleXZ, float scaleY) { var instanceId = RendererPool.AddInstance(objectId, pos, orientation, scaleXZ, scaleY); if (!objectInstanceIds.ContainsKey(objectId)) objectInstanceIds.Add(objectId, new HashSet()); objectInstanceIds[objectId].Add((uint)instanceId); return instanceId; } public virtual void RemoveInstance(int objectId, int instanceId, bool noRemove = false) { if (noRemove) { RendererPool.RemoveInstance(objectId, instanceId); } else { if (!objectInstanceIds.ContainsKey(objectId)) return; if (objectInstanceIds[objectId].Remove((uint)instanceId)) RendererPool.RemoveInstance(objectId, instanceId); } } public void Clear() { foreach (var obj in objectInstanceIds) { if (!objectInstanceIds.ContainsKey(obj.Key)) continue; foreach (var id in obj.Value) { RemoveInstance(obj.Key, (int)id, true); } } objectInstanceIds.Clear(); } public virtual void UpdateForCamera(Camera camera, Plane[] planes) { } public virtual bool IsInRange(Camera camera, Plane[] planes) { return true; } public override int GetHashCode() { return streamerInstanceId.GetHashCode();//HashCode.Combine(streamerInstanceId, objectInstanceIds.Count); } } }