90 lines
2.7 KiB
C#
90 lines
2.7 KiB
C#
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<InstanceInfo> loadedInstances = new List<InstanceInfo>();
|
|
}
|
|
|
|
internal static int nextStreamerId = 1;
|
|
protected readonly int streamerInstanceId = Interlocked.Increment(ref nextStreamerId);
|
|
|
|
public readonly HashSet<int> owners = new HashSet<int>();
|
|
public Dictionary<int, HashSet<uint>> objectInstanceIds = new Dictionary<int, HashSet<uint>>();
|
|
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<uint>());
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|