From af5077a96d510a31fea01fb1c478347667e3778c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 24 Dec 2016 04:41:02 +0700 Subject: [PATCH] move grid logic from screen solver to IsoGrid.cs --- Assets/IsoTools/Examples/Scenes/Scene11.unity | 93 ++++++- Assets/IsoTools/Scripts/Internal/IsoGrid.cs | 215 ++++++++++++++++ .../IsoTools/Scripts/Internal/IsoGrid.cs.meta | 12 + .../IsoTools/Scripts/Internal/IsoQuadTree.cs | 28 +-- .../Scripts/Internal/IsoScreenSolver.cs | 238 +++++++----------- .../Scripts/Internal/IsoSortingSolver.cs | 29 ++- Assets/IsoTools/Scripts/IsoWorld.cs | 5 + 7 files changed, 447 insertions(+), 173 deletions(-) create mode 100644 Assets/IsoTools/Scripts/Internal/IsoGrid.cs create mode 100644 Assets/IsoTools/Scripts/Internal/IsoGrid.cs.meta diff --git a/Assets/IsoTools/Examples/Scenes/Scene11.unity b/Assets/IsoTools/Examples/Scenes/Scene11.unity index a59a824..9e78875 100644 --- a/Assets/IsoTools/Examples/Scenes/Scene11.unity +++ b/Assets/IsoTools/Examples/Scenes/Scene11.unity @@ -91,6 +91,88 @@ NavMeshSettings: cellSize: 0.16666667 accuratePlacement: 0 m_NavMeshData: {fileID: 0} +--- !u!1001 &59660026 +Prefab: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 522845242} + m_Modifications: + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalPosition.x + value: 0.00000047683716 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalPosition.y + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalPosition.z + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalRotation.x + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalRotation.y + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalRotation.z + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_RootOrder + value: 6 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _position.x + value: -1 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _position.y + value: -1 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _position.z + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _showTheirDepends + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _showIsoBounds + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _showScreenBounds + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: _showSelfDepends + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 11472392, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_Enabled + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 180122, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + propertyPath: m_Name + value: TileA (7) + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_ParentPrefab: {fileID: 100100000, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + m_IsPrefabParent: 0 +--- !u!4 &59660027 stripped +Transform: + m_PrefabParentObject: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} + m_PrefabInternal: {fileID: 59660026} --- !u!1 &522845241 GameObject: m_ObjectHideFlags: 0 @@ -124,6 +206,7 @@ Transform: - {fileID: 1887318779} - {fileID: 769235454} - {fileID: 1680987096} + - {fileID: 59660027} m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -259,7 +342,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalPosition.z - value: 1.4000001 + value: 1.5000001 objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalRotation.x @@ -341,7 +424,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalPosition.z - value: 1.1 + value: 1.3000001 objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalRotation.x @@ -423,7 +506,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalPosition.z - value: 1.3000001 + value: 1.4000001 objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalRotation.x @@ -592,7 +675,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalPosition.z - value: 1 + value: 1.1 objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalRotation.x @@ -674,7 +757,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalPosition.z - value: 1.5000001 + value: 1.6000001 objectReference: {fileID: 0} - target: {fileID: 417424, guid: 185575a05f87743c0b2ddb83dd39c6cd, type: 2} propertyPath: m_LocalRotation.x diff --git a/Assets/IsoTools/Scripts/Internal/IsoGrid.cs b/Assets/IsoTools/Scripts/Internal/IsoGrid.cs new file mode 100644 index 0000000..8e654b3 --- /dev/null +++ b/Assets/IsoTools/Scripts/Internal/IsoGrid.cs @@ -0,0 +1,215 @@ +using UnityEngine; +using System.Collections.Generic; + +namespace IsoTools.Internal { + public class IsoGrid { + + // + // Cell + // + + class Cell { + public IsoList Items = new IsoList(); + } + + // + // CellPool + // + + class CellPool : IsoPool { + public CellPool(int capacity) : base(capacity) { + } + + public override Cell CreateItem() { + return new Cell(); + } + + public override void CleanUpItem(Cell item) { + item.Items.Clear(); + } + } + + // + // IItemAdapter + // + + public interface IItemAdapter { + IsoRect GetBounds (T item); + void SetMinMaxCells (T item, Vector2 min, Vector2 max); + void GetMinMaxCells (T item, ref Vector2 min, ref Vector2 max); + } + + // + // ILookUpper + // + + public interface ILookUpper { + void LookUp(IsoList items); + } + + // + // Members + // + + IsoIPool _cellPool = null; + IsoList _gridCells = null; + + IsoList _gridItems = null; + IItemAdapter _itemAdapter = null; + + float _gridCellSize = 0.0f; + Vector2 _gridMinNumPos = Vector2.zero; + Vector2 _gridMaxNumPos = Vector2.zero; + Vector2 _gridNumPosCount = Vector2.zero; + + // + // Public + // + + public IsoGrid(IItemAdapter item_adapter, int capacity) { + if ( item_adapter == null ) { + throw new System.ArgumentNullException("item_adapter"); + } + if ( capacity < 0 ) { + throw new System.ArgumentOutOfRangeException( + "capacity", "capacity must be >= 0"); + } + _cellPool = new CellPool(capacity); + _gridCells = new IsoList(capacity); + _gridItems = new IsoList(capacity); + _itemAdapter = item_adapter; + } + + public void AddItem(T content) { + _gridItems.Add(content); + } + + public void ClearItems() { + _gridItems.Clear(); + } + + public void RebuildGrid(float min_cell_size) { + ClearGrid(); + CalculateCellSize(min_cell_size); + PrepareGridNumPos(); + SetupGridCells(); + FillGridCells(); + } + + public void ClearGrid() { + while ( _gridCells.Count > 0 ) { + _cellPool.Release(_gridCells.Pop()); + } + _gridCellSize = 0.0f; + _gridMinNumPos = Vector2.zero; + _gridMaxNumPos = Vector2.zero; + _gridNumPosCount = Vector2.zero; + } + + public void LookUpCells(Vector2 min, Vector2 max, ILookUpper look_upper) { + for ( var y = min.y; y < max.y; ++y ) { + for ( var x = min.x; x < max.x; ++x ) { + var cell = FindCell(x, y); + if ( cell != null ) { + look_upper.LookUp(cell.Items); + } + }} + } + + // + // Private + // + + void CalculateCellSize(float min_cell_size) { + _gridCellSize = 0.0f; + for ( int i = 0, e = _gridItems.Count; i < e; ++i ) { + var bounds = _itemAdapter.GetBounds(_gridItems[i]); + var size_x = bounds.x.max - bounds.x.min; + var size_y = bounds.y.max - bounds.y.min; + _gridCellSize += size_x > size_y ? size_x : size_y; + } + _gridCellSize = _gridItems.Count > 0 + ? Mathf.Round(Mathf.Max(min_cell_size, _gridCellSize / _gridItems.Count)) + : min_cell_size; + } + + void PrepareGridNumPos() { + if ( _gridItems.Count > 0 ) { + _gridMinNumPos.Set(float.MaxValue, float.MaxValue); + _gridMaxNumPos.Set(float.MinValue, float.MinValue); + for ( int i = 0, e = _gridItems.Count; i < e; ++i ) { + var item = _gridItems[i]; + var bounds = _itemAdapter.GetBounds(item); + var min_x = bounds.x.min / _gridCellSize; + var min_y = bounds.y.min / _gridCellSize; + var max_x = bounds.x.max / _gridCellSize; + var max_y = bounds.y.max / _gridCellSize; + var min_cell_x = (float)(int)(min_x >= 0.0f ? min_x : min_x - 1.0f); + var min_cell_y = (float)(int)(min_y >= 0.0f ? min_y : min_y - 1.0f); + var max_cell_x = (float)(int)(max_x >= 0.0f ? max_x + 1.0f : max_x); + var max_cell_y = (float)(int)(max_y >= 0.0f ? max_y + 1.0f : max_y); + if ( _gridMinNumPos.x > min_cell_x ) { + _gridMinNumPos.x = min_cell_x; + } + if ( _gridMinNumPos.y > min_cell_y ) { + _gridMinNumPos.y = min_cell_y; + } + if ( _gridMaxNumPos.x < max_cell_x ) { + _gridMaxNumPos.x = max_cell_x; + } + if ( _gridMaxNumPos.y < max_cell_y ) { + _gridMaxNumPos.y = max_cell_y; + } + _itemAdapter.SetMinMaxCells( + item, + new Vector2(min_cell_x, min_cell_y), + new Vector2(max_cell_x, max_cell_y)); + } + } else { + _gridMinNumPos.Set(0.0f, 0.0f); + _gridMaxNumPos.Set(_gridCellSize, _gridCellSize); + } + _gridNumPosCount = _gridMaxNumPos - _gridMinNumPos; + } + + void SetupGridCells() { + var cell_count = Mathf.RoundToInt(_gridNumPosCount.x * _gridNumPosCount.y); + if ( _gridCells.Capacity < cell_count ) { + _gridCells.Capacity = cell_count * 2; + } + while ( _gridCells.Count < cell_count ) { + _gridCells.Add(_cellPool.Take()); + } + } + + void FillGridCells() { + var min_cell = Vector2.zero; + var max_cell = Vector2.zero; + for ( int i = 0, e = _gridItems.Count; i < e; ++i ) { + var item = _gridItems[i]; + _itemAdapter.GetMinMaxCells(item, ref min_cell, ref max_cell); + min_cell -= _gridMinNumPos; + max_cell -= _gridMinNumPos; + _itemAdapter.SetMinMaxCells(item, min_cell, max_cell); + for ( var y = min_cell.y; y < max_cell.y; ++y ) { + for ( var x = min_cell.x; x < max_cell.x; ++x ) { + var cell = FindCell(x, y); + if ( cell != null ) { + cell.Items.Add(item); + } + }} + } + } + + Cell FindCell(float num_pos_x, float num_pos_y) { + if ( num_pos_x < 0.0f || num_pos_y < 0.0f ) { + return null; + } + if ( num_pos_x >= _gridNumPosCount.x || num_pos_y >= _gridNumPosCount.y ) { + return null; + } + var cell_index = (int)(num_pos_x + _gridNumPosCount.x * num_pos_y); + return _gridCells[cell_index]; + } + } +} \ No newline at end of file diff --git a/Assets/IsoTools/Scripts/Internal/IsoGrid.cs.meta b/Assets/IsoTools/Scripts/Internal/IsoGrid.cs.meta new file mode 100644 index 0000000..98e17e7 --- /dev/null +++ b/Assets/IsoTools/Scripts/Internal/IsoGrid.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: bcb6f6ab1a08544cf8dfc30925adfa52 +timeCreated: 1482441520 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/IsoTools/Scripts/Internal/IsoQuadTree.cs b/Assets/IsoTools/Scripts/Internal/IsoQuadTree.cs index 6df99da..be9dbc6 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoQuadTree.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoQuadTree.cs @@ -47,7 +47,7 @@ return this; } - public Node Clear(IsoIPool node_pool, IsoIPool item_pool) { + public Node Clear(IsoIPool node_pool, IsoIPool item_pool) { ClearChildren(node_pool, item_pool); ClearContents(item_pool); return Init(null, IsoRect.zero); @@ -158,19 +158,6 @@ } } - // - // NodePool - // - - class NodePool : IsoPool { - public NodePool(int capacity) : base(capacity) { - } - - public override Node CreateItem() { - return new Node(); - } - } - // // ItemPool // @@ -184,6 +171,19 @@ } } + // + // NodePool + // + + class NodePool : IsoPool { + public NodePool(int capacity) : base(capacity) { + } + + public override Node CreateItem() { + return new Node(); + } + } + // // Members // diff --git a/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs b/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs index acf9644..6dcc6f8 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs @@ -7,24 +7,57 @@ using UnityEngine.Profiling; namespace IsoTools.Internal { public class IsoScreenSolver { - Vector2 _minIsoXY = Vector2.zero; - IsoAssocList _curVisibles = new IsoAssocList(); - IsoAssocList _oldVisibles = new IsoAssocList(); + Vector2 _minIsoXY = Vector2.zero; + IsoAssocList _curVisibles = new IsoAssocList(); + IsoAssocList _oldVisibles = new IsoAssocList(); + IsoGrid _visibleGrid = new IsoGrid(new IsoGridItemAdapter(), 47); + IsoGridLookUpper _gridLookUpper = new IsoGridLookUpper(); + List _tmpRenderers = new List(); - class Sector { - public IsoList objects = new IsoList(); - public void Reset() { - objects.Clear(); + // --------------------------------------------------------------------- + // + // IsoGridItemAdapter + // + // --------------------------------------------------------------------- + + class IsoGridItemAdapter : IsoGrid.IItemAdapter { + public IsoRect GetBounds(IsoObject item) { + return item.Internal.ScreenBounds; + } + + public void SetMinMaxCells(IsoObject item, Vector2 min, Vector2 max) { + item.Internal.MinSector = min; + item.Internal.MaxSector = max; + } + + public void GetMinMaxCells(IsoObject item, ref Vector2 min, ref Vector2 max) { + min = item.Internal.MinSector; + max = item.Internal.MaxSector; } } - IsoList _sectors = new IsoList(); - float _sectorsSize = 0.0f; - Vector2 _sectorsMinNumPos = Vector2.zero; - Vector2 _sectorsMaxNumPos = Vector2.zero; - Vector2 _sectorsNumPosCount = Vector2.zero; + // --------------------------------------------------------------------- + // + // IsoGridLookUpper + // + // --------------------------------------------------------------------- - List _tmpRenderers = new List(); + class IsoGridLookUpper : IsoGrid.ILookUpper { + IsoObject _isoObject; + + public void LookUp(IsoList items) { + LookUpSectorDepends(_isoObject, items); + LookUpSectorRDepends(_isoObject, items); + } + + public void Setup(IsoObject iso_object) { + _isoObject = iso_object; + } + + public void Reset() { + _isoObject = null; + } + } // --------------------------------------------------------------------- // @@ -67,6 +100,25 @@ namespace IsoTools.Internal { return false; } + #if UNITY_EDITOR + public void OnDrawGizmos(IsoWorld iso_world) { + /* + for ( int y = 0, ye = (int)_sectorsNumPosCount.y; y < ye; ++y ) { + for ( int x = 0, xe = (int)_sectorsNumPosCount.x; x < xe; ++x ) { + var sector = FindSector((float)x, (float)y); + if ( sector != null && sector.objects.Count > 0 ) { + var rect = new IsoRect( + (x * _sectorsSize), + (y * _sectorsSize), + (x * _sectorsSize) + _sectorsSize, + (y * _sectorsSize) + _sectorsSize); + rect.Translate(_sectorsMinNumPos * _sectorsSize); + IsoUtils.DrawRect(rect, Color.blue); + } + }}*/ + } + #endif + // --------------------------------------------------------------------- // // Functions @@ -77,8 +129,8 @@ namespace IsoTools.Internal { Profiler.BeginSample("ResolveVisibles"); ResolveVisibles(instances); Profiler.EndSample(); - Profiler.BeginSample("ResolveSectors"); - ResolveSectors(iso_world); + Profiler.BeginSample("ResolveVisibleGrid"); + ResolveVisibleGrid(iso_world); Profiler.EndSample(); } @@ -89,18 +141,17 @@ namespace IsoTools.Internal { public void Clear() { _curVisibles.Clear(); _oldVisibles.Clear(); - _sectors.Clear(); + _visibleGrid.ClearGrid(); } - public void SetupIsoObjectDepends(IsoObject obj_a) { - ClearIsoObjectDepends(obj_a); - var min = obj_a.Internal.MinSector; - var max = obj_a.Internal.MaxSector; - for ( var y = min.y; y < max.y; ++y ) { - for ( var x = min.x; x < max.x; ++x ) { - LookUpSectorDepends(x, y, obj_a); - LookUpSectorRDepends(x, y, obj_a); - }} + public void SetupIsoObjectDepends(IsoObject iso_object) { + ClearIsoObjectDepends(iso_object); + _gridLookUpper.Setup(iso_object); + _visibleGrid.LookUpCells( + iso_object.Internal.MinSector, + iso_object.Internal.MaxSector, + _gridLookUpper); + _gridLookUpper.Reset(); } public void ClearIsoObjectDepends(IsoObject iso_object) { @@ -174,111 +225,20 @@ namespace IsoTools.Internal { // --------------------------------------------------------------------- // - // ResolveSectors + // ResolveVisibleGrid // // --------------------------------------------------------------------- - void ResolveSectors(IsoWorld iso_world) { - SetupSectorSize(iso_world); - SetupObjectSectors(); - SetupSectors(); - } - - void SetupSectorSize(IsoWorld iso_world) { - _sectorsSize = 0.0f; + void ResolveVisibleGrid(IsoWorld iso_world) { + _visibleGrid.ClearItems(); for ( int i = 0, e = _curVisibles.Count; i < e; ++i ) { - var iso_internal = _curVisibles[i].Internal; - var size_x = iso_internal.ScreenBounds.x.max - iso_internal.ScreenBounds.x.min; - var size_y = iso_internal.ScreenBounds.y.max - iso_internal.ScreenBounds.y.min; - _sectorsSize += size_x > size_y ? size_x : size_y; + var iso_object = _curVisibles[i]; + _visibleGrid.AddItem(iso_object); } var min_sector_size = IsoUtils.Vec2MaxF( iso_world.IsoToScreen(IsoUtils.vec3OneXY) - iso_world.IsoToScreen(Vector3.zero)); - _sectorsSize = _curVisibles.Count > 0 - ? Mathf.Round(Mathf.Max(min_sector_size, _sectorsSize / _curVisibles.Count)) - : min_sector_size; - } - - void SetupObjectSectors() { - if ( _curVisibles.Count > 0 ) { - _sectorsMinNumPos.Set(float.MaxValue, float.MaxValue); - _sectorsMaxNumPos.Set(float.MinValue, float.MinValue); - for ( int i = 0, e = _curVisibles.Count; i < e; ++i ) { - var iso_internal = _curVisibles[i].Internal; - var min_x = iso_internal.ScreenBounds.x.min / _sectorsSize; - var min_y = iso_internal.ScreenBounds.y.min / _sectorsSize; - var max_x = iso_internal.ScreenBounds.x.max / _sectorsSize; - var max_y = iso_internal.ScreenBounds.y.max / _sectorsSize; - iso_internal.MinSector.x = (int)(min_x >= 0.0f ? min_x : min_x - 1.0f); - iso_internal.MinSector.y = (int)(min_y >= 0.0f ? min_y : min_y - 1.0f); - iso_internal.MaxSector.x = (int)(max_x >= 0.0f ? max_x + 1.0f : max_x); - iso_internal.MaxSector.y = (int)(max_y >= 0.0f ? max_y + 1.0f : max_y); - if ( _sectorsMinNumPos.x > iso_internal.MinSector.x ) { - _sectorsMinNumPos.x = iso_internal.MinSector.x; - } - if ( _sectorsMinNumPos.y > iso_internal.MinSector.y ) { - _sectorsMinNumPos.y = iso_internal.MinSector.y; - } - if ( _sectorsMaxNumPos.x < iso_internal.MaxSector.x ) { - _sectorsMaxNumPos.x = iso_internal.MaxSector.x; - } - if ( _sectorsMaxNumPos.y < iso_internal.MaxSector.y ) { - _sectorsMaxNumPos.y = iso_internal.MaxSector.y; - } - } - } else { - _sectorsMinNumPos.Set(0.0f, 0.0f); - _sectorsMaxNumPos.Set(_sectorsSize, _sectorsSize); - } - _sectorsNumPosCount = _sectorsMaxNumPos - _sectorsMinNumPos; - } - - void SetupSectors() { - ResizeSectors((int)(_sectorsNumPosCount.x * _sectorsNumPosCount.y)); - FillSectors(); - } - - void ResizeSectors(int count) { - if ( _sectors.Count < count ) { - if ( _sectors.Capacity < count ) { - _sectors.Capacity = count * 2; - } - while ( _sectors.Count < count ) { - _sectors.Add(new Sector()); - } - } - for ( int i = 0, e = _sectors.Count; i < e; ++i ) { - _sectors[i].Reset(); - } - } - - void FillSectors() { - for ( int i = 0, e = _curVisibles.Count; i < e; ++i ) { - var iso_object = _curVisibles[i]; - iso_object.Internal.MinSector -= _sectorsMinNumPos; - iso_object.Internal.MaxSector -= _sectorsMinNumPos; - var min = iso_object.Internal.MinSector; - var max = iso_object.Internal.MaxSector; - for ( var y = min.y; y < max.y; ++y ) { - for ( var x = min.x; x < max.x; ++x ) { - var sector = FindSector(x, y); - if ( sector != null ) { - sector.objects.Add(iso_object); - } - }} - } - } - - Sector FindSector(float num_pos_x, float num_pos_y) { - if ( num_pos_x < 0.0f || num_pos_y < 0.0f ) { - return null; - } - if ( num_pos_x >= _sectorsNumPosCount.x || num_pos_y >= _sectorsNumPosCount.y ) { - return null; - } - var sector_index = (int)(num_pos_x + _sectorsNumPosCount.x * num_pos_y); - return _sectors[sector_index]; + _visibleGrid.RebuildGrid(min_sector_size); } // --------------------------------------------------------------------- @@ -287,39 +247,33 @@ namespace IsoTools.Internal { // // --------------------------------------------------------------------- - void LookUpSectorDepends(float num_pos_x, float num_pos_y, IsoObject obj_a) { - var sec = FindSector(num_pos_x, num_pos_y); - if ( sec != null ) { - for ( int i = 0, e = sec.objects.Count; i < e; ++i ) { - var obj_b = sec.objects[i]; - if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_a, obj_b) ) { - obj_a.Internal.SelfDepends.Add(obj_b); - obj_b.Internal.TheirDepends.Add(obj_a); - } + static void LookUpSectorDepends(IsoObject obj_a, IsoList 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) ) { + obj_a.Internal.SelfDepends.Add(obj_b); + obj_b.Internal.TheirDepends.Add(obj_a); } } } - void LookUpSectorRDepends(float num_pos_x, float num_pos_y, IsoObject obj_a) { - var sec = FindSector(num_pos_x, num_pos_y); - if ( sec != null ) { - for ( int i = 0, e = sec.objects.Count; i < e; ++i ) { - var obj_b = sec.objects[i]; - if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_b, obj_a) ) { - obj_b.Internal.SelfDepends.Add(obj_a); - obj_a.Internal.TheirDepends.Add(obj_b); - } + static void LookUpSectorRDepends(IsoObject obj_a, IsoList 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) ) { + obj_b.Internal.SelfDepends.Add(obj_a); + obj_a.Internal.TheirDepends.Add(obj_b); } } } - bool IsIsoObjectDepends(IsoObject a, IsoObject b) { + static bool IsIsoObjectDepends(IsoObject a, IsoObject b) { return a.Internal.ScreenBounds.Overlaps(b.Internal.ScreenBounds) && IsIsoObjectDepends(a.position, a.size, b.position, b.size); } - bool IsIsoObjectDepends(Vector3 a_min, Vector3 a_size, Vector3 b_min, Vector3 b_size) { + static bool IsIsoObjectDepends(Vector3 a_min, Vector3 a_size, Vector3 b_min, Vector3 b_size) { var a_max = a_min + a_size; var b_max = b_min + b_size; var a_yesno = a_max.x > b_min.x && a_max.y > b_min.y && b_max.z > a_min.z; diff --git a/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs b/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs index 5b09ad0..f420b05 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs @@ -31,6 +31,11 @@ namespace IsoTools.Internal { return false; } + #if UNITY_EDITOR + public void OnDrawGizmos() { + } + #endif + // --------------------------------------------------------------------- // // Functions @@ -74,8 +79,10 @@ namespace IsoTools.Internal { screen_solver.SetupIsoObjectDepends(iso_object); iso_object.Internal.Dirty = false; } - if ( UpdateIsoObjectBounds3d(iso_object) ) { - mark_dirty = true; + if ( iso_object.mode == IsoObject.Mode.Mode3d ) { + if ( UpdateIsoObjectBounds3d(iso_object) ) { + mark_dirty = true; + } } } @@ -92,16 +99,14 @@ namespace IsoTools.Internal { } bool UpdateIsoObjectBounds3d(IsoObject iso_object) { - if ( iso_object.mode != IsoObject.Mode.Mode3d ) { - var minmax3d = IsoObjectMinMax3D(iso_object); - var offset3d = iso_object.Internal.Transform.position.z - minmax3d.center; - if ( iso_object.Internal.MinMax3d.Approximately(minmax3d) || - !Mathf.Approximately(iso_object.Internal.Offset3d, offset3d) ) - { - iso_object.Internal.MinMax3d = minmax3d; - iso_object.Internal.Offset3d = offset3d; - return true; - } + var minmax3d = IsoObjectMinMax3D(iso_object); + var offset3d = iso_object.Internal.Transform.position.z - minmax3d.center; + if ( iso_object.Internal.MinMax3d.Approximately(minmax3d) || + !Mathf.Approximately(iso_object.Internal.Offset3d, offset3d) ) + { + iso_object.Internal.MinMax3d = minmax3d; + iso_object.Internal.Offset3d = offset3d; + return true; } return false; } diff --git a/Assets/IsoTools/Scripts/IsoWorld.cs b/Assets/IsoTools/Scripts/IsoWorld.cs index 2f410e8..b7af7ed 100644 --- a/Assets/IsoTools/Scripts/IsoWorld.cs +++ b/Assets/IsoTools/Scripts/IsoWorld.cs @@ -437,6 +437,11 @@ namespace IsoTools { } } + void OnDrawGizmos() { + _screenSolver.OnDrawGizmos(this); + _sortingSolver.OnDrawGizmos(); + } + /* QuadTree WIP void OnDrawGizmos() { var qt = new IsoQuadTree(_objects.Count);