mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-14 00:40:30 +07:00
quad tree for visibility wip
This commit is contained in:
@@ -3,6 +3,22 @@
|
||||
namespace IsoTools.Internal {
|
||||
public class IsoGrid<T> {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Interfaces
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public interface IItemAdapter {
|
||||
IsoRect GetBounds (T item);
|
||||
void SetMinMaxCells (T item, IsoPoint2 min, IsoPoint2 max);
|
||||
void GetMinMaxCells (T item, ref IsoPoint2 min, ref IsoPoint2 max);
|
||||
}
|
||||
|
||||
public interface ILookUpper {
|
||||
void LookUp(IsoList<T> items);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// CellPool
|
||||
@@ -26,22 +42,6 @@ namespace IsoTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Interfaces
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public interface IItemAdapter {
|
||||
IsoRect GetBounds (T item);
|
||||
void SetMinMaxCells (T item, IsoPoint2 min, IsoPoint2 max);
|
||||
void GetMinMaxCells (T item, ref IsoPoint2 min, ref IsoPoint2 max);
|
||||
}
|
||||
|
||||
public interface ILookUpper {
|
||||
void LookUp(IsoList<T> items);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Members
|
||||
@@ -79,8 +79,8 @@ namespace IsoTools.Internal {
|
||||
_itemAdapter = item_adapter;
|
||||
}
|
||||
|
||||
public void AddItem(T content) {
|
||||
_gridItems.Add(content);
|
||||
public void AddItem(T item) {
|
||||
_gridItems.Add(item);
|
||||
}
|
||||
|
||||
public void ClearItems() {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
|
||||
namespace IsoTools.Internal {
|
||||
namespace IsoTools.Internal {
|
||||
public class IsoList<T> {
|
||||
T[] _data;
|
||||
int _size;
|
||||
@@ -15,7 +13,7 @@ namespace IsoTools.Internal {
|
||||
|
||||
public IsoList(int capacity) {
|
||||
if ( capacity < 0 ) {
|
||||
throw new ArgumentOutOfRangeException(
|
||||
throw new System.ArgumentOutOfRangeException(
|
||||
"capacity", "capacity must be >= 0");
|
||||
} else if ( capacity == 0 ) {
|
||||
_data = _emptyData;
|
||||
@@ -31,7 +29,7 @@ namespace IsoTools.Internal {
|
||||
var new_capacity = _size == 0
|
||||
? _defaultCapacity : _size * 2;
|
||||
var new_data = new T[new_capacity];
|
||||
Array.Copy(_data, new_data, _size);
|
||||
System.Array.Copy(_data, new_data, _size);
|
||||
_data = new_data;
|
||||
}
|
||||
_data[_size++] = value;
|
||||
@@ -39,7 +37,7 @@ namespace IsoTools.Internal {
|
||||
|
||||
public T Pop() {
|
||||
if ( _size == 0 ) {
|
||||
throw new InvalidOperationException("empty list");
|
||||
throw new System.InvalidOperationException("empty list");
|
||||
}
|
||||
var last = _data[--_size];
|
||||
_data[_size] = default(T);
|
||||
@@ -48,19 +46,19 @@ namespace IsoTools.Internal {
|
||||
|
||||
public T Peek() {
|
||||
if ( _size == 0 ) {
|
||||
throw new InvalidOperationException("empty list");
|
||||
throw new System.InvalidOperationException("empty list");
|
||||
}
|
||||
return _data[_size - 1];
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
Array.Clear(_data, 0, _size);
|
||||
System.Array.Clear(_data, 0, _size);
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
public void UnorderedRemoveAt(int index) {
|
||||
if ( (uint)index >= (uint)_size ) {
|
||||
throw new IndexOutOfRangeException();
|
||||
throw new System.IndexOutOfRangeException();
|
||||
}
|
||||
_data[index] = _data[--_size];
|
||||
_data[_size] = default(T);
|
||||
@@ -69,13 +67,13 @@ namespace IsoTools.Internal {
|
||||
public T this[int index] {
|
||||
get {
|
||||
if ( (uint)index >= (uint)_size ) {
|
||||
throw new IndexOutOfRangeException();
|
||||
throw new System.IndexOutOfRangeException();
|
||||
}
|
||||
return _data[index];
|
||||
}
|
||||
set {
|
||||
if ( (uint)index >= (uint)_size ) {
|
||||
throw new IndexOutOfRangeException();
|
||||
throw new System.IndexOutOfRangeException();
|
||||
}
|
||||
_data[index] = value;
|
||||
}
|
||||
@@ -89,12 +87,12 @@ namespace IsoTools.Internal {
|
||||
get { return _data.Length; }
|
||||
set {
|
||||
if ( value < _size ) {
|
||||
throw new ArgumentOutOfRangeException("value");
|
||||
throw new System.ArgumentOutOfRangeException("value");
|
||||
} else if ( value != _data.Length ) {
|
||||
if ( value > 0 ) {
|
||||
var new_data = new T[value];
|
||||
if ( _size > 0 ) {
|
||||
Array.Copy(_data, new_data, _size);
|
||||
System.Array.Copy(_data, new_data, _size);
|
||||
}
|
||||
_data = new_data;
|
||||
} else {
|
||||
|
||||
@@ -69,9 +69,13 @@ namespace IsoTools.Internal {
|
||||
}
|
||||
|
||||
public static IsoMinMax Merge(IsoMinMax a, IsoMinMax b) {
|
||||
return new IsoMinMax(
|
||||
a.min < b.min ? a.min : b.min,
|
||||
a.max > b.max ? a.max : b.max);
|
||||
if ( a.min > b.min ) {
|
||||
a.min = b.min;
|
||||
}
|
||||
if ( a.max < b.max ) {
|
||||
a.max = b.max;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,15 +38,21 @@
|
||||
}
|
||||
|
||||
public static IsoPoint2 operator+(IsoPoint2 a, IsoPoint2 b) {
|
||||
return new IsoPoint2(a.x + b.x, a.y + b.y);
|
||||
a.x += b.x;
|
||||
a.y += b.y;
|
||||
return a;
|
||||
}
|
||||
|
||||
public static IsoPoint2 operator-(IsoPoint2 a, IsoPoint2 b) {
|
||||
return new IsoPoint2(a.x - b.x, a.y - b.y);
|
||||
a.x -= b.x;
|
||||
a.y -= b.y;
|
||||
return a;
|
||||
}
|
||||
|
||||
public static IsoPoint2 operator-(IsoPoint2 a) {
|
||||
return new IsoPoint2(-a.x, -a.y);
|
||||
a.x = -a.x;
|
||||
a.y = -a.y;
|
||||
return a;
|
||||
}
|
||||
|
||||
public static bool operator==(IsoPoint2 lhs, IsoPoint2 rhs) {
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
namespace IsoTools.Internal {
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IsoTools.Internal {
|
||||
public class IsoQuadTree<T> {
|
||||
|
||||
const int MinChildCountPerNode = 3;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// IItem
|
||||
// Interfaces
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public interface IItem {
|
||||
public interface IBoundsLookUpper {
|
||||
void LookUp(IsoRect bounds);
|
||||
}
|
||||
|
||||
//
|
||||
// Item
|
||||
//
|
||||
public interface IContentLookUpper {
|
||||
void LookUp(T content);
|
||||
}
|
||||
|
||||
class Item : IItem {
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// ItemPool
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
class Item {
|
||||
public Node Owner = null;
|
||||
public IsoRect Bounds = IsoRect.zero;
|
||||
public T Content = default(T);
|
||||
@@ -31,137 +40,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Node
|
||||
//
|
||||
|
||||
class Node {
|
||||
public Node Parent = null;
|
||||
public IsoRect Bounds = IsoRect.zero;
|
||||
public Node[] Children = new Node[4];
|
||||
public IsoAssocList<Item> Contents = new IsoAssocList<Item>(MinChildCountPerNode);
|
||||
|
||||
public Node Init(Node parent, IsoRect bounds) {
|
||||
Parent = parent;
|
||||
Bounds = bounds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Node Clear(IsoIPool<Node> node_pool, IsoIPool<Item> item_pool) {
|
||||
ClearChildren(node_pool, item_pool);
|
||||
ClearContents(item_pool);
|
||||
return Init(null, IsoRect.zero);
|
||||
}
|
||||
|
||||
public bool Insert(
|
||||
IsoRect bounds, T content, out Item item,
|
||||
IsoIPool<Node> node_pool, IsoIPool<Item> item_pool)
|
||||
{
|
||||
if ( !Bounds.Contains(bounds) ) {
|
||||
item = null;
|
||||
return false;
|
||||
}
|
||||
bool has_any_children = false;
|
||||
for ( int i = 0, e = Children.Length; i < e; ++i ) {
|
||||
if ( Children[i] != null ) {
|
||||
has_any_children = true;
|
||||
if ( Children[i].Insert(bounds, content, out item, node_pool, item_pool) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( has_any_children || Contents.Count >= MinChildCountPerNode ) {
|
||||
for ( int i = 0, e = Children.Length; i < e; ++i ) {
|
||||
if ( Children[i] == null ) {
|
||||
var child_bounds = GetChildBounds(i);
|
||||
if ( child_bounds.Contains(bounds) ) {
|
||||
Children[i] = node_pool.Take().Init(this, child_bounds);
|
||||
if ( Children[i].Insert(bounds, content, out item, node_pool, item_pool) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
item = item_pool.Take().Init(this, bounds, content);
|
||||
Contents.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void VisitAllBounds(System.Action<IsoRect> act) {
|
||||
act(Bounds);
|
||||
for ( int i = 0, e = Children.Length; i < e; ++i ) {
|
||||
if ( Children[i] != null ) {
|
||||
Children[i].VisitAllBounds(act);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void VisitItemsByBounds(IsoRect bounds, System.Action<T> act) {
|
||||
if ( Bounds.Overlaps(bounds) ) {
|
||||
for ( int i = 0, e = Contents.Count; i < e; ++i ) {
|
||||
act(Contents[i].Content);
|
||||
}
|
||||
for ( int i = 0, e = Children.Length; i < e; ++i ) {
|
||||
if ( Children[i] != null ) {
|
||||
Children[i].VisitItemsByBounds(bounds, act);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Private
|
||||
//
|
||||
|
||||
IsoRect GetChildBounds(int index) {
|
||||
var size = Bounds.size * 0.5f;
|
||||
var center = Bounds.center;
|
||||
switch ( index ) {
|
||||
case 0: { // LT
|
||||
return new IsoRect(center - size, center);
|
||||
}
|
||||
case 1: { // RT
|
||||
var rect = new IsoRect(center - size, center);
|
||||
rect.Translate(size.x, 0.0f);
|
||||
return rect;
|
||||
}
|
||||
case 2: { // LB
|
||||
var rect = new IsoRect(center, center + size);
|
||||
rect.Translate(-size.x, 0.0f);
|
||||
return rect;
|
||||
}
|
||||
case 3: { // RB
|
||||
return new IsoRect(center, center + size);
|
||||
}
|
||||
default:
|
||||
return IsoRect.zero;
|
||||
}
|
||||
}
|
||||
|
||||
void ClearChildren(IsoIPool<Node> node_pool, IsoIPool<Item> item_pool) {
|
||||
for ( int i = 0, e = Children.Length; i < e; ++i ) {
|
||||
if ( Children[i] != null ) {
|
||||
node_pool.Release(
|
||||
Children[i].Clear(node_pool, item_pool));
|
||||
}
|
||||
}
|
||||
System.Array.Clear(Children, 0, Children.Length);
|
||||
}
|
||||
|
||||
void ClearContents(IsoIPool<Item> item_pool) {
|
||||
for ( int i = 0, e = Contents.Count; i < e; ++i ) {
|
||||
item_pool.Release(
|
||||
Contents[i].Clear());
|
||||
}
|
||||
Contents.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// ItemPool
|
||||
//
|
||||
|
||||
class ItemPool : IsoPool<Item> {
|
||||
public ItemPool(int capacity) : base(capacity) {
|
||||
}
|
||||
@@ -171,9 +49,147 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// NodePool
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
const int MinChildCountPerNode = 3;
|
||||
|
||||
class Node {
|
||||
public Node[] Nodes = new Node[4];
|
||||
public IsoAssocList<Item> Items = new IsoAssocList<Item>(MinChildCountPerNode);
|
||||
public Node Parent = null;
|
||||
public IsoRect Bounds = IsoRect.zero;
|
||||
public IsoRect[] NodeBounds = new IsoRect[4];
|
||||
|
||||
public Node Init(Node parent, IsoRect bounds) {
|
||||
Parent = parent;
|
||||
Bounds = bounds;
|
||||
return FillNodeBounds();
|
||||
}
|
||||
|
||||
public Node Clear(IsoIPool<Node> node_pool, IsoIPool<Item> item_pool) {
|
||||
ClearNodes(node_pool, item_pool);
|
||||
ClearItems(item_pool);
|
||||
return Init(null, IsoRect.zero);
|
||||
}
|
||||
|
||||
public bool AddItem(
|
||||
IsoRect bounds, T content, out Item item,
|
||||
IsoIPool<Node> node_pool, IsoIPool<Item> item_pool)
|
||||
{
|
||||
if ( !Bounds.Contains(bounds) ) {
|
||||
item = null;
|
||||
return false;
|
||||
}
|
||||
bool has_any_nodes = false;
|
||||
for ( int i = 0, e = Nodes.Length; i < e; ++i ) {
|
||||
var node = Nodes[i];
|
||||
if ( node != null ) {
|
||||
has_any_nodes = true;
|
||||
if ( node.AddItem(bounds, content, out item, node_pool, item_pool) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( has_any_nodes || Items.Count >= MinChildCountPerNode ) {
|
||||
for ( int i = 0, e = Nodes.Length; i < e; ++i ) {
|
||||
var node = Nodes[i];
|
||||
if ( node == null ) {
|
||||
if ( NodeBounds[i].Contains(bounds) ) {
|
||||
Nodes[i] = node = node_pool.Take().Init(this, NodeBounds[i]);
|
||||
if ( node.AddItem(bounds, content, out item, node_pool, item_pool) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
item = item_pool.Take().Init(this, bounds, content);
|
||||
Items.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void RemoveItem(Item item) {
|
||||
Items.Remove(item);
|
||||
}
|
||||
|
||||
public void VisitAllBounds(IBoundsLookUpper look_upper) {
|
||||
look_upper.LookUp(Bounds);
|
||||
for ( int i = 0, e = Nodes.Length; i < e; ++i ) {
|
||||
if ( Nodes[i] != null ) {
|
||||
Nodes[i].VisitAllBounds(look_upper);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void VisitItemsByBounds(IsoRect bounds, IContentLookUpper look_upper) {
|
||||
if ( Bounds.Overlaps(bounds) ) {
|
||||
for ( int i = 0, e = Items.Count; i < e; ++i ) {
|
||||
var item = Items[i];
|
||||
if ( bounds.Overlaps(item.Bounds) ) {
|
||||
look_upper.LookUp(item.Content);
|
||||
}
|
||||
}
|
||||
for ( int i = 0, e = Nodes.Length; i < e; ++i ) {
|
||||
var node = Nodes[i];
|
||||
if ( node != null ) {
|
||||
node.VisitItemsByBounds(bounds, look_upper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Private
|
||||
//
|
||||
|
||||
Node FillNodeBounds() {
|
||||
var size = Bounds.size * 0.5f;
|
||||
var center = Bounds.center;
|
||||
{ // LT
|
||||
var rect = new IsoRect(center - size, center);
|
||||
NodeBounds[0] = rect;
|
||||
}
|
||||
{ // RT
|
||||
var rect = new IsoRect(center - size, center);
|
||||
rect.Translate(size.x, 0.0f);
|
||||
NodeBounds[1] = rect;
|
||||
}
|
||||
{ // LB
|
||||
var rect = new IsoRect(center, center + size);
|
||||
rect.Translate(-size.x, 0.0f);
|
||||
NodeBounds[2] = rect;
|
||||
}
|
||||
{ // RB
|
||||
var rect = new IsoRect(center, center + size);
|
||||
NodeBounds[3] = rect;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
void ClearNodes(IsoIPool<Node> node_pool, IsoIPool<Item> item_pool) {
|
||||
for ( int i = 0, e = Nodes.Length; i < e; ++i ) {
|
||||
var node = Nodes[i];
|
||||
if ( node != null ) {
|
||||
node_pool.Release(
|
||||
node.Clear(node_pool, item_pool));
|
||||
}
|
||||
}
|
||||
System.Array.Clear(Nodes, 0, Nodes.Length);
|
||||
}
|
||||
|
||||
void ClearItems(IsoIPool<Item> item_pool) {
|
||||
for ( int i = 0, e = Items.Count; i < e; ++i ) {
|
||||
var item = Items[i];
|
||||
item_pool.Release(
|
||||
item.Clear());
|
||||
}
|
||||
Items.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
class NodePool : IsoPool<Node> {
|
||||
public NodePool(int capacity) : base(capacity) {
|
||||
@@ -184,86 +200,126 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Members
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
Node _root = null;
|
||||
IsoIPool<Node> _nodePool = null;
|
||||
IsoIPool<Item> _itemPool = null;
|
||||
Node _rootNode = null;
|
||||
Dictionary<T, Item> _allItems = null;
|
||||
IsoIPool<Node> _nodePool = null;
|
||||
IsoIPool<Item> _itemPool = null;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Public
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public IsoQuadTree(int capacity) {
|
||||
_root = null;
|
||||
_rootNode = null;
|
||||
_allItems = new Dictionary<T, Item>(capacity);
|
||||
_nodePool = new NodePool(capacity);
|
||||
_itemPool = new ItemPool(capacity);
|
||||
}
|
||||
|
||||
public IItem Insert(IsoRect bounds, T content) {
|
||||
if ( _root == null ) {
|
||||
var initial_bounds = new IsoRect(
|
||||
bounds.center - bounds.size * 2.0f,
|
||||
bounds.center + bounds.size * 2.0f);
|
||||
_root = _nodePool.Take().Init(null, initial_bounds);
|
||||
public void AddItem(IsoRect bounds, T content) {
|
||||
if ( bounds.x.size > 0.0f && bounds.y.size > 0.0f ) {
|
||||
if ( _allItems.ContainsKey(content) ) {
|
||||
MoveItem(bounds, content);
|
||||
} else {
|
||||
if ( _rootNode == null ) {
|
||||
var initial_bounds = new IsoRect(
|
||||
bounds.center - bounds.size * 2.0f,
|
||||
bounds.center + bounds.size * 2.0f);
|
||||
_rootNode = _nodePool.Take().Init(null, initial_bounds);
|
||||
}
|
||||
Item item;
|
||||
while ( !_rootNode.AddItem(bounds, content, out item, _nodePool, _itemPool) ) {
|
||||
GrowUp(
|
||||
bounds.center.x < _rootNode.Bounds.center.x,
|
||||
bounds.center.y < _rootNode.Bounds.center.y);
|
||||
}
|
||||
_allItems.Add(content, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveItem(T content) {
|
||||
Item item;
|
||||
while ( !_root.Insert(bounds, content, out item, _nodePool, _itemPool) ) {
|
||||
GrowUp(
|
||||
bounds.center.x < _root.Bounds.center.x,
|
||||
bounds.center.y < _root.Bounds.center.y);
|
||||
if ( _allItems.TryGetValue(content, out item) ) {
|
||||
if ( item.Owner != null ) {
|
||||
item.Owner.RemoveItem(item);
|
||||
}
|
||||
_allItems.Remove(content);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public void MoveItem(IsoRect bounds, T content) {
|
||||
//TODO implme
|
||||
RemoveItem(content);
|
||||
AddItem(bounds, content);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
if ( _root != null ) {
|
||||
if ( _rootNode != null ) {
|
||||
_nodePool.Release(
|
||||
_root.Clear(_nodePool, _itemPool));
|
||||
_root = null;
|
||||
_rootNode.Clear(_nodePool, _itemPool));
|
||||
_rootNode = null;
|
||||
}
|
||||
_allItems.Clear();
|
||||
}
|
||||
|
||||
public void VisitAllBounds(IBoundsLookUpper look_upper) {
|
||||
if ( look_upper == null ) {
|
||||
throw new System.ArgumentNullException("look_upper");
|
||||
}
|
||||
if ( _rootNode != null ) {
|
||||
_rootNode.VisitAllBounds(look_upper);
|
||||
}
|
||||
}
|
||||
|
||||
public void VisitAllBounds(System.Action<IsoRect> act) {
|
||||
if ( _root != null ) {
|
||||
_root.VisitAllBounds(act);
|
||||
}
|
||||
}
|
||||
|
||||
public void VisitItemsByBounds(IsoRect bounds, System.Action<T> act) {
|
||||
if ( _root != null ) {
|
||||
_root.VisitItemsByBounds(bounds, act);
|
||||
public void VisitItemsByBounds(IsoRect bounds, IContentLookUpper look_upper) {
|
||||
if ( look_upper == null ) {
|
||||
throw new System.ArgumentNullException("look_upper");
|
||||
}
|
||||
if ( _rootNode != null ) {
|
||||
_rootNode.VisitItemsByBounds(bounds, look_upper);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Private
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void GrowUp(bool left, bool top) {
|
||||
var new_root_bounds = _root.Bounds;
|
||||
var new_root_bounds = _rootNode.Bounds;
|
||||
new_root_bounds.Translate(
|
||||
left ? -_root.Bounds.size.x : 0.0f,
|
||||
top ? -_root.Bounds.size.y : 0.0f);
|
||||
new_root_bounds.Resize(_root.Bounds.size * 2.0f);
|
||||
left ? -_rootNode.Bounds.size.x : 0.0f,
|
||||
top ? -_rootNode.Bounds.size.y : 0.0f);
|
||||
new_root_bounds.Resize(_rootNode.Bounds.size * 2.0f);
|
||||
var new_root = _nodePool.Take().Init(null, new_root_bounds);
|
||||
if ( left ) {
|
||||
if ( top ) {
|
||||
new_root.Children[3] = _root;
|
||||
new_root.Nodes[3] = _rootNode;
|
||||
} else {
|
||||
new_root.Children[1] = _root;
|
||||
new_root.Nodes[1] = _rootNode;
|
||||
}
|
||||
} else {
|
||||
if ( top ) {
|
||||
new_root.Children[2] = _root;
|
||||
new_root.Nodes[2] = _rootNode;
|
||||
} else {
|
||||
new_root.Children[0] = _root;
|
||||
new_root.Nodes[0] = _rootNode;
|
||||
}
|
||||
}
|
||||
_root.Parent = new_root;
|
||||
_root = new_root;
|
||||
_rootNode.Parent = new_root;
|
||||
_rootNode = new_root;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,9 +102,9 @@ namespace IsoTools.Internal {
|
||||
}
|
||||
|
||||
public static IsoRect Merge(IsoRect a, IsoRect b) {
|
||||
return new IsoRect(
|
||||
IsoMinMax.Merge(a.x, b.x),
|
||||
IsoMinMax.Merge(a.y, b.y));
|
||||
a.x = IsoMinMax.Merge(a.x, b.x);
|
||||
a.y = IsoMinMax.Merge(a.y, b.y);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,60 @@ using UnityEngine.Profiling;
|
||||
|
||||
namespace IsoTools.Internal {
|
||||
public class IsoScreenSolver {
|
||||
Vector2 _minIsoXY = Vector2.zero;
|
||||
IsoAssocList<IsoObject> _curVisibles = new IsoAssocList<IsoObject>();
|
||||
IsoAssocList<IsoObject> _oldVisibles = new IsoAssocList<IsoObject>();
|
||||
IsoGrid<IsoObject> _visibleGrid = new IsoGrid<IsoObject>(new IsoGridItemAdapter(), 47);
|
||||
IsoGridLookUpper _gridLookUpper = new IsoGridLookUpper();
|
||||
List<Renderer> _tmpRenderers = new List<Renderer>();
|
||||
Vector2 _minIsoXY = Vector2.zero;
|
||||
IsoAssocList<IsoObject> _oldVisibles = new IsoAssocList<IsoObject>();
|
||||
IsoAssocList<IsoObject> _curVisibles = new IsoAssocList<IsoObject>();
|
||||
|
||||
IsoQuadTree<IsoObject> _quadTree = new IsoQuadTree<IsoObject>(47);
|
||||
IsoGrid<IsoObject> _screenGrid = new IsoGrid<IsoObject>(new IsoGridItemAdapter(), 47);
|
||||
|
||||
IsoQTBoundsLookUpper _qtBoundsLU = new IsoQTBoundsLookUpper();
|
||||
IsoQTContentLookUpper _qtContentLU = new IsoQTContentLookUpper();
|
||||
IsoGridLookUpper _screenGridLU = new IsoGridLookUpper();
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// IsoQTBoundsLookUpper
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
class IsoQTBoundsLookUpper : IsoQuadTree<IsoObject>.IBoundsLookUpper {
|
||||
public void LookUp(IsoRect bounds) {
|
||||
#if UNITY_EDITOR
|
||||
IsoUtils.DrawRect(bounds, Color.blue);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// IsoQTContentLookUpper
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
class IsoQTContentLookUpper : IsoQuadTree<IsoObject>.IContentLookUpper {
|
||||
Camera[] _tmpCameras = new Camera[8];
|
||||
IsoScreenSolver _screenSolver = null;
|
||||
|
||||
public void LookUp(IsoObject iso_object) {
|
||||
iso_object.Internal.Placed = false;
|
||||
_screenSolver._oldVisibles.Add(iso_object);
|
||||
}
|
||||
|
||||
public void LookUpForVisibility(IsoScreenSolver screen_solver) {
|
||||
_screenSolver = screen_solver;
|
||||
var cam_count = Camera.GetAllCameras(_tmpCameras);
|
||||
for ( var i = 0; i < cam_count; ++i ) {
|
||||
var camera = _tmpCameras[i];
|
||||
var vp_rect = camera.rect;
|
||||
var wrl_min = camera.ViewportToWorldPoint(new Vector3(vp_rect.xMin, vp_rect.yMin));
|
||||
var wrl_max = camera.ViewportToWorldPoint(new Vector3(vp_rect.xMax, vp_rect.yMax));
|
||||
_screenSolver._quadTree.VisitItemsByBounds(new IsoRect(wrl_min, wrl_max), this);
|
||||
}
|
||||
_screenSolver = null;
|
||||
System.Array.Clear(_tmpCameras, 0, _tmpCameras.Length);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
@@ -46,15 +94,18 @@ namespace IsoTools.Internal {
|
||||
IsoObject _isoObject;
|
||||
|
||||
public void LookUp(IsoList<IsoObject> items) {
|
||||
LookUpSectorDepends(_isoObject, items);
|
||||
LookUpSectorRDepends(_isoObject, items);
|
||||
LookUpCellForLDepends(_isoObject, items);
|
||||
LookUpCellForRDepends(_isoObject, items);
|
||||
}
|
||||
|
||||
public void Setup(IsoObject iso_object) {
|
||||
public void LookUpForDepends(
|
||||
IsoScreenSolver screen_solver, IsoObject iso_object)
|
||||
{
|
||||
_isoObject = iso_object;
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
screen_solver._screenGrid.LookUpCells(
|
||||
iso_object.Internal.MinGridCell,
|
||||
iso_object.Internal.MaxGridCell,
|
||||
this);
|
||||
_isoObject = null;
|
||||
}
|
||||
}
|
||||
@@ -69,14 +120,14 @@ namespace IsoTools.Internal {
|
||||
get { return _minIsoXY; }
|
||||
}
|
||||
|
||||
public IsoAssocList<IsoObject> curVisibles {
|
||||
get { return _curVisibles; }
|
||||
}
|
||||
|
||||
public IsoAssocList<IsoObject> oldVisibles {
|
||||
get { return _oldVisibles; }
|
||||
}
|
||||
|
||||
public IsoAssocList<IsoObject> curVisibles {
|
||||
get { return _curVisibles; }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Callbacks
|
||||
@@ -84,15 +135,22 @@ namespace IsoTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public void OnAddInstance(IsoObject iso_object) {
|
||||
_quadTree.AddItem(
|
||||
iso_object.Internal.ScreenBounds,
|
||||
iso_object);
|
||||
}
|
||||
|
||||
public void OnRemoveInstance(IsoObject iso_object) {
|
||||
_oldVisibles.Remove(iso_object);
|
||||
_curVisibles.Remove(iso_object);
|
||||
_quadTree.RemoveItem(iso_object);
|
||||
ClearIsoObjectDepends(iso_object);
|
||||
}
|
||||
|
||||
public bool OnMarkDirtyInstance(IsoObject iso_object) {
|
||||
_quadTree.MoveItem(
|
||||
iso_object.Internal.ScreenBounds,
|
||||
iso_object);
|
||||
if ( !iso_object.Internal.Dirty && _curVisibles.Contains(iso_object) ) {
|
||||
iso_object.Internal.Dirty = true;
|
||||
return true;
|
||||
@@ -102,6 +160,7 @@ namespace IsoTools.Internal {
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public void OnDrawGizmos(IsoWorld iso_world) {
|
||||
_quadTree.VisitAllBounds(_qtBoundsLU);
|
||||
/*
|
||||
for ( int y = 0, ye = (int)_sectorsNumPosCount.y; y < ye; ++y ) {
|
||||
for ( int x = 0, xe = (int)_sectorsNumPosCount.x; x < xe; ++x ) {
|
||||
@@ -126,32 +185,27 @@ namespace IsoTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public void StepSortingAction(IsoWorld iso_world, IsoAssocList<IsoObject> instances) {
|
||||
Profiler.BeginSample("ResolveVisibles");
|
||||
Profiler.BeginSample("IsoScreenSolver.ResolveVisibles");
|
||||
ResolveVisibles(instances);
|
||||
Profiler.EndSample();
|
||||
Profiler.BeginSample("ResolveVisibleGrid");
|
||||
Profiler.BeginSample("IsoScreenSolver.ResolveVisibleGrid");
|
||||
ResolveVisibleGrid(iso_world);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
public void PostStepSortingAction() {
|
||||
_tmpRenderers.Clear();
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
_curVisibles.Clear();
|
||||
_oldVisibles.Clear();
|
||||
_visibleGrid.ClearGrid();
|
||||
_curVisibles.Clear();
|
||||
_quadTree.Clear();
|
||||
_screenGrid.ClearGrid();
|
||||
}
|
||||
|
||||
public void SetupIsoObjectDepends(IsoObject iso_object) {
|
||||
ClearIsoObjectDepends(iso_object);
|
||||
_gridLookUpper.Setup(iso_object);
|
||||
_visibleGrid.LookUpCells(
|
||||
iso_object.Internal.MinGridCell,
|
||||
iso_object.Internal.MaxGridCell,
|
||||
_gridLookUpper);
|
||||
_gridLookUpper.Reset();
|
||||
_screenGridLU.LookUpForDepends(this, iso_object);
|
||||
}
|
||||
|
||||
public void ClearIsoObjectDepends(IsoObject iso_object) {
|
||||
@@ -173,7 +227,15 @@ namespace IsoTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void ResolveVisibles(IsoAssocList<IsoObject> instances) {
|
||||
_oldVisibles.Clear();
|
||||
Profiler.BeginSample("ProcessAllInstances");
|
||||
ProcessAllInstances(instances);
|
||||
Profiler.EndSample();
|
||||
Profiler.BeginSample("ProcessNewVisibles");
|
||||
ProcessNewVisibles();
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
void ProcessAllInstances(IsoAssocList<IsoObject> instances) {
|
||||
if ( instances.Count > 0 ) {
|
||||
_minIsoXY.Set(float.MaxValue, float.MaxValue);
|
||||
for ( int i = 0, e = instances.Count; i < e; ++i ) {
|
||||
@@ -191,36 +253,22 @@ namespace IsoTools.Internal {
|
||||
{
|
||||
iso_object.FixIsoPosition();
|
||||
}
|
||||
if ( IsIsoObjectVisible(iso_object) ) {
|
||||
iso_object.Internal.Placed = false;
|
||||
_oldVisibles.Add(iso_object);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_minIsoXY.Set(0.0f, 0.0f);
|
||||
}
|
||||
var temp_visibles = _curVisibles;
|
||||
}
|
||||
|
||||
void ProcessNewVisibles() {
|
||||
_oldVisibles.Clear();
|
||||
_qtContentLU.LookUpForVisibility(this);
|
||||
SwapCurrentVisibles();
|
||||
}
|
||||
|
||||
void SwapCurrentVisibles() {
|
||||
var swap_tmp = _curVisibles;
|
||||
_curVisibles = _oldVisibles;
|
||||
_oldVisibles = temp_visibles;
|
||||
}
|
||||
|
||||
bool IsIsoObjectVisible(IsoObject iso_object) {
|
||||
var renderers = GetIsoObjectRenderers(iso_object);
|
||||
for ( int i = 0, e = renderers.Count; i < e; ++i ) {
|
||||
if ( renderers[i].isVisible ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Renderer> GetIsoObjectRenderers(IsoObject iso_object) {
|
||||
if ( iso_object.cacheRenderers ) {
|
||||
return iso_object.Internal.Renderers;
|
||||
} else {
|
||||
iso_object.GetComponentsInChildren<Renderer>(_tmpRenderers);
|
||||
return _tmpRenderers;
|
||||
}
|
||||
_oldVisibles = swap_tmp;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -230,24 +278,24 @@ namespace IsoTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void ResolveVisibleGrid(IsoWorld iso_world) {
|
||||
_visibleGrid.ClearItems();
|
||||
_screenGrid.ClearItems();
|
||||
for ( int i = 0, e = _curVisibles.Count; i < e; ++i ) {
|
||||
var iso_object = _curVisibles[i];
|
||||
_visibleGrid.AddItem(iso_object);
|
||||
_screenGrid.AddItem(iso_object);
|
||||
}
|
||||
var min_sector_size = IsoUtils.Vec2MaxF(
|
||||
iso_world.IsoToScreen(IsoUtils.vec3OneXY) -
|
||||
iso_world.IsoToScreen(Vector3.zero));
|
||||
_visibleGrid.RebuildGrid(min_sector_size);
|
||||
_screenGrid.RebuildGrid(min_sector_size);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// LookUpSectorDepends
|
||||
// LookUpCellForDepends
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static void LookUpSectorDepends(IsoObject obj_a, IsoList<IsoObject> others) {
|
||||
static void LookUpCellForLDepends(IsoObject obj_a, IsoList<IsoObject> others) {
|
||||
for ( int i = 0, e = others.Count; i < e; ++i ) {
|
||||
var obj_b = others[i];
|
||||
if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_a, obj_b) ) {
|
||||
@@ -257,7 +305,7 @@ namespace IsoTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
static void LookUpSectorRDepends(IsoObject obj_a, IsoList<IsoObject> others) {
|
||||
static void LookUpCellForRDepends(IsoObject obj_a, IsoList<IsoObject> others) {
|
||||
for ( int i = 0, e = others.Count; i < e; ++i ) {
|
||||
var obj_b = others[i];
|
||||
if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_b, obj_a) ) {
|
||||
@@ -293,9 +341,13 @@ namespace IsoTools.Internal {
|
||||
var dB_y = b_max.y - a_min.y;
|
||||
var dB_z = a_max.z - b_min.z;
|
||||
|
||||
var dP_x = a_size.x + b_size.x - Mathf.Abs(dA_x - dB_x);
|
||||
var dP_y = a_size.y + b_size.y - Mathf.Abs(dA_y - dB_y);
|
||||
var dP_z = a_size.z + b_size.z - Mathf.Abs(dA_z - dB_z);
|
||||
var dD_x = dB_x - dA_x;
|
||||
var dD_y = dB_y - dA_y;
|
||||
var dD_z = dB_z - dA_z;
|
||||
|
||||
var dP_x = a_size.x + b_size.x - (dD_x > 0.0f ? dD_x : -dD_x);
|
||||
var dP_y = a_size.y + b_size.y - (dD_y > 0.0f ? dD_y : -dD_y);
|
||||
var dP_z = a_size.z + b_size.z - (dD_z > 0.0f ? dD_z : -dD_z);
|
||||
|
||||
if ( dP_x <= dP_y && dP_x <= dP_z ) {
|
||||
return dA_x > dB_x;
|
||||
|
||||
@@ -43,11 +43,11 @@ namespace IsoTools.Internal {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public bool StepSortingAction(IsoWorld iso_world, IsoScreenSolver screen_solver) {
|
||||
Profiler.BeginSample("ResolveVisibles");
|
||||
Profiler.BeginSample("IsoSortingSolver.ResolveVisibles");
|
||||
var dirty = ResolveVisibles(screen_solver);
|
||||
Profiler.EndSample();
|
||||
if ( dirty ) {
|
||||
Profiler.BeginSample("PlaceAllVisibles");
|
||||
Profiler.BeginSample("IsoSortingSolver.PlaceAllVisibles");
|
||||
PlaceAllVisibles(iso_world, screen_solver);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ namespace IsoTools {
|
||||
void Awake() {
|
||||
FixCachedTransform();
|
||||
FixLastTransform();
|
||||
FixIsoPosition();
|
||||
FixTransform();
|
||||
}
|
||||
|
||||
protected override void OnEnable() {
|
||||
|
||||
Reference in New Issue
Block a user