remove screen grid

This commit is contained in:
2016-12-27 04:04:40 +07:00
parent bebe8644ef
commit 10a33b001c
7 changed files with 89 additions and 421 deletions

View File

@@ -1,249 +0,0 @@
using UnityEngine;
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 IBoundsLookUpper {
void LookUp(IsoRect bounds);
}
public interface IContentLookUpper {
void LookUp(IsoList<T> items);
}
// ---------------------------------------------------------------------
//
// CellPool
//
// ---------------------------------------------------------------------
class Cell {
public IsoList<T> Items = new IsoList<T>();
}
class CellPool : IsoPool<Cell> {
public CellPool(int capacity) : base(capacity) {
}
public override Cell CreateItem() {
return new Cell();
}
public override void CleanUpItem(Cell item) {
item.Items.Clear();
}
}
// ---------------------------------------------------------------------
//
// Members
//
// ---------------------------------------------------------------------
IsoIPool<Cell> _cellPool = null;
IsoList<Cell> _gridCells = null;
IsoList<T> _gridItems = null;
IItemAdapter _itemAdapter = null;
float _gridCellSize = 0.0f;
IsoPoint2 _gridMinNumPos = IsoPoint2.zero;
IsoPoint2 _gridMaxNumPos = IsoPoint2.zero;
IsoPoint2 _gridNumPosCount = IsoPoint2.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<Cell>(capacity);
_gridItems = new IsoList<T>(capacity);
_itemAdapter = item_adapter;
}
public void AddItem(T item) {
_gridItems.Add(item);
}
public void ClearItems() {
_gridItems.Clear();
}
public void RebuildGrid(float min_cell_size) {
if ( min_cell_size < Mathf.Epsilon ) {
throw new System.ArgumentOutOfRangeException(
"min_cell_size", "min_cell_size must be >= Mathf.Epsilon");
}
ClearGrid();
CalculateCellSize(min_cell_size);
PrepareGridNumPos();
SetupGridCells();
FillGridCells();
}
public void ClearGrid() {
while ( _gridCells.Count > 0 ) {
_cellPool.Release(_gridCells.Pop());
}
_gridCellSize = 0.0f;
_gridMinNumPos = IsoPoint2.zero;
_gridMaxNumPos = IsoPoint2.zero;
_gridNumPosCount = IsoPoint2.zero;
}
public void VisitAllBounds(IBoundsLookUpper look_upper) {
if ( look_upper == null ) {
throw new System.ArgumentNullException("look_upper");
}
for ( int y = 0, ye = _gridNumPosCount.y; y < ye; ++y ) {
for ( int x = 0, xe = _gridNumPosCount.x; x < xe; ++x ) {
var cell = GetCell(x, y);
if ( cell.Items.Count > 0 ) {
var rect = new IsoRect(
x * _gridCellSize,
y * _gridCellSize,
x * _gridCellSize + _gridCellSize,
y * _gridCellSize + _gridCellSize);
rect.Translate(
_gridMinNumPos.x * _gridCellSize,
_gridMinNumPos.y * _gridCellSize);
look_upper.LookUp(rect);
}
}}
}
public void VisitItemsByCells(
IsoPoint2 min_cell, IsoPoint2 max_cell,
IContentLookUpper look_upper)
{
if ( min_cell.x < 0 || min_cell.y < 0 ) {
throw new System.ArgumentOutOfRangeException("min_cell");
}
if ( max_cell.x > _gridNumPosCount.x || max_cell.y > _gridNumPosCount.y ) {
throw new System.ArgumentOutOfRangeException("max_cell");
}
if ( look_upper == null ) {
throw new System.ArgumentNullException("look_upper");
}
for ( int y = min_cell.y, ye = max_cell.y; y < ye; ++y ) {
for ( int x = min_cell.x, xe = max_cell.x; x < xe; ++x ) {
var cell = GetCell(x, y);
if ( cell.Items.Count > 0 ) {
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.Max(min_cell_size, _gridCellSize / _gridItems.Count)
: min_cell_size;
}
void PrepareGridNumPos() {
if ( _gridItems.Count > 0 ) {
_gridMinNumPos.Set(int.MaxValue, int.MaxValue);
_gridMaxNumPos.Set(int.MinValue, int.MinValue);
for ( int i = 0, e = _gridItems.Count; i < e; ++i ) {
var item = _gridItems[i];
var bounds = _itemAdapter.GetBounds(item);
var min_f_x = bounds.x.min / _gridCellSize;
var min_f_y = bounds.y.min / _gridCellSize;
var max_f_x = bounds.x.max / _gridCellSize;
var max_f_y = bounds.y.max / _gridCellSize;
var min_i_x = (int)(min_f_x >= 0.0f ? min_f_x : min_f_x - 1.0f);
var min_i_y = (int)(min_f_y >= 0.0f ? min_f_y : min_f_y - 1.0f);
var max_i_x = (int)(max_f_x >= 0.0f ? max_f_x + 1.0f : max_f_x);
var max_i_y = (int)(max_f_y >= 0.0f ? max_f_y + 1.0f : max_f_y);
if ( _gridMinNumPos.x > min_i_x ) {
_gridMinNumPos.x = min_i_x;
}
if ( _gridMinNumPos.y > min_i_y ) {
_gridMinNumPos.y = min_i_y;
}
if ( _gridMaxNumPos.x < max_i_x ) {
_gridMaxNumPos.x = max_i_x;
}
if ( _gridMaxNumPos.y < max_i_y ) {
_gridMaxNumPos.y = max_i_y;
}
_itemAdapter.SetMinMaxCells(
item,
new IsoPoint2(min_i_x, min_i_y),
new IsoPoint2(max_i_x, max_i_y));
}
} else {
_gridMinNumPos.Set(0, 0);
_gridMaxNumPos.Set(1, 1);
}
_gridNumPosCount = _gridMaxNumPos - _gridMinNumPos;
}
void SetupGridCells() {
var cell_count = _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 = IsoPoint2.zero;
var max_cell = IsoPoint2.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 ( int y = min_cell.y, ye = max_cell.y; y < ye; ++y ) {
for ( int x = min_cell.x, xe = max_cell.x; x < xe; ++x ) {
var cell = GetCell(x, y);
cell.Items.Add(item);
}}
}
}
Cell GetCell(int cell_x, int cell_y) {
var cell_index = cell_x + _gridNumPosCount.x * cell_y;
return _gridCells[cell_index];
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: bcb6f6ab1a08544cf8dfc30925adfa52
timeCreated: 1482441520
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -344,6 +344,20 @@ namespace IsoTools.Internal {
}
}
public void VisitItemsByContent(T content, IContentLookUpper look_upper) {
if ( content == null ) {
throw new System.ArgumentNullException("content");
}
if ( look_upper == null ) {
throw new System.ArgumentNullException("look_upper");
}
Item item;
if ( _allItems.TryGetValue(content, out item) ) {
item.Owner.VisitItemsByBounds(item.Bounds, look_upper);
BackwardVisitNodes(item.Owner.Parent, item.Bounds, look_upper);
}
}
// ---------------------------------------------------------------------
//
// Private
@@ -380,5 +394,17 @@ namespace IsoTools.Internal {
}
return node;
}
void BackwardVisitNodes(Node node, IsoRect bounds, IContentLookUpper loop_upper) {
while ( node != null ) {
for ( int i = 0, e = node.Items.Count; i < e; ++i ) {
var item = node.Items[i];
if ( bounds.Overlaps(item.Bounds) ) {
loop_upper.LookUp(item.Content);
}
}
node = node.Parent;
}
}
}
}

View File

@@ -11,19 +11,15 @@ using UnityEngine.Profiling;
namespace IsoTools.Internal {
public class IsoScreenSolver {
Vector2 _minIsoXY = Vector2.zero;
Vector2 _minIsoXY = Vector2.zero;
IsoAssocList<IsoObject> _oldVisibles = new IsoAssocList<IsoObject>();
IsoAssocList<IsoObject> _curVisibles = new IsoAssocList<IsoObject>();
IsoAssocList<IsoObject> _oldVisibles = new IsoAssocList<IsoObject>(47);
IsoAssocList<IsoObject> _curVisibles = new IsoAssocList<IsoObject>(47);
IsoQuadTree<IsoObject> _quadTree = new IsoQuadTree<IsoObject>(47);
IsoGrid<IsoObject> _screenGrid = new IsoGrid<IsoObject>(new IsoSGItemAdapter(), 47);
IsoQTBoundsLookUpper _qtBoundsLU = new IsoQTBoundsLookUpper();
IsoQTContentLookUpper _qtContentLU = new IsoQTContentLookUpper();
IsoSGBoundsLookUpper _sgBoundsLU = new IsoSGBoundsLookUpper();
IsoSGContentLookUpper _sgContentLU = new IsoSGContentLookUpper();
IsoQuadTree<IsoObject> _quadTree = new IsoQuadTree<IsoObject>(47);
IsoQTBoundsLookUpper _qtBoundsLU = new IsoQTBoundsLookUpper();
IsoQTDependsLookUpper _qtDependsLU = new IsoQTDependsLookUpper();
IsoQTVisibilityLookUpper _qtVisibilityLU = new IsoQTVisibilityLookUpper();
// ---------------------------------------------------------------------
//
@@ -44,11 +40,34 @@ namespace IsoTools.Internal {
// ---------------------------------------------------------------------
//
// IsoQTContentLookUpper
// IsoQTDependsLookUpper
//
// ---------------------------------------------------------------------
class IsoQTContentLookUpper : IsoQuadTree<IsoObject>.IContentLookUpper {
class IsoQTDependsLookUpper : IsoQuadTree<IsoObject>.IContentLookUpper {
IsoObject _isoObject;
public void LookUp(IsoObject other_iso_object) {
LookUpCellForLDepends(_isoObject, other_iso_object);
LookUpCellForRDepends(_isoObject, other_iso_object);
}
public void LookUpForDepends(
IsoScreenSolver screen_solver, IsoObject iso_object)
{
_isoObject = iso_object;
screen_solver._quadTree.VisitItemsByContent(iso_object, this);
_isoObject = null;
}
}
// ---------------------------------------------------------------------
//
// IsoQTVisibilityLookUpper
//
// ---------------------------------------------------------------------
class IsoQTVisibilityLookUpper : IsoQuadTree<IsoObject>.IContentLookUpper {
Camera[] _tmpCameras = new Camera[8];
IsoScreenSolver _screenSolver = null;
@@ -105,71 +124,6 @@ namespace IsoTools.Internal {
}
}
// ---------------------------------------------------------------------
//
// IsoSGItemAdapter
//
// ---------------------------------------------------------------------
class IsoSGItemAdapter : IsoGrid<IsoObject>.IItemAdapter {
public IsoRect GetBounds(IsoObject item) {
return item.Internal.ScreenBounds;
}
public void SetMinMaxCells(IsoObject item, IsoPoint2 min, IsoPoint2 max) {
item.Internal.MinGridCell = min;
item.Internal.MaxGridCell = max;
}
public void GetMinMaxCells(IsoObject item, ref IsoPoint2 min, ref IsoPoint2 max) {
min = item.Internal.MinGridCell;
max = item.Internal.MaxGridCell;
}
}
// ---------------------------------------------------------------------
//
// IsoSGBoundsLookUpper
//
// ---------------------------------------------------------------------
class IsoSGBoundsLookUpper : IsoGrid<IsoObject>.IBoundsLookUpper {
public void LookUp(IsoRect bounds) {
#if UNITY_EDITOR
IsoUtils.DrawSolidRect(
bounds,
IsoUtils.ColorChangeA(Color.green, 0.1f),
Color.green);
#endif
}
}
// ---------------------------------------------------------------------
//
// IsoSGContentLookUpper
//
// ---------------------------------------------------------------------
class IsoSGContentLookUpper : IsoGrid<IsoObject>.IContentLookUpper {
IsoObject _isoObject;
public void LookUp(IsoList<IsoObject> items) {
LookUpCellForLDepends(_isoObject, items);
LookUpCellForRDepends(_isoObject, items);
}
public void LookUpForDepends(
IsoScreenSolver screen_solver, IsoObject iso_object)
{
_isoObject = iso_object;
screen_solver._screenGrid.VisitItemsByCells(
iso_object.Internal.MinGridCell,
iso_object.Internal.MaxGridCell,
this);
_isoObject = null;
}
}
// ---------------------------------------------------------------------
//
// Properties
@@ -225,40 +179,33 @@ namespace IsoTools.Internal {
if ( iso_world.isShowQuadTree ) {
_quadTree.VisitAllBounds(_qtBoundsLU);
}
if ( iso_world.isShowScreenGrid ) {
_screenGrid.VisitAllBounds(_sgBoundsLU);
}
}
#endif
// ---------------------------------------------------------------------
//
// Functions
// Public
//
// ---------------------------------------------------------------------
public void StepSortingAction(IsoWorld iso_world, IsoAssocList<IsoObject> instances) {
Profiler.BeginSample("IsoScreenSolver.ResolveVisibles");
ResolveVisibles(iso_world, instances);
Profiler.BeginSample("IsoScreenSolver.ProcessInstances");
ProcessInstances(instances);
Profiler.EndSample();
Profiler.BeginSample("IsoScreenSolver.ResolveVisibleGrid");
ResolveVisibleGrid(iso_world);
Profiler.BeginSample("IsoScreenSolver.ProcessVisibles");
ProcessVisibles(iso_world.isSortInSceneView);
Profiler.EndSample();
}
public void PostStepSortingAction() {
}
public void Clear() {
_oldVisibles.Clear();
_curVisibles.Clear();
_quadTree.Clear();
_screenGrid.ClearGrid();
}
public void SetupIsoObjectDepends(IsoObject iso_object) {
ClearIsoObjectDepends(iso_object);
_sgContentLU.LookUpForDepends(this, iso_object);
_qtDependsLU.LookUpForDepends(this, iso_object);
}
public void ClearIsoObjectDepends(IsoObject iso_object) {
@@ -275,20 +222,11 @@ namespace IsoTools.Internal {
// ---------------------------------------------------------------------
//
// ResolveVisibles
// Private
//
// ---------------------------------------------------------------------
void ResolveVisibles(IsoWorld iso_world, IsoAssocList<IsoObject> instances) {
Profiler.BeginSample("ProcessAllInstances");
ProcessAllInstances(instances);
Profiler.EndSample();
Profiler.BeginSample("ProcessNewVisibles");
ProcessNewVisibles(iso_world.isSortInSceneView);
Profiler.EndSample();
}
void ProcessAllInstances(IsoAssocList<IsoObject> instances) {
void ProcessInstances(IsoAssocList<IsoObject> instances) {
if ( instances.Count > 0 ) {
for ( int i = 0, e = instances.Count; i < e; ++i ) {
var iso_object = instances[i];
@@ -302,9 +240,9 @@ namespace IsoTools.Internal {
}
}
void ProcessNewVisibles(bool include_scene_view) {
void ProcessVisibles(bool include_scene_view) {
_oldVisibles.Clear();
_qtContentLU.LookUpForVisibility(this, include_scene_view);
_qtVisibilityLU.LookUpForVisibility(this, include_scene_view);
SwapCurrentVisibles();
}
@@ -314,56 +252,32 @@ namespace IsoTools.Internal {
_oldVisibles = swap_tmp;
}
// ---------------------------------------------------------------------
//
// ResolveVisibleGrid
//
// ---------------------------------------------------------------------
void ResolveVisibleGrid(IsoWorld iso_world) {
_screenGrid.ClearItems();
for ( int i = 0, e = _curVisibles.Count; i < e; ++i ) {
var iso_object = _curVisibles[i];
_screenGrid.AddItem(iso_object);
}
var min_sector_size = IsoUtils.Vec2MaxF(
iso_world.IsoToScreen(IsoUtils.vec3OneXY) -
iso_world.IsoToScreen(Vector3.zero));
_screenGrid.RebuildGrid(min_sector_size);
}
// ---------------------------------------------------------------------
//
// LookUpCellForDepends
//
// ---------------------------------------------------------------------
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) ) {
obj_a.Internal.SelfDepends.Add(obj_b);
obj_b.Internal.TheirDepends.Add(obj_a);
}
static void LookUpCellForLDepends(IsoObject obj_a, IsoObject obj_b) {
if ( obj_a != obj_b &&
!obj_b.Internal.Dirty &&
IsIsoObjectDepends(obj_a.position, obj_a.size, obj_b.position, obj_b.size) )
{
obj_a.Internal.SelfDepends.Add(obj_b);
obj_b.Internal.TheirDepends.Add(obj_a);
}
}
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) ) {
obj_b.Internal.SelfDepends.Add(obj_a);
obj_a.Internal.TheirDepends.Add(obj_b);
}
static void LookUpCellForRDepends(IsoObject obj_a, IsoObject obj_b) {
if ( obj_a != obj_b &&
!obj_b.Internal.Dirty &&
IsIsoObjectDepends(obj_b.position, obj_b.size, obj_a.position, obj_a.size) )
{
obj_b.Internal.SelfDepends.Add(obj_a);
obj_a.Internal.TheirDepends.Add(obj_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);
}
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;

View File

@@ -51,17 +51,13 @@ namespace IsoTools.Internal {
var dirty = ResolveVisibles(screen_solver);
Profiler.EndSample();
if ( dirty ) {
Profiler.BeginSample("IsoSortingSolver.PlaceAllVisibles");
PlaceAllVisibles(iso_world, screen_solver);
Profiler.BeginSample("IsoSortingSolver.PlaceVisibles");
PlaceVisibles(iso_world, screen_solver);
Profiler.EndSample();
}
return dirty;
}
public void PostStepSortingAction() {
_tmpRenderers.Clear();
}
public void Clear() {
}
@@ -78,6 +74,7 @@ namespace IsoTools.Internal {
for ( int i = 0, e = cur_visibles.Count; i < e; ++i ) {
var iso_object = cur_visibles[i];
//TODO: remove `old_visibles.Contains`?
if ( iso_object.Internal.Dirty || !old_visibles.Contains(iso_object) ) {
mark_dirty = true;
screen_solver.SetupIsoObjectDepends(iso_object);
@@ -153,16 +150,17 @@ namespace IsoTools.Internal {
// ---------------------------------------------------------------------
//
// PlaceAllVisibles
// PlaceVisibles
//
// ---------------------------------------------------------------------
void PlaceAllVisibles(IsoWorld iso_world, IsoScreenSolver screen_solver) {
void PlaceVisibles(IsoWorld iso_world, IsoScreenSolver screen_solver) {
var step_depth = iso_world.stepDepth;
var start_depth = iso_world.startDepth;
var cur_visibles = screen_solver.curVisibles;
for ( int i = 0, e = cur_visibles.Count; i < e; ++i ) {
start_depth = RecursivePlaceIsoObject(
cur_visibles[i], start_depth, iso_world.stepDepth);
cur_visibles[i], start_depth, step_depth);
}
}

View File

@@ -194,8 +194,6 @@ namespace IsoTools {
public bool Dirty = false;
public bool Placed = false;
public IsoRect ScreenBounds = IsoRect.zero;
public IsoPoint2 MinGridCell = IsoPoint2.zero;
public IsoPoint2 MaxGridCell = IsoPoint2.zero;
public IsoMinMax MinMax3d = IsoMinMax.zero;
public float Offset3d = 0.0f;
public Transform Transform = null;

View File

@@ -323,11 +323,6 @@ namespace IsoTools {
get { return _showQuadTree; }
set { _showQuadTree = value; }
}
[SerializeField] bool _showScreenGrid = false;
public bool isShowScreenGrid {
get { return _showScreenGrid; }
set { _showScreenGrid = value; }
}
#endif
// ---------------------------------------------------------------------
@@ -385,8 +380,6 @@ namespace IsoTools {
if ( _sortingSolver.StepSortingAction(this, _screenSolver) ) {
MarkDirty();
}
_screenSolver.PostStepSortingAction();
_sortingSolver.PostStepSortingAction();
}
// ---------------------------------------------------------------------