micro optimizations

This commit is contained in:
2016-11-26 03:57:59 +07:00
parent e1c09e4552
commit 521c0d2ff2
3 changed files with 87 additions and 46 deletions

View File

@@ -13,19 +13,53 @@ namespace IsoTools.Internal {
// //
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
public static Vector2 vec2OneX { get { return new Vector2(1.0f, 0.0f); } } public static readonly Vector2 vec2OneX = new Vector2(1.0f, 0.0f);
public static Vector2 vec2OneY { get { return new Vector2(0.0f, 1.0f); } } public static readonly Vector2 vec2OneY = new Vector2(0.0f, 1.0f);
public static Vector2 vec2OneXY { get { return new Vector2(1.0f, 1.0f); } } public static readonly Vector3 vec3OneX = new Vector3(1.0f, 0.0f, 0.0f);
public static readonly Vector3 vec3OneY = new Vector3(0.0f, 1.0f, 0.0f);
public static Vector3 vec3OneX { get { return new Vector3(1.0f, 0.0f, 0.0f); } } public static readonly Vector3 vec3OneZ = new Vector3(0.0f, 0.0f, 1.0f);
public static Vector3 vec3OneY { get { return new Vector3(0.0f, 1.0f, 0.0f); } } public static readonly Vector3 vec3OneXY = new Vector3(1.0f, 1.0f, 0.0f);
public static Vector3 vec3OneZ { get { return new Vector3(0.0f, 0.0f, 1.0f); } } public static readonly Vector3 vec3OneYZ = new Vector3(0.0f, 1.0f, 1.0f);
public static Vector3 vec3OneXY { get { return new Vector3(1.0f, 1.0f, 0.0f); } } public static readonly Vector3 vec3OneXZ = new Vector3(1.0f, 0.0f, 1.0f);
public static Vector3 vec3OneYZ { get { return new Vector3(0.0f, 1.0f, 1.0f); } }
public static Vector3 vec3OneXZ { get { return new Vector3(1.0f, 0.0f, 1.0f); } }
static public readonly int FloatBeautifierDigits = 4; static public readonly int FloatBeautifierDigits = 4;
// ---------------------------------------------------------------------
//
// Rect
//
// ---------------------------------------------------------------------
public struct Rect {
public float xMin;
public float yMin;
public float xMax;
public float yMax;
public Rect(float xmin, float ymin, float width, float height) {
this.xMin = xmin;
this.yMin = ymin;
this.xMax = xmin + width;
this.yMax = ymin + height;
}
public Vector2 size {
get {
return new Vector2(xMax - xMin, yMax - yMin);
}
}
public bool Overlaps(Rect other) {
return
xMax > other.xMin && xMin < other.xMax &&
yMax > other.yMin && yMin < other.yMax;
}
static public Rect zero {
get { return new Rect(); }
}
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// //
// MinMax // MinMax
@@ -40,7 +74,7 @@ namespace IsoTools.Internal {
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
public float size { public float size {
get { return max - min; } get { return max - min; }
} }

View File

@@ -194,7 +194,7 @@ namespace IsoTools {
public class InternalState { public class InternalState {
public bool Dirty = false; public bool Dirty = false;
public bool Placed = false; public bool Placed = false;
public Rect ScreenRect = new Rect(); public IsoUtils.Rect ScreenRect = IsoUtils.Rect.zero;
public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero; public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero;
public float Offset3d = 0.0f; public float Offset3d = 0.0f;
public Vector2 MinSector = Vector2.zero; public Vector2 MinSector = Vector2.zero;
@@ -275,7 +275,7 @@ namespace IsoTools {
var r = isoWorld.IsoToScreen(position + IsoUtils.Vec3FromX(size.x)).x; var r = isoWorld.IsoToScreen(position + IsoUtils.Vec3FromX(size.x)).x;
var b = isoWorld.IsoToScreen(position).y; var b = isoWorld.IsoToScreen(position).y;
var t = isoWorld.IsoToScreen(position + size).y; var t = isoWorld.IsoToScreen(position + size).y;
Internal.ScreenRect = new Rect(l, b, r - l, t - b); Internal.ScreenRect = new IsoUtils.Rect(l, b, r - l, t - b);
} }
} }

View File

@@ -559,17 +559,19 @@ namespace IsoTools {
var a_max = a_min + a_size; var a_max = a_min + a_size;
var b_max = b_min + b_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; var a_yesno = a_max.x > b_min.x && a_max.y > b_min.y && b_max.z > a_min.z;
var b_yesno = b_max.x > a_min.x && b_max.y > a_min.y && a_max.z > b_min.z; if ( a_yesno ) {
if ( a_yesno && b_yesno ) { var b_yesno = b_max.x > a_min.x && b_max.y > a_min.y && a_max.z > b_min.z;
var da_p = new Vector3(a_max.x - b_min.x, a_max.y - b_min.y, b_max.z - a_min.z); if ( b_yesno ) {
var db_p = new Vector3(b_max.x - a_min.x, b_max.y - a_min.y, a_max.z - b_min.z); var da_p = new Vector3(a_max.x - b_min.x, a_max.y - b_min.y, b_max.z - a_min.z);
var dp_p = a_size + b_size - IsoUtils.Vec3Abs(da_p - db_p); var db_p = new Vector3(b_max.x - a_min.x, b_max.y - a_min.y, a_max.z - b_min.z);
if ( dp_p.x <= dp_p.y && dp_p.x <= dp_p.z ) { var dp_p = a_size + b_size - IsoUtils.Vec3Abs(da_p - db_p);
return da_p.x > db_p.x; if ( dp_p.x <= dp_p.y && dp_p.x <= dp_p.z ) {
} else if ( dp_p.y <= dp_p.x && dp_p.y <= dp_p.z ) { return da_p.x > db_p.x;
return da_p.y > db_p.y; } else if ( dp_p.y <= dp_p.x && dp_p.y <= dp_p.z ) {
} else { return da_p.y > db_p.y;
return da_p.z > db_p.z; } else {
return da_p.z > db_p.z;
}
} }
} }
return a_yesno; return a_yesno;
@@ -577,23 +579,23 @@ namespace IsoTools {
bool IsIsoObjectDepends(IsoObject a, IsoObject b) { bool IsIsoObjectDepends(IsoObject a, IsoObject b) {
return return
a.Internal.ScreenRect.Overlaps(b.Internal.ScreenRect, false) && a.Internal.ScreenRect.Overlaps(b.Internal.ScreenRect) &&
IsIsoObjectDepends(a.position, a.size, b.position, b.size); IsIsoObjectDepends(a.position, a.size, b.position, b.size);
} }
Sector FindSector(Vector2 num_pos) { Sector FindSector(float num_pos_x, float num_pos_y) {
if ( num_pos.x < 0 || num_pos.y < 0 ) { if ( num_pos_x < 0 || num_pos_y < 0 ) {
return null; return null;
} }
if ( num_pos.x >= _sectorsNumPosCount.x || num_pos.y >= _sectorsNumPosCount.y ) { if ( num_pos_x >= _sectorsNumPosCount.x || num_pos_y >= _sectorsNumPosCount.y ) {
return null; return null;
} }
var sector_index = Mathf.FloorToInt(num_pos.x + _sectorsNumPosCount.x * num_pos.y); var sector_index = (int)(num_pos_x + _sectorsNumPosCount.x * num_pos_y);
return _sectors[sector_index]; return _sectors[sector_index];
} }
void LookUpSectorDepends(Vector2 num_pos, IsoObject obj_a) { void LookUpSectorDepends(float num_pos_x, float num_pos_y, IsoObject obj_a) {
var sec = FindSector(num_pos); var sec = FindSector(num_pos_x, num_pos_y);
if ( sec != null ) { if ( sec != null ) {
for ( int i = 0, e = sec.objects.Count; i < e; ++i ) { for ( int i = 0, e = sec.objects.Count; i < e; ++i ) {
var obj_b = sec.objects[i]; var obj_b = sec.objects[i];
@@ -605,8 +607,8 @@ namespace IsoTools {
} }
} }
void LookUpSectorRDepends(Vector2 num_pos, IsoObject obj_a) { void LookUpSectorRDepends(float num_pos_x, float num_pos_y, IsoObject obj_a) {
var sec = FindSector(num_pos); var sec = FindSector(num_pos_x, num_pos_y);
if ( sec != null ) { if ( sec != null ) {
for ( int i = 0, e = sec.objects.Count; i < e; ++i ) { for ( int i = 0, e = sec.objects.Count; i < e; ++i ) {
var obj_b = sec.objects[i]; var obj_b = sec.objects[i];
@@ -633,8 +635,8 @@ namespace IsoTools {
void SetupObjectsSectors() { void SetupObjectsSectors() {
if ( _visibles.Count > 0 ) { if ( _visibles.Count > 0 ) {
_sectorsMinNumPos = new Vector2(float.MaxValue, float.MaxValue); _sectorsMinNumPos.Set(float.MaxValue, float.MaxValue);
_sectorsMaxNumPos = new Vector2(float.MinValue, float.MinValue); _sectorsMaxNumPos.Set(float.MinValue, float.MinValue);
for ( int i = 0, e = _visibles.Count; i < e; ++i ) { for ( int i = 0, e = _visibles.Count; i < e; ++i ) {
var iso_internal = _visibles[i].Internal; var iso_internal = _visibles[i].Internal;
@@ -661,8 +663,8 @@ namespace IsoTools {
} }
} }
} else { } else {
_sectorsMinNumPos = Vector2.zero; _sectorsMinNumPos.Set(0.0f, 0.0f);
_sectorsMaxNumPos = new Vector2(_sectorsSize, _sectorsSize); _sectorsMaxNumPos.Set(_sectorsSize, _sectorsSize);
} }
_sectorsNumPosCount = _sectorsMaxNumPos - _sectorsMinNumPos; _sectorsNumPosCount = _sectorsMaxNumPos - _sectorsMinNumPos;
} }
@@ -690,7 +692,7 @@ namespace IsoTools {
var max = iso_object.Internal.MaxSector; var max = iso_object.Internal.MaxSector;
for ( var y = min.y; y < max.y; ++y ) { for ( var y = min.y; y < max.y; ++y ) {
for ( var x = min.x; x < max.x; ++x ) { for ( var x = min.x; x < max.x; ++x ) {
var sector = FindSector(new Vector2(x, y)); var sector = FindSector(x, y);
if ( sector != null ) { if ( sector != null ) {
sector.objects.Push(iso_object); sector.objects.Push(iso_object);
} }
@@ -699,7 +701,7 @@ namespace IsoTools {
} }
void SetupSectors() { void SetupSectors() {
ResizeSectors(Mathf.FloorToInt(_sectorsNumPosCount.x * _sectorsNumPosCount.y)); ResizeSectors((int)(_sectorsNumPosCount.x * _sectorsNumPosCount.y));
TuneSectors(); TuneSectors();
} }
@@ -739,10 +741,16 @@ namespace IsoTools {
void CalculateNewVisibles() { void CalculateNewVisibles() {
_oldVisibles.Clear(); _oldVisibles.Clear();
if ( _objects.Count > 0 ) { if ( _objects.Count > 0 ) {
_minXY = new Vector2(float.MaxValue, float.MaxValue); _minXY.Set(float.MaxValue, float.MaxValue);
for ( int i = 0, e = _objects.Count; i < e; ++i ) { for ( int i = 0, e = _objects.Count; i < e; ++i ) {
var iso_object = _objects[i]; var iso_object = _objects[i];
_minXY = IsoUtils.Vec2Min(_minXY, iso_object.position); var iso_object_pos = iso_object.position;
if ( _minXY.x > iso_object_pos.x ) {
_minXY.x = iso_object_pos.x;
}
if ( _minXY.y > iso_object_pos.y ) {
_minXY.y = iso_object_pos.y;
}
if ( !IsoUtils.Vec2Approximately( if ( !IsoUtils.Vec2Approximately(
iso_object.Internal.LastTrans, iso_object.Internal.LastTrans,
iso_object.Internal.Transform.position) ) iso_object.Internal.Transform.position) )
@@ -755,7 +763,7 @@ namespace IsoTools {
} }
} }
} else { } else {
_minXY = Vector2.zero; _minXY.Set(0.0f, 0.0f);
} }
var old_visibles = _visibles; var old_visibles = _visibles;
_visibles = _oldVisibles; _visibles = _oldVisibles;
@@ -801,9 +809,8 @@ namespace IsoTools {
var max = obj_a.Internal.MaxSector; var max = obj_a.Internal.MaxSector;
for ( var y = min.y; y < max.y; ++y ) { for ( var y = min.y; y < max.y; ++y ) {
for ( var x = min.x; x < max.x; ++x ) { for ( var x = min.x; x < max.x; ++x ) {
var v = new Vector2(x, y); LookUpSectorDepends(x, y, obj_a);
LookUpSectorDepends(v, obj_a); LookUpSectorRDepends(x, y, obj_a);
LookUpSectorRDepends(v, obj_a);
}} }}
} }