diff --git a/Assets/IsoTools/Scripts/Internal/IsoUtils.cs b/Assets/IsoTools/Scripts/Internal/IsoUtils.cs index 03ed446..59beb46 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoUtils.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoUtils.cs @@ -24,6 +24,40 @@ namespace IsoTools.Internal { 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); } } + // --------------------------------------------------------------------- + // + // MinMax + // + // --------------------------------------------------------------------- + + public struct MinMax { + public float min; + public float max; + + public MinMax(float min, float max) { + this.min = min; + this.max = max; + } + + public float size { + get { return max - min; } + } + + public float center { + get { return min / 2.0f + max / 2.0f; } + } + + public bool Approximately(MinMax minmax) { + return + Mathf.Approximately(min, minmax.min) && + Mathf.Approximately(max, minmax.max); + } + + static public MinMax zero { + get { return new MinMax(); } + } + } + // --------------------------------------------------------------------- // // Abs/Min/Max @@ -477,7 +511,7 @@ namespace IsoTools.Internal { public static IsoContactPoint[] IsoConvertContactPoints(ContactPoint[] points) { var iso_points = new IsoContactPoint[points.Length]; - for ( var i = 0; i < points.Length; ++i ) { + for ( int i = 0, e = points.Length; i < e; ++i ) { iso_points[i] = new IsoContactPoint(points[i]); } return iso_points; @@ -485,7 +519,7 @@ namespace IsoTools.Internal { public static IsoRaycastHit[] IsoConvertRaycastHits(RaycastHit[] hits) { var iso_hits = new IsoRaycastHit[hits.Length]; - for ( var i = 0; i < hits.Length; ++i ) { + for ( int i = 0, e = hits.Length; i < e; ++i ) { iso_hits[i] = new IsoRaycastHit(hits[i]); } return iso_hits; diff --git a/Assets/IsoTools/Scripts/IsoObject.cs b/Assets/IsoTools/Scripts/IsoObject.cs index 55f171b..ab1eb14 100644 --- a/Assets/IsoTools/Scripts/IsoObject.cs +++ b/Assets/IsoTools/Scripts/IsoObject.cs @@ -168,8 +168,8 @@ namespace IsoTools { public bool Dirty = false; public bool Visited = false; public Rect ScreenRect = new Rect(); - public Bounds Bounds3d = new Bounds(); public float Offset3d = 0.0f; + public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero; public Vector2 MinSector = Vector2.zero; public Vector2 MaxSector = Vector2.zero; public HashSet SelfDepends = new HashSet(); diff --git a/Assets/IsoTools/Scripts/IsoWorld.cs b/Assets/IsoTools/Scripts/IsoWorld.cs index dfcf9f8..2e66956 100644 --- a/Assets/IsoTools/Scripts/IsoWorld.cs +++ b/Assets/IsoTools/Scripts/IsoWorld.cs @@ -343,34 +343,49 @@ namespace IsoTools { bool UpdateIsoObjectBounds3d(IsoObject iso_object) { if ( iso_object.mode == IsoObject.Mode.Mode3d ) { - var bounds3d = IsoObject3DBounds(iso_object); - var offset3d = iso_object.transform.position.z - bounds3d.center.z; - if ( iso_object.Internal.Bounds3d.extents != bounds3d.extents || - !Mathf.Approximately(iso_object.Internal.Offset3d, offset3d) ) + var minmax3d = IsoObjectMinMax3D(iso_object); + var offset3d = iso_object.transform.position.z - minmax3d.center; + if ( !Mathf.Approximately(iso_object.Internal.Offset3d, offset3d) || + iso_object.Internal.MinMax3d.Approximately(minmax3d) ) { - iso_object.Internal.Bounds3d = bounds3d; iso_object.Internal.Offset3d = offset3d; + iso_object.Internal.MinMax3d = minmax3d; return true; } } return false; } - - Bounds IsoObject3DBounds(IsoObject iso_object) { - var bounds = new Bounds(); + + IsoUtils.MinMax IsoObjectMinMax3D(IsoObject iso_object) { iso_object.GetComponentsInChildren(_tmpRenderers); - if ( _tmpRenderers.Count > 0 ) { - bounds = _tmpRenderers[0].bounds; - for ( var i = 1; i < _tmpRenderers.Count; ++i ) { - bounds.Encapsulate(_tmpRenderers[i].bounds); + if ( _tmpRenderers.Count < 1 ) { + return IsoUtils.MinMax.zero; + } + var bounds = _tmpRenderers[0].bounds; + var center = bounds.center.z; + var extent = bounds.extents.z; + var minbounds = center - extent; + var maxbounds = center + extent; + var minmax = new IsoUtils.MinMax(center - extent, center + extent); + for ( int i = 1, e = _tmpRenderers.Count; i < e; ++i ) { + bounds = _tmpRenderers[i].bounds; + center = bounds.center.z; + extent = bounds.extents.z; + minbounds = center - extent; + maxbounds = center + extent; + if ( minbounds < minmax.min ) { + minmax.min = minbounds; + } + if ( maxbounds > minmax.max ) { + minmax.max = maxbounds; } } - return bounds; + return minmax; } bool IsIsoObjectVisible(IsoObject iso_object) { iso_object.GetComponentsInChildren(_tmpRenderers); - for ( var i = 0; i < _tmpRenderers.Count; ++i ) { + for ( int i = 0, e = _tmpRenderers.Count; i < e; ++i ) { if ( _tmpRenderers[i].isVisible ) { return true; } @@ -617,10 +632,9 @@ namespace IsoTools { while ( self_depends_iter.MoveNext() ) { depth = RecursivePlaceIsoObject(self_depends_iter.Current, depth); } - if ( iso_object.mode == IsoObject.Mode.Mode3d ) { var zoffset = iso_object.Internal.Offset3d; - var extents = iso_object.Internal.Bounds3d.extents.z; + var extents = iso_object.Internal.MinMax3d.size; PlaceIsoObject(iso_object, depth + extents + zoffset); return depth + extents * 2.0f + stepDepth; } else { diff --git a/Assets/IsoTools/Tiled/TiledMapProperties.cs b/Assets/IsoTools/Tiled/TiledMapProperties.cs index c487ad9..069cdb5 100644 --- a/Assets/IsoTools/Tiled/TiledMapProperties.cs +++ b/Assets/IsoTools/Tiled/TiledMapProperties.cs @@ -24,7 +24,7 @@ namespace IsoTools.Tiled { public bool Has(string property_name) { if ( _properties != null ) { - for ( var i = 0; i < _properties.Count / 2; ++i ) { + for ( int i = 0, e = _properties.Count / 2; i < e; ++i ) { if ( _properties[i * 2] == property_name ) { return true; } @@ -165,7 +165,7 @@ namespace IsoTools.Tiled { public bool TryGetAsString(string property_name, out string value) { if ( _properties != null ) { - for ( var i = 0; i < _properties.Count / 2; ++i ) { + for ( int i = 0, e = _properties.Count / 2; i < e; ++i ) { if ( _properties[i * 2] == property_name ) { value = _properties[i * 2 + 1]; return true;